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