Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 1 | #include "y2014_bot3/control_loops/drivetrain/drivetrain.h" |
Comran Morshed | e9b1292 | 2015-11-04 19:46:48 +0000 | [diff] [blame] | 2 | |
Comran Morshed | e9b1292 | 2015-11-04 19:46:48 +0000 | [diff] [blame] | 3 | #include <stdio.h> |
| 4 | #include <sched.h> |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 5 | #include <cmath> |
Comran Morshed | e9b1292 | 2015-11-04 19:46:48 +0000 | [diff] [blame] | 6 | #include <memory> |
Comran Morshed | e9b1292 | 2015-11-04 19:46:48 +0000 | [diff] [blame] | 7 | #include "Eigen/Dense" |
| 8 | |
| 9 | #include "aos/common/logging/logging.h" |
| 10 | #include "aos/common/controls/polytope.h" |
| 11 | #include "aos/common/commonmath.h" |
| 12 | #include "aos/common/logging/queue_logging.h" |
| 13 | #include "aos/common/logging/matrix_logging.h" |
| 14 | |
Comran Morshed | e9b1292 | 2015-11-04 19:46:48 +0000 | [diff] [blame] | 15 | #include "frc971/control_loops/state_feedback_loop.h" |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 16 | #include "frc971/control_loops/coerce_goal.h" |
| 17 | #include "y2014_bot3/control_loops/drivetrain/polydrivetrain_cim_plant.h" |
| 18 | #include "y2014_bot3/control_loops/drivetrain/drivetrain.q.h" |
| 19 | #include "frc971/queues/gyro.q.h" |
| 20 | #include "frc971/shifter_hall_effect.h" |
| 21 | #include "y2014_bot3/control_loops/drivetrain/drivetrain_dog_motor_plant.h" |
| 22 | #include "y2014_bot3/control_loops/drivetrain/polydrivetrain_dog_motor_plant.h" |
Comran Morshed | e9b1292 | 2015-11-04 19:46:48 +0000 | [diff] [blame] | 23 | |
| 24 | using ::frc971::sensors::gyro_reading; |
Comran Morshed | e9b1292 | 2015-11-04 19:46:48 +0000 | [diff] [blame] | 25 | |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 26 | namespace y2014_bot3 { |
Comran Morshed | e9b1292 | 2015-11-04 19:46:48 +0000 | [diff] [blame] | 27 | namespace control_loops { |
| 28 | |
| 29 | class DrivetrainMotorsSS { |
| 30 | public: |
| 31 | class LimitedDrivetrainLoop : public StateFeedbackLoop<4, 2, 2> { |
| 32 | public: |
| 33 | LimitedDrivetrainLoop(StateFeedbackLoop<4, 2, 2> &&loop) |
| 34 | : StateFeedbackLoop<4, 2, 2>(::std::move(loop)), |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 35 | U_Poly_((Eigen::Matrix<double, 4, 2>() << 1, 0, -1, 0, 0, 1, 0, -1) |
| 36 | .finished(), |
| 37 | (Eigen::Matrix<double, 4, 1>() << 12.0, 12.0, 12.0, 12.0) |
| 38 | .finished()) { |
Comran Morshed | e9b1292 | 2015-11-04 19:46:48 +0000 | [diff] [blame] | 39 | ::aos::controls::HPolytope<0>::Init(); |
| 40 | T << 1, -1, 1, 1; |
| 41 | T_inverse = T.inverse(); |
| 42 | } |
| 43 | |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 44 | bool output_was_capped() const { return output_was_capped_; } |
Comran Morshed | e9b1292 | 2015-11-04 19:46:48 +0000 | [diff] [blame] | 45 | |
| 46 | private: |
| 47 | virtual void CapU() { |
| 48 | const Eigen::Matrix<double, 4, 1> error = R() - X_hat(); |
| 49 | |
| 50 | if (::std::abs(U(0, 0)) > 12.0 || ::std::abs(U(1, 0)) > 12.0) { |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 51 | mutable_U() = |
| 52 | U() * 12.0 / ::std::max(::std::abs(U(0, 0)), ::std::abs(U(1, 0))); |
| 53 | LOG_MATRIX(DEBUG, "U is now", U()); |
| 54 | // TODO(Austin): Figure out why the polytope stuff wasn't working and |
| 55 | // remove this hack. |
Comran Morshed | e9b1292 | 2015-11-04 19:46:48 +0000 | [diff] [blame] | 56 | output_was_capped_ = true; |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 57 | return; |
| 58 | |
Comran Morshed | e9b1292 | 2015-11-04 19:46:48 +0000 | [diff] [blame] | 59 | LOG_MATRIX(DEBUG, "U at start", U()); |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 60 | LOG_MATRIX(DEBUG, "R at start", R()); |
| 61 | LOG_MATRIX(DEBUG, "Xhat at start", X_hat()); |
Comran Morshed | e9b1292 | 2015-11-04 19:46:48 +0000 | [diff] [blame] | 62 | |
| 63 | Eigen::Matrix<double, 2, 2> position_K; |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 64 | position_K << K(0, 0), K(0, 2), K(1, 0), K(1, 2); |
Comran Morshed | e9b1292 | 2015-11-04 19:46:48 +0000 | [diff] [blame] | 65 | Eigen::Matrix<double, 2, 2> velocity_K; |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 66 | velocity_K << K(0, 1), K(0, 3), K(1, 1), K(1, 3); |
Comran Morshed | e9b1292 | 2015-11-04 19:46:48 +0000 | [diff] [blame] | 67 | |
| 68 | Eigen::Matrix<double, 2, 1> position_error; |
| 69 | position_error << error(0, 0), error(2, 0); |
| 70 | const auto drive_error = T_inverse * position_error; |
| 71 | Eigen::Matrix<double, 2, 1> velocity_error; |
| 72 | velocity_error << error(1, 0), error(3, 0); |
| 73 | LOG_MATRIX(DEBUG, "error", error); |
| 74 | |
| 75 | const auto &poly = U_Poly_; |
| 76 | const Eigen::Matrix<double, 4, 2> pos_poly_H = |
| 77 | poly.H() * position_K * T; |
| 78 | const Eigen::Matrix<double, 4, 1> pos_poly_k = |
| 79 | poly.k() - poly.H() * velocity_K * velocity_error; |
| 80 | const ::aos::controls::HPolytope<2> pos_poly(pos_poly_H, pos_poly_k); |
| 81 | |
| 82 | Eigen::Matrix<double, 2, 1> adjusted_pos_error; |
| 83 | { |
| 84 | const auto &P = drive_error; |
| 85 | |
| 86 | Eigen::Matrix<double, 1, 2> L45; |
| 87 | L45 << ::aos::sign(P(1, 0)), -::aos::sign(P(0, 0)); |
| 88 | const double w45 = 0; |
| 89 | |
| 90 | Eigen::Matrix<double, 1, 2> LH; |
| 91 | if (::std::abs(P(0, 0)) > ::std::abs(P(1, 0))) { |
| 92 | LH << 0, 1; |
| 93 | } else { |
| 94 | LH << 1, 0; |
| 95 | } |
| 96 | const double wh = LH.dot(P); |
| 97 | |
| 98 | Eigen::Matrix<double, 2, 2> standard; |
| 99 | standard << L45, LH; |
| 100 | Eigen::Matrix<double, 2, 1> W; |
| 101 | W << w45, wh; |
| 102 | const Eigen::Matrix<double, 2, 1> intersection = |
| 103 | standard.inverse() * W; |
| 104 | |
| 105 | bool is_inside_h; |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 106 | const auto adjusted_pos_error_h = frc971::control_loops::DoCoerceGoal( |
| 107 | pos_poly, LH, wh, drive_error, &is_inside_h); |
Comran Morshed | e9b1292 | 2015-11-04 19:46:48 +0000 | [diff] [blame] | 108 | const auto adjusted_pos_error_45 = |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 109 | frc971::control_loops::DoCoerceGoal(pos_poly, L45, w45, |
| 110 | intersection, nullptr); |
Comran Morshed | e9b1292 | 2015-11-04 19:46:48 +0000 | [diff] [blame] | 111 | if (pos_poly.IsInside(intersection)) { |
| 112 | adjusted_pos_error = adjusted_pos_error_h; |
| 113 | } else { |
| 114 | if (is_inside_h) { |
| 115 | if (adjusted_pos_error_h.norm() > adjusted_pos_error_45.norm()) { |
| 116 | adjusted_pos_error = adjusted_pos_error_h; |
| 117 | } else { |
| 118 | adjusted_pos_error = adjusted_pos_error_45; |
| 119 | } |
| 120 | } else { |
| 121 | adjusted_pos_error = adjusted_pos_error_45; |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | LOG_MATRIX(DEBUG, "adjusted_pos_error", adjusted_pos_error); |
| 127 | mutable_U() = |
| 128 | velocity_K * velocity_error + position_K * T * adjusted_pos_error; |
| 129 | LOG_MATRIX(DEBUG, "U is now", U()); |
| 130 | } else { |
| 131 | output_was_capped_ = false; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | const ::aos::controls::HPolytope<2> U_Poly_; |
| 136 | Eigen::Matrix<double, 2, 2> T, T_inverse; |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 137 | bool output_was_capped_ = false; |
Comran Morshed | e9b1292 | 2015-11-04 19:46:48 +0000 | [diff] [blame] | 138 | }; |
| 139 | |
| 140 | DrivetrainMotorsSS() |
| 141 | : loop_(new LimitedDrivetrainLoop( |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 142 | ::y2014_bot3::control_loops::MakeDrivetrainLoop())), |
Comran Morshed | e9b1292 | 2015-11-04 19:46:48 +0000 | [diff] [blame] | 143 | filtered_offset_(0.0), |
| 144 | gyro_(0.0), |
| 145 | left_goal_(0.0), |
| 146 | right_goal_(0.0), |
| 147 | raw_left_(0.0), |
| 148 | raw_right_(0.0) { |
| 149 | // Low gear on both. |
| 150 | loop_->set_controller_index(0); |
| 151 | } |
| 152 | |
| 153 | void SetGoal(double left, double left_velocity, double right, |
| 154 | double right_velocity) { |
| 155 | left_goal_ = left; |
| 156 | right_goal_ = right; |
| 157 | loop_->mutable_R() << left, left_velocity, right, right_velocity; |
| 158 | } |
| 159 | void SetRawPosition(double left, double right) { |
| 160 | raw_right_ = right; |
| 161 | raw_left_ = left; |
| 162 | Eigen::Matrix<double, 2, 1> Y; |
| 163 | Y << left + filtered_offset_, right - filtered_offset_; |
| 164 | loop_->Correct(Y); |
| 165 | } |
| 166 | void SetPosition(double left, double right, double gyro) { |
| 167 | // Decay the offset quickly because this gyro is great. |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 168 | const double offset = (right - left - gyro * kDrivetrainTurnWidth) / 2.0; |
Comran Morshed | e9b1292 | 2015-11-04 19:46:48 +0000 | [diff] [blame] | 169 | filtered_offset_ = 0.25 * offset + 0.75 * filtered_offset_; |
| 170 | gyro_ = gyro; |
| 171 | SetRawPosition(left, right); |
| 172 | } |
| 173 | |
| 174 | void SetExternalMotors(double left_voltage, double right_voltage) { |
| 175 | loop_->mutable_U() << left_voltage, right_voltage; |
| 176 | } |
| 177 | |
| 178 | void Update(bool stop_motors, bool enable_control_loop) { |
| 179 | if (enable_control_loop) { |
| 180 | loop_->Update(stop_motors); |
| 181 | } else { |
| 182 | if (stop_motors) { |
| 183 | loop_->mutable_U().setZero(); |
| 184 | loop_->mutable_U_uncapped().setZero(); |
| 185 | } |
Austin Schuh | c2b7774 | 2015-11-26 16:18:27 -0800 | [diff] [blame^] | 186 | loop_->UpdateObserver(loop_->U()); |
Comran Morshed | e9b1292 | 2015-11-04 19:46:48 +0000 | [diff] [blame] | 187 | } |
| 188 | ::Eigen::Matrix<double, 4, 1> E = loop_->R() - loop_->X_hat(); |
| 189 | LOG_MATRIX(DEBUG, "E", E); |
| 190 | } |
| 191 | |
| 192 | double GetEstimatedRobotSpeed() const { |
| 193 | // lets just call the average of left and right velocities close enough |
| 194 | return (loop_->X_hat(1, 0) + loop_->X_hat(3, 0)) / 2; |
| 195 | } |
| 196 | |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 197 | double GetEstimatedLeftEncoder() const { return loop_->X_hat(0, 0); } |
Comran Morshed | e9b1292 | 2015-11-04 19:46:48 +0000 | [diff] [blame] | 198 | |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 199 | double GetEstimatedRightEncoder() const { return loop_->X_hat(2, 0); } |
Comran Morshed | e9b1292 | 2015-11-04 19:46:48 +0000 | [diff] [blame] | 200 | |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 201 | bool OutputWasCapped() const { return loop_->output_was_capped(); } |
Comran Morshed | e9b1292 | 2015-11-04 19:46:48 +0000 | [diff] [blame] | 202 | |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 203 | void SendMotors(DrivetrainQueue::Output *output) const { |
Comran Morshed | e9b1292 | 2015-11-04 19:46:48 +0000 | [diff] [blame] | 204 | if (output) { |
| 205 | output->left_voltage = loop_->U(0, 0); |
| 206 | output->right_voltage = loop_->U(1, 0); |
| 207 | output->left_high = false; |
| 208 | output->right_high = false; |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | const LimitedDrivetrainLoop &loop() const { return *loop_; } |
| 213 | |
| 214 | private: |
| 215 | ::std::unique_ptr<LimitedDrivetrainLoop> loop_; |
| 216 | |
| 217 | double filtered_offset_; |
| 218 | double gyro_; |
| 219 | double left_goal_; |
| 220 | double right_goal_; |
| 221 | double raw_left_; |
| 222 | double raw_right_; |
| 223 | }; |
| 224 | |
| 225 | class PolyDrivetrain { |
| 226 | public: |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 227 | enum Gear { HIGH, LOW }; |
Comran Morshed | e9b1292 | 2015-11-04 19:46:48 +0000 | [diff] [blame] | 228 | // Stall Torque in N m |
| 229 | static constexpr double kStallTorque = 2.42; |
| 230 | // Stall Current in Amps |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 231 | static constexpr double kStallCurrent = 133.0; |
Comran Morshed | e9b1292 | 2015-11-04 19:46:48 +0000 | [diff] [blame] | 232 | // Free Speed in RPM. Used number from last year. |
| 233 | static constexpr double kFreeSpeed = 4650.0; |
| 234 | // Free Current in Amps |
| 235 | static constexpr double kFreeCurrent = 2.7; |
| 236 | // Moment of inertia of the drivetrain in kg m^2 |
| 237 | // Just borrowed from last year. |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 238 | static constexpr double J = 10; |
Comran Morshed | e9b1292 | 2015-11-04 19:46:48 +0000 | [diff] [blame] | 239 | // Mass of the robot, in kg. |
| 240 | static constexpr double m = 68; |
| 241 | // Radius of the robot, in meters (from last year). |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 242 | static constexpr double rb = 0.66675 / 2.0; |
Comran Morshed | e9b1292 | 2015-11-04 19:46:48 +0000 | [diff] [blame] | 243 | static constexpr double kWheelRadius = 0.04445; |
| 244 | // Resistance of the motor, divided by the number of motors. |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 245 | static constexpr double kR = |
| 246 | (12.0 / kStallCurrent / 4 + 0.03) / (0.93 * 0.93); |
Comran Morshed | e9b1292 | 2015-11-04 19:46:48 +0000 | [diff] [blame] | 247 | // Motor velocity constant |
| 248 | static constexpr double Kv = |
| 249 | ((kFreeSpeed / 60.0 * 2.0 * M_PI) / (12.0 - kR * kFreeCurrent)); |
| 250 | // Torque constant |
| 251 | static constexpr double Kt = kStallTorque / kStallCurrent; |
| 252 | |
| 253 | PolyDrivetrain() |
| 254 | : U_Poly_((Eigen::Matrix<double, 4, 2>() << /*[[*/ 1, 0 /*]*/, |
| 255 | /*[*/ -1, 0 /*]*/, |
| 256 | /*[*/ 0, 1 /*]*/, |
| 257 | /*[*/ 0, -1 /*]]*/).finished(), |
| 258 | (Eigen::Matrix<double, 4, 1>() << /*[[*/ 12 /*]*/, |
| 259 | /*[*/ 12 /*]*/, |
| 260 | /*[*/ 12 /*]*/, |
| 261 | /*[*/ 12 /*]]*/).finished()), |
| 262 | loop_(new StateFeedbackLoop<2, 2, 2>( |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 263 | ::y2014_bot3::control_loops::MakeVelocityDrivetrainLoop())), |
Comran Morshed | e9b1292 | 2015-11-04 19:46:48 +0000 | [diff] [blame] | 264 | ttrust_(1.1), |
| 265 | wheel_(0.0), |
| 266 | throttle_(0.0), |
| 267 | quickturn_(false), |
| 268 | stale_count_(0), |
| 269 | position_time_delta_(0.01), |
| 270 | left_gear_(LOW), |
| 271 | right_gear_(LOW), |
| 272 | counter_(0) { |
Comran Morshed | e9b1292 | 2015-11-04 19:46:48 +0000 | [diff] [blame] | 273 | last_position_.Zero(); |
| 274 | position_.Zero(); |
| 275 | } |
Comran Morshed | e9b1292 | 2015-11-04 19:46:48 +0000 | [diff] [blame] | 276 | |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 277 | static double MotorSpeed(bool high_gear, double velocity) { |
| 278 | if (high_gear) { |
| 279 | return velocity / kDrivetrainHighGearRatio / kWheelRadius; |
Comran Morshed | e9b1292 | 2015-11-04 19:46:48 +0000 | [diff] [blame] | 280 | } else { |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 281 | return velocity / kDrivetrainLowGearRatio / kWheelRadius; |
Comran Morshed | e9b1292 | 2015-11-04 19:46:48 +0000 | [diff] [blame] | 282 | } |
| 283 | } |
| 284 | |
| 285 | void SetGoal(double wheel, double throttle, bool quickturn, bool highgear) { |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 286 | const double kWheelNonLinearity = 0.5; |
Comran Morshed | e9b1292 | 2015-11-04 19:46:48 +0000 | [diff] [blame] | 287 | // Apply a sin function that's scaled to make it feel better. |
| 288 | const double angular_range = M_PI_2 * kWheelNonLinearity; |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 289 | |
| 290 | quickturn_ = quickturn; |
Comran Morshed | e9b1292 | 2015-11-04 19:46:48 +0000 | [diff] [blame] | 291 | wheel_ = sin(angular_range * wheel) / sin(angular_range); |
| 292 | wheel_ = sin(angular_range * wheel_) / sin(angular_range); |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 293 | if (!quickturn_) { |
| 294 | wheel_ *= 1.5; |
| 295 | } |
Comran Morshed | e9b1292 | 2015-11-04 19:46:48 +0000 | [diff] [blame] | 296 | |
| 297 | static const double kThrottleDeadband = 0.05; |
| 298 | if (::std::abs(throttle) < kThrottleDeadband) { |
| 299 | throttle_ = 0; |
| 300 | } else { |
| 301 | throttle_ = copysign((::std::abs(throttle) - kThrottleDeadband) / |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 302 | (1.0 - kThrottleDeadband), |
| 303 | throttle); |
Comran Morshed | e9b1292 | 2015-11-04 19:46:48 +0000 | [diff] [blame] | 304 | } |
| 305 | |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 306 | Gear requested_gear = highgear ? HIGH : LOW; |
| 307 | left_gear_ = right_gear_ = requested_gear; |
Comran Morshed | e9b1292 | 2015-11-04 19:46:48 +0000 | [diff] [blame] | 308 | } |
| 309 | |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 310 | void SetPosition(const DrivetrainQueue::Position *position) { |
Comran Morshed | e9b1292 | 2015-11-04 19:46:48 +0000 | [diff] [blame] | 311 | if (position == NULL) { |
| 312 | ++stale_count_; |
| 313 | } else { |
| 314 | last_position_ = position_; |
| 315 | position_ = *position; |
| 316 | position_time_delta_ = (stale_count_ + 1) * 0.01; |
| 317 | stale_count_ = 0; |
| 318 | } |
Comran Morshed | e9b1292 | 2015-11-04 19:46:48 +0000 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | double FilterVelocity(double throttle) { |
| 322 | const Eigen::Matrix<double, 2, 2> FF = |
| 323 | loop_->B().inverse() * |
| 324 | (Eigen::Matrix<double, 2, 2>::Identity() - loop_->A()); |
| 325 | |
| 326 | constexpr int kHighGearController = 3; |
| 327 | const Eigen::Matrix<double, 2, 2> FF_high = |
| 328 | loop_->controller(kHighGearController).plant.B().inverse() * |
| 329 | (Eigen::Matrix<double, 2, 2>::Identity() - |
| 330 | loop_->controller(kHighGearController).plant.A()); |
| 331 | |
| 332 | ::Eigen::Matrix<double, 1, 2> FF_sum = FF.colwise().sum(); |
| 333 | int min_FF_sum_index; |
| 334 | const double min_FF_sum = FF_sum.minCoeff(&min_FF_sum_index); |
| 335 | const double min_K_sum = loop_->K().col(min_FF_sum_index).sum(); |
| 336 | const double high_min_FF_sum = FF_high.col(0).sum(); |
| 337 | |
| 338 | const double adjusted_ff_voltage = ::aos::Clip( |
| 339 | throttle * 12.0 * min_FF_sum / high_min_FF_sum, -12.0, 12.0); |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 340 | return (adjusted_ff_voltage + |
| 341 | ttrust_ * min_K_sum * (loop_->X_hat(0, 0) + loop_->X_hat(1, 0)) / |
| 342 | 2.0) / |
| 343 | (ttrust_ * min_K_sum + min_FF_sum); |
Comran Morshed | e9b1292 | 2015-11-04 19:46:48 +0000 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | double MaxVelocity() { |
| 347 | const Eigen::Matrix<double, 2, 2> FF = |
| 348 | loop_->B().inverse() * |
| 349 | (Eigen::Matrix<double, 2, 2>::Identity() - loop_->A()); |
| 350 | |
| 351 | constexpr int kHighGearController = 3; |
| 352 | const Eigen::Matrix<double, 2, 2> FF_high = |
| 353 | loop_->controller(kHighGearController).plant.B().inverse() * |
| 354 | (Eigen::Matrix<double, 2, 2>::Identity() - |
| 355 | loop_->controller(kHighGearController).plant.A()); |
| 356 | |
| 357 | ::Eigen::Matrix<double, 1, 2> FF_sum = FF.colwise().sum(); |
| 358 | int min_FF_sum_index; |
| 359 | const double min_FF_sum = FF_sum.minCoeff(&min_FF_sum_index); |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 360 | // const double min_K_sum = loop_->K().col(min_FF_sum_index).sum(); |
Comran Morshed | e9b1292 | 2015-11-04 19:46:48 +0000 | [diff] [blame] | 361 | const double high_min_FF_sum = FF_high.col(0).sum(); |
| 362 | |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 363 | const double adjusted_ff_voltage = |
| 364 | ::aos::Clip(12.0 * min_FF_sum / high_min_FF_sum, -12.0, 12.0); |
Comran Morshed | e9b1292 | 2015-11-04 19:46:48 +0000 | [diff] [blame] | 365 | return adjusted_ff_voltage / min_FF_sum; |
| 366 | } |
| 367 | |
| 368 | void Update() { |
| 369 | // TODO(austin): Observer for the current velocity instead of difference |
| 370 | // calculations. |
| 371 | ++counter_; |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 372 | |
Comran Morshed | e9b1292 | 2015-11-04 19:46:48 +0000 | [diff] [blame] | 373 | const double current_left_velocity = |
| 374 | (position_.left_encoder - last_position_.left_encoder) / |
| 375 | position_time_delta_; |
| 376 | const double current_right_velocity = |
| 377 | (position_.right_encoder - last_position_.right_encoder) / |
| 378 | position_time_delta_; |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 379 | const double left_motor_speed = MotorSpeed(left_gear_ == HIGH, |
| 380 | current_left_velocity); |
| 381 | const double right_motor_speed = MotorSpeed(right_gear_ == HIGH, |
| 382 | current_right_velocity); |
Comran Morshed | e9b1292 | 2015-11-04 19:46:48 +0000 | [diff] [blame] | 383 | |
| 384 | { |
| 385 | CIMLogging logging; |
| 386 | |
| 387 | // Reset the CIM model to the current conditions to be ready for when we |
| 388 | // shift. |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 389 | logging.left_in_gear = true; |
Comran Morshed | e9b1292 | 2015-11-04 19:46:48 +0000 | [diff] [blame] | 390 | logging.left_motor_speed = left_motor_speed; |
| 391 | logging.left_velocity = current_left_velocity; |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 392 | |
| 393 | logging.right_in_gear = true; |
Comran Morshed | e9b1292 | 2015-11-04 19:46:48 +0000 | [diff] [blame] | 394 | logging.right_motor_speed = right_motor_speed; |
| 395 | logging.right_velocity = current_right_velocity; |
| 396 | |
| 397 | LOG_STRUCT(DEBUG, "currently", logging); |
| 398 | } |
| 399 | |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 400 | { |
Comran Morshed | e9b1292 | 2015-11-04 19:46:48 +0000 | [diff] [blame] | 401 | // FF * X = U (steady state) |
| 402 | const Eigen::Matrix<double, 2, 2> FF = |
| 403 | loop_->B().inverse() * |
| 404 | (Eigen::Matrix<double, 2, 2>::Identity() - loop_->A()); |
| 405 | |
| 406 | // Invert the plant to figure out how the velocity filter would have to |
| 407 | // work |
| 408 | // out in order to filter out the forwards negative inertia. |
| 409 | // This math assumes that the left and right power and velocity are |
| 410 | // equals, |
| 411 | // and that the plant is the same on the left and right. |
| 412 | const double fvel = FilterVelocity(throttle_); |
| 413 | |
| 414 | const double sign_svel = wheel_ * ((fvel > 0.0) ? 1.0 : -1.0); |
| 415 | double steering_velocity; |
| 416 | if (quickturn_) { |
| 417 | steering_velocity = wheel_ * MaxVelocity(); |
| 418 | } else { |
| 419 | steering_velocity = ::std::abs(fvel) * wheel_; |
| 420 | } |
| 421 | const double left_velocity = fvel - steering_velocity; |
| 422 | const double right_velocity = fvel + steering_velocity; |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 423 | LOG(DEBUG, "l=%f r=%f\n", left_velocity, right_velocity); |
Comran Morshed | e9b1292 | 2015-11-04 19:46:48 +0000 | [diff] [blame] | 424 | |
| 425 | // Integrate velocity to get the position. |
| 426 | // This position is used to get integral control. |
| 427 | loop_->mutable_R() << left_velocity, right_velocity; |
| 428 | |
| 429 | if (!quickturn_) { |
| 430 | // K * R = w |
| 431 | Eigen::Matrix<double, 1, 2> equality_k; |
| 432 | equality_k << 1 + sign_svel, -(1 - sign_svel); |
| 433 | const double equality_w = 0.0; |
| 434 | |
| 435 | // Construct a constraint on R by manipulating the constraint on U |
| 436 | ::aos::controls::HPolytope<2> R_poly = ::aos::controls::HPolytope<2>( |
| 437 | U_Poly_.H() * (loop_->K() + FF), |
| 438 | U_Poly_.k() + U_Poly_.H() * loop_->K() * loop_->X_hat()); |
| 439 | |
| 440 | // Limit R back inside the box. |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 441 | loop_->mutable_R() = frc971::control_loops::CoerceGoal( |
| 442 | R_poly, equality_k, equality_w, loop_->R()); |
Comran Morshed | e9b1292 | 2015-11-04 19:46:48 +0000 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | const Eigen::Matrix<double, 2, 1> FF_volts = FF * loop_->R(); |
| 446 | const Eigen::Matrix<double, 2, 1> U_ideal = |
| 447 | loop_->K() * (loop_->R() - loop_->X_hat()) + FF_volts; |
| 448 | |
| 449 | for (int i = 0; i < 2; i++) { |
| 450 | loop_->mutable_U()[i] = ::aos::Clip(U_ideal[i], -12, 12); |
| 451 | } |
| 452 | |
| 453 | // TODO(austin): Model this better. |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 454 | // TODO(austin): Feedback? |
Comran Morshed | e9b1292 | 2015-11-04 19:46:48 +0000 | [diff] [blame] | 455 | loop_->mutable_X_hat() = |
| 456 | loop_->A() * loop_->X_hat() + loop_->B() * loop_->U(); |
Comran Morshed | e9b1292 | 2015-11-04 19:46:48 +0000 | [diff] [blame] | 457 | } |
| 458 | } |
| 459 | |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 460 | void SendMotors(DrivetrainQueue::Output *output) { |
Comran Morshed | e9b1292 | 2015-11-04 19:46:48 +0000 | [diff] [blame] | 461 | if (output != NULL) { |
| 462 | output->left_voltage = loop_->U(0, 0); |
| 463 | output->right_voltage = loop_->U(1, 0); |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 464 | output->left_high = left_gear_ == HIGH; |
| 465 | output->right_high = right_gear_ == HIGH; |
Comran Morshed | e9b1292 | 2015-11-04 19:46:48 +0000 | [diff] [blame] | 466 | } |
| 467 | } |
| 468 | |
| 469 | private: |
| 470 | const ::aos::controls::HPolytope<2> U_Poly_; |
| 471 | |
| 472 | ::std::unique_ptr<StateFeedbackLoop<2, 2, 2>> loop_; |
| 473 | |
| 474 | const double ttrust_; |
| 475 | double wheel_; |
| 476 | double throttle_; |
| 477 | bool quickturn_; |
| 478 | int stale_count_; |
| 479 | double position_time_delta_; |
| 480 | Gear left_gear_; |
| 481 | Gear right_gear_; |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 482 | DrivetrainQueue::Position last_position_; |
| 483 | DrivetrainQueue::Position position_; |
Comran Morshed | e9b1292 | 2015-11-04 19:46:48 +0000 | [diff] [blame] | 484 | int counter_; |
| 485 | }; |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 486 | |
Comran Morshed | e9b1292 | 2015-11-04 19:46:48 +0000 | [diff] [blame] | 487 | constexpr double PolyDrivetrain::kStallTorque; |
| 488 | constexpr double PolyDrivetrain::kStallCurrent; |
| 489 | constexpr double PolyDrivetrain::kFreeSpeed; |
| 490 | constexpr double PolyDrivetrain::kFreeCurrent; |
| 491 | constexpr double PolyDrivetrain::J; |
| 492 | constexpr double PolyDrivetrain::m; |
| 493 | constexpr double PolyDrivetrain::rb; |
| 494 | constexpr double PolyDrivetrain::kWheelRadius; |
| 495 | constexpr double PolyDrivetrain::kR; |
| 496 | constexpr double PolyDrivetrain::Kv; |
| 497 | constexpr double PolyDrivetrain::Kt; |
| 498 | |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 499 | void DrivetrainLoop::RunIteration(const DrivetrainQueue::Goal *goal, |
| 500 | const DrivetrainQueue::Position *position, |
| 501 | DrivetrainQueue::Output *output, |
| 502 | DrivetrainQueue::Status *status) { |
Comran Morshed | e9b1292 | 2015-11-04 19:46:48 +0000 | [diff] [blame] | 503 | // TODO(aschuh): These should be members of the class. |
| 504 | static DrivetrainMotorsSS dt_closedloop; |
| 505 | static PolyDrivetrain dt_openloop; |
| 506 | |
| 507 | bool bad_pos = false; |
| 508 | if (position == nullptr) { |
| 509 | LOG_INTERVAL(no_position_); |
| 510 | bad_pos = true; |
| 511 | } |
| 512 | no_position_.Print(); |
| 513 | |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 514 | bool control_loop_driving = false; |
| 515 | if (goal) { |
| 516 | double wheel = goal->steering; |
| 517 | double throttle = goal->throttle; |
| 518 | bool quickturn = goal->quickturn; |
| 519 | bool highgear = goal->highgear; |
Comran Morshed | e9b1292 | 2015-11-04 19:46:48 +0000 | [diff] [blame] | 520 | |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 521 | control_loop_driving = goal->control_loop_driving; |
| 522 | double left_goal = goal->left_goal; |
| 523 | double right_goal = goal->right_goal; |
Comran Morshed | e9b1292 | 2015-11-04 19:46:48 +0000 | [diff] [blame] | 524 | |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 525 | dt_closedloop.SetGoal(left_goal, goal->left_velocity_goal, right_goal, |
| 526 | goal->right_velocity_goal); |
| 527 | dt_openloop.SetGoal(wheel, throttle, quickturn, highgear); |
| 528 | } |
| 529 | |
Comran Morshed | e9b1292 | 2015-11-04 19:46:48 +0000 | [diff] [blame] | 530 | if (!bad_pos) { |
| 531 | const double left_encoder = position->left_encoder; |
| 532 | const double right_encoder = position->right_encoder; |
| 533 | if (gyro_reading.FetchLatest()) { |
| 534 | LOG_STRUCT(DEBUG, "using", *gyro_reading.get()); |
| 535 | dt_closedloop.SetPosition(left_encoder, right_encoder, |
| 536 | gyro_reading->angle); |
| 537 | } else { |
| 538 | dt_closedloop.SetRawPosition(left_encoder, right_encoder); |
| 539 | } |
| 540 | } |
| 541 | dt_openloop.SetPosition(position); |
Comran Morshed | e9b1292 | 2015-11-04 19:46:48 +0000 | [diff] [blame] | 542 | dt_openloop.Update(); |
| 543 | |
| 544 | if (control_loop_driving) { |
| 545 | dt_closedloop.Update(output == NULL, true); |
| 546 | dt_closedloop.SendMotors(output); |
| 547 | } else { |
| 548 | dt_openloop.SendMotors(output); |
| 549 | if (output) { |
| 550 | dt_closedloop.SetExternalMotors(output->left_voltage, |
| 551 | output->right_voltage); |
| 552 | } |
| 553 | dt_closedloop.Update(output == NULL, false); |
| 554 | } |
| 555 | |
| 556 | // set the output status of the control loop state |
| 557 | if (status) { |
| 558 | bool done = false; |
| 559 | if (goal) { |
| 560 | done = ((::std::abs(goal->left_goal - |
| 561 | dt_closedloop.GetEstimatedLeftEncoder()) < |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 562 | kDrivetrainDoneDistance) && |
Comran Morshed | e9b1292 | 2015-11-04 19:46:48 +0000 | [diff] [blame] | 563 | (::std::abs(goal->right_goal - |
| 564 | dt_closedloop.GetEstimatedRightEncoder()) < |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 565 | kDrivetrainDoneDistance)); |
Comran Morshed | e9b1292 | 2015-11-04 19:46:48 +0000 | [diff] [blame] | 566 | } |
| 567 | status->is_done = done; |
| 568 | status->robot_speed = dt_closedloop.GetEstimatedRobotSpeed(); |
| 569 | status->filtered_left_position = dt_closedloop.GetEstimatedLeftEncoder(); |
| 570 | status->filtered_right_position = dt_closedloop.GetEstimatedRightEncoder(); |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 571 | |
| 572 | status->filtered_left_velocity = dt_closedloop.loop().X_hat(1, 0); |
| 573 | status->filtered_right_velocity = dt_closedloop.loop().X_hat(3, 0); |
Comran Morshed | e9b1292 | 2015-11-04 19:46:48 +0000 | [diff] [blame] | 574 | status->output_was_capped = dt_closedloop.OutputWasCapped(); |
| 575 | status->uncapped_left_voltage = dt_closedloop.loop().U_uncapped(0, 0); |
| 576 | status->uncapped_right_voltage = dt_closedloop.loop().U_uncapped(1, 0); |
| 577 | } |
| 578 | } |
| 579 | |
| 580 | } // namespace control_loops |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 581 | } // namespace y2014_bot3 |