blob: 168f87fcb6b5c8764a5d15333971c9aa126f3e42 [file] [log] [blame]
James Kuszmaulf254c1a2013-03-10 16:31:26 -07001#include "frc971/control_loops/drivetrain/drivetrain.h"
brians343bc112013-02-10 01:53:46 +00002
3#include <stdio.h>
4#include <sched.h>
5#include <cmath>
Austin Schuh4352ac62013-03-19 06:23:16 +00006#include <memory>
Austin Schuh2054f5f2013-10-27 14:54:10 -07007#include "Eigen/Dense"
brians343bc112013-02-10 01:53:46 +00008
brians343bc112013-02-10 01:53:46 +00009#include "aos/common/logging/logging.h"
Briana6553ed2014-04-02 21:26:46 -070010#include "aos/common/controls/polytope.h"
Austin Schuh2054f5f2013-10-27 14:54:10 -070011#include "aos/common/commonmath.h"
Brian Silverman61e41fd2014-02-16 19:08:50 -080012#include "aos/common/logging/queue_logging.h"
Brian Silvermanfd5e2a32014-02-22 20:02:39 -080013#include "aos/common/logging/matrix_logging.h"
Brian Silverman61e41fd2014-02-16 19:08:50 -080014
James Kuszmaulf254c1a2013-03-10 16:31:26 -070015#include "frc971/control_loops/state_feedback_loop.h"
James Kuszmaulfb0e0ae2014-03-25 07:04:47 -070016#include "frc971/control_loops/coerce_goal.h"
Austin Schuh427b3702013-11-02 13:44:09 -070017#include "frc971/control_loops/drivetrain/polydrivetrain_cim_plant.h"
James Kuszmaulf254c1a2013-03-10 16:31:26 -070018#include "frc971/control_loops/drivetrain/drivetrain.q.h"
Brian Silverman6bf0d3c2014-03-08 12:52:54 -080019#include "frc971/queues/other_sensors.q.h"
Brian Silverman1a6590d2013-11-04 14:46:46 -080020#include "frc971/constants.h"
brians343bc112013-02-10 01:53:46 +000021
Brian Silverman6bf0d3c2014-03-08 12:52:54 -080022using frc971::sensors::gyro_reading;
brians343bc112013-02-10 01:53:46 +000023
24namespace frc971 {
25namespace control_loops {
26
27// Width of the robot.
Brian Silverman176303a2014-04-10 10:54:55 -070028const double width = 25.0 / 100.0 * 2.54;
brians343bc112013-02-10 01:53:46 +000029
Austin Schuh4352ac62013-03-19 06:23:16 +000030class DrivetrainMotorsSS {
brians343bc112013-02-10 01:53:46 +000031 public:
Brian Silverman2c590c32013-11-04 18:08:54 -080032 DrivetrainMotorsSS()
33 : loop_(new StateFeedbackLoop<4, 2, 2>(
Brian Silverman176303a2014-04-10 10:54:55 -070034 constants::GetValues().make_drivetrain_loop())),
35 filtered_offset_(0.0),
36 gyro_(0.0),
37 left_goal_(0.0),
38 right_goal_(0.0),
39 raw_left_(0.0),
40 raw_right_(0.0),
41 control_loop_driving_(false) {
42 // High gear on both.
43 loop_->set_controller_index(3);
brians343bc112013-02-10 01:53:46 +000044 }
Brian Silverman176303a2014-04-10 10:54:55 -070045
46 void SetGoal(double left, double left_velocity, double right,
47 double right_velocity) {
48 left_goal_ = left;
49 right_goal_ = right;
Austin Schuh4352ac62013-03-19 06:23:16 +000050 loop_->R << left, left_velocity, right, right_velocity;
brians343bc112013-02-10 01:53:46 +000051 }
52 void SetRawPosition(double left, double right) {
Brian Silverman176303a2014-04-10 10:54:55 -070053 raw_right_ = right;
54 raw_left_ = left;
Austin Schuhf9286cd2014-02-11 00:51:09 -080055 Eigen::Matrix<double, 2, 1> Y;
Brian Silverman176303a2014-04-10 10:54:55 -070056 Y << left + filtered_offset_, right - filtered_offset_;
Austin Schuhf9286cd2014-02-11 00:51:09 -080057 loop_->Correct(Y);
brians343bc112013-02-10 01:53:46 +000058 }
Austin Schuh4352ac62013-03-19 06:23:16 +000059 void SetPosition(
60 double left, double right, double gyro, bool control_loop_driving) {
brians343bc112013-02-10 01:53:46 +000061 // Decay the offset quickly because this gyro is great.
Brian Silverman176303a2014-04-10 10:54:55 -070062 const double offset = (right - left - gyro * width) / 2.0;
63 filtered_offset_ = 0.25 * offset + 0.75 * filtered_offset_;
64 gyro_ = gyro;
65 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) {
Brian Silverman176303a2014-04-10 10:54:55 -070074 if (control_loop_driving_) {
Ben Fredrickson890c3fe2014-03-02 00:15:16 +000075 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);
Brian Silverman176303a2014-04-10 10:54:55 -0700105 output->left_high = true;
106 output->right_high = true;
brians8ad74052013-03-16 21:04:51 +0000107 }
brians343bc112013-02-10 01:53:46 +0000108 }
brians343bc112013-02-10 01:53:46 +0000109
110 private:
Austin Schuh4352ac62013-03-19 06:23:16 +0000111 ::std::unique_ptr<StateFeedbackLoop<4, 2, 2>> loop_;
112
Brian Silverman176303a2014-04-10 10:54:55 -0700113 double filtered_offset_;
114 double gyro_;
115 double left_goal_;
116 double right_goal_;
117 double raw_left_;
118 double raw_right_;
119 bool control_loop_driving_;
brians343bc112013-02-10 01:53:46 +0000120};
121
Austin Schuh2054f5f2013-10-27 14:54:10 -0700122class PolyDrivetrain {
123 public:
Austin Schuh427b3702013-11-02 13:44:09 -0700124
125 enum Gear {
126 HIGH,
127 LOW,
128 SHIFTING_UP,
129 SHIFTING_DOWN
130 };
131 // Stall Torque in N m
132 static constexpr double kStallTorque = 2.42;
133 // Stall Current in Amps
134 static constexpr double kStallCurrent = 133;
135 // Free Speed in RPM. Used number from last year.
136 static constexpr double kFreeSpeed = 4650.0;
137 // Free Current in Amps
138 static constexpr double kFreeCurrent = 2.7;
139 // Moment of inertia of the drivetrain in kg m^2
140 // Just borrowed from last year.
141 static constexpr double J = 6.4;
142 // Mass of the robot, in kg.
143 static constexpr double m = 68;
144 // Radius of the robot, in meters (from last year).
145 static constexpr double rb = 0.617998644 / 2.0;
Brian Silverman1a6590d2013-11-04 14:46:46 -0800146 static constexpr double kWheelRadius = 0.04445;
Austin Schuh427b3702013-11-02 13:44:09 -0700147 // Resistance of the motor, divided by the number of motors.
148 static constexpr double kR = (12.0 / kStallCurrent / 4 + 0.03) / (0.93 * 0.93);
149 // Motor velocity constant
150 static constexpr double Kv =
151 ((kFreeSpeed / 60.0 * 2.0 * M_PI) / (12.0 - kR * kFreeCurrent));
152 // Torque constant
153 static constexpr double Kt = kStallTorque / kStallCurrent;
Austin Schuh427b3702013-11-02 13:44:09 -0700154
Austin Schuh2054f5f2013-10-27 14:54:10 -0700155 PolyDrivetrain()
156 : U_Poly_((Eigen::Matrix<double, 4, 2>() << /*[[*/ 1, 0 /*]*/,
157 /*[*/ -1, 0 /*]*/,
158 /*[*/ 0, 1 /*]*/,
159 /*[*/ 0, -1 /*]]*/).finished(),
160 (Eigen::Matrix<double, 4, 1>() << /*[[*/ 12 /*]*/,
161 /*[*/ 12 /*]*/,
162 /*[*/ 12 /*]*/,
163 /*[*/ 12 /*]]*/).finished()),
Brian Silverman2c590c32013-11-04 18:08:54 -0800164 loop_(new StateFeedbackLoop<2, 2, 2>(
165 constants::GetValues().make_v_drivetrain_loop())),
Austin Schuh427b3702013-11-02 13:44:09 -0700166 ttrust_(1.1),
167 wheel_(0.0),
168 throttle_(0.0),
169 quickturn_(false),
170 stale_count_(0),
171 position_time_delta_(0.01),
172 left_gear_(LOW),
Brian Silvermande8fd552013-11-03 15:53:42 -0800173 right_gear_(LOW),
174 counter_(0) {
Austin Schuh2054f5f2013-10-27 14:54:10 -0700175
Austin Schuh427b3702013-11-02 13:44:09 -0700176 last_position_.Zero();
177 position_.Zero();
Austin Schuh2054f5f2013-10-27 14:54:10 -0700178 }
Austin Schuh427b3702013-11-02 13:44:09 -0700179 static bool IsInGear(Gear gear) { return gear == LOW || gear == HIGH; }
180
Austin Schuh78d55462014-02-23 01:39:30 -0800181 static double MotorSpeed(const constants::ShifterHallEffect &hall_effect,
182 double shifter_position, double velocity) {
Austin Schuh427b3702013-11-02 13:44:09 -0700183 // TODO(austin): G_high, G_low and kWheelRadius
Austin Schuh78d55462014-02-23 01:39:30 -0800184 const double avg_hall_effect =
185 (hall_effect.clear_high + hall_effect.clear_low) / 2.0;
186
187 if (shifter_position > avg_hall_effect) {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800188 return velocity / constants::GetValues().high_gear_ratio / kWheelRadius;
Austin Schuh427b3702013-11-02 13:44:09 -0700189 } else {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800190 return velocity / constants::GetValues().low_gear_ratio / kWheelRadius;
191 }
192 }
193
Austin Schuh78d55462014-02-23 01:39:30 -0800194 Gear ComputeGear(const constants::ShifterHallEffect &hall_effect,
195 double velocity, Gear current) {
196 const double low_omega = MotorSpeed(hall_effect, 0.0, ::std::abs(velocity));
197 const double high_omega =
198 MotorSpeed(hall_effect, 1.0, ::std::abs(velocity));
Brian Silverman1a6590d2013-11-04 14:46:46 -0800199
Brian Silverman1a6590d2013-11-04 14:46:46 -0800200 double high_torque = ((12.0 - high_omega / Kv) * Kt / kR);
201 double low_torque = ((12.0 - low_omega / Kv) * Kt / kR);
202 double high_power = high_torque * high_omega;
203 double low_power = low_torque * low_omega;
Brian Silverman55a930b2013-11-04 20:59:00 -0800204
205 // TODO(aschuh): Do this right!
206 if ((current == HIGH || high_power > low_power + 160) &&
207 ::std::abs(velocity) > 0.14) {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800208 return HIGH;
209 } else {
210 return LOW;
Austin Schuh427b3702013-11-02 13:44:09 -0700211 }
212 }
213
Austin Schuh2054f5f2013-10-27 14:54:10 -0700214 void SetGoal(double wheel, double throttle, bool quickturn, bool highgear) {
Brian Silvermande8fd552013-11-03 15:53:42 -0800215 const double kWheelNonLinearity = 0.3;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700216 // Apply a sin function that's scaled to make it feel better.
217 const double angular_range = M_PI_2 * kWheelNonLinearity;
218 wheel_ = sin(angular_range * wheel) / sin(angular_range);
219 wheel_ = sin(angular_range * wheel_) / sin(angular_range);
Austin Schuh2054f5f2013-10-27 14:54:10 -0700220 quickturn_ = quickturn;
Austin Schuh427b3702013-11-02 13:44:09 -0700221
Brian Silverman1a6590d2013-11-04 14:46:46 -0800222 static const double kThrottleDeadband = 0.05;
223 if (::std::abs(throttle) < kThrottleDeadband) {
224 throttle_ = 0;
225 } else {
226 throttle_ = copysign((::std::abs(throttle) - kThrottleDeadband) /
227 (1.0 - kThrottleDeadband), throttle);
228 }
229
Austin Schuh427b3702013-11-02 13:44:09 -0700230 // TODO(austin): Fix the upshift logic to include states.
Brian Silverman1a6590d2013-11-04 14:46:46 -0800231 Gear requested_gear;
Brian Silverman55a930b2013-11-04 20:59:00 -0800232 if (false) {
Austin Schuh78d55462014-02-23 01:39:30 -0800233 const auto &values = constants::GetValues();
Brian Silverman1a6590d2013-11-04 14:46:46 -0800234 const double current_left_velocity =
235 (position_.left_encoder - last_position_.left_encoder) /
236 position_time_delta_;
237 const double current_right_velocity =
238 (position_.right_encoder - last_position_.right_encoder) /
239 position_time_delta_;
240
Austin Schuh78d55462014-02-23 01:39:30 -0800241 Gear left_requested =
242 ComputeGear(values.left_drive, current_left_velocity, left_gear_);
243 Gear right_requested =
244 ComputeGear(values.right_drive, current_right_velocity, right_gear_);
Brian Silverman1a6590d2013-11-04 14:46:46 -0800245 requested_gear =
246 (left_requested == HIGH || right_requested == HIGH) ? HIGH : LOW;
247 } else {
248 requested_gear = highgear ? HIGH : LOW;
249 }
250
251 const Gear shift_up =
252 constants::GetValues().clutch_transmission ? HIGH : SHIFTING_UP;
253 const Gear shift_down =
254 constants::GetValues().clutch_transmission ? LOW : SHIFTING_DOWN;
Austin Schuh427b3702013-11-02 13:44:09 -0700255
256 if (left_gear_ != requested_gear) {
257 if (IsInGear(left_gear_)) {
258 if (requested_gear == HIGH) {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800259 left_gear_ = shift_up;
Austin Schuh427b3702013-11-02 13:44:09 -0700260 } else {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800261 left_gear_ = shift_down;
Austin Schuh427b3702013-11-02 13:44:09 -0700262 }
Brian Silverman55a930b2013-11-04 20:59:00 -0800263 } else {
264 if (requested_gear == HIGH && left_gear_ == SHIFTING_DOWN) {
265 left_gear_ = SHIFTING_UP;
266 } else if (requested_gear == LOW && left_gear_ == SHIFTING_UP) {
267 left_gear_ = SHIFTING_DOWN;
268 }
Austin Schuh427b3702013-11-02 13:44:09 -0700269 }
270 }
271 if (right_gear_ != requested_gear) {
272 if (IsInGear(right_gear_)) {
273 if (requested_gear == HIGH) {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800274 right_gear_ = shift_up;
Austin Schuh427b3702013-11-02 13:44:09 -0700275 } else {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800276 right_gear_ = shift_down;
Austin Schuh427b3702013-11-02 13:44:09 -0700277 }
Brian Silverman55a930b2013-11-04 20:59:00 -0800278 } else {
279 if (requested_gear == HIGH && right_gear_ == SHIFTING_DOWN) {
280 right_gear_ = SHIFTING_UP;
281 } else if (requested_gear == LOW && right_gear_ == SHIFTING_UP) {
282 right_gear_ = SHIFTING_DOWN;
283 }
Austin Schuh427b3702013-11-02 13:44:09 -0700284 }
Austin Schuh2054f5f2013-10-27 14:54:10 -0700285 }
286 }
Austin Schuh427b3702013-11-02 13:44:09 -0700287 void SetPosition(const Drivetrain::Position *position) {
Austin Schuh78d55462014-02-23 01:39:30 -0800288 const auto &values = constants::GetValues();
Austin Schuh427b3702013-11-02 13:44:09 -0700289 if (position == NULL) {
290 ++stale_count_;
291 } else {
292 last_position_ = position_;
293 position_ = *position;
294 position_time_delta_ = (stale_count_ + 1) * 0.01;
295 stale_count_ = 0;
296 }
297
298 if (position) {
Brian Silverman61e41fd2014-02-16 19:08:50 -0800299 GearLogging gear_logging;
Austin Schuh427b3702013-11-02 13:44:09 -0700300 // Switch to the correct controller.
Austin Schuh78d55462014-02-23 01:39:30 -0800301 const double left_middle_shifter_position =
302 (values.left_drive.clear_high + values.left_drive.clear_low) / 2.0;
303 const double right_middle_shifter_position =
304 (values.right_drive.clear_high + values.right_drive.clear_low) / 2.0;
305
306 if (position->left_shifter_position < left_middle_shifter_position) {
307 if (position->right_shifter_position < right_middle_shifter_position ||
308 right_gear_ == LOW) {
Brian Silverman61e41fd2014-02-16 19:08:50 -0800309 gear_logging.left_loop_high = false;
310 gear_logging.right_loop_high = false;
311 loop_->set_controller_index(gear_logging.controller_index = 0);
Austin Schuh427b3702013-11-02 13:44:09 -0700312 } else {
Brian Silverman61e41fd2014-02-16 19:08:50 -0800313 gear_logging.left_loop_high = false;
314 gear_logging.right_loop_high = true;
315 loop_->set_controller_index(gear_logging.controller_index = 1);
Austin Schuh427b3702013-11-02 13:44:09 -0700316 }
317 } else {
Austin Schuh78d55462014-02-23 01:39:30 -0800318 if (position->right_shifter_position < right_middle_shifter_position ||
319 left_gear_ == LOW) {
Brian Silverman61e41fd2014-02-16 19:08:50 -0800320 gear_logging.left_loop_high = true;
321 gear_logging.right_loop_high = false;
322 loop_->set_controller_index(gear_logging.controller_index = 2);
Austin Schuh427b3702013-11-02 13:44:09 -0700323 } else {
Brian Silverman61e41fd2014-02-16 19:08:50 -0800324 gear_logging.left_loop_high = true;
325 gear_logging.right_loop_high = true;
326 loop_->set_controller_index(gear_logging.controller_index = 3);
Austin Schuh427b3702013-11-02 13:44:09 -0700327 }
328 }
Brian Silverman61e41fd2014-02-16 19:08:50 -0800329
Austin Schuh427b3702013-11-02 13:44:09 -0700330 // TODO(austin): Constants.
Austin Schuh78d55462014-02-23 01:39:30 -0800331 if (position->left_shifter_position > values.left_drive.clear_high && left_gear_ == SHIFTING_UP) {
Austin Schuh427b3702013-11-02 13:44:09 -0700332 left_gear_ = HIGH;
333 }
Austin Schuh78d55462014-02-23 01:39:30 -0800334 if (position->left_shifter_position < values.left_drive.clear_low && left_gear_ == SHIFTING_DOWN) {
Austin Schuh427b3702013-11-02 13:44:09 -0700335 left_gear_ = LOW;
336 }
Austin Schuh78d55462014-02-23 01:39:30 -0800337 if (position->right_shifter_position > values.right_drive.clear_high && right_gear_ == SHIFTING_UP) {
Austin Schuh427b3702013-11-02 13:44:09 -0700338 right_gear_ = HIGH;
339 }
Austin Schuh78d55462014-02-23 01:39:30 -0800340 if (position->right_shifter_position < values.right_drive.clear_low && right_gear_ == SHIFTING_DOWN) {
Austin Schuh427b3702013-11-02 13:44:09 -0700341 right_gear_ = LOW;
342 }
Brian Silverman61e41fd2014-02-16 19:08:50 -0800343
344 gear_logging.left_state = left_gear_;
345 gear_logging.right_state = right_gear_;
346 LOG_STRUCT(DEBUG, "state", gear_logging);
Austin Schuh427b3702013-11-02 13:44:09 -0700347 }
348 }
349
Austin Schuh2054f5f2013-10-27 14:54:10 -0700350 double FilterVelocity(double throttle) {
351 const Eigen::Matrix<double, 2, 2> FF =
352 loop_->B().inverse() *
353 (Eigen::Matrix<double, 2, 2>::Identity() - loop_->A());
354
355 constexpr int kHighGearController = 3;
356 const Eigen::Matrix<double, 2, 2> FF_high =
357 loop_->controller(kHighGearController).plant.B.inverse() *
358 (Eigen::Matrix<double, 2, 2>::Identity() -
359 loop_->controller(kHighGearController).plant.A);
360
361 ::Eigen::Matrix<double, 1, 2> FF_sum = FF.colwise().sum();
362 int min_FF_sum_index;
363 const double min_FF_sum = FF_sum.minCoeff(&min_FF_sum_index);
364 const double min_K_sum = loop_->K().col(min_FF_sum_index).sum();
365 const double high_min_FF_sum = FF_high.col(0).sum();
366
367 const double adjusted_ff_voltage = ::aos::Clip(
368 throttle * 12.0 * min_FF_sum / high_min_FF_sum, -12.0, 12.0);
369 return ((adjusted_ff_voltage +
370 ttrust_ * min_K_sum * (loop_->X_hat(0, 0) + loop_->X_hat(1, 0)) /
371 2.0) /
372 (ttrust_ * min_K_sum + min_FF_sum));
373 }
374
Brian Silverman718b1d72013-10-28 16:22:45 -0700375 double MaxVelocity() {
376 const Eigen::Matrix<double, 2, 2> FF =
377 loop_->B().inverse() *
378 (Eigen::Matrix<double, 2, 2>::Identity() - loop_->A());
379
380 constexpr int kHighGearController = 3;
381 const Eigen::Matrix<double, 2, 2> FF_high =
382 loop_->controller(kHighGearController).plant.B.inverse() *
383 (Eigen::Matrix<double, 2, 2>::Identity() -
384 loop_->controller(kHighGearController).plant.A);
385
386 ::Eigen::Matrix<double, 1, 2> FF_sum = FF.colwise().sum();
387 int min_FF_sum_index;
388 const double min_FF_sum = FF_sum.minCoeff(&min_FF_sum_index);
389 //const double min_K_sum = loop_->K().col(min_FF_sum_index).sum();
390 const double high_min_FF_sum = FF_high.col(0).sum();
391
392 const double adjusted_ff_voltage = ::aos::Clip(
393 12.0 * min_FF_sum / high_min_FF_sum, -12.0, 12.0);
394 return adjusted_ff_voltage / min_FF_sum;
395 }
396
Austin Schuh2054f5f2013-10-27 14:54:10 -0700397 void Update() {
Austin Schuh78d55462014-02-23 01:39:30 -0800398 const auto &values = constants::GetValues();
Austin Schuh427b3702013-11-02 13:44:09 -0700399 // TODO(austin): Observer for the current velocity instead of difference
400 // calculations.
Brian Silvermande8fd552013-11-03 15:53:42 -0800401 ++counter_;
Austin Schuh427b3702013-11-02 13:44:09 -0700402 const double current_left_velocity =
Brian Silvermande8fd552013-11-03 15:53:42 -0800403 (position_.left_encoder - last_position_.left_encoder) /
Austin Schuh427b3702013-11-02 13:44:09 -0700404 position_time_delta_;
405 const double current_right_velocity =
Brian Silvermande8fd552013-11-03 15:53:42 -0800406 (position_.right_encoder - last_position_.right_encoder) /
Austin Schuh427b3702013-11-02 13:44:09 -0700407 position_time_delta_;
408 const double left_motor_speed =
Austin Schuh78d55462014-02-23 01:39:30 -0800409 MotorSpeed(values.left_drive, position_.left_shifter_position,
410 current_left_velocity);
Austin Schuh427b3702013-11-02 13:44:09 -0700411 const double right_motor_speed =
Austin Schuh78d55462014-02-23 01:39:30 -0800412 MotorSpeed(values.right_drive, position_.right_shifter_position,
413 current_right_velocity);
Austin Schuh2054f5f2013-10-27 14:54:10 -0700414
Brian Silverman61e41fd2014-02-16 19:08:50 -0800415 {
416 CIMLogging logging;
417
418 // Reset the CIM model to the current conditions to be ready for when we
419 // shift.
420 if (IsInGear(left_gear_)) {
421 logging.left_in_gear = true;
Brian Silverman61e41fd2014-02-16 19:08:50 -0800422 } else {
423 logging.left_in_gear = false;
424 }
425 logging.left_motor_speed = left_motor_speed;
426 logging.left_velocity = current_left_velocity;
427 if (IsInGear(right_gear_)) {
428 logging.right_in_gear = true;
Brian Silverman61e41fd2014-02-16 19:08:50 -0800429 } else {
430 logging.right_in_gear = false;
431 }
432 logging.right_motor_speed = right_motor_speed;
433 logging.right_velocity = current_right_velocity;
434
435 LOG_STRUCT(DEBUG, "currently", logging);
Austin Schuh427b3702013-11-02 13:44:09 -0700436 }
Austin Schuh2054f5f2013-10-27 14:54:10 -0700437
Austin Schuh427b3702013-11-02 13:44:09 -0700438 if (IsInGear(left_gear_) && IsInGear(right_gear_)) {
439 // FF * X = U (steady state)
440 const Eigen::Matrix<double, 2, 2> FF =
441 loop_->B().inverse() *
442 (Eigen::Matrix<double, 2, 2>::Identity() - loop_->A());
443
444 // Invert the plant to figure out how the velocity filter would have to
445 // work
446 // out in order to filter out the forwards negative inertia.
447 // This math assumes that the left and right power and velocity are
448 // equals,
449 // and that the plant is the same on the left and right.
450 const double fvel = FilterVelocity(throttle_);
451
452 const double sign_svel = wheel_ * ((fvel > 0.0) ? 1.0 : -1.0);
453 double steering_velocity;
454 if (quickturn_) {
455 steering_velocity = wheel_ * MaxVelocity();
456 } else {
457 steering_velocity = ::std::abs(fvel) * wheel_;
458 }
459 const double left_velocity = fvel - steering_velocity;
460 const double right_velocity = fvel + steering_velocity;
461
462 // Integrate velocity to get the position.
463 // This position is used to get integral control.
464 loop_->R << left_velocity, right_velocity;
465
466 if (!quickturn_) {
467 // K * R = w
468 Eigen::Matrix<double, 1, 2> equality_k;
469 equality_k << 1 + sign_svel, -(1 - sign_svel);
470 const double equality_w = 0.0;
471
472 // Construct a constraint on R by manipulating the constraint on U
473 ::aos::controls::HPolytope<2> R_poly = ::aos::controls::HPolytope<2>(
474 U_Poly_.H() * (loop_->K() + FF),
475 U_Poly_.k() + U_Poly_.H() * loop_->K() * loop_->X_hat);
476
477 // Limit R back inside the box.
478 loop_->R = CoerceGoal(R_poly, equality_k, equality_w, loop_->R);
479 }
480
481 const Eigen::Matrix<double, 2, 1> FF_volts = FF * loop_->R;
482 const Eigen::Matrix<double, 2, 1> U_ideal =
483 loop_->K() * (loop_->R - loop_->X_hat) + FF_volts;
484
485 for (int i = 0; i < 2; i++) {
486 loop_->U[i] = ::aos::Clip(U_ideal[i], -12, 12);
487 }
Brian Silvermande8fd552013-11-03 15:53:42 -0800488
489 // TODO(austin): Model this better.
490 // TODO(austin): Feed back?
491 loop_->X_hat = loop_->A() * loop_->X_hat + loop_->B() * loop_->U;
Brian Silverman718b1d72013-10-28 16:22:45 -0700492 } else {
Austin Schuh427b3702013-11-02 13:44:09 -0700493 // Any motor is not in gear. Speed match.
494 ::Eigen::Matrix<double, 1, 1> R_left;
Austin Schuh427b3702013-11-02 13:44:09 -0700495 ::Eigen::Matrix<double, 1, 1> R_right;
Austin Schuhb5afb692014-03-02 11:54:11 -0800496 R_left(0, 0) = left_motor_speed;
Austin Schuh427b3702013-11-02 13:44:09 -0700497 R_right(0, 0) = right_motor_speed;
Austin Schuhb5afb692014-03-02 11:54:11 -0800498
499 const double wiggle =
Brian Silvermanf970f2c2014-03-22 19:34:30 -0700500 (static_cast<double>((counter_ % 20) / 10) - 0.5) * 5.0;
Austin Schuhb5afb692014-03-02 11:54:11 -0800501
Brian Silverman56658322014-03-22 16:57:22 -0700502 loop_->U(0, 0) = ::aos::Clip(
503 (R_left / Kv)(0, 0) + (IsInGear(left_gear_) ? 0 : wiggle),
504 -12.0, 12.0);
505 loop_->U(1, 0) = ::aos::Clip(
506 (R_right / Kv)(0, 0) + (IsInGear(right_gear_) ? 0 : wiggle),
507 -12.0, 12.0);
Brian Silvermande8fd552013-11-03 15:53:42 -0800508 loop_->U *= 12.0 / position_.battery_voltage;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700509 }
Austin Schuh2054f5f2013-10-27 14:54:10 -0700510 }
511
512 void SendMotors(Drivetrain::Output *output) {
Brian Silvermande8fd552013-11-03 15:53:42 -0800513 if (output != NULL) {
514 output->left_voltage = loop_->U(0, 0);
515 output->right_voltage = loop_->U(1, 0);
Brian Silverman61e41fd2014-02-16 19:08:50 -0800516 output->left_high = left_gear_ == HIGH || left_gear_ == SHIFTING_UP;
517 output->right_high = right_gear_ == HIGH || right_gear_ == SHIFTING_UP;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700518 }
519 }
520
521 private:
522 const ::aos::controls::HPolytope<2> U_Poly_;
523
524 ::std::unique_ptr<StateFeedbackLoop<2, 2, 2>> loop_;
525
Austin Schuh427b3702013-11-02 13:44:09 -0700526 const double ttrust_;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700527 double wheel_;
528 double throttle_;
529 bool quickturn_;
Austin Schuh427b3702013-11-02 13:44:09 -0700530 int stale_count_;
531 double position_time_delta_;
532 Gear left_gear_;
533 Gear right_gear_;
534 Drivetrain::Position last_position_;
535 Drivetrain::Position position_;
Brian Silvermande8fd552013-11-03 15:53:42 -0800536 int counter_;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700537};
Brian Silvermancec3c8d2013-12-20 21:04:55 -0800538constexpr double PolyDrivetrain::kStallTorque;
539constexpr double PolyDrivetrain::kStallCurrent;
540constexpr double PolyDrivetrain::kFreeSpeed;
541constexpr double PolyDrivetrain::kFreeCurrent;
542constexpr double PolyDrivetrain::J;
543constexpr double PolyDrivetrain::m;
544constexpr double PolyDrivetrain::rb;
545constexpr double PolyDrivetrain::kWheelRadius;
546constexpr double PolyDrivetrain::kR;
547constexpr double PolyDrivetrain::Kv;
548constexpr double PolyDrivetrain::Kt;
549
Austin Schuh2054f5f2013-10-27 14:54:10 -0700550
brians343bc112013-02-10 01:53:46 +0000551void DrivetrainLoop::RunIteration(const Drivetrain::Goal *goal,
552 const Drivetrain::Position *position,
553 Drivetrain::Output *output,
Ben Fredrickson890c3fe2014-03-02 00:15:16 +0000554 Drivetrain::Status * status) {
brians343bc112013-02-10 01:53:46 +0000555 // TODO(aschuh): These should be members of the class.
556 static DrivetrainMotorsSS dt_closedloop;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700557 static PolyDrivetrain dt_openloop;
brians343bc112013-02-10 01:53:46 +0000558
559 bool bad_pos = false;
Austin Schuh427b3702013-11-02 13:44:09 -0700560 if (position == nullptr) {
Brian Silverman50a9d032014-02-16 17:20:57 -0800561 LOG_INTERVAL(no_position_);
brians343bc112013-02-10 01:53:46 +0000562 bad_pos = true;
563 }
Brian Silverman9c9ab422014-02-25 13:42:53 -0800564 no_position_.Print();
brians343bc112013-02-10 01:53:46 +0000565
566 double wheel = goal->steering;
567 double throttle = goal->throttle;
568 bool quickturn = goal->quickturn;
569 bool highgear = goal->highgear;
570
571 bool control_loop_driving = goal->control_loop_driving;
572 double left_goal = goal->left_goal;
573 double right_goal = goal->right_goal;
574
Austin Schuh2054f5f2013-10-27 14:54:10 -0700575 dt_closedloop.SetGoal(left_goal, goal->left_velocity_goal, right_goal,
576 goal->right_velocity_goal);
brians343bc112013-02-10 01:53:46 +0000577 if (!bad_pos) {
578 const double left_encoder = position->left_encoder;
579 const double right_encoder = position->right_encoder;
Brian Silverman6bf0d3c2014-03-08 12:52:54 -0800580 if (gyro_reading.FetchLatest()) {
Brian Silverman74e5b4e2014-03-09 16:58:58 -0700581 LOG_STRUCT(DEBUG, "using", *gyro_reading.get());
582 dt_closedloop.SetPosition(left_encoder, right_encoder,
583 gyro_reading->angle, control_loop_driving);
brians343bc112013-02-10 01:53:46 +0000584 } else {
585 dt_closedloop.SetRawPosition(left_encoder, right_encoder);
586 }
587 }
Austin Schuh427b3702013-11-02 13:44:09 -0700588 dt_openloop.SetPosition(position);
brians343bc112013-02-10 01:53:46 +0000589 dt_openloop.SetGoal(wheel, throttle, quickturn, highgear);
590 dt_openloop.Update();
Ben Fredrickson890c3fe2014-03-02 00:15:16 +0000591
Brian Silverman2845c4c2013-03-16 19:54:08 -0700592 if (control_loop_driving) {
Ben Fredrickson890c3fe2014-03-02 00:15:16 +0000593 dt_closedloop.Update(output == NULL);
Brian Silverman2845c4c2013-03-16 19:54:08 -0700594 dt_closedloop.SendMotors(output);
595 } else {
596 dt_openloop.SendMotors(output);
Austin Schuhb5afb692014-03-02 11:54:11 -0800597 if (output) {
598 dt_closedloop.SetExternalMotors(output->left_voltage,
599 output->right_voltage);
600 }
Ben Fredrickson890c3fe2014-03-02 00:15:16 +0000601 dt_closedloop.Update(output == NULL);
brians343bc112013-02-10 01:53:46 +0000602 }
Ben Fredrickson890c3fe2014-03-02 00:15:16 +0000603
604 // set the output status of the controll loop state
605 if (status) {
606 bool done = false;
607 if (goal) {
608 done = ((::std::abs(goal->left_goal -
609 dt_closedloop.GetEstimatedLeftEncoder()) <
610 constants::GetValues().drivetrain_done_distance) &&
611 (::std::abs(goal->right_goal -
612 dt_closedloop.GetEstimatedRightEncoder()) <
613 constants::GetValues().drivetrain_done_distance));
614 }
615 status->is_done = done;
616 status->robot_speed = dt_closedloop.GetEstimatedRobotSpeed();
Austin Schuh577edf62014-04-13 10:33:05 -0700617 status->filtered_left_position = dt_closedloop.GetEstimatedLeftEncoder();
618 status->filtered_right_position = dt_closedloop.GetEstimatedRightEncoder();
brians343bc112013-02-10 01:53:46 +0000619 }
620}
621
622} // namespace control_loops
623} // namespace frc971