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