blob: b348bf869db8e98b6093f6444e1207199820ae81 [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
Brian Silverman4b36e2e2015-03-16 15:46:53 -0700244 static constexpr double kStallCurrent = 133.0;
Austin Schuh427b3702013-11-02 13:44:09 -0700245 // 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.
Brian Silverman4b36e2e2015-03-16 15:46:53 -0700251 static constexpr double J = 10;
Austin Schuh427b3702013-11-02 13:44:09 -0700252 // Mass of the robot, in kg.
253 static constexpr double m = 68;
254 // Radius of the robot, in meters (from last year).
Brian Silverman4b36e2e2015-03-16 15:46:53 -0700255 static constexpr double rb = 0.9603 / 2.0;
256 static constexpr double kWheelRadius = 0.0515938;
Austin Schuh427b3702013-11-02 13:44:09 -0700257 // Resistance of the motor, divided by the number of motors.
Brian Silverman4b36e2e2015-03-16 15:46:53 -0700258 static constexpr double kR = (12.0 / kStallCurrent / 2 + 0.03) / (0.93 * 0.93);
Austin Schuh427b3702013-11-02 13:44:09 -0700259 // 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 Silverman4b36e2e2015-03-16 15:46:53 -0700325 const double kWheelNonLinearity = 0.5;
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;
Brian Silverman4b36e2e2015-03-16 15:46:53 -0700328
Austin Schuh2054f5f2013-10-27 14:54:10 -0700329 wheel_ = sin(angular_range * wheel) / sin(angular_range);
330 wheel_ = sin(angular_range * wheel_) / sin(angular_range);
Brian Silverman4b36e2e2015-03-16 15:46:53 -0700331 wheel_ *= 2.3;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700332 quickturn_ = quickturn;
Austin Schuh427b3702013-11-02 13:44:09 -0700333
Brian Silverman1a6590d2013-11-04 14:46:46 -0800334 static const double kThrottleDeadband = 0.05;
335 if (::std::abs(throttle) < kThrottleDeadband) {
336 throttle_ = 0;
337 } else {
338 throttle_ = copysign((::std::abs(throttle) - kThrottleDeadband) /
339 (1.0 - kThrottleDeadband), throttle);
340 }
341
Austin Schuh427b3702013-11-02 13:44:09 -0700342 // TODO(austin): Fix the upshift logic to include states.
Brian Silverman1a6590d2013-11-04 14:46:46 -0800343 Gear requested_gear;
Brian Silverman55a930b2013-11-04 20:59:00 -0800344 if (false) {
Austin Schuh78d55462014-02-23 01:39:30 -0800345 const auto &values = constants::GetValues();
Brian Silverman1a6590d2013-11-04 14:46:46 -0800346 const double current_left_velocity =
347 (position_.left_encoder - last_position_.left_encoder) /
348 position_time_delta_;
349 const double current_right_velocity =
350 (position_.right_encoder - last_position_.right_encoder) /
351 position_time_delta_;
352
Austin Schuh78d55462014-02-23 01:39:30 -0800353 Gear left_requested =
354 ComputeGear(values.left_drive, current_left_velocity, left_gear_);
355 Gear right_requested =
356 ComputeGear(values.right_drive, current_right_velocity, right_gear_);
Brian Silverman1a6590d2013-11-04 14:46:46 -0800357 requested_gear =
358 (left_requested == HIGH || right_requested == HIGH) ? HIGH : LOW;
359 } else {
360 requested_gear = highgear ? HIGH : LOW;
361 }
362
363 const Gear shift_up =
364 constants::GetValues().clutch_transmission ? HIGH : SHIFTING_UP;
365 const Gear shift_down =
366 constants::GetValues().clutch_transmission ? LOW : SHIFTING_DOWN;
Austin Schuh427b3702013-11-02 13:44:09 -0700367
368 if (left_gear_ != requested_gear) {
369 if (IsInGear(left_gear_)) {
370 if (requested_gear == HIGH) {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800371 left_gear_ = shift_up;
Austin Schuh427b3702013-11-02 13:44:09 -0700372 } else {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800373 left_gear_ = shift_down;
Austin Schuh427b3702013-11-02 13:44:09 -0700374 }
Brian Silverman55a930b2013-11-04 20:59:00 -0800375 } else {
376 if (requested_gear == HIGH && left_gear_ == SHIFTING_DOWN) {
377 left_gear_ = SHIFTING_UP;
378 } else if (requested_gear == LOW && left_gear_ == SHIFTING_UP) {
379 left_gear_ = SHIFTING_DOWN;
380 }
Austin Schuh427b3702013-11-02 13:44:09 -0700381 }
382 }
383 if (right_gear_ != requested_gear) {
384 if (IsInGear(right_gear_)) {
385 if (requested_gear == HIGH) {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800386 right_gear_ = shift_up;
Austin Schuh427b3702013-11-02 13:44:09 -0700387 } else {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800388 right_gear_ = shift_down;
Austin Schuh427b3702013-11-02 13:44:09 -0700389 }
Brian Silverman55a930b2013-11-04 20:59:00 -0800390 } else {
391 if (requested_gear == HIGH && right_gear_ == SHIFTING_DOWN) {
392 right_gear_ = SHIFTING_UP;
393 } else if (requested_gear == LOW && right_gear_ == SHIFTING_UP) {
394 right_gear_ = SHIFTING_DOWN;
395 }
Austin Schuh427b3702013-11-02 13:44:09 -0700396 }
Austin Schuh2054f5f2013-10-27 14:54:10 -0700397 }
398 }
Brian Silvermanada5f2c2015-02-01 02:41:14 -0500399 void SetPosition(const DrivetrainQueue::Position *position) {
Austin Schuh78d55462014-02-23 01:39:30 -0800400 const auto &values = constants::GetValues();
Austin Schuh427b3702013-11-02 13:44:09 -0700401 if (position == NULL) {
402 ++stale_count_;
403 } else {
404 last_position_ = position_;
405 position_ = *position;
406 position_time_delta_ = (stale_count_ + 1) * 0.01;
407 stale_count_ = 0;
408 }
409
Brian Silverman93335ae2015-01-26 20:43:39 -0500410#if HAVE_SHIFTERS
Austin Schuh427b3702013-11-02 13:44:09 -0700411 if (position) {
Brian Silverman61e41fd2014-02-16 19:08:50 -0800412 GearLogging gear_logging;
Austin Schuh427b3702013-11-02 13:44:09 -0700413 // Switch to the correct controller.
Austin Schuh78d55462014-02-23 01:39:30 -0800414 const double left_middle_shifter_position =
415 (values.left_drive.clear_high + values.left_drive.clear_low) / 2.0;
416 const double right_middle_shifter_position =
417 (values.right_drive.clear_high + values.right_drive.clear_low) / 2.0;
418
Daniel Pettifd0a51d2014-11-07 16:59:24 -0800419 if (position->left_shifter_position < left_middle_shifter_position ||
420 left_gear_ == LOW) {
Austin Schuh78d55462014-02-23 01:39:30 -0800421 if (position->right_shifter_position < right_middle_shifter_position ||
422 right_gear_ == LOW) {
Brian Silverman61e41fd2014-02-16 19:08:50 -0800423 gear_logging.left_loop_high = false;
424 gear_logging.right_loop_high = false;
425 loop_->set_controller_index(gear_logging.controller_index = 0);
Austin Schuh427b3702013-11-02 13:44:09 -0700426 } else {
Brian Silverman61e41fd2014-02-16 19:08:50 -0800427 gear_logging.left_loop_high = false;
428 gear_logging.right_loop_high = true;
429 loop_->set_controller_index(gear_logging.controller_index = 1);
Austin Schuh427b3702013-11-02 13:44:09 -0700430 }
431 } else {
Austin Schuh78d55462014-02-23 01:39:30 -0800432 if (position->right_shifter_position < right_middle_shifter_position ||
Daniel Pettifd0a51d2014-11-07 16:59:24 -0800433 right_gear_ == LOW) {
Brian Silverman61e41fd2014-02-16 19:08:50 -0800434 gear_logging.left_loop_high = true;
435 gear_logging.right_loop_high = false;
436 loop_->set_controller_index(gear_logging.controller_index = 2);
Austin Schuh427b3702013-11-02 13:44:09 -0700437 } else {
Brian Silverman61e41fd2014-02-16 19:08:50 -0800438 gear_logging.left_loop_high = true;
439 gear_logging.right_loop_high = true;
440 loop_->set_controller_index(gear_logging.controller_index = 3);
Austin Schuh427b3702013-11-02 13:44:09 -0700441 }
442 }
Brian Silverman61e41fd2014-02-16 19:08:50 -0800443
Austin Schuh427b3702013-11-02 13:44:09 -0700444 // TODO(austin): Constants.
Austin Schuh78d55462014-02-23 01:39:30 -0800445 if (position->left_shifter_position > values.left_drive.clear_high && left_gear_ == SHIFTING_UP) {
Austin Schuh427b3702013-11-02 13:44:09 -0700446 left_gear_ = HIGH;
447 }
Austin Schuh78d55462014-02-23 01:39:30 -0800448 if (position->left_shifter_position < values.left_drive.clear_low && left_gear_ == SHIFTING_DOWN) {
Austin Schuh427b3702013-11-02 13:44:09 -0700449 left_gear_ = LOW;
450 }
Austin Schuh78d55462014-02-23 01:39:30 -0800451 if (position->right_shifter_position > values.right_drive.clear_high && right_gear_ == SHIFTING_UP) {
Austin Schuh427b3702013-11-02 13:44:09 -0700452 right_gear_ = HIGH;
453 }
Austin Schuh78d55462014-02-23 01:39:30 -0800454 if (position->right_shifter_position < values.right_drive.clear_low && right_gear_ == SHIFTING_DOWN) {
Austin Schuh427b3702013-11-02 13:44:09 -0700455 right_gear_ = LOW;
456 }
Brian Silverman61e41fd2014-02-16 19:08:50 -0800457
458 gear_logging.left_state = left_gear_;
459 gear_logging.right_state = right_gear_;
460 LOG_STRUCT(DEBUG, "state", gear_logging);
Austin Schuh427b3702013-11-02 13:44:09 -0700461 }
Brian Silverman93335ae2015-01-26 20:43:39 -0500462#else
463 (void) values;
464#endif
Austin Schuh427b3702013-11-02 13:44:09 -0700465 }
466
Austin Schuh2054f5f2013-10-27 14:54:10 -0700467 double FilterVelocity(double throttle) {
468 const Eigen::Matrix<double, 2, 2> FF =
469 loop_->B().inverse() *
470 (Eigen::Matrix<double, 2, 2>::Identity() - loop_->A());
471
472 constexpr int kHighGearController = 3;
473 const Eigen::Matrix<double, 2, 2> FF_high =
Brian Silverman273d8a32014-05-10 22:19:09 -0700474 loop_->controller(kHighGearController).plant.B().inverse() *
Austin Schuh2054f5f2013-10-27 14:54:10 -0700475 (Eigen::Matrix<double, 2, 2>::Identity() -
Brian Silverman273d8a32014-05-10 22:19:09 -0700476 loop_->controller(kHighGearController).plant.A());
Austin Schuh2054f5f2013-10-27 14:54:10 -0700477
478 ::Eigen::Matrix<double, 1, 2> FF_sum = FF.colwise().sum();
479 int min_FF_sum_index;
480 const double min_FF_sum = FF_sum.minCoeff(&min_FF_sum_index);
481 const double min_K_sum = loop_->K().col(min_FF_sum_index).sum();
482 const double high_min_FF_sum = FF_high.col(0).sum();
483
484 const double adjusted_ff_voltage = ::aos::Clip(
485 throttle * 12.0 * min_FF_sum / high_min_FF_sum, -12.0, 12.0);
Brian Silverman4b36e2e2015-03-16 15:46:53 -0700486 return (adjusted_ff_voltage +
487 ttrust_ * min_K_sum * (loop_->X_hat(0, 0) + loop_->X_hat(1, 0)) /
488 2.0) /
489 (ttrust_ * min_K_sum + min_FF_sum);
Austin Schuh2054f5f2013-10-27 14:54:10 -0700490 }
491
Brian Silverman718b1d72013-10-28 16:22:45 -0700492 double MaxVelocity() {
493 const Eigen::Matrix<double, 2, 2> FF =
494 loop_->B().inverse() *
495 (Eigen::Matrix<double, 2, 2>::Identity() - loop_->A());
496
497 constexpr int kHighGearController = 3;
498 const Eigen::Matrix<double, 2, 2> FF_high =
Brian Silverman273d8a32014-05-10 22:19:09 -0700499 loop_->controller(kHighGearController).plant.B().inverse() *
Brian Silverman718b1d72013-10-28 16:22:45 -0700500 (Eigen::Matrix<double, 2, 2>::Identity() -
Brian Silverman273d8a32014-05-10 22:19:09 -0700501 loop_->controller(kHighGearController).plant.A());
Brian Silverman718b1d72013-10-28 16:22:45 -0700502
503 ::Eigen::Matrix<double, 1, 2> FF_sum = FF.colwise().sum();
504 int min_FF_sum_index;
505 const double min_FF_sum = FF_sum.minCoeff(&min_FF_sum_index);
506 //const double min_K_sum = loop_->K().col(min_FF_sum_index).sum();
507 const double high_min_FF_sum = FF_high.col(0).sum();
508
509 const double adjusted_ff_voltage = ::aos::Clip(
510 12.0 * min_FF_sum / high_min_FF_sum, -12.0, 12.0);
511 return adjusted_ff_voltage / min_FF_sum;
512 }
513
Austin Schuh2054f5f2013-10-27 14:54:10 -0700514 void Update() {
Austin Schuh78d55462014-02-23 01:39:30 -0800515 const auto &values = constants::GetValues();
Austin Schuh427b3702013-11-02 13:44:09 -0700516 // TODO(austin): Observer for the current velocity instead of difference
517 // calculations.
Brian Silvermande8fd552013-11-03 15:53:42 -0800518 ++counter_;
Brian Silverman93335ae2015-01-26 20:43:39 -0500519#if HAVE_SHIFTERS
Austin Schuh427b3702013-11-02 13:44:09 -0700520 const double current_left_velocity =
Brian Silvermande8fd552013-11-03 15:53:42 -0800521 (position_.left_encoder - last_position_.left_encoder) /
Austin Schuh427b3702013-11-02 13:44:09 -0700522 position_time_delta_;
523 const double current_right_velocity =
Brian Silvermande8fd552013-11-03 15:53:42 -0800524 (position_.right_encoder - last_position_.right_encoder) /
Austin Schuh427b3702013-11-02 13:44:09 -0700525 position_time_delta_;
526 const double left_motor_speed =
Austin Schuh78d55462014-02-23 01:39:30 -0800527 MotorSpeed(values.left_drive, position_.left_shifter_position,
528 current_left_velocity);
Austin Schuh427b3702013-11-02 13:44:09 -0700529 const double right_motor_speed =
Austin Schuh78d55462014-02-23 01:39:30 -0800530 MotorSpeed(values.right_drive, position_.right_shifter_position,
531 current_right_velocity);
Austin Schuh2054f5f2013-10-27 14:54:10 -0700532
Brian Silverman61e41fd2014-02-16 19:08:50 -0800533 {
534 CIMLogging logging;
535
536 // Reset the CIM model to the current conditions to be ready for when we
537 // shift.
538 if (IsInGear(left_gear_)) {
539 logging.left_in_gear = true;
Brian Silverman61e41fd2014-02-16 19:08:50 -0800540 } else {
541 logging.left_in_gear = false;
542 }
543 logging.left_motor_speed = left_motor_speed;
544 logging.left_velocity = current_left_velocity;
545 if (IsInGear(right_gear_)) {
546 logging.right_in_gear = true;
Brian Silverman61e41fd2014-02-16 19:08:50 -0800547 } else {
548 logging.right_in_gear = false;
549 }
550 logging.right_motor_speed = right_motor_speed;
551 logging.right_velocity = current_right_velocity;
552
553 LOG_STRUCT(DEBUG, "currently", logging);
Austin Schuh427b3702013-11-02 13:44:09 -0700554 }
Brian Silverman93335ae2015-01-26 20:43:39 -0500555#else
556 (void) values;
557#endif
Austin Schuh2054f5f2013-10-27 14:54:10 -0700558
Brian Silverman93335ae2015-01-26 20:43:39 -0500559#if HAVE_SHIFTERS
Austin Schuh427b3702013-11-02 13:44:09 -0700560 if (IsInGear(left_gear_) && IsInGear(right_gear_)) {
Brian Silverman93335ae2015-01-26 20:43:39 -0500561#else
562 {
563#endif
Austin Schuh427b3702013-11-02 13:44:09 -0700564 // FF * X = U (steady state)
565 const Eigen::Matrix<double, 2, 2> FF =
566 loop_->B().inverse() *
567 (Eigen::Matrix<double, 2, 2>::Identity() - loop_->A());
568
569 // Invert the plant to figure out how the velocity filter would have to
570 // work
571 // out in order to filter out the forwards negative inertia.
572 // This math assumes that the left and right power and velocity are
573 // equals,
574 // and that the plant is the same on the left and right.
575 const double fvel = FilterVelocity(throttle_);
576
577 const double sign_svel = wheel_ * ((fvel > 0.0) ? 1.0 : -1.0);
578 double steering_velocity;
579 if (quickturn_) {
580 steering_velocity = wheel_ * MaxVelocity();
581 } else {
582 steering_velocity = ::std::abs(fvel) * wheel_;
583 }
584 const double left_velocity = fvel - steering_velocity;
585 const double right_velocity = fvel + steering_velocity;
Brian Silverman4b36e2e2015-03-16 15:46:53 -0700586 LOG(DEBUG, "l=%f r=%f\n", left_velocity, right_velocity);
Austin Schuh427b3702013-11-02 13:44:09 -0700587
588 // Integrate velocity to get the position.
589 // This position is used to get integral control.
Brian Silverman0ca790b2014-06-12 21:33:08 -0700590 loop_->mutable_R() << left_velocity, right_velocity;
Austin Schuh427b3702013-11-02 13:44:09 -0700591
592 if (!quickturn_) {
593 // K * R = w
594 Eigen::Matrix<double, 1, 2> equality_k;
595 equality_k << 1 + sign_svel, -(1 - sign_svel);
596 const double equality_w = 0.0;
597
598 // Construct a constraint on R by manipulating the constraint on U
599 ::aos::controls::HPolytope<2> R_poly = ::aos::controls::HPolytope<2>(
600 U_Poly_.H() * (loop_->K() + FF),
Brian Silverman273d8a32014-05-10 22:19:09 -0700601 U_Poly_.k() + U_Poly_.H() * loop_->K() * loop_->X_hat());
Austin Schuh427b3702013-11-02 13:44:09 -0700602
603 // Limit R back inside the box.
Brian Silverman0ca790b2014-06-12 21:33:08 -0700604 loop_->mutable_R() =
Brian Silverman273d8a32014-05-10 22:19:09 -0700605 CoerceGoal(R_poly, equality_k, equality_w, loop_->R());
Austin Schuh427b3702013-11-02 13:44:09 -0700606 }
607
Brian Silverman273d8a32014-05-10 22:19:09 -0700608 const Eigen::Matrix<double, 2, 1> FF_volts = FF * loop_->R();
Austin Schuh427b3702013-11-02 13:44:09 -0700609 const Eigen::Matrix<double, 2, 1> U_ideal =
Brian Silverman273d8a32014-05-10 22:19:09 -0700610 loop_->K() * (loop_->R() - loop_->X_hat()) + FF_volts;
Austin Schuh427b3702013-11-02 13:44:09 -0700611
612 for (int i = 0; i < 2; i++) {
Brian Silverman0ca790b2014-06-12 21:33:08 -0700613 loop_->mutable_U()[i] = ::aos::Clip(U_ideal[i], -12, 12);
Austin Schuh427b3702013-11-02 13:44:09 -0700614 }
Brian Silvermande8fd552013-11-03 15:53:42 -0800615
616 // TODO(austin): Model this better.
617 // TODO(austin): Feed back?
Brian Silverman0ca790b2014-06-12 21:33:08 -0700618 loop_->mutable_X_hat() =
Brian Silverman273d8a32014-05-10 22:19:09 -0700619 loop_->A() * loop_->X_hat() + loop_->B() * loop_->U();
Brian Silverman93335ae2015-01-26 20:43:39 -0500620#if HAVE_SHIFTERS
Brian Silverman718b1d72013-10-28 16:22:45 -0700621 } else {
Austin Schuh427b3702013-11-02 13:44:09 -0700622 // Any motor is not in gear. Speed match.
623 ::Eigen::Matrix<double, 1, 1> R_left;
Austin Schuh427b3702013-11-02 13:44:09 -0700624 ::Eigen::Matrix<double, 1, 1> R_right;
Austin Schuhb5afb692014-03-02 11:54:11 -0800625 R_left(0, 0) = left_motor_speed;
Austin Schuh427b3702013-11-02 13:44:09 -0700626 R_right(0, 0) = right_motor_speed;
Austin Schuhb5afb692014-03-02 11:54:11 -0800627
628 const double wiggle =
Brian Silvermanf970f2c2014-03-22 19:34:30 -0700629 (static_cast<double>((counter_ % 20) / 10) - 0.5) * 5.0;
Austin Schuhb5afb692014-03-02 11:54:11 -0800630
Brian Silvermana21c3a22014-06-12 21:49:15 -0700631 loop_->mutable_U(0, 0) = ::aos::Clip(
Brian Silverman56658322014-03-22 16:57:22 -0700632 (R_left / Kv)(0, 0) + (IsInGear(left_gear_) ? 0 : wiggle),
633 -12.0, 12.0);
Brian Silvermana21c3a22014-06-12 21:49:15 -0700634 loop_->mutable_U(1, 0) = ::aos::Clip(
Brian Silverman56658322014-03-22 16:57:22 -0700635 (R_right / Kv)(0, 0) + (IsInGear(right_gear_) ? 0 : wiggle),
636 -12.0, 12.0);
Brian Silverman699f0cb2015-02-05 19:45:01 -0500637 loop_->mutable_U() *= 12.0 / ::aos::robot_state->voltage_battery;
Brian Silverman93335ae2015-01-26 20:43:39 -0500638#endif
Austin Schuh2054f5f2013-10-27 14:54:10 -0700639 }
Austin Schuh2054f5f2013-10-27 14:54:10 -0700640 }
641
Brian Silvermanada5f2c2015-02-01 02:41:14 -0500642 void SendMotors(DrivetrainQueue::Output *output) {
Brian Silvermande8fd552013-11-03 15:53:42 -0800643 if (output != NULL) {
644 output->left_voltage = loop_->U(0, 0);
645 output->right_voltage = loop_->U(1, 0);
Brian Silverman61e41fd2014-02-16 19:08:50 -0800646 output->left_high = left_gear_ == HIGH || left_gear_ == SHIFTING_UP;
647 output->right_high = right_gear_ == HIGH || right_gear_ == SHIFTING_UP;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700648 }
649 }
650
651 private:
652 const ::aos::controls::HPolytope<2> U_Poly_;
653
654 ::std::unique_ptr<StateFeedbackLoop<2, 2, 2>> loop_;
655
Austin Schuh427b3702013-11-02 13:44:09 -0700656 const double ttrust_;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700657 double wheel_;
658 double throttle_;
659 bool quickturn_;
Austin Schuh427b3702013-11-02 13:44:09 -0700660 int stale_count_;
661 double position_time_delta_;
662 Gear left_gear_;
663 Gear right_gear_;
Brian Silvermanada5f2c2015-02-01 02:41:14 -0500664 DrivetrainQueue::Position last_position_;
665 DrivetrainQueue::Position position_;
Brian Silvermande8fd552013-11-03 15:53:42 -0800666 int counter_;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700667};
Brian Silvermancec3c8d2013-12-20 21:04:55 -0800668constexpr double PolyDrivetrain::kStallTorque;
669constexpr double PolyDrivetrain::kStallCurrent;
670constexpr double PolyDrivetrain::kFreeSpeed;
671constexpr double PolyDrivetrain::kFreeCurrent;
672constexpr double PolyDrivetrain::J;
673constexpr double PolyDrivetrain::m;
674constexpr double PolyDrivetrain::rb;
675constexpr double PolyDrivetrain::kWheelRadius;
676constexpr double PolyDrivetrain::kR;
677constexpr double PolyDrivetrain::Kv;
678constexpr double PolyDrivetrain::Kt;
679
Austin Schuh2054f5f2013-10-27 14:54:10 -0700680
Brian Silvermanada5f2c2015-02-01 02:41:14 -0500681void DrivetrainLoop::RunIteration(const DrivetrainQueue::Goal *goal,
682 const DrivetrainQueue::Position *position,
683 DrivetrainQueue::Output *output,
684 DrivetrainQueue::Status * status) {
brians343bc112013-02-10 01:53:46 +0000685 // TODO(aschuh): These should be members of the class.
686 static DrivetrainMotorsSS dt_closedloop;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700687 static PolyDrivetrain dt_openloop;
brians343bc112013-02-10 01:53:46 +0000688
689 bool bad_pos = false;
Austin Schuh427b3702013-11-02 13:44:09 -0700690 if (position == nullptr) {
Brian Silverman50a9d032014-02-16 17:20:57 -0800691 LOG_INTERVAL(no_position_);
brians343bc112013-02-10 01:53:46 +0000692 bad_pos = true;
693 }
Brian Silverman9c9ab422014-02-25 13:42:53 -0800694 no_position_.Print();
brians343bc112013-02-10 01:53:46 +0000695
Brian Silverman089f5812015-02-15 01:58:19 -0500696 bool control_loop_driving = false;
697 if (goal) {
698 double wheel = goal->steering;
699 double throttle = goal->throttle;
700 bool quickturn = goal->quickturn;
Brian Silverman93335ae2015-01-26 20:43:39 -0500701#if HAVE_SHIFTERS
Brian Silverman089f5812015-02-15 01:58:19 -0500702 bool highgear = goal->highgear;
Brian Silverman93335ae2015-01-26 20:43:39 -0500703#endif
brians343bc112013-02-10 01:53:46 +0000704
Brian Silverman089f5812015-02-15 01:58:19 -0500705 control_loop_driving = goal->control_loop_driving;
706 double left_goal = goal->left_goal;
707 double right_goal = goal->right_goal;
brians343bc112013-02-10 01:53:46 +0000708
Brian Silverman089f5812015-02-15 01:58:19 -0500709 dt_closedloop.SetGoal(left_goal, goal->left_velocity_goal, right_goal,
710 goal->right_velocity_goal);
711#if HAVE_SHIFTERS
712 dt_openloop.SetGoal(wheel, throttle, quickturn, highgear);
713#else
714 dt_openloop.SetGoal(wheel, throttle, quickturn, false);
715#endif
716 }
717
brians343bc112013-02-10 01:53:46 +0000718 if (!bad_pos) {
719 const double left_encoder = position->left_encoder;
720 const double right_encoder = position->right_encoder;
Brian Silverman6bf0d3c2014-03-08 12:52:54 -0800721 if (gyro_reading.FetchLatest()) {
Brian Silverman74e5b4e2014-03-09 16:58:58 -0700722 LOG_STRUCT(DEBUG, "using", *gyro_reading.get());
723 dt_closedloop.SetPosition(left_encoder, right_encoder,
Brian Silvermanfaac3a22014-04-30 17:54:30 -0700724 gyro_reading->angle);
brians343bc112013-02-10 01:53:46 +0000725 } else {
726 dt_closedloop.SetRawPosition(left_encoder, right_encoder);
727 }
728 }
Austin Schuh427b3702013-11-02 13:44:09 -0700729 dt_openloop.SetPosition(position);
brians343bc112013-02-10 01:53:46 +0000730 dt_openloop.Update();
Ben Fredrickson890c3fe2014-03-02 00:15:16 +0000731
Brian Silverman2845c4c2013-03-16 19:54:08 -0700732 if (control_loop_driving) {
Brian Silvermanfaac3a22014-04-30 17:54:30 -0700733 dt_closedloop.Update(output == NULL, true);
Brian Silverman2845c4c2013-03-16 19:54:08 -0700734 dt_closedloop.SendMotors(output);
735 } else {
736 dt_openloop.SendMotors(output);
Austin Schuhb5afb692014-03-02 11:54:11 -0800737 if (output) {
738 dt_closedloop.SetExternalMotors(output->left_voltage,
739 output->right_voltage);
740 }
Brian Silvermanfaac3a22014-04-30 17:54:30 -0700741 dt_closedloop.Update(output == NULL, false);
brians343bc112013-02-10 01:53:46 +0000742 }
Brian Silverman089f5812015-02-15 01:58:19 -0500743
Brian Silverman2c764f02014-04-25 09:21:21 -0500744 // set the output status of the control loop state
Ben Fredrickson890c3fe2014-03-02 00:15:16 +0000745 if (status) {
746 bool done = false;
747 if (goal) {
748 done = ((::std::abs(goal->left_goal -
749 dt_closedloop.GetEstimatedLeftEncoder()) <
750 constants::GetValues().drivetrain_done_distance) &&
751 (::std::abs(goal->right_goal -
752 dt_closedloop.GetEstimatedRightEncoder()) <
753 constants::GetValues().drivetrain_done_distance));
754 }
755 status->is_done = done;
756 status->robot_speed = dt_closedloop.GetEstimatedRobotSpeed();
Austin Schuh577edf62014-04-13 10:33:05 -0700757 status->filtered_left_position = dt_closedloop.GetEstimatedLeftEncoder();
758 status->filtered_right_position = dt_closedloop.GetEstimatedRightEncoder();
Brian Silvermanb94069c2014-04-17 14:34:24 -0700759 status->output_was_capped = dt_closedloop.OutputWasCapped();
760 status->uncapped_left_voltage = dt_closedloop.loop().U_uncapped(0, 0);
761 status->uncapped_right_voltage = dt_closedloop.loop().U_uncapped(1, 0);
brians343bc112013-02-10 01:53:46 +0000762 }
763}
764
765} // namespace control_loops
766} // namespace frc971