blob: b5eb83ab956e546d4a80ee5474078b8c68391f8d [file] [log] [blame]
Brian Silvermanb691f5e2015-08-02 11:37:55 -07001#include "y2015/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
Brian Silvermanb691f5e2015-08-02 11:37:55 -070015#include "y2015/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"
Brian Silvermanb691f5e2015-08-02 11:37:55 -070018#include "y2015/control_loops/drivetrain/polydrivetrain_cim_plant.h"
19#include "y2015/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) {
Austin Schuh322d2d12015-04-18 22:57:42 -070058 mutable_U() =
59 U() * 12.0 / ::std::max(::std::abs(U(0, 0)), ::std::abs(U(1, 0)));
60 LOG_MATRIX(DEBUG, "U is now", U());
61 // TODO(Austin): Figure out why the polytope stuff wasn't working and
62 // remove this hack.
Brian Silvermanb94069c2014-04-17 14:34:24 -070063 output_was_capped_ = true;
Austin Schuh322d2d12015-04-18 22:57:42 -070064 return;
65
Brian Silverman273d8a32014-05-10 22:19:09 -070066 LOG_MATRIX(DEBUG, "U at start", U());
Austin Schuh322d2d12015-04-18 22:57:42 -070067 LOG_MATRIX(DEBUG, "R at start", R());
68 LOG_MATRIX(DEBUG, "Xhat at start", X_hat());
Brian Silvermanad9e0002014-04-13 14:55:57 -070069
70 Eigen::Matrix<double, 2, 2> position_K;
71 position_K << K(0, 0), K(0, 2),
72 K(1, 0), K(1, 2);
73 Eigen::Matrix<double, 2, 2> velocity_K;
74 velocity_K << K(0, 1), K(0, 3),
75 K(1, 1), K(1, 3);
76
77 Eigen::Matrix<double, 2, 1> position_error;
78 position_error << error(0, 0), error(2, 0);
79 const auto drive_error = T_inverse * position_error;
80 Eigen::Matrix<double, 2, 1> velocity_error;
81 velocity_error << error(1, 0), error(3, 0);
82 LOG_MATRIX(DEBUG, "error", error);
83
84 const auto &poly = U_Poly_;
Brian Silverman273d8a32014-05-10 22:19:09 -070085 const Eigen::Matrix<double, 4, 2> pos_poly_H =
86 poly.H() * position_K * T;
Brian Silvermanad9e0002014-04-13 14:55:57 -070087 const Eigen::Matrix<double, 4, 1> pos_poly_k =
88 poly.k() - poly.H() * velocity_K * velocity_error;
89 const ::aos::controls::HPolytope<2> pos_poly(pos_poly_H, pos_poly_k);
90
91 Eigen::Matrix<double, 2, 1> adjusted_pos_error;
92 {
93 const auto &P = drive_error;
94
95 Eigen::Matrix<double, 1, 2> L45;
96 L45 << ::aos::sign(P(1, 0)), -::aos::sign(P(0, 0));
97 const double w45 = 0;
98
99 Eigen::Matrix<double, 1, 2> LH;
100 if (::std::abs(P(0, 0)) > ::std::abs(P(1, 0))) {
101 LH << 0, 1;
102 } else {
103 LH << 1, 0;
104 }
105 const double wh = LH.dot(P);
106
107 Eigen::Matrix<double, 2, 2> standard;
108 standard << L45, LH;
109 Eigen::Matrix<double, 2, 1> W;
110 W << w45, wh;
Brian Silverman273d8a32014-05-10 22:19:09 -0700111 const Eigen::Matrix<double, 2, 1> intersection =
112 standard.inverse() * W;
Brian Silvermanad9e0002014-04-13 14:55:57 -0700113
114 bool is_inside_h;
115 const auto adjusted_pos_error_h =
116 DoCoerceGoal(pos_poly, LH, wh, drive_error, &is_inside_h);
117 const auto adjusted_pos_error_45 =
118 DoCoerceGoal(pos_poly, L45, w45, intersection, nullptr);
119 if (pos_poly.IsInside(intersection)) {
120 adjusted_pos_error = adjusted_pos_error_h;
121 } else {
122 if (is_inside_h) {
123 if (adjusted_pos_error_h.norm() > adjusted_pos_error_45.norm()) {
124 adjusted_pos_error = adjusted_pos_error_h;
125 } else {
126 adjusted_pos_error = adjusted_pos_error_45;
127 }
128 } else {
129 adjusted_pos_error = adjusted_pos_error_45;
130 }
131 }
132 }
133
134 LOG_MATRIX(DEBUG, "adjusted_pos_error", adjusted_pos_error);
Brian Silverman0ca790b2014-06-12 21:33:08 -0700135 mutable_U() =
Brian Silverman273d8a32014-05-10 22:19:09 -0700136 velocity_K * velocity_error + position_K * T * adjusted_pos_error;
137 LOG_MATRIX(DEBUG, "U is now", U());
Brian Silvermanb94069c2014-04-17 14:34:24 -0700138 } else {
139 output_was_capped_ = false;
Brian Silvermanad9e0002014-04-13 14:55:57 -0700140 }
141 }
142
143 const ::aos::controls::HPolytope<2> U_Poly_;
144 Eigen::Matrix<double, 2, 2> T, T_inverse;
Brian Silverman94738b62014-05-02 17:43:11 -0700145 bool output_was_capped_ = false;;
Brian Silvermanad9e0002014-04-13 14:55:57 -0700146 };
147
Brian Silverman2c590c32013-11-04 18:08:54 -0800148 DrivetrainMotorsSS()
Brian Silvermanad9e0002014-04-13 14:55:57 -0700149 : loop_(new LimitedDrivetrainLoop(
Brian Silverman176303a2014-04-10 10:54:55 -0700150 constants::GetValues().make_drivetrain_loop())),
151 filtered_offset_(0.0),
152 gyro_(0.0),
153 left_goal_(0.0),
154 right_goal_(0.0),
155 raw_left_(0.0),
Brian Silvermanfaac3a22014-04-30 17:54:30 -0700156 raw_right_(0.0) {
Brian Silvermanb94069c2014-04-17 14:34:24 -0700157 // Low gear on both.
158 loop_->set_controller_index(0);
brians343bc112013-02-10 01:53:46 +0000159 }
Brian Silverman176303a2014-04-10 10:54:55 -0700160
161 void SetGoal(double left, double left_velocity, double right,
162 double right_velocity) {
163 left_goal_ = left;
164 right_goal_ = right;
Brian Silverman0ca790b2014-06-12 21:33:08 -0700165 loop_->mutable_R() << left, left_velocity, right, right_velocity;
brians343bc112013-02-10 01:53:46 +0000166 }
167 void SetRawPosition(double left, double right) {
Brian Silverman176303a2014-04-10 10:54:55 -0700168 raw_right_ = right;
169 raw_left_ = left;
Austin Schuhf9286cd2014-02-11 00:51:09 -0800170 Eigen::Matrix<double, 2, 1> Y;
Brian Silverman176303a2014-04-10 10:54:55 -0700171 Y << left + filtered_offset_, right - filtered_offset_;
Austin Schuhf9286cd2014-02-11 00:51:09 -0800172 loop_->Correct(Y);
brians343bc112013-02-10 01:53:46 +0000173 }
Brian Silvermanfaac3a22014-04-30 17:54:30 -0700174 void SetPosition(double left, double right, double gyro) {
brians343bc112013-02-10 01:53:46 +0000175 // Decay the offset quickly because this gyro is great.
Brian Silvermanad9e0002014-04-13 14:55:57 -0700176 const double offset =
177 (right - left - gyro * constants::GetValues().turn_width) / 2.0;
Brian Silverman176303a2014-04-10 10:54:55 -0700178 filtered_offset_ = 0.25 * offset + 0.75 * filtered_offset_;
179 gyro_ = gyro;
brians343bc112013-02-10 01:53:46 +0000180 SetRawPosition(left, right);
brians343bc112013-02-10 01:53:46 +0000181 }
Austin Schuh4352ac62013-03-19 06:23:16 +0000182
Ben Fredrickson890c3fe2014-03-02 00:15:16 +0000183 void SetExternalMotors(double left_voltage, double right_voltage) {
Brian Silverman0ca790b2014-06-12 21:33:08 -0700184 loop_->mutable_U() << left_voltage, right_voltage;
Ben Fredrickson890c3fe2014-03-02 00:15:16 +0000185 }
186
Brian Silvermanfaac3a22014-04-30 17:54:30 -0700187 void Update(bool stop_motors, bool enable_control_loop) {
188 if (enable_control_loop) {
Ben Fredrickson890c3fe2014-03-02 00:15:16 +0000189 loop_->Update(stop_motors);
190 } else {
191 if (stop_motors) {
Brian Silverman0ca790b2014-06-12 21:33:08 -0700192 loop_->mutable_U().setZero();
193 loop_->mutable_U_uncapped().setZero();
Ben Fredrickson890c3fe2014-03-02 00:15:16 +0000194 }
Austin Schuhc2b77742015-11-26 16:18:27 -0800195 loop_->UpdateObserver(loop_->U());
Ben Fredrickson890c3fe2014-03-02 00:15:16 +0000196 }
Brian Silverman273d8a32014-05-10 22:19:09 -0700197 ::Eigen::Matrix<double, 4, 1> E = loop_->R() - loop_->X_hat();
Brian Silverman3146b642014-03-20 14:52:46 -0700198 LOG_MATRIX(DEBUG, "E", E);
Ben Fredrickson890c3fe2014-03-02 00:15:16 +0000199 }
200
Brian Silvermanb94069c2014-04-17 14:34:24 -0700201 double GetEstimatedRobotSpeed() const {
Ben Fredrickson890c3fe2014-03-02 00:15:16 +0000202 // lets just call the average of left and right velocities close enough
203 return (loop_->X_hat(1, 0) + loop_->X_hat(3, 0)) / 2;
204 }
Brian Silvermanada5f2c2015-02-01 02:41:14 -0500205
Brian Silvermanb94069c2014-04-17 14:34:24 -0700206 double GetEstimatedLeftEncoder() const {
Ben Fredrickson890c3fe2014-03-02 00:15:16 +0000207 return loop_->X_hat(0, 0);
208 }
Brian Silvermanada5f2c2015-02-01 02:41:14 -0500209
Brian Silvermanb94069c2014-04-17 14:34:24 -0700210 double GetEstimatedRightEncoder() const {
Ben Fredrickson890c3fe2014-03-02 00:15:16 +0000211 return loop_->X_hat(2, 0);
brians343bc112013-02-10 01:53:46 +0000212 }
213
Brian Silvermanb94069c2014-04-17 14:34:24 -0700214 bool OutputWasCapped() const {
215 return loop_->output_was_capped();
216 }
217
Brian Silvermanada5f2c2015-02-01 02:41:14 -0500218 void SendMotors(DrivetrainQueue::Output *output) const {
Austin Schuh4352ac62013-03-19 06:23:16 +0000219 if (output) {
220 output->left_voltage = loop_->U(0, 0);
221 output->right_voltage = loop_->U(1, 0);
Brian Silvermane3a277a2014-04-13 14:56:57 -0700222 output->left_high = false;
223 output->right_high = false;
brians8ad74052013-03-16 21:04:51 +0000224 }
brians343bc112013-02-10 01:53:46 +0000225 }
brians343bc112013-02-10 01:53:46 +0000226
Brian Silvermanb94069c2014-04-17 14:34:24 -0700227 const LimitedDrivetrainLoop &loop() const { return *loop_; }
228
brians343bc112013-02-10 01:53:46 +0000229 private:
Brian Silvermanad9e0002014-04-13 14:55:57 -0700230 ::std::unique_ptr<LimitedDrivetrainLoop> loop_;
Austin Schuh4352ac62013-03-19 06:23:16 +0000231
Brian Silverman176303a2014-04-10 10:54:55 -0700232 double filtered_offset_;
233 double gyro_;
234 double left_goal_;
235 double right_goal_;
236 double raw_left_;
237 double raw_right_;
brians343bc112013-02-10 01:53:46 +0000238};
239
Austin Schuh2054f5f2013-10-27 14:54:10 -0700240class PolyDrivetrain {
241 public:
Austin Schuh427b3702013-11-02 13:44:09 -0700242
243 enum Gear {
244 HIGH,
245 LOW,
246 SHIFTING_UP,
247 SHIFTING_DOWN
248 };
249 // Stall Torque in N m
250 static constexpr double kStallTorque = 2.42;
251 // Stall Current in Amps
Brian Silverman4b36e2e2015-03-16 15:46:53 -0700252 static constexpr double kStallCurrent = 133.0;
Austin Schuh427b3702013-11-02 13:44:09 -0700253 // Free Speed in RPM. Used number from last year.
254 static constexpr double kFreeSpeed = 4650.0;
255 // Free Current in Amps
256 static constexpr double kFreeCurrent = 2.7;
257 // Moment of inertia of the drivetrain in kg m^2
258 // Just borrowed from last year.
Brian Silverman4b36e2e2015-03-16 15:46:53 -0700259 static constexpr double J = 10;
Austin Schuh427b3702013-11-02 13:44:09 -0700260 // Mass of the robot, in kg.
261 static constexpr double m = 68;
262 // Radius of the robot, in meters (from last year).
Brian Silverman4b36e2e2015-03-16 15:46:53 -0700263 static constexpr double rb = 0.9603 / 2.0;
264 static constexpr double kWheelRadius = 0.0515938;
Austin Schuh427b3702013-11-02 13:44:09 -0700265 // Resistance of the motor, divided by the number of motors.
Brian Silverman4b36e2e2015-03-16 15:46:53 -0700266 static constexpr double kR = (12.0 / kStallCurrent / 2 + 0.03) / (0.93 * 0.93);
Austin Schuh427b3702013-11-02 13:44:09 -0700267 // Motor velocity constant
268 static constexpr double Kv =
269 ((kFreeSpeed / 60.0 * 2.0 * M_PI) / (12.0 - kR * kFreeCurrent));
270 // Torque constant
271 static constexpr double Kt = kStallTorque / kStallCurrent;
Austin Schuh427b3702013-11-02 13:44:09 -0700272
Austin Schuh2054f5f2013-10-27 14:54:10 -0700273 PolyDrivetrain()
274 : U_Poly_((Eigen::Matrix<double, 4, 2>() << /*[[*/ 1, 0 /*]*/,
275 /*[*/ -1, 0 /*]*/,
276 /*[*/ 0, 1 /*]*/,
277 /*[*/ 0, -1 /*]]*/).finished(),
278 (Eigen::Matrix<double, 4, 1>() << /*[[*/ 12 /*]*/,
279 /*[*/ 12 /*]*/,
280 /*[*/ 12 /*]*/,
281 /*[*/ 12 /*]]*/).finished()),
Brian Silverman2c590c32013-11-04 18:08:54 -0800282 loop_(new StateFeedbackLoop<2, 2, 2>(
283 constants::GetValues().make_v_drivetrain_loop())),
Austin Schuh427b3702013-11-02 13:44:09 -0700284 ttrust_(1.1),
285 wheel_(0.0),
286 throttle_(0.0),
287 quickturn_(false),
288 stale_count_(0),
289 position_time_delta_(0.01),
290 left_gear_(LOW),
Brian Silvermande8fd552013-11-03 15:53:42 -0800291 right_gear_(LOW),
292 counter_(0) {
Austin Schuh2054f5f2013-10-27 14:54:10 -0700293
Austin Schuh427b3702013-11-02 13:44:09 -0700294 last_position_.Zero();
295 position_.Zero();
Austin Schuh2054f5f2013-10-27 14:54:10 -0700296 }
Austin Schuh427b3702013-11-02 13:44:09 -0700297 static bool IsInGear(Gear gear) { return gear == LOW || gear == HIGH; }
298
Austin Schuh78d55462014-02-23 01:39:30 -0800299 static double MotorSpeed(const constants::ShifterHallEffect &hall_effect,
300 double shifter_position, double velocity) {
Austin Schuh427b3702013-11-02 13:44:09 -0700301 // TODO(austin): G_high, G_low and kWheelRadius
Austin Schuh78d55462014-02-23 01:39:30 -0800302 const double avg_hall_effect =
303 (hall_effect.clear_high + hall_effect.clear_low) / 2.0;
304
305 if (shifter_position > avg_hall_effect) {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800306 return velocity / constants::GetValues().high_gear_ratio / kWheelRadius;
Austin Schuh427b3702013-11-02 13:44:09 -0700307 } else {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800308 return velocity / constants::GetValues().low_gear_ratio / kWheelRadius;
309 }
310 }
311
Austin Schuh78d55462014-02-23 01:39:30 -0800312 Gear ComputeGear(const constants::ShifterHallEffect &hall_effect,
313 double velocity, Gear current) {
314 const double low_omega = MotorSpeed(hall_effect, 0.0, ::std::abs(velocity));
315 const double high_omega =
316 MotorSpeed(hall_effect, 1.0, ::std::abs(velocity));
Brian Silverman1a6590d2013-11-04 14:46:46 -0800317
Brian Silverman1a6590d2013-11-04 14:46:46 -0800318 double high_torque = ((12.0 - high_omega / Kv) * Kt / kR);
319 double low_torque = ((12.0 - low_omega / Kv) * Kt / kR);
320 double high_power = high_torque * high_omega;
321 double low_power = low_torque * low_omega;
Brian Silverman55a930b2013-11-04 20:59:00 -0800322
323 // TODO(aschuh): Do this right!
324 if ((current == HIGH || high_power > low_power + 160) &&
325 ::std::abs(velocity) > 0.14) {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800326 return HIGH;
327 } else {
328 return LOW;
Austin Schuh427b3702013-11-02 13:44:09 -0700329 }
330 }
331
Austin Schuh2054f5f2013-10-27 14:54:10 -0700332 void SetGoal(double wheel, double throttle, bool quickturn, bool highgear) {
Brian Silverman4b36e2e2015-03-16 15:46:53 -0700333 const double kWheelNonLinearity = 0.5;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700334 // Apply a sin function that's scaled to make it feel better.
335 const double angular_range = M_PI_2 * kWheelNonLinearity;
Brian Silverman4b36e2e2015-03-16 15:46:53 -0700336
Austin Schuh2054f5f2013-10-27 14:54:10 -0700337 wheel_ = sin(angular_range * wheel) / sin(angular_range);
338 wheel_ = sin(angular_range * wheel_) / sin(angular_range);
Brian Silverman4b36e2e2015-03-16 15:46:53 -0700339 wheel_ *= 2.3;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700340 quickturn_ = quickturn;
Austin Schuh427b3702013-11-02 13:44:09 -0700341
Brian Silverman1a6590d2013-11-04 14:46:46 -0800342 static const double kThrottleDeadband = 0.05;
343 if (::std::abs(throttle) < kThrottleDeadband) {
344 throttle_ = 0;
345 } else {
346 throttle_ = copysign((::std::abs(throttle) - kThrottleDeadband) /
347 (1.0 - kThrottleDeadband), throttle);
348 }
349
Austin Schuh427b3702013-11-02 13:44:09 -0700350 // TODO(austin): Fix the upshift logic to include states.
Brian Silverman1a6590d2013-11-04 14:46:46 -0800351 Gear requested_gear;
Brian Silverman55a930b2013-11-04 20:59:00 -0800352 if (false) {
Austin Schuh78d55462014-02-23 01:39:30 -0800353 const auto &values = constants::GetValues();
Brian Silverman1a6590d2013-11-04 14:46:46 -0800354 const double current_left_velocity =
355 (position_.left_encoder - last_position_.left_encoder) /
356 position_time_delta_;
357 const double current_right_velocity =
358 (position_.right_encoder - last_position_.right_encoder) /
359 position_time_delta_;
360
Austin Schuh78d55462014-02-23 01:39:30 -0800361 Gear left_requested =
362 ComputeGear(values.left_drive, current_left_velocity, left_gear_);
363 Gear right_requested =
364 ComputeGear(values.right_drive, current_right_velocity, right_gear_);
Brian Silverman1a6590d2013-11-04 14:46:46 -0800365 requested_gear =
366 (left_requested == HIGH || right_requested == HIGH) ? HIGH : LOW;
367 } else {
368 requested_gear = highgear ? HIGH : LOW;
369 }
370
371 const Gear shift_up =
372 constants::GetValues().clutch_transmission ? HIGH : SHIFTING_UP;
373 const Gear shift_down =
374 constants::GetValues().clutch_transmission ? LOW : SHIFTING_DOWN;
Austin Schuh427b3702013-11-02 13:44:09 -0700375
376 if (left_gear_ != requested_gear) {
377 if (IsInGear(left_gear_)) {
378 if (requested_gear == HIGH) {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800379 left_gear_ = shift_up;
Austin Schuh427b3702013-11-02 13:44:09 -0700380 } else {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800381 left_gear_ = shift_down;
Austin Schuh427b3702013-11-02 13:44:09 -0700382 }
Brian Silverman55a930b2013-11-04 20:59:00 -0800383 } else {
384 if (requested_gear == HIGH && left_gear_ == SHIFTING_DOWN) {
385 left_gear_ = SHIFTING_UP;
386 } else if (requested_gear == LOW && left_gear_ == SHIFTING_UP) {
387 left_gear_ = SHIFTING_DOWN;
388 }
Austin Schuh427b3702013-11-02 13:44:09 -0700389 }
390 }
391 if (right_gear_ != requested_gear) {
392 if (IsInGear(right_gear_)) {
393 if (requested_gear == HIGH) {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800394 right_gear_ = shift_up;
Austin Schuh427b3702013-11-02 13:44:09 -0700395 } else {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800396 right_gear_ = shift_down;
Austin Schuh427b3702013-11-02 13:44:09 -0700397 }
Brian Silverman55a930b2013-11-04 20:59:00 -0800398 } else {
399 if (requested_gear == HIGH && right_gear_ == SHIFTING_DOWN) {
400 right_gear_ = SHIFTING_UP;
401 } else if (requested_gear == LOW && right_gear_ == SHIFTING_UP) {
402 right_gear_ = SHIFTING_DOWN;
403 }
Austin Schuh427b3702013-11-02 13:44:09 -0700404 }
Austin Schuh2054f5f2013-10-27 14:54:10 -0700405 }
406 }
Brian Silvermanada5f2c2015-02-01 02:41:14 -0500407 void SetPosition(const DrivetrainQueue::Position *position) {
Austin Schuh78d55462014-02-23 01:39:30 -0800408 const auto &values = constants::GetValues();
Austin Schuh427b3702013-11-02 13:44:09 -0700409 if (position == NULL) {
410 ++stale_count_;
411 } else {
412 last_position_ = position_;
413 position_ = *position;
414 position_time_delta_ = (stale_count_ + 1) * 0.01;
415 stale_count_ = 0;
416 }
417
Brian Silverman93335ae2015-01-26 20:43:39 -0500418#if HAVE_SHIFTERS
Austin Schuh427b3702013-11-02 13:44:09 -0700419 if (position) {
Brian Silverman61e41fd2014-02-16 19:08:50 -0800420 GearLogging gear_logging;
Austin Schuh427b3702013-11-02 13:44:09 -0700421 // Switch to the correct controller.
Austin Schuh78d55462014-02-23 01:39:30 -0800422 const double left_middle_shifter_position =
423 (values.left_drive.clear_high + values.left_drive.clear_low) / 2.0;
424 const double right_middle_shifter_position =
425 (values.right_drive.clear_high + values.right_drive.clear_low) / 2.0;
426
Daniel Pettifd0a51d2014-11-07 16:59:24 -0800427 if (position->left_shifter_position < left_middle_shifter_position ||
428 left_gear_ == LOW) {
Austin Schuh78d55462014-02-23 01:39:30 -0800429 if (position->right_shifter_position < right_middle_shifter_position ||
430 right_gear_ == LOW) {
Brian Silverman61e41fd2014-02-16 19:08:50 -0800431 gear_logging.left_loop_high = false;
432 gear_logging.right_loop_high = false;
433 loop_->set_controller_index(gear_logging.controller_index = 0);
Austin Schuh427b3702013-11-02 13:44:09 -0700434 } else {
Brian Silverman61e41fd2014-02-16 19:08:50 -0800435 gear_logging.left_loop_high = false;
436 gear_logging.right_loop_high = true;
437 loop_->set_controller_index(gear_logging.controller_index = 1);
Austin Schuh427b3702013-11-02 13:44:09 -0700438 }
439 } else {
Austin Schuh78d55462014-02-23 01:39:30 -0800440 if (position->right_shifter_position < right_middle_shifter_position ||
Daniel Pettifd0a51d2014-11-07 16:59:24 -0800441 right_gear_ == LOW) {
Brian Silverman61e41fd2014-02-16 19:08:50 -0800442 gear_logging.left_loop_high = true;
443 gear_logging.right_loop_high = false;
444 loop_->set_controller_index(gear_logging.controller_index = 2);
Austin Schuh427b3702013-11-02 13:44:09 -0700445 } else {
Brian Silverman61e41fd2014-02-16 19:08:50 -0800446 gear_logging.left_loop_high = true;
447 gear_logging.right_loop_high = true;
448 loop_->set_controller_index(gear_logging.controller_index = 3);
Austin Schuh427b3702013-11-02 13:44:09 -0700449 }
450 }
Brian Silverman61e41fd2014-02-16 19:08:50 -0800451
Austin Schuh427b3702013-11-02 13:44:09 -0700452 // TODO(austin): Constants.
Austin Schuh78d55462014-02-23 01:39:30 -0800453 if (position->left_shifter_position > values.left_drive.clear_high && left_gear_ == SHIFTING_UP) {
Austin Schuh427b3702013-11-02 13:44:09 -0700454 left_gear_ = HIGH;
455 }
Austin Schuh78d55462014-02-23 01:39:30 -0800456 if (position->left_shifter_position < values.left_drive.clear_low && left_gear_ == SHIFTING_DOWN) {
Austin Schuh427b3702013-11-02 13:44:09 -0700457 left_gear_ = LOW;
458 }
Austin Schuh78d55462014-02-23 01:39:30 -0800459 if (position->right_shifter_position > values.right_drive.clear_high && right_gear_ == SHIFTING_UP) {
Austin Schuh427b3702013-11-02 13:44:09 -0700460 right_gear_ = HIGH;
461 }
Austin Schuh78d55462014-02-23 01:39:30 -0800462 if (position->right_shifter_position < values.right_drive.clear_low && right_gear_ == SHIFTING_DOWN) {
Austin Schuh427b3702013-11-02 13:44:09 -0700463 right_gear_ = LOW;
464 }
Brian Silverman61e41fd2014-02-16 19:08:50 -0800465
466 gear_logging.left_state = left_gear_;
467 gear_logging.right_state = right_gear_;
468 LOG_STRUCT(DEBUG, "state", gear_logging);
Austin Schuh427b3702013-11-02 13:44:09 -0700469 }
Brian Silverman93335ae2015-01-26 20:43:39 -0500470#else
471 (void) values;
472#endif
Austin Schuh427b3702013-11-02 13:44:09 -0700473 }
474
Austin Schuh2054f5f2013-10-27 14:54:10 -0700475 double FilterVelocity(double throttle) {
476 const Eigen::Matrix<double, 2, 2> FF =
477 loop_->B().inverse() *
478 (Eigen::Matrix<double, 2, 2>::Identity() - loop_->A());
479
480 constexpr int kHighGearController = 3;
481 const Eigen::Matrix<double, 2, 2> FF_high =
Brian Silverman273d8a32014-05-10 22:19:09 -0700482 loop_->controller(kHighGearController).plant.B().inverse() *
Austin Schuh2054f5f2013-10-27 14:54:10 -0700483 (Eigen::Matrix<double, 2, 2>::Identity() -
Brian Silverman273d8a32014-05-10 22:19:09 -0700484 loop_->controller(kHighGearController).plant.A());
Austin Schuh2054f5f2013-10-27 14:54:10 -0700485
486 ::Eigen::Matrix<double, 1, 2> FF_sum = FF.colwise().sum();
487 int min_FF_sum_index;
488 const double min_FF_sum = FF_sum.minCoeff(&min_FF_sum_index);
489 const double min_K_sum = loop_->K().col(min_FF_sum_index).sum();
490 const double high_min_FF_sum = FF_high.col(0).sum();
491
492 const double adjusted_ff_voltage = ::aos::Clip(
493 throttle * 12.0 * min_FF_sum / high_min_FF_sum, -12.0, 12.0);
Brian Silverman4b36e2e2015-03-16 15:46:53 -0700494 return (adjusted_ff_voltage +
495 ttrust_ * min_K_sum * (loop_->X_hat(0, 0) + loop_->X_hat(1, 0)) /
496 2.0) /
497 (ttrust_ * min_K_sum + min_FF_sum);
Austin Schuh2054f5f2013-10-27 14:54:10 -0700498 }
499
Brian Silverman718b1d72013-10-28 16:22:45 -0700500 double MaxVelocity() {
501 const Eigen::Matrix<double, 2, 2> FF =
502 loop_->B().inverse() *
503 (Eigen::Matrix<double, 2, 2>::Identity() - loop_->A());
504
505 constexpr int kHighGearController = 3;
506 const Eigen::Matrix<double, 2, 2> FF_high =
Brian Silverman273d8a32014-05-10 22:19:09 -0700507 loop_->controller(kHighGearController).plant.B().inverse() *
Brian Silverman718b1d72013-10-28 16:22:45 -0700508 (Eigen::Matrix<double, 2, 2>::Identity() -
Brian Silverman273d8a32014-05-10 22:19:09 -0700509 loop_->controller(kHighGearController).plant.A());
Brian Silverman718b1d72013-10-28 16:22:45 -0700510
511 ::Eigen::Matrix<double, 1, 2> FF_sum = FF.colwise().sum();
512 int min_FF_sum_index;
513 const double min_FF_sum = FF_sum.minCoeff(&min_FF_sum_index);
514 //const double min_K_sum = loop_->K().col(min_FF_sum_index).sum();
515 const double high_min_FF_sum = FF_high.col(0).sum();
516
517 const double adjusted_ff_voltage = ::aos::Clip(
518 12.0 * min_FF_sum / high_min_FF_sum, -12.0, 12.0);
519 return adjusted_ff_voltage / min_FF_sum;
520 }
521
Austin Schuh2054f5f2013-10-27 14:54:10 -0700522 void Update() {
Austin Schuh78d55462014-02-23 01:39:30 -0800523 const auto &values = constants::GetValues();
Austin Schuh427b3702013-11-02 13:44:09 -0700524 // TODO(austin): Observer for the current velocity instead of difference
525 // calculations.
Brian Silvermande8fd552013-11-03 15:53:42 -0800526 ++counter_;
Brian Silverman93335ae2015-01-26 20:43:39 -0500527#if HAVE_SHIFTERS
Austin Schuh427b3702013-11-02 13:44:09 -0700528 const double current_left_velocity =
Brian Silvermande8fd552013-11-03 15:53:42 -0800529 (position_.left_encoder - last_position_.left_encoder) /
Austin Schuh427b3702013-11-02 13:44:09 -0700530 position_time_delta_;
531 const double current_right_velocity =
Brian Silvermande8fd552013-11-03 15:53:42 -0800532 (position_.right_encoder - last_position_.right_encoder) /
Austin Schuh427b3702013-11-02 13:44:09 -0700533 position_time_delta_;
534 const double left_motor_speed =
Austin Schuh78d55462014-02-23 01:39:30 -0800535 MotorSpeed(values.left_drive, position_.left_shifter_position,
536 current_left_velocity);
Austin Schuh427b3702013-11-02 13:44:09 -0700537 const double right_motor_speed =
Austin Schuh78d55462014-02-23 01:39:30 -0800538 MotorSpeed(values.right_drive, position_.right_shifter_position,
539 current_right_velocity);
Austin Schuh2054f5f2013-10-27 14:54:10 -0700540
Brian Silverman61e41fd2014-02-16 19:08:50 -0800541 {
542 CIMLogging logging;
543
544 // Reset the CIM model to the current conditions to be ready for when we
545 // shift.
546 if (IsInGear(left_gear_)) {
547 logging.left_in_gear = true;
Brian Silverman61e41fd2014-02-16 19:08:50 -0800548 } else {
549 logging.left_in_gear = false;
550 }
551 logging.left_motor_speed = left_motor_speed;
552 logging.left_velocity = current_left_velocity;
553 if (IsInGear(right_gear_)) {
554 logging.right_in_gear = true;
Brian Silverman61e41fd2014-02-16 19:08:50 -0800555 } else {
556 logging.right_in_gear = false;
557 }
558 logging.right_motor_speed = right_motor_speed;
559 logging.right_velocity = current_right_velocity;
560
561 LOG_STRUCT(DEBUG, "currently", logging);
Austin Schuh427b3702013-11-02 13:44:09 -0700562 }
Brian Silverman93335ae2015-01-26 20:43:39 -0500563#else
564 (void) values;
565#endif
Austin Schuh2054f5f2013-10-27 14:54:10 -0700566
Brian Silverman93335ae2015-01-26 20:43:39 -0500567#if HAVE_SHIFTERS
Austin Schuh427b3702013-11-02 13:44:09 -0700568 if (IsInGear(left_gear_) && IsInGear(right_gear_)) {
Brian Silverman93335ae2015-01-26 20:43:39 -0500569#else
570 {
571#endif
Austin Schuh427b3702013-11-02 13:44:09 -0700572 // FF * X = U (steady state)
573 const Eigen::Matrix<double, 2, 2> FF =
574 loop_->B().inverse() *
575 (Eigen::Matrix<double, 2, 2>::Identity() - loop_->A());
576
577 // Invert the plant to figure out how the velocity filter would have to
578 // work
579 // out in order to filter out the forwards negative inertia.
580 // This math assumes that the left and right power and velocity are
581 // equals,
582 // and that the plant is the same on the left and right.
583 const double fvel = FilterVelocity(throttle_);
584
585 const double sign_svel = wheel_ * ((fvel > 0.0) ? 1.0 : -1.0);
586 double steering_velocity;
587 if (quickturn_) {
588 steering_velocity = wheel_ * MaxVelocity();
589 } else {
590 steering_velocity = ::std::abs(fvel) * wheel_;
591 }
592 const double left_velocity = fvel - steering_velocity;
593 const double right_velocity = fvel + steering_velocity;
Brian Silverman4b36e2e2015-03-16 15:46:53 -0700594 LOG(DEBUG, "l=%f r=%f\n", left_velocity, right_velocity);
Austin Schuh427b3702013-11-02 13:44:09 -0700595
596 // Integrate velocity to get the position.
597 // This position is used to get integral control.
Brian Silverman0ca790b2014-06-12 21:33:08 -0700598 loop_->mutable_R() << left_velocity, right_velocity;
Austin Schuh427b3702013-11-02 13:44:09 -0700599
600 if (!quickturn_) {
601 // K * R = w
602 Eigen::Matrix<double, 1, 2> equality_k;
603 equality_k << 1 + sign_svel, -(1 - sign_svel);
604 const double equality_w = 0.0;
605
606 // Construct a constraint on R by manipulating the constraint on U
607 ::aos::controls::HPolytope<2> R_poly = ::aos::controls::HPolytope<2>(
608 U_Poly_.H() * (loop_->K() + FF),
Brian Silverman273d8a32014-05-10 22:19:09 -0700609 U_Poly_.k() + U_Poly_.H() * loop_->K() * loop_->X_hat());
Austin Schuh427b3702013-11-02 13:44:09 -0700610
611 // Limit R back inside the box.
Brian Silverman0ca790b2014-06-12 21:33:08 -0700612 loop_->mutable_R() =
Brian Silverman273d8a32014-05-10 22:19:09 -0700613 CoerceGoal(R_poly, equality_k, equality_w, loop_->R());
Austin Schuh427b3702013-11-02 13:44:09 -0700614 }
615
Brian Silverman273d8a32014-05-10 22:19:09 -0700616 const Eigen::Matrix<double, 2, 1> FF_volts = FF * loop_->R();
Austin Schuh427b3702013-11-02 13:44:09 -0700617 const Eigen::Matrix<double, 2, 1> U_ideal =
Brian Silverman273d8a32014-05-10 22:19:09 -0700618 loop_->K() * (loop_->R() - loop_->X_hat()) + FF_volts;
Austin Schuh427b3702013-11-02 13:44:09 -0700619
620 for (int i = 0; i < 2; i++) {
Brian Silverman0ca790b2014-06-12 21:33:08 -0700621 loop_->mutable_U()[i] = ::aos::Clip(U_ideal[i], -12, 12);
Austin Schuh427b3702013-11-02 13:44:09 -0700622 }
Brian Silvermande8fd552013-11-03 15:53:42 -0800623
624 // TODO(austin): Model this better.
625 // TODO(austin): Feed back?
Brian Silverman0ca790b2014-06-12 21:33:08 -0700626 loop_->mutable_X_hat() =
Brian Silverman273d8a32014-05-10 22:19:09 -0700627 loop_->A() * loop_->X_hat() + loop_->B() * loop_->U();
Brian Silverman93335ae2015-01-26 20:43:39 -0500628#if HAVE_SHIFTERS
Brian Silverman718b1d72013-10-28 16:22:45 -0700629 } else {
Austin Schuh427b3702013-11-02 13:44:09 -0700630 // Any motor is not in gear. Speed match.
631 ::Eigen::Matrix<double, 1, 1> R_left;
Austin Schuh427b3702013-11-02 13:44:09 -0700632 ::Eigen::Matrix<double, 1, 1> R_right;
Austin Schuhb5afb692014-03-02 11:54:11 -0800633 R_left(0, 0) = left_motor_speed;
Austin Schuh427b3702013-11-02 13:44:09 -0700634 R_right(0, 0) = right_motor_speed;
Austin Schuhb5afb692014-03-02 11:54:11 -0800635
636 const double wiggle =
Brian Silvermanf970f2c2014-03-22 19:34:30 -0700637 (static_cast<double>((counter_ % 20) / 10) - 0.5) * 5.0;
Austin Schuhb5afb692014-03-02 11:54:11 -0800638
Brian Silvermana21c3a22014-06-12 21:49:15 -0700639 loop_->mutable_U(0, 0) = ::aos::Clip(
Brian Silverman56658322014-03-22 16:57:22 -0700640 (R_left / Kv)(0, 0) + (IsInGear(left_gear_) ? 0 : wiggle),
641 -12.0, 12.0);
Brian Silvermana21c3a22014-06-12 21:49:15 -0700642 loop_->mutable_U(1, 0) = ::aos::Clip(
Brian Silverman56658322014-03-22 16:57:22 -0700643 (R_right / Kv)(0, 0) + (IsInGear(right_gear_) ? 0 : wiggle),
644 -12.0, 12.0);
Brian Silverman699f0cb2015-02-05 19:45:01 -0500645 loop_->mutable_U() *= 12.0 / ::aos::robot_state->voltage_battery;
Brian Silverman93335ae2015-01-26 20:43:39 -0500646#endif
Austin Schuh2054f5f2013-10-27 14:54:10 -0700647 }
Austin Schuh2054f5f2013-10-27 14:54:10 -0700648 }
649
Brian Silvermanada5f2c2015-02-01 02:41:14 -0500650 void SendMotors(DrivetrainQueue::Output *output) {
Brian Silvermande8fd552013-11-03 15:53:42 -0800651 if (output != NULL) {
652 output->left_voltage = loop_->U(0, 0);
653 output->right_voltage = loop_->U(1, 0);
Brian Silverman61e41fd2014-02-16 19:08:50 -0800654 output->left_high = left_gear_ == HIGH || left_gear_ == SHIFTING_UP;
655 output->right_high = right_gear_ == HIGH || right_gear_ == SHIFTING_UP;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700656 }
657 }
658
659 private:
660 const ::aos::controls::HPolytope<2> U_Poly_;
661
662 ::std::unique_ptr<StateFeedbackLoop<2, 2, 2>> loop_;
663
Austin Schuh427b3702013-11-02 13:44:09 -0700664 const double ttrust_;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700665 double wheel_;
666 double throttle_;
667 bool quickturn_;
Austin Schuh427b3702013-11-02 13:44:09 -0700668 int stale_count_;
669 double position_time_delta_;
670 Gear left_gear_;
671 Gear right_gear_;
Brian Silvermanada5f2c2015-02-01 02:41:14 -0500672 DrivetrainQueue::Position last_position_;
673 DrivetrainQueue::Position position_;
Brian Silvermande8fd552013-11-03 15:53:42 -0800674 int counter_;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700675};
Brian Silvermancec3c8d2013-12-20 21:04:55 -0800676constexpr double PolyDrivetrain::kStallTorque;
677constexpr double PolyDrivetrain::kStallCurrent;
678constexpr double PolyDrivetrain::kFreeSpeed;
679constexpr double PolyDrivetrain::kFreeCurrent;
680constexpr double PolyDrivetrain::J;
681constexpr double PolyDrivetrain::m;
682constexpr double PolyDrivetrain::rb;
683constexpr double PolyDrivetrain::kWheelRadius;
684constexpr double PolyDrivetrain::kR;
685constexpr double PolyDrivetrain::Kv;
686constexpr double PolyDrivetrain::Kt;
687
Austin Schuh2054f5f2013-10-27 14:54:10 -0700688
Brian Silvermanada5f2c2015-02-01 02:41:14 -0500689void DrivetrainLoop::RunIteration(const DrivetrainQueue::Goal *goal,
690 const DrivetrainQueue::Position *position,
691 DrivetrainQueue::Output *output,
692 DrivetrainQueue::Status * status) {
brians343bc112013-02-10 01:53:46 +0000693 // TODO(aschuh): These should be members of the class.
694 static DrivetrainMotorsSS dt_closedloop;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700695 static PolyDrivetrain dt_openloop;
brians343bc112013-02-10 01:53:46 +0000696
697 bool bad_pos = false;
Austin Schuh427b3702013-11-02 13:44:09 -0700698 if (position == nullptr) {
Brian Silverman50a9d032014-02-16 17:20:57 -0800699 LOG_INTERVAL(no_position_);
brians343bc112013-02-10 01:53:46 +0000700 bad_pos = true;
701 }
Brian Silverman9c9ab422014-02-25 13:42:53 -0800702 no_position_.Print();
brians343bc112013-02-10 01:53:46 +0000703
Brian Silverman089f5812015-02-15 01:58:19 -0500704 bool control_loop_driving = false;
705 if (goal) {
706 double wheel = goal->steering;
707 double throttle = goal->throttle;
708 bool quickturn = goal->quickturn;
Brian Silverman93335ae2015-01-26 20:43:39 -0500709#if HAVE_SHIFTERS
Brian Silverman089f5812015-02-15 01:58:19 -0500710 bool highgear = goal->highgear;
Brian Silverman93335ae2015-01-26 20:43:39 -0500711#endif
brians343bc112013-02-10 01:53:46 +0000712
Brian Silverman089f5812015-02-15 01:58:19 -0500713 control_loop_driving = goal->control_loop_driving;
714 double left_goal = goal->left_goal;
715 double right_goal = goal->right_goal;
brians343bc112013-02-10 01:53:46 +0000716
Brian Silverman089f5812015-02-15 01:58:19 -0500717 dt_closedloop.SetGoal(left_goal, goal->left_velocity_goal, right_goal,
718 goal->right_velocity_goal);
719#if HAVE_SHIFTERS
720 dt_openloop.SetGoal(wheel, throttle, quickturn, highgear);
721#else
722 dt_openloop.SetGoal(wheel, throttle, quickturn, false);
723#endif
724 }
725
brians343bc112013-02-10 01:53:46 +0000726 if (!bad_pos) {
727 const double left_encoder = position->left_encoder;
728 const double right_encoder = position->right_encoder;
Brian Silverman6bf0d3c2014-03-08 12:52:54 -0800729 if (gyro_reading.FetchLatest()) {
Brian Silverman74e5b4e2014-03-09 16:58:58 -0700730 LOG_STRUCT(DEBUG, "using", *gyro_reading.get());
731 dt_closedloop.SetPosition(left_encoder, right_encoder,
Brian Silvermanfaac3a22014-04-30 17:54:30 -0700732 gyro_reading->angle);
brians343bc112013-02-10 01:53:46 +0000733 } else {
734 dt_closedloop.SetRawPosition(left_encoder, right_encoder);
735 }
736 }
Austin Schuh427b3702013-11-02 13:44:09 -0700737 dt_openloop.SetPosition(position);
brians343bc112013-02-10 01:53:46 +0000738 dt_openloop.Update();
Ben Fredrickson890c3fe2014-03-02 00:15:16 +0000739
Brian Silverman2845c4c2013-03-16 19:54:08 -0700740 if (control_loop_driving) {
Brian Silvermanfaac3a22014-04-30 17:54:30 -0700741 dt_closedloop.Update(output == NULL, true);
Brian Silverman2845c4c2013-03-16 19:54:08 -0700742 dt_closedloop.SendMotors(output);
743 } else {
744 dt_openloop.SendMotors(output);
Austin Schuhb5afb692014-03-02 11:54:11 -0800745 if (output) {
746 dt_closedloop.SetExternalMotors(output->left_voltage,
747 output->right_voltage);
748 }
Brian Silvermanfaac3a22014-04-30 17:54:30 -0700749 dt_closedloop.Update(output == NULL, false);
brians343bc112013-02-10 01:53:46 +0000750 }
Brian Silverman089f5812015-02-15 01:58:19 -0500751
Brian Silverman2c764f02014-04-25 09:21:21 -0500752 // set the output status of the control loop state
Ben Fredrickson890c3fe2014-03-02 00:15:16 +0000753 if (status) {
Ben Fredrickson890c3fe2014-03-02 00:15:16 +0000754 status->robot_speed = dt_closedloop.GetEstimatedRobotSpeed();
Austin Schuh093535c2016-03-05 23:21:00 -0800755 status->estimated_left_position = dt_closedloop.GetEstimatedLeftEncoder();
756 status->estimated_right_position = dt_closedloop.GetEstimatedRightEncoder();
Austin Schuh913c2f22015-04-18 22:57:19 -0700757
Austin Schuh093535c2016-03-05 23:21:00 -0800758 status->estimated_left_velocity = dt_closedloop.loop().X_hat(1, 0);
759 status->estimated_right_velocity = dt_closedloop.loop().X_hat(3, 0);
Brian Silvermanb94069c2014-04-17 14:34:24 -0700760 status->output_was_capped = dt_closedloop.OutputWasCapped();
761 status->uncapped_left_voltage = dt_closedloop.loop().U_uncapped(0, 0);
762 status->uncapped_right_voltage = dt_closedloop.loop().U_uncapped(1, 0);
brians343bc112013-02-10 01:53:46 +0000763 }
764}
765
766} // namespace control_loops
767} // namespace frc971