blob: c88169a7c246e42046b2d122431df17e819da2ff [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 Silverman93335ae2015-01-26 20:43:39 -050023// 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 Silverman6bf0d3c2014-03-08 12:52:54 -080027using frc971::sensors::gyro_reading;
brians343bc112013-02-10 01:53:46 +000028
29namespace frc971 {
30namespace control_loops {
31
Austin Schuh4352ac62013-03-19 06:23:16 +000032class DrivetrainMotorsSS {
brians343bc112013-02-10 01:53:46 +000033 public:
Brian Silvermanad9e0002014-04-13 14:55:57 -070034 class LimitedDrivetrainLoop : public StateFeedbackLoop<4, 2, 2> {
35 public:
Brian Silverman0a151c92014-05-02 15:28:44 -070036 LimitedDrivetrainLoop(StateFeedbackLoop<4, 2, 2> &&loop)
37 : StateFeedbackLoop<4, 2, 2>(::std::move(loop)),
Brian Silvermanad9e0002014-04-13 14:55:57 -070038 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 Silvermanb94069c2014-04-17 14:34:24 -070049 bool output_was_capped() const {
50 return output_was_capped_;
51 }
52
Brian Silvermanad9e0002014-04-13 14:55:57 -070053 private:
54 virtual void CapU() {
Brian Silverman273d8a32014-05-10 22:19:09 -070055 const Eigen::Matrix<double, 4, 1> error = R() - X_hat();
Brian Silvermanad9e0002014-04-13 14:55:57 -070056
Brian Silvermanb94069c2014-04-17 14:34:24 -070057 if (::std::abs(U(0, 0)) > 12.0 || ::std::abs(U(1, 0)) > 12.0) {
58 output_was_capped_ = true;
Brian Silverman273d8a32014-05-10 22:19:09 -070059 LOG_MATRIX(DEBUG, "U at start", U());
Brian Silvermanad9e0002014-04-13 14:55:57 -070060
61 Eigen::Matrix<double, 2, 2> position_K;
62 position_K << K(0, 0), K(0, 2),
63 K(1, 0), K(1, 2);
64 Eigen::Matrix<double, 2, 2> velocity_K;
65 velocity_K << K(0, 1), K(0, 3),
66 K(1, 1), K(1, 3);
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_;
Brian Silverman273d8a32014-05-10 22:19:09 -070076 const Eigen::Matrix<double, 4, 2> pos_poly_H =
77 poly.H() * position_K * T;
Brian Silvermanad9e0002014-04-13 14:55:57 -070078 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;
Brian Silverman273d8a32014-05-10 22:19:09 -0700102 const Eigen::Matrix<double, 2, 1> intersection =
103 standard.inverse() * W;
Brian Silvermanad9e0002014-04-13 14:55:57 -0700104
105 bool is_inside_h;
106 const auto adjusted_pos_error_h =
107 DoCoerceGoal(pos_poly, LH, wh, drive_error, &is_inside_h);
108 const auto adjusted_pos_error_45 =
109 DoCoerceGoal(pos_poly, L45, w45, intersection, nullptr);
110 if (pos_poly.IsInside(intersection)) {
111 adjusted_pos_error = adjusted_pos_error_h;
112 } else {
113 if (is_inside_h) {
114 if (adjusted_pos_error_h.norm() > adjusted_pos_error_45.norm()) {
115 adjusted_pos_error = adjusted_pos_error_h;
116 } else {
117 adjusted_pos_error = adjusted_pos_error_45;
118 }
119 } else {
120 adjusted_pos_error = adjusted_pos_error_45;
121 }
122 }
123 }
124
125 LOG_MATRIX(DEBUG, "adjusted_pos_error", adjusted_pos_error);
Brian Silverman0ca790b2014-06-12 21:33:08 -0700126 mutable_U() =
Brian Silverman273d8a32014-05-10 22:19:09 -0700127 velocity_K * velocity_error + position_K * T * adjusted_pos_error;
128 LOG_MATRIX(DEBUG, "U is now", U());
Brian Silvermanb94069c2014-04-17 14:34:24 -0700129 } else {
130 output_was_capped_ = false;
Brian Silvermanad9e0002014-04-13 14:55:57 -0700131 }
132 }
133
134 const ::aos::controls::HPolytope<2> U_Poly_;
135 Eigen::Matrix<double, 2, 2> T, T_inverse;
Brian Silverman94738b62014-05-02 17:43:11 -0700136 bool output_was_capped_ = false;;
Brian Silvermanad9e0002014-04-13 14:55:57 -0700137 };
138
Brian Silverman2c590c32013-11-04 18:08:54 -0800139 DrivetrainMotorsSS()
Brian Silvermanad9e0002014-04-13 14:55:57 -0700140 : loop_(new LimitedDrivetrainLoop(
Brian Silverman176303a2014-04-10 10:54:55 -0700141 constants::GetValues().make_drivetrain_loop())),
142 filtered_offset_(0.0),
143 gyro_(0.0),
144 left_goal_(0.0),
145 right_goal_(0.0),
146 raw_left_(0.0),
Brian Silvermanfaac3a22014-04-30 17:54:30 -0700147 raw_right_(0.0) {
Brian Silvermanb94069c2014-04-17 14:34:24 -0700148 // Low gear on both.
149 loop_->set_controller_index(0);
brians343bc112013-02-10 01:53:46 +0000150 }
Brian Silverman176303a2014-04-10 10:54:55 -0700151
152 void SetGoal(double left, double left_velocity, double right,
153 double right_velocity) {
154 left_goal_ = left;
155 right_goal_ = right;
Brian Silverman0ca790b2014-06-12 21:33:08 -0700156 loop_->mutable_R() << left, left_velocity, right, right_velocity;
brians343bc112013-02-10 01:53:46 +0000157 }
158 void SetRawPosition(double left, double right) {
Brian Silverman176303a2014-04-10 10:54:55 -0700159 raw_right_ = right;
160 raw_left_ = left;
Austin Schuhf9286cd2014-02-11 00:51:09 -0800161 Eigen::Matrix<double, 2, 1> Y;
Brian Silverman176303a2014-04-10 10:54:55 -0700162 Y << left + filtered_offset_, right - filtered_offset_;
Austin Schuhf9286cd2014-02-11 00:51:09 -0800163 loop_->Correct(Y);
brians343bc112013-02-10 01:53:46 +0000164 }
Brian Silvermanfaac3a22014-04-30 17:54:30 -0700165 void SetPosition(double left, double right, double gyro) {
brians343bc112013-02-10 01:53:46 +0000166 // Decay the offset quickly because this gyro is great.
Brian Silvermanad9e0002014-04-13 14:55:57 -0700167 const double offset =
168 (right - left - gyro * constants::GetValues().turn_width) / 2.0;
Brian Silverman2c764f02014-04-25 09:21:21 -0500169 // TODO(brians): filtered_offset_ = offset first time around.
Brian Silverman176303a2014-04-10 10:54:55 -0700170 filtered_offset_ = 0.25 * offset + 0.75 * filtered_offset_;
171 gyro_ = gyro;
brians343bc112013-02-10 01:53:46 +0000172 SetRawPosition(left, right);
brians343bc112013-02-10 01:53:46 +0000173 }
Austin Schuh4352ac62013-03-19 06:23:16 +0000174
Ben Fredrickson890c3fe2014-03-02 00:15:16 +0000175 void SetExternalMotors(double left_voltage, double right_voltage) {
Brian Silverman0ca790b2014-06-12 21:33:08 -0700176 loop_->mutable_U() << left_voltage, right_voltage;
Ben Fredrickson890c3fe2014-03-02 00:15:16 +0000177 }
178
Brian Silvermanfaac3a22014-04-30 17:54:30 -0700179 void Update(bool stop_motors, bool enable_control_loop) {
180 if (enable_control_loop) {
Ben Fredrickson890c3fe2014-03-02 00:15:16 +0000181 loop_->Update(stop_motors);
182 } else {
183 if (stop_motors) {
Brian Silverman0ca790b2014-06-12 21:33:08 -0700184 loop_->mutable_U().setZero();
185 loop_->mutable_U_uncapped().setZero();
Ben Fredrickson890c3fe2014-03-02 00:15:16 +0000186 }
187 loop_->UpdateObserver();
188 }
Brian Silverman273d8a32014-05-10 22:19:09 -0700189 ::Eigen::Matrix<double, 4, 1> E = loop_->R() - loop_->X_hat();
Brian Silverman3146b642014-03-20 14:52:46 -0700190 LOG_MATRIX(DEBUG, "E", E);
Ben Fredrickson890c3fe2014-03-02 00:15:16 +0000191 }
192
Brian Silvermanb94069c2014-04-17 14:34:24 -0700193 double GetEstimatedRobotSpeed() const {
Ben Fredrickson890c3fe2014-03-02 00:15:16 +0000194 // lets just call the average of left and right velocities close enough
195 return (loop_->X_hat(1, 0) + loop_->X_hat(3, 0)) / 2;
196 }
Brian Silvermanada5f2c2015-02-01 02:41:14 -0500197
Brian Silvermanb94069c2014-04-17 14:34:24 -0700198 double GetEstimatedLeftEncoder() const {
Ben Fredrickson890c3fe2014-03-02 00:15:16 +0000199 return loop_->X_hat(0, 0);
200 }
Brian Silvermanada5f2c2015-02-01 02:41:14 -0500201
Brian Silvermanb94069c2014-04-17 14:34:24 -0700202 double GetEstimatedRightEncoder() const {
Ben Fredrickson890c3fe2014-03-02 00:15:16 +0000203 return loop_->X_hat(2, 0);
brians343bc112013-02-10 01:53:46 +0000204 }
205
Brian Silvermanb94069c2014-04-17 14:34:24 -0700206 bool OutputWasCapped() const {
207 return loop_->output_was_capped();
208 }
209
Brian Silvermanada5f2c2015-02-01 02:41:14 -0500210 void SendMotors(DrivetrainQueue::Output *output) const {
Austin Schuh4352ac62013-03-19 06:23:16 +0000211 if (output) {
212 output->left_voltage = loop_->U(0, 0);
213 output->right_voltage = loop_->U(1, 0);
Brian Silvermane3a277a2014-04-13 14:56:57 -0700214 output->left_high = false;
215 output->right_high = false;
brians8ad74052013-03-16 21:04:51 +0000216 }
brians343bc112013-02-10 01:53:46 +0000217 }
brians343bc112013-02-10 01:53:46 +0000218
Brian Silvermanb94069c2014-04-17 14:34:24 -0700219 const LimitedDrivetrainLoop &loop() const { return *loop_; }
220
brians343bc112013-02-10 01:53:46 +0000221 private:
Brian Silvermanad9e0002014-04-13 14:55:57 -0700222 ::std::unique_ptr<LimitedDrivetrainLoop> loop_;
Austin Schuh4352ac62013-03-19 06:23:16 +0000223
Brian Silverman176303a2014-04-10 10:54:55 -0700224 double filtered_offset_;
225 double gyro_;
226 double left_goal_;
227 double right_goal_;
228 double raw_left_;
229 double raw_right_;
brians343bc112013-02-10 01:53:46 +0000230};
231
Austin Schuh2054f5f2013-10-27 14:54:10 -0700232class PolyDrivetrain {
233 public:
Austin Schuh427b3702013-11-02 13:44:09 -0700234
235 enum Gear {
236 HIGH,
237 LOW,
238 SHIFTING_UP,
239 SHIFTING_DOWN
240 };
241 // Stall Torque in N m
242 static constexpr double kStallTorque = 2.42;
243 // Stall Current in Amps
244 static constexpr double kStallCurrent = 133;
245 // Free Speed in RPM. Used number from last year.
246 static constexpr double kFreeSpeed = 4650.0;
247 // Free Current in Amps
248 static constexpr double kFreeCurrent = 2.7;
249 // Moment of inertia of the drivetrain in kg m^2
250 // Just borrowed from last year.
251 static constexpr double J = 6.4;
252 // Mass of the robot, in kg.
253 static constexpr double m = 68;
254 // Radius of the robot, in meters (from last year).
255 static constexpr double rb = 0.617998644 / 2.0;
Brian Silverman1a6590d2013-11-04 14:46:46 -0800256 static constexpr double kWheelRadius = 0.04445;
Austin Schuh427b3702013-11-02 13:44:09 -0700257 // Resistance of the motor, divided by the number of motors.
258 static constexpr double kR = (12.0 / kStallCurrent / 4 + 0.03) / (0.93 * 0.93);
259 // Motor velocity constant
260 static constexpr double Kv =
261 ((kFreeSpeed / 60.0 * 2.0 * M_PI) / (12.0 - kR * kFreeCurrent));
262 // Torque constant
263 static constexpr double Kt = kStallTorque / kStallCurrent;
Austin Schuh427b3702013-11-02 13:44:09 -0700264
Austin Schuh2054f5f2013-10-27 14:54:10 -0700265 PolyDrivetrain()
266 : U_Poly_((Eigen::Matrix<double, 4, 2>() << /*[[*/ 1, 0 /*]*/,
267 /*[*/ -1, 0 /*]*/,
268 /*[*/ 0, 1 /*]*/,
269 /*[*/ 0, -1 /*]]*/).finished(),
270 (Eigen::Matrix<double, 4, 1>() << /*[[*/ 12 /*]*/,
271 /*[*/ 12 /*]*/,
272 /*[*/ 12 /*]*/,
273 /*[*/ 12 /*]]*/).finished()),
Brian Silverman2c590c32013-11-04 18:08:54 -0800274 loop_(new StateFeedbackLoop<2, 2, 2>(
275 constants::GetValues().make_v_drivetrain_loop())),
Austin Schuh427b3702013-11-02 13:44:09 -0700276 ttrust_(1.1),
277 wheel_(0.0),
278 throttle_(0.0),
279 quickturn_(false),
280 stale_count_(0),
281 position_time_delta_(0.01),
282 left_gear_(LOW),
Brian Silvermande8fd552013-11-03 15:53:42 -0800283 right_gear_(LOW),
284 counter_(0) {
Austin Schuh2054f5f2013-10-27 14:54:10 -0700285
Austin Schuh427b3702013-11-02 13:44:09 -0700286 last_position_.Zero();
287 position_.Zero();
Austin Schuh2054f5f2013-10-27 14:54:10 -0700288 }
Austin Schuh427b3702013-11-02 13:44:09 -0700289 static bool IsInGear(Gear gear) { return gear == LOW || gear == HIGH; }
290
Austin Schuh78d55462014-02-23 01:39:30 -0800291 static double MotorSpeed(const constants::ShifterHallEffect &hall_effect,
292 double shifter_position, double velocity) {
Austin Schuh427b3702013-11-02 13:44:09 -0700293 // TODO(austin): G_high, G_low and kWheelRadius
Austin Schuh78d55462014-02-23 01:39:30 -0800294 const double avg_hall_effect =
295 (hall_effect.clear_high + hall_effect.clear_low) / 2.0;
296
297 if (shifter_position > avg_hall_effect) {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800298 return velocity / constants::GetValues().high_gear_ratio / kWheelRadius;
Austin Schuh427b3702013-11-02 13:44:09 -0700299 } else {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800300 return velocity / constants::GetValues().low_gear_ratio / kWheelRadius;
301 }
302 }
303
Austin Schuh78d55462014-02-23 01:39:30 -0800304 Gear ComputeGear(const constants::ShifterHallEffect &hall_effect,
305 double velocity, Gear current) {
306 const double low_omega = MotorSpeed(hall_effect, 0.0, ::std::abs(velocity));
307 const double high_omega =
308 MotorSpeed(hall_effect, 1.0, ::std::abs(velocity));
Brian Silverman1a6590d2013-11-04 14:46:46 -0800309
Brian Silverman1a6590d2013-11-04 14:46:46 -0800310 double high_torque = ((12.0 - high_omega / Kv) * Kt / kR);
311 double low_torque = ((12.0 - low_omega / Kv) * Kt / kR);
312 double high_power = high_torque * high_omega;
313 double low_power = low_torque * low_omega;
Brian Silverman55a930b2013-11-04 20:59:00 -0800314
315 // TODO(aschuh): Do this right!
316 if ((current == HIGH || high_power > low_power + 160) &&
317 ::std::abs(velocity) > 0.14) {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800318 return HIGH;
319 } else {
320 return LOW;
Austin Schuh427b3702013-11-02 13:44:09 -0700321 }
322 }
323
Austin Schuh2054f5f2013-10-27 14:54:10 -0700324 void SetGoal(double wheel, double throttle, bool quickturn, bool highgear) {
Brian Silvermande8fd552013-11-03 15:53:42 -0800325 const double kWheelNonLinearity = 0.3;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700326 // Apply a sin function that's scaled to make it feel better.
327 const double angular_range = M_PI_2 * kWheelNonLinearity;
328 wheel_ = sin(angular_range * wheel) / sin(angular_range);
329 wheel_ = sin(angular_range * wheel_) / sin(angular_range);
Austin Schuh2054f5f2013-10-27 14:54:10 -0700330 quickturn_ = quickturn;
Austin Schuh427b3702013-11-02 13:44:09 -0700331
Brian Silverman1a6590d2013-11-04 14:46:46 -0800332 static const double kThrottleDeadband = 0.05;
333 if (::std::abs(throttle) < kThrottleDeadband) {
334 throttle_ = 0;
335 } else {
336 throttle_ = copysign((::std::abs(throttle) - kThrottleDeadband) /
337 (1.0 - kThrottleDeadband), throttle);
338 }
339
Austin Schuh427b3702013-11-02 13:44:09 -0700340 // TODO(austin): Fix the upshift logic to include states.
Brian Silverman1a6590d2013-11-04 14:46:46 -0800341 Gear requested_gear;
Brian Silverman55a930b2013-11-04 20:59:00 -0800342 if (false) {
Austin Schuh78d55462014-02-23 01:39:30 -0800343 const auto &values = constants::GetValues();
Brian Silverman1a6590d2013-11-04 14:46:46 -0800344 const double current_left_velocity =
345 (position_.left_encoder - last_position_.left_encoder) /
346 position_time_delta_;
347 const double current_right_velocity =
348 (position_.right_encoder - last_position_.right_encoder) /
349 position_time_delta_;
350
Austin Schuh78d55462014-02-23 01:39:30 -0800351 Gear left_requested =
352 ComputeGear(values.left_drive, current_left_velocity, left_gear_);
353 Gear right_requested =
354 ComputeGear(values.right_drive, current_right_velocity, right_gear_);
Brian Silverman1a6590d2013-11-04 14:46:46 -0800355 requested_gear =
356 (left_requested == HIGH || right_requested == HIGH) ? HIGH : LOW;
357 } else {
358 requested_gear = highgear ? HIGH : LOW;
359 }
360
361 const Gear shift_up =
362 constants::GetValues().clutch_transmission ? HIGH : SHIFTING_UP;
363 const Gear shift_down =
364 constants::GetValues().clutch_transmission ? LOW : SHIFTING_DOWN;
Austin Schuh427b3702013-11-02 13:44:09 -0700365
366 if (left_gear_ != requested_gear) {
367 if (IsInGear(left_gear_)) {
368 if (requested_gear == HIGH) {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800369 left_gear_ = shift_up;
Austin Schuh427b3702013-11-02 13:44:09 -0700370 } else {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800371 left_gear_ = shift_down;
Austin Schuh427b3702013-11-02 13:44:09 -0700372 }
Brian Silverman55a930b2013-11-04 20:59:00 -0800373 } else {
374 if (requested_gear == HIGH && left_gear_ == SHIFTING_DOWN) {
375 left_gear_ = SHIFTING_UP;
376 } else if (requested_gear == LOW && left_gear_ == SHIFTING_UP) {
377 left_gear_ = SHIFTING_DOWN;
378 }
Austin Schuh427b3702013-11-02 13:44:09 -0700379 }
380 }
381 if (right_gear_ != requested_gear) {
382 if (IsInGear(right_gear_)) {
383 if (requested_gear == HIGH) {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800384 right_gear_ = shift_up;
Austin Schuh427b3702013-11-02 13:44:09 -0700385 } else {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800386 right_gear_ = shift_down;
Austin Schuh427b3702013-11-02 13:44:09 -0700387 }
Brian Silverman55a930b2013-11-04 20:59:00 -0800388 } else {
389 if (requested_gear == HIGH && right_gear_ == SHIFTING_DOWN) {
390 right_gear_ = SHIFTING_UP;
391 } else if (requested_gear == LOW && right_gear_ == SHIFTING_UP) {
392 right_gear_ = SHIFTING_DOWN;
393 }
Austin Schuh427b3702013-11-02 13:44:09 -0700394 }
Austin Schuh2054f5f2013-10-27 14:54:10 -0700395 }
396 }
Brian Silvermanada5f2c2015-02-01 02:41:14 -0500397 void SetPosition(const DrivetrainQueue::Position *position) {
Austin Schuh78d55462014-02-23 01:39:30 -0800398 const auto &values = constants::GetValues();
Austin Schuh427b3702013-11-02 13:44:09 -0700399 if (position == NULL) {
400 ++stale_count_;
401 } else {
402 last_position_ = position_;
403 position_ = *position;
404 position_time_delta_ = (stale_count_ + 1) * 0.01;
405 stale_count_ = 0;
406 }
407
Brian Silverman93335ae2015-01-26 20:43:39 -0500408#if HAVE_SHIFTERS
Austin Schuh427b3702013-11-02 13:44:09 -0700409 if (position) {
Brian Silverman61e41fd2014-02-16 19:08:50 -0800410 GearLogging gear_logging;
Austin Schuh427b3702013-11-02 13:44:09 -0700411 // Switch to the correct controller.
Austin Schuh78d55462014-02-23 01:39:30 -0800412 const double left_middle_shifter_position =
413 (values.left_drive.clear_high + values.left_drive.clear_low) / 2.0;
414 const double right_middle_shifter_position =
415 (values.right_drive.clear_high + values.right_drive.clear_low) / 2.0;
416
Daniel Pettifd0a51d2014-11-07 16:59:24 -0800417 if (position->left_shifter_position < left_middle_shifter_position ||
418 left_gear_ == LOW) {
Austin Schuh78d55462014-02-23 01:39:30 -0800419 if (position->right_shifter_position < right_middle_shifter_position ||
420 right_gear_ == LOW) {
Brian Silverman61e41fd2014-02-16 19:08:50 -0800421 gear_logging.left_loop_high = false;
422 gear_logging.right_loop_high = false;
423 loop_->set_controller_index(gear_logging.controller_index = 0);
Austin Schuh427b3702013-11-02 13:44:09 -0700424 } else {
Brian Silverman61e41fd2014-02-16 19:08:50 -0800425 gear_logging.left_loop_high = false;
426 gear_logging.right_loop_high = true;
427 loop_->set_controller_index(gear_logging.controller_index = 1);
Austin Schuh427b3702013-11-02 13:44:09 -0700428 }
429 } else {
Austin Schuh78d55462014-02-23 01:39:30 -0800430 if (position->right_shifter_position < right_middle_shifter_position ||
Daniel Pettifd0a51d2014-11-07 16:59:24 -0800431 right_gear_ == LOW) {
Brian Silverman61e41fd2014-02-16 19:08:50 -0800432 gear_logging.left_loop_high = true;
433 gear_logging.right_loop_high = false;
434 loop_->set_controller_index(gear_logging.controller_index = 2);
Austin Schuh427b3702013-11-02 13:44:09 -0700435 } else {
Brian Silverman61e41fd2014-02-16 19:08:50 -0800436 gear_logging.left_loop_high = true;
437 gear_logging.right_loop_high = true;
438 loop_->set_controller_index(gear_logging.controller_index = 3);
Austin Schuh427b3702013-11-02 13:44:09 -0700439 }
440 }
Brian Silverman61e41fd2014-02-16 19:08:50 -0800441
Austin Schuh427b3702013-11-02 13:44:09 -0700442 // TODO(austin): Constants.
Austin Schuh78d55462014-02-23 01:39:30 -0800443 if (position->left_shifter_position > values.left_drive.clear_high && left_gear_ == SHIFTING_UP) {
Austin Schuh427b3702013-11-02 13:44:09 -0700444 left_gear_ = HIGH;
445 }
Austin Schuh78d55462014-02-23 01:39:30 -0800446 if (position->left_shifter_position < values.left_drive.clear_low && left_gear_ == SHIFTING_DOWN) {
Austin Schuh427b3702013-11-02 13:44:09 -0700447 left_gear_ = LOW;
448 }
Austin Schuh78d55462014-02-23 01:39:30 -0800449 if (position->right_shifter_position > values.right_drive.clear_high && right_gear_ == SHIFTING_UP) {
Austin Schuh427b3702013-11-02 13:44:09 -0700450 right_gear_ = HIGH;
451 }
Austin Schuh78d55462014-02-23 01:39:30 -0800452 if (position->right_shifter_position < values.right_drive.clear_low && right_gear_ == SHIFTING_DOWN) {
Austin Schuh427b3702013-11-02 13:44:09 -0700453 right_gear_ = LOW;
454 }
Brian Silverman61e41fd2014-02-16 19:08:50 -0800455
456 gear_logging.left_state = left_gear_;
457 gear_logging.right_state = right_gear_;
458 LOG_STRUCT(DEBUG, "state", gear_logging);
Austin Schuh427b3702013-11-02 13:44:09 -0700459 }
Brian Silverman93335ae2015-01-26 20:43:39 -0500460#else
461 (void) values;
462#endif
Austin Schuh427b3702013-11-02 13:44:09 -0700463 }
464
Austin Schuh2054f5f2013-10-27 14:54:10 -0700465 double FilterVelocity(double throttle) {
466 const Eigen::Matrix<double, 2, 2> FF =
467 loop_->B().inverse() *
468 (Eigen::Matrix<double, 2, 2>::Identity() - loop_->A());
469
470 constexpr int kHighGearController = 3;
471 const Eigen::Matrix<double, 2, 2> FF_high =
Brian Silverman273d8a32014-05-10 22:19:09 -0700472 loop_->controller(kHighGearController).plant.B().inverse() *
Austin Schuh2054f5f2013-10-27 14:54:10 -0700473 (Eigen::Matrix<double, 2, 2>::Identity() -
Brian Silverman273d8a32014-05-10 22:19:09 -0700474 loop_->controller(kHighGearController).plant.A());
Austin Schuh2054f5f2013-10-27 14:54:10 -0700475
476 ::Eigen::Matrix<double, 1, 2> FF_sum = FF.colwise().sum();
477 int min_FF_sum_index;
478 const double min_FF_sum = FF_sum.minCoeff(&min_FF_sum_index);
479 const double min_K_sum = loop_->K().col(min_FF_sum_index).sum();
480 const double high_min_FF_sum = FF_high.col(0).sum();
481
482 const double adjusted_ff_voltage = ::aos::Clip(
483 throttle * 12.0 * min_FF_sum / high_min_FF_sum, -12.0, 12.0);
484 return ((adjusted_ff_voltage +
Brian Silvermana21c3a22014-06-12 21:49:15 -0700485 ttrust_ * min_K_sum * (loop_->X_hat(0, 0) + loop_->X_hat(1, 0)) / 2.0) /
Austin Schuh2054f5f2013-10-27 14:54:10 -0700486 (ttrust_ * min_K_sum + min_FF_sum));
487 }
488
Brian Silverman718b1d72013-10-28 16:22:45 -0700489 double MaxVelocity() {
490 const Eigen::Matrix<double, 2, 2> FF =
491 loop_->B().inverse() *
492 (Eigen::Matrix<double, 2, 2>::Identity() - loop_->A());
493
494 constexpr int kHighGearController = 3;
495 const Eigen::Matrix<double, 2, 2> FF_high =
Brian Silverman273d8a32014-05-10 22:19:09 -0700496 loop_->controller(kHighGearController).plant.B().inverse() *
Brian Silverman718b1d72013-10-28 16:22:45 -0700497 (Eigen::Matrix<double, 2, 2>::Identity() -
Brian Silverman273d8a32014-05-10 22:19:09 -0700498 loop_->controller(kHighGearController).plant.A());
Brian Silverman718b1d72013-10-28 16:22:45 -0700499
500 ::Eigen::Matrix<double, 1, 2> FF_sum = FF.colwise().sum();
501 int min_FF_sum_index;
502 const double min_FF_sum = FF_sum.minCoeff(&min_FF_sum_index);
503 //const double min_K_sum = loop_->K().col(min_FF_sum_index).sum();
504 const double high_min_FF_sum = FF_high.col(0).sum();
505
506 const double adjusted_ff_voltage = ::aos::Clip(
507 12.0 * min_FF_sum / high_min_FF_sum, -12.0, 12.0);
508 return adjusted_ff_voltage / min_FF_sum;
509 }
510
Austin Schuh2054f5f2013-10-27 14:54:10 -0700511 void Update() {
Austin Schuh78d55462014-02-23 01:39:30 -0800512 const auto &values = constants::GetValues();
Austin Schuh427b3702013-11-02 13:44:09 -0700513 // TODO(austin): Observer for the current velocity instead of difference
514 // calculations.
Brian Silvermande8fd552013-11-03 15:53:42 -0800515 ++counter_;
Brian Silverman93335ae2015-01-26 20:43:39 -0500516#if HAVE_SHIFTERS
Austin Schuh427b3702013-11-02 13:44:09 -0700517 const double current_left_velocity =
Brian Silvermande8fd552013-11-03 15:53:42 -0800518 (position_.left_encoder - last_position_.left_encoder) /
Austin Schuh427b3702013-11-02 13:44:09 -0700519 position_time_delta_;
520 const double current_right_velocity =
Brian Silvermande8fd552013-11-03 15:53:42 -0800521 (position_.right_encoder - last_position_.right_encoder) /
Austin Schuh427b3702013-11-02 13:44:09 -0700522 position_time_delta_;
523 const double left_motor_speed =
Austin Schuh78d55462014-02-23 01:39:30 -0800524 MotorSpeed(values.left_drive, position_.left_shifter_position,
525 current_left_velocity);
Austin Schuh427b3702013-11-02 13:44:09 -0700526 const double right_motor_speed =
Austin Schuh78d55462014-02-23 01:39:30 -0800527 MotorSpeed(values.right_drive, position_.right_shifter_position,
528 current_right_velocity);
Austin Schuh2054f5f2013-10-27 14:54:10 -0700529
Brian Silverman61e41fd2014-02-16 19:08:50 -0800530 {
531 CIMLogging logging;
532
533 // Reset the CIM model to the current conditions to be ready for when we
534 // shift.
535 if (IsInGear(left_gear_)) {
536 logging.left_in_gear = true;
Brian Silverman61e41fd2014-02-16 19:08:50 -0800537 } else {
538 logging.left_in_gear = false;
539 }
540 logging.left_motor_speed = left_motor_speed;
541 logging.left_velocity = current_left_velocity;
542 if (IsInGear(right_gear_)) {
543 logging.right_in_gear = true;
Brian Silverman61e41fd2014-02-16 19:08:50 -0800544 } else {
545 logging.right_in_gear = false;
546 }
547 logging.right_motor_speed = right_motor_speed;
548 logging.right_velocity = current_right_velocity;
549
550 LOG_STRUCT(DEBUG, "currently", logging);
Austin Schuh427b3702013-11-02 13:44:09 -0700551 }
Brian Silverman93335ae2015-01-26 20:43:39 -0500552#else
553 (void) values;
554#endif
Austin Schuh2054f5f2013-10-27 14:54:10 -0700555
Brian Silverman93335ae2015-01-26 20:43:39 -0500556#if HAVE_SHIFTERS
Austin Schuh427b3702013-11-02 13:44:09 -0700557 if (IsInGear(left_gear_) && IsInGear(right_gear_)) {
Brian Silverman93335ae2015-01-26 20:43:39 -0500558#else
559 {
560#endif
Austin Schuh427b3702013-11-02 13:44:09 -0700561 // FF * X = U (steady state)
562 const Eigen::Matrix<double, 2, 2> FF =
563 loop_->B().inverse() *
564 (Eigen::Matrix<double, 2, 2>::Identity() - loop_->A());
565
566 // Invert the plant to figure out how the velocity filter would have to
567 // work
568 // out in order to filter out the forwards negative inertia.
569 // This math assumes that the left and right power and velocity are
570 // equals,
571 // and that the plant is the same on the left and right.
572 const double fvel = FilterVelocity(throttle_);
573
574 const double sign_svel = wheel_ * ((fvel > 0.0) ? 1.0 : -1.0);
575 double steering_velocity;
576 if (quickturn_) {
577 steering_velocity = wheel_ * MaxVelocity();
578 } else {
579 steering_velocity = ::std::abs(fvel) * wheel_;
580 }
581 const double left_velocity = fvel - steering_velocity;
582 const double right_velocity = fvel + steering_velocity;
583
584 // Integrate velocity to get the position.
585 // This position is used to get integral control.
Brian Silverman0ca790b2014-06-12 21:33:08 -0700586 loop_->mutable_R() << left_velocity, right_velocity;
Austin Schuh427b3702013-11-02 13:44:09 -0700587
588 if (!quickturn_) {
589 // K * R = w
590 Eigen::Matrix<double, 1, 2> equality_k;
591 equality_k << 1 + sign_svel, -(1 - sign_svel);
592 const double equality_w = 0.0;
593
594 // Construct a constraint on R by manipulating the constraint on U
595 ::aos::controls::HPolytope<2> R_poly = ::aos::controls::HPolytope<2>(
596 U_Poly_.H() * (loop_->K() + FF),
Brian Silverman273d8a32014-05-10 22:19:09 -0700597 U_Poly_.k() + U_Poly_.H() * loop_->K() * loop_->X_hat());
Austin Schuh427b3702013-11-02 13:44:09 -0700598
599 // Limit R back inside the box.
Brian Silverman0ca790b2014-06-12 21:33:08 -0700600 loop_->mutable_R() =
Brian Silverman273d8a32014-05-10 22:19:09 -0700601 CoerceGoal(R_poly, equality_k, equality_w, loop_->R());
Austin Schuh427b3702013-11-02 13:44:09 -0700602 }
603
Brian Silverman273d8a32014-05-10 22:19:09 -0700604 const Eigen::Matrix<double, 2, 1> FF_volts = FF * loop_->R();
Austin Schuh427b3702013-11-02 13:44:09 -0700605 const Eigen::Matrix<double, 2, 1> U_ideal =
Brian Silverman273d8a32014-05-10 22:19:09 -0700606 loop_->K() * (loop_->R() - loop_->X_hat()) + FF_volts;
Austin Schuh427b3702013-11-02 13:44:09 -0700607
608 for (int i = 0; i < 2; i++) {
Brian Silverman0ca790b2014-06-12 21:33:08 -0700609 loop_->mutable_U()[i] = ::aos::Clip(U_ideal[i], -12, 12);
Austin Schuh427b3702013-11-02 13:44:09 -0700610 }
Brian Silvermande8fd552013-11-03 15:53:42 -0800611
612 // TODO(austin): Model this better.
613 // TODO(austin): Feed back?
Brian Silverman0ca790b2014-06-12 21:33:08 -0700614 loop_->mutable_X_hat() =
Brian Silverman273d8a32014-05-10 22:19:09 -0700615 loop_->A() * loop_->X_hat() + loop_->B() * loop_->U();
Brian Silverman93335ae2015-01-26 20:43:39 -0500616#if HAVE_SHIFTERS
Brian Silverman718b1d72013-10-28 16:22:45 -0700617 } else {
Austin Schuh427b3702013-11-02 13:44:09 -0700618 // Any motor is not in gear. Speed match.
619 ::Eigen::Matrix<double, 1, 1> R_left;
Austin Schuh427b3702013-11-02 13:44:09 -0700620 ::Eigen::Matrix<double, 1, 1> R_right;
Austin Schuhb5afb692014-03-02 11:54:11 -0800621 R_left(0, 0) = left_motor_speed;
Austin Schuh427b3702013-11-02 13:44:09 -0700622 R_right(0, 0) = right_motor_speed;
Austin Schuhb5afb692014-03-02 11:54:11 -0800623
624 const double wiggle =
Brian Silvermanf970f2c2014-03-22 19:34:30 -0700625 (static_cast<double>((counter_ % 20) / 10) - 0.5) * 5.0;
Austin Schuhb5afb692014-03-02 11:54:11 -0800626
Brian Silvermana21c3a22014-06-12 21:49:15 -0700627 loop_->mutable_U(0, 0) = ::aos::Clip(
Brian Silverman56658322014-03-22 16:57:22 -0700628 (R_left / Kv)(0, 0) + (IsInGear(left_gear_) ? 0 : wiggle),
629 -12.0, 12.0);
Brian Silvermana21c3a22014-06-12 21:49:15 -0700630 loop_->mutable_U(1, 0) = ::aos::Clip(
Brian Silverman56658322014-03-22 16:57:22 -0700631 (R_right / Kv)(0, 0) + (IsInGear(right_gear_) ? 0 : wiggle),
632 -12.0, 12.0);
Brian Silverman0ca790b2014-06-12 21:33:08 -0700633 loop_->mutable_U() *= 12.0 / position_.battery_voltage;
Brian Silverman93335ae2015-01-26 20:43:39 -0500634#endif
Austin Schuh2054f5f2013-10-27 14:54:10 -0700635 }
Austin Schuh2054f5f2013-10-27 14:54:10 -0700636 }
637
Brian Silvermanada5f2c2015-02-01 02:41:14 -0500638 void SendMotors(DrivetrainQueue::Output *output) {
Brian Silvermande8fd552013-11-03 15:53:42 -0800639 if (output != NULL) {
640 output->left_voltage = loop_->U(0, 0);
641 output->right_voltage = loop_->U(1, 0);
Brian Silverman61e41fd2014-02-16 19:08:50 -0800642 output->left_high = left_gear_ == HIGH || left_gear_ == SHIFTING_UP;
643 output->right_high = right_gear_ == HIGH || right_gear_ == SHIFTING_UP;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700644 }
645 }
646
647 private:
648 const ::aos::controls::HPolytope<2> U_Poly_;
649
650 ::std::unique_ptr<StateFeedbackLoop<2, 2, 2>> loop_;
651
Austin Schuh427b3702013-11-02 13:44:09 -0700652 const double ttrust_;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700653 double wheel_;
654 double throttle_;
655 bool quickturn_;
Austin Schuh427b3702013-11-02 13:44:09 -0700656 int stale_count_;
657 double position_time_delta_;
658 Gear left_gear_;
659 Gear right_gear_;
Brian Silvermanada5f2c2015-02-01 02:41:14 -0500660 DrivetrainQueue::Position last_position_;
661 DrivetrainQueue::Position position_;
Brian Silvermande8fd552013-11-03 15:53:42 -0800662 int counter_;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700663};
Brian Silvermancec3c8d2013-12-20 21:04:55 -0800664constexpr double PolyDrivetrain::kStallTorque;
665constexpr double PolyDrivetrain::kStallCurrent;
666constexpr double PolyDrivetrain::kFreeSpeed;
667constexpr double PolyDrivetrain::kFreeCurrent;
668constexpr double PolyDrivetrain::J;
669constexpr double PolyDrivetrain::m;
670constexpr double PolyDrivetrain::rb;
671constexpr double PolyDrivetrain::kWheelRadius;
672constexpr double PolyDrivetrain::kR;
673constexpr double PolyDrivetrain::Kv;
674constexpr double PolyDrivetrain::Kt;
675
Austin Schuh2054f5f2013-10-27 14:54:10 -0700676
Brian Silvermanada5f2c2015-02-01 02:41:14 -0500677void DrivetrainLoop::RunIteration(const DrivetrainQueue::Goal *goal,
678 const DrivetrainQueue::Position *position,
679 DrivetrainQueue::Output *output,
680 DrivetrainQueue::Status * status) {
brians343bc112013-02-10 01:53:46 +0000681 // TODO(aschuh): These should be members of the class.
682 static DrivetrainMotorsSS dt_closedloop;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700683 static PolyDrivetrain dt_openloop;
brians343bc112013-02-10 01:53:46 +0000684
685 bool bad_pos = false;
Austin Schuh427b3702013-11-02 13:44:09 -0700686 if (position == nullptr) {
Brian Silverman50a9d032014-02-16 17:20:57 -0800687 LOG_INTERVAL(no_position_);
brians343bc112013-02-10 01:53:46 +0000688 bad_pos = true;
689 }
Brian Silverman9c9ab422014-02-25 13:42:53 -0800690 no_position_.Print();
brians343bc112013-02-10 01:53:46 +0000691
692 double wheel = goal->steering;
693 double throttle = goal->throttle;
694 bool quickturn = goal->quickturn;
Brian Silverman93335ae2015-01-26 20:43:39 -0500695#if HAVE_SHIFTERS
brians343bc112013-02-10 01:53:46 +0000696 bool highgear = goal->highgear;
Brian Silverman93335ae2015-01-26 20:43:39 -0500697#endif
brians343bc112013-02-10 01:53:46 +0000698
699 bool control_loop_driving = goal->control_loop_driving;
700 double left_goal = goal->left_goal;
701 double right_goal = goal->right_goal;
702
Austin Schuh2054f5f2013-10-27 14:54:10 -0700703 dt_closedloop.SetGoal(left_goal, goal->left_velocity_goal, right_goal,
704 goal->right_velocity_goal);
brians343bc112013-02-10 01:53:46 +0000705 if (!bad_pos) {
706 const double left_encoder = position->left_encoder;
707 const double right_encoder = position->right_encoder;
Brian Silverman6bf0d3c2014-03-08 12:52:54 -0800708 if (gyro_reading.FetchLatest()) {
Brian Silverman74e5b4e2014-03-09 16:58:58 -0700709 LOG_STRUCT(DEBUG, "using", *gyro_reading.get());
710 dt_closedloop.SetPosition(left_encoder, right_encoder,
Brian Silvermanfaac3a22014-04-30 17:54:30 -0700711 gyro_reading->angle);
brians343bc112013-02-10 01:53:46 +0000712 } else {
713 dt_closedloop.SetRawPosition(left_encoder, right_encoder);
714 }
715 }
Austin Schuh427b3702013-11-02 13:44:09 -0700716 dt_openloop.SetPosition(position);
Brian Silverman93335ae2015-01-26 20:43:39 -0500717#if HAVE_SHIFTERS
brians343bc112013-02-10 01:53:46 +0000718 dt_openloop.SetGoal(wheel, throttle, quickturn, highgear);
Brian Silverman93335ae2015-01-26 20:43:39 -0500719#else
720 dt_openloop.SetGoal(wheel, throttle, quickturn, false);
721#endif
brians343bc112013-02-10 01:53:46 +0000722 dt_openloop.Update();
Ben Fredrickson890c3fe2014-03-02 00:15:16 +0000723
Brian Silverman2845c4c2013-03-16 19:54:08 -0700724 if (control_loop_driving) {
Brian Silvermanfaac3a22014-04-30 17:54:30 -0700725 dt_closedloop.Update(output == NULL, true);
Brian Silverman2845c4c2013-03-16 19:54:08 -0700726 dt_closedloop.SendMotors(output);
727 } else {
728 dt_openloop.SendMotors(output);
Austin Schuhb5afb692014-03-02 11:54:11 -0800729 if (output) {
730 dt_closedloop.SetExternalMotors(output->left_voltage,
731 output->right_voltage);
732 }
Brian Silvermanfaac3a22014-04-30 17:54:30 -0700733 dt_closedloop.Update(output == NULL, false);
brians343bc112013-02-10 01:53:46 +0000734 }
Ben Fredrickson890c3fe2014-03-02 00:15:16 +0000735
Brian Silverman2c764f02014-04-25 09:21:21 -0500736 // set the output status of the control loop state
Ben Fredrickson890c3fe2014-03-02 00:15:16 +0000737 if (status) {
738 bool done = false;
739 if (goal) {
740 done = ((::std::abs(goal->left_goal -
741 dt_closedloop.GetEstimatedLeftEncoder()) <
742 constants::GetValues().drivetrain_done_distance) &&
743 (::std::abs(goal->right_goal -
744 dt_closedloop.GetEstimatedRightEncoder()) <
745 constants::GetValues().drivetrain_done_distance));
746 }
747 status->is_done = done;
748 status->robot_speed = dt_closedloop.GetEstimatedRobotSpeed();
Austin Schuh577edf62014-04-13 10:33:05 -0700749 status->filtered_left_position = dt_closedloop.GetEstimatedLeftEncoder();
750 status->filtered_right_position = dt_closedloop.GetEstimatedRightEncoder();
Brian Silvermanb94069c2014-04-17 14:34:24 -0700751 status->output_was_capped = dt_closedloop.OutputWasCapped();
752 status->uncapped_left_voltage = dt_closedloop.loop().U_uncapped(0, 0);
753 status->uncapped_right_voltage = dt_closedloop.loop().U_uncapped(1, 0);
brians343bc112013-02-10 01:53:46 +0000754 }
755}
756
757} // namespace control_loops
758} // namespace frc971