blob: c92b45e1c936266a99600df29fd008e41bb72e40 [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.
Brian Silverman176303a2014-04-10 10:54:55 -070029const double width = 25.0 / 100.0 * 2.54;
brians343bc112013-02-10 01:53:46 +000030
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>(
Brian Silverman176303a2014-04-10 10:54:55 -070035 constants::GetValues().make_drivetrain_loop())),
36 filtered_offset_(0.0),
37 gyro_(0.0),
38 left_goal_(0.0),
39 right_goal_(0.0),
40 raw_left_(0.0),
41 raw_right_(0.0),
42 control_loop_driving_(false) {
43 // High gear on both.
44 loop_->set_controller_index(3);
brians343bc112013-02-10 01:53:46 +000045 }
Brian Silverman176303a2014-04-10 10:54:55 -070046
47 void SetGoal(double left, double left_velocity, double right,
48 double right_velocity) {
49 left_goal_ = left;
50 right_goal_ = right;
Austin Schuh4352ac62013-03-19 06:23:16 +000051 loop_->R << left, left_velocity, right, right_velocity;
brians343bc112013-02-10 01:53:46 +000052 }
53 void SetRawPosition(double left, double right) {
Brian Silverman176303a2014-04-10 10:54:55 -070054 raw_right_ = right;
55 raw_left_ = left;
Austin Schuhf9286cd2014-02-11 00:51:09 -080056 Eigen::Matrix<double, 2, 1> Y;
Brian Silverman176303a2014-04-10 10:54:55 -070057 Y << left + filtered_offset_, right - filtered_offset_;
Austin Schuhf9286cd2014-02-11 00:51:09 -080058 loop_->Correct(Y);
brians343bc112013-02-10 01:53:46 +000059 }
Austin Schuh4352ac62013-03-19 06:23:16 +000060 void SetPosition(
61 double left, double right, double gyro, bool control_loop_driving) {
brians343bc112013-02-10 01:53:46 +000062 // Decay the offset quickly because this gyro is great.
Brian Silverman176303a2014-04-10 10:54:55 -070063 const double offset = (right - left - gyro * width) / 2.0;
64 filtered_offset_ = 0.25 * offset + 0.75 * filtered_offset_;
65 gyro_ = gyro;
66 control_loop_driving_ = control_loop_driving;
brians343bc112013-02-10 01:53:46 +000067 SetRawPosition(left, right);
brians343bc112013-02-10 01:53:46 +000068 }
Austin Schuh4352ac62013-03-19 06:23:16 +000069
Ben Fredrickson890c3fe2014-03-02 00:15:16 +000070 void SetExternalMotors(double left_voltage, double right_voltage) {
71 loop_->U << left_voltage, right_voltage;
72 }
73
Austin Schuhf9286cd2014-02-11 00:51:09 -080074 void Update(bool stop_motors) {
Brian Silverman176303a2014-04-10 10:54:55 -070075 if (control_loop_driving_) {
Ben Fredrickson890c3fe2014-03-02 00:15:16 +000076 loop_->Update(stop_motors);
77 } else {
78 if (stop_motors) {
79 loop_->U.setZero();
80 loop_->U_uncapped.setZero();
81 }
82 loop_->UpdateObserver();
83 }
Brian Silverman3146b642014-03-20 14:52:46 -070084 ::Eigen::Matrix<double, 4, 1> E = loop_->R - loop_->X_hat;
85 LOG_MATRIX(DEBUG, "E", E);
Ben Fredrickson890c3fe2014-03-02 00:15:16 +000086 }
87
88 double GetEstimatedRobotSpeed() {
89 // lets just call the average of left and right velocities close enough
90 return (loop_->X_hat(1, 0) + loop_->X_hat(3, 0)) / 2;
91 }
92
93 double GetEstimatedLeftEncoder() {
94 // lets just call the average of left and right velocities close enough
95 return loop_->X_hat(0, 0);
96 }
97
98 double GetEstimatedRightEncoder() {
99 return loop_->X_hat(2, 0);
brians343bc112013-02-10 01:53:46 +0000100 }
101
Austin Schuh4352ac62013-03-19 06:23:16 +0000102 void SendMotors(Drivetrain::Output *output) {
103 if (output) {
104 output->left_voltage = loop_->U(0, 0);
105 output->right_voltage = loop_->U(1, 0);
Brian Silverman176303a2014-04-10 10:54:55 -0700106 output->left_high = true;
107 output->right_high = true;
brians8ad74052013-03-16 21:04:51 +0000108 }
brians343bc112013-02-10 01:53:46 +0000109 }
brians343bc112013-02-10 01:53:46 +0000110
111 private:
Austin Schuh4352ac62013-03-19 06:23:16 +0000112 ::std::unique_ptr<StateFeedbackLoop<4, 2, 2>> loop_;
113
Brian Silverman176303a2014-04-10 10:54:55 -0700114 double filtered_offset_;
115 double gyro_;
116 double left_goal_;
117 double right_goal_;
118 double raw_left_;
119 double raw_right_;
120 bool control_loop_driving_;
brians343bc112013-02-10 01:53:46 +0000121};
122
Austin Schuh2054f5f2013-10-27 14:54:10 -0700123class PolyDrivetrain {
124 public:
Austin Schuh427b3702013-11-02 13:44:09 -0700125
126 enum Gear {
127 HIGH,
128 LOW,
129 SHIFTING_UP,
130 SHIFTING_DOWN
131 };
132 // Stall Torque in N m
133 static constexpr double kStallTorque = 2.42;
134 // Stall Current in Amps
135 static constexpr double kStallCurrent = 133;
136 // Free Speed in RPM. Used number from last year.
137 static constexpr double kFreeSpeed = 4650.0;
138 // Free Current in Amps
139 static constexpr double kFreeCurrent = 2.7;
140 // Moment of inertia of the drivetrain in kg m^2
141 // Just borrowed from last year.
142 static constexpr double J = 6.4;
143 // Mass of the robot, in kg.
144 static constexpr double m = 68;
145 // Radius of the robot, in meters (from last year).
146 static constexpr double rb = 0.617998644 / 2.0;
Brian Silverman1a6590d2013-11-04 14:46:46 -0800147 static constexpr double kWheelRadius = 0.04445;
Austin Schuh427b3702013-11-02 13:44:09 -0700148 // Resistance of the motor, divided by the number of motors.
149 static constexpr double kR = (12.0 / kStallCurrent / 4 + 0.03) / (0.93 * 0.93);
150 // Motor velocity constant
151 static constexpr double Kv =
152 ((kFreeSpeed / 60.0 * 2.0 * M_PI) / (12.0 - kR * kFreeCurrent));
153 // Torque constant
154 static constexpr double Kt = kStallTorque / kStallCurrent;
Austin Schuh427b3702013-11-02 13:44:09 -0700155
Austin Schuh2054f5f2013-10-27 14:54:10 -0700156 PolyDrivetrain()
157 : U_Poly_((Eigen::Matrix<double, 4, 2>() << /*[[*/ 1, 0 /*]*/,
158 /*[*/ -1, 0 /*]*/,
159 /*[*/ 0, 1 /*]*/,
160 /*[*/ 0, -1 /*]]*/).finished(),
161 (Eigen::Matrix<double, 4, 1>() << /*[[*/ 12 /*]*/,
162 /*[*/ 12 /*]*/,
163 /*[*/ 12 /*]*/,
164 /*[*/ 12 /*]]*/).finished()),
Brian Silverman2c590c32013-11-04 18:08:54 -0800165 loop_(new StateFeedbackLoop<2, 2, 2>(
166 constants::GetValues().make_v_drivetrain_loop())),
Austin Schuh427b3702013-11-02 13:44:09 -0700167 ttrust_(1.1),
168 wheel_(0.0),
169 throttle_(0.0),
170 quickturn_(false),
171 stale_count_(0),
172 position_time_delta_(0.01),
173 left_gear_(LOW),
Brian Silvermande8fd552013-11-03 15:53:42 -0800174 right_gear_(LOW),
175 counter_(0) {
Austin Schuh2054f5f2013-10-27 14:54:10 -0700176
Austin Schuh427b3702013-11-02 13:44:09 -0700177 last_position_.Zero();
178 position_.Zero();
Austin Schuh2054f5f2013-10-27 14:54:10 -0700179 }
Austin Schuh427b3702013-11-02 13:44:09 -0700180 static bool IsInGear(Gear gear) { return gear == LOW || gear == HIGH; }
181
Austin Schuh78d55462014-02-23 01:39:30 -0800182 static double MotorSpeed(const constants::ShifterHallEffect &hall_effect,
183 double shifter_position, double velocity) {
Austin Schuh427b3702013-11-02 13:44:09 -0700184 // TODO(austin): G_high, G_low and kWheelRadius
Austin Schuh78d55462014-02-23 01:39:30 -0800185 const double avg_hall_effect =
186 (hall_effect.clear_high + hall_effect.clear_low) / 2.0;
187
188 if (shifter_position > avg_hall_effect) {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800189 return velocity / constants::GetValues().high_gear_ratio / kWheelRadius;
Austin Schuh427b3702013-11-02 13:44:09 -0700190 } else {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800191 return velocity / constants::GetValues().low_gear_ratio / kWheelRadius;
192 }
193 }
194
Austin Schuh78d55462014-02-23 01:39:30 -0800195 Gear ComputeGear(const constants::ShifterHallEffect &hall_effect,
196 double velocity, Gear current) {
197 const double low_omega = MotorSpeed(hall_effect, 0.0, ::std::abs(velocity));
198 const double high_omega =
199 MotorSpeed(hall_effect, 1.0, ::std::abs(velocity));
Brian Silverman1a6590d2013-11-04 14:46:46 -0800200
Brian Silverman1a6590d2013-11-04 14:46:46 -0800201 double high_torque = ((12.0 - high_omega / Kv) * Kt / kR);
202 double low_torque = ((12.0 - low_omega / Kv) * Kt / kR);
203 double high_power = high_torque * high_omega;
204 double low_power = low_torque * low_omega;
Brian Silverman55a930b2013-11-04 20:59:00 -0800205
206 // TODO(aschuh): Do this right!
207 if ((current == HIGH || high_power > low_power + 160) &&
208 ::std::abs(velocity) > 0.14) {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800209 return HIGH;
210 } else {
211 return LOW;
Austin Schuh427b3702013-11-02 13:44:09 -0700212 }
213 }
214
Austin Schuh2054f5f2013-10-27 14:54:10 -0700215 void SetGoal(double wheel, double throttle, bool quickturn, bool highgear) {
Brian Silvermande8fd552013-11-03 15:53:42 -0800216 const double kWheelNonLinearity = 0.3;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700217 // Apply a sin function that's scaled to make it feel better.
218 const double angular_range = M_PI_2 * kWheelNonLinearity;
219 wheel_ = sin(angular_range * wheel) / sin(angular_range);
220 wheel_ = sin(angular_range * wheel_) / sin(angular_range);
Austin Schuh2054f5f2013-10-27 14:54:10 -0700221 quickturn_ = quickturn;
Austin Schuh427b3702013-11-02 13:44:09 -0700222
Brian Silverman1a6590d2013-11-04 14:46:46 -0800223 static const double kThrottleDeadband = 0.05;
224 if (::std::abs(throttle) < kThrottleDeadband) {
225 throttle_ = 0;
226 } else {
227 throttle_ = copysign((::std::abs(throttle) - kThrottleDeadband) /
228 (1.0 - kThrottleDeadband), throttle);
229 }
230
Austin Schuh427b3702013-11-02 13:44:09 -0700231 // TODO(austin): Fix the upshift logic to include states.
Brian Silverman1a6590d2013-11-04 14:46:46 -0800232 Gear requested_gear;
Brian Silverman55a930b2013-11-04 20:59:00 -0800233 if (false) {
Austin Schuh78d55462014-02-23 01:39:30 -0800234 const auto &values = constants::GetValues();
Brian Silverman1a6590d2013-11-04 14:46:46 -0800235 const double current_left_velocity =
236 (position_.left_encoder - last_position_.left_encoder) /
237 position_time_delta_;
238 const double current_right_velocity =
239 (position_.right_encoder - last_position_.right_encoder) /
240 position_time_delta_;
241
Austin Schuh78d55462014-02-23 01:39:30 -0800242 Gear left_requested =
243 ComputeGear(values.left_drive, current_left_velocity, left_gear_);
244 Gear right_requested =
245 ComputeGear(values.right_drive, current_right_velocity, right_gear_);
Brian Silverman1a6590d2013-11-04 14:46:46 -0800246 requested_gear =
247 (left_requested == HIGH || right_requested == HIGH) ? HIGH : LOW;
248 } else {
249 requested_gear = highgear ? HIGH : LOW;
250 }
251
252 const Gear shift_up =
253 constants::GetValues().clutch_transmission ? HIGH : SHIFTING_UP;
254 const Gear shift_down =
255 constants::GetValues().clutch_transmission ? LOW : SHIFTING_DOWN;
Austin Schuh427b3702013-11-02 13:44:09 -0700256
257 if (left_gear_ != requested_gear) {
258 if (IsInGear(left_gear_)) {
259 if (requested_gear == HIGH) {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800260 left_gear_ = shift_up;
Austin Schuh427b3702013-11-02 13:44:09 -0700261 } else {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800262 left_gear_ = shift_down;
Austin Schuh427b3702013-11-02 13:44:09 -0700263 }
Brian Silverman55a930b2013-11-04 20:59:00 -0800264 } else {
265 if (requested_gear == HIGH && left_gear_ == SHIFTING_DOWN) {
266 left_gear_ = SHIFTING_UP;
267 } else if (requested_gear == LOW && left_gear_ == SHIFTING_UP) {
268 left_gear_ = SHIFTING_DOWN;
269 }
Austin Schuh427b3702013-11-02 13:44:09 -0700270 }
271 }
272 if (right_gear_ != requested_gear) {
273 if (IsInGear(right_gear_)) {
274 if (requested_gear == HIGH) {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800275 right_gear_ = shift_up;
Austin Schuh427b3702013-11-02 13:44:09 -0700276 } else {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800277 right_gear_ = shift_down;
Austin Schuh427b3702013-11-02 13:44:09 -0700278 }
Brian Silverman55a930b2013-11-04 20:59:00 -0800279 } else {
280 if (requested_gear == HIGH && right_gear_ == SHIFTING_DOWN) {
281 right_gear_ = SHIFTING_UP;
282 } else if (requested_gear == LOW && right_gear_ == SHIFTING_UP) {
283 right_gear_ = SHIFTING_DOWN;
284 }
Austin Schuh427b3702013-11-02 13:44:09 -0700285 }
Austin Schuh2054f5f2013-10-27 14:54:10 -0700286 }
287 }
Austin Schuh427b3702013-11-02 13:44:09 -0700288 void SetPosition(const Drivetrain::Position *position) {
Austin Schuh78d55462014-02-23 01:39:30 -0800289 const auto &values = constants::GetValues();
Austin Schuh427b3702013-11-02 13:44:09 -0700290 if (position == NULL) {
291 ++stale_count_;
292 } else {
293 last_position_ = position_;
294 position_ = *position;
295 position_time_delta_ = (stale_count_ + 1) * 0.01;
296 stale_count_ = 0;
297 }
298
299 if (position) {
Brian Silverman61e41fd2014-02-16 19:08:50 -0800300 GearLogging gear_logging;
Austin Schuh427b3702013-11-02 13:44:09 -0700301 // Switch to the correct controller.
Austin Schuh78d55462014-02-23 01:39:30 -0800302 const double left_middle_shifter_position =
303 (values.left_drive.clear_high + values.left_drive.clear_low) / 2.0;
304 const double right_middle_shifter_position =
305 (values.right_drive.clear_high + values.right_drive.clear_low) / 2.0;
306
307 if (position->left_shifter_position < left_middle_shifter_position) {
308 if (position->right_shifter_position < right_middle_shifter_position ||
309 right_gear_ == LOW) {
Brian Silverman61e41fd2014-02-16 19:08:50 -0800310 gear_logging.left_loop_high = false;
311 gear_logging.right_loop_high = false;
312 loop_->set_controller_index(gear_logging.controller_index = 0);
Austin Schuh427b3702013-11-02 13:44:09 -0700313 } else {
Brian Silverman61e41fd2014-02-16 19:08:50 -0800314 gear_logging.left_loop_high = false;
315 gear_logging.right_loop_high = true;
316 loop_->set_controller_index(gear_logging.controller_index = 1);
Austin Schuh427b3702013-11-02 13:44:09 -0700317 }
318 } else {
Austin Schuh78d55462014-02-23 01:39:30 -0800319 if (position->right_shifter_position < right_middle_shifter_position ||
320 left_gear_ == LOW) {
Brian Silverman61e41fd2014-02-16 19:08:50 -0800321 gear_logging.left_loop_high = true;
322 gear_logging.right_loop_high = false;
323 loop_->set_controller_index(gear_logging.controller_index = 2);
Austin Schuh427b3702013-11-02 13:44:09 -0700324 } else {
Brian Silverman61e41fd2014-02-16 19:08:50 -0800325 gear_logging.left_loop_high = true;
326 gear_logging.right_loop_high = true;
327 loop_->set_controller_index(gear_logging.controller_index = 3);
Austin Schuh427b3702013-11-02 13:44:09 -0700328 }
329 }
Brian Silverman61e41fd2014-02-16 19:08:50 -0800330
Austin Schuh427b3702013-11-02 13:44:09 -0700331 // TODO(austin): Constants.
Austin Schuh78d55462014-02-23 01:39:30 -0800332 if (position->left_shifter_position > values.left_drive.clear_high && left_gear_ == SHIFTING_UP) {
Austin Schuh427b3702013-11-02 13:44:09 -0700333 left_gear_ = HIGH;
334 }
Austin Schuh78d55462014-02-23 01:39:30 -0800335 if (position->left_shifter_position < values.left_drive.clear_low && left_gear_ == SHIFTING_DOWN) {
Austin Schuh427b3702013-11-02 13:44:09 -0700336 left_gear_ = LOW;
337 }
Austin Schuh78d55462014-02-23 01:39:30 -0800338 if (position->right_shifter_position > values.right_drive.clear_high && right_gear_ == SHIFTING_UP) {
Austin Schuh427b3702013-11-02 13:44:09 -0700339 right_gear_ = HIGH;
340 }
Austin Schuh78d55462014-02-23 01:39:30 -0800341 if (position->right_shifter_position < values.right_drive.clear_low && right_gear_ == SHIFTING_DOWN) {
Austin Schuh427b3702013-11-02 13:44:09 -0700342 right_gear_ = LOW;
343 }
Brian Silverman61e41fd2014-02-16 19:08:50 -0800344
345 gear_logging.left_state = left_gear_;
346 gear_logging.right_state = right_gear_;
347 LOG_STRUCT(DEBUG, "state", gear_logging);
Austin Schuh427b3702013-11-02 13:44:09 -0700348 }
349 }
350
Austin Schuh2054f5f2013-10-27 14:54:10 -0700351 double FilterVelocity(double throttle) {
352 const Eigen::Matrix<double, 2, 2> FF =
353 loop_->B().inverse() *
354 (Eigen::Matrix<double, 2, 2>::Identity() - loop_->A());
355
356 constexpr int kHighGearController = 3;
357 const Eigen::Matrix<double, 2, 2> FF_high =
358 loop_->controller(kHighGearController).plant.B.inverse() *
359 (Eigen::Matrix<double, 2, 2>::Identity() -
360 loop_->controller(kHighGearController).plant.A);
361
362 ::Eigen::Matrix<double, 1, 2> FF_sum = FF.colwise().sum();
363 int min_FF_sum_index;
364 const double min_FF_sum = FF_sum.minCoeff(&min_FF_sum_index);
365 const double min_K_sum = loop_->K().col(min_FF_sum_index).sum();
366 const double high_min_FF_sum = FF_high.col(0).sum();
367
368 const double adjusted_ff_voltage = ::aos::Clip(
369 throttle * 12.0 * min_FF_sum / high_min_FF_sum, -12.0, 12.0);
370 return ((adjusted_ff_voltage +
371 ttrust_ * min_K_sum * (loop_->X_hat(0, 0) + loop_->X_hat(1, 0)) /
372 2.0) /
373 (ttrust_ * min_K_sum + min_FF_sum));
374 }
375
Brian Silverman718b1d72013-10-28 16:22:45 -0700376 double MaxVelocity() {
377 const Eigen::Matrix<double, 2, 2> FF =
378 loop_->B().inverse() *
379 (Eigen::Matrix<double, 2, 2>::Identity() - loop_->A());
380
381 constexpr int kHighGearController = 3;
382 const Eigen::Matrix<double, 2, 2> FF_high =
383 loop_->controller(kHighGearController).plant.B.inverse() *
384 (Eigen::Matrix<double, 2, 2>::Identity() -
385 loop_->controller(kHighGearController).plant.A);
386
387 ::Eigen::Matrix<double, 1, 2> FF_sum = FF.colwise().sum();
388 int min_FF_sum_index;
389 const double min_FF_sum = FF_sum.minCoeff(&min_FF_sum_index);
390 //const double min_K_sum = loop_->K().col(min_FF_sum_index).sum();
391 const double high_min_FF_sum = FF_high.col(0).sum();
392
393 const double adjusted_ff_voltage = ::aos::Clip(
394 12.0 * min_FF_sum / high_min_FF_sum, -12.0, 12.0);
395 return adjusted_ff_voltage / min_FF_sum;
396 }
397
Austin Schuh2054f5f2013-10-27 14:54:10 -0700398 void Update() {
Austin Schuh78d55462014-02-23 01:39:30 -0800399 const auto &values = constants::GetValues();
Austin Schuh427b3702013-11-02 13:44:09 -0700400 // TODO(austin): Observer for the current velocity instead of difference
401 // calculations.
Brian Silvermande8fd552013-11-03 15:53:42 -0800402 ++counter_;
Austin Schuh427b3702013-11-02 13:44:09 -0700403 const double current_left_velocity =
Brian Silvermande8fd552013-11-03 15:53:42 -0800404 (position_.left_encoder - last_position_.left_encoder) /
Austin Schuh427b3702013-11-02 13:44:09 -0700405 position_time_delta_;
406 const double current_right_velocity =
Brian Silvermande8fd552013-11-03 15:53:42 -0800407 (position_.right_encoder - last_position_.right_encoder) /
Austin Schuh427b3702013-11-02 13:44:09 -0700408 position_time_delta_;
409 const double left_motor_speed =
Austin Schuh78d55462014-02-23 01:39:30 -0800410 MotorSpeed(values.left_drive, position_.left_shifter_position,
411 current_left_velocity);
Austin Schuh427b3702013-11-02 13:44:09 -0700412 const double right_motor_speed =
Austin Schuh78d55462014-02-23 01:39:30 -0800413 MotorSpeed(values.right_drive, position_.right_shifter_position,
414 current_right_velocity);
Austin Schuh2054f5f2013-10-27 14:54:10 -0700415
Brian Silverman61e41fd2014-02-16 19:08:50 -0800416 {
417 CIMLogging logging;
418
419 // Reset the CIM model to the current conditions to be ready for when we
420 // shift.
421 if (IsInGear(left_gear_)) {
422 logging.left_in_gear = true;
Brian Silverman61e41fd2014-02-16 19:08:50 -0800423 } else {
424 logging.left_in_gear = false;
425 }
426 logging.left_motor_speed = left_motor_speed;
427 logging.left_velocity = current_left_velocity;
428 if (IsInGear(right_gear_)) {
429 logging.right_in_gear = true;
Brian Silverman61e41fd2014-02-16 19:08:50 -0800430 } else {
431 logging.right_in_gear = false;
432 }
433 logging.right_motor_speed = right_motor_speed;
434 logging.right_velocity = current_right_velocity;
435
436 LOG_STRUCT(DEBUG, "currently", logging);
Austin Schuh427b3702013-11-02 13:44:09 -0700437 }
Austin Schuh2054f5f2013-10-27 14:54:10 -0700438
Austin Schuh427b3702013-11-02 13:44:09 -0700439 if (IsInGear(left_gear_) && IsInGear(right_gear_)) {
440 // FF * X = U (steady state)
441 const Eigen::Matrix<double, 2, 2> FF =
442 loop_->B().inverse() *
443 (Eigen::Matrix<double, 2, 2>::Identity() - loop_->A());
444
445 // Invert the plant to figure out how the velocity filter would have to
446 // work
447 // out in order to filter out the forwards negative inertia.
448 // This math assumes that the left and right power and velocity are
449 // equals,
450 // and that the plant is the same on the left and right.
451 const double fvel = FilterVelocity(throttle_);
452
453 const double sign_svel = wheel_ * ((fvel > 0.0) ? 1.0 : -1.0);
454 double steering_velocity;
455 if (quickturn_) {
456 steering_velocity = wheel_ * MaxVelocity();
457 } else {
458 steering_velocity = ::std::abs(fvel) * wheel_;
459 }
460 const double left_velocity = fvel - steering_velocity;
461 const double right_velocity = fvel + steering_velocity;
462
463 // Integrate velocity to get the position.
464 // This position is used to get integral control.
465 loop_->R << left_velocity, right_velocity;
466
467 if (!quickturn_) {
468 // K * R = w
469 Eigen::Matrix<double, 1, 2> equality_k;
470 equality_k << 1 + sign_svel, -(1 - sign_svel);
471 const double equality_w = 0.0;
472
473 // Construct a constraint on R by manipulating the constraint on U
474 ::aos::controls::HPolytope<2> R_poly = ::aos::controls::HPolytope<2>(
475 U_Poly_.H() * (loop_->K() + FF),
476 U_Poly_.k() + U_Poly_.H() * loop_->K() * loop_->X_hat);
477
478 // Limit R back inside the box.
479 loop_->R = CoerceGoal(R_poly, equality_k, equality_w, loop_->R);
480 }
481
482 const Eigen::Matrix<double, 2, 1> FF_volts = FF * loop_->R;
483 const Eigen::Matrix<double, 2, 1> U_ideal =
484 loop_->K() * (loop_->R - loop_->X_hat) + FF_volts;
485
486 for (int i = 0; i < 2; i++) {
487 loop_->U[i] = ::aos::Clip(U_ideal[i], -12, 12);
488 }
Brian Silvermande8fd552013-11-03 15:53:42 -0800489
490 // TODO(austin): Model this better.
491 // TODO(austin): Feed back?
492 loop_->X_hat = loop_->A() * loop_->X_hat + loop_->B() * loop_->U;
Brian Silverman718b1d72013-10-28 16:22:45 -0700493 } else {
Austin Schuh427b3702013-11-02 13:44:09 -0700494 // Any motor is not in gear. Speed match.
495 ::Eigen::Matrix<double, 1, 1> R_left;
Austin Schuh427b3702013-11-02 13:44:09 -0700496 ::Eigen::Matrix<double, 1, 1> R_right;
Austin Schuhb5afb692014-03-02 11:54:11 -0800497 R_left(0, 0) = left_motor_speed;
Austin Schuh427b3702013-11-02 13:44:09 -0700498 R_right(0, 0) = right_motor_speed;
Austin Schuhb5afb692014-03-02 11:54:11 -0800499
500 const double wiggle =
Brian Silvermanf970f2c2014-03-22 19:34:30 -0700501 (static_cast<double>((counter_ % 20) / 10) - 0.5) * 5.0;
Austin Schuhb5afb692014-03-02 11:54:11 -0800502
Brian Silverman56658322014-03-22 16:57:22 -0700503 loop_->U(0, 0) = ::aos::Clip(
504 (R_left / Kv)(0, 0) + (IsInGear(left_gear_) ? 0 : wiggle),
505 -12.0, 12.0);
506 loop_->U(1, 0) = ::aos::Clip(
507 (R_right / Kv)(0, 0) + (IsInGear(right_gear_) ? 0 : wiggle),
508 -12.0, 12.0);
Brian Silvermande8fd552013-11-03 15:53:42 -0800509 loop_->U *= 12.0 / position_.battery_voltage;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700510 }
Austin Schuh2054f5f2013-10-27 14:54:10 -0700511 }
512
513 void SendMotors(Drivetrain::Output *output) {
Brian Silvermande8fd552013-11-03 15:53:42 -0800514 if (output != NULL) {
515 output->left_voltage = loop_->U(0, 0);
516 output->right_voltage = loop_->U(1, 0);
Brian Silverman61e41fd2014-02-16 19:08:50 -0800517 output->left_high = left_gear_ == HIGH || left_gear_ == SHIFTING_UP;
518 output->right_high = right_gear_ == HIGH || right_gear_ == SHIFTING_UP;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700519 }
520 }
521
522 private:
523 const ::aos::controls::HPolytope<2> U_Poly_;
524
525 ::std::unique_ptr<StateFeedbackLoop<2, 2, 2>> loop_;
526
Austin Schuh427b3702013-11-02 13:44:09 -0700527 const double ttrust_;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700528 double wheel_;
529 double throttle_;
530 bool quickturn_;
Austin Schuh427b3702013-11-02 13:44:09 -0700531 int stale_count_;
532 double position_time_delta_;
533 Gear left_gear_;
534 Gear right_gear_;
535 Drivetrain::Position last_position_;
536 Drivetrain::Position position_;
Brian Silvermande8fd552013-11-03 15:53:42 -0800537 int counter_;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700538};
Brian Silvermancec3c8d2013-12-20 21:04:55 -0800539constexpr double PolyDrivetrain::kStallTorque;
540constexpr double PolyDrivetrain::kStallCurrent;
541constexpr double PolyDrivetrain::kFreeSpeed;
542constexpr double PolyDrivetrain::kFreeCurrent;
543constexpr double PolyDrivetrain::J;
544constexpr double PolyDrivetrain::m;
545constexpr double PolyDrivetrain::rb;
546constexpr double PolyDrivetrain::kWheelRadius;
547constexpr double PolyDrivetrain::kR;
548constexpr double PolyDrivetrain::Kv;
549constexpr double PolyDrivetrain::Kt;
550
Austin Schuh2054f5f2013-10-27 14:54:10 -0700551
brians343bc112013-02-10 01:53:46 +0000552void DrivetrainLoop::RunIteration(const Drivetrain::Goal *goal,
553 const Drivetrain::Position *position,
554 Drivetrain::Output *output,
Ben Fredrickson890c3fe2014-03-02 00:15:16 +0000555 Drivetrain::Status * status) {
brians343bc112013-02-10 01:53:46 +0000556 // TODO(aschuh): These should be members of the class.
557 static DrivetrainMotorsSS dt_closedloop;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700558 static PolyDrivetrain dt_openloop;
brians343bc112013-02-10 01:53:46 +0000559
560 bool bad_pos = false;
Austin Schuh427b3702013-11-02 13:44:09 -0700561 if (position == nullptr) {
Brian Silverman50a9d032014-02-16 17:20:57 -0800562 LOG_INTERVAL(no_position_);
brians343bc112013-02-10 01:53:46 +0000563 bad_pos = true;
564 }
Brian Silverman9c9ab422014-02-25 13:42:53 -0800565 no_position_.Print();
brians343bc112013-02-10 01:53:46 +0000566
567 double wheel = goal->steering;
568 double throttle = goal->throttle;
569 bool quickturn = goal->quickturn;
570 bool highgear = goal->highgear;
571
572 bool control_loop_driving = goal->control_loop_driving;
573 double left_goal = goal->left_goal;
574 double right_goal = goal->right_goal;
575
Austin Schuh2054f5f2013-10-27 14:54:10 -0700576 dt_closedloop.SetGoal(left_goal, goal->left_velocity_goal, right_goal,
577 goal->right_velocity_goal);
brians343bc112013-02-10 01:53:46 +0000578 if (!bad_pos) {
579 const double left_encoder = position->left_encoder;
580 const double right_encoder = position->right_encoder;
Brian Silverman6bf0d3c2014-03-08 12:52:54 -0800581 if (gyro_reading.FetchLatest()) {
Brian Silverman74e5b4e2014-03-09 16:58:58 -0700582 LOG_STRUCT(DEBUG, "using", *gyro_reading.get());
583 dt_closedloop.SetPosition(left_encoder, right_encoder,
584 gyro_reading->angle, control_loop_driving);
brians343bc112013-02-10 01:53:46 +0000585 } else {
586 dt_closedloop.SetRawPosition(left_encoder, right_encoder);
587 }
588 }
Austin Schuh427b3702013-11-02 13:44:09 -0700589 dt_openloop.SetPosition(position);
brians343bc112013-02-10 01:53:46 +0000590 dt_openloop.SetGoal(wheel, throttle, quickturn, highgear);
591 dt_openloop.Update();
Ben Fredrickson890c3fe2014-03-02 00:15:16 +0000592
Brian Silverman2845c4c2013-03-16 19:54:08 -0700593 if (control_loop_driving) {
Ben Fredrickson890c3fe2014-03-02 00:15:16 +0000594 dt_closedloop.Update(output == NULL);
Brian Silverman2845c4c2013-03-16 19:54:08 -0700595 dt_closedloop.SendMotors(output);
596 } else {
597 dt_openloop.SendMotors(output);
Austin Schuhb5afb692014-03-02 11:54:11 -0800598 if (output) {
599 dt_closedloop.SetExternalMotors(output->left_voltage,
600 output->right_voltage);
601 }
Ben Fredrickson890c3fe2014-03-02 00:15:16 +0000602 dt_closedloop.Update(output == NULL);
brians343bc112013-02-10 01:53:46 +0000603 }
Ben Fredrickson890c3fe2014-03-02 00:15:16 +0000604
605 // set the output status of the controll loop state
606 if (status) {
607 bool done = false;
608 if (goal) {
609 done = ((::std::abs(goal->left_goal -
610 dt_closedloop.GetEstimatedLeftEncoder()) <
611 constants::GetValues().drivetrain_done_distance) &&
612 (::std::abs(goal->right_goal -
613 dt_closedloop.GetEstimatedRightEncoder()) <
614 constants::GetValues().drivetrain_done_distance));
615 }
616 status->is_done = done;
617 status->robot_speed = dt_closedloop.GetEstimatedRobotSpeed();
brians343bc112013-02-10 01:53:46 +0000618 }
619}
620
621} // namespace control_loops
622} // namespace frc971