blob: 092f66ea1289f541b09431b196ba3c2f22b97dea [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"
10#include "aos/common/queue.h"
Briana6553ed2014-04-02 21:26:46 -070011#include "aos/common/controls/polytope.h"
Austin Schuh2054f5f2013-10-27 14:54:10 -070012#include "aos/common/commonmath.h"
Brian Silverman61e41fd2014-02-16 19:08:50 -080013#include "aos/common/logging/queue_logging.h"
Brian Silvermanfd5e2a32014-02-22 20:02:39 -080014#include "aos/common/logging/matrix_logging.h"
Brian Silverman61e41fd2014-02-16 19:08:50 -080015
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 Silverman6bf0d3c2014-03-08 12:52:54 -080020#include "frc971/queues/other_sensors.q.h"
Brian Silverman1a6590d2013-11-04 14:46:46 -080021#include "frc971/constants.h"
brians343bc112013-02-10 01:53:46 +000022
Brian Silverman6bf0d3c2014-03-08 12:52:54 -080023using frc971::sensors::gyro_reading;
brians343bc112013-02-10 01:53:46 +000024
25namespace frc971 {
26namespace control_loops {
27
28// Width of the robot.
29const double width = 22.0 / 100.0 * 2.54;
30
Austin Schuh4352ac62013-03-19 06:23:16 +000031class DrivetrainMotorsSS {
brians343bc112013-02-10 01:53:46 +000032 public:
Brian Silverman2c590c32013-11-04 18:08:54 -080033 DrivetrainMotorsSS()
34 : loop_(new StateFeedbackLoop<4, 2, 2>(
35 constants::GetValues().make_drivetrain_loop())) {
brians343bc112013-02-10 01:53:46 +000036 _offset = 0;
37 _integral_offset = 0;
38 _left_goal = 0.0;
39 _right_goal = 0.0;
40 _raw_left = 0.0;
41 _raw_right = 0.0;
Austin Schuh4352ac62013-03-19 06:23:16 +000042 _control_loop_driving = false;
brians343bc112013-02-10 01:53:46 +000043 }
44 void SetGoal(double left, double left_velocity, double right, double right_velocity) {
45 _left_goal = left;
46 _right_goal = right;
Austin Schuh4352ac62013-03-19 06:23:16 +000047 loop_->R << left, left_velocity, right, right_velocity;
brians343bc112013-02-10 01:53:46 +000048 }
49 void SetRawPosition(double left, double right) {
50 _raw_right = right;
51 _raw_left = left;
Austin Schuhf9286cd2014-02-11 00:51:09 -080052 Eigen::Matrix<double, 2, 1> Y;
53 Y << left, right;
54 loop_->Correct(Y);
brians343bc112013-02-10 01:53:46 +000055 }
Austin Schuh4352ac62013-03-19 06:23:16 +000056 void SetPosition(
57 double left, double right, double gyro, bool control_loop_driving) {
brians343bc112013-02-10 01:53:46 +000058 // Decay the offset quickly because this gyro is great.
59 _offset = (0.25) * (right - left - gyro * width) / 2.0 + 0.75 * _offset;
Brian Silvermande8fd552013-11-03 15:53:42 -080060 //const double angle_error = (_right_goal - _left_goal) / width - (_raw_right - _offset - _raw_left - _offset) / width;
Austin Schuh4352ac62013-03-19 06:23:16 +000061 // TODO(aschuh): Add in the gyro.
62 _integral_offset = 0.0;
63 _offset = 0.0;
brians343bc112013-02-10 01:53:46 +000064 _gyro = gyro;
Austin Schuh4352ac62013-03-19 06:23:16 +000065 _control_loop_driving = control_loop_driving;
brians343bc112013-02-10 01:53:46 +000066 SetRawPosition(left, right);
brians343bc112013-02-10 01:53:46 +000067 }
Austin Schuh4352ac62013-03-19 06:23:16 +000068
Ben Fredrickson890c3fe2014-03-02 00:15:16 +000069 void SetExternalMotors(double left_voltage, double right_voltage) {
70 loop_->U << left_voltage, right_voltage;
71 }
72
Austin Schuhf9286cd2014-02-11 00:51:09 -080073 void Update(bool stop_motors) {
Ben Fredrickson890c3fe2014-03-02 00:15:16 +000074 if (_control_loop_driving) {
75 loop_->Update(stop_motors);
76 } else {
77 if (stop_motors) {
78 loop_->U.setZero();
79 loop_->U_uncapped.setZero();
80 }
81 loop_->UpdateObserver();
82 }
Brian Silverman3146b642014-03-20 14:52:46 -070083 ::Eigen::Matrix<double, 4, 1> E = loop_->R - loop_->X_hat;
84 LOG_MATRIX(DEBUG, "E", E);
Ben Fredrickson890c3fe2014-03-02 00:15:16 +000085 }
86
87 double GetEstimatedRobotSpeed() {
88 // lets just call the average of left and right velocities close enough
89 return (loop_->X_hat(1, 0) + loop_->X_hat(3, 0)) / 2;
90 }
91
92 double GetEstimatedLeftEncoder() {
93 // lets just call the average of left and right velocities close enough
94 return loop_->X_hat(0, 0);
95 }
96
97 double GetEstimatedRightEncoder() {
98 return loop_->X_hat(2, 0);
brians343bc112013-02-10 01:53:46 +000099 }
100
Austin Schuh4352ac62013-03-19 06:23:16 +0000101 void SendMotors(Drivetrain::Output *output) {
102 if (output) {
103 output->left_voltage = loop_->U(0, 0);
104 output->right_voltage = loop_->U(1, 0);
brians8ad74052013-03-16 21:04:51 +0000105 }
brians343bc112013-02-10 01:53:46 +0000106 }
brians343bc112013-02-10 01:53:46 +0000107
108 private:
Austin Schuh4352ac62013-03-19 06:23:16 +0000109 ::std::unique_ptr<StateFeedbackLoop<4, 2, 2>> loop_;
110
brians343bc112013-02-10 01:53:46 +0000111 double _integral_offset;
112 double _offset;
113 double _gyro;
114 double _left_goal;
115 double _right_goal;
116 double _raw_left;
117 double _raw_right;
Austin Schuh4352ac62013-03-19 06:23:16 +0000118 bool _control_loop_driving;
brians343bc112013-02-10 01:53:46 +0000119};
120
Austin Schuh2054f5f2013-10-27 14:54:10 -0700121class PolyDrivetrain {
122 public:
Austin Schuh427b3702013-11-02 13:44:09 -0700123
124 enum Gear {
125 HIGH,
126 LOW,
127 SHIFTING_UP,
128 SHIFTING_DOWN
129 };
130 // Stall Torque in N m
131 static constexpr double kStallTorque = 2.42;
132 // Stall Current in Amps
133 static constexpr double kStallCurrent = 133;
134 // Free Speed in RPM. Used number from last year.
135 static constexpr double kFreeSpeed = 4650.0;
136 // Free Current in Amps
137 static constexpr double kFreeCurrent = 2.7;
138 // Moment of inertia of the drivetrain in kg m^2
139 // Just borrowed from last year.
140 static constexpr double J = 6.4;
141 // Mass of the robot, in kg.
142 static constexpr double m = 68;
143 // Radius of the robot, in meters (from last year).
144 static constexpr double rb = 0.617998644 / 2.0;
Brian Silverman1a6590d2013-11-04 14:46:46 -0800145 static constexpr double kWheelRadius = 0.04445;
Austin Schuh427b3702013-11-02 13:44:09 -0700146 // Resistance of the motor, divided by the number of motors.
147 static constexpr double kR = (12.0 / kStallCurrent / 4 + 0.03) / (0.93 * 0.93);
148 // Motor velocity constant
149 static constexpr double Kv =
150 ((kFreeSpeed / 60.0 * 2.0 * M_PI) / (12.0 - kR * kFreeCurrent));
151 // Torque constant
152 static constexpr double Kt = kStallTorque / kStallCurrent;
Austin Schuh427b3702013-11-02 13:44:09 -0700153
Austin Schuh2054f5f2013-10-27 14:54:10 -0700154 PolyDrivetrain()
155 : U_Poly_((Eigen::Matrix<double, 4, 2>() << /*[[*/ 1, 0 /*]*/,
156 /*[*/ -1, 0 /*]*/,
157 /*[*/ 0, 1 /*]*/,
158 /*[*/ 0, -1 /*]]*/).finished(),
159 (Eigen::Matrix<double, 4, 1>() << /*[[*/ 12 /*]*/,
160 /*[*/ 12 /*]*/,
161 /*[*/ 12 /*]*/,
162 /*[*/ 12 /*]]*/).finished()),
Brian Silverman2c590c32013-11-04 18:08:54 -0800163 loop_(new StateFeedbackLoop<2, 2, 2>(
164 constants::GetValues().make_v_drivetrain_loop())),
Austin Schuh427b3702013-11-02 13:44:09 -0700165 ttrust_(1.1),
166 wheel_(0.0),
167 throttle_(0.0),
168 quickturn_(false),
169 stale_count_(0),
170 position_time_delta_(0.01),
171 left_gear_(LOW),
Brian Silvermande8fd552013-11-03 15:53:42 -0800172 right_gear_(LOW),
173 counter_(0) {
Austin Schuh2054f5f2013-10-27 14:54:10 -0700174
Austin Schuh427b3702013-11-02 13:44:09 -0700175 last_position_.Zero();
176 position_.Zero();
Austin Schuh2054f5f2013-10-27 14:54:10 -0700177 }
Austin Schuh427b3702013-11-02 13:44:09 -0700178 static bool IsInGear(Gear gear) { return gear == LOW || gear == HIGH; }
179
Austin Schuh78d55462014-02-23 01:39:30 -0800180 static double MotorSpeed(const constants::ShifterHallEffect &hall_effect,
181 double shifter_position, double velocity) {
Austin Schuh427b3702013-11-02 13:44:09 -0700182 // TODO(austin): G_high, G_low and kWheelRadius
Austin Schuh78d55462014-02-23 01:39:30 -0800183 const double avg_hall_effect =
184 (hall_effect.clear_high + hall_effect.clear_low) / 2.0;
185
186 if (shifter_position > avg_hall_effect) {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800187 return velocity / constants::GetValues().high_gear_ratio / kWheelRadius;
Austin Schuh427b3702013-11-02 13:44:09 -0700188 } else {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800189 return velocity / constants::GetValues().low_gear_ratio / kWheelRadius;
190 }
191 }
192
Austin Schuh78d55462014-02-23 01:39:30 -0800193 Gear ComputeGear(const constants::ShifterHallEffect &hall_effect,
194 double velocity, Gear current) {
195 const double low_omega = MotorSpeed(hall_effect, 0.0, ::std::abs(velocity));
196 const double high_omega =
197 MotorSpeed(hall_effect, 1.0, ::std::abs(velocity));
Brian Silverman1a6590d2013-11-04 14:46:46 -0800198
Brian Silverman1a6590d2013-11-04 14:46:46 -0800199 double high_torque = ((12.0 - high_omega / Kv) * Kt / kR);
200 double low_torque = ((12.0 - low_omega / Kv) * Kt / kR);
201 double high_power = high_torque * high_omega;
202 double low_power = low_torque * low_omega;
Brian Silverman55a930b2013-11-04 20:59:00 -0800203
204 // TODO(aschuh): Do this right!
205 if ((current == HIGH || high_power > low_power + 160) &&
206 ::std::abs(velocity) > 0.14) {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800207 return HIGH;
208 } else {
209 return LOW;
Austin Schuh427b3702013-11-02 13:44:09 -0700210 }
211 }
212
Austin Schuh2054f5f2013-10-27 14:54:10 -0700213 void SetGoal(double wheel, double throttle, bool quickturn, bool highgear) {
Brian Silvermande8fd552013-11-03 15:53:42 -0800214 const double kWheelNonLinearity = 0.3;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700215 // Apply a sin function that's scaled to make it feel better.
216 const double angular_range = M_PI_2 * kWheelNonLinearity;
217 wheel_ = sin(angular_range * wheel) / sin(angular_range);
218 wheel_ = sin(angular_range * wheel_) / sin(angular_range);
Austin Schuh2054f5f2013-10-27 14:54:10 -0700219 quickturn_ = quickturn;
Austin Schuh427b3702013-11-02 13:44:09 -0700220
Brian Silverman1a6590d2013-11-04 14:46:46 -0800221 static const double kThrottleDeadband = 0.05;
222 if (::std::abs(throttle) < kThrottleDeadband) {
223 throttle_ = 0;
224 } else {
225 throttle_ = copysign((::std::abs(throttle) - kThrottleDeadband) /
226 (1.0 - kThrottleDeadband), throttle);
227 }
228
Austin Schuh427b3702013-11-02 13:44:09 -0700229 // TODO(austin): Fix the upshift logic to include states.
Brian Silverman1a6590d2013-11-04 14:46:46 -0800230 Gear requested_gear;
Brian Silverman55a930b2013-11-04 20:59:00 -0800231 if (false) {
Austin Schuh78d55462014-02-23 01:39:30 -0800232 const auto &values = constants::GetValues();
Brian Silverman1a6590d2013-11-04 14:46:46 -0800233 const double current_left_velocity =
234 (position_.left_encoder - last_position_.left_encoder) /
235 position_time_delta_;
236 const double current_right_velocity =
237 (position_.right_encoder - last_position_.right_encoder) /
238 position_time_delta_;
239
Austin Schuh78d55462014-02-23 01:39:30 -0800240 Gear left_requested =
241 ComputeGear(values.left_drive, current_left_velocity, left_gear_);
242 Gear right_requested =
243 ComputeGear(values.right_drive, current_right_velocity, right_gear_);
Brian Silverman1a6590d2013-11-04 14:46:46 -0800244 requested_gear =
245 (left_requested == HIGH || right_requested == HIGH) ? HIGH : LOW;
246 } else {
247 requested_gear = highgear ? HIGH : LOW;
248 }
249
250 const Gear shift_up =
251 constants::GetValues().clutch_transmission ? HIGH : SHIFTING_UP;
252 const Gear shift_down =
253 constants::GetValues().clutch_transmission ? LOW : SHIFTING_DOWN;
Austin Schuh427b3702013-11-02 13:44:09 -0700254
255 if (left_gear_ != requested_gear) {
256 if (IsInGear(left_gear_)) {
257 if (requested_gear == HIGH) {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800258 left_gear_ = shift_up;
Austin Schuh427b3702013-11-02 13:44:09 -0700259 } else {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800260 left_gear_ = shift_down;
Austin Schuh427b3702013-11-02 13:44:09 -0700261 }
Brian Silverman55a930b2013-11-04 20:59:00 -0800262 } else {
263 if (requested_gear == HIGH && left_gear_ == SHIFTING_DOWN) {
264 left_gear_ = SHIFTING_UP;
265 } else if (requested_gear == LOW && left_gear_ == SHIFTING_UP) {
266 left_gear_ = SHIFTING_DOWN;
267 }
Austin Schuh427b3702013-11-02 13:44:09 -0700268 }
269 }
270 if (right_gear_ != requested_gear) {
271 if (IsInGear(right_gear_)) {
272 if (requested_gear == HIGH) {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800273 right_gear_ = shift_up;
Austin Schuh427b3702013-11-02 13:44:09 -0700274 } else {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800275 right_gear_ = shift_down;
Austin Schuh427b3702013-11-02 13:44:09 -0700276 }
Brian Silverman55a930b2013-11-04 20:59:00 -0800277 } else {
278 if (requested_gear == HIGH && right_gear_ == SHIFTING_DOWN) {
279 right_gear_ = SHIFTING_UP;
280 } else if (requested_gear == LOW && right_gear_ == SHIFTING_UP) {
281 right_gear_ = SHIFTING_DOWN;
282 }
Austin Schuh427b3702013-11-02 13:44:09 -0700283 }
Austin Schuh2054f5f2013-10-27 14:54:10 -0700284 }
285 }
Austin Schuh427b3702013-11-02 13:44:09 -0700286 void SetPosition(const Drivetrain::Position *position) {
Austin Schuh78d55462014-02-23 01:39:30 -0800287 const auto &values = constants::GetValues();
Austin Schuh427b3702013-11-02 13:44:09 -0700288 if (position == NULL) {
289 ++stale_count_;
290 } else {
291 last_position_ = position_;
292 position_ = *position;
293 position_time_delta_ = (stale_count_ + 1) * 0.01;
294 stale_count_ = 0;
295 }
296
297 if (position) {
Brian Silverman61e41fd2014-02-16 19:08:50 -0800298 GearLogging gear_logging;
Austin Schuh427b3702013-11-02 13:44:09 -0700299 // Switch to the correct controller.
Austin Schuh78d55462014-02-23 01:39:30 -0800300 const double left_middle_shifter_position =
301 (values.left_drive.clear_high + values.left_drive.clear_low) / 2.0;
302 const double right_middle_shifter_position =
303 (values.right_drive.clear_high + values.right_drive.clear_low) / 2.0;
304
305 if (position->left_shifter_position < left_middle_shifter_position) {
306 if (position->right_shifter_position < right_middle_shifter_position ||
307 right_gear_ == LOW) {
Brian Silverman61e41fd2014-02-16 19:08:50 -0800308 gear_logging.left_loop_high = false;
309 gear_logging.right_loop_high = false;
310 loop_->set_controller_index(gear_logging.controller_index = 0);
Austin Schuh427b3702013-11-02 13:44:09 -0700311 } else {
Brian Silverman61e41fd2014-02-16 19:08:50 -0800312 gear_logging.left_loop_high = false;
313 gear_logging.right_loop_high = true;
314 loop_->set_controller_index(gear_logging.controller_index = 1);
Austin Schuh427b3702013-11-02 13:44:09 -0700315 }
316 } else {
Austin Schuh78d55462014-02-23 01:39:30 -0800317 if (position->right_shifter_position < right_middle_shifter_position ||
318 left_gear_ == LOW) {
Brian Silverman61e41fd2014-02-16 19:08:50 -0800319 gear_logging.left_loop_high = true;
320 gear_logging.right_loop_high = false;
321 loop_->set_controller_index(gear_logging.controller_index = 2);
Austin Schuh427b3702013-11-02 13:44:09 -0700322 } else {
Brian Silverman61e41fd2014-02-16 19:08:50 -0800323 gear_logging.left_loop_high = true;
324 gear_logging.right_loop_high = true;
325 loop_->set_controller_index(gear_logging.controller_index = 3);
Austin Schuh427b3702013-11-02 13:44:09 -0700326 }
327 }
Brian Silverman61e41fd2014-02-16 19:08:50 -0800328
Austin Schuh427b3702013-11-02 13:44:09 -0700329 // TODO(austin): Constants.
Austin Schuh78d55462014-02-23 01:39:30 -0800330 if (position->left_shifter_position > values.left_drive.clear_high && left_gear_ == SHIFTING_UP) {
Austin Schuh427b3702013-11-02 13:44:09 -0700331 left_gear_ = HIGH;
332 }
Austin Schuh78d55462014-02-23 01:39:30 -0800333 if (position->left_shifter_position < values.left_drive.clear_low && left_gear_ == SHIFTING_DOWN) {
Austin Schuh427b3702013-11-02 13:44:09 -0700334 left_gear_ = LOW;
335 }
Austin Schuh78d55462014-02-23 01:39:30 -0800336 if (position->right_shifter_position > values.right_drive.clear_high && right_gear_ == SHIFTING_UP) {
Austin Schuh427b3702013-11-02 13:44:09 -0700337 right_gear_ = HIGH;
338 }
Austin Schuh78d55462014-02-23 01:39:30 -0800339 if (position->right_shifter_position < values.right_drive.clear_low && right_gear_ == SHIFTING_DOWN) {
Austin Schuh427b3702013-11-02 13:44:09 -0700340 right_gear_ = LOW;
341 }
Brian Silverman61e41fd2014-02-16 19:08:50 -0800342
343 gear_logging.left_state = left_gear_;
344 gear_logging.right_state = right_gear_;
345 LOG_STRUCT(DEBUG, "state", gear_logging);
Austin Schuh427b3702013-11-02 13:44:09 -0700346 }
347 }
348
Austin Schuh2054f5f2013-10-27 14:54:10 -0700349 double FilterVelocity(double throttle) {
350 const Eigen::Matrix<double, 2, 2> FF =
351 loop_->B().inverse() *
352 (Eigen::Matrix<double, 2, 2>::Identity() - loop_->A());
353
354 constexpr int kHighGearController = 3;
355 const Eigen::Matrix<double, 2, 2> FF_high =
356 loop_->controller(kHighGearController).plant.B.inverse() *
357 (Eigen::Matrix<double, 2, 2>::Identity() -
358 loop_->controller(kHighGearController).plant.A);
359
360 ::Eigen::Matrix<double, 1, 2> FF_sum = FF.colwise().sum();
361 int min_FF_sum_index;
362 const double min_FF_sum = FF_sum.minCoeff(&min_FF_sum_index);
363 const double min_K_sum = loop_->K().col(min_FF_sum_index).sum();
364 const double high_min_FF_sum = FF_high.col(0).sum();
365
366 const double adjusted_ff_voltage = ::aos::Clip(
367 throttle * 12.0 * min_FF_sum / high_min_FF_sum, -12.0, 12.0);
368 return ((adjusted_ff_voltage +
369 ttrust_ * min_K_sum * (loop_->X_hat(0, 0) + loop_->X_hat(1, 0)) /
370 2.0) /
371 (ttrust_ * min_K_sum + min_FF_sum));
372 }
373
Brian Silverman718b1d72013-10-28 16:22:45 -0700374 double MaxVelocity() {
375 const Eigen::Matrix<double, 2, 2> FF =
376 loop_->B().inverse() *
377 (Eigen::Matrix<double, 2, 2>::Identity() - loop_->A());
378
379 constexpr int kHighGearController = 3;
380 const Eigen::Matrix<double, 2, 2> FF_high =
381 loop_->controller(kHighGearController).plant.B.inverse() *
382 (Eigen::Matrix<double, 2, 2>::Identity() -
383 loop_->controller(kHighGearController).plant.A);
384
385 ::Eigen::Matrix<double, 1, 2> FF_sum = FF.colwise().sum();
386 int min_FF_sum_index;
387 const double min_FF_sum = FF_sum.minCoeff(&min_FF_sum_index);
388 //const double min_K_sum = loop_->K().col(min_FF_sum_index).sum();
389 const double high_min_FF_sum = FF_high.col(0).sum();
390
391 const double adjusted_ff_voltage = ::aos::Clip(
392 12.0 * min_FF_sum / high_min_FF_sum, -12.0, 12.0);
393 return adjusted_ff_voltage / min_FF_sum;
394 }
395
Austin Schuh2054f5f2013-10-27 14:54:10 -0700396 void Update() {
Austin Schuh78d55462014-02-23 01:39:30 -0800397 const auto &values = constants::GetValues();
Austin Schuh427b3702013-11-02 13:44:09 -0700398 // TODO(austin): Observer for the current velocity instead of difference
399 // calculations.
Brian Silvermande8fd552013-11-03 15:53:42 -0800400 ++counter_;
Austin Schuh427b3702013-11-02 13:44:09 -0700401 const double current_left_velocity =
Brian Silvermande8fd552013-11-03 15:53:42 -0800402 (position_.left_encoder - last_position_.left_encoder) /
Austin Schuh427b3702013-11-02 13:44:09 -0700403 position_time_delta_;
404 const double current_right_velocity =
Brian Silvermande8fd552013-11-03 15:53:42 -0800405 (position_.right_encoder - last_position_.right_encoder) /
Austin Schuh427b3702013-11-02 13:44:09 -0700406 position_time_delta_;
407 const double left_motor_speed =
Austin Schuh78d55462014-02-23 01:39:30 -0800408 MotorSpeed(values.left_drive, position_.left_shifter_position,
409 current_left_velocity);
Austin Schuh427b3702013-11-02 13:44:09 -0700410 const double right_motor_speed =
Austin Schuh78d55462014-02-23 01:39:30 -0800411 MotorSpeed(values.right_drive, position_.right_shifter_position,
412 current_right_velocity);
Austin Schuh2054f5f2013-10-27 14:54:10 -0700413
Brian Silverman61e41fd2014-02-16 19:08:50 -0800414 {
415 CIMLogging logging;
416
417 // Reset the CIM model to the current conditions to be ready for when we
418 // shift.
419 if (IsInGear(left_gear_)) {
420 logging.left_in_gear = true;
Brian Silverman61e41fd2014-02-16 19:08:50 -0800421 } else {
422 logging.left_in_gear = false;
423 }
424 logging.left_motor_speed = left_motor_speed;
425 logging.left_velocity = current_left_velocity;
426 if (IsInGear(right_gear_)) {
427 logging.right_in_gear = true;
Brian Silverman61e41fd2014-02-16 19:08:50 -0800428 } else {
429 logging.right_in_gear = false;
430 }
431 logging.right_motor_speed = right_motor_speed;
432 logging.right_velocity = current_right_velocity;
433
434 LOG_STRUCT(DEBUG, "currently", logging);
Austin Schuh427b3702013-11-02 13:44:09 -0700435 }
Austin Schuh2054f5f2013-10-27 14:54:10 -0700436
Austin Schuh427b3702013-11-02 13:44:09 -0700437 if (IsInGear(left_gear_) && IsInGear(right_gear_)) {
438 // FF * X = U (steady state)
439 const Eigen::Matrix<double, 2, 2> FF =
440 loop_->B().inverse() *
441 (Eigen::Matrix<double, 2, 2>::Identity() - loop_->A());
442
443 // Invert the plant to figure out how the velocity filter would have to
444 // work
445 // out in order to filter out the forwards negative inertia.
446 // This math assumes that the left and right power and velocity are
447 // equals,
448 // and that the plant is the same on the left and right.
449 const double fvel = FilterVelocity(throttle_);
450
451 const double sign_svel = wheel_ * ((fvel > 0.0) ? 1.0 : -1.0);
452 double steering_velocity;
453 if (quickturn_) {
454 steering_velocity = wheel_ * MaxVelocity();
455 } else {
456 steering_velocity = ::std::abs(fvel) * wheel_;
457 }
458 const double left_velocity = fvel - steering_velocity;
459 const double right_velocity = fvel + steering_velocity;
460
461 // Integrate velocity to get the position.
462 // This position is used to get integral control.
463 loop_->R << left_velocity, right_velocity;
464
465 if (!quickturn_) {
466 // K * R = w
467 Eigen::Matrix<double, 1, 2> equality_k;
468 equality_k << 1 + sign_svel, -(1 - sign_svel);
469 const double equality_w = 0.0;
470
471 // Construct a constraint on R by manipulating the constraint on U
472 ::aos::controls::HPolytope<2> R_poly = ::aos::controls::HPolytope<2>(
473 U_Poly_.H() * (loop_->K() + FF),
474 U_Poly_.k() + U_Poly_.H() * loop_->K() * loop_->X_hat);
475
476 // Limit R back inside the box.
477 loop_->R = CoerceGoal(R_poly, equality_k, equality_w, loop_->R);
478 }
479
480 const Eigen::Matrix<double, 2, 1> FF_volts = FF * loop_->R;
481 const Eigen::Matrix<double, 2, 1> U_ideal =
482 loop_->K() * (loop_->R - loop_->X_hat) + FF_volts;
483
484 for (int i = 0; i < 2; i++) {
485 loop_->U[i] = ::aos::Clip(U_ideal[i], -12, 12);
486 }
Brian Silvermande8fd552013-11-03 15:53:42 -0800487
488 // TODO(austin): Model this better.
489 // TODO(austin): Feed back?
490 loop_->X_hat = loop_->A() * loop_->X_hat + loop_->B() * loop_->U;
Brian Silverman718b1d72013-10-28 16:22:45 -0700491 } else {
Austin Schuh427b3702013-11-02 13:44:09 -0700492 // Any motor is not in gear. Speed match.
493 ::Eigen::Matrix<double, 1, 1> R_left;
Austin Schuh427b3702013-11-02 13:44:09 -0700494 ::Eigen::Matrix<double, 1, 1> R_right;
Austin Schuhb5afb692014-03-02 11:54:11 -0800495 R_left(0, 0) = left_motor_speed;
Austin Schuh427b3702013-11-02 13:44:09 -0700496 R_right(0, 0) = right_motor_speed;
Austin Schuhb5afb692014-03-02 11:54:11 -0800497
498 const double wiggle =
Brian Silvermanf970f2c2014-03-22 19:34:30 -0700499 (static_cast<double>((counter_ % 20) / 10) - 0.5) * 5.0;
Austin Schuhb5afb692014-03-02 11:54:11 -0800500
Brian Silverman56658322014-03-22 16:57:22 -0700501 loop_->U(0, 0) = ::aos::Clip(
502 (R_left / Kv)(0, 0) + (IsInGear(left_gear_) ? 0 : wiggle),
503 -12.0, 12.0);
504 loop_->U(1, 0) = ::aos::Clip(
505 (R_right / Kv)(0, 0) + (IsInGear(right_gear_) ? 0 : wiggle),
506 -12.0, 12.0);
Brian Silvermande8fd552013-11-03 15:53:42 -0800507 loop_->U *= 12.0 / position_.battery_voltage;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700508 }
Austin Schuh2054f5f2013-10-27 14:54:10 -0700509 }
510
511 void SendMotors(Drivetrain::Output *output) {
Brian Silvermande8fd552013-11-03 15:53:42 -0800512 if (output != NULL) {
513 output->left_voltage = loop_->U(0, 0);
514 output->right_voltage = loop_->U(1, 0);
Brian Silverman61e41fd2014-02-16 19:08:50 -0800515 output->left_high = left_gear_ == HIGH || left_gear_ == SHIFTING_UP;
516 output->right_high = right_gear_ == HIGH || right_gear_ == SHIFTING_UP;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700517 }
518 }
519
520 private:
521 const ::aos::controls::HPolytope<2> U_Poly_;
522
523 ::std::unique_ptr<StateFeedbackLoop<2, 2, 2>> loop_;
524
Austin Schuh427b3702013-11-02 13:44:09 -0700525 const double ttrust_;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700526 double wheel_;
527 double throttle_;
528 bool quickturn_;
Austin Schuh427b3702013-11-02 13:44:09 -0700529 int stale_count_;
530 double position_time_delta_;
531 Gear left_gear_;
532 Gear right_gear_;
533 Drivetrain::Position last_position_;
534 Drivetrain::Position position_;
Brian Silvermande8fd552013-11-03 15:53:42 -0800535 int counter_;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700536};
Brian Silvermancec3c8d2013-12-20 21:04:55 -0800537constexpr double PolyDrivetrain::kStallTorque;
538constexpr double PolyDrivetrain::kStallCurrent;
539constexpr double PolyDrivetrain::kFreeSpeed;
540constexpr double PolyDrivetrain::kFreeCurrent;
541constexpr double PolyDrivetrain::J;
542constexpr double PolyDrivetrain::m;
543constexpr double PolyDrivetrain::rb;
544constexpr double PolyDrivetrain::kWheelRadius;
545constexpr double PolyDrivetrain::kR;
546constexpr double PolyDrivetrain::Kv;
547constexpr double PolyDrivetrain::Kt;
548
Austin Schuh2054f5f2013-10-27 14:54:10 -0700549
brians343bc112013-02-10 01:53:46 +0000550void DrivetrainLoop::RunIteration(const Drivetrain::Goal *goal,
551 const Drivetrain::Position *position,
552 Drivetrain::Output *output,
Ben Fredrickson890c3fe2014-03-02 00:15:16 +0000553 Drivetrain::Status * status) {
brians343bc112013-02-10 01:53:46 +0000554 // TODO(aschuh): These should be members of the class.
555 static DrivetrainMotorsSS dt_closedloop;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700556 static PolyDrivetrain dt_openloop;
brians343bc112013-02-10 01:53:46 +0000557
558 bool bad_pos = false;
Austin Schuh427b3702013-11-02 13:44:09 -0700559 if (position == nullptr) {
Brian Silverman50a9d032014-02-16 17:20:57 -0800560 LOG_INTERVAL(no_position_);
brians343bc112013-02-10 01:53:46 +0000561 bad_pos = true;
562 }
Brian Silverman9c9ab422014-02-25 13:42:53 -0800563 no_position_.Print();
brians343bc112013-02-10 01:53:46 +0000564
565 double wheel = goal->steering;
566 double throttle = goal->throttle;
567 bool quickturn = goal->quickturn;
568 bool highgear = goal->highgear;
569
570 bool control_loop_driving = goal->control_loop_driving;
571 double left_goal = goal->left_goal;
572 double right_goal = goal->right_goal;
573
Austin Schuh2054f5f2013-10-27 14:54:10 -0700574 dt_closedloop.SetGoal(left_goal, goal->left_velocity_goal, right_goal,
575 goal->right_velocity_goal);
brians343bc112013-02-10 01:53:46 +0000576 if (!bad_pos) {
577 const double left_encoder = position->left_encoder;
578 const double right_encoder = position->right_encoder;
Brian Silverman6bf0d3c2014-03-08 12:52:54 -0800579 if (gyro_reading.FetchLatest()) {
Brian Silverman74e5b4e2014-03-09 16:58:58 -0700580 LOG_STRUCT(DEBUG, "using", *gyro_reading.get());
581 dt_closedloop.SetPosition(left_encoder, right_encoder,
582 gyro_reading->angle, control_loop_driving);
brians343bc112013-02-10 01:53:46 +0000583 } else {
584 dt_closedloop.SetRawPosition(left_encoder, right_encoder);
585 }
586 }
Austin Schuh427b3702013-11-02 13:44:09 -0700587 dt_openloop.SetPosition(position);
brians343bc112013-02-10 01:53:46 +0000588 dt_openloop.SetGoal(wheel, throttle, quickturn, highgear);
589 dt_openloop.Update();
Ben Fredrickson890c3fe2014-03-02 00:15:16 +0000590
Brian Silverman2845c4c2013-03-16 19:54:08 -0700591 if (control_loop_driving) {
Ben Fredrickson890c3fe2014-03-02 00:15:16 +0000592 dt_closedloop.Update(output == NULL);
Brian Silverman2845c4c2013-03-16 19:54:08 -0700593 dt_closedloop.SendMotors(output);
594 } else {
595 dt_openloop.SendMotors(output);
Austin Schuhb5afb692014-03-02 11:54:11 -0800596 if (output) {
597 dt_closedloop.SetExternalMotors(output->left_voltage,
598 output->right_voltage);
599 }
Ben Fredrickson890c3fe2014-03-02 00:15:16 +0000600 dt_closedloop.Update(output == NULL);
brians343bc112013-02-10 01:53:46 +0000601 }
Ben Fredrickson890c3fe2014-03-02 00:15:16 +0000602
603 // set the output status of the controll loop state
604 if (status) {
605 bool done = false;
606 if (goal) {
607 done = ((::std::abs(goal->left_goal -
608 dt_closedloop.GetEstimatedLeftEncoder()) <
609 constants::GetValues().drivetrain_done_distance) &&
610 (::std::abs(goal->right_goal -
611 dt_closedloop.GetEstimatedRightEncoder()) <
612 constants::GetValues().drivetrain_done_distance));
613 }
614 status->is_done = done;
615 status->robot_speed = dt_closedloop.GetEstimatedRobotSpeed();
brians343bc112013-02-10 01:53:46 +0000616 }
617}
618
619} // namespace control_loops
620} // namespace frc971