blob: 08cf99ecec0fd4c388d3be99c32ffb0d505b3112 [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 Silverman176303a2014-04-10 10:54:55 -0700169 filtered_offset_ = 0.25 * offset + 0.75 * filtered_offset_;
170 gyro_ = gyro;
brians343bc112013-02-10 01:53:46 +0000171 SetRawPosition(left, right);
brians343bc112013-02-10 01:53:46 +0000172 }
Austin Schuh4352ac62013-03-19 06:23:16 +0000173
Ben Fredrickson890c3fe2014-03-02 00:15:16 +0000174 void SetExternalMotors(double left_voltage, double right_voltage) {
Brian Silverman0ca790b2014-06-12 21:33:08 -0700175 loop_->mutable_U() << left_voltage, right_voltage;
Ben Fredrickson890c3fe2014-03-02 00:15:16 +0000176 }
177
Brian Silvermanfaac3a22014-04-30 17:54:30 -0700178 void Update(bool stop_motors, bool enable_control_loop) {
179 if (enable_control_loop) {
Ben Fredrickson890c3fe2014-03-02 00:15:16 +0000180 loop_->Update(stop_motors);
181 } else {
182 if (stop_motors) {
Brian Silverman0ca790b2014-06-12 21:33:08 -0700183 loop_->mutable_U().setZero();
184 loop_->mutable_U_uncapped().setZero();
Ben Fredrickson890c3fe2014-03-02 00:15:16 +0000185 }
186 loop_->UpdateObserver();
187 }
Brian Silverman273d8a32014-05-10 22:19:09 -0700188 ::Eigen::Matrix<double, 4, 1> E = loop_->R() - loop_->X_hat();
Brian Silverman3146b642014-03-20 14:52:46 -0700189 LOG_MATRIX(DEBUG, "E", E);
Ben Fredrickson890c3fe2014-03-02 00:15:16 +0000190 }
191
Brian Silvermanb94069c2014-04-17 14:34:24 -0700192 double GetEstimatedRobotSpeed() const {
Ben Fredrickson890c3fe2014-03-02 00:15:16 +0000193 // lets just call the average of left and right velocities close enough
194 return (loop_->X_hat(1, 0) + loop_->X_hat(3, 0)) / 2;
195 }
Brian Silvermanada5f2c2015-02-01 02:41:14 -0500196
Brian Silvermanb94069c2014-04-17 14:34:24 -0700197 double GetEstimatedLeftEncoder() const {
Ben Fredrickson890c3fe2014-03-02 00:15:16 +0000198 return loop_->X_hat(0, 0);
199 }
Brian Silvermanada5f2c2015-02-01 02:41:14 -0500200
Brian Silvermanb94069c2014-04-17 14:34:24 -0700201 double GetEstimatedRightEncoder() const {
Ben Fredrickson890c3fe2014-03-02 00:15:16 +0000202 return loop_->X_hat(2, 0);
brians343bc112013-02-10 01:53:46 +0000203 }
204
Brian Silvermanb94069c2014-04-17 14:34:24 -0700205 bool OutputWasCapped() const {
206 return loop_->output_was_capped();
207 }
208
Brian Silvermanada5f2c2015-02-01 02:41:14 -0500209 void SendMotors(DrivetrainQueue::Output *output) const {
Austin Schuh4352ac62013-03-19 06:23:16 +0000210 if (output) {
211 output->left_voltage = loop_->U(0, 0);
212 output->right_voltage = loop_->U(1, 0);
Brian Silvermane3a277a2014-04-13 14:56:57 -0700213 output->left_high = false;
214 output->right_high = false;
brians8ad74052013-03-16 21:04:51 +0000215 }
brians343bc112013-02-10 01:53:46 +0000216 }
brians343bc112013-02-10 01:53:46 +0000217
Brian Silvermanb94069c2014-04-17 14:34:24 -0700218 const LimitedDrivetrainLoop &loop() const { return *loop_; }
219
brians343bc112013-02-10 01:53:46 +0000220 private:
Brian Silvermanad9e0002014-04-13 14:55:57 -0700221 ::std::unique_ptr<LimitedDrivetrainLoop> loop_;
Austin Schuh4352ac62013-03-19 06:23:16 +0000222
Brian Silverman176303a2014-04-10 10:54:55 -0700223 double filtered_offset_;
224 double gyro_;
225 double left_goal_;
226 double right_goal_;
227 double raw_left_;
228 double raw_right_;
brians343bc112013-02-10 01:53:46 +0000229};
230
Austin Schuh2054f5f2013-10-27 14:54:10 -0700231class PolyDrivetrain {
232 public:
Austin Schuh427b3702013-11-02 13:44:09 -0700233
234 enum Gear {
235 HIGH,
236 LOW,
237 SHIFTING_UP,
238 SHIFTING_DOWN
239 };
240 // Stall Torque in N m
241 static constexpr double kStallTorque = 2.42;
242 // Stall Current in Amps
Brian Silverman4b36e2e2015-03-16 15:46:53 -0700243 static constexpr double kStallCurrent = 133.0;
Austin Schuh427b3702013-11-02 13:44:09 -0700244 // Free Speed in RPM. Used number from last year.
245 static constexpr double kFreeSpeed = 4650.0;
246 // Free Current in Amps
247 static constexpr double kFreeCurrent = 2.7;
248 // Moment of inertia of the drivetrain in kg m^2
249 // Just borrowed from last year.
Brian Silverman4b36e2e2015-03-16 15:46:53 -0700250 static constexpr double J = 10;
Austin Schuh427b3702013-11-02 13:44:09 -0700251 // Mass of the robot, in kg.
252 static constexpr double m = 68;
253 // Radius of the robot, in meters (from last year).
Brian Silverman4b36e2e2015-03-16 15:46:53 -0700254 static constexpr double rb = 0.9603 / 2.0;
255 static constexpr double kWheelRadius = 0.0515938;
Austin Schuh427b3702013-11-02 13:44:09 -0700256 // Resistance of the motor, divided by the number of motors.
Brian Silverman4b36e2e2015-03-16 15:46:53 -0700257 static constexpr double kR = (12.0 / kStallCurrent / 2 + 0.03) / (0.93 * 0.93);
Austin Schuh427b3702013-11-02 13:44:09 -0700258 // Motor velocity constant
259 static constexpr double Kv =
260 ((kFreeSpeed / 60.0 * 2.0 * M_PI) / (12.0 - kR * kFreeCurrent));
261 // Torque constant
262 static constexpr double Kt = kStallTorque / kStallCurrent;
Austin Schuh427b3702013-11-02 13:44:09 -0700263
Austin Schuh2054f5f2013-10-27 14:54:10 -0700264 PolyDrivetrain()
265 : U_Poly_((Eigen::Matrix<double, 4, 2>() << /*[[*/ 1, 0 /*]*/,
266 /*[*/ -1, 0 /*]*/,
267 /*[*/ 0, 1 /*]*/,
268 /*[*/ 0, -1 /*]]*/).finished(),
269 (Eigen::Matrix<double, 4, 1>() << /*[[*/ 12 /*]*/,
270 /*[*/ 12 /*]*/,
271 /*[*/ 12 /*]*/,
272 /*[*/ 12 /*]]*/).finished()),
Brian Silverman2c590c32013-11-04 18:08:54 -0800273 loop_(new StateFeedbackLoop<2, 2, 2>(
274 constants::GetValues().make_v_drivetrain_loop())),
Austin Schuh427b3702013-11-02 13:44:09 -0700275 ttrust_(1.1),
276 wheel_(0.0),
277 throttle_(0.0),
278 quickturn_(false),
279 stale_count_(0),
280 position_time_delta_(0.01),
281 left_gear_(LOW),
Brian Silvermande8fd552013-11-03 15:53:42 -0800282 right_gear_(LOW),
283 counter_(0) {
Austin Schuh2054f5f2013-10-27 14:54:10 -0700284
Austin Schuh427b3702013-11-02 13:44:09 -0700285 last_position_.Zero();
286 position_.Zero();
Austin Schuh2054f5f2013-10-27 14:54:10 -0700287 }
Austin Schuh427b3702013-11-02 13:44:09 -0700288 static bool IsInGear(Gear gear) { return gear == LOW || gear == HIGH; }
289
Austin Schuh78d55462014-02-23 01:39:30 -0800290 static double MotorSpeed(const constants::ShifterHallEffect &hall_effect,
291 double shifter_position, double velocity) {
Austin Schuh427b3702013-11-02 13:44:09 -0700292 // TODO(austin): G_high, G_low and kWheelRadius
Austin Schuh78d55462014-02-23 01:39:30 -0800293 const double avg_hall_effect =
294 (hall_effect.clear_high + hall_effect.clear_low) / 2.0;
295
296 if (shifter_position > avg_hall_effect) {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800297 return velocity / constants::GetValues().high_gear_ratio / kWheelRadius;
Austin Schuh427b3702013-11-02 13:44:09 -0700298 } else {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800299 return velocity / constants::GetValues().low_gear_ratio / kWheelRadius;
300 }
301 }
302
Austin Schuh78d55462014-02-23 01:39:30 -0800303 Gear ComputeGear(const constants::ShifterHallEffect &hall_effect,
304 double velocity, Gear current) {
305 const double low_omega = MotorSpeed(hall_effect, 0.0, ::std::abs(velocity));
306 const double high_omega =
307 MotorSpeed(hall_effect, 1.0, ::std::abs(velocity));
Brian Silverman1a6590d2013-11-04 14:46:46 -0800308
Brian Silverman1a6590d2013-11-04 14:46:46 -0800309 double high_torque = ((12.0 - high_omega / Kv) * Kt / kR);
310 double low_torque = ((12.0 - low_omega / Kv) * Kt / kR);
311 double high_power = high_torque * high_omega;
312 double low_power = low_torque * low_omega;
Brian Silverman55a930b2013-11-04 20:59:00 -0800313
314 // TODO(aschuh): Do this right!
315 if ((current == HIGH || high_power > low_power + 160) &&
316 ::std::abs(velocity) > 0.14) {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800317 return HIGH;
318 } else {
319 return LOW;
Austin Schuh427b3702013-11-02 13:44:09 -0700320 }
321 }
322
Austin Schuh2054f5f2013-10-27 14:54:10 -0700323 void SetGoal(double wheel, double throttle, bool quickturn, bool highgear) {
Brian Silverman4b36e2e2015-03-16 15:46:53 -0700324 const double kWheelNonLinearity = 0.5;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700325 // Apply a sin function that's scaled to make it feel better.
326 const double angular_range = M_PI_2 * kWheelNonLinearity;
Brian Silverman4b36e2e2015-03-16 15:46:53 -0700327
Austin Schuh2054f5f2013-10-27 14:54:10 -0700328 wheel_ = sin(angular_range * wheel) / sin(angular_range);
329 wheel_ = sin(angular_range * wheel_) / sin(angular_range);
Brian Silverman4b36e2e2015-03-16 15:46:53 -0700330 wheel_ *= 2.3;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700331 quickturn_ = quickturn;
Austin Schuh427b3702013-11-02 13:44:09 -0700332
Brian Silverman1a6590d2013-11-04 14:46:46 -0800333 static const double kThrottleDeadband = 0.05;
334 if (::std::abs(throttle) < kThrottleDeadband) {
335 throttle_ = 0;
336 } else {
337 throttle_ = copysign((::std::abs(throttle) - kThrottleDeadband) /
338 (1.0 - kThrottleDeadband), throttle);
339 }
340
Austin Schuh427b3702013-11-02 13:44:09 -0700341 // TODO(austin): Fix the upshift logic to include states.
Brian Silverman1a6590d2013-11-04 14:46:46 -0800342 Gear requested_gear;
Brian Silverman55a930b2013-11-04 20:59:00 -0800343 if (false) {
Austin Schuh78d55462014-02-23 01:39:30 -0800344 const auto &values = constants::GetValues();
Brian Silverman1a6590d2013-11-04 14:46:46 -0800345 const double current_left_velocity =
346 (position_.left_encoder - last_position_.left_encoder) /
347 position_time_delta_;
348 const double current_right_velocity =
349 (position_.right_encoder - last_position_.right_encoder) /
350 position_time_delta_;
351
Austin Schuh78d55462014-02-23 01:39:30 -0800352 Gear left_requested =
353 ComputeGear(values.left_drive, current_left_velocity, left_gear_);
354 Gear right_requested =
355 ComputeGear(values.right_drive, current_right_velocity, right_gear_);
Brian Silverman1a6590d2013-11-04 14:46:46 -0800356 requested_gear =
357 (left_requested == HIGH || right_requested == HIGH) ? HIGH : LOW;
358 } else {
359 requested_gear = highgear ? HIGH : LOW;
360 }
361
362 const Gear shift_up =
363 constants::GetValues().clutch_transmission ? HIGH : SHIFTING_UP;
364 const Gear shift_down =
365 constants::GetValues().clutch_transmission ? LOW : SHIFTING_DOWN;
Austin Schuh427b3702013-11-02 13:44:09 -0700366
367 if (left_gear_ != requested_gear) {
368 if (IsInGear(left_gear_)) {
369 if (requested_gear == HIGH) {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800370 left_gear_ = shift_up;
Austin Schuh427b3702013-11-02 13:44:09 -0700371 } else {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800372 left_gear_ = shift_down;
Austin Schuh427b3702013-11-02 13:44:09 -0700373 }
Brian Silverman55a930b2013-11-04 20:59:00 -0800374 } else {
375 if (requested_gear == HIGH && left_gear_ == SHIFTING_DOWN) {
376 left_gear_ = SHIFTING_UP;
377 } else if (requested_gear == LOW && left_gear_ == SHIFTING_UP) {
378 left_gear_ = SHIFTING_DOWN;
379 }
Austin Schuh427b3702013-11-02 13:44:09 -0700380 }
381 }
382 if (right_gear_ != requested_gear) {
383 if (IsInGear(right_gear_)) {
384 if (requested_gear == HIGH) {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800385 right_gear_ = shift_up;
Austin Schuh427b3702013-11-02 13:44:09 -0700386 } else {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800387 right_gear_ = shift_down;
Austin Schuh427b3702013-11-02 13:44:09 -0700388 }
Brian Silverman55a930b2013-11-04 20:59:00 -0800389 } else {
390 if (requested_gear == HIGH && right_gear_ == SHIFTING_DOWN) {
391 right_gear_ = SHIFTING_UP;
392 } else if (requested_gear == LOW && right_gear_ == SHIFTING_UP) {
393 right_gear_ = SHIFTING_DOWN;
394 }
Austin Schuh427b3702013-11-02 13:44:09 -0700395 }
Austin Schuh2054f5f2013-10-27 14:54:10 -0700396 }
397 }
Brian Silvermanada5f2c2015-02-01 02:41:14 -0500398 void SetPosition(const DrivetrainQueue::Position *position) {
Austin Schuh78d55462014-02-23 01:39:30 -0800399 const auto &values = constants::GetValues();
Austin Schuh427b3702013-11-02 13:44:09 -0700400 if (position == NULL) {
401 ++stale_count_;
402 } else {
403 last_position_ = position_;
404 position_ = *position;
405 position_time_delta_ = (stale_count_ + 1) * 0.01;
406 stale_count_ = 0;
407 }
408
Brian Silverman93335ae2015-01-26 20:43:39 -0500409#if HAVE_SHIFTERS
Austin Schuh427b3702013-11-02 13:44:09 -0700410 if (position) {
Brian Silverman61e41fd2014-02-16 19:08:50 -0800411 GearLogging gear_logging;
Austin Schuh427b3702013-11-02 13:44:09 -0700412 // Switch to the correct controller.
Austin Schuh78d55462014-02-23 01:39:30 -0800413 const double left_middle_shifter_position =
414 (values.left_drive.clear_high + values.left_drive.clear_low) / 2.0;
415 const double right_middle_shifter_position =
416 (values.right_drive.clear_high + values.right_drive.clear_low) / 2.0;
417
Daniel Pettifd0a51d2014-11-07 16:59:24 -0800418 if (position->left_shifter_position < left_middle_shifter_position ||
419 left_gear_ == LOW) {
Austin Schuh78d55462014-02-23 01:39:30 -0800420 if (position->right_shifter_position < right_middle_shifter_position ||
421 right_gear_ == LOW) {
Brian Silverman61e41fd2014-02-16 19:08:50 -0800422 gear_logging.left_loop_high = false;
423 gear_logging.right_loop_high = false;
424 loop_->set_controller_index(gear_logging.controller_index = 0);
Austin Schuh427b3702013-11-02 13:44:09 -0700425 } else {
Brian Silverman61e41fd2014-02-16 19:08:50 -0800426 gear_logging.left_loop_high = false;
427 gear_logging.right_loop_high = true;
428 loop_->set_controller_index(gear_logging.controller_index = 1);
Austin Schuh427b3702013-11-02 13:44:09 -0700429 }
430 } else {
Austin Schuh78d55462014-02-23 01:39:30 -0800431 if (position->right_shifter_position < right_middle_shifter_position ||
Daniel Pettifd0a51d2014-11-07 16:59:24 -0800432 right_gear_ == LOW) {
Brian Silverman61e41fd2014-02-16 19:08:50 -0800433 gear_logging.left_loop_high = true;
434 gear_logging.right_loop_high = false;
435 loop_->set_controller_index(gear_logging.controller_index = 2);
Austin Schuh427b3702013-11-02 13:44:09 -0700436 } else {
Brian Silverman61e41fd2014-02-16 19:08:50 -0800437 gear_logging.left_loop_high = true;
438 gear_logging.right_loop_high = true;
439 loop_->set_controller_index(gear_logging.controller_index = 3);
Austin Schuh427b3702013-11-02 13:44:09 -0700440 }
441 }
Brian Silverman61e41fd2014-02-16 19:08:50 -0800442
Austin Schuh427b3702013-11-02 13:44:09 -0700443 // TODO(austin): Constants.
Austin Schuh78d55462014-02-23 01:39:30 -0800444 if (position->left_shifter_position > values.left_drive.clear_high && left_gear_ == SHIFTING_UP) {
Austin Schuh427b3702013-11-02 13:44:09 -0700445 left_gear_ = HIGH;
446 }
Austin Schuh78d55462014-02-23 01:39:30 -0800447 if (position->left_shifter_position < values.left_drive.clear_low && left_gear_ == SHIFTING_DOWN) {
Austin Schuh427b3702013-11-02 13:44:09 -0700448 left_gear_ = LOW;
449 }
Austin Schuh78d55462014-02-23 01:39:30 -0800450 if (position->right_shifter_position > values.right_drive.clear_high && right_gear_ == SHIFTING_UP) {
Austin Schuh427b3702013-11-02 13:44:09 -0700451 right_gear_ = HIGH;
452 }
Austin Schuh78d55462014-02-23 01:39:30 -0800453 if (position->right_shifter_position < values.right_drive.clear_low && right_gear_ == SHIFTING_DOWN) {
Austin Schuh427b3702013-11-02 13:44:09 -0700454 right_gear_ = LOW;
455 }
Brian Silverman61e41fd2014-02-16 19:08:50 -0800456
457 gear_logging.left_state = left_gear_;
458 gear_logging.right_state = right_gear_;
459 LOG_STRUCT(DEBUG, "state", gear_logging);
Austin Schuh427b3702013-11-02 13:44:09 -0700460 }
Brian Silverman93335ae2015-01-26 20:43:39 -0500461#else
462 (void) values;
463#endif
Austin Schuh427b3702013-11-02 13:44:09 -0700464 }
465
Austin Schuh2054f5f2013-10-27 14:54:10 -0700466 double FilterVelocity(double throttle) {
467 const Eigen::Matrix<double, 2, 2> FF =
468 loop_->B().inverse() *
469 (Eigen::Matrix<double, 2, 2>::Identity() - loop_->A());
470
471 constexpr int kHighGearController = 3;
472 const Eigen::Matrix<double, 2, 2> FF_high =
Brian Silverman273d8a32014-05-10 22:19:09 -0700473 loop_->controller(kHighGearController).plant.B().inverse() *
Austin Schuh2054f5f2013-10-27 14:54:10 -0700474 (Eigen::Matrix<double, 2, 2>::Identity() -
Brian Silverman273d8a32014-05-10 22:19:09 -0700475 loop_->controller(kHighGearController).plant.A());
Austin Schuh2054f5f2013-10-27 14:54:10 -0700476
477 ::Eigen::Matrix<double, 1, 2> FF_sum = FF.colwise().sum();
478 int min_FF_sum_index;
479 const double min_FF_sum = FF_sum.minCoeff(&min_FF_sum_index);
480 const double min_K_sum = loop_->K().col(min_FF_sum_index).sum();
481 const double high_min_FF_sum = FF_high.col(0).sum();
482
483 const double adjusted_ff_voltage = ::aos::Clip(
484 throttle * 12.0 * min_FF_sum / high_min_FF_sum, -12.0, 12.0);
Brian Silverman4b36e2e2015-03-16 15:46:53 -0700485 return (adjusted_ff_voltage +
486 ttrust_ * min_K_sum * (loop_->X_hat(0, 0) + loop_->X_hat(1, 0)) /
487 2.0) /
488 (ttrust_ * min_K_sum + min_FF_sum);
Austin Schuh2054f5f2013-10-27 14:54:10 -0700489 }
490
Brian Silverman718b1d72013-10-28 16:22:45 -0700491 double MaxVelocity() {
492 const Eigen::Matrix<double, 2, 2> FF =
493 loop_->B().inverse() *
494 (Eigen::Matrix<double, 2, 2>::Identity() - loop_->A());
495
496 constexpr int kHighGearController = 3;
497 const Eigen::Matrix<double, 2, 2> FF_high =
Brian Silverman273d8a32014-05-10 22:19:09 -0700498 loop_->controller(kHighGearController).plant.B().inverse() *
Brian Silverman718b1d72013-10-28 16:22:45 -0700499 (Eigen::Matrix<double, 2, 2>::Identity() -
Brian Silverman273d8a32014-05-10 22:19:09 -0700500 loop_->controller(kHighGearController).plant.A());
Brian Silverman718b1d72013-10-28 16:22:45 -0700501
502 ::Eigen::Matrix<double, 1, 2> FF_sum = FF.colwise().sum();
503 int min_FF_sum_index;
504 const double min_FF_sum = FF_sum.minCoeff(&min_FF_sum_index);
505 //const double min_K_sum = loop_->K().col(min_FF_sum_index).sum();
506 const double high_min_FF_sum = FF_high.col(0).sum();
507
508 const double adjusted_ff_voltage = ::aos::Clip(
509 12.0 * min_FF_sum / high_min_FF_sum, -12.0, 12.0);
510 return adjusted_ff_voltage / min_FF_sum;
511 }
512
Austin Schuh2054f5f2013-10-27 14:54:10 -0700513 void Update() {
Austin Schuh78d55462014-02-23 01:39:30 -0800514 const auto &values = constants::GetValues();
Austin Schuh427b3702013-11-02 13:44:09 -0700515 // TODO(austin): Observer for the current velocity instead of difference
516 // calculations.
Brian Silvermande8fd552013-11-03 15:53:42 -0800517 ++counter_;
Brian Silverman93335ae2015-01-26 20:43:39 -0500518#if HAVE_SHIFTERS
Austin Schuh427b3702013-11-02 13:44:09 -0700519 const double current_left_velocity =
Brian Silvermande8fd552013-11-03 15:53:42 -0800520 (position_.left_encoder - last_position_.left_encoder) /
Austin Schuh427b3702013-11-02 13:44:09 -0700521 position_time_delta_;
522 const double current_right_velocity =
Brian Silvermande8fd552013-11-03 15:53:42 -0800523 (position_.right_encoder - last_position_.right_encoder) /
Austin Schuh427b3702013-11-02 13:44:09 -0700524 position_time_delta_;
525 const double left_motor_speed =
Austin Schuh78d55462014-02-23 01:39:30 -0800526 MotorSpeed(values.left_drive, position_.left_shifter_position,
527 current_left_velocity);
Austin Schuh427b3702013-11-02 13:44:09 -0700528 const double right_motor_speed =
Austin Schuh78d55462014-02-23 01:39:30 -0800529 MotorSpeed(values.right_drive, position_.right_shifter_position,
530 current_right_velocity);
Austin Schuh2054f5f2013-10-27 14:54:10 -0700531
Brian Silverman61e41fd2014-02-16 19:08:50 -0800532 {
533 CIMLogging logging;
534
535 // Reset the CIM model to the current conditions to be ready for when we
536 // shift.
537 if (IsInGear(left_gear_)) {
538 logging.left_in_gear = true;
Brian Silverman61e41fd2014-02-16 19:08:50 -0800539 } else {
540 logging.left_in_gear = false;
541 }
542 logging.left_motor_speed = left_motor_speed;
543 logging.left_velocity = current_left_velocity;
544 if (IsInGear(right_gear_)) {
545 logging.right_in_gear = true;
Brian Silverman61e41fd2014-02-16 19:08:50 -0800546 } else {
547 logging.right_in_gear = false;
548 }
549 logging.right_motor_speed = right_motor_speed;
550 logging.right_velocity = current_right_velocity;
551
552 LOG_STRUCT(DEBUG, "currently", logging);
Austin Schuh427b3702013-11-02 13:44:09 -0700553 }
Brian Silverman93335ae2015-01-26 20:43:39 -0500554#else
555 (void) values;
556#endif
Austin Schuh2054f5f2013-10-27 14:54:10 -0700557
Brian Silverman93335ae2015-01-26 20:43:39 -0500558#if HAVE_SHIFTERS
Austin Schuh427b3702013-11-02 13:44:09 -0700559 if (IsInGear(left_gear_) && IsInGear(right_gear_)) {
Brian Silverman93335ae2015-01-26 20:43:39 -0500560#else
561 {
562#endif
Austin Schuh427b3702013-11-02 13:44:09 -0700563 // FF * X = U (steady state)
564 const Eigen::Matrix<double, 2, 2> FF =
565 loop_->B().inverse() *
566 (Eigen::Matrix<double, 2, 2>::Identity() - loop_->A());
567
568 // Invert the plant to figure out how the velocity filter would have to
569 // work
570 // out in order to filter out the forwards negative inertia.
571 // This math assumes that the left and right power and velocity are
572 // equals,
573 // and that the plant is the same on the left and right.
574 const double fvel = FilterVelocity(throttle_);
575
576 const double sign_svel = wheel_ * ((fvel > 0.0) ? 1.0 : -1.0);
577 double steering_velocity;
578 if (quickturn_) {
579 steering_velocity = wheel_ * MaxVelocity();
580 } else {
581 steering_velocity = ::std::abs(fvel) * wheel_;
582 }
583 const double left_velocity = fvel - steering_velocity;
584 const double right_velocity = fvel + steering_velocity;
Brian Silverman4b36e2e2015-03-16 15:46:53 -0700585 LOG(DEBUG, "l=%f r=%f\n", left_velocity, right_velocity);
Austin Schuh427b3702013-11-02 13:44:09 -0700586
587 // Integrate velocity to get the position.
588 // This position is used to get integral control.
Brian Silverman0ca790b2014-06-12 21:33:08 -0700589 loop_->mutable_R() << left_velocity, right_velocity;
Austin Schuh427b3702013-11-02 13:44:09 -0700590
591 if (!quickturn_) {
592 // K * R = w
593 Eigen::Matrix<double, 1, 2> equality_k;
594 equality_k << 1 + sign_svel, -(1 - sign_svel);
595 const double equality_w = 0.0;
596
597 // Construct a constraint on R by manipulating the constraint on U
598 ::aos::controls::HPolytope<2> R_poly = ::aos::controls::HPolytope<2>(
599 U_Poly_.H() * (loop_->K() + FF),
Brian Silverman273d8a32014-05-10 22:19:09 -0700600 U_Poly_.k() + U_Poly_.H() * loop_->K() * loop_->X_hat());
Austin Schuh427b3702013-11-02 13:44:09 -0700601
602 // Limit R back inside the box.
Brian Silverman0ca790b2014-06-12 21:33:08 -0700603 loop_->mutable_R() =
Brian Silverman273d8a32014-05-10 22:19:09 -0700604 CoerceGoal(R_poly, equality_k, equality_w, loop_->R());
Austin Schuh427b3702013-11-02 13:44:09 -0700605 }
606
Brian Silverman273d8a32014-05-10 22:19:09 -0700607 const Eigen::Matrix<double, 2, 1> FF_volts = FF * loop_->R();
Austin Schuh427b3702013-11-02 13:44:09 -0700608 const Eigen::Matrix<double, 2, 1> U_ideal =
Brian Silverman273d8a32014-05-10 22:19:09 -0700609 loop_->K() * (loop_->R() - loop_->X_hat()) + FF_volts;
Austin Schuh427b3702013-11-02 13:44:09 -0700610
611 for (int i = 0; i < 2; i++) {
Brian Silverman0ca790b2014-06-12 21:33:08 -0700612 loop_->mutable_U()[i] = ::aos::Clip(U_ideal[i], -12, 12);
Austin Schuh427b3702013-11-02 13:44:09 -0700613 }
Brian Silvermande8fd552013-11-03 15:53:42 -0800614
615 // TODO(austin): Model this better.
616 // TODO(austin): Feed back?
Brian Silverman0ca790b2014-06-12 21:33:08 -0700617 loop_->mutable_X_hat() =
Brian Silverman273d8a32014-05-10 22:19:09 -0700618 loop_->A() * loop_->X_hat() + loop_->B() * loop_->U();
Brian Silverman93335ae2015-01-26 20:43:39 -0500619#if HAVE_SHIFTERS
Brian Silverman718b1d72013-10-28 16:22:45 -0700620 } else {
Austin Schuh427b3702013-11-02 13:44:09 -0700621 // Any motor is not in gear. Speed match.
622 ::Eigen::Matrix<double, 1, 1> R_left;
Austin Schuh427b3702013-11-02 13:44:09 -0700623 ::Eigen::Matrix<double, 1, 1> R_right;
Austin Schuhb5afb692014-03-02 11:54:11 -0800624 R_left(0, 0) = left_motor_speed;
Austin Schuh427b3702013-11-02 13:44:09 -0700625 R_right(0, 0) = right_motor_speed;
Austin Schuhb5afb692014-03-02 11:54:11 -0800626
627 const double wiggle =
Brian Silvermanf970f2c2014-03-22 19:34:30 -0700628 (static_cast<double>((counter_ % 20) / 10) - 0.5) * 5.0;
Austin Schuhb5afb692014-03-02 11:54:11 -0800629
Brian Silvermana21c3a22014-06-12 21:49:15 -0700630 loop_->mutable_U(0, 0) = ::aos::Clip(
Brian Silverman56658322014-03-22 16:57:22 -0700631 (R_left / Kv)(0, 0) + (IsInGear(left_gear_) ? 0 : wiggle),
632 -12.0, 12.0);
Brian Silvermana21c3a22014-06-12 21:49:15 -0700633 loop_->mutable_U(1, 0) = ::aos::Clip(
Brian Silverman56658322014-03-22 16:57:22 -0700634 (R_right / Kv)(0, 0) + (IsInGear(right_gear_) ? 0 : wiggle),
635 -12.0, 12.0);
Brian Silverman699f0cb2015-02-05 19:45:01 -0500636 loop_->mutable_U() *= 12.0 / ::aos::robot_state->voltage_battery;
Brian Silverman93335ae2015-01-26 20:43:39 -0500637#endif
Austin Schuh2054f5f2013-10-27 14:54:10 -0700638 }
Austin Schuh2054f5f2013-10-27 14:54:10 -0700639 }
640
Brian Silvermanada5f2c2015-02-01 02:41:14 -0500641 void SendMotors(DrivetrainQueue::Output *output) {
Brian Silvermande8fd552013-11-03 15:53:42 -0800642 if (output != NULL) {
643 output->left_voltage = loop_->U(0, 0);
644 output->right_voltage = loop_->U(1, 0);
Brian Silverman61e41fd2014-02-16 19:08:50 -0800645 output->left_high = left_gear_ == HIGH || left_gear_ == SHIFTING_UP;
646 output->right_high = right_gear_ == HIGH || right_gear_ == SHIFTING_UP;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700647 }
648 }
649
650 private:
651 const ::aos::controls::HPolytope<2> U_Poly_;
652
653 ::std::unique_ptr<StateFeedbackLoop<2, 2, 2>> loop_;
654
Austin Schuh427b3702013-11-02 13:44:09 -0700655 const double ttrust_;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700656 double wheel_;
657 double throttle_;
658 bool quickturn_;
Austin Schuh427b3702013-11-02 13:44:09 -0700659 int stale_count_;
660 double position_time_delta_;
661 Gear left_gear_;
662 Gear right_gear_;
Brian Silvermanada5f2c2015-02-01 02:41:14 -0500663 DrivetrainQueue::Position last_position_;
664 DrivetrainQueue::Position position_;
Brian Silvermande8fd552013-11-03 15:53:42 -0800665 int counter_;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700666};
Brian Silvermancec3c8d2013-12-20 21:04:55 -0800667constexpr double PolyDrivetrain::kStallTorque;
668constexpr double PolyDrivetrain::kStallCurrent;
669constexpr double PolyDrivetrain::kFreeSpeed;
670constexpr double PolyDrivetrain::kFreeCurrent;
671constexpr double PolyDrivetrain::J;
672constexpr double PolyDrivetrain::m;
673constexpr double PolyDrivetrain::rb;
674constexpr double PolyDrivetrain::kWheelRadius;
675constexpr double PolyDrivetrain::kR;
676constexpr double PolyDrivetrain::Kv;
677constexpr double PolyDrivetrain::Kt;
678
Austin Schuh2054f5f2013-10-27 14:54:10 -0700679
Brian Silvermanada5f2c2015-02-01 02:41:14 -0500680void DrivetrainLoop::RunIteration(const DrivetrainQueue::Goal *goal,
681 const DrivetrainQueue::Position *position,
682 DrivetrainQueue::Output *output,
683 DrivetrainQueue::Status * status) {
brians343bc112013-02-10 01:53:46 +0000684 // TODO(aschuh): These should be members of the class.
685 static DrivetrainMotorsSS dt_closedloop;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700686 static PolyDrivetrain dt_openloop;
brians343bc112013-02-10 01:53:46 +0000687
688 bool bad_pos = false;
Austin Schuh427b3702013-11-02 13:44:09 -0700689 if (position == nullptr) {
Brian Silverman50a9d032014-02-16 17:20:57 -0800690 LOG_INTERVAL(no_position_);
brians343bc112013-02-10 01:53:46 +0000691 bad_pos = true;
692 }
Brian Silverman9c9ab422014-02-25 13:42:53 -0800693 no_position_.Print();
brians343bc112013-02-10 01:53:46 +0000694
Brian Silverman089f5812015-02-15 01:58:19 -0500695 bool control_loop_driving = false;
696 if (goal) {
697 double wheel = goal->steering;
698 double throttle = goal->throttle;
699 bool quickturn = goal->quickturn;
Brian Silverman93335ae2015-01-26 20:43:39 -0500700#if HAVE_SHIFTERS
Brian Silverman089f5812015-02-15 01:58:19 -0500701 bool highgear = goal->highgear;
Brian Silverman93335ae2015-01-26 20:43:39 -0500702#endif
brians343bc112013-02-10 01:53:46 +0000703
Brian Silverman089f5812015-02-15 01:58:19 -0500704 control_loop_driving = goal->control_loop_driving;
705 double left_goal = goal->left_goal;
706 double right_goal = goal->right_goal;
brians343bc112013-02-10 01:53:46 +0000707
Brian Silverman089f5812015-02-15 01:58:19 -0500708 dt_closedloop.SetGoal(left_goal, goal->left_velocity_goal, right_goal,
709 goal->right_velocity_goal);
710#if HAVE_SHIFTERS
711 dt_openloop.SetGoal(wheel, throttle, quickturn, highgear);
712#else
713 dt_openloop.SetGoal(wheel, throttle, quickturn, false);
714#endif
715 }
716
brians343bc112013-02-10 01:53:46 +0000717 if (!bad_pos) {
718 const double left_encoder = position->left_encoder;
719 const double right_encoder = position->right_encoder;
Brian Silverman6bf0d3c2014-03-08 12:52:54 -0800720 if (gyro_reading.FetchLatest()) {
Brian Silverman74e5b4e2014-03-09 16:58:58 -0700721 LOG_STRUCT(DEBUG, "using", *gyro_reading.get());
722 dt_closedloop.SetPosition(left_encoder, right_encoder,
Brian Silvermanfaac3a22014-04-30 17:54:30 -0700723 gyro_reading->angle);
brians343bc112013-02-10 01:53:46 +0000724 } else {
725 dt_closedloop.SetRawPosition(left_encoder, right_encoder);
726 }
727 }
Austin Schuh427b3702013-11-02 13:44:09 -0700728 dt_openloop.SetPosition(position);
brians343bc112013-02-10 01:53:46 +0000729 dt_openloop.Update();
Ben Fredrickson890c3fe2014-03-02 00:15:16 +0000730
Brian Silverman2845c4c2013-03-16 19:54:08 -0700731 if (control_loop_driving) {
Brian Silvermanfaac3a22014-04-30 17:54:30 -0700732 dt_closedloop.Update(output == NULL, true);
Brian Silverman2845c4c2013-03-16 19:54:08 -0700733 dt_closedloop.SendMotors(output);
734 } else {
735 dt_openloop.SendMotors(output);
Austin Schuhb5afb692014-03-02 11:54:11 -0800736 if (output) {
737 dt_closedloop.SetExternalMotors(output->left_voltage,
738 output->right_voltage);
739 }
Brian Silvermanfaac3a22014-04-30 17:54:30 -0700740 dt_closedloop.Update(output == NULL, false);
brians343bc112013-02-10 01:53:46 +0000741 }
Brian Silverman089f5812015-02-15 01:58:19 -0500742
Brian Silverman2c764f02014-04-25 09:21:21 -0500743 // set the output status of the control loop state
Ben Fredrickson890c3fe2014-03-02 00:15:16 +0000744 if (status) {
745 bool done = false;
746 if (goal) {
747 done = ((::std::abs(goal->left_goal -
748 dt_closedloop.GetEstimatedLeftEncoder()) <
749 constants::GetValues().drivetrain_done_distance) &&
750 (::std::abs(goal->right_goal -
751 dt_closedloop.GetEstimatedRightEncoder()) <
752 constants::GetValues().drivetrain_done_distance));
753 }
754 status->is_done = done;
755 status->robot_speed = dt_closedloop.GetEstimatedRobotSpeed();
Austin Schuh577edf62014-04-13 10:33:05 -0700756 status->filtered_left_position = dt_closedloop.GetEstimatedLeftEncoder();
757 status->filtered_right_position = dt_closedloop.GetEstimatedRightEncoder();
Austin Schuh913c2f22015-04-18 22:57:19 -0700758
759 status->filtered_left_velocity = dt_closedloop.loop().X_hat(1, 0);
760 status->filtered_right_velocity = dt_closedloop.loop().X_hat(3, 0);
Brian Silvermanb94069c2014-04-17 14:34:24 -0700761 status->output_was_capped = dt_closedloop.OutputWasCapped();
762 status->uncapped_left_voltage = dt_closedloop.loop().U_uncapped(0, 0);
763 status->uncapped_right_voltage = dt_closedloop.loop().U_uncapped(1, 0);
brians343bc112013-02-10 01:53:46 +0000764 }
765}
766
767} // namespace control_loops
768} // namespace frc971