James Kuszmaul | f254c1a | 2013-03-10 16:31:26 -0700 | [diff] [blame] | 1 | #include "frc971/control_loops/drivetrain/drivetrain.h" |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 2 | |
| 3 | #include <stdio.h> |
| 4 | #include <sched.h> |
| 5 | #include <cmath> |
Austin Schuh | 4352ac6 | 2013-03-19 06:23:16 +0000 | [diff] [blame] | 6 | #include <memory> |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 7 | #include "Eigen/Dense" |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 8 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 9 | #include "aos/common/logging/logging.h" |
Brian | a6553ed | 2014-04-02 21:26:46 -0700 | [diff] [blame] | 10 | #include "aos/common/controls/polytope.h" |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 11 | #include "aos/common/commonmath.h" |
Brian Silverman | 61e41fd | 2014-02-16 19:08:50 -0800 | [diff] [blame] | 12 | #include "aos/common/logging/queue_logging.h" |
Brian Silverman | fd5e2a3 | 2014-02-22 20:02:39 -0800 | [diff] [blame] | 13 | #include "aos/common/logging/matrix_logging.h" |
Brian Silverman | 61e41fd | 2014-02-16 19:08:50 -0800 | [diff] [blame] | 14 | |
James Kuszmaul | f254c1a | 2013-03-10 16:31:26 -0700 | [diff] [blame] | 15 | #include "frc971/control_loops/state_feedback_loop.h" |
James Kuszmaul | fb0e0ae | 2014-03-25 07:04:47 -0700 | [diff] [blame] | 16 | #include "frc971/control_loops/coerce_goal.h" |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 17 | #include "frc971/control_loops/drivetrain/polydrivetrain_cim_plant.h" |
James Kuszmaul | f254c1a | 2013-03-10 16:31:26 -0700 | [diff] [blame] | 18 | #include "frc971/control_loops/drivetrain/drivetrain.q.h" |
Brian Silverman | 6bf0d3c | 2014-03-08 12:52:54 -0800 | [diff] [blame] | 19 | #include "frc971/queues/other_sensors.q.h" |
Brian Silverman | 1a6590d | 2013-11-04 14:46:46 -0800 | [diff] [blame] | 20 | #include "frc971/constants.h" |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 21 | |
Brian Silverman | 6bf0d3c | 2014-03-08 12:52:54 -0800 | [diff] [blame] | 22 | using frc971::sensors::gyro_reading; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 23 | |
| 24 | namespace frc971 { |
| 25 | namespace control_loops { |
| 26 | |
Austin Schuh | 4352ac6 | 2013-03-19 06:23:16 +0000 | [diff] [blame] | 27 | class DrivetrainMotorsSS { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 28 | public: |
Brian Silverman | ad9e000 | 2014-04-13 14:55:57 -0700 | [diff] [blame] | 29 | class LimitedDrivetrainLoop : public StateFeedbackLoop<4, 2, 2> { |
| 30 | public: |
| 31 | LimitedDrivetrainLoop(const StateFeedbackLoop<4, 2, 2> &loop) |
| 32 | : StateFeedbackLoop<4, 2, 2>(loop), |
| 33 | U_Poly_((Eigen::Matrix<double, 4, 2>() << 1, 0, |
| 34 | -1, 0, |
| 35 | 0, 1, |
| 36 | 0, -1).finished(), |
| 37 | (Eigen::Matrix<double, 4, 1>() << 12.0, 12.0, |
| 38 | 12.0, 12.0).finished()) { |
| 39 | ::aos::controls::HPolytope<0>::Init(); |
| 40 | T << 1, -1, 1, 1; |
| 41 | T_inverse = T.inverse(); |
| 42 | } |
| 43 | |
| 44 | private: |
| 45 | virtual void CapU() { |
| 46 | const Eigen::Matrix<double, 4, 1> error = R - X_hat; |
| 47 | |
| 48 | if (U(0, 0) > 12.0 || U(1, 0) > 12.0) { |
| 49 | LOG_MATRIX(DEBUG, "U at start", U); |
| 50 | |
| 51 | Eigen::Matrix<double, 2, 2> position_K; |
| 52 | position_K << K(0, 0), K(0, 2), |
| 53 | K(1, 0), K(1, 2); |
| 54 | Eigen::Matrix<double, 2, 2> velocity_K; |
| 55 | velocity_K << K(0, 1), K(0, 3), |
| 56 | K(1, 1), K(1, 3); |
| 57 | |
| 58 | Eigen::Matrix<double, 2, 1> position_error; |
| 59 | position_error << error(0, 0), error(2, 0); |
| 60 | const auto drive_error = T_inverse * position_error; |
| 61 | Eigen::Matrix<double, 2, 1> velocity_error; |
| 62 | velocity_error << error(1, 0), error(3, 0); |
| 63 | LOG_MATRIX(DEBUG, "error", error); |
| 64 | |
| 65 | const auto &poly = U_Poly_; |
| 66 | const Eigen::Matrix<double, 4, 2> pos_poly_H = poly.H() * position_K * T; |
| 67 | const Eigen::Matrix<double, 4, 1> pos_poly_k = |
| 68 | poly.k() - poly.H() * velocity_K * velocity_error; |
| 69 | const ::aos::controls::HPolytope<2> pos_poly(pos_poly_H, pos_poly_k); |
| 70 | |
| 71 | Eigen::Matrix<double, 2, 1> adjusted_pos_error; |
| 72 | { |
| 73 | const auto &P = drive_error; |
| 74 | |
| 75 | Eigen::Matrix<double, 1, 2> L45; |
| 76 | L45 << ::aos::sign(P(1, 0)), -::aos::sign(P(0, 0)); |
| 77 | const double w45 = 0; |
| 78 | |
| 79 | Eigen::Matrix<double, 1, 2> LH; |
| 80 | if (::std::abs(P(0, 0)) > ::std::abs(P(1, 0))) { |
| 81 | LH << 0, 1; |
| 82 | } else { |
| 83 | LH << 1, 0; |
| 84 | } |
| 85 | const double wh = LH.dot(P); |
| 86 | |
| 87 | Eigen::Matrix<double, 2, 2> standard; |
| 88 | standard << L45, LH; |
| 89 | Eigen::Matrix<double, 2, 1> W; |
| 90 | W << w45, wh; |
| 91 | const Eigen::Matrix<double, 2, 1> intersection = standard.inverse() * W; |
| 92 | |
| 93 | bool is_inside_h; |
| 94 | const auto adjusted_pos_error_h = |
| 95 | DoCoerceGoal(pos_poly, LH, wh, drive_error, &is_inside_h); |
| 96 | const auto adjusted_pos_error_45 = |
| 97 | DoCoerceGoal(pos_poly, L45, w45, intersection, nullptr); |
| 98 | if (pos_poly.IsInside(intersection)) { |
| 99 | adjusted_pos_error = adjusted_pos_error_h; |
| 100 | } else { |
| 101 | if (is_inside_h) { |
| 102 | if (adjusted_pos_error_h.norm() > adjusted_pos_error_45.norm()) { |
| 103 | adjusted_pos_error = adjusted_pos_error_h; |
| 104 | } else { |
| 105 | adjusted_pos_error = adjusted_pos_error_45; |
| 106 | } |
| 107 | } else { |
| 108 | adjusted_pos_error = adjusted_pos_error_45; |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | LOG_MATRIX(DEBUG, "adjusted_pos_error", adjusted_pos_error); |
| 114 | U = velocity_K * velocity_error + position_K * T * adjusted_pos_error; |
| 115 | LOG_MATRIX(DEBUG, "U is now", U); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | const ::aos::controls::HPolytope<2> U_Poly_; |
| 120 | Eigen::Matrix<double, 2, 2> T, T_inverse; |
| 121 | }; |
| 122 | |
Brian Silverman | 2c590c3 | 2013-11-04 18:08:54 -0800 | [diff] [blame] | 123 | DrivetrainMotorsSS() |
Brian Silverman | ad9e000 | 2014-04-13 14:55:57 -0700 | [diff] [blame] | 124 | : loop_(new LimitedDrivetrainLoop( |
Brian Silverman | 176303a | 2014-04-10 10:54:55 -0700 | [diff] [blame] | 125 | constants::GetValues().make_drivetrain_loop())), |
| 126 | filtered_offset_(0.0), |
| 127 | gyro_(0.0), |
| 128 | left_goal_(0.0), |
| 129 | right_goal_(0.0), |
| 130 | raw_left_(0.0), |
| 131 | raw_right_(0.0), |
| 132 | control_loop_driving_(false) { |
| 133 | // High gear on both. |
| 134 | loop_->set_controller_index(3); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 135 | } |
Brian Silverman | 176303a | 2014-04-10 10:54:55 -0700 | [diff] [blame] | 136 | |
| 137 | void SetGoal(double left, double left_velocity, double right, |
| 138 | double right_velocity) { |
| 139 | left_goal_ = left; |
| 140 | right_goal_ = right; |
Austin Schuh | 4352ac6 | 2013-03-19 06:23:16 +0000 | [diff] [blame] | 141 | loop_->R << left, left_velocity, right, right_velocity; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 142 | } |
| 143 | void SetRawPosition(double left, double right) { |
Brian Silverman | 176303a | 2014-04-10 10:54:55 -0700 | [diff] [blame] | 144 | raw_right_ = right; |
| 145 | raw_left_ = left; |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 146 | Eigen::Matrix<double, 2, 1> Y; |
Brian Silverman | 176303a | 2014-04-10 10:54:55 -0700 | [diff] [blame] | 147 | Y << left + filtered_offset_, right - filtered_offset_; |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 148 | loop_->Correct(Y); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 149 | } |
Austin Schuh | 4352ac6 | 2013-03-19 06:23:16 +0000 | [diff] [blame] | 150 | void SetPosition( |
| 151 | double left, double right, double gyro, bool control_loop_driving) { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 152 | // Decay the offset quickly because this gyro is great. |
Brian Silverman | ad9e000 | 2014-04-13 14:55:57 -0700 | [diff] [blame] | 153 | const double offset = |
| 154 | (right - left - gyro * constants::GetValues().turn_width) / 2.0; |
Brian Silverman | 176303a | 2014-04-10 10:54:55 -0700 | [diff] [blame] | 155 | filtered_offset_ = 0.25 * offset + 0.75 * filtered_offset_; |
| 156 | gyro_ = gyro; |
| 157 | control_loop_driving_ = control_loop_driving; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 158 | SetRawPosition(left, right); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 159 | } |
Austin Schuh | 4352ac6 | 2013-03-19 06:23:16 +0000 | [diff] [blame] | 160 | |
Ben Fredrickson | 890c3fe | 2014-03-02 00:15:16 +0000 | [diff] [blame] | 161 | void SetExternalMotors(double left_voltage, double right_voltage) { |
| 162 | loop_->U << left_voltage, right_voltage; |
| 163 | } |
| 164 | |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 165 | void Update(bool stop_motors) { |
Brian Silverman | 176303a | 2014-04-10 10:54:55 -0700 | [diff] [blame] | 166 | if (control_loop_driving_) { |
Ben Fredrickson | 890c3fe | 2014-03-02 00:15:16 +0000 | [diff] [blame] | 167 | loop_->Update(stop_motors); |
| 168 | } else { |
| 169 | if (stop_motors) { |
| 170 | loop_->U.setZero(); |
| 171 | loop_->U_uncapped.setZero(); |
| 172 | } |
| 173 | loop_->UpdateObserver(); |
| 174 | } |
Brian Silverman | 3146b64 | 2014-03-20 14:52:46 -0700 | [diff] [blame] | 175 | ::Eigen::Matrix<double, 4, 1> E = loop_->R - loop_->X_hat; |
| 176 | LOG_MATRIX(DEBUG, "E", E); |
Ben Fredrickson | 890c3fe | 2014-03-02 00:15:16 +0000 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | double GetEstimatedRobotSpeed() { |
| 180 | // lets just call the average of left and right velocities close enough |
| 181 | return (loop_->X_hat(1, 0) + loop_->X_hat(3, 0)) / 2; |
| 182 | } |
| 183 | |
| 184 | double GetEstimatedLeftEncoder() { |
Ben Fredrickson | 890c3fe | 2014-03-02 00:15:16 +0000 | [diff] [blame] | 185 | return loop_->X_hat(0, 0); |
| 186 | } |
| 187 | |
| 188 | double GetEstimatedRightEncoder() { |
| 189 | return loop_->X_hat(2, 0); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 190 | } |
| 191 | |
Austin Schuh | 4352ac6 | 2013-03-19 06:23:16 +0000 | [diff] [blame] | 192 | void SendMotors(Drivetrain::Output *output) { |
| 193 | if (output) { |
| 194 | output->left_voltage = loop_->U(0, 0); |
| 195 | output->right_voltage = loop_->U(1, 0); |
Brian Silverman | e3a277a | 2014-04-13 14:56:57 -0700 | [diff] [blame] | 196 | output->left_high = false; |
| 197 | output->right_high = false; |
brians | 8ad7405 | 2013-03-16 21:04:51 +0000 | [diff] [blame] | 198 | } |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 199 | } |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 200 | |
| 201 | private: |
Brian Silverman | ad9e000 | 2014-04-13 14:55:57 -0700 | [diff] [blame] | 202 | ::std::unique_ptr<LimitedDrivetrainLoop> loop_; |
Austin Schuh | 4352ac6 | 2013-03-19 06:23:16 +0000 | [diff] [blame] | 203 | |
Brian Silverman | 176303a | 2014-04-10 10:54:55 -0700 | [diff] [blame] | 204 | double filtered_offset_; |
| 205 | double gyro_; |
| 206 | double left_goal_; |
| 207 | double right_goal_; |
| 208 | double raw_left_; |
| 209 | double raw_right_; |
| 210 | bool control_loop_driving_; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 211 | }; |
| 212 | |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 213 | class PolyDrivetrain { |
| 214 | public: |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 215 | |
| 216 | enum Gear { |
| 217 | HIGH, |
| 218 | LOW, |
| 219 | SHIFTING_UP, |
| 220 | SHIFTING_DOWN |
| 221 | }; |
| 222 | // Stall Torque in N m |
| 223 | static constexpr double kStallTorque = 2.42; |
| 224 | // Stall Current in Amps |
| 225 | static constexpr double kStallCurrent = 133; |
| 226 | // Free Speed in RPM. Used number from last year. |
| 227 | static constexpr double kFreeSpeed = 4650.0; |
| 228 | // Free Current in Amps |
| 229 | static constexpr double kFreeCurrent = 2.7; |
| 230 | // Moment of inertia of the drivetrain in kg m^2 |
| 231 | // Just borrowed from last year. |
| 232 | static constexpr double J = 6.4; |
| 233 | // Mass of the robot, in kg. |
| 234 | static constexpr double m = 68; |
| 235 | // Radius of the robot, in meters (from last year). |
| 236 | static constexpr double rb = 0.617998644 / 2.0; |
Brian Silverman | 1a6590d | 2013-11-04 14:46:46 -0800 | [diff] [blame] | 237 | static constexpr double kWheelRadius = 0.04445; |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 238 | // Resistance of the motor, divided by the number of motors. |
| 239 | static constexpr double kR = (12.0 / kStallCurrent / 4 + 0.03) / (0.93 * 0.93); |
| 240 | // Motor velocity constant |
| 241 | static constexpr double Kv = |
| 242 | ((kFreeSpeed / 60.0 * 2.0 * M_PI) / (12.0 - kR * kFreeCurrent)); |
| 243 | // Torque constant |
| 244 | static constexpr double Kt = kStallTorque / kStallCurrent; |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 245 | |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 246 | PolyDrivetrain() |
| 247 | : U_Poly_((Eigen::Matrix<double, 4, 2>() << /*[[*/ 1, 0 /*]*/, |
| 248 | /*[*/ -1, 0 /*]*/, |
| 249 | /*[*/ 0, 1 /*]*/, |
| 250 | /*[*/ 0, -1 /*]]*/).finished(), |
| 251 | (Eigen::Matrix<double, 4, 1>() << /*[[*/ 12 /*]*/, |
| 252 | /*[*/ 12 /*]*/, |
| 253 | /*[*/ 12 /*]*/, |
| 254 | /*[*/ 12 /*]]*/).finished()), |
Brian Silverman | 2c590c3 | 2013-11-04 18:08:54 -0800 | [diff] [blame] | 255 | loop_(new StateFeedbackLoop<2, 2, 2>( |
| 256 | constants::GetValues().make_v_drivetrain_loop())), |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 257 | ttrust_(1.1), |
| 258 | wheel_(0.0), |
| 259 | throttle_(0.0), |
| 260 | quickturn_(false), |
| 261 | stale_count_(0), |
| 262 | position_time_delta_(0.01), |
| 263 | left_gear_(LOW), |
Brian Silverman | de8fd55 | 2013-11-03 15:53:42 -0800 | [diff] [blame] | 264 | right_gear_(LOW), |
| 265 | counter_(0) { |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 266 | |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 267 | last_position_.Zero(); |
| 268 | position_.Zero(); |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 269 | } |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 270 | static bool IsInGear(Gear gear) { return gear == LOW || gear == HIGH; } |
| 271 | |
Austin Schuh | 78d5546 | 2014-02-23 01:39:30 -0800 | [diff] [blame] | 272 | static double MotorSpeed(const constants::ShifterHallEffect &hall_effect, |
| 273 | double shifter_position, double velocity) { |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 274 | // TODO(austin): G_high, G_low and kWheelRadius |
Austin Schuh | 78d5546 | 2014-02-23 01:39:30 -0800 | [diff] [blame] | 275 | const double avg_hall_effect = |
| 276 | (hall_effect.clear_high + hall_effect.clear_low) / 2.0; |
| 277 | |
| 278 | if (shifter_position > avg_hall_effect) { |
Brian Silverman | 1a6590d | 2013-11-04 14:46:46 -0800 | [diff] [blame] | 279 | return velocity / constants::GetValues().high_gear_ratio / kWheelRadius; |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 280 | } else { |
Brian Silverman | 1a6590d | 2013-11-04 14:46:46 -0800 | [diff] [blame] | 281 | return velocity / constants::GetValues().low_gear_ratio / kWheelRadius; |
| 282 | } |
| 283 | } |
| 284 | |
Austin Schuh | 78d5546 | 2014-02-23 01:39:30 -0800 | [diff] [blame] | 285 | Gear ComputeGear(const constants::ShifterHallEffect &hall_effect, |
| 286 | double velocity, Gear current) { |
| 287 | const double low_omega = MotorSpeed(hall_effect, 0.0, ::std::abs(velocity)); |
| 288 | const double high_omega = |
| 289 | MotorSpeed(hall_effect, 1.0, ::std::abs(velocity)); |
Brian Silverman | 1a6590d | 2013-11-04 14:46:46 -0800 | [diff] [blame] | 290 | |
Brian Silverman | 1a6590d | 2013-11-04 14:46:46 -0800 | [diff] [blame] | 291 | double high_torque = ((12.0 - high_omega / Kv) * Kt / kR); |
| 292 | double low_torque = ((12.0 - low_omega / Kv) * Kt / kR); |
| 293 | double high_power = high_torque * high_omega; |
| 294 | double low_power = low_torque * low_omega; |
Brian Silverman | 55a930b | 2013-11-04 20:59:00 -0800 | [diff] [blame] | 295 | |
| 296 | // TODO(aschuh): Do this right! |
| 297 | if ((current == HIGH || high_power > low_power + 160) && |
| 298 | ::std::abs(velocity) > 0.14) { |
Brian Silverman | 1a6590d | 2013-11-04 14:46:46 -0800 | [diff] [blame] | 299 | return HIGH; |
| 300 | } else { |
| 301 | return LOW; |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 302 | } |
| 303 | } |
| 304 | |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 305 | void SetGoal(double wheel, double throttle, bool quickturn, bool highgear) { |
Brian Silverman | de8fd55 | 2013-11-03 15:53:42 -0800 | [diff] [blame] | 306 | const double kWheelNonLinearity = 0.3; |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 307 | // Apply a sin function that's scaled to make it feel better. |
| 308 | const double angular_range = M_PI_2 * kWheelNonLinearity; |
| 309 | wheel_ = sin(angular_range * wheel) / sin(angular_range); |
| 310 | wheel_ = sin(angular_range * wheel_) / sin(angular_range); |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 311 | quickturn_ = quickturn; |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 312 | |
Brian Silverman | 1a6590d | 2013-11-04 14:46:46 -0800 | [diff] [blame] | 313 | static const double kThrottleDeadband = 0.05; |
| 314 | if (::std::abs(throttle) < kThrottleDeadband) { |
| 315 | throttle_ = 0; |
| 316 | } else { |
| 317 | throttle_ = copysign((::std::abs(throttle) - kThrottleDeadband) / |
| 318 | (1.0 - kThrottleDeadband), throttle); |
| 319 | } |
| 320 | |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 321 | // TODO(austin): Fix the upshift logic to include states. |
Brian Silverman | 1a6590d | 2013-11-04 14:46:46 -0800 | [diff] [blame] | 322 | Gear requested_gear; |
Brian Silverman | 55a930b | 2013-11-04 20:59:00 -0800 | [diff] [blame] | 323 | if (false) { |
Austin Schuh | 78d5546 | 2014-02-23 01:39:30 -0800 | [diff] [blame] | 324 | const auto &values = constants::GetValues(); |
Brian Silverman | 1a6590d | 2013-11-04 14:46:46 -0800 | [diff] [blame] | 325 | const double current_left_velocity = |
| 326 | (position_.left_encoder - last_position_.left_encoder) / |
| 327 | position_time_delta_; |
| 328 | const double current_right_velocity = |
| 329 | (position_.right_encoder - last_position_.right_encoder) / |
| 330 | position_time_delta_; |
| 331 | |
Austin Schuh | 78d5546 | 2014-02-23 01:39:30 -0800 | [diff] [blame] | 332 | Gear left_requested = |
| 333 | ComputeGear(values.left_drive, current_left_velocity, left_gear_); |
| 334 | Gear right_requested = |
| 335 | ComputeGear(values.right_drive, current_right_velocity, right_gear_); |
Brian Silverman | 1a6590d | 2013-11-04 14:46:46 -0800 | [diff] [blame] | 336 | requested_gear = |
| 337 | (left_requested == HIGH || right_requested == HIGH) ? HIGH : LOW; |
| 338 | } else { |
| 339 | requested_gear = highgear ? HIGH : LOW; |
| 340 | } |
| 341 | |
| 342 | const Gear shift_up = |
| 343 | constants::GetValues().clutch_transmission ? HIGH : SHIFTING_UP; |
| 344 | const Gear shift_down = |
| 345 | constants::GetValues().clutch_transmission ? LOW : SHIFTING_DOWN; |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 346 | |
| 347 | if (left_gear_ != requested_gear) { |
| 348 | if (IsInGear(left_gear_)) { |
| 349 | if (requested_gear == HIGH) { |
Brian Silverman | 1a6590d | 2013-11-04 14:46:46 -0800 | [diff] [blame] | 350 | left_gear_ = shift_up; |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 351 | } else { |
Brian Silverman | 1a6590d | 2013-11-04 14:46:46 -0800 | [diff] [blame] | 352 | left_gear_ = shift_down; |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 353 | } |
Brian Silverman | 55a930b | 2013-11-04 20:59:00 -0800 | [diff] [blame] | 354 | } else { |
| 355 | if (requested_gear == HIGH && left_gear_ == SHIFTING_DOWN) { |
| 356 | left_gear_ = SHIFTING_UP; |
| 357 | } else if (requested_gear == LOW && left_gear_ == SHIFTING_UP) { |
| 358 | left_gear_ = SHIFTING_DOWN; |
| 359 | } |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 360 | } |
| 361 | } |
| 362 | if (right_gear_ != requested_gear) { |
| 363 | if (IsInGear(right_gear_)) { |
| 364 | if (requested_gear == HIGH) { |
Brian Silverman | 1a6590d | 2013-11-04 14:46:46 -0800 | [diff] [blame] | 365 | right_gear_ = shift_up; |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 366 | } else { |
Brian Silverman | 1a6590d | 2013-11-04 14:46:46 -0800 | [diff] [blame] | 367 | right_gear_ = shift_down; |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 368 | } |
Brian Silverman | 55a930b | 2013-11-04 20:59:00 -0800 | [diff] [blame] | 369 | } else { |
| 370 | if (requested_gear == HIGH && right_gear_ == SHIFTING_DOWN) { |
| 371 | right_gear_ = SHIFTING_UP; |
| 372 | } else if (requested_gear == LOW && right_gear_ == SHIFTING_UP) { |
| 373 | right_gear_ = SHIFTING_DOWN; |
| 374 | } |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 375 | } |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 376 | } |
| 377 | } |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 378 | void SetPosition(const Drivetrain::Position *position) { |
Austin Schuh | 78d5546 | 2014-02-23 01:39:30 -0800 | [diff] [blame] | 379 | const auto &values = constants::GetValues(); |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 380 | if (position == NULL) { |
| 381 | ++stale_count_; |
| 382 | } else { |
| 383 | last_position_ = position_; |
| 384 | position_ = *position; |
| 385 | position_time_delta_ = (stale_count_ + 1) * 0.01; |
| 386 | stale_count_ = 0; |
| 387 | } |
| 388 | |
| 389 | if (position) { |
Brian Silverman | 61e41fd | 2014-02-16 19:08:50 -0800 | [diff] [blame] | 390 | GearLogging gear_logging; |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 391 | // Switch to the correct controller. |
Austin Schuh | 78d5546 | 2014-02-23 01:39:30 -0800 | [diff] [blame] | 392 | const double left_middle_shifter_position = |
| 393 | (values.left_drive.clear_high + values.left_drive.clear_low) / 2.0; |
| 394 | const double right_middle_shifter_position = |
| 395 | (values.right_drive.clear_high + values.right_drive.clear_low) / 2.0; |
| 396 | |
| 397 | if (position->left_shifter_position < left_middle_shifter_position) { |
| 398 | if (position->right_shifter_position < right_middle_shifter_position || |
| 399 | right_gear_ == LOW) { |
Brian Silverman | 61e41fd | 2014-02-16 19:08:50 -0800 | [diff] [blame] | 400 | gear_logging.left_loop_high = false; |
| 401 | gear_logging.right_loop_high = false; |
| 402 | loop_->set_controller_index(gear_logging.controller_index = 0); |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 403 | } else { |
Brian Silverman | 61e41fd | 2014-02-16 19:08:50 -0800 | [diff] [blame] | 404 | gear_logging.left_loop_high = false; |
| 405 | gear_logging.right_loop_high = true; |
| 406 | loop_->set_controller_index(gear_logging.controller_index = 1); |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 407 | } |
| 408 | } else { |
Austin Schuh | 78d5546 | 2014-02-23 01:39:30 -0800 | [diff] [blame] | 409 | if (position->right_shifter_position < right_middle_shifter_position || |
| 410 | left_gear_ == LOW) { |
Brian Silverman | 61e41fd | 2014-02-16 19:08:50 -0800 | [diff] [blame] | 411 | gear_logging.left_loop_high = true; |
| 412 | gear_logging.right_loop_high = false; |
| 413 | loop_->set_controller_index(gear_logging.controller_index = 2); |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 414 | } else { |
Brian Silverman | 61e41fd | 2014-02-16 19:08:50 -0800 | [diff] [blame] | 415 | gear_logging.left_loop_high = true; |
| 416 | gear_logging.right_loop_high = true; |
| 417 | loop_->set_controller_index(gear_logging.controller_index = 3); |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 418 | } |
| 419 | } |
Brian Silverman | 61e41fd | 2014-02-16 19:08:50 -0800 | [diff] [blame] | 420 | |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 421 | // TODO(austin): Constants. |
Austin Schuh | 78d5546 | 2014-02-23 01:39:30 -0800 | [diff] [blame] | 422 | if (position->left_shifter_position > values.left_drive.clear_high && left_gear_ == SHIFTING_UP) { |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 423 | left_gear_ = HIGH; |
| 424 | } |
Austin Schuh | 78d5546 | 2014-02-23 01:39:30 -0800 | [diff] [blame] | 425 | if (position->left_shifter_position < values.left_drive.clear_low && left_gear_ == SHIFTING_DOWN) { |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 426 | left_gear_ = LOW; |
| 427 | } |
Austin Schuh | 78d5546 | 2014-02-23 01:39:30 -0800 | [diff] [blame] | 428 | if (position->right_shifter_position > values.right_drive.clear_high && right_gear_ == SHIFTING_UP) { |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 429 | right_gear_ = HIGH; |
| 430 | } |
Austin Schuh | 78d5546 | 2014-02-23 01:39:30 -0800 | [diff] [blame] | 431 | if (position->right_shifter_position < values.right_drive.clear_low && right_gear_ == SHIFTING_DOWN) { |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 432 | right_gear_ = LOW; |
| 433 | } |
Brian Silverman | 61e41fd | 2014-02-16 19:08:50 -0800 | [diff] [blame] | 434 | |
| 435 | gear_logging.left_state = left_gear_; |
| 436 | gear_logging.right_state = right_gear_; |
| 437 | LOG_STRUCT(DEBUG, "state", gear_logging); |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 438 | } |
| 439 | } |
| 440 | |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 441 | double FilterVelocity(double throttle) { |
| 442 | const Eigen::Matrix<double, 2, 2> FF = |
| 443 | loop_->B().inverse() * |
| 444 | (Eigen::Matrix<double, 2, 2>::Identity() - loop_->A()); |
| 445 | |
| 446 | constexpr int kHighGearController = 3; |
| 447 | const Eigen::Matrix<double, 2, 2> FF_high = |
| 448 | loop_->controller(kHighGearController).plant.B.inverse() * |
| 449 | (Eigen::Matrix<double, 2, 2>::Identity() - |
| 450 | loop_->controller(kHighGearController).plant.A); |
| 451 | |
| 452 | ::Eigen::Matrix<double, 1, 2> FF_sum = FF.colwise().sum(); |
| 453 | int min_FF_sum_index; |
| 454 | const double min_FF_sum = FF_sum.minCoeff(&min_FF_sum_index); |
| 455 | const double min_K_sum = loop_->K().col(min_FF_sum_index).sum(); |
| 456 | const double high_min_FF_sum = FF_high.col(0).sum(); |
| 457 | |
| 458 | const double adjusted_ff_voltage = ::aos::Clip( |
| 459 | throttle * 12.0 * min_FF_sum / high_min_FF_sum, -12.0, 12.0); |
| 460 | return ((adjusted_ff_voltage + |
| 461 | ttrust_ * min_K_sum * (loop_->X_hat(0, 0) + loop_->X_hat(1, 0)) / |
| 462 | 2.0) / |
| 463 | (ttrust_ * min_K_sum + min_FF_sum)); |
| 464 | } |
| 465 | |
Brian Silverman | 718b1d7 | 2013-10-28 16:22:45 -0700 | [diff] [blame] | 466 | double MaxVelocity() { |
| 467 | const Eigen::Matrix<double, 2, 2> FF = |
| 468 | loop_->B().inverse() * |
| 469 | (Eigen::Matrix<double, 2, 2>::Identity() - loop_->A()); |
| 470 | |
| 471 | constexpr int kHighGearController = 3; |
| 472 | const Eigen::Matrix<double, 2, 2> FF_high = |
| 473 | loop_->controller(kHighGearController).plant.B.inverse() * |
| 474 | (Eigen::Matrix<double, 2, 2>::Identity() - |
| 475 | loop_->controller(kHighGearController).plant.A); |
| 476 | |
| 477 | ::Eigen::Matrix<double, 1, 2> FF_sum = FF.colwise().sum(); |
| 478 | int min_FF_sum_index; |
| 479 | const double min_FF_sum = FF_sum.minCoeff(&min_FF_sum_index); |
| 480 | //const double min_K_sum = loop_->K().col(min_FF_sum_index).sum(); |
| 481 | const double high_min_FF_sum = FF_high.col(0).sum(); |
| 482 | |
| 483 | const double adjusted_ff_voltage = ::aos::Clip( |
| 484 | 12.0 * min_FF_sum / high_min_FF_sum, -12.0, 12.0); |
| 485 | return adjusted_ff_voltage / min_FF_sum; |
| 486 | } |
| 487 | |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 488 | void Update() { |
Austin Schuh | 78d5546 | 2014-02-23 01:39:30 -0800 | [diff] [blame] | 489 | const auto &values = constants::GetValues(); |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 490 | // TODO(austin): Observer for the current velocity instead of difference |
| 491 | // calculations. |
Brian Silverman | de8fd55 | 2013-11-03 15:53:42 -0800 | [diff] [blame] | 492 | ++counter_; |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 493 | const double current_left_velocity = |
Brian Silverman | de8fd55 | 2013-11-03 15:53:42 -0800 | [diff] [blame] | 494 | (position_.left_encoder - last_position_.left_encoder) / |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 495 | position_time_delta_; |
| 496 | const double current_right_velocity = |
Brian Silverman | de8fd55 | 2013-11-03 15:53:42 -0800 | [diff] [blame] | 497 | (position_.right_encoder - last_position_.right_encoder) / |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 498 | position_time_delta_; |
| 499 | const double left_motor_speed = |
Austin Schuh | 78d5546 | 2014-02-23 01:39:30 -0800 | [diff] [blame] | 500 | MotorSpeed(values.left_drive, position_.left_shifter_position, |
| 501 | current_left_velocity); |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 502 | const double right_motor_speed = |
Austin Schuh | 78d5546 | 2014-02-23 01:39:30 -0800 | [diff] [blame] | 503 | MotorSpeed(values.right_drive, position_.right_shifter_position, |
| 504 | current_right_velocity); |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 505 | |
Brian Silverman | 61e41fd | 2014-02-16 19:08:50 -0800 | [diff] [blame] | 506 | { |
| 507 | CIMLogging logging; |
| 508 | |
| 509 | // Reset the CIM model to the current conditions to be ready for when we |
| 510 | // shift. |
| 511 | if (IsInGear(left_gear_)) { |
| 512 | logging.left_in_gear = true; |
Brian Silverman | 61e41fd | 2014-02-16 19:08:50 -0800 | [diff] [blame] | 513 | } else { |
| 514 | logging.left_in_gear = false; |
| 515 | } |
| 516 | logging.left_motor_speed = left_motor_speed; |
| 517 | logging.left_velocity = current_left_velocity; |
| 518 | if (IsInGear(right_gear_)) { |
| 519 | logging.right_in_gear = true; |
Brian Silverman | 61e41fd | 2014-02-16 19:08:50 -0800 | [diff] [blame] | 520 | } else { |
| 521 | logging.right_in_gear = false; |
| 522 | } |
| 523 | logging.right_motor_speed = right_motor_speed; |
| 524 | logging.right_velocity = current_right_velocity; |
| 525 | |
| 526 | LOG_STRUCT(DEBUG, "currently", logging); |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 527 | } |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 528 | |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 529 | if (IsInGear(left_gear_) && IsInGear(right_gear_)) { |
| 530 | // FF * X = U (steady state) |
| 531 | const Eigen::Matrix<double, 2, 2> FF = |
| 532 | loop_->B().inverse() * |
| 533 | (Eigen::Matrix<double, 2, 2>::Identity() - loop_->A()); |
| 534 | |
| 535 | // Invert the plant to figure out how the velocity filter would have to |
| 536 | // work |
| 537 | // out in order to filter out the forwards negative inertia. |
| 538 | // This math assumes that the left and right power and velocity are |
| 539 | // equals, |
| 540 | // and that the plant is the same on the left and right. |
| 541 | const double fvel = FilterVelocity(throttle_); |
| 542 | |
| 543 | const double sign_svel = wheel_ * ((fvel > 0.0) ? 1.0 : -1.0); |
| 544 | double steering_velocity; |
| 545 | if (quickturn_) { |
| 546 | steering_velocity = wheel_ * MaxVelocity(); |
| 547 | } else { |
| 548 | steering_velocity = ::std::abs(fvel) * wheel_; |
| 549 | } |
| 550 | const double left_velocity = fvel - steering_velocity; |
| 551 | const double right_velocity = fvel + steering_velocity; |
| 552 | |
| 553 | // Integrate velocity to get the position. |
| 554 | // This position is used to get integral control. |
| 555 | loop_->R << left_velocity, right_velocity; |
| 556 | |
| 557 | if (!quickturn_) { |
| 558 | // K * R = w |
| 559 | Eigen::Matrix<double, 1, 2> equality_k; |
| 560 | equality_k << 1 + sign_svel, -(1 - sign_svel); |
| 561 | const double equality_w = 0.0; |
| 562 | |
| 563 | // Construct a constraint on R by manipulating the constraint on U |
| 564 | ::aos::controls::HPolytope<2> R_poly = ::aos::controls::HPolytope<2>( |
| 565 | U_Poly_.H() * (loop_->K() + FF), |
| 566 | U_Poly_.k() + U_Poly_.H() * loop_->K() * loop_->X_hat); |
| 567 | |
| 568 | // Limit R back inside the box. |
| 569 | loop_->R = CoerceGoal(R_poly, equality_k, equality_w, loop_->R); |
| 570 | } |
| 571 | |
| 572 | const Eigen::Matrix<double, 2, 1> FF_volts = FF * loop_->R; |
| 573 | const Eigen::Matrix<double, 2, 1> U_ideal = |
| 574 | loop_->K() * (loop_->R - loop_->X_hat) + FF_volts; |
| 575 | |
| 576 | for (int i = 0; i < 2; i++) { |
| 577 | loop_->U[i] = ::aos::Clip(U_ideal[i], -12, 12); |
| 578 | } |
Brian Silverman | de8fd55 | 2013-11-03 15:53:42 -0800 | [diff] [blame] | 579 | |
| 580 | // TODO(austin): Model this better. |
| 581 | // TODO(austin): Feed back? |
| 582 | loop_->X_hat = loop_->A() * loop_->X_hat + loop_->B() * loop_->U; |
Brian Silverman | 718b1d7 | 2013-10-28 16:22:45 -0700 | [diff] [blame] | 583 | } else { |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 584 | // Any motor is not in gear. Speed match. |
| 585 | ::Eigen::Matrix<double, 1, 1> R_left; |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 586 | ::Eigen::Matrix<double, 1, 1> R_right; |
Austin Schuh | b5afb69 | 2014-03-02 11:54:11 -0800 | [diff] [blame] | 587 | R_left(0, 0) = left_motor_speed; |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 588 | R_right(0, 0) = right_motor_speed; |
Austin Schuh | b5afb69 | 2014-03-02 11:54:11 -0800 | [diff] [blame] | 589 | |
| 590 | const double wiggle = |
Brian Silverman | f970f2c | 2014-03-22 19:34:30 -0700 | [diff] [blame] | 591 | (static_cast<double>((counter_ % 20) / 10) - 0.5) * 5.0; |
Austin Schuh | b5afb69 | 2014-03-02 11:54:11 -0800 | [diff] [blame] | 592 | |
Brian Silverman | 5665832 | 2014-03-22 16:57:22 -0700 | [diff] [blame] | 593 | loop_->U(0, 0) = ::aos::Clip( |
| 594 | (R_left / Kv)(0, 0) + (IsInGear(left_gear_) ? 0 : wiggle), |
| 595 | -12.0, 12.0); |
| 596 | loop_->U(1, 0) = ::aos::Clip( |
| 597 | (R_right / Kv)(0, 0) + (IsInGear(right_gear_) ? 0 : wiggle), |
| 598 | -12.0, 12.0); |
Brian Silverman | de8fd55 | 2013-11-03 15:53:42 -0800 | [diff] [blame] | 599 | loop_->U *= 12.0 / position_.battery_voltage; |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 600 | } |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | void SendMotors(Drivetrain::Output *output) { |
Brian Silverman | de8fd55 | 2013-11-03 15:53:42 -0800 | [diff] [blame] | 604 | if (output != NULL) { |
| 605 | output->left_voltage = loop_->U(0, 0); |
| 606 | output->right_voltage = loop_->U(1, 0); |
Brian Silverman | 61e41fd | 2014-02-16 19:08:50 -0800 | [diff] [blame] | 607 | output->left_high = left_gear_ == HIGH || left_gear_ == SHIFTING_UP; |
| 608 | output->right_high = right_gear_ == HIGH || right_gear_ == SHIFTING_UP; |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 609 | } |
| 610 | } |
| 611 | |
| 612 | private: |
| 613 | const ::aos::controls::HPolytope<2> U_Poly_; |
| 614 | |
| 615 | ::std::unique_ptr<StateFeedbackLoop<2, 2, 2>> loop_; |
| 616 | |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 617 | const double ttrust_; |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 618 | double wheel_; |
| 619 | double throttle_; |
| 620 | bool quickturn_; |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 621 | int stale_count_; |
| 622 | double position_time_delta_; |
| 623 | Gear left_gear_; |
| 624 | Gear right_gear_; |
| 625 | Drivetrain::Position last_position_; |
| 626 | Drivetrain::Position position_; |
Brian Silverman | de8fd55 | 2013-11-03 15:53:42 -0800 | [diff] [blame] | 627 | int counter_; |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 628 | }; |
Brian Silverman | cec3c8d | 2013-12-20 21:04:55 -0800 | [diff] [blame] | 629 | constexpr double PolyDrivetrain::kStallTorque; |
| 630 | constexpr double PolyDrivetrain::kStallCurrent; |
| 631 | constexpr double PolyDrivetrain::kFreeSpeed; |
| 632 | constexpr double PolyDrivetrain::kFreeCurrent; |
| 633 | constexpr double PolyDrivetrain::J; |
| 634 | constexpr double PolyDrivetrain::m; |
| 635 | constexpr double PolyDrivetrain::rb; |
| 636 | constexpr double PolyDrivetrain::kWheelRadius; |
| 637 | constexpr double PolyDrivetrain::kR; |
| 638 | constexpr double PolyDrivetrain::Kv; |
| 639 | constexpr double PolyDrivetrain::Kt; |
| 640 | |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 641 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 642 | void DrivetrainLoop::RunIteration(const Drivetrain::Goal *goal, |
| 643 | const Drivetrain::Position *position, |
| 644 | Drivetrain::Output *output, |
Ben Fredrickson | 890c3fe | 2014-03-02 00:15:16 +0000 | [diff] [blame] | 645 | Drivetrain::Status * status) { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 646 | // TODO(aschuh): These should be members of the class. |
| 647 | static DrivetrainMotorsSS dt_closedloop; |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 648 | static PolyDrivetrain dt_openloop; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 649 | |
| 650 | bool bad_pos = false; |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 651 | if (position == nullptr) { |
Brian Silverman | 50a9d03 | 2014-02-16 17:20:57 -0800 | [diff] [blame] | 652 | LOG_INTERVAL(no_position_); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 653 | bad_pos = true; |
| 654 | } |
Brian Silverman | 9c9ab42 | 2014-02-25 13:42:53 -0800 | [diff] [blame] | 655 | no_position_.Print(); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 656 | |
| 657 | double wheel = goal->steering; |
| 658 | double throttle = goal->throttle; |
| 659 | bool quickturn = goal->quickturn; |
| 660 | bool highgear = goal->highgear; |
| 661 | |
| 662 | bool control_loop_driving = goal->control_loop_driving; |
| 663 | double left_goal = goal->left_goal; |
| 664 | double right_goal = goal->right_goal; |
| 665 | |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 666 | dt_closedloop.SetGoal(left_goal, goal->left_velocity_goal, right_goal, |
| 667 | goal->right_velocity_goal); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 668 | if (!bad_pos) { |
| 669 | const double left_encoder = position->left_encoder; |
| 670 | const double right_encoder = position->right_encoder; |
Brian Silverman | 6bf0d3c | 2014-03-08 12:52:54 -0800 | [diff] [blame] | 671 | if (gyro_reading.FetchLatest()) { |
Brian Silverman | 74e5b4e | 2014-03-09 16:58:58 -0700 | [diff] [blame] | 672 | LOG_STRUCT(DEBUG, "using", *gyro_reading.get()); |
| 673 | dt_closedloop.SetPosition(left_encoder, right_encoder, |
| 674 | gyro_reading->angle, control_loop_driving); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 675 | } else { |
| 676 | dt_closedloop.SetRawPosition(left_encoder, right_encoder); |
| 677 | } |
| 678 | } |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 679 | dt_openloop.SetPosition(position); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 680 | dt_openloop.SetGoal(wheel, throttle, quickturn, highgear); |
| 681 | dt_openloop.Update(); |
Ben Fredrickson | 890c3fe | 2014-03-02 00:15:16 +0000 | [diff] [blame] | 682 | |
Brian Silverman | 2845c4c | 2013-03-16 19:54:08 -0700 | [diff] [blame] | 683 | if (control_loop_driving) { |
Ben Fredrickson | 890c3fe | 2014-03-02 00:15:16 +0000 | [diff] [blame] | 684 | dt_closedloop.Update(output == NULL); |
Brian Silverman | 2845c4c | 2013-03-16 19:54:08 -0700 | [diff] [blame] | 685 | dt_closedloop.SendMotors(output); |
| 686 | } else { |
| 687 | dt_openloop.SendMotors(output); |
Austin Schuh | b5afb69 | 2014-03-02 11:54:11 -0800 | [diff] [blame] | 688 | if (output) { |
| 689 | dt_closedloop.SetExternalMotors(output->left_voltage, |
| 690 | output->right_voltage); |
| 691 | } |
Ben Fredrickson | 890c3fe | 2014-03-02 00:15:16 +0000 | [diff] [blame] | 692 | dt_closedloop.Update(output == NULL); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 693 | } |
Ben Fredrickson | 890c3fe | 2014-03-02 00:15:16 +0000 | [diff] [blame] | 694 | |
| 695 | // set the output status of the controll loop state |
| 696 | if (status) { |
| 697 | bool done = false; |
| 698 | if (goal) { |
| 699 | done = ((::std::abs(goal->left_goal - |
| 700 | dt_closedloop.GetEstimatedLeftEncoder()) < |
| 701 | constants::GetValues().drivetrain_done_distance) && |
| 702 | (::std::abs(goal->right_goal - |
| 703 | dt_closedloop.GetEstimatedRightEncoder()) < |
| 704 | constants::GetValues().drivetrain_done_distance)); |
| 705 | } |
| 706 | status->is_done = done; |
| 707 | status->robot_speed = dt_closedloop.GetEstimatedRobotSpeed(); |
Austin Schuh | 577edf6 | 2014-04-13 10:33:05 -0700 | [diff] [blame] | 708 | status->filtered_left_position = dt_closedloop.GetEstimatedLeftEncoder(); |
| 709 | status->filtered_right_position = dt_closedloop.GetEstimatedRightEncoder(); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 710 | } |
| 711 | } |
| 712 | |
| 713 | } // namespace control_loops |
| 714 | } // namespace frc971 |