blob: ca9c3c72782670a028fc08974326ad953fc6769e [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
James Kuszmaulf254c1a2013-03-10 16:31:26 -070015#include "frc971/control_loops/state_feedback_loop.h"
James Kuszmaulfb0e0ae2014-03-25 07:04:47 -070016#include "frc971/control_loops/coerce_goal.h"
Austin Schuh427b3702013-11-02 13:44:09 -070017#include "frc971/control_loops/drivetrain/polydrivetrain_cim_plant.h"
James Kuszmaulf254c1a2013-03-10 16:31:26 -070018#include "frc971/control_loops/drivetrain/drivetrain.q.h"
Brian Silverman6bf0d3c2014-03-08 12:52:54 -080019#include "frc971/queues/other_sensors.q.h"
Brian Silverman1a6590d2013-11-04 14:46:46 -080020#include "frc971/constants.h"
brians343bc112013-02-10 01:53:46 +000021
Brian Silverman6bf0d3c2014-03-08 12:52:54 -080022using frc971::sensors::gyro_reading;
brians343bc112013-02-10 01:53:46 +000023
24namespace frc971 {
25namespace control_loops {
26
Austin Schuh4352ac62013-03-19 06:23:16 +000027class DrivetrainMotorsSS {
brians343bc112013-02-10 01:53:46 +000028 public:
Brian Silvermanad9e0002014-04-13 14:55:57 -070029 class LimitedDrivetrainLoop : public StateFeedbackLoop<4, 2, 2> {
30 public:
31 LimitedDrivetrainLoop(const StateFeedbackLoop<4, 2, 2> &loop)
32 : StateFeedbackLoop<4, 2, 2>(loop),
33 U_Poly_((Eigen::Matrix<double, 4, 2>() << 1, 0,
34 -1, 0,
35 0, 1,
36 0, -1).finished(),
37 (Eigen::Matrix<double, 4, 1>() << 12.0, 12.0,
38 12.0, 12.0).finished()) {
39 ::aos::controls::HPolytope<0>::Init();
40 T << 1, -1, 1, 1;
41 T_inverse = T.inverse();
42 }
43
Brian Silvermanb94069c2014-04-17 14:34:24 -070044 bool output_was_capped() const {
45 return output_was_capped_;
46 }
47
Brian Silvermanad9e0002014-04-13 14:55:57 -070048 private:
49 virtual void CapU() {
50 const Eigen::Matrix<double, 4, 1> error = R - X_hat;
51
Brian Silvermanb94069c2014-04-17 14:34:24 -070052 if (::std::abs(U(0, 0)) > 12.0 || ::std::abs(U(1, 0)) > 12.0) {
53 output_was_capped_ = true;
Brian Silvermanad9e0002014-04-13 14:55:57 -070054 LOG_MATRIX(DEBUG, "U at start", U);
55
56 Eigen::Matrix<double, 2, 2> position_K;
57 position_K << K(0, 0), K(0, 2),
58 K(1, 0), K(1, 2);
59 Eigen::Matrix<double, 2, 2> velocity_K;
60 velocity_K << K(0, 1), K(0, 3),
61 K(1, 1), K(1, 3);
62
63 Eigen::Matrix<double, 2, 1> position_error;
64 position_error << error(0, 0), error(2, 0);
65 const auto drive_error = T_inverse * position_error;
66 Eigen::Matrix<double, 2, 1> velocity_error;
67 velocity_error << error(1, 0), error(3, 0);
68 LOG_MATRIX(DEBUG, "error", error);
69
70 const auto &poly = U_Poly_;
71 const Eigen::Matrix<double, 4, 2> pos_poly_H = poly.H() * position_K * T;
72 const Eigen::Matrix<double, 4, 1> pos_poly_k =
73 poly.k() - poly.H() * velocity_K * velocity_error;
74 const ::aos::controls::HPolytope<2> pos_poly(pos_poly_H, pos_poly_k);
75
76 Eigen::Matrix<double, 2, 1> adjusted_pos_error;
77 {
78 const auto &P = drive_error;
79
80 Eigen::Matrix<double, 1, 2> L45;
81 L45 << ::aos::sign(P(1, 0)), -::aos::sign(P(0, 0));
82 const double w45 = 0;
83
84 Eigen::Matrix<double, 1, 2> LH;
85 if (::std::abs(P(0, 0)) > ::std::abs(P(1, 0))) {
86 LH << 0, 1;
87 } else {
88 LH << 1, 0;
89 }
90 const double wh = LH.dot(P);
91
92 Eigen::Matrix<double, 2, 2> standard;
93 standard << L45, LH;
94 Eigen::Matrix<double, 2, 1> W;
95 W << w45, wh;
96 const Eigen::Matrix<double, 2, 1> intersection = standard.inverse() * W;
97
98 bool is_inside_h;
99 const auto adjusted_pos_error_h =
100 DoCoerceGoal(pos_poly, LH, wh, drive_error, &is_inside_h);
101 const auto adjusted_pos_error_45 =
102 DoCoerceGoal(pos_poly, L45, w45, intersection, nullptr);
103 if (pos_poly.IsInside(intersection)) {
104 adjusted_pos_error = adjusted_pos_error_h;
105 } else {
106 if (is_inside_h) {
107 if (adjusted_pos_error_h.norm() > adjusted_pos_error_45.norm()) {
108 adjusted_pos_error = adjusted_pos_error_h;
109 } else {
110 adjusted_pos_error = adjusted_pos_error_45;
111 }
112 } else {
113 adjusted_pos_error = adjusted_pos_error_45;
114 }
115 }
116 }
117
118 LOG_MATRIX(DEBUG, "adjusted_pos_error", adjusted_pos_error);
119 U = velocity_K * velocity_error + position_K * T * adjusted_pos_error;
120 LOG_MATRIX(DEBUG, "U is now", U);
Brian Silvermanb94069c2014-04-17 14:34:24 -0700121 } else {
122 output_was_capped_ = false;
Brian Silvermanad9e0002014-04-13 14:55:57 -0700123 }
124 }
125
126 const ::aos::controls::HPolytope<2> U_Poly_;
127 Eigen::Matrix<double, 2, 2> T, T_inverse;
Brian Silvermanb94069c2014-04-17 14:34:24 -0700128 bool output_was_capped_;
Brian Silvermanad9e0002014-04-13 14:55:57 -0700129 };
130
Brian Silverman2c590c32013-11-04 18:08:54 -0800131 DrivetrainMotorsSS()
Brian Silvermanad9e0002014-04-13 14:55:57 -0700132 : loop_(new LimitedDrivetrainLoop(
Brian Silverman176303a2014-04-10 10:54:55 -0700133 constants::GetValues().make_drivetrain_loop())),
134 filtered_offset_(0.0),
135 gyro_(0.0),
136 left_goal_(0.0),
137 right_goal_(0.0),
138 raw_left_(0.0),
Brian Silvermanfaac3a22014-04-30 17:54:30 -0700139 raw_right_(0.0) {
Brian Silvermanb94069c2014-04-17 14:34:24 -0700140 // Low gear on both.
141 loop_->set_controller_index(0);
brians343bc112013-02-10 01:53:46 +0000142 }
Brian Silverman176303a2014-04-10 10:54:55 -0700143
144 void SetGoal(double left, double left_velocity, double right,
145 double right_velocity) {
146 left_goal_ = left;
147 right_goal_ = right;
Austin Schuh4352ac62013-03-19 06:23:16 +0000148 loop_->R << left, left_velocity, right, right_velocity;
brians343bc112013-02-10 01:53:46 +0000149 }
150 void SetRawPosition(double left, double right) {
Brian Silverman176303a2014-04-10 10:54:55 -0700151 raw_right_ = right;
152 raw_left_ = left;
Austin Schuhf9286cd2014-02-11 00:51:09 -0800153 Eigen::Matrix<double, 2, 1> Y;
Brian Silverman176303a2014-04-10 10:54:55 -0700154 Y << left + filtered_offset_, right - filtered_offset_;
Austin Schuhf9286cd2014-02-11 00:51:09 -0800155 loop_->Correct(Y);
brians343bc112013-02-10 01:53:46 +0000156 }
Brian Silvermanfaac3a22014-04-30 17:54:30 -0700157 void SetPosition(double left, double right, double gyro) {
brians343bc112013-02-10 01:53:46 +0000158 // Decay the offset quickly because this gyro is great.
Brian Silvermanad9e0002014-04-13 14:55:57 -0700159 const double offset =
160 (right - left - gyro * constants::GetValues().turn_width) / 2.0;
Brian Silverman176303a2014-04-10 10:54:55 -0700161 filtered_offset_ = 0.25 * offset + 0.75 * filtered_offset_;
162 gyro_ = gyro;
brians343bc112013-02-10 01:53:46 +0000163 SetRawPosition(left, right);
brians343bc112013-02-10 01:53:46 +0000164 }
Austin Schuh4352ac62013-03-19 06:23:16 +0000165
Ben Fredrickson890c3fe2014-03-02 00:15:16 +0000166 void SetExternalMotors(double left_voltage, double right_voltage) {
167 loop_->U << left_voltage, right_voltage;
168 }
169
Brian Silvermanfaac3a22014-04-30 17:54:30 -0700170 void Update(bool stop_motors, bool enable_control_loop) {
171 if (enable_control_loop) {
Ben Fredrickson890c3fe2014-03-02 00:15:16 +0000172 loop_->Update(stop_motors);
173 } else {
174 if (stop_motors) {
175 loop_->U.setZero();
176 loop_->U_uncapped.setZero();
177 }
178 loop_->UpdateObserver();
179 }
Brian Silverman3146b642014-03-20 14:52:46 -0700180 ::Eigen::Matrix<double, 4, 1> E = loop_->R - loop_->X_hat;
181 LOG_MATRIX(DEBUG, "E", E);
Ben Fredrickson890c3fe2014-03-02 00:15:16 +0000182 }
183
Brian Silvermanb94069c2014-04-17 14:34:24 -0700184 double GetEstimatedRobotSpeed() const {
Ben Fredrickson890c3fe2014-03-02 00:15:16 +0000185 // lets just call the average of left and right velocities close enough
186 return (loop_->X_hat(1, 0) + loop_->X_hat(3, 0)) / 2;
187 }
188
Brian Silvermanb94069c2014-04-17 14:34:24 -0700189 double GetEstimatedLeftEncoder() const {
Ben Fredrickson890c3fe2014-03-02 00:15:16 +0000190 return loop_->X_hat(0, 0);
191 }
192
Brian Silvermanb94069c2014-04-17 14:34:24 -0700193 double GetEstimatedRightEncoder() const {
Ben Fredrickson890c3fe2014-03-02 00:15:16 +0000194 return loop_->X_hat(2, 0);
brians343bc112013-02-10 01:53:46 +0000195 }
196
Brian Silvermanb94069c2014-04-17 14:34:24 -0700197 bool OutputWasCapped() const {
198 return loop_->output_was_capped();
199 }
200
201 void SendMotors(Drivetrain::Output *output) const {
Austin Schuh4352ac62013-03-19 06:23:16 +0000202 if (output) {
203 output->left_voltage = loop_->U(0, 0);
204 output->right_voltage = loop_->U(1, 0);
Brian Silvermane3a277a2014-04-13 14:56:57 -0700205 output->left_high = false;
206 output->right_high = false;
brians8ad74052013-03-16 21:04:51 +0000207 }
brians343bc112013-02-10 01:53:46 +0000208 }
brians343bc112013-02-10 01:53:46 +0000209
Brian Silvermanb94069c2014-04-17 14:34:24 -0700210 const LimitedDrivetrainLoop &loop() const { return *loop_; }
211
brians343bc112013-02-10 01:53:46 +0000212 private:
Brian Silvermanad9e0002014-04-13 14:55:57 -0700213 ::std::unique_ptr<LimitedDrivetrainLoop> loop_;
Austin Schuh4352ac62013-03-19 06:23:16 +0000214
Brian Silverman176303a2014-04-10 10:54:55 -0700215 double filtered_offset_;
216 double gyro_;
217 double left_goal_;
218 double right_goal_;
219 double raw_left_;
220 double raw_right_;
brians343bc112013-02-10 01:53:46 +0000221};
222
Austin Schuh2054f5f2013-10-27 14:54:10 -0700223class PolyDrivetrain {
224 public:
Austin Schuh427b3702013-11-02 13:44:09 -0700225
226 enum Gear {
227 HIGH,
228 LOW,
229 SHIFTING_UP,
230 SHIFTING_DOWN
231 };
232 // Stall Torque in N m
233 static constexpr double kStallTorque = 2.42;
234 // Stall Current in Amps
235 static constexpr double kStallCurrent = 133;
236 // Free Speed in RPM. Used number from last year.
237 static constexpr double kFreeSpeed = 4650.0;
238 // Free Current in Amps
239 static constexpr double kFreeCurrent = 2.7;
240 // Moment of inertia of the drivetrain in kg m^2
241 // Just borrowed from last year.
242 static constexpr double J = 6.4;
243 // Mass of the robot, in kg.
244 static constexpr double m = 68;
245 // Radius of the robot, in meters (from last year).
246 static constexpr double rb = 0.617998644 / 2.0;
Brian Silverman1a6590d2013-11-04 14:46:46 -0800247 static constexpr double kWheelRadius = 0.04445;
Austin Schuh427b3702013-11-02 13:44:09 -0700248 // Resistance of the motor, divided by the number of motors.
249 static constexpr double kR = (12.0 / kStallCurrent / 4 + 0.03) / (0.93 * 0.93);
250 // Motor velocity constant
251 static constexpr double Kv =
252 ((kFreeSpeed / 60.0 * 2.0 * M_PI) / (12.0 - kR * kFreeCurrent));
253 // Torque constant
254 static constexpr double Kt = kStallTorque / kStallCurrent;
Austin Schuh427b3702013-11-02 13:44:09 -0700255
Austin Schuh2054f5f2013-10-27 14:54:10 -0700256 PolyDrivetrain()
257 : U_Poly_((Eigen::Matrix<double, 4, 2>() << /*[[*/ 1, 0 /*]*/,
258 /*[*/ -1, 0 /*]*/,
259 /*[*/ 0, 1 /*]*/,
260 /*[*/ 0, -1 /*]]*/).finished(),
261 (Eigen::Matrix<double, 4, 1>() << /*[[*/ 12 /*]*/,
262 /*[*/ 12 /*]*/,
263 /*[*/ 12 /*]*/,
264 /*[*/ 12 /*]]*/).finished()),
Brian Silverman2c590c32013-11-04 18:08:54 -0800265 loop_(new StateFeedbackLoop<2, 2, 2>(
266 constants::GetValues().make_v_drivetrain_loop())),
Austin Schuh427b3702013-11-02 13:44:09 -0700267 ttrust_(1.1),
268 wheel_(0.0),
269 throttle_(0.0),
270 quickturn_(false),
271 stale_count_(0),
272 position_time_delta_(0.01),
273 left_gear_(LOW),
Brian Silvermande8fd552013-11-03 15:53:42 -0800274 right_gear_(LOW),
275 counter_(0) {
Austin Schuh2054f5f2013-10-27 14:54:10 -0700276
Austin Schuh427b3702013-11-02 13:44:09 -0700277 last_position_.Zero();
278 position_.Zero();
Austin Schuh2054f5f2013-10-27 14:54:10 -0700279 }
Austin Schuh427b3702013-11-02 13:44:09 -0700280 static bool IsInGear(Gear gear) { return gear == LOW || gear == HIGH; }
281
Austin Schuh78d55462014-02-23 01:39:30 -0800282 static double MotorSpeed(const constants::ShifterHallEffect &hall_effect,
283 double shifter_position, double velocity) {
Austin Schuh427b3702013-11-02 13:44:09 -0700284 // TODO(austin): G_high, G_low and kWheelRadius
Austin Schuh78d55462014-02-23 01:39:30 -0800285 const double avg_hall_effect =
286 (hall_effect.clear_high + hall_effect.clear_low) / 2.0;
287
288 if (shifter_position > avg_hall_effect) {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800289 return velocity / constants::GetValues().high_gear_ratio / kWheelRadius;
Austin Schuh427b3702013-11-02 13:44:09 -0700290 } else {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800291 return velocity / constants::GetValues().low_gear_ratio / kWheelRadius;
292 }
293 }
294
Austin Schuh78d55462014-02-23 01:39:30 -0800295 Gear ComputeGear(const constants::ShifterHallEffect &hall_effect,
296 double velocity, Gear current) {
297 const double low_omega = MotorSpeed(hall_effect, 0.0, ::std::abs(velocity));
298 const double high_omega =
299 MotorSpeed(hall_effect, 1.0, ::std::abs(velocity));
Brian Silverman1a6590d2013-11-04 14:46:46 -0800300
Brian Silverman1a6590d2013-11-04 14:46:46 -0800301 double high_torque = ((12.0 - high_omega / Kv) * Kt / kR);
302 double low_torque = ((12.0 - low_omega / Kv) * Kt / kR);
303 double high_power = high_torque * high_omega;
304 double low_power = low_torque * low_omega;
Brian Silverman55a930b2013-11-04 20:59:00 -0800305
306 // TODO(aschuh): Do this right!
307 if ((current == HIGH || high_power > low_power + 160) &&
308 ::std::abs(velocity) > 0.14) {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800309 return HIGH;
310 } else {
311 return LOW;
Austin Schuh427b3702013-11-02 13:44:09 -0700312 }
313 }
314
Austin Schuh2054f5f2013-10-27 14:54:10 -0700315 void SetGoal(double wheel, double throttle, bool quickturn, bool highgear) {
Brian Silvermande8fd552013-11-03 15:53:42 -0800316 const double kWheelNonLinearity = 0.3;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700317 // Apply a sin function that's scaled to make it feel better.
318 const double angular_range = M_PI_2 * kWheelNonLinearity;
319 wheel_ = sin(angular_range * wheel) / sin(angular_range);
320 wheel_ = sin(angular_range * wheel_) / sin(angular_range);
Austin Schuh2054f5f2013-10-27 14:54:10 -0700321 quickturn_ = quickturn;
Austin Schuh427b3702013-11-02 13:44:09 -0700322
Brian Silverman1a6590d2013-11-04 14:46:46 -0800323 static const double kThrottleDeadband = 0.05;
324 if (::std::abs(throttle) < kThrottleDeadband) {
325 throttle_ = 0;
326 } else {
327 throttle_ = copysign((::std::abs(throttle) - kThrottleDeadband) /
328 (1.0 - kThrottleDeadband), throttle);
329 }
330
Austin Schuh427b3702013-11-02 13:44:09 -0700331 // TODO(austin): Fix the upshift logic to include states.
Brian Silverman1a6590d2013-11-04 14:46:46 -0800332 Gear requested_gear;
Brian Silverman55a930b2013-11-04 20:59:00 -0800333 if (false) {
Austin Schuh78d55462014-02-23 01:39:30 -0800334 const auto &values = constants::GetValues();
Brian Silverman1a6590d2013-11-04 14:46:46 -0800335 const double current_left_velocity =
336 (position_.left_encoder - last_position_.left_encoder) /
337 position_time_delta_;
338 const double current_right_velocity =
339 (position_.right_encoder - last_position_.right_encoder) /
340 position_time_delta_;
341
Austin Schuh78d55462014-02-23 01:39:30 -0800342 Gear left_requested =
343 ComputeGear(values.left_drive, current_left_velocity, left_gear_);
344 Gear right_requested =
345 ComputeGear(values.right_drive, current_right_velocity, right_gear_);
Brian Silverman1a6590d2013-11-04 14:46:46 -0800346 requested_gear =
347 (left_requested == HIGH || right_requested == HIGH) ? HIGH : LOW;
348 } else {
349 requested_gear = highgear ? HIGH : LOW;
350 }
351
352 const Gear shift_up =
353 constants::GetValues().clutch_transmission ? HIGH : SHIFTING_UP;
354 const Gear shift_down =
355 constants::GetValues().clutch_transmission ? LOW : SHIFTING_DOWN;
Austin Schuh427b3702013-11-02 13:44:09 -0700356
357 if (left_gear_ != requested_gear) {
358 if (IsInGear(left_gear_)) {
359 if (requested_gear == HIGH) {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800360 left_gear_ = shift_up;
Austin Schuh427b3702013-11-02 13:44:09 -0700361 } else {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800362 left_gear_ = shift_down;
Austin Schuh427b3702013-11-02 13:44:09 -0700363 }
Brian Silverman55a930b2013-11-04 20:59:00 -0800364 } else {
365 if (requested_gear == HIGH && left_gear_ == SHIFTING_DOWN) {
366 left_gear_ = SHIFTING_UP;
367 } else if (requested_gear == LOW && left_gear_ == SHIFTING_UP) {
368 left_gear_ = SHIFTING_DOWN;
369 }
Austin Schuh427b3702013-11-02 13:44:09 -0700370 }
371 }
372 if (right_gear_ != requested_gear) {
373 if (IsInGear(right_gear_)) {
374 if (requested_gear == HIGH) {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800375 right_gear_ = shift_up;
Austin Schuh427b3702013-11-02 13:44:09 -0700376 } else {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800377 right_gear_ = shift_down;
Austin Schuh427b3702013-11-02 13:44:09 -0700378 }
Brian Silverman55a930b2013-11-04 20:59:00 -0800379 } else {
380 if (requested_gear == HIGH && right_gear_ == SHIFTING_DOWN) {
381 right_gear_ = SHIFTING_UP;
382 } else if (requested_gear == LOW && right_gear_ == SHIFTING_UP) {
383 right_gear_ = SHIFTING_DOWN;
384 }
Austin Schuh427b3702013-11-02 13:44:09 -0700385 }
Austin Schuh2054f5f2013-10-27 14:54:10 -0700386 }
387 }
Austin Schuh427b3702013-11-02 13:44:09 -0700388 void SetPosition(const Drivetrain::Position *position) {
Austin Schuh78d55462014-02-23 01:39:30 -0800389 const auto &values = constants::GetValues();
Austin Schuh427b3702013-11-02 13:44:09 -0700390 if (position == NULL) {
391 ++stale_count_;
392 } else {
393 last_position_ = position_;
394 position_ = *position;
395 position_time_delta_ = (stale_count_ + 1) * 0.01;
396 stale_count_ = 0;
397 }
398
399 if (position) {
Brian Silverman61e41fd2014-02-16 19:08:50 -0800400 GearLogging gear_logging;
Austin Schuh427b3702013-11-02 13:44:09 -0700401 // Switch to the correct controller.
Austin Schuh78d55462014-02-23 01:39:30 -0800402 const double left_middle_shifter_position =
403 (values.left_drive.clear_high + values.left_drive.clear_low) / 2.0;
404 const double right_middle_shifter_position =
405 (values.right_drive.clear_high + values.right_drive.clear_low) / 2.0;
406
407 if (position->left_shifter_position < left_middle_shifter_position) {
408 if (position->right_shifter_position < right_middle_shifter_position ||
409 right_gear_ == LOW) {
Brian Silverman61e41fd2014-02-16 19:08:50 -0800410 gear_logging.left_loop_high = false;
411 gear_logging.right_loop_high = false;
412 loop_->set_controller_index(gear_logging.controller_index = 0);
Austin Schuh427b3702013-11-02 13:44:09 -0700413 } else {
Brian Silverman61e41fd2014-02-16 19:08:50 -0800414 gear_logging.left_loop_high = false;
415 gear_logging.right_loop_high = true;
416 loop_->set_controller_index(gear_logging.controller_index = 1);
Austin Schuh427b3702013-11-02 13:44:09 -0700417 }
418 } else {
Austin Schuh78d55462014-02-23 01:39:30 -0800419 if (position->right_shifter_position < right_middle_shifter_position ||
420 left_gear_ == LOW) {
Brian Silverman61e41fd2014-02-16 19:08:50 -0800421 gear_logging.left_loop_high = true;
422 gear_logging.right_loop_high = false;
423 loop_->set_controller_index(gear_logging.controller_index = 2);
Austin Schuh427b3702013-11-02 13:44:09 -0700424 } else {
Brian Silverman61e41fd2014-02-16 19:08:50 -0800425 gear_logging.left_loop_high = true;
426 gear_logging.right_loop_high = true;
427 loop_->set_controller_index(gear_logging.controller_index = 3);
Austin Schuh427b3702013-11-02 13:44:09 -0700428 }
429 }
Brian Silverman61e41fd2014-02-16 19:08:50 -0800430
Austin Schuh427b3702013-11-02 13:44:09 -0700431 // TODO(austin): Constants.
Austin Schuh78d55462014-02-23 01:39:30 -0800432 if (position->left_shifter_position > values.left_drive.clear_high && left_gear_ == SHIFTING_UP) {
Austin Schuh427b3702013-11-02 13:44:09 -0700433 left_gear_ = HIGH;
434 }
Austin Schuh78d55462014-02-23 01:39:30 -0800435 if (position->left_shifter_position < values.left_drive.clear_low && left_gear_ == SHIFTING_DOWN) {
Austin Schuh427b3702013-11-02 13:44:09 -0700436 left_gear_ = LOW;
437 }
Austin Schuh78d55462014-02-23 01:39:30 -0800438 if (position->right_shifter_position > values.right_drive.clear_high && right_gear_ == SHIFTING_UP) {
Austin Schuh427b3702013-11-02 13:44:09 -0700439 right_gear_ = HIGH;
440 }
Austin Schuh78d55462014-02-23 01:39:30 -0800441 if (position->right_shifter_position < values.right_drive.clear_low && right_gear_ == SHIFTING_DOWN) {
Austin Schuh427b3702013-11-02 13:44:09 -0700442 right_gear_ = LOW;
443 }
Brian Silverman61e41fd2014-02-16 19:08:50 -0800444
445 gear_logging.left_state = left_gear_;
446 gear_logging.right_state = right_gear_;
447 LOG_STRUCT(DEBUG, "state", gear_logging);
Austin Schuh427b3702013-11-02 13:44:09 -0700448 }
449 }
450
Austin Schuh2054f5f2013-10-27 14:54:10 -0700451 double FilterVelocity(double throttle) {
452 const Eigen::Matrix<double, 2, 2> FF =
453 loop_->B().inverse() *
454 (Eigen::Matrix<double, 2, 2>::Identity() - loop_->A());
455
456 constexpr int kHighGearController = 3;
457 const Eigen::Matrix<double, 2, 2> FF_high =
458 loop_->controller(kHighGearController).plant.B.inverse() *
459 (Eigen::Matrix<double, 2, 2>::Identity() -
460 loop_->controller(kHighGearController).plant.A);
461
462 ::Eigen::Matrix<double, 1, 2> FF_sum = FF.colwise().sum();
463 int min_FF_sum_index;
464 const double min_FF_sum = FF_sum.minCoeff(&min_FF_sum_index);
465 const double min_K_sum = loop_->K().col(min_FF_sum_index).sum();
466 const double high_min_FF_sum = FF_high.col(0).sum();
467
468 const double adjusted_ff_voltage = ::aos::Clip(
469 throttle * 12.0 * min_FF_sum / high_min_FF_sum, -12.0, 12.0);
470 return ((adjusted_ff_voltage +
471 ttrust_ * min_K_sum * (loop_->X_hat(0, 0) + loop_->X_hat(1, 0)) /
472 2.0) /
473 (ttrust_ * min_K_sum + min_FF_sum));
474 }
475
Brian Silverman718b1d72013-10-28 16:22:45 -0700476 double MaxVelocity() {
477 const Eigen::Matrix<double, 2, 2> FF =
478 loop_->B().inverse() *
479 (Eigen::Matrix<double, 2, 2>::Identity() - loop_->A());
480
481 constexpr int kHighGearController = 3;
482 const Eigen::Matrix<double, 2, 2> FF_high =
483 loop_->controller(kHighGearController).plant.B.inverse() *
484 (Eigen::Matrix<double, 2, 2>::Identity() -
485 loop_->controller(kHighGearController).plant.A);
486
487 ::Eigen::Matrix<double, 1, 2> FF_sum = FF.colwise().sum();
488 int min_FF_sum_index;
489 const double min_FF_sum = FF_sum.minCoeff(&min_FF_sum_index);
490 //const double min_K_sum = loop_->K().col(min_FF_sum_index).sum();
491 const double high_min_FF_sum = FF_high.col(0).sum();
492
493 const double adjusted_ff_voltage = ::aos::Clip(
494 12.0 * min_FF_sum / high_min_FF_sum, -12.0, 12.0);
495 return adjusted_ff_voltage / min_FF_sum;
496 }
497
Austin Schuh2054f5f2013-10-27 14:54:10 -0700498 void Update() {
Austin Schuh78d55462014-02-23 01:39:30 -0800499 const auto &values = constants::GetValues();
Austin Schuh427b3702013-11-02 13:44:09 -0700500 // TODO(austin): Observer for the current velocity instead of difference
501 // calculations.
Brian Silvermande8fd552013-11-03 15:53:42 -0800502 ++counter_;
Austin Schuh427b3702013-11-02 13:44:09 -0700503 const double current_left_velocity =
Brian Silvermande8fd552013-11-03 15:53:42 -0800504 (position_.left_encoder - last_position_.left_encoder) /
Austin Schuh427b3702013-11-02 13:44:09 -0700505 position_time_delta_;
506 const double current_right_velocity =
Brian Silvermande8fd552013-11-03 15:53:42 -0800507 (position_.right_encoder - last_position_.right_encoder) /
Austin Schuh427b3702013-11-02 13:44:09 -0700508 position_time_delta_;
509 const double left_motor_speed =
Austin Schuh78d55462014-02-23 01:39:30 -0800510 MotorSpeed(values.left_drive, position_.left_shifter_position,
511 current_left_velocity);
Austin Schuh427b3702013-11-02 13:44:09 -0700512 const double right_motor_speed =
Austin Schuh78d55462014-02-23 01:39:30 -0800513 MotorSpeed(values.right_drive, position_.right_shifter_position,
514 current_right_velocity);
Austin Schuh2054f5f2013-10-27 14:54:10 -0700515
Brian Silverman61e41fd2014-02-16 19:08:50 -0800516 {
517 CIMLogging logging;
518
519 // Reset the CIM model to the current conditions to be ready for when we
520 // shift.
521 if (IsInGear(left_gear_)) {
522 logging.left_in_gear = true;
Brian Silverman61e41fd2014-02-16 19:08:50 -0800523 } else {
524 logging.left_in_gear = false;
525 }
526 logging.left_motor_speed = left_motor_speed;
527 logging.left_velocity = current_left_velocity;
528 if (IsInGear(right_gear_)) {
529 logging.right_in_gear = true;
Brian Silverman61e41fd2014-02-16 19:08:50 -0800530 } else {
531 logging.right_in_gear = false;
532 }
533 logging.right_motor_speed = right_motor_speed;
534 logging.right_velocity = current_right_velocity;
535
536 LOG_STRUCT(DEBUG, "currently", logging);
Austin Schuh427b3702013-11-02 13:44:09 -0700537 }
Austin Schuh2054f5f2013-10-27 14:54:10 -0700538
Austin Schuh427b3702013-11-02 13:44:09 -0700539 if (IsInGear(left_gear_) && IsInGear(right_gear_)) {
540 // FF * X = U (steady state)
541 const Eigen::Matrix<double, 2, 2> FF =
542 loop_->B().inverse() *
543 (Eigen::Matrix<double, 2, 2>::Identity() - loop_->A());
544
545 // Invert the plant to figure out how the velocity filter would have to
546 // work
547 // out in order to filter out the forwards negative inertia.
548 // This math assumes that the left and right power and velocity are
549 // equals,
550 // and that the plant is the same on the left and right.
551 const double fvel = FilterVelocity(throttle_);
552
553 const double sign_svel = wheel_ * ((fvel > 0.0) ? 1.0 : -1.0);
554 double steering_velocity;
555 if (quickturn_) {
556 steering_velocity = wheel_ * MaxVelocity();
557 } else {
558 steering_velocity = ::std::abs(fvel) * wheel_;
559 }
560 const double left_velocity = fvel - steering_velocity;
561 const double right_velocity = fvel + steering_velocity;
562
563 // Integrate velocity to get the position.
564 // This position is used to get integral control.
565 loop_->R << left_velocity, right_velocity;
566
567 if (!quickturn_) {
568 // K * R = w
569 Eigen::Matrix<double, 1, 2> equality_k;
570 equality_k << 1 + sign_svel, -(1 - sign_svel);
571 const double equality_w = 0.0;
572
573 // Construct a constraint on R by manipulating the constraint on U
574 ::aos::controls::HPolytope<2> R_poly = ::aos::controls::HPolytope<2>(
575 U_Poly_.H() * (loop_->K() + FF),
576 U_Poly_.k() + U_Poly_.H() * loop_->K() * loop_->X_hat);
577
578 // Limit R back inside the box.
579 loop_->R = CoerceGoal(R_poly, equality_k, equality_w, loop_->R);
580 }
581
582 const Eigen::Matrix<double, 2, 1> FF_volts = FF * loop_->R;
583 const Eigen::Matrix<double, 2, 1> U_ideal =
584 loop_->K() * (loop_->R - loop_->X_hat) + FF_volts;
585
586 for (int i = 0; i < 2; i++) {
587 loop_->U[i] = ::aos::Clip(U_ideal[i], -12, 12);
588 }
Brian Silvermande8fd552013-11-03 15:53:42 -0800589
590 // TODO(austin): Model this better.
591 // TODO(austin): Feed back?
592 loop_->X_hat = loop_->A() * loop_->X_hat + loop_->B() * loop_->U;
Brian Silverman718b1d72013-10-28 16:22:45 -0700593 } else {
Austin Schuh427b3702013-11-02 13:44:09 -0700594 // Any motor is not in gear. Speed match.
595 ::Eigen::Matrix<double, 1, 1> R_left;
Austin Schuh427b3702013-11-02 13:44:09 -0700596 ::Eigen::Matrix<double, 1, 1> R_right;
Austin Schuhb5afb692014-03-02 11:54:11 -0800597 R_left(0, 0) = left_motor_speed;
Austin Schuh427b3702013-11-02 13:44:09 -0700598 R_right(0, 0) = right_motor_speed;
Austin Schuhb5afb692014-03-02 11:54:11 -0800599
600 const double wiggle =
Brian Silvermanf970f2c2014-03-22 19:34:30 -0700601 (static_cast<double>((counter_ % 20) / 10) - 0.5) * 5.0;
Austin Schuhb5afb692014-03-02 11:54:11 -0800602
Brian Silverman56658322014-03-22 16:57:22 -0700603 loop_->U(0, 0) = ::aos::Clip(
604 (R_left / Kv)(0, 0) + (IsInGear(left_gear_) ? 0 : wiggle),
605 -12.0, 12.0);
606 loop_->U(1, 0) = ::aos::Clip(
607 (R_right / Kv)(0, 0) + (IsInGear(right_gear_) ? 0 : wiggle),
608 -12.0, 12.0);
Brian Silvermande8fd552013-11-03 15:53:42 -0800609 loop_->U *= 12.0 / position_.battery_voltage;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700610 }
Austin Schuh2054f5f2013-10-27 14:54:10 -0700611 }
612
613 void SendMotors(Drivetrain::Output *output) {
Brian Silvermande8fd552013-11-03 15:53:42 -0800614 if (output != NULL) {
615 output->left_voltage = loop_->U(0, 0);
616 output->right_voltage = loop_->U(1, 0);
Brian Silverman61e41fd2014-02-16 19:08:50 -0800617 output->left_high = left_gear_ == HIGH || left_gear_ == SHIFTING_UP;
618 output->right_high = right_gear_ == HIGH || right_gear_ == SHIFTING_UP;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700619 }
620 }
621
622 private:
623 const ::aos::controls::HPolytope<2> U_Poly_;
624
625 ::std::unique_ptr<StateFeedbackLoop<2, 2, 2>> loop_;
626
Austin Schuh427b3702013-11-02 13:44:09 -0700627 const double ttrust_;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700628 double wheel_;
629 double throttle_;
630 bool quickturn_;
Austin Schuh427b3702013-11-02 13:44:09 -0700631 int stale_count_;
632 double position_time_delta_;
633 Gear left_gear_;
634 Gear right_gear_;
635 Drivetrain::Position last_position_;
636 Drivetrain::Position position_;
Brian Silvermande8fd552013-11-03 15:53:42 -0800637 int counter_;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700638};
Brian Silvermancec3c8d2013-12-20 21:04:55 -0800639constexpr double PolyDrivetrain::kStallTorque;
640constexpr double PolyDrivetrain::kStallCurrent;
641constexpr double PolyDrivetrain::kFreeSpeed;
642constexpr double PolyDrivetrain::kFreeCurrent;
643constexpr double PolyDrivetrain::J;
644constexpr double PolyDrivetrain::m;
645constexpr double PolyDrivetrain::rb;
646constexpr double PolyDrivetrain::kWheelRadius;
647constexpr double PolyDrivetrain::kR;
648constexpr double PolyDrivetrain::Kv;
649constexpr double PolyDrivetrain::Kt;
650
Austin Schuh2054f5f2013-10-27 14:54:10 -0700651
brians343bc112013-02-10 01:53:46 +0000652void DrivetrainLoop::RunIteration(const Drivetrain::Goal *goal,
653 const Drivetrain::Position *position,
654 Drivetrain::Output *output,
Ben Fredrickson890c3fe2014-03-02 00:15:16 +0000655 Drivetrain::Status * status) {
brians343bc112013-02-10 01:53:46 +0000656 // TODO(aschuh): These should be members of the class.
657 static DrivetrainMotorsSS dt_closedloop;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700658 static PolyDrivetrain dt_openloop;
brians343bc112013-02-10 01:53:46 +0000659
660 bool bad_pos = false;
Austin Schuh427b3702013-11-02 13:44:09 -0700661 if (position == nullptr) {
Brian Silverman50a9d032014-02-16 17:20:57 -0800662 LOG_INTERVAL(no_position_);
brians343bc112013-02-10 01:53:46 +0000663 bad_pos = true;
664 }
Brian Silverman9c9ab422014-02-25 13:42:53 -0800665 no_position_.Print();
brians343bc112013-02-10 01:53:46 +0000666
667 double wheel = goal->steering;
668 double throttle = goal->throttle;
669 bool quickturn = goal->quickturn;
670 bool highgear = goal->highgear;
671
672 bool control_loop_driving = goal->control_loop_driving;
673 double left_goal = goal->left_goal;
674 double right_goal = goal->right_goal;
675
Austin Schuh2054f5f2013-10-27 14:54:10 -0700676 dt_closedloop.SetGoal(left_goal, goal->left_velocity_goal, right_goal,
677 goal->right_velocity_goal);
brians343bc112013-02-10 01:53:46 +0000678 if (!bad_pos) {
679 const double left_encoder = position->left_encoder;
680 const double right_encoder = position->right_encoder;
Brian Silverman6bf0d3c2014-03-08 12:52:54 -0800681 if (gyro_reading.FetchLatest()) {
Brian Silverman74e5b4e2014-03-09 16:58:58 -0700682 LOG_STRUCT(DEBUG, "using", *gyro_reading.get());
683 dt_closedloop.SetPosition(left_encoder, right_encoder,
Brian Silvermanfaac3a22014-04-30 17:54:30 -0700684 gyro_reading->angle);
brians343bc112013-02-10 01:53:46 +0000685 } else {
686 dt_closedloop.SetRawPosition(left_encoder, right_encoder);
687 }
688 }
Austin Schuh427b3702013-11-02 13:44:09 -0700689 dt_openloop.SetPosition(position);
brians343bc112013-02-10 01:53:46 +0000690 dt_openloop.SetGoal(wheel, throttle, quickturn, highgear);
691 dt_openloop.Update();
Ben Fredrickson890c3fe2014-03-02 00:15:16 +0000692
Brian Silverman2845c4c2013-03-16 19:54:08 -0700693 if (control_loop_driving) {
Brian Silvermanfaac3a22014-04-30 17:54:30 -0700694 dt_closedloop.Update(output == NULL, true);
Brian Silverman2845c4c2013-03-16 19:54:08 -0700695 dt_closedloop.SendMotors(output);
696 } else {
697 dt_openloop.SendMotors(output);
Austin Schuhb5afb692014-03-02 11:54:11 -0800698 if (output) {
699 dt_closedloop.SetExternalMotors(output->left_voltage,
700 output->right_voltage);
701 }
Brian Silvermanfaac3a22014-04-30 17:54:30 -0700702 dt_closedloop.Update(output == NULL, false);
brians343bc112013-02-10 01:53:46 +0000703 }
Ben Fredrickson890c3fe2014-03-02 00:15:16 +0000704
Brian Silvermanfaac3a22014-04-30 17:54:30 -0700705 // set the output status of the control loop state
Ben Fredrickson890c3fe2014-03-02 00:15:16 +0000706 if (status) {
707 bool done = false;
708 if (goal) {
709 done = ((::std::abs(goal->left_goal -
710 dt_closedloop.GetEstimatedLeftEncoder()) <
711 constants::GetValues().drivetrain_done_distance) &&
712 (::std::abs(goal->right_goal -
713 dt_closedloop.GetEstimatedRightEncoder()) <
714 constants::GetValues().drivetrain_done_distance));
715 }
716 status->is_done = done;
717 status->robot_speed = dt_closedloop.GetEstimatedRobotSpeed();
Austin Schuh577edf62014-04-13 10:33:05 -0700718 status->filtered_left_position = dt_closedloop.GetEstimatedLeftEncoder();
719 status->filtered_right_position = dt_closedloop.GetEstimatedRightEncoder();
Brian Silvermanb94069c2014-04-17 14:34:24 -0700720 status->output_was_capped = dt_closedloop.OutputWasCapped();
721 status->uncapped_left_voltage = dt_closedloop.loop().U_uncapped(0, 0);
722 status->uncapped_right_voltage = dt_closedloop.loop().U_uncapped(1, 0);
brians343bc112013-02-10 01:53:46 +0000723 }
724}
725
726} // namespace control_loops
727} // namespace frc971