blob: b54949d8204642a29d8da029ac8708138575ab28 [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"
Austin Schuh2054f5f2013-10-27 14:54:10 -070011#include "aos/controls/polytope.h"
12#include "aos/common/commonmath.h"
Brian Silverman61e41fd2014-02-16 19:08:50 -080013#include "aos/common/logging/queue_logging.h"
14
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"
Austin Schuh3bb9a442014-02-02 16:01:45 -080019#include "frc971/queues/gyro_angle.q.h"
Brian Silverman1a6590d2013-11-04 14:46:46 -080020#include "frc971/constants.h"
brians343bc112013-02-10 01:53:46 +000021
22using frc971::sensors::gyro;
23
24namespace frc971 {
25namespace control_loops {
26
27// Width of the robot.
28const double width = 22.0 / 100.0 * 2.54;
29
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>(
34 constants::GetValues().make_drivetrain_loop())) {
brians343bc112013-02-10 01:53:46 +000035 _offset = 0;
36 _integral_offset = 0;
37 _left_goal = 0.0;
38 _right_goal = 0.0;
39 _raw_left = 0.0;
40 _raw_right = 0.0;
Austin Schuh4352ac62013-03-19 06:23:16 +000041 _control_loop_driving = false;
brians343bc112013-02-10 01:53:46 +000042 }
43 void SetGoal(double left, double left_velocity, double right, double right_velocity) {
44 _left_goal = left;
45 _right_goal = right;
Austin Schuh4352ac62013-03-19 06:23:16 +000046 loop_->R << left, left_velocity, right, right_velocity;
brians343bc112013-02-10 01:53:46 +000047 }
48 void SetRawPosition(double left, double right) {
49 _raw_right = right;
50 _raw_left = left;
Austin Schuhf9286cd2014-02-11 00:51:09 -080051 Eigen::Matrix<double, 2, 1> Y;
52 Y << left, right;
53 loop_->Correct(Y);
brians343bc112013-02-10 01:53:46 +000054 }
Austin Schuh4352ac62013-03-19 06:23:16 +000055 void SetPosition(
56 double left, double right, double gyro, bool control_loop_driving) {
brians343bc112013-02-10 01:53:46 +000057 // Decay the offset quickly because this gyro is great.
58 _offset = (0.25) * (right - left - gyro * width) / 2.0 + 0.75 * _offset;
Brian Silvermande8fd552013-11-03 15:53:42 -080059 //const double angle_error = (_right_goal - _left_goal) / width - (_raw_right - _offset - _raw_left - _offset) / width;
Austin Schuh4352ac62013-03-19 06:23:16 +000060 // TODO(aschuh): Add in the gyro.
61 _integral_offset = 0.0;
62 _offset = 0.0;
brians343bc112013-02-10 01:53:46 +000063 _gyro = gyro;
Austin Schuh4352ac62013-03-19 06:23:16 +000064 _control_loop_driving = control_loop_driving;
brians343bc112013-02-10 01:53:46 +000065 SetRawPosition(left, right);
brians343bc112013-02-10 01:53:46 +000066 }
Austin Schuh4352ac62013-03-19 06:23:16 +000067
Austin Schuhf9286cd2014-02-11 00:51:09 -080068 void Update(bool stop_motors) {
69 loop_->Update(stop_motors);
brians343bc112013-02-10 01:53:46 +000070 }
71
Austin Schuh4352ac62013-03-19 06:23:16 +000072 void SendMotors(Drivetrain::Output *output) {
73 if (output) {
74 output->left_voltage = loop_->U(0, 0);
75 output->right_voltage = loop_->U(1, 0);
brians8ad74052013-03-16 21:04:51 +000076 }
brians343bc112013-02-10 01:53:46 +000077 }
78 void PrintMotors() const {
Austin Schuh4352ac62013-03-19 06:23:16 +000079 ::Eigen::Matrix<double, 4, 1> E = loop_->R - loop_->X_hat;
80 LOG(DEBUG, "E[0, 0]: %f E[1, 0] %f E[2, 0] %f E[3, 0] %f\n", E(0, 0), E(1, 0), E(2, 0), E(3, 0));
brians343bc112013-02-10 01:53:46 +000081 }
82
83 private:
Austin Schuh4352ac62013-03-19 06:23:16 +000084 ::std::unique_ptr<StateFeedbackLoop<4, 2, 2>> loop_;
85
brians343bc112013-02-10 01:53:46 +000086 double _integral_offset;
87 double _offset;
88 double _gyro;
89 double _left_goal;
90 double _right_goal;
91 double _raw_left;
92 double _raw_right;
Austin Schuh4352ac62013-03-19 06:23:16 +000093 bool _control_loop_driving;
brians343bc112013-02-10 01:53:46 +000094};
95
Austin Schuh2054f5f2013-10-27 14:54:10 -070096class PolyDrivetrain {
97 public:
Austin Schuh427b3702013-11-02 13:44:09 -070098
99 enum Gear {
100 HIGH,
101 LOW,
102 SHIFTING_UP,
103 SHIFTING_DOWN
104 };
105 // Stall Torque in N m
106 static constexpr double kStallTorque = 2.42;
107 // Stall Current in Amps
108 static constexpr double kStallCurrent = 133;
109 // Free Speed in RPM. Used number from last year.
110 static constexpr double kFreeSpeed = 4650.0;
111 // Free Current in Amps
112 static constexpr double kFreeCurrent = 2.7;
113 // Moment of inertia of the drivetrain in kg m^2
114 // Just borrowed from last year.
115 static constexpr double J = 6.4;
116 // Mass of the robot, in kg.
117 static constexpr double m = 68;
118 // Radius of the robot, in meters (from last year).
119 static constexpr double rb = 0.617998644 / 2.0;
Brian Silverman1a6590d2013-11-04 14:46:46 -0800120 static constexpr double kWheelRadius = 0.04445;
Austin Schuh427b3702013-11-02 13:44:09 -0700121 // Resistance of the motor, divided by the number of motors.
122 static constexpr double kR = (12.0 / kStallCurrent / 4 + 0.03) / (0.93 * 0.93);
123 // Motor velocity constant
124 static constexpr double Kv =
125 ((kFreeSpeed / 60.0 * 2.0 * M_PI) / (12.0 - kR * kFreeCurrent));
126 // Torque constant
127 static constexpr double Kt = kStallTorque / kStallCurrent;
Austin Schuh427b3702013-11-02 13:44:09 -0700128
Austin Schuh2054f5f2013-10-27 14:54:10 -0700129 PolyDrivetrain()
130 : U_Poly_((Eigen::Matrix<double, 4, 2>() << /*[[*/ 1, 0 /*]*/,
131 /*[*/ -1, 0 /*]*/,
132 /*[*/ 0, 1 /*]*/,
133 /*[*/ 0, -1 /*]]*/).finished(),
134 (Eigen::Matrix<double, 4, 1>() << /*[[*/ 12 /*]*/,
135 /*[*/ 12 /*]*/,
136 /*[*/ 12 /*]*/,
137 /*[*/ 12 /*]]*/).finished()),
Brian Silverman2c590c32013-11-04 18:08:54 -0800138 loop_(new StateFeedbackLoop<2, 2, 2>(
139 constants::GetValues().make_v_drivetrain_loop())),
Austin Schuh427b3702013-11-02 13:44:09 -0700140 left_cim_(new StateFeedbackLoop<1, 1, 1>(MakeCIMLoop())),
141 right_cim_(new StateFeedbackLoop<1, 1, 1>(MakeCIMLoop())),
142 ttrust_(1.1),
143 wheel_(0.0),
144 throttle_(0.0),
145 quickturn_(false),
146 stale_count_(0),
147 position_time_delta_(0.01),
148 left_gear_(LOW),
Brian Silvermande8fd552013-11-03 15:53:42 -0800149 right_gear_(LOW),
150 counter_(0) {
Austin Schuh2054f5f2013-10-27 14:54:10 -0700151
Austin Schuh427b3702013-11-02 13:44:09 -0700152 last_position_.Zero();
153 position_.Zero();
Austin Schuh2054f5f2013-10-27 14:54:10 -0700154 }
Austin Schuh427b3702013-11-02 13:44:09 -0700155 static bool IsInGear(Gear gear) { return gear == LOW || gear == HIGH; }
156
157 static double MotorSpeed(double shifter_position, double velocity) {
158 // TODO(austin): G_high, G_low and kWheelRadius
Brian Silvermande8fd552013-11-03 15:53:42 -0800159 if (shifter_position > 0.57) {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800160 return velocity / constants::GetValues().high_gear_ratio / kWheelRadius;
Austin Schuh427b3702013-11-02 13:44:09 -0700161 } else {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800162 return velocity / constants::GetValues().low_gear_ratio / kWheelRadius;
163 }
164 }
165
166 Gear ComputeGear(double velocity, Gear current) {
167 const double low_omega = MotorSpeed(0, ::std::abs(velocity));
168 const double high_omega = MotorSpeed(1.0, ::std::abs(velocity));
169
Brian Silverman1a6590d2013-11-04 14:46:46 -0800170 double high_torque = ((12.0 - high_omega / Kv) * Kt / kR);
171 double low_torque = ((12.0 - low_omega / Kv) * Kt / kR);
172 double high_power = high_torque * high_omega;
173 double low_power = low_torque * low_omega;
Brian Silverman55a930b2013-11-04 20:59:00 -0800174
175 // TODO(aschuh): Do this right!
176 if ((current == HIGH || high_power > low_power + 160) &&
177 ::std::abs(velocity) > 0.14) {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800178 return HIGH;
179 } else {
180 return LOW;
Austin Schuh427b3702013-11-02 13:44:09 -0700181 }
182 }
183
Austin Schuh2054f5f2013-10-27 14:54:10 -0700184 void SetGoal(double wheel, double throttle, bool quickturn, bool highgear) {
Brian Silvermande8fd552013-11-03 15:53:42 -0800185 const double kWheelNonLinearity = 0.3;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700186 // Apply a sin function that's scaled to make it feel better.
187 const double angular_range = M_PI_2 * kWheelNonLinearity;
188 wheel_ = sin(angular_range * wheel) / sin(angular_range);
189 wheel_ = sin(angular_range * wheel_) / sin(angular_range);
Austin Schuh2054f5f2013-10-27 14:54:10 -0700190 quickturn_ = quickturn;
Austin Schuh427b3702013-11-02 13:44:09 -0700191
Brian Silverman1a6590d2013-11-04 14:46:46 -0800192 static const double kThrottleDeadband = 0.05;
193 if (::std::abs(throttle) < kThrottleDeadband) {
194 throttle_ = 0;
195 } else {
196 throttle_ = copysign((::std::abs(throttle) - kThrottleDeadband) /
197 (1.0 - kThrottleDeadband), throttle);
198 }
199
Austin Schuh427b3702013-11-02 13:44:09 -0700200 // TODO(austin): Fix the upshift logic to include states.
Brian Silverman1a6590d2013-11-04 14:46:46 -0800201 Gear requested_gear;
Brian Silverman55a930b2013-11-04 20:59:00 -0800202 if (false) {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800203 const double current_left_velocity =
204 (position_.left_encoder - last_position_.left_encoder) /
205 position_time_delta_;
206 const double current_right_velocity =
207 (position_.right_encoder - last_position_.right_encoder) /
208 position_time_delta_;
209
210 Gear left_requested = ComputeGear(current_left_velocity, left_gear_);
211 Gear right_requested = ComputeGear(current_right_velocity, right_gear_);
212 requested_gear =
213 (left_requested == HIGH || right_requested == HIGH) ? HIGH : LOW;
214 } else {
215 requested_gear = highgear ? HIGH : LOW;
216 }
217
218 const Gear shift_up =
219 constants::GetValues().clutch_transmission ? HIGH : SHIFTING_UP;
220 const Gear shift_down =
221 constants::GetValues().clutch_transmission ? LOW : SHIFTING_DOWN;
Austin Schuh427b3702013-11-02 13:44:09 -0700222
223 if (left_gear_ != requested_gear) {
224 if (IsInGear(left_gear_)) {
225 if (requested_gear == HIGH) {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800226 left_gear_ = shift_up;
Austin Schuh427b3702013-11-02 13:44:09 -0700227 } else {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800228 left_gear_ = shift_down;
Austin Schuh427b3702013-11-02 13:44:09 -0700229 }
Brian Silverman55a930b2013-11-04 20:59:00 -0800230 } else {
231 if (requested_gear == HIGH && left_gear_ == SHIFTING_DOWN) {
232 left_gear_ = SHIFTING_UP;
233 } else if (requested_gear == LOW && left_gear_ == SHIFTING_UP) {
234 left_gear_ = SHIFTING_DOWN;
235 }
Austin Schuh427b3702013-11-02 13:44:09 -0700236 }
237 }
238 if (right_gear_ != requested_gear) {
239 if (IsInGear(right_gear_)) {
240 if (requested_gear == HIGH) {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800241 right_gear_ = shift_up;
Austin Schuh427b3702013-11-02 13:44:09 -0700242 } else {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800243 right_gear_ = shift_down;
Austin Schuh427b3702013-11-02 13:44:09 -0700244 }
Brian Silverman55a930b2013-11-04 20:59:00 -0800245 } else {
246 if (requested_gear == HIGH && right_gear_ == SHIFTING_DOWN) {
247 right_gear_ = SHIFTING_UP;
248 } else if (requested_gear == LOW && right_gear_ == SHIFTING_UP) {
249 right_gear_ = SHIFTING_DOWN;
250 }
Austin Schuh427b3702013-11-02 13:44:09 -0700251 }
Austin Schuh2054f5f2013-10-27 14:54:10 -0700252 }
253 }
Austin Schuh427b3702013-11-02 13:44:09 -0700254 void SetPosition(const Drivetrain::Position *position) {
255 if (position == NULL) {
256 ++stale_count_;
257 } else {
258 last_position_ = position_;
259 position_ = *position;
260 position_time_delta_ = (stale_count_ + 1) * 0.01;
261 stale_count_ = 0;
262 }
263
264 if (position) {
Brian Silverman61e41fd2014-02-16 19:08:50 -0800265 GearLogging gear_logging;
Austin Schuh427b3702013-11-02 13:44:09 -0700266 // Switch to the correct controller.
Brian Silvermande8fd552013-11-03 15:53:42 -0800267 // TODO(austin): Un-hard code 0.57
268 if (position->left_shifter_position < 0.57) {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800269 if (position->right_shifter_position < 0.57 || right_gear_ == LOW) {
Brian Silverman61e41fd2014-02-16 19:08:50 -0800270 gear_logging.left_loop_high = false;
271 gear_logging.right_loop_high = false;
272 loop_->set_controller_index(gear_logging.controller_index = 0);
Austin Schuh427b3702013-11-02 13:44:09 -0700273 } else {
Brian Silverman61e41fd2014-02-16 19:08:50 -0800274 gear_logging.left_loop_high = false;
275 gear_logging.right_loop_high = true;
276 loop_->set_controller_index(gear_logging.controller_index = 1);
Austin Schuh427b3702013-11-02 13:44:09 -0700277 }
278 } else {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800279 if (position->right_shifter_position < 0.57 || left_gear_ == LOW) {
Brian Silverman61e41fd2014-02-16 19:08:50 -0800280 gear_logging.left_loop_high = true;
281 gear_logging.right_loop_high = false;
282 loop_->set_controller_index(gear_logging.controller_index = 2);
Austin Schuh427b3702013-11-02 13:44:09 -0700283 } else {
Brian Silverman61e41fd2014-02-16 19:08:50 -0800284 gear_logging.left_loop_high = true;
285 gear_logging.right_loop_high = true;
286 loop_->set_controller_index(gear_logging.controller_index = 3);
Austin Schuh427b3702013-11-02 13:44:09 -0700287 }
288 }
Brian Silverman61e41fd2014-02-16 19:08:50 -0800289
Austin Schuh427b3702013-11-02 13:44:09 -0700290 // TODO(austin): Constants.
291 if (position->left_shifter_position > 0.9 && left_gear_ == SHIFTING_UP) {
292 left_gear_ = HIGH;
293 }
294 if (position->left_shifter_position < 0.1 && left_gear_ == SHIFTING_DOWN) {
295 left_gear_ = LOW;
296 }
297 if (position->right_shifter_position > 0.9 && right_gear_ == SHIFTING_UP) {
298 right_gear_ = HIGH;
299 }
300 if (position->right_shifter_position < 0.1 && right_gear_ == SHIFTING_DOWN) {
301 right_gear_ = LOW;
302 }
Brian Silverman61e41fd2014-02-16 19:08:50 -0800303
304 gear_logging.left_state = left_gear_;
305 gear_logging.right_state = right_gear_;
306 LOG_STRUCT(DEBUG, "state", gear_logging);
Austin Schuh427b3702013-11-02 13:44:09 -0700307 }
308 }
309
Austin Schuh2054f5f2013-10-27 14:54:10 -0700310 double FilterVelocity(double throttle) {
311 const Eigen::Matrix<double, 2, 2> FF =
312 loop_->B().inverse() *
313 (Eigen::Matrix<double, 2, 2>::Identity() - loop_->A());
314
315 constexpr int kHighGearController = 3;
316 const Eigen::Matrix<double, 2, 2> FF_high =
317 loop_->controller(kHighGearController).plant.B.inverse() *
318 (Eigen::Matrix<double, 2, 2>::Identity() -
319 loop_->controller(kHighGearController).plant.A);
320
321 ::Eigen::Matrix<double, 1, 2> FF_sum = FF.colwise().sum();
322 int min_FF_sum_index;
323 const double min_FF_sum = FF_sum.minCoeff(&min_FF_sum_index);
324 const double min_K_sum = loop_->K().col(min_FF_sum_index).sum();
325 const double high_min_FF_sum = FF_high.col(0).sum();
326
327 const double adjusted_ff_voltage = ::aos::Clip(
328 throttle * 12.0 * min_FF_sum / high_min_FF_sum, -12.0, 12.0);
329 return ((adjusted_ff_voltage +
330 ttrust_ * min_K_sum * (loop_->X_hat(0, 0) + loop_->X_hat(1, 0)) /
331 2.0) /
332 (ttrust_ * min_K_sum + min_FF_sum));
333 }
334
Brian Silverman718b1d72013-10-28 16:22:45 -0700335 double MaxVelocity() {
336 const Eigen::Matrix<double, 2, 2> FF =
337 loop_->B().inverse() *
338 (Eigen::Matrix<double, 2, 2>::Identity() - loop_->A());
339
340 constexpr int kHighGearController = 3;
341 const Eigen::Matrix<double, 2, 2> FF_high =
342 loop_->controller(kHighGearController).plant.B.inverse() *
343 (Eigen::Matrix<double, 2, 2>::Identity() -
344 loop_->controller(kHighGearController).plant.A);
345
346 ::Eigen::Matrix<double, 1, 2> FF_sum = FF.colwise().sum();
347 int min_FF_sum_index;
348 const double min_FF_sum = FF_sum.minCoeff(&min_FF_sum_index);
349 //const double min_K_sum = loop_->K().col(min_FF_sum_index).sum();
350 const double high_min_FF_sum = FF_high.col(0).sum();
351
352 const double adjusted_ff_voltage = ::aos::Clip(
353 12.0 * min_FF_sum / high_min_FF_sum, -12.0, 12.0);
354 return adjusted_ff_voltage / min_FF_sum;
355 }
356
Austin Schuh2054f5f2013-10-27 14:54:10 -0700357 void Update() {
Austin Schuh427b3702013-11-02 13:44:09 -0700358 // TODO(austin): Observer for the current velocity instead of difference
359 // calculations.
Brian Silvermande8fd552013-11-03 15:53:42 -0800360 ++counter_;
Austin Schuh427b3702013-11-02 13:44:09 -0700361 const double current_left_velocity =
Brian Silvermande8fd552013-11-03 15:53:42 -0800362 (position_.left_encoder - last_position_.left_encoder) /
Austin Schuh427b3702013-11-02 13:44:09 -0700363 position_time_delta_;
364 const double current_right_velocity =
Brian Silvermande8fd552013-11-03 15:53:42 -0800365 (position_.right_encoder - last_position_.right_encoder) /
Austin Schuh427b3702013-11-02 13:44:09 -0700366 position_time_delta_;
367 const double left_motor_speed =
368 MotorSpeed(position_.left_shifter_position, current_left_velocity);
369 const double right_motor_speed =
370 MotorSpeed(position_.right_shifter_position, current_right_velocity);
Austin Schuh2054f5f2013-10-27 14:54:10 -0700371
Brian Silverman61e41fd2014-02-16 19:08:50 -0800372 {
373 CIMLogging logging;
374
375 // Reset the CIM model to the current conditions to be ready for when we
376 // shift.
377 if (IsInGear(left_gear_)) {
378 logging.left_in_gear = true;
379 left_cim_->X_hat(0, 0) = left_motor_speed;
380 } else {
381 logging.left_in_gear = false;
382 }
383 logging.left_motor_speed = left_motor_speed;
384 logging.left_velocity = current_left_velocity;
385 if (IsInGear(right_gear_)) {
386 logging.right_in_gear = true;
387 right_cim_->X_hat(0, 0) = right_motor_speed;
388 } else {
389 logging.right_in_gear = false;
390 }
391 logging.right_motor_speed = right_motor_speed;
392 logging.right_velocity = current_right_velocity;
393
394 LOG_STRUCT(DEBUG, "currently", logging);
Austin Schuh427b3702013-11-02 13:44:09 -0700395 }
Austin Schuh2054f5f2013-10-27 14:54:10 -0700396
Austin Schuh427b3702013-11-02 13:44:09 -0700397 if (IsInGear(left_gear_) && IsInGear(right_gear_)) {
398 // FF * X = U (steady state)
399 const Eigen::Matrix<double, 2, 2> FF =
400 loop_->B().inverse() *
401 (Eigen::Matrix<double, 2, 2>::Identity() - loop_->A());
402
403 // Invert the plant to figure out how the velocity filter would have to
404 // work
405 // out in order to filter out the forwards negative inertia.
406 // This math assumes that the left and right power and velocity are
407 // equals,
408 // and that the plant is the same on the left and right.
409 const double fvel = FilterVelocity(throttle_);
410
411 const double sign_svel = wheel_ * ((fvel > 0.0) ? 1.0 : -1.0);
412 double steering_velocity;
413 if (quickturn_) {
414 steering_velocity = wheel_ * MaxVelocity();
415 } else {
416 steering_velocity = ::std::abs(fvel) * wheel_;
417 }
418 const double left_velocity = fvel - steering_velocity;
419 const double right_velocity = fvel + steering_velocity;
420
421 // Integrate velocity to get the position.
422 // This position is used to get integral control.
423 loop_->R << left_velocity, right_velocity;
424
425 if (!quickturn_) {
426 // K * R = w
427 Eigen::Matrix<double, 1, 2> equality_k;
428 equality_k << 1 + sign_svel, -(1 - sign_svel);
429 const double equality_w = 0.0;
430
431 // Construct a constraint on R by manipulating the constraint on U
432 ::aos::controls::HPolytope<2> R_poly = ::aos::controls::HPolytope<2>(
433 U_Poly_.H() * (loop_->K() + FF),
434 U_Poly_.k() + U_Poly_.H() * loop_->K() * loop_->X_hat);
435
436 // Limit R back inside the box.
437 loop_->R = CoerceGoal(R_poly, equality_k, equality_w, loop_->R);
438 }
439
440 const Eigen::Matrix<double, 2, 1> FF_volts = FF * loop_->R;
441 const Eigen::Matrix<double, 2, 1> U_ideal =
442 loop_->K() * (loop_->R - loop_->X_hat) + FF_volts;
443
444 for (int i = 0; i < 2; i++) {
445 loop_->U[i] = ::aos::Clip(U_ideal[i], -12, 12);
446 }
Brian Silvermande8fd552013-11-03 15:53:42 -0800447
448 // TODO(austin): Model this better.
449 // TODO(austin): Feed back?
450 loop_->X_hat = loop_->A() * loop_->X_hat + loop_->B() * loop_->U;
Brian Silverman718b1d72013-10-28 16:22:45 -0700451 } else {
Austin Schuh427b3702013-11-02 13:44:09 -0700452 // Any motor is not in gear. Speed match.
453 ::Eigen::Matrix<double, 1, 1> R_left;
454 R_left(0, 0) = left_motor_speed;
Brian Silverman61e41fd2014-02-16 19:08:50 -0800455 const double wiggle =
456 (static_cast<double>((counter_ % 4) / 2) - 0.5) * 3.5;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700457
Brian Silvermande8fd552013-11-03 15:53:42 -0800458 loop_->U(0, 0) =
459 ::aos::Clip((R_left / Kv)(0, 0) + wiggle, -position_.battery_voltage,
460 position_.battery_voltage);
461 right_cim_->X_hat = right_cim_->A() * right_cim_->X_hat +
462 right_cim_->B() * loop_->U(0, 0);
Austin Schuh2054f5f2013-10-27 14:54:10 -0700463
Austin Schuh427b3702013-11-02 13:44:09 -0700464 ::Eigen::Matrix<double, 1, 1> R_right;
465 R_right(0, 0) = right_motor_speed;
Brian Silvermande8fd552013-11-03 15:53:42 -0800466 loop_->U(1, 0) =
467 ::aos::Clip((R_right / Kv)(0, 0) + wiggle, -position_.battery_voltage,
468 position_.battery_voltage);
469 right_cim_->X_hat = right_cim_->A() * right_cim_->X_hat +
470 right_cim_->B() * loop_->U(1, 0);
471 loop_->U *= 12.0 / position_.battery_voltage;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700472 }
Austin Schuh2054f5f2013-10-27 14:54:10 -0700473 }
474
475 void SendMotors(Drivetrain::Output *output) {
Brian Silvermande8fd552013-11-03 15:53:42 -0800476 if (output != NULL) {
477 output->left_voltage = loop_->U(0, 0);
478 output->right_voltage = loop_->U(1, 0);
Brian Silverman61e41fd2014-02-16 19:08:50 -0800479 output->left_high = left_gear_ == HIGH || left_gear_ == SHIFTING_UP;
480 output->right_high = right_gear_ == HIGH || right_gear_ == SHIFTING_UP;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700481 }
482 }
483
484 private:
485 const ::aos::controls::HPolytope<2> U_Poly_;
486
487 ::std::unique_ptr<StateFeedbackLoop<2, 2, 2>> loop_;
Austin Schuh427b3702013-11-02 13:44:09 -0700488 ::std::unique_ptr<StateFeedbackLoop<1, 1, 1>> left_cim_;
489 ::std::unique_ptr<StateFeedbackLoop<1, 1, 1>> right_cim_;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700490
Austin Schuh427b3702013-11-02 13:44:09 -0700491 const double ttrust_;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700492 double wheel_;
493 double throttle_;
494 bool quickturn_;
Austin Schuh427b3702013-11-02 13:44:09 -0700495 int stale_count_;
496 double position_time_delta_;
497 Gear left_gear_;
498 Gear right_gear_;
499 Drivetrain::Position last_position_;
500 Drivetrain::Position position_;
Brian Silvermande8fd552013-11-03 15:53:42 -0800501 int counter_;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700502};
Brian Silvermancec3c8d2013-12-20 21:04:55 -0800503constexpr double PolyDrivetrain::kStallTorque;
504constexpr double PolyDrivetrain::kStallCurrent;
505constexpr double PolyDrivetrain::kFreeSpeed;
506constexpr double PolyDrivetrain::kFreeCurrent;
507constexpr double PolyDrivetrain::J;
508constexpr double PolyDrivetrain::m;
509constexpr double PolyDrivetrain::rb;
510constexpr double PolyDrivetrain::kWheelRadius;
511constexpr double PolyDrivetrain::kR;
512constexpr double PolyDrivetrain::Kv;
513constexpr double PolyDrivetrain::Kt;
514
Austin Schuh2054f5f2013-10-27 14:54:10 -0700515
brians343bc112013-02-10 01:53:46 +0000516void DrivetrainLoop::RunIteration(const Drivetrain::Goal *goal,
517 const Drivetrain::Position *position,
518 Drivetrain::Output *output,
519 Drivetrain::Status * /*status*/) {
520 // TODO(aschuh): These should be members of the class.
521 static DrivetrainMotorsSS dt_closedloop;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700522 static PolyDrivetrain dt_openloop;
brians343bc112013-02-10 01:53:46 +0000523
524 bool bad_pos = false;
Austin Schuh427b3702013-11-02 13:44:09 -0700525 if (position == nullptr) {
Brian Silverman50a9d032014-02-16 17:20:57 -0800526 LOG_INTERVAL(no_position_);
brians343bc112013-02-10 01:53:46 +0000527 bad_pos = true;
528 }
529
530 double wheel = goal->steering;
531 double throttle = goal->throttle;
532 bool quickturn = goal->quickturn;
533 bool highgear = goal->highgear;
534
535 bool control_loop_driving = goal->control_loop_driving;
536 double left_goal = goal->left_goal;
537 double right_goal = goal->right_goal;
538
Austin Schuh2054f5f2013-10-27 14:54:10 -0700539 dt_closedloop.SetGoal(left_goal, goal->left_velocity_goal, right_goal,
540 goal->right_velocity_goal);
brians343bc112013-02-10 01:53:46 +0000541 if (!bad_pos) {
542 const double left_encoder = position->left_encoder;
543 const double right_encoder = position->right_encoder;
544 if (gyro.FetchLatest()) {
Brian Silverman61e41fd2014-02-16 19:08:50 -0800545 LOG_STRUCT(DEBUG, "using", *gyro);
Austin Schuh2054f5f2013-10-27 14:54:10 -0700546 dt_closedloop.SetPosition(left_encoder, right_encoder, gyro->angle,
547 control_loop_driving);
brians343bc112013-02-10 01:53:46 +0000548 } else {
549 dt_closedloop.SetRawPosition(left_encoder, right_encoder);
550 }
551 }
Austin Schuh427b3702013-11-02 13:44:09 -0700552 dt_openloop.SetPosition(position);
Austin Schuhf9286cd2014-02-11 00:51:09 -0800553 dt_closedloop.Update(output == NULL);
brians343bc112013-02-10 01:53:46 +0000554 dt_openloop.SetGoal(wheel, throttle, quickturn, highgear);
555 dt_openloop.Update();
Brian Silverman2845c4c2013-03-16 19:54:08 -0700556 if (control_loop_driving) {
557 dt_closedloop.SendMotors(output);
558 } else {
559 dt_openloop.SendMotors(output);
brians343bc112013-02-10 01:53:46 +0000560 }
561}
562
563} // namespace control_loops
564} // namespace frc971