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