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/controls/polytope.h" |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 5 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 6 | #include "aos/commonmath.h" |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 7 | #include "frc971/control_loops/coerce_goal.h" |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 8 | #include "frc971/control_loops/drivetrain/gear.h" |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 9 | #ifdef __linux__ |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 10 | #include "frc971/control_loops/drivetrain/drivetrain.q.h" |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 11 | #include "aos/logging/logging.h" |
| 12 | #include "aos/logging/matrix_logging.h" |
| 13 | #include "aos/logging/queue_logging.h" |
| 14 | #include "aos/robot_state/robot_state.q.h" |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 15 | #else |
| 16 | #include "frc971/control_loops/drivetrain/drivetrain_uc.q.h" |
| 17 | #endif // __linux__ |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 18 | #include "frc971/control_loops/state_feedback_loop.h" |
| 19 | #include "frc971/control_loops/drivetrain/drivetrain_config.h" |
| 20 | |
| 21 | namespace frc971 { |
| 22 | namespace control_loops { |
| 23 | namespace drivetrain { |
| 24 | |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 25 | template <typename Scalar = double> |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 26 | class PolyDrivetrain { |
| 27 | public: |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 28 | PolyDrivetrain(const DrivetrainConfig<Scalar> &dt_config, |
| 29 | StateFeedbackLoop<7, 2, 4, Scalar> *kf); |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 30 | |
Austin Schuh | c5fceb8 | 2017-02-25 16:24:12 -0800 | [diff] [blame] | 31 | int controller_index() const { return loop_->index(); } |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 32 | |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 33 | // Computes the speed of the motor given the hall effect position and the |
| 34 | // speed of the robot. |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 35 | Scalar MotorSpeed(const constants::ShifterHallEffect &hall_effect, |
| 36 | Scalar shifter_position, Scalar velocity, Gear gear); |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 37 | |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 38 | void SetGoal(const ::frc971::control_loops::DrivetrainQueue::Goal &goal); |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 39 | |
| 40 | void SetPosition( |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 41 | const ::frc971::control_loops::DrivetrainQueue::Position *position, |
| 42 | Gear left_gear, Gear right_gear); |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 43 | |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 44 | Scalar FilterVelocity(Scalar throttle) const; |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 45 | |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 46 | Scalar MaxVelocity(); |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 47 | |
Austin Schuh | eeec74a | 2019-01-27 20:58:59 -0800 | [diff] [blame^] | 48 | void Update(Scalar voltage_battery); |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 49 | |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 50 | void SetOutput(::frc971::control_loops::DrivetrainQueue::Output *output); |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 51 | |
Austin Schuh | 4156560 | 2016-02-28 20:10:49 -0800 | [diff] [blame] | 52 | void PopulateStatus(::frc971::control_loops::DrivetrainQueue::Status *status); |
| 53 | |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 54 | // Computes the next state of a shifter given the current state and the |
| 55 | // requested state. |
| 56 | Gear UpdateSingleGear(Gear requested_gear, Gear current_gear); |
| 57 | |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 58 | // Returns the current estimated velocity in m/s. |
| 59 | Scalar velocity() const { |
Brian Silverman | 342d4dc | 2018-09-02 16:26:01 -0700 | [diff] [blame] | 60 | return (loop_->mutable_X_hat()(0) + loop_->mutable_X_hat()(1)) * kHalf; |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 61 | } |
| 62 | |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 63 | private: |
Brian Silverman | 342d4dc | 2018-09-02 16:26:01 -0700 | [diff] [blame] | 64 | static constexpr Scalar kZero = static_cast<Scalar>(0.0); |
| 65 | static constexpr Scalar kHalf = static_cast<Scalar>(0.5); |
| 66 | static constexpr Scalar kOne = static_cast<Scalar>(1.0); |
| 67 | static constexpr Scalar kTwo = static_cast<Scalar>(2.0); |
| 68 | static constexpr Scalar kTwelve = static_cast<Scalar>(12.0); |
| 69 | |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 70 | StateFeedbackLoop<7, 2, 4, Scalar> *kf_; |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 71 | |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 72 | const ::aos::controls::HVPolytope<2, 4, 4, Scalar> U_Poly_; |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 73 | |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 74 | ::std::unique_ptr<StateFeedbackLoop<2, 2, 2, Scalar>> loop_; |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 75 | |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 76 | const Scalar ttrust_; |
| 77 | Scalar wheel_; |
| 78 | Scalar throttle_; |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 79 | bool quickturn_; |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 80 | |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 81 | Gear left_gear_; |
| 82 | Gear right_gear_; |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 83 | |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 84 | ::frc971::control_loops::DrivetrainQueue::Position last_position_; |
| 85 | ::frc971::control_loops::DrivetrainQueue::Position position_; |
| 86 | int counter_; |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 87 | DrivetrainConfig<Scalar> dt_config_; |
Austin Schuh | 4156560 | 2016-02-28 20:10:49 -0800 | [diff] [blame] | 88 | |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 89 | Scalar goal_left_velocity_ = 0.0; |
| 90 | Scalar goal_right_velocity_ = 0.0; |
Kyle Stachowicz | 2f3c20f | 2017-07-13 16:04:05 -0700 | [diff] [blame] | 91 | |
| 92 | // Stored from the last iteration, for logging shifting logic. |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 93 | Scalar left_motor_speed_ = 0.0; |
| 94 | Scalar right_motor_speed_ = 0.0; |
| 95 | Scalar current_left_velocity_ = 0.0; |
| 96 | Scalar current_right_velocity_ = 0.0; |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 97 | }; |
| 98 | |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 99 | template <typename Scalar> |
| 100 | PolyDrivetrain<Scalar>::PolyDrivetrain( |
| 101 | const DrivetrainConfig<Scalar> &dt_config, |
| 102 | StateFeedbackLoop<7, 2, 4, Scalar> *kf) |
| 103 | : kf_(kf), |
| 104 | U_Poly_((Eigen::Matrix<Scalar, 4, 2>() << /*[[*/ 1, 0 /*]*/, |
| 105 | /*[*/ -1, 0 /*]*/, |
| 106 | /*[*/ 0, 1 /*]*/, |
| 107 | /*[*/ 0, -1 /*]]*/) |
| 108 | .finished(), |
| 109 | (Eigen::Matrix<Scalar, 4, 1>() << /*[[*/ 12 /*]*/, |
| 110 | /*[*/ 12 /*]*/, |
| 111 | /*[*/ 12 /*]*/, |
| 112 | /*[*/ 12 /*]]*/) |
| 113 | .finished(), |
| 114 | (Eigen::Matrix<Scalar, 2, 4>() << /*[[*/ 12, 12, -12, -12 /*]*/, |
| 115 | /*[*/ -12, 12, 12, -12 /*]*/) |
| 116 | .finished()), |
| 117 | loop_(new StateFeedbackLoop<2, 2, 2, Scalar>( |
| 118 | dt_config.make_v_drivetrain_loop())), |
| 119 | ttrust_(1.1), |
| 120 | wheel_(0.0), |
| 121 | throttle_(0.0), |
| 122 | quickturn_(false), |
| 123 | left_gear_(dt_config.default_high_gear ? Gear::HIGH : Gear::LOW), |
| 124 | right_gear_(dt_config.default_high_gear ? Gear::HIGH : Gear::LOW), |
| 125 | counter_(0), |
| 126 | dt_config_(dt_config) { |
| 127 | last_position_.Zero(); |
| 128 | position_.Zero(); |
| 129 | } |
| 130 | |
| 131 | template <typename Scalar> |
| 132 | Scalar PolyDrivetrain<Scalar>::MotorSpeed( |
| 133 | const constants::ShifterHallEffect &hall_effect, Scalar shifter_position, |
| 134 | Scalar velocity, Gear gear) { |
| 135 | const Scalar high_gear_speed = |
Brian Silverman | 342d4dc | 2018-09-02 16:26:01 -0700 | [diff] [blame] | 136 | velocity / |
| 137 | static_cast<Scalar>(dt_config_.high_gear_ratio / dt_config_.wheel_radius); |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 138 | const Scalar low_gear_speed = |
Brian Silverman | 342d4dc | 2018-09-02 16:26:01 -0700 | [diff] [blame] | 139 | velocity / |
| 140 | static_cast<Scalar>(dt_config_.low_gear_ratio / dt_config_.wheel_radius); |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 141 | |
Brian Silverman | 342d4dc | 2018-09-02 16:26:01 -0700 | [diff] [blame] | 142 | if (shifter_position < static_cast<Scalar>(hall_effect.clear_low)) { |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 143 | // We're in low gear, so return speed for that gear. |
| 144 | return low_gear_speed; |
Brian Silverman | 342d4dc | 2018-09-02 16:26:01 -0700 | [diff] [blame] | 145 | } else if (shifter_position > static_cast<Scalar>(hall_effect.clear_high)) { |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 146 | // We're in high gear, so return speed for that gear. |
| 147 | return high_gear_speed; |
| 148 | } |
| 149 | |
| 150 | // Not in gear, so speed-match to destination gear. |
| 151 | switch (gear) { |
| 152 | case Gear::HIGH: |
| 153 | case Gear::SHIFTING_UP: |
| 154 | return high_gear_speed; |
| 155 | case Gear::LOW: |
| 156 | case Gear::SHIFTING_DOWN: |
| 157 | default: |
| 158 | return low_gear_speed; |
| 159 | break; |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | template <typename Scalar> |
| 164 | Gear PolyDrivetrain<Scalar>::UpdateSingleGear(Gear requested_gear, |
| 165 | Gear current_gear) { |
| 166 | const Gear shift_up = |
| 167 | (dt_config_.shifter_type == ShifterType::HALL_EFFECT_SHIFTER) |
| 168 | ? Gear::SHIFTING_UP |
| 169 | : Gear::HIGH; |
| 170 | const Gear shift_down = |
| 171 | (dt_config_.shifter_type == ShifterType::HALL_EFFECT_SHIFTER) |
| 172 | ? Gear::SHIFTING_DOWN |
| 173 | : Gear::LOW; |
| 174 | if (current_gear != requested_gear) { |
| 175 | if (IsInGear(current_gear)) { |
| 176 | if (requested_gear == Gear::HIGH) { |
| 177 | if (current_gear != Gear::HIGH) { |
| 178 | current_gear = shift_up; |
| 179 | } |
| 180 | } else { |
| 181 | if (current_gear != Gear::LOW) { |
| 182 | current_gear = shift_down; |
| 183 | } |
| 184 | } |
| 185 | } else { |
| 186 | if (requested_gear == Gear::HIGH && current_gear == Gear::SHIFTING_DOWN) { |
| 187 | current_gear = Gear::SHIFTING_UP; |
| 188 | } else if (requested_gear == Gear::LOW && |
| 189 | current_gear == Gear::SHIFTING_UP) { |
| 190 | current_gear = Gear::SHIFTING_DOWN; |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | return current_gear; |
| 195 | } |
| 196 | |
| 197 | template <typename Scalar> |
| 198 | void PolyDrivetrain<Scalar>::SetGoal( |
| 199 | const ::frc971::control_loops::DrivetrainQueue::Goal &goal) { |
| 200 | const Scalar wheel = goal.wheel; |
| 201 | const Scalar throttle = goal.throttle; |
| 202 | const bool quickturn = goal.quickturn; |
| 203 | const bool highgear = goal.highgear; |
| 204 | |
| 205 | // Apply a sin function that's scaled to make it feel better. |
Brian Silverman | 342d4dc | 2018-09-02 16:26:01 -0700 | [diff] [blame] | 206 | const Scalar angular_range = |
| 207 | static_cast<Scalar>(M_PI_2) * dt_config_.wheel_non_linearity; |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 208 | |
| 209 | wheel_ = sin(angular_range * wheel) / sin(angular_range); |
| 210 | wheel_ = sin(angular_range * wheel_) / sin(angular_range); |
Brian Silverman | 342d4dc | 2018-09-02 16:26:01 -0700 | [diff] [blame] | 211 | wheel_ = kTwo * wheel - wheel_; |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 212 | quickturn_ = quickturn; |
| 213 | |
| 214 | if (quickturn_) { |
| 215 | wheel_ *= dt_config_.quickturn_wheel_multiplier; |
| 216 | } else { |
| 217 | wheel_ *= dt_config_.wheel_multiplier; |
| 218 | } |
| 219 | |
Brian Silverman | 342d4dc | 2018-09-02 16:26:01 -0700 | [diff] [blame] | 220 | static constexpr Scalar kThrottleDeadband = static_cast<Scalar>(0.05); |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 221 | if (::std::abs(throttle) < kThrottleDeadband) { |
| 222 | throttle_ = 0; |
| 223 | } else { |
| 224 | throttle_ = copysign( |
Brian Silverman | 342d4dc | 2018-09-02 16:26:01 -0700 | [diff] [blame] | 225 | (::std::abs(throttle) - kThrottleDeadband) / (kOne - kThrottleDeadband), |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 226 | throttle); |
| 227 | } |
| 228 | |
| 229 | Gear requested_gear = highgear ? Gear::HIGH : Gear::LOW; |
| 230 | |
| 231 | left_gear_ = UpdateSingleGear(requested_gear, left_gear_); |
| 232 | right_gear_ = UpdateSingleGear(requested_gear, right_gear_); |
| 233 | } |
| 234 | |
| 235 | template <typename Scalar> |
| 236 | void PolyDrivetrain<Scalar>::SetPosition( |
| 237 | const ::frc971::control_loops::DrivetrainQueue::Position *position, |
| 238 | Gear left_gear, Gear right_gear) { |
| 239 | left_gear_ = left_gear; |
| 240 | right_gear_ = right_gear; |
| 241 | last_position_ = position_; |
| 242 | position_ = *position; |
| 243 | } |
| 244 | |
| 245 | template <typename Scalar> |
| 246 | Scalar PolyDrivetrain<Scalar>::FilterVelocity(Scalar throttle) const { |
| 247 | const Eigen::Matrix<Scalar, 2, 2> FF = |
| 248 | loop_->plant().B().inverse() * |
| 249 | (Eigen::Matrix<Scalar, 2, 2>::Identity() - loop_->plant().A()); |
| 250 | |
| 251 | constexpr int kHighGearController = 3; |
| 252 | const Eigen::Matrix<Scalar, 2, 2> FF_high = |
| 253 | loop_->plant().coefficients(kHighGearController).B.inverse() * |
| 254 | (Eigen::Matrix<Scalar, 2, 2>::Identity() - |
| 255 | loop_->plant().coefficients(kHighGearController).A); |
| 256 | |
| 257 | ::Eigen::Matrix<Scalar, 1, 2> FF_sum = FF.colwise().sum(); |
| 258 | int min_FF_sum_index; |
| 259 | const Scalar min_FF_sum = FF_sum.minCoeff(&min_FF_sum_index); |
| 260 | const Scalar min_K_sum = loop_->controller().K().col(min_FF_sum_index).sum(); |
| 261 | const Scalar high_min_FF_sum = FF_high.col(0).sum(); |
| 262 | |
| 263 | const Scalar adjusted_ff_voltage = |
Brian Silverman | 342d4dc | 2018-09-02 16:26:01 -0700 | [diff] [blame] | 264 | ::aos::Clip(throttle * kTwelve * min_FF_sum / high_min_FF_sum, -kTwelve, kTwelve); |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 265 | return (adjusted_ff_voltage + |
Brian Silverman | 342d4dc | 2018-09-02 16:26:01 -0700 | [diff] [blame] | 266 | ttrust_ * min_K_sum * (loop_->X_hat(0, 0) + loop_->X_hat(1, 0)) * |
| 267 | kHalf) / |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 268 | (ttrust_ * min_K_sum + min_FF_sum); |
| 269 | } |
| 270 | |
| 271 | template <typename Scalar> |
| 272 | Scalar PolyDrivetrain<Scalar>::MaxVelocity() { |
| 273 | const Eigen::Matrix<Scalar, 2, 2> FF = |
| 274 | loop_->plant().B().inverse() * |
| 275 | (Eigen::Matrix<Scalar, 2, 2>::Identity() - loop_->plant().A()); |
| 276 | |
| 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. |
| 317 | const Scalar fvel = FilterVelocity(throttle_); |
| 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_) { |
| 322 | steering_velocity = wheel_ * MaxVelocity(); |
| 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 | |
| 356 | const Eigen::Matrix<Scalar, 2, 1> FF_volts = FF * loop_->R(); |
| 357 | const Eigen::Matrix<Scalar, 2, 1> U_ideal = |
| 358 | loop_->controller().K() * (loop_->R() - loop_->X_hat()) + FF_volts; |
| 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) { |
| 365 | loop_->mutable_X_hat() = |
| 366 | loop_->plant().A() * loop_->X_hat() + loop_->plant().B() * loop_->U(); |
| 367 | } |
| 368 | |
| 369 | // Housekeeping: set the shifting logging values to zero, because we're not shifting |
Brian Silverman | 342d4dc | 2018-09-02 16:26:01 -0700 | [diff] [blame] | 370 | left_motor_speed_ = kZero; |
| 371 | right_motor_speed_ = kZero; |
| 372 | current_left_velocity_ = kZero; |
| 373 | current_right_velocity_ = kZero; |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 374 | } else { |
Austin Schuh | bb735b7 | 2019-01-03 12:58:41 -0800 | [diff] [blame] | 375 | const Scalar dt = |
| 376 | ::std::chrono::duration_cast<::std::chrono::duration<Scalar>>( |
| 377 | dt_config_.dt) |
| 378 | .count(); |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 379 | current_left_velocity_ = |
Austin Schuh | bb735b7 | 2019-01-03 12:58:41 -0800 | [diff] [blame] | 380 | (position_.left_encoder - last_position_.left_encoder) / dt; |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 381 | current_right_velocity_ = |
Austin Schuh | bb735b7 | 2019-01-03 12:58:41 -0800 | [diff] [blame] | 382 | (position_.right_encoder - last_position_.right_encoder) / dt; |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 383 | left_motor_speed_ = |
| 384 | MotorSpeed(dt_config_.left_drive, position_.left_shifter_position, |
| 385 | current_left_velocity_, left_gear_); |
| 386 | right_motor_speed_ = |
| 387 | MotorSpeed(dt_config_.right_drive, position_.right_shifter_position, |
| 388 | current_right_velocity_, right_gear_); |
| 389 | |
| 390 | goal_left_velocity_ = current_left_velocity_; |
| 391 | goal_right_velocity_ = current_right_velocity_; |
| 392 | |
| 393 | // Any motor is not in gear. Speed match. |
| 394 | ::Eigen::Matrix<Scalar, 1, 1> R_left; |
| 395 | ::Eigen::Matrix<Scalar, 1, 1> R_right; |
| 396 | R_left(0, 0) = left_motor_speed_; |
| 397 | R_right(0, 0) = right_motor_speed_; |
| 398 | |
Brian Silverman | 342d4dc | 2018-09-02 16:26:01 -0700 | [diff] [blame] | 399 | const Scalar wiggle = (static_cast<Scalar>((counter_ % 30) / 15) - kHalf) * |
| 400 | static_cast<Scalar>(8.0); |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 401 | |
| 402 | loop_->mutable_U(0, 0) = ::aos::Clip( |
| 403 | (R_left / dt_config_.v)(0, 0) + (IsInGear(left_gear_) ? 0 : wiggle), |
Brian Silverman | 342d4dc | 2018-09-02 16:26:01 -0700 | [diff] [blame] | 404 | -kTwelve, kTwelve); |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 405 | loop_->mutable_U(1, 0) = ::aos::Clip( |
| 406 | (R_right / dt_config_.v)(0, 0) + (IsInGear(right_gear_) ? 0 : wiggle), |
Brian Silverman | 342d4dc | 2018-09-02 16:26:01 -0700 | [diff] [blame] | 407 | -kTwelve, kTwelve); |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 408 | #ifdef __linux__ |
Austin Schuh | eeec74a | 2019-01-27 20:58:59 -0800 | [diff] [blame^] | 409 | loop_->mutable_U() *= kTwelve / voltage_battery; |
| 410 | #else |
| 411 | (void)voltage_battery; |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 412 | #endif // __linux__ |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | template <typename Scalar> |
| 417 | void PolyDrivetrain<Scalar>::SetOutput( |
| 418 | ::frc971::control_loops::DrivetrainQueue::Output *output) { |
| 419 | if (output != NULL) { |
| 420 | output->left_voltage = loop_->U(0, 0); |
| 421 | output->right_voltage = loop_->U(1, 0); |
| 422 | output->left_high = MaybeHigh(left_gear_); |
| 423 | output->right_high = MaybeHigh(right_gear_); |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | template <typename Scalar> |
| 428 | void PolyDrivetrain<Scalar>::PopulateStatus( |
| 429 | ::frc971::control_loops::DrivetrainQueue::Status *status) { |
| 430 | status->left_velocity_goal = goal_left_velocity_; |
| 431 | status->right_velocity_goal = goal_right_velocity_; |
| 432 | |
| 433 | status->cim_logging.left_in_gear = IsInGear(left_gear_); |
| 434 | status->cim_logging.left_motor_speed = left_motor_speed_; |
| 435 | status->cim_logging.left_velocity = current_left_velocity_; |
| 436 | |
| 437 | status->cim_logging.right_in_gear = IsInGear(right_gear_); |
| 438 | status->cim_logging.right_motor_speed = right_motor_speed_; |
| 439 | status->cim_logging.right_velocity = current_right_velocity_; |
| 440 | } |
| 441 | |
| 442 | |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 443 | } // namespace drivetrain |
| 444 | } // namespace control_loops |
| 445 | } // namespace frc971 |
| 446 | |
| 447 | #endif // FRC971_CONTROL_LOOPS_DRIVETRAIN_POLYDRIVETRAIN_H_ |