blob: db2d31e1bf4a0edc935dca1823eac1375ee2ce3b [file] [log] [blame]
James Kuszmaulf254c1a2013-03-10 16:31:26 -07001#include "frc971/control_loops/drivetrain/drivetrain.h"
brians343bc112013-02-10 01:53:46 +00002
3#include <stdio.h>
4#include <sched.h>
5#include <cmath>
Austin Schuh4352ac62013-03-19 06:23:16 +00006#include <memory>
Austin Schuh2054f5f2013-10-27 14:54:10 -07007#include "Eigen/Dense"
brians343bc112013-02-10 01:53:46 +00008
brians343bc112013-02-10 01:53:46 +00009#include "aos/common/logging/logging.h"
Briana6553ed2014-04-02 21:26:46 -070010#include "aos/common/controls/polytope.h"
Austin Schuh2054f5f2013-10-27 14:54:10 -070011#include "aos/common/commonmath.h"
Brian Silverman61e41fd2014-02-16 19:08:50 -080012#include "aos/common/logging/queue_logging.h"
Brian Silvermanfd5e2a32014-02-22 20:02:39 -080013#include "aos/common/logging/matrix_logging.h"
Brian Silverman61e41fd2014-02-16 19:08:50 -080014
Daniel Petti532c3e92014-11-04 22:15:19 -080015#include "frc971/constants.h"
James Kuszmaulf254c1a2013-03-10 16:31:26 -070016#include "frc971/control_loops/state_feedback_loop.h"
James Kuszmaulfb0e0ae2014-03-25 07:04:47 -070017#include "frc971/control_loops/coerce_goal.h"
Austin Schuh427b3702013-11-02 13:44:09 -070018#include "frc971/control_loops/drivetrain/polydrivetrain_cim_plant.h"
James Kuszmaulf254c1a2013-03-10 16:31:26 -070019#include "frc971/control_loops/drivetrain/drivetrain.q.h"
Brian Silverman07ec88e2014-12-28 00:13:08 -080020#include "frc971/queues/gyro.q.h"
Daniel Pettiaece37f2014-10-25 17:13:44 -070021#include "frc971/shifter_hall_effect.h"
brians343bc112013-02-10 01:53:46 +000022
Brian Silverman6bf0d3c2014-03-08 12:52:54 -080023using frc971::sensors::gyro_reading;
brians343bc112013-02-10 01:53:46 +000024
25namespace frc971 {
26namespace control_loops {
27
Austin Schuh4352ac62013-03-19 06:23:16 +000028class DrivetrainMotorsSS {
brians343bc112013-02-10 01:53:46 +000029 public:
Brian Silvermanad9e0002014-04-13 14:55:57 -070030 class LimitedDrivetrainLoop : public StateFeedbackLoop<4, 2, 2> {
31 public:
Brian Silverman0a151c92014-05-02 15:28:44 -070032 LimitedDrivetrainLoop(StateFeedbackLoop<4, 2, 2> &&loop)
33 : StateFeedbackLoop<4, 2, 2>(::std::move(loop)),
Brian Silvermanad9e0002014-04-13 14:55:57 -070034 U_Poly_((Eigen::Matrix<double, 4, 2>() << 1, 0,
35 -1, 0,
36 0, 1,
37 0, -1).finished(),
38 (Eigen::Matrix<double, 4, 1>() << 12.0, 12.0,
39 12.0, 12.0).finished()) {
40 ::aos::controls::HPolytope<0>::Init();
41 T << 1, -1, 1, 1;
42 T_inverse = T.inverse();
43 }
44
Brian Silvermanb94069c2014-04-17 14:34:24 -070045 bool output_was_capped() const {
46 return output_was_capped_;
47 }
48
Brian Silvermanad9e0002014-04-13 14:55:57 -070049 private:
50 virtual void CapU() {
Brian Silverman273d8a32014-05-10 22:19:09 -070051 const Eigen::Matrix<double, 4, 1> error = R() - X_hat();
Brian Silvermanad9e0002014-04-13 14:55:57 -070052
Brian Silvermanb94069c2014-04-17 14:34:24 -070053 if (::std::abs(U(0, 0)) > 12.0 || ::std::abs(U(1, 0)) > 12.0) {
54 output_was_capped_ = true;
Brian Silverman273d8a32014-05-10 22:19:09 -070055 LOG_MATRIX(DEBUG, "U at start", U());
Brian Silvermanad9e0002014-04-13 14:55:57 -070056
57 Eigen::Matrix<double, 2, 2> position_K;
58 position_K << K(0, 0), K(0, 2),
59 K(1, 0), K(1, 2);
60 Eigen::Matrix<double, 2, 2> velocity_K;
61 velocity_K << K(0, 1), K(0, 3),
62 K(1, 1), K(1, 3);
63
64 Eigen::Matrix<double, 2, 1> position_error;
65 position_error << error(0, 0), error(2, 0);
66 const auto drive_error = T_inverse * position_error;
67 Eigen::Matrix<double, 2, 1> velocity_error;
68 velocity_error << error(1, 0), error(3, 0);
69 LOG_MATRIX(DEBUG, "error", error);
70
71 const auto &poly = U_Poly_;
Brian Silverman273d8a32014-05-10 22:19:09 -070072 const Eigen::Matrix<double, 4, 2> pos_poly_H =
73 poly.H() * position_K * T;
Brian Silvermanad9e0002014-04-13 14:55:57 -070074 const Eigen::Matrix<double, 4, 1> pos_poly_k =
75 poly.k() - poly.H() * velocity_K * velocity_error;
76 const ::aos::controls::HPolytope<2> pos_poly(pos_poly_H, pos_poly_k);
77
78 Eigen::Matrix<double, 2, 1> adjusted_pos_error;
79 {
80 const auto &P = drive_error;
81
82 Eigen::Matrix<double, 1, 2> L45;
83 L45 << ::aos::sign(P(1, 0)), -::aos::sign(P(0, 0));
84 const double w45 = 0;
85
86 Eigen::Matrix<double, 1, 2> LH;
87 if (::std::abs(P(0, 0)) > ::std::abs(P(1, 0))) {
88 LH << 0, 1;
89 } else {
90 LH << 1, 0;
91 }
92 const double wh = LH.dot(P);
93
94 Eigen::Matrix<double, 2, 2> standard;
95 standard << L45, LH;
96 Eigen::Matrix<double, 2, 1> W;
97 W << w45, wh;
Brian Silverman273d8a32014-05-10 22:19:09 -070098 const Eigen::Matrix<double, 2, 1> intersection =
99 standard.inverse() * W;
Brian Silvermanad9e0002014-04-13 14:55:57 -0700100
101 bool is_inside_h;
102 const auto adjusted_pos_error_h =
103 DoCoerceGoal(pos_poly, LH, wh, drive_error, &is_inside_h);
104 const auto adjusted_pos_error_45 =
105 DoCoerceGoal(pos_poly, L45, w45, intersection, nullptr);
106 if (pos_poly.IsInside(intersection)) {
107 adjusted_pos_error = adjusted_pos_error_h;
108 } else {
109 if (is_inside_h) {
110 if (adjusted_pos_error_h.norm() > adjusted_pos_error_45.norm()) {
111 adjusted_pos_error = adjusted_pos_error_h;
112 } else {
113 adjusted_pos_error = adjusted_pos_error_45;
114 }
115 } else {
116 adjusted_pos_error = adjusted_pos_error_45;
117 }
118 }
119 }
120
121 LOG_MATRIX(DEBUG, "adjusted_pos_error", adjusted_pos_error);
Brian Silverman0ca790b2014-06-12 21:33:08 -0700122 mutable_U() =
Brian Silverman273d8a32014-05-10 22:19:09 -0700123 velocity_K * velocity_error + position_K * T * adjusted_pos_error;
124 LOG_MATRIX(DEBUG, "U is now", U());
Brian Silvermanb94069c2014-04-17 14:34:24 -0700125 } else {
126 output_was_capped_ = false;
Brian Silvermanad9e0002014-04-13 14:55:57 -0700127 }
128 }
129
130 const ::aos::controls::HPolytope<2> U_Poly_;
131 Eigen::Matrix<double, 2, 2> T, T_inverse;
Brian Silverman94738b62014-05-02 17:43:11 -0700132 bool output_was_capped_ = false;;
Brian Silvermanad9e0002014-04-13 14:55:57 -0700133 };
134
Brian Silverman2c590c32013-11-04 18:08:54 -0800135 DrivetrainMotorsSS()
Brian Silvermanad9e0002014-04-13 14:55:57 -0700136 : loop_(new LimitedDrivetrainLoop(
Brian Silverman176303a2014-04-10 10:54:55 -0700137 constants::GetValues().make_drivetrain_loop())),
138 filtered_offset_(0.0),
139 gyro_(0.0),
140 left_goal_(0.0),
141 right_goal_(0.0),
142 raw_left_(0.0),
Brian Silvermanfaac3a22014-04-30 17:54:30 -0700143 raw_right_(0.0) {
Brian Silvermanb94069c2014-04-17 14:34:24 -0700144 // Low gear on both.
145 loop_->set_controller_index(0);
brians343bc112013-02-10 01:53:46 +0000146 }
Brian Silverman176303a2014-04-10 10:54:55 -0700147
148 void SetGoal(double left, double left_velocity, double right,
149 double right_velocity) {
150 left_goal_ = left;
151 right_goal_ = right;
Brian Silverman0ca790b2014-06-12 21:33:08 -0700152 loop_->mutable_R() << left, left_velocity, right, right_velocity;
brians343bc112013-02-10 01:53:46 +0000153 }
154 void SetRawPosition(double left, double right) {
Brian Silverman176303a2014-04-10 10:54:55 -0700155 raw_right_ = right;
156 raw_left_ = left;
Austin Schuhf9286cd2014-02-11 00:51:09 -0800157 Eigen::Matrix<double, 2, 1> Y;
Brian Silverman176303a2014-04-10 10:54:55 -0700158 Y << left + filtered_offset_, right - filtered_offset_;
Austin Schuhf9286cd2014-02-11 00:51:09 -0800159 loop_->Correct(Y);
brians343bc112013-02-10 01:53:46 +0000160 }
Brian Silvermanfaac3a22014-04-30 17:54:30 -0700161 void SetPosition(double left, double right, double gyro) {
brians343bc112013-02-10 01:53:46 +0000162 // Decay the offset quickly because this gyro is great.
Brian Silvermanad9e0002014-04-13 14:55:57 -0700163 const double offset =
164 (right - left - gyro * constants::GetValues().turn_width) / 2.0;
Brian Silverman2c764f02014-04-25 09:21:21 -0500165 // TODO(brians): filtered_offset_ = offset first time around.
Brian Silverman176303a2014-04-10 10:54:55 -0700166 filtered_offset_ = 0.25 * offset + 0.75 * filtered_offset_;
167 gyro_ = gyro;
brians343bc112013-02-10 01:53:46 +0000168 SetRawPosition(left, right);
brians343bc112013-02-10 01:53:46 +0000169 }
Austin Schuh4352ac62013-03-19 06:23:16 +0000170
Ben Fredrickson890c3fe2014-03-02 00:15:16 +0000171 void SetExternalMotors(double left_voltage, double right_voltage) {
Brian Silverman0ca790b2014-06-12 21:33:08 -0700172 loop_->mutable_U() << left_voltage, right_voltage;
Ben Fredrickson890c3fe2014-03-02 00:15:16 +0000173 }
174
Brian Silvermanfaac3a22014-04-30 17:54:30 -0700175 void Update(bool stop_motors, bool enable_control_loop) {
176 if (enable_control_loop) {
Ben Fredrickson890c3fe2014-03-02 00:15:16 +0000177 loop_->Update(stop_motors);
178 } else {
179 if (stop_motors) {
Brian Silverman0ca790b2014-06-12 21:33:08 -0700180 loop_->mutable_U().setZero();
181 loop_->mutable_U_uncapped().setZero();
Ben Fredrickson890c3fe2014-03-02 00:15:16 +0000182 }
183 loop_->UpdateObserver();
184 }
Brian Silverman273d8a32014-05-10 22:19:09 -0700185 ::Eigen::Matrix<double, 4, 1> E = loop_->R() - loop_->X_hat();
Brian Silverman3146b642014-03-20 14:52:46 -0700186 LOG_MATRIX(DEBUG, "E", E);
Ben Fredrickson890c3fe2014-03-02 00:15:16 +0000187 }
188
Brian Silvermanb94069c2014-04-17 14:34:24 -0700189 double GetEstimatedRobotSpeed() const {
Ben Fredrickson890c3fe2014-03-02 00:15:16 +0000190 // lets just call the average of left and right velocities close enough
191 return (loop_->X_hat(1, 0) + loop_->X_hat(3, 0)) / 2;
192 }
193
Brian Silvermanb94069c2014-04-17 14:34:24 -0700194 double GetEstimatedLeftEncoder() const {
Ben Fredrickson890c3fe2014-03-02 00:15:16 +0000195 return loop_->X_hat(0, 0);
196 }
197
Brian Silvermanb94069c2014-04-17 14:34:24 -0700198 double GetEstimatedRightEncoder() const {
Ben Fredrickson890c3fe2014-03-02 00:15:16 +0000199 return loop_->X_hat(2, 0);
brians343bc112013-02-10 01:53:46 +0000200 }
201
Brian Silvermanb94069c2014-04-17 14:34:24 -0700202 bool OutputWasCapped() const {
203 return loop_->output_was_capped();
204 }
205
206 void SendMotors(Drivetrain::Output *output) const {
Austin Schuh4352ac62013-03-19 06:23:16 +0000207 if (output) {
208 output->left_voltage = loop_->U(0, 0);
209 output->right_voltage = loop_->U(1, 0);
Brian Silvermane3a277a2014-04-13 14:56:57 -0700210 output->left_high = false;
211 output->right_high = false;
brians8ad74052013-03-16 21:04:51 +0000212 }
brians343bc112013-02-10 01:53:46 +0000213 }
brians343bc112013-02-10 01:53:46 +0000214
Brian Silvermanb94069c2014-04-17 14:34:24 -0700215 const LimitedDrivetrainLoop &loop() const { return *loop_; }
216
brians343bc112013-02-10 01:53:46 +0000217 private:
Brian Silvermanad9e0002014-04-13 14:55:57 -0700218 ::std::unique_ptr<LimitedDrivetrainLoop> loop_;
Austin Schuh4352ac62013-03-19 06:23:16 +0000219
Brian Silverman176303a2014-04-10 10:54:55 -0700220 double filtered_offset_;
221 double gyro_;
222 double left_goal_;
223 double right_goal_;
224 double raw_left_;
225 double raw_right_;
brians343bc112013-02-10 01:53:46 +0000226};
227
Austin Schuh2054f5f2013-10-27 14:54:10 -0700228class PolyDrivetrain {
229 public:
Austin Schuh427b3702013-11-02 13:44:09 -0700230
231 enum Gear {
232 HIGH,
233 LOW,
234 SHIFTING_UP,
235 SHIFTING_DOWN
236 };
237 // Stall Torque in N m
238 static constexpr double kStallTorque = 2.42;
239 // Stall Current in Amps
240 static constexpr double kStallCurrent = 133;
241 // Free Speed in RPM. Used number from last year.
242 static constexpr double kFreeSpeed = 4650.0;
243 // Free Current in Amps
244 static constexpr double kFreeCurrent = 2.7;
245 // Moment of inertia of the drivetrain in kg m^2
246 // Just borrowed from last year.
247 static constexpr double J = 6.4;
248 // Mass of the robot, in kg.
249 static constexpr double m = 68;
250 // Radius of the robot, in meters (from last year).
251 static constexpr double rb = 0.617998644 / 2.0;
Brian Silverman1a6590d2013-11-04 14:46:46 -0800252 static constexpr double kWheelRadius = 0.04445;
Austin Schuh427b3702013-11-02 13:44:09 -0700253 // Resistance of the motor, divided by the number of motors.
254 static constexpr double kR = (12.0 / kStallCurrent / 4 + 0.03) / (0.93 * 0.93);
255 // Motor velocity constant
256 static constexpr double Kv =
257 ((kFreeSpeed / 60.0 * 2.0 * M_PI) / (12.0 - kR * kFreeCurrent));
258 // Torque constant
259 static constexpr double Kt = kStallTorque / kStallCurrent;
Austin Schuh427b3702013-11-02 13:44:09 -0700260
Austin Schuh2054f5f2013-10-27 14:54:10 -0700261 PolyDrivetrain()
262 : U_Poly_((Eigen::Matrix<double, 4, 2>() << /*[[*/ 1, 0 /*]*/,
263 /*[*/ -1, 0 /*]*/,
264 /*[*/ 0, 1 /*]*/,
265 /*[*/ 0, -1 /*]]*/).finished(),
266 (Eigen::Matrix<double, 4, 1>() << /*[[*/ 12 /*]*/,
267 /*[*/ 12 /*]*/,
268 /*[*/ 12 /*]*/,
269 /*[*/ 12 /*]]*/).finished()),
Brian Silverman2c590c32013-11-04 18:08:54 -0800270 loop_(new StateFeedbackLoop<2, 2, 2>(
271 constants::GetValues().make_v_drivetrain_loop())),
Austin Schuh427b3702013-11-02 13:44:09 -0700272 ttrust_(1.1),
273 wheel_(0.0),
274 throttle_(0.0),
275 quickturn_(false),
276 stale_count_(0),
277 position_time_delta_(0.01),
278 left_gear_(LOW),
Brian Silvermande8fd552013-11-03 15:53:42 -0800279 right_gear_(LOW),
280 counter_(0) {
Austin Schuh2054f5f2013-10-27 14:54:10 -0700281
Austin Schuh427b3702013-11-02 13:44:09 -0700282 last_position_.Zero();
283 position_.Zero();
Austin Schuh2054f5f2013-10-27 14:54:10 -0700284 }
Austin Schuh427b3702013-11-02 13:44:09 -0700285 static bool IsInGear(Gear gear) { return gear == LOW || gear == HIGH; }
286
Austin Schuh78d55462014-02-23 01:39:30 -0800287 static double MotorSpeed(const constants::ShifterHallEffect &hall_effect,
288 double shifter_position, double velocity) {
Austin Schuh427b3702013-11-02 13:44:09 -0700289 // TODO(austin): G_high, G_low and kWheelRadius
Austin Schuh78d55462014-02-23 01:39:30 -0800290 const double avg_hall_effect =
291 (hall_effect.clear_high + hall_effect.clear_low) / 2.0;
292
293 if (shifter_position > avg_hall_effect) {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800294 return velocity / constants::GetValues().high_gear_ratio / kWheelRadius;
Austin Schuh427b3702013-11-02 13:44:09 -0700295 } else {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800296 return velocity / constants::GetValues().low_gear_ratio / kWheelRadius;
297 }
298 }
299
Austin Schuh78d55462014-02-23 01:39:30 -0800300 Gear ComputeGear(const constants::ShifterHallEffect &hall_effect,
301 double velocity, Gear current) {
302 const double low_omega = MotorSpeed(hall_effect, 0.0, ::std::abs(velocity));
303 const double high_omega =
304 MotorSpeed(hall_effect, 1.0, ::std::abs(velocity));
Brian Silverman1a6590d2013-11-04 14:46:46 -0800305
Brian Silverman1a6590d2013-11-04 14:46:46 -0800306 double high_torque = ((12.0 - high_omega / Kv) * Kt / kR);
307 double low_torque = ((12.0 - low_omega / Kv) * Kt / kR);
308 double high_power = high_torque * high_omega;
309 double low_power = low_torque * low_omega;
Brian Silverman55a930b2013-11-04 20:59:00 -0800310
311 // TODO(aschuh): Do this right!
312 if ((current == HIGH || high_power > low_power + 160) &&
313 ::std::abs(velocity) > 0.14) {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800314 return HIGH;
315 } else {
316 return LOW;
Austin Schuh427b3702013-11-02 13:44:09 -0700317 }
318 }
319
Austin Schuh2054f5f2013-10-27 14:54:10 -0700320 void SetGoal(double wheel, double throttle, bool quickturn, bool highgear) {
Brian Silvermande8fd552013-11-03 15:53:42 -0800321 const double kWheelNonLinearity = 0.3;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700322 // Apply a sin function that's scaled to make it feel better.
323 const double angular_range = M_PI_2 * kWheelNonLinearity;
324 wheel_ = sin(angular_range * wheel) / sin(angular_range);
325 wheel_ = sin(angular_range * wheel_) / sin(angular_range);
Austin Schuh2054f5f2013-10-27 14:54:10 -0700326 quickturn_ = quickturn;
Austin Schuh427b3702013-11-02 13:44:09 -0700327
Brian Silverman1a6590d2013-11-04 14:46:46 -0800328 static const double kThrottleDeadband = 0.05;
329 if (::std::abs(throttle) < kThrottleDeadband) {
330 throttle_ = 0;
331 } else {
332 throttle_ = copysign((::std::abs(throttle) - kThrottleDeadband) /
333 (1.0 - kThrottleDeadband), throttle);
334 }
335
Austin Schuh427b3702013-11-02 13:44:09 -0700336 // TODO(austin): Fix the upshift logic to include states.
Brian Silverman1a6590d2013-11-04 14:46:46 -0800337 Gear requested_gear;
Brian Silverman55a930b2013-11-04 20:59:00 -0800338 if (false) {
Austin Schuh78d55462014-02-23 01:39:30 -0800339 const auto &values = constants::GetValues();
Brian Silverman1a6590d2013-11-04 14:46:46 -0800340 const double current_left_velocity =
341 (position_.left_encoder - last_position_.left_encoder) /
342 position_time_delta_;
343 const double current_right_velocity =
344 (position_.right_encoder - last_position_.right_encoder) /
345 position_time_delta_;
346
Austin Schuh78d55462014-02-23 01:39:30 -0800347 Gear left_requested =
348 ComputeGear(values.left_drive, current_left_velocity, left_gear_);
349 Gear right_requested =
350 ComputeGear(values.right_drive, current_right_velocity, right_gear_);
Brian Silverman1a6590d2013-11-04 14:46:46 -0800351 requested_gear =
352 (left_requested == HIGH || right_requested == HIGH) ? HIGH : LOW;
353 } else {
354 requested_gear = highgear ? HIGH : LOW;
355 }
356
357 const Gear shift_up =
358 constants::GetValues().clutch_transmission ? HIGH : SHIFTING_UP;
359 const Gear shift_down =
360 constants::GetValues().clutch_transmission ? LOW : SHIFTING_DOWN;
Austin Schuh427b3702013-11-02 13:44:09 -0700361
362 if (left_gear_ != requested_gear) {
363 if (IsInGear(left_gear_)) {
364 if (requested_gear == HIGH) {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800365 left_gear_ = shift_up;
Austin Schuh427b3702013-11-02 13:44:09 -0700366 } else {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800367 left_gear_ = shift_down;
Austin Schuh427b3702013-11-02 13:44:09 -0700368 }
Brian Silverman55a930b2013-11-04 20:59:00 -0800369 } else {
370 if (requested_gear == HIGH && left_gear_ == SHIFTING_DOWN) {
371 left_gear_ = SHIFTING_UP;
372 } else if (requested_gear == LOW && left_gear_ == SHIFTING_UP) {
373 left_gear_ = SHIFTING_DOWN;
374 }
Austin Schuh427b3702013-11-02 13:44:09 -0700375 }
376 }
377 if (right_gear_ != requested_gear) {
378 if (IsInGear(right_gear_)) {
379 if (requested_gear == HIGH) {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800380 right_gear_ = shift_up;
Austin Schuh427b3702013-11-02 13:44:09 -0700381 } else {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800382 right_gear_ = shift_down;
Austin Schuh427b3702013-11-02 13:44:09 -0700383 }
Brian Silverman55a930b2013-11-04 20:59:00 -0800384 } else {
385 if (requested_gear == HIGH && right_gear_ == SHIFTING_DOWN) {
386 right_gear_ = SHIFTING_UP;
387 } else if (requested_gear == LOW && right_gear_ == SHIFTING_UP) {
388 right_gear_ = SHIFTING_DOWN;
389 }
Austin Schuh427b3702013-11-02 13:44:09 -0700390 }
Austin Schuh2054f5f2013-10-27 14:54:10 -0700391 }
392 }
Austin Schuh427b3702013-11-02 13:44:09 -0700393 void SetPosition(const Drivetrain::Position *position) {
Austin Schuh78d55462014-02-23 01:39:30 -0800394 const auto &values = constants::GetValues();
Austin Schuh427b3702013-11-02 13:44:09 -0700395 if (position == NULL) {
396 ++stale_count_;
397 } else {
398 last_position_ = position_;
399 position_ = *position;
400 position_time_delta_ = (stale_count_ + 1) * 0.01;
401 stale_count_ = 0;
402 }
403
404 if (position) {
Brian Silverman61e41fd2014-02-16 19:08:50 -0800405 GearLogging gear_logging;
Austin Schuh427b3702013-11-02 13:44:09 -0700406 // Switch to the correct controller.
Austin Schuh78d55462014-02-23 01:39:30 -0800407 const double left_middle_shifter_position =
408 (values.left_drive.clear_high + values.left_drive.clear_low) / 2.0;
409 const double right_middle_shifter_position =
410 (values.right_drive.clear_high + values.right_drive.clear_low) / 2.0;
411
Daniel Pettifd0a51d2014-11-07 16:59:24 -0800412 if (position->left_shifter_position < left_middle_shifter_position ||
413 left_gear_ == LOW) {
Austin Schuh78d55462014-02-23 01:39:30 -0800414 if (position->right_shifter_position < right_middle_shifter_position ||
415 right_gear_ == LOW) {
Brian Silverman61e41fd2014-02-16 19:08:50 -0800416 gear_logging.left_loop_high = false;
417 gear_logging.right_loop_high = false;
418 loop_->set_controller_index(gear_logging.controller_index = 0);
Austin Schuh427b3702013-11-02 13:44:09 -0700419 } else {
Brian Silverman61e41fd2014-02-16 19:08:50 -0800420 gear_logging.left_loop_high = false;
421 gear_logging.right_loop_high = true;
422 loop_->set_controller_index(gear_logging.controller_index = 1);
Austin Schuh427b3702013-11-02 13:44:09 -0700423 }
424 } else {
Austin Schuh78d55462014-02-23 01:39:30 -0800425 if (position->right_shifter_position < right_middle_shifter_position ||
Daniel Pettifd0a51d2014-11-07 16:59:24 -0800426 right_gear_ == LOW) {
Brian Silverman61e41fd2014-02-16 19:08:50 -0800427 gear_logging.left_loop_high = true;
428 gear_logging.right_loop_high = false;
429 loop_->set_controller_index(gear_logging.controller_index = 2);
Austin Schuh427b3702013-11-02 13:44:09 -0700430 } else {
Brian Silverman61e41fd2014-02-16 19:08:50 -0800431 gear_logging.left_loop_high = true;
432 gear_logging.right_loop_high = true;
433 loop_->set_controller_index(gear_logging.controller_index = 3);
Austin Schuh427b3702013-11-02 13:44:09 -0700434 }
435 }
Brian Silverman61e41fd2014-02-16 19:08:50 -0800436
Austin Schuh427b3702013-11-02 13:44:09 -0700437 // TODO(austin): Constants.
Austin Schuh78d55462014-02-23 01:39:30 -0800438 if (position->left_shifter_position > values.left_drive.clear_high && left_gear_ == SHIFTING_UP) {
Austin Schuh427b3702013-11-02 13:44:09 -0700439 left_gear_ = HIGH;
440 }
Austin Schuh78d55462014-02-23 01:39:30 -0800441 if (position->left_shifter_position < values.left_drive.clear_low && left_gear_ == SHIFTING_DOWN) {
Austin Schuh427b3702013-11-02 13:44:09 -0700442 left_gear_ = LOW;
443 }
Austin Schuh78d55462014-02-23 01:39:30 -0800444 if (position->right_shifter_position > values.right_drive.clear_high && right_gear_ == SHIFTING_UP) {
Austin Schuh427b3702013-11-02 13:44:09 -0700445 right_gear_ = HIGH;
446 }
Austin Schuh78d55462014-02-23 01:39:30 -0800447 if (position->right_shifter_position < values.right_drive.clear_low && right_gear_ == SHIFTING_DOWN) {
Austin Schuh427b3702013-11-02 13:44:09 -0700448 right_gear_ = LOW;
449 }
Brian Silverman61e41fd2014-02-16 19:08:50 -0800450
451 gear_logging.left_state = left_gear_;
452 gear_logging.right_state = right_gear_;
453 LOG_STRUCT(DEBUG, "state", gear_logging);
Austin Schuh427b3702013-11-02 13:44:09 -0700454 }
455 }
456
Austin Schuh2054f5f2013-10-27 14:54:10 -0700457 double FilterVelocity(double throttle) {
458 const Eigen::Matrix<double, 2, 2> FF =
459 loop_->B().inverse() *
460 (Eigen::Matrix<double, 2, 2>::Identity() - loop_->A());
461
462 constexpr int kHighGearController = 3;
463 const Eigen::Matrix<double, 2, 2> FF_high =
Brian Silverman273d8a32014-05-10 22:19:09 -0700464 loop_->controller(kHighGearController).plant.B().inverse() *
Austin Schuh2054f5f2013-10-27 14:54:10 -0700465 (Eigen::Matrix<double, 2, 2>::Identity() -
Brian Silverman273d8a32014-05-10 22:19:09 -0700466 loop_->controller(kHighGearController).plant.A());
Austin Schuh2054f5f2013-10-27 14:54:10 -0700467
468 ::Eigen::Matrix<double, 1, 2> FF_sum = FF.colwise().sum();
469 int min_FF_sum_index;
470 const double min_FF_sum = FF_sum.minCoeff(&min_FF_sum_index);
471 const double min_K_sum = loop_->K().col(min_FF_sum_index).sum();
472 const double high_min_FF_sum = FF_high.col(0).sum();
473
474 const double adjusted_ff_voltage = ::aos::Clip(
475 throttle * 12.0 * min_FF_sum / high_min_FF_sum, -12.0, 12.0);
476 return ((adjusted_ff_voltage +
Brian Silvermana21c3a22014-06-12 21:49:15 -0700477 ttrust_ * min_K_sum * (loop_->X_hat(0, 0) + loop_->X_hat(1, 0)) / 2.0) /
Austin Schuh2054f5f2013-10-27 14:54:10 -0700478 (ttrust_ * min_K_sum + min_FF_sum));
479 }
480
Brian Silverman718b1d72013-10-28 16:22:45 -0700481 double MaxVelocity() {
482 const Eigen::Matrix<double, 2, 2> FF =
483 loop_->B().inverse() *
484 (Eigen::Matrix<double, 2, 2>::Identity() - loop_->A());
485
486 constexpr int kHighGearController = 3;
487 const Eigen::Matrix<double, 2, 2> FF_high =
Brian Silverman273d8a32014-05-10 22:19:09 -0700488 loop_->controller(kHighGearController).plant.B().inverse() *
Brian Silverman718b1d72013-10-28 16:22:45 -0700489 (Eigen::Matrix<double, 2, 2>::Identity() -
Brian Silverman273d8a32014-05-10 22:19:09 -0700490 loop_->controller(kHighGearController).plant.A());
Brian Silverman718b1d72013-10-28 16:22:45 -0700491
492 ::Eigen::Matrix<double, 1, 2> FF_sum = FF.colwise().sum();
493 int min_FF_sum_index;
494 const double min_FF_sum = FF_sum.minCoeff(&min_FF_sum_index);
495 //const double min_K_sum = loop_->K().col(min_FF_sum_index).sum();
496 const double high_min_FF_sum = FF_high.col(0).sum();
497
498 const double adjusted_ff_voltage = ::aos::Clip(
499 12.0 * min_FF_sum / high_min_FF_sum, -12.0, 12.0);
500 return adjusted_ff_voltage / min_FF_sum;
501 }
502
Austin Schuh2054f5f2013-10-27 14:54:10 -0700503 void Update() {
Austin Schuh78d55462014-02-23 01:39:30 -0800504 const auto &values = constants::GetValues();
Austin Schuh427b3702013-11-02 13:44:09 -0700505 // TODO(austin): Observer for the current velocity instead of difference
506 // calculations.
Brian Silvermande8fd552013-11-03 15:53:42 -0800507 ++counter_;
Austin Schuh427b3702013-11-02 13:44:09 -0700508 const double current_left_velocity =
Brian Silvermande8fd552013-11-03 15:53:42 -0800509 (position_.left_encoder - last_position_.left_encoder) /
Austin Schuh427b3702013-11-02 13:44:09 -0700510 position_time_delta_;
511 const double current_right_velocity =
Brian Silvermande8fd552013-11-03 15:53:42 -0800512 (position_.right_encoder - last_position_.right_encoder) /
Austin Schuh427b3702013-11-02 13:44:09 -0700513 position_time_delta_;
514 const double left_motor_speed =
Austin Schuh78d55462014-02-23 01:39:30 -0800515 MotorSpeed(values.left_drive, position_.left_shifter_position,
516 current_left_velocity);
Austin Schuh427b3702013-11-02 13:44:09 -0700517 const double right_motor_speed =
Austin Schuh78d55462014-02-23 01:39:30 -0800518 MotorSpeed(values.right_drive, position_.right_shifter_position,
519 current_right_velocity);
Austin Schuh2054f5f2013-10-27 14:54:10 -0700520
Brian Silverman61e41fd2014-02-16 19:08:50 -0800521 {
522 CIMLogging logging;
523
524 // Reset the CIM model to the current conditions to be ready for when we
525 // shift.
526 if (IsInGear(left_gear_)) {
527 logging.left_in_gear = true;
Brian Silverman61e41fd2014-02-16 19:08:50 -0800528 } else {
529 logging.left_in_gear = false;
530 }
531 logging.left_motor_speed = left_motor_speed;
532 logging.left_velocity = current_left_velocity;
533 if (IsInGear(right_gear_)) {
534 logging.right_in_gear = true;
Brian Silverman61e41fd2014-02-16 19:08:50 -0800535 } else {
536 logging.right_in_gear = false;
537 }
538 logging.right_motor_speed = right_motor_speed;
539 logging.right_velocity = current_right_velocity;
540
541 LOG_STRUCT(DEBUG, "currently", logging);
Austin Schuh427b3702013-11-02 13:44:09 -0700542 }
Austin Schuh2054f5f2013-10-27 14:54:10 -0700543
Austin Schuh427b3702013-11-02 13:44:09 -0700544 if (IsInGear(left_gear_) && IsInGear(right_gear_)) {
545 // FF * X = U (steady state)
546 const Eigen::Matrix<double, 2, 2> FF =
547 loop_->B().inverse() *
548 (Eigen::Matrix<double, 2, 2>::Identity() - loop_->A());
549
550 // Invert the plant to figure out how the velocity filter would have to
551 // work
552 // out in order to filter out the forwards negative inertia.
553 // This math assumes that the left and right power and velocity are
554 // equals,
555 // and that the plant is the same on the left and right.
556 const double fvel = FilterVelocity(throttle_);
557
558 const double sign_svel = wheel_ * ((fvel > 0.0) ? 1.0 : -1.0);
559 double steering_velocity;
560 if (quickturn_) {
561 steering_velocity = wheel_ * MaxVelocity();
562 } else {
563 steering_velocity = ::std::abs(fvel) * wheel_;
564 }
565 const double left_velocity = fvel - steering_velocity;
566 const double right_velocity = fvel + steering_velocity;
567
568 // Integrate velocity to get the position.
569 // This position is used to get integral control.
Brian Silverman0ca790b2014-06-12 21:33:08 -0700570 loop_->mutable_R() << left_velocity, right_velocity;
Austin Schuh427b3702013-11-02 13:44:09 -0700571
572 if (!quickturn_) {
573 // K * R = w
574 Eigen::Matrix<double, 1, 2> equality_k;
575 equality_k << 1 + sign_svel, -(1 - sign_svel);
576 const double equality_w = 0.0;
577
578 // Construct a constraint on R by manipulating the constraint on U
579 ::aos::controls::HPolytope<2> R_poly = ::aos::controls::HPolytope<2>(
580 U_Poly_.H() * (loop_->K() + FF),
Brian Silverman273d8a32014-05-10 22:19:09 -0700581 U_Poly_.k() + U_Poly_.H() * loop_->K() * loop_->X_hat());
Austin Schuh427b3702013-11-02 13:44:09 -0700582
583 // Limit R back inside the box.
Brian Silverman0ca790b2014-06-12 21:33:08 -0700584 loop_->mutable_R() =
Brian Silverman273d8a32014-05-10 22:19:09 -0700585 CoerceGoal(R_poly, equality_k, equality_w, loop_->R());
Austin Schuh427b3702013-11-02 13:44:09 -0700586 }
587
Brian Silverman273d8a32014-05-10 22:19:09 -0700588 const Eigen::Matrix<double, 2, 1> FF_volts = FF * loop_->R();
Austin Schuh427b3702013-11-02 13:44:09 -0700589 const Eigen::Matrix<double, 2, 1> U_ideal =
Brian Silverman273d8a32014-05-10 22:19:09 -0700590 loop_->K() * (loop_->R() - loop_->X_hat()) + FF_volts;
Austin Schuh427b3702013-11-02 13:44:09 -0700591
592 for (int i = 0; i < 2; i++) {
Brian Silverman0ca790b2014-06-12 21:33:08 -0700593 loop_->mutable_U()[i] = ::aos::Clip(U_ideal[i], -12, 12);
Austin Schuh427b3702013-11-02 13:44:09 -0700594 }
Brian Silvermande8fd552013-11-03 15:53:42 -0800595
596 // TODO(austin): Model this better.
597 // TODO(austin): Feed back?
Brian Silverman0ca790b2014-06-12 21:33:08 -0700598 loop_->mutable_X_hat() =
Brian Silverman273d8a32014-05-10 22:19:09 -0700599 loop_->A() * loop_->X_hat() + loop_->B() * loop_->U();
Brian Silverman718b1d72013-10-28 16:22:45 -0700600 } else {
Austin Schuh427b3702013-11-02 13:44:09 -0700601 // Any motor is not in gear. Speed match.
602 ::Eigen::Matrix<double, 1, 1> R_left;
Austin Schuh427b3702013-11-02 13:44:09 -0700603 ::Eigen::Matrix<double, 1, 1> R_right;
Austin Schuhb5afb692014-03-02 11:54:11 -0800604 R_left(0, 0) = left_motor_speed;
Austin Schuh427b3702013-11-02 13:44:09 -0700605 R_right(0, 0) = right_motor_speed;
Austin Schuhb5afb692014-03-02 11:54:11 -0800606
607 const double wiggle =
Brian Silvermanf970f2c2014-03-22 19:34:30 -0700608 (static_cast<double>((counter_ % 20) / 10) - 0.5) * 5.0;
Austin Schuhb5afb692014-03-02 11:54:11 -0800609
Brian Silvermana21c3a22014-06-12 21:49:15 -0700610 loop_->mutable_U(0, 0) = ::aos::Clip(
Brian Silverman56658322014-03-22 16:57:22 -0700611 (R_left / Kv)(0, 0) + (IsInGear(left_gear_) ? 0 : wiggle),
612 -12.0, 12.0);
Brian Silvermana21c3a22014-06-12 21:49:15 -0700613 loop_->mutable_U(1, 0) = ::aos::Clip(
Brian Silverman56658322014-03-22 16:57:22 -0700614 (R_right / Kv)(0, 0) + (IsInGear(right_gear_) ? 0 : wiggle),
615 -12.0, 12.0);
Brian Silverman0ca790b2014-06-12 21:33:08 -0700616 loop_->mutable_U() *= 12.0 / position_.battery_voltage;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700617 }
Austin Schuh2054f5f2013-10-27 14:54:10 -0700618 }
619
620 void SendMotors(Drivetrain::Output *output) {
Brian Silvermande8fd552013-11-03 15:53:42 -0800621 if (output != NULL) {
622 output->left_voltage = loop_->U(0, 0);
623 output->right_voltage = loop_->U(1, 0);
Brian Silverman61e41fd2014-02-16 19:08:50 -0800624 output->left_high = left_gear_ == HIGH || left_gear_ == SHIFTING_UP;
625 output->right_high = right_gear_ == HIGH || right_gear_ == SHIFTING_UP;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700626 }
627 }
628
629 private:
630 const ::aos::controls::HPolytope<2> U_Poly_;
631
632 ::std::unique_ptr<StateFeedbackLoop<2, 2, 2>> loop_;
633
Austin Schuh427b3702013-11-02 13:44:09 -0700634 const double ttrust_;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700635 double wheel_;
636 double throttle_;
637 bool quickturn_;
Austin Schuh427b3702013-11-02 13:44:09 -0700638 int stale_count_;
639 double position_time_delta_;
640 Gear left_gear_;
641 Gear right_gear_;
642 Drivetrain::Position last_position_;
643 Drivetrain::Position position_;
Brian Silvermande8fd552013-11-03 15:53:42 -0800644 int counter_;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700645};
Brian Silvermancec3c8d2013-12-20 21:04:55 -0800646constexpr double PolyDrivetrain::kStallTorque;
647constexpr double PolyDrivetrain::kStallCurrent;
648constexpr double PolyDrivetrain::kFreeSpeed;
649constexpr double PolyDrivetrain::kFreeCurrent;
650constexpr double PolyDrivetrain::J;
651constexpr double PolyDrivetrain::m;
652constexpr double PolyDrivetrain::rb;
653constexpr double PolyDrivetrain::kWheelRadius;
654constexpr double PolyDrivetrain::kR;
655constexpr double PolyDrivetrain::Kv;
656constexpr double PolyDrivetrain::Kt;
657
Austin Schuh2054f5f2013-10-27 14:54:10 -0700658
brians343bc112013-02-10 01:53:46 +0000659void DrivetrainLoop::RunIteration(const Drivetrain::Goal *goal,
660 const Drivetrain::Position *position,
661 Drivetrain::Output *output,
Ben Fredrickson890c3fe2014-03-02 00:15:16 +0000662 Drivetrain::Status * status) {
brians343bc112013-02-10 01:53:46 +0000663 // TODO(aschuh): These should be members of the class.
664 static DrivetrainMotorsSS dt_closedloop;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700665 static PolyDrivetrain dt_openloop;
brians343bc112013-02-10 01:53:46 +0000666
667 bool bad_pos = false;
Austin Schuh427b3702013-11-02 13:44:09 -0700668 if (position == nullptr) {
Brian Silverman50a9d032014-02-16 17:20:57 -0800669 LOG_INTERVAL(no_position_);
brians343bc112013-02-10 01:53:46 +0000670 bad_pos = true;
671 }
Brian Silverman9c9ab422014-02-25 13:42:53 -0800672 no_position_.Print();
brians343bc112013-02-10 01:53:46 +0000673
674 double wheel = goal->steering;
675 double throttle = goal->throttle;
676 bool quickturn = goal->quickturn;
677 bool highgear = goal->highgear;
678
679 bool control_loop_driving = goal->control_loop_driving;
680 double left_goal = goal->left_goal;
681 double right_goal = goal->right_goal;
682
Austin Schuh2054f5f2013-10-27 14:54:10 -0700683 dt_closedloop.SetGoal(left_goal, goal->left_velocity_goal, right_goal,
684 goal->right_velocity_goal);
brians343bc112013-02-10 01:53:46 +0000685 if (!bad_pos) {
686 const double left_encoder = position->left_encoder;
687 const double right_encoder = position->right_encoder;
Brian Silverman6bf0d3c2014-03-08 12:52:54 -0800688 if (gyro_reading.FetchLatest()) {
Brian Silverman74e5b4e2014-03-09 16:58:58 -0700689 LOG_STRUCT(DEBUG, "using", *gyro_reading.get());
690 dt_closedloop.SetPosition(left_encoder, right_encoder,
Brian Silvermanfaac3a22014-04-30 17:54:30 -0700691 gyro_reading->angle);
brians343bc112013-02-10 01:53:46 +0000692 } else {
693 dt_closedloop.SetRawPosition(left_encoder, right_encoder);
694 }
695 }
Austin Schuh427b3702013-11-02 13:44:09 -0700696 dt_openloop.SetPosition(position);
brians343bc112013-02-10 01:53:46 +0000697 dt_openloop.SetGoal(wheel, throttle, quickturn, highgear);
698 dt_openloop.Update();
Ben Fredrickson890c3fe2014-03-02 00:15:16 +0000699
Brian Silverman2845c4c2013-03-16 19:54:08 -0700700 if (control_loop_driving) {
Brian Silvermanfaac3a22014-04-30 17:54:30 -0700701 dt_closedloop.Update(output == NULL, true);
Brian Silverman2845c4c2013-03-16 19:54:08 -0700702 dt_closedloop.SendMotors(output);
703 } else {
704 dt_openloop.SendMotors(output);
Austin Schuhb5afb692014-03-02 11:54:11 -0800705 if (output) {
706 dt_closedloop.SetExternalMotors(output->left_voltage,
707 output->right_voltage);
708 }
Brian Silvermanfaac3a22014-04-30 17:54:30 -0700709 dt_closedloop.Update(output == NULL, false);
brians343bc112013-02-10 01:53:46 +0000710 }
Ben Fredrickson890c3fe2014-03-02 00:15:16 +0000711
Brian Silverman2c764f02014-04-25 09:21:21 -0500712 // set the output status of the control loop state
Ben Fredrickson890c3fe2014-03-02 00:15:16 +0000713 if (status) {
714 bool done = false;
715 if (goal) {
716 done = ((::std::abs(goal->left_goal -
717 dt_closedloop.GetEstimatedLeftEncoder()) <
718 constants::GetValues().drivetrain_done_distance) &&
719 (::std::abs(goal->right_goal -
720 dt_closedloop.GetEstimatedRightEncoder()) <
721 constants::GetValues().drivetrain_done_distance));
722 }
723 status->is_done = done;
724 status->robot_speed = dt_closedloop.GetEstimatedRobotSpeed();
Austin Schuh577edf62014-04-13 10:33:05 -0700725 status->filtered_left_position = dt_closedloop.GetEstimatedLeftEncoder();
726 status->filtered_right_position = dt_closedloop.GetEstimatedRightEncoder();
Brian Silvermanb94069c2014-04-17 14:34:24 -0700727 status->output_was_capped = dt_closedloop.OutputWasCapped();
728 status->uncapped_left_voltage = dt_closedloop.loop().U_uncapped(0, 0);
729 status->uncapped_right_voltage = dt_closedloop.loop().U_uncapped(1, 0);
brians343bc112013-02-10 01:53:46 +0000730 }
731}
732
733} // namespace control_loops
734} // namespace frc971