Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 1 | #ifndef FRC971_CONTROL_LOOPS_DRIVETRAIN_POLYDRIVETRAIN_H_ |
| 2 | #define FRC971_CONTROL_LOOPS_DRIVETRAIN_POLYDRIVETRAIN_H_ |
| 3 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 4 | #include "aos/commonmath.h" |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 5 | #include "frc971/control_loops/coerce_goal.h" |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 6 | #include "frc971/control_loops/drivetrain/gear.h" |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 7 | #include "frc971/control_loops/polytope.h" |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 8 | #ifdef __linux__ |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 9 | #include "aos/logging/logging.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 10 | #include "frc971/control_loops/control_loops_generated.h" |
| 11 | #include "frc971/control_loops/drivetrain/drivetrain_goal_generated.h" |
| 12 | #include "frc971/control_loops/drivetrain/drivetrain_output_generated.h" |
| 13 | #include "frc971/control_loops/drivetrain/drivetrain_position_generated.h" |
| 14 | #include "frc971/control_loops/drivetrain/drivetrain_status_generated.h" |
James Kuszmaul | 7077d34 | 2021-06-09 20:23:58 -0700 | [diff] [blame] | 15 | #include "frc971/input/robot_state_generated.h" |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 16 | #else |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 17 | #include "frc971/control_loops/drivetrain/drivetrain_goal_float_generated.h" |
| 18 | #include "frc971/control_loops/drivetrain/drivetrain_output_float_generated.h" |
| 19 | #include "frc971/control_loops/drivetrain/drivetrain_position_float_generated.h" |
| 20 | #include "frc971/control_loops/drivetrain/drivetrain_status_float_generated.h" |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 21 | #endif // __linux__ |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 22 | #include "frc971/control_loops/drivetrain/drivetrain_config.h" |
Austin Schuh | 95771d9 | 2021-01-23 14:42:25 -0800 | [diff] [blame] | 23 | #include "frc971/control_loops/drivetrain/drivetrain_states.h" |
Lee Mracek | 8ae6c92 | 2020-01-21 21:55:17 -0500 | [diff] [blame] | 24 | #include "frc971/control_loops/state_feedback_loop.h" |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 25 | |
Stephan Pleines | d99b1ee | 2024-02-02 20:56:44 -0800 | [diff] [blame] | 26 | namespace frc971::control_loops::drivetrain { |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 27 | |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 28 | template <typename Scalar = double> |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 29 | class PolyDrivetrain { |
| 30 | public: |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 31 | PolyDrivetrain(const DrivetrainConfig<Scalar> &dt_config, |
| 32 | StateFeedbackLoop<7, 2, 4, Scalar> *kf); |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 33 | |
Austin Schuh | c5fceb8 | 2017-02-25 16:24:12 -0800 | [diff] [blame] | 34 | int controller_index() const { return loop_->index(); } |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 35 | |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 36 | // Computes the speed of the motor given the hall effect position and the |
| 37 | // speed of the robot. |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 38 | Scalar MotorSpeed(const constants::ShifterHallEffect &hall_effect, |
| 39 | Scalar shifter_position, Scalar velocity, Gear gear); |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 40 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 41 | void SetGoal(const Scalar wheel, const Scalar throttle, const bool quickturn, |
| 42 | const bool highgear); |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 43 | |
| 44 | void SetPosition( |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 45 | const ::frc971::control_loops::drivetrain::Position *position, |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 46 | Gear left_gear, Gear right_gear); |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 47 | |
Lee Mracek | 8ae6c92 | 2020-01-21 21:55:17 -0500 | [diff] [blame] | 48 | Scalar FilterVelocity(Scalar throttle, |
| 49 | const Eigen::Matrix<Scalar, 2, 2> &FF) const; |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 50 | |
Lee Mracek | 8ae6c92 | 2020-01-21 21:55:17 -0500 | [diff] [blame] | 51 | Scalar MaxVelocity(const Eigen::Matrix<Scalar, 2, 2> &FF); |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 52 | |
Austin Schuh | eeec74a | 2019-01-27 20:58:59 -0800 | [diff] [blame] | 53 | void Update(Scalar voltage_battery); |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 54 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 55 | void SetOutput(::frc971::control_loops::drivetrain::OutputT *output); |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 56 | |
James Kuszmaul | af5dfad | 2020-01-03 20:02:54 -0800 | [diff] [blame] | 57 | flatbuffers::Offset<PolyDriveLogging> PopulateStatus( |
| 58 | flatbuffers::FlatBufferBuilder *fbb); |
| 59 | |
| 60 | flatbuffers::Offset<CIMLogging> PopulateShiftingStatus( |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 61 | flatbuffers::FlatBufferBuilder *fbb); |
Austin Schuh | 4156560 | 2016-02-28 20:10:49 -0800 | [diff] [blame] | 62 | |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 63 | // Computes the next state of a shifter given the current state and the |
| 64 | // requested state. |
| 65 | Gear UpdateSingleGear(Gear requested_gear, Gear current_gear); |
| 66 | |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 67 | // Returns the current estimated velocity in m/s. |
| 68 | Scalar velocity() const { |
Brian Silverman | 342d4dc | 2018-09-02 16:26:01 -0700 | [diff] [blame] | 69 | return (loop_->mutable_X_hat()(0) + loop_->mutable_X_hat()(1)) * kHalf; |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 72 | private: |
Brian Silverman | 342d4dc | 2018-09-02 16:26:01 -0700 | [diff] [blame] | 73 | static constexpr Scalar kZero = static_cast<Scalar>(0.0); |
| 74 | static constexpr Scalar kHalf = static_cast<Scalar>(0.5); |
| 75 | static constexpr Scalar kOne = static_cast<Scalar>(1.0); |
| 76 | static constexpr Scalar kTwo = static_cast<Scalar>(2.0); |
| 77 | static constexpr Scalar kTwelve = static_cast<Scalar>(12.0); |
| 78 | |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 79 | StateFeedbackLoop<7, 2, 4, Scalar> *kf_; |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 80 | |
James Kuszmaul | 6175066 | 2021-06-21 21:32:33 -0700 | [diff] [blame] | 81 | const ::frc971::controls::HVPolytope<2, 4, 4, Scalar> U_Poly_; |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 82 | |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 83 | ::std::unique_ptr<StateFeedbackLoop<2, 2, 2, Scalar>> loop_; |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 84 | |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 85 | const Scalar ttrust_; |
| 86 | Scalar wheel_; |
| 87 | Scalar throttle_; |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 88 | bool quickturn_; |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 89 | |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 90 | Gear left_gear_; |
| 91 | Gear right_gear_; |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 92 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 93 | ::frc971::control_loops::drivetrain::PositionT last_position_; |
| 94 | ::frc971::control_loops::drivetrain::PositionT position_; |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 95 | int counter_; |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 96 | DrivetrainConfig<Scalar> dt_config_; |
Austin Schuh | 4156560 | 2016-02-28 20:10:49 -0800 | [diff] [blame] | 97 | |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 98 | Scalar goal_left_velocity_ = 0.0; |
| 99 | Scalar goal_right_velocity_ = 0.0; |
Kyle Stachowicz | 2f3c20f | 2017-07-13 16:04:05 -0700 | [diff] [blame] | 100 | |
| 101 | // Stored from the last iteration, for logging shifting logic. |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 102 | Scalar left_motor_speed_ = 0.0; |
| 103 | Scalar right_motor_speed_ = 0.0; |
| 104 | Scalar current_left_velocity_ = 0.0; |
| 105 | Scalar current_right_velocity_ = 0.0; |
James Kuszmaul | af5dfad | 2020-01-03 20:02:54 -0800 | [diff] [blame] | 106 | |
| 107 | // Feedforward voltage, for logging. |
| 108 | Eigen::Matrix<Scalar, 2, 1> ff_volts_{0.0, 0.0}; |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 109 | }; |
| 110 | |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 111 | template <typename Scalar> |
| 112 | PolyDrivetrain<Scalar>::PolyDrivetrain( |
| 113 | const DrivetrainConfig<Scalar> &dt_config, |
| 114 | StateFeedbackLoop<7, 2, 4, Scalar> *kf) |
| 115 | : kf_(kf), |
| 116 | U_Poly_((Eigen::Matrix<Scalar, 4, 2>() << /*[[*/ 1, 0 /*]*/, |
| 117 | /*[*/ -1, 0 /*]*/, |
| 118 | /*[*/ 0, 1 /*]*/, |
| 119 | /*[*/ 0, -1 /*]]*/) |
| 120 | .finished(), |
| 121 | (Eigen::Matrix<Scalar, 4, 1>() << /*[[*/ 12 /*]*/, |
| 122 | /*[*/ 12 /*]*/, |
| 123 | /*[*/ 12 /*]*/, |
| 124 | /*[*/ 12 /*]]*/) |
| 125 | .finished(), |
| 126 | (Eigen::Matrix<Scalar, 2, 4>() << /*[[*/ 12, 12, -12, -12 /*]*/, |
| 127 | /*[*/ -12, 12, 12, -12 /*]*/) |
| 128 | .finished()), |
| 129 | loop_(new StateFeedbackLoop<2, 2, 2, Scalar>( |
| 130 | dt_config.make_v_drivetrain_loop())), |
| 131 | ttrust_(1.1), |
| 132 | wheel_(0.0), |
| 133 | throttle_(0.0), |
| 134 | quickturn_(false), |
| 135 | left_gear_(dt_config.default_high_gear ? Gear::HIGH : Gear::LOW), |
| 136 | right_gear_(dt_config.default_high_gear ? Gear::HIGH : Gear::LOW), |
| 137 | counter_(0), |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 138 | dt_config_(dt_config) {} |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 139 | |
| 140 | template <typename Scalar> |
| 141 | Scalar PolyDrivetrain<Scalar>::MotorSpeed( |
| 142 | const constants::ShifterHallEffect &hall_effect, Scalar shifter_position, |
| 143 | Scalar velocity, Gear gear) { |
| 144 | const Scalar high_gear_speed = |
Brian Silverman | 342d4dc | 2018-09-02 16:26:01 -0700 | [diff] [blame] | 145 | velocity / |
| 146 | static_cast<Scalar>(dt_config_.high_gear_ratio / dt_config_.wheel_radius); |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 147 | const Scalar low_gear_speed = |
Brian Silverman | 342d4dc | 2018-09-02 16:26:01 -0700 | [diff] [blame] | 148 | velocity / |
| 149 | static_cast<Scalar>(dt_config_.low_gear_ratio / dt_config_.wheel_radius); |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 150 | |
James Kuszmaul | 6802533 | 2024-01-20 17:06:02 -0800 | [diff] [blame] | 151 | if (shifter_position < static_cast<Scalar>(hall_effect.clear_low())) { |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 152 | // We're in low gear, so return speed for that gear. |
| 153 | return low_gear_speed; |
James Kuszmaul | 6802533 | 2024-01-20 17:06:02 -0800 | [diff] [blame] | 154 | } else if (shifter_position > static_cast<Scalar>(hall_effect.clear_high())) { |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 155 | // We're in high gear, so return speed for that gear. |
| 156 | return high_gear_speed; |
| 157 | } |
| 158 | |
| 159 | // Not in gear, so speed-match to destination gear. |
| 160 | switch (gear) { |
| 161 | case Gear::HIGH: |
| 162 | case Gear::SHIFTING_UP: |
| 163 | return high_gear_speed; |
| 164 | case Gear::LOW: |
| 165 | case Gear::SHIFTING_DOWN: |
| 166 | default: |
| 167 | return low_gear_speed; |
| 168 | break; |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | template <typename Scalar> |
| 173 | Gear PolyDrivetrain<Scalar>::UpdateSingleGear(Gear requested_gear, |
| 174 | Gear current_gear) { |
| 175 | const Gear shift_up = |
James Kuszmaul | 6802533 | 2024-01-20 17:06:02 -0800 | [diff] [blame] | 176 | (dt_config_.shifter_type == ShifterType::kHallEffectShifter) |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 177 | ? Gear::SHIFTING_UP |
| 178 | : Gear::HIGH; |
| 179 | const Gear shift_down = |
James Kuszmaul | 6802533 | 2024-01-20 17:06:02 -0800 | [diff] [blame] | 180 | (dt_config_.shifter_type == ShifterType::kHallEffectShifter) |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 181 | ? Gear::SHIFTING_DOWN |
| 182 | : Gear::LOW; |
| 183 | if (current_gear != requested_gear) { |
| 184 | if (IsInGear(current_gear)) { |
| 185 | if (requested_gear == Gear::HIGH) { |
| 186 | if (current_gear != Gear::HIGH) { |
| 187 | current_gear = shift_up; |
| 188 | } |
| 189 | } else { |
| 190 | if (current_gear != Gear::LOW) { |
| 191 | current_gear = shift_down; |
| 192 | } |
| 193 | } |
| 194 | } else { |
| 195 | if (requested_gear == Gear::HIGH && current_gear == Gear::SHIFTING_DOWN) { |
| 196 | current_gear = Gear::SHIFTING_UP; |
| 197 | } else if (requested_gear == Gear::LOW && |
| 198 | current_gear == Gear::SHIFTING_UP) { |
| 199 | current_gear = Gear::SHIFTING_DOWN; |
| 200 | } |
| 201 | } |
| 202 | } |
| 203 | return current_gear; |
| 204 | } |
| 205 | |
| 206 | template <typename Scalar> |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 207 | void PolyDrivetrain<Scalar>::SetGoal(const Scalar wheel, const Scalar throttle, |
| 208 | const bool quickturn, |
| 209 | const bool highgear) { |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 210 | // Apply a sin function that's scaled to make it feel better. |
Brian Silverman | 342d4dc | 2018-09-02 16:26:01 -0700 | [diff] [blame] | 211 | const Scalar angular_range = |
| 212 | static_cast<Scalar>(M_PI_2) * dt_config_.wheel_non_linearity; |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 213 | |
| 214 | wheel_ = sin(angular_range * wheel) / sin(angular_range); |
| 215 | wheel_ = sin(angular_range * wheel_) / sin(angular_range); |
Brian Silverman | 342d4dc | 2018-09-02 16:26:01 -0700 | [diff] [blame] | 216 | wheel_ = kTwo * wheel - wheel_; |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 217 | quickturn_ = quickturn; |
| 218 | |
| 219 | if (quickturn_) { |
| 220 | wheel_ *= dt_config_.quickturn_wheel_multiplier; |
| 221 | } else { |
| 222 | wheel_ *= dt_config_.wheel_multiplier; |
| 223 | } |
| 224 | |
Brian Silverman | 342d4dc | 2018-09-02 16:26:01 -0700 | [diff] [blame] | 225 | static constexpr Scalar kThrottleDeadband = static_cast<Scalar>(0.05); |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 226 | if (::std::abs(throttle) < kThrottleDeadband) { |
| 227 | throttle_ = 0; |
| 228 | } else { |
| 229 | throttle_ = copysign( |
Brian Silverman | 342d4dc | 2018-09-02 16:26:01 -0700 | [diff] [blame] | 230 | (::std::abs(throttle) - kThrottleDeadband) / (kOne - kThrottleDeadband), |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 231 | throttle); |
| 232 | } |
| 233 | |
| 234 | Gear requested_gear = highgear ? Gear::HIGH : Gear::LOW; |
| 235 | |
| 236 | left_gear_ = UpdateSingleGear(requested_gear, left_gear_); |
| 237 | right_gear_ = UpdateSingleGear(requested_gear, right_gear_); |
| 238 | } |
| 239 | |
| 240 | template <typename Scalar> |
| 241 | void PolyDrivetrain<Scalar>::SetPosition( |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 242 | const ::frc971::control_loops::drivetrain::Position *position, |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 243 | Gear left_gear, Gear right_gear) { |
| 244 | left_gear_ = left_gear; |
| 245 | right_gear_ = right_gear; |
| 246 | last_position_ = position_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 247 | position->UnPackTo(&position_); |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | template <typename Scalar> |
Lee Mracek | 8ae6c92 | 2020-01-21 21:55:17 -0500 | [diff] [blame] | 251 | Scalar PolyDrivetrain<Scalar>::FilterVelocity( |
| 252 | Scalar throttle, const Eigen::Matrix<Scalar, 2, 2> &FF) const { |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 253 | constexpr int kHighGearController = 3; |
| 254 | const Eigen::Matrix<Scalar, 2, 2> FF_high = |
| 255 | loop_->plant().coefficients(kHighGearController).B.inverse() * |
| 256 | (Eigen::Matrix<Scalar, 2, 2>::Identity() - |
| 257 | loop_->plant().coefficients(kHighGearController).A); |
| 258 | |
| 259 | ::Eigen::Matrix<Scalar, 1, 2> FF_sum = FF.colwise().sum(); |
| 260 | int min_FF_sum_index; |
| 261 | const Scalar min_FF_sum = FF_sum.minCoeff(&min_FF_sum_index); |
| 262 | const Scalar min_K_sum = loop_->controller().K().col(min_FF_sum_index).sum(); |
| 263 | const Scalar high_min_FF_sum = FF_high.col(0).sum(); |
| 264 | |
Lee Mracek | 8ae6c92 | 2020-01-21 21:55:17 -0500 | [diff] [blame] | 265 | const Scalar adjusted_ff_voltage = ::aos::Clip( |
| 266 | throttle * kTwelve * min_FF_sum / high_min_FF_sum, -kTwelve, kTwelve); |
| 267 | return (adjusted_ff_voltage + ttrust_ * min_K_sum * |
| 268 | (loop_->X_hat(0, 0) + loop_->X_hat(1, 0)) * |
| 269 | kHalf) / |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 270 | (ttrust_ * min_K_sum + min_FF_sum); |
| 271 | } |
| 272 | |
| 273 | template <typename Scalar> |
Lee Mracek | 8ae6c92 | 2020-01-21 21:55:17 -0500 | [diff] [blame] | 274 | Scalar PolyDrivetrain<Scalar>::MaxVelocity( |
| 275 | const Eigen::Matrix<Scalar, 2, 2> &FF) { |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 276 | constexpr int kHighGearController = 3; |
| 277 | const Eigen::Matrix<Scalar, 2, 2> FF_high = |
| 278 | loop_->plant().coefficients(kHighGearController).B.inverse() * |
| 279 | (Eigen::Matrix<Scalar, 2, 2>::Identity() - |
| 280 | loop_->plant().coefficients(kHighGearController).A); |
| 281 | |
| 282 | ::Eigen::Matrix<Scalar, 1, 2> FF_sum = FF.colwise().sum(); |
| 283 | int min_FF_sum_index; |
| 284 | const Scalar min_FF_sum = FF_sum.minCoeff(&min_FF_sum_index); |
| 285 | // const Scalar min_K_sum = loop_->K().col(min_FF_sum_index).sum(); |
| 286 | const Scalar high_min_FF_sum = FF_high.col(0).sum(); |
| 287 | |
| 288 | const Scalar adjusted_ff_voltage = |
Brian Silverman | 342d4dc | 2018-09-02 16:26:01 -0700 | [diff] [blame] | 289 | ::aos::Clip(kTwelve * min_FF_sum / high_min_FF_sum, -kTwelve, kTwelve); |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 290 | return adjusted_ff_voltage / min_FF_sum; |
| 291 | } |
| 292 | |
| 293 | template <typename Scalar> |
Austin Schuh | eeec74a | 2019-01-27 20:58:59 -0800 | [diff] [blame] | 294 | void PolyDrivetrain<Scalar>::Update(Scalar voltage_battery) { |
James Kuszmaul | 6802533 | 2024-01-20 17:06:02 -0800 | [diff] [blame] | 295 | if (dt_config_.loop_type == LoopType::kClosedLoop) { |
Austin Schuh | 95771d9 | 2021-01-23 14:42:25 -0800 | [diff] [blame] | 296 | loop_->mutable_X_hat()(0, 0) = kf_->X_hat()(kLeftVelocity); |
| 297 | loop_->mutable_X_hat()(1, 0) = kf_->X_hat()(kRightVelocity); |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | // TODO(austin): Observer for the current velocity instead of difference |
| 301 | // calculations. |
| 302 | ++counter_; |
| 303 | |
| 304 | if (IsInGear(left_gear_) && IsInGear(right_gear_)) { |
| 305 | // FF * X = U (steady state) |
| 306 | const Eigen::Matrix<Scalar, 2, 2> FF = |
| 307 | loop_->plant().B().inverse() * |
| 308 | (Eigen::Matrix<Scalar, 2, 2>::Identity() - loop_->plant().A()); |
| 309 | |
| 310 | // Invert the plant to figure out how the velocity filter would have to |
| 311 | // work |
| 312 | // out in order to filter out the forwards negative inertia. |
| 313 | // This math assumes that the left and right power and velocity are |
| 314 | // equals, |
| 315 | // and that the plant is the same on the left and right. |
Lee Mracek | 8ae6c92 | 2020-01-21 21:55:17 -0500 | [diff] [blame] | 316 | const Scalar fvel = FilterVelocity(throttle_, FF); |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 317 | |
Brian Silverman | 342d4dc | 2018-09-02 16:26:01 -0700 | [diff] [blame] | 318 | const Scalar sign_svel = wheel_ * ((fvel > kZero) ? kOne : -kOne); |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 319 | Scalar steering_velocity; |
| 320 | if (quickturn_) { |
Lee Mracek | 8ae6c92 | 2020-01-21 21:55:17 -0500 | [diff] [blame] | 321 | steering_velocity = wheel_ * MaxVelocity(FF); |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 322 | } else { |
| 323 | steering_velocity = ::std::abs(fvel) * wheel_; |
| 324 | } |
| 325 | const Scalar left_velocity = fvel - steering_velocity; |
| 326 | const Scalar right_velocity = fvel + steering_velocity; |
| 327 | goal_left_velocity_ = left_velocity; |
| 328 | goal_right_velocity_ = right_velocity; |
| 329 | |
| 330 | // Integrate velocity to get the position. |
| 331 | // This position is used to get integral control. |
| 332 | loop_->mutable_R() << left_velocity, right_velocity; |
| 333 | |
| 334 | if (!quickturn_) { |
| 335 | // K * R = w |
| 336 | Eigen::Matrix<Scalar, 1, 2> equality_k; |
| 337 | equality_k << 1 + sign_svel, -(1 - sign_svel); |
Brian Silverman | 342d4dc | 2018-09-02 16:26:01 -0700 | [diff] [blame] | 338 | const Scalar equality_w = kZero; |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 339 | |
| 340 | // Construct a constraint on R by manipulating the constraint on U |
James Kuszmaul | 6175066 | 2021-06-21 21:32:33 -0700 | [diff] [blame] | 341 | ::frc971::controls::HVPolytope<2, 4, 4, Scalar> R_poly_hv( |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 342 | U_Poly_.static_H() * (loop_->controller().K() + FF), |
| 343 | U_Poly_.static_k() + |
| 344 | U_Poly_.static_H() * loop_->controller().K() * loop_->X_hat(), |
| 345 | (loop_->controller().K() + FF).inverse() * |
James Kuszmaul | 6175066 | 2021-06-21 21:32:33 -0700 | [diff] [blame] | 346 | ::frc971::controls::ShiftPoints<2, 4, Scalar>( |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 347 | U_Poly_.StaticVertices(), |
| 348 | loop_->controller().K() * loop_->X_hat())); |
| 349 | |
| 350 | // Limit R back inside the box. |
| 351 | loop_->mutable_R() = |
| 352 | CoerceGoal<Scalar>(R_poly_hv, equality_k, equality_w, loop_->R()); |
| 353 | } |
| 354 | |
James Kuszmaul | af5dfad | 2020-01-03 20:02:54 -0800 | [diff] [blame] | 355 | ff_volts_ = FF * loop_->R(); |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 356 | const Eigen::Matrix<Scalar, 2, 1> U_ideal = |
James Kuszmaul | af5dfad | 2020-01-03 20:02:54 -0800 | [diff] [blame] | 357 | loop_->controller().K() * (loop_->R() - loop_->X_hat()) + ff_volts_; |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 358 | |
| 359 | for (int i = 0; i < 2; i++) { |
| 360 | loop_->mutable_U()[i] = ::aos::Clip(U_ideal[i], -12, 12); |
| 361 | } |
| 362 | |
James Kuszmaul | 6802533 | 2024-01-20 17:06:02 -0800 | [diff] [blame] | 363 | if (dt_config_.loop_type == LoopType::kOpenLoop) { |
James Kuszmaul | af5dfad | 2020-01-03 20:02:54 -0800 | [diff] [blame] | 364 | ff_volts_.setZero(); |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 365 | loop_->mutable_X_hat() = |
| 366 | loop_->plant().A() * loop_->X_hat() + loop_->plant().B() * loop_->U(); |
| 367 | } |
| 368 | |
Lee Mracek | 8ae6c92 | 2020-01-21 21:55:17 -0500 | [diff] [blame] | 369 | // Housekeeping: set the shifting logging values to zero, because we're not |
| 370 | // shifting |
Brian Silverman | 342d4dc | 2018-09-02 16:26:01 -0700 | [diff] [blame] | 371 | left_motor_speed_ = kZero; |
| 372 | right_motor_speed_ = kZero; |
| 373 | current_left_velocity_ = kZero; |
| 374 | current_right_velocity_ = kZero; |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 375 | } else { |
Austin Schuh | bb735b7 | 2019-01-03 12:58:41 -0800 | [diff] [blame] | 376 | const Scalar dt = |
| 377 | ::std::chrono::duration_cast<::std::chrono::duration<Scalar>>( |
| 378 | dt_config_.dt) |
| 379 | .count(); |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 380 | current_left_velocity_ = |
Austin Schuh | bb735b7 | 2019-01-03 12:58:41 -0800 | [diff] [blame] | 381 | (position_.left_encoder - last_position_.left_encoder) / dt; |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 382 | current_right_velocity_ = |
Austin Schuh | bb735b7 | 2019-01-03 12:58:41 -0800 | [diff] [blame] | 383 | (position_.right_encoder - last_position_.right_encoder) / dt; |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 384 | left_motor_speed_ = |
| 385 | MotorSpeed(dt_config_.left_drive, position_.left_shifter_position, |
| 386 | current_left_velocity_, left_gear_); |
| 387 | right_motor_speed_ = |
| 388 | MotorSpeed(dt_config_.right_drive, position_.right_shifter_position, |
| 389 | current_right_velocity_, right_gear_); |
| 390 | |
| 391 | goal_left_velocity_ = current_left_velocity_; |
| 392 | goal_right_velocity_ = current_right_velocity_; |
| 393 | |
| 394 | // Any motor is not in gear. Speed match. |
| 395 | ::Eigen::Matrix<Scalar, 1, 1> R_left; |
| 396 | ::Eigen::Matrix<Scalar, 1, 1> R_right; |
| 397 | R_left(0, 0) = left_motor_speed_; |
| 398 | R_right(0, 0) = right_motor_speed_; |
| 399 | |
Brian Silverman | 342d4dc | 2018-09-02 16:26:01 -0700 | [diff] [blame] | 400 | const Scalar wiggle = (static_cast<Scalar>((counter_ % 30) / 15) - kHalf) * |
| 401 | static_cast<Scalar>(8.0); |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 402 | |
| 403 | loop_->mutable_U(0, 0) = ::aos::Clip( |
| 404 | (R_left / dt_config_.v)(0, 0) + (IsInGear(left_gear_) ? 0 : wiggle), |
Brian Silverman | 342d4dc | 2018-09-02 16:26:01 -0700 | [diff] [blame] | 405 | -kTwelve, kTwelve); |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 406 | loop_->mutable_U(1, 0) = ::aos::Clip( |
| 407 | (R_right / dt_config_.v)(0, 0) + (IsInGear(right_gear_) ? 0 : wiggle), |
Brian Silverman | 342d4dc | 2018-09-02 16:26:01 -0700 | [diff] [blame] | 408 | -kTwelve, kTwelve); |
James Kuszmaul | af5dfad | 2020-01-03 20:02:54 -0800 | [diff] [blame] | 409 | ff_volts_ = loop_->U(); |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 410 | #ifdef __linux__ |
Austin Schuh | eeec74a | 2019-01-27 20:58:59 -0800 | [diff] [blame] | 411 | loop_->mutable_U() *= kTwelve / voltage_battery; |
| 412 | #else |
| 413 | (void)voltage_battery; |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 414 | #endif // __linux__ |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | template <typename Scalar> |
| 419 | void PolyDrivetrain<Scalar>::SetOutput( |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 420 | ::frc971::control_loops::drivetrain::OutputT *output) { |
| 421 | if (output != nullptr) { |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 422 | output->left_voltage = loop_->U(0, 0); |
| 423 | output->right_voltage = loop_->U(1, 0); |
| 424 | output->left_high = MaybeHigh(left_gear_); |
| 425 | output->right_high = MaybeHigh(right_gear_); |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | template <typename Scalar> |
Lee Mracek | 8ae6c92 | 2020-01-21 21:55:17 -0500 | [diff] [blame] | 430 | flatbuffers::Offset<PolyDriveLogging> PolyDrivetrain<Scalar>::PopulateStatus( |
James Kuszmaul | af5dfad | 2020-01-03 20:02:54 -0800 | [diff] [blame] | 431 | flatbuffers::FlatBufferBuilder *fbb) { |
| 432 | PolyDriveLogging::Builder builder(*fbb); |
| 433 | |
| 434 | builder.add_goal_left_velocity(goal_left_velocity_); |
| 435 | builder.add_goal_right_velocity(goal_right_velocity_); |
| 436 | builder.add_ff_left_voltage(ff_volts_(0, 0)); |
| 437 | builder.add_ff_right_voltage(ff_volts_(1, 0)); |
| 438 | |
| 439 | return builder.Finish(); |
| 440 | } |
| 441 | |
| 442 | template <typename Scalar> |
| 443 | flatbuffers::Offset<CIMLogging> PolyDrivetrain<Scalar>::PopulateShiftingStatus( |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 444 | flatbuffers::FlatBufferBuilder *fbb) { |
| 445 | CIMLogging::Builder builder(*fbb); |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 446 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 447 | builder.add_left_in_gear(IsInGear(left_gear_)); |
| 448 | builder.add_left_motor_speed(left_motor_speed_); |
| 449 | builder.add_left_velocity(current_left_velocity_); |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 450 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 451 | builder.add_right_in_gear(IsInGear(right_gear_)); |
| 452 | builder.add_right_motor_speed(right_motor_speed_); |
| 453 | builder.add_right_velocity(current_right_velocity_); |
| 454 | |
| 455 | return builder.Finish(); |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 456 | } |
| 457 | |
Stephan Pleines | d99b1ee | 2024-02-02 20:56:44 -0800 | [diff] [blame] | 458 | } // namespace frc971::control_loops::drivetrain |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 459 | |
| 460 | #endif // FRC971_CONTROL_LOOPS_DRIVETRAIN_POLYDRIVETRAIN_H_ |