blob: 982c6e94262cb74cf43b2294c5c939a6ca9afba6 [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"
James Kuszmaulf254c1a2013-03-10 16:31:26 -070013#include "frc971/control_loops/state_feedback_loop.h"
Austin Schuh427b3702013-11-02 13:44:09 -070014#include "frc971/control_loops/drivetrain/polydrivetrain_cim_plant.h"
James Kuszmaulf254c1a2013-03-10 16:31:26 -070015#include "frc971/control_loops/drivetrain/drivetrain.q.h"
Austin Schuh3bb9a442014-02-02 16:01:45 -080016#include "frc971/queues/gyro_angle.q.h"
17#include "frc971/queues/piston.q.h"
Brian Silverman1a6590d2013-11-04 14:46:46 -080018#include "frc971/constants.h"
brians343bc112013-02-10 01:53:46 +000019
20using frc971::sensors::gyro;
21
22namespace frc971 {
23namespace control_loops {
24
25// Width of the robot.
26const double width = 22.0 / 100.0 * 2.54;
27
Austin Schuh2054f5f2013-10-27 14:54:10 -070028Eigen::Matrix<double, 2, 1> CoerceGoal(aos::controls::HPolytope<2> &region,
29 const Eigen::Matrix<double, 1, 2> &K,
30 double w,
31 const Eigen::Matrix<double, 2, 1> &R) {
32 if (region.IsInside(R)) {
33 return R;
34 }
35 Eigen::Matrix<double, 2, 1> parallel_vector;
36 Eigen::Matrix<double, 2, 1> perpendicular_vector;
37 perpendicular_vector = K.transpose().normalized();
38 parallel_vector << perpendicular_vector(1, 0), -perpendicular_vector(0, 0);
39
40 aos::controls::HPolytope<1> t_poly(
41 region.H() * parallel_vector,
42 region.k() - region.H() * perpendicular_vector * w);
43
44 Eigen::Matrix<double, 1, Eigen::Dynamic> vertices = t_poly.Vertices();
45 if (vertices.innerSize() > 0) {
46 double min_distance_sqr = 0;
47 Eigen::Matrix<double, 2, 1> closest_point;
48 for (int i = 0; i < vertices.innerSize(); i++) {
49 Eigen::Matrix<double, 2, 1> point;
50 point = parallel_vector * vertices(0, i) + perpendicular_vector * w;
51 const double length = (R - point).squaredNorm();
52 if (i == 0 || length < min_distance_sqr) {
53 closest_point = point;
54 min_distance_sqr = length;
55 }
56 }
57 return closest_point;
58 } else {
59 Eigen::Matrix<double, 2, Eigen::Dynamic> region_vertices =
60 region.Vertices();
Brian Silverman41abe012014-02-08 18:25:02 -080061 double min_distance = INFINITY;
Austin Schuh2054f5f2013-10-27 14:54:10 -070062 int closest_i = 0;
63 for (int i = 0; i < region_vertices.outerSize(); i++) {
64 const double length = ::std::abs(
65 (perpendicular_vector.transpose() * (region_vertices.col(i)))(0, 0));
66 if (i == 0 || length < min_distance) {
67 closest_i = i;
68 min_distance = length;
69 }
70 }
71 return region_vertices.col(closest_i);
72 }
73}
74
Austin Schuh4352ac62013-03-19 06:23:16 +000075class DrivetrainMotorsSS {
brians343bc112013-02-10 01:53:46 +000076 public:
Brian Silverman2c590c32013-11-04 18:08:54 -080077 DrivetrainMotorsSS()
78 : loop_(new StateFeedbackLoop<4, 2, 2>(
79 constants::GetValues().make_drivetrain_loop())) {
brians343bc112013-02-10 01:53:46 +000080 _offset = 0;
81 _integral_offset = 0;
82 _left_goal = 0.0;
83 _right_goal = 0.0;
84 _raw_left = 0.0;
85 _raw_right = 0.0;
Austin Schuh4352ac62013-03-19 06:23:16 +000086 _control_loop_driving = false;
brians343bc112013-02-10 01:53:46 +000087 }
88 void SetGoal(double left, double left_velocity, double right, double right_velocity) {
89 _left_goal = left;
90 _right_goal = right;
Austin Schuh4352ac62013-03-19 06:23:16 +000091 loop_->R << left, left_velocity, right, right_velocity;
brians343bc112013-02-10 01:53:46 +000092 }
93 void SetRawPosition(double left, double right) {
94 _raw_right = right;
95 _raw_left = left;
Austin Schuhf9286cd2014-02-11 00:51:09 -080096 Eigen::Matrix<double, 2, 1> Y;
97 Y << left, right;
98 loop_->Correct(Y);
brians343bc112013-02-10 01:53:46 +000099 }
Austin Schuh4352ac62013-03-19 06:23:16 +0000100 void SetPosition(
101 double left, double right, double gyro, bool control_loop_driving) {
brians343bc112013-02-10 01:53:46 +0000102 // Decay the offset quickly because this gyro is great.
103 _offset = (0.25) * (right - left - gyro * width) / 2.0 + 0.75 * _offset;
Brian Silvermande8fd552013-11-03 15:53:42 -0800104 //const double angle_error = (_right_goal - _left_goal) / width - (_raw_right - _offset - _raw_left - _offset) / width;
Austin Schuh4352ac62013-03-19 06:23:16 +0000105 // TODO(aschuh): Add in the gyro.
106 _integral_offset = 0.0;
107 _offset = 0.0;
brians343bc112013-02-10 01:53:46 +0000108 _gyro = gyro;
Austin Schuh4352ac62013-03-19 06:23:16 +0000109 _control_loop_driving = control_loop_driving;
brians343bc112013-02-10 01:53:46 +0000110 SetRawPosition(left, right);
brians343bc112013-02-10 01:53:46 +0000111 }
Austin Schuh4352ac62013-03-19 06:23:16 +0000112
Austin Schuhf9286cd2014-02-11 00:51:09 -0800113 void Update(bool stop_motors) {
114 loop_->Update(stop_motors);
brians343bc112013-02-10 01:53:46 +0000115 }
116
Austin Schuh4352ac62013-03-19 06:23:16 +0000117 void SendMotors(Drivetrain::Output *output) {
118 if (output) {
119 output->left_voltage = loop_->U(0, 0);
120 output->right_voltage = loop_->U(1, 0);
brians8ad74052013-03-16 21:04:51 +0000121 }
brians343bc112013-02-10 01:53:46 +0000122 }
123 void PrintMotors() const {
124 // LOG(DEBUG, "Left Power %f Right Power %f lg %f rg %f le %f re %f gyro %f\n", U[0], U[1], R[0], R[2], Y[0], Y[1], _gyro);
Austin Schuh4352ac62013-03-19 06:23:16 +0000125 ::Eigen::Matrix<double, 4, 1> E = loop_->R - loop_->X_hat;
126 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 +0000127 }
128
129 private:
Austin Schuh4352ac62013-03-19 06:23:16 +0000130 ::std::unique_ptr<StateFeedbackLoop<4, 2, 2>> loop_;
131
brians343bc112013-02-10 01:53:46 +0000132 double _integral_offset;
133 double _offset;
134 double _gyro;
135 double _left_goal;
136 double _right_goal;
137 double _raw_left;
138 double _raw_right;
Austin Schuh4352ac62013-03-19 06:23:16 +0000139 bool _control_loop_driving;
brians343bc112013-02-10 01:53:46 +0000140};
141
Austin Schuh2054f5f2013-10-27 14:54:10 -0700142class PolyDrivetrain {
143 public:
Austin Schuh427b3702013-11-02 13:44:09 -0700144
145 enum Gear {
146 HIGH,
147 LOW,
148 SHIFTING_UP,
149 SHIFTING_DOWN
150 };
151 // Stall Torque in N m
152 static constexpr double kStallTorque = 2.42;
153 // Stall Current in Amps
154 static constexpr double kStallCurrent = 133;
155 // Free Speed in RPM. Used number from last year.
156 static constexpr double kFreeSpeed = 4650.0;
157 // Free Current in Amps
158 static constexpr double kFreeCurrent = 2.7;
159 // Moment of inertia of the drivetrain in kg m^2
160 // Just borrowed from last year.
161 static constexpr double J = 6.4;
162 // Mass of the robot, in kg.
163 static constexpr double m = 68;
164 // Radius of the robot, in meters (from last year).
165 static constexpr double rb = 0.617998644 / 2.0;
Brian Silverman1a6590d2013-11-04 14:46:46 -0800166 static constexpr double kWheelRadius = 0.04445;
Austin Schuh427b3702013-11-02 13:44:09 -0700167 // Resistance of the motor, divided by the number of motors.
168 static constexpr double kR = (12.0 / kStallCurrent / 4 + 0.03) / (0.93 * 0.93);
169 // Motor velocity constant
170 static constexpr double Kv =
171 ((kFreeSpeed / 60.0 * 2.0 * M_PI) / (12.0 - kR * kFreeCurrent));
172 // Torque constant
173 static constexpr double Kt = kStallTorque / kStallCurrent;
Austin Schuh427b3702013-11-02 13:44:09 -0700174
Austin Schuh2054f5f2013-10-27 14:54:10 -0700175 PolyDrivetrain()
176 : U_Poly_((Eigen::Matrix<double, 4, 2>() << /*[[*/ 1, 0 /*]*/,
177 /*[*/ -1, 0 /*]*/,
178 /*[*/ 0, 1 /*]*/,
179 /*[*/ 0, -1 /*]]*/).finished(),
180 (Eigen::Matrix<double, 4, 1>() << /*[[*/ 12 /*]*/,
181 /*[*/ 12 /*]*/,
182 /*[*/ 12 /*]*/,
183 /*[*/ 12 /*]]*/).finished()),
Brian Silverman2c590c32013-11-04 18:08:54 -0800184 loop_(new StateFeedbackLoop<2, 2, 2>(
185 constants::GetValues().make_v_drivetrain_loop())),
Austin Schuh427b3702013-11-02 13:44:09 -0700186 left_cim_(new StateFeedbackLoop<1, 1, 1>(MakeCIMLoop())),
187 right_cim_(new StateFeedbackLoop<1, 1, 1>(MakeCIMLoop())),
188 ttrust_(1.1),
189 wheel_(0.0),
190 throttle_(0.0),
191 quickturn_(false),
192 stale_count_(0),
193 position_time_delta_(0.01),
194 left_gear_(LOW),
Brian Silvermande8fd552013-11-03 15:53:42 -0800195 right_gear_(LOW),
196 counter_(0) {
Austin Schuh2054f5f2013-10-27 14:54:10 -0700197
Austin Schuh427b3702013-11-02 13:44:09 -0700198 last_position_.Zero();
199 position_.Zero();
Austin Schuh2054f5f2013-10-27 14:54:10 -0700200 }
Austin Schuh427b3702013-11-02 13:44:09 -0700201 static bool IsInGear(Gear gear) { return gear == LOW || gear == HIGH; }
202
203 static double MotorSpeed(double shifter_position, double velocity) {
204 // TODO(austin): G_high, G_low and kWheelRadius
Brian Silvermande8fd552013-11-03 15:53:42 -0800205 if (shifter_position > 0.57) {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800206 return velocity / constants::GetValues().high_gear_ratio / kWheelRadius;
Austin Schuh427b3702013-11-02 13:44:09 -0700207 } else {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800208 return velocity / constants::GetValues().low_gear_ratio / kWheelRadius;
209 }
210 }
211
212 Gear ComputeGear(double velocity, Gear current) {
213 const double low_omega = MotorSpeed(0, ::std::abs(velocity));
214 const double high_omega = MotorSpeed(1.0, ::std::abs(velocity));
215
Brian Silverman1a6590d2013-11-04 14:46:46 -0800216 double high_torque = ((12.0 - high_omega / Kv) * Kt / kR);
217 double low_torque = ((12.0 - low_omega / Kv) * Kt / kR);
218 double high_power = high_torque * high_omega;
219 double low_power = low_torque * low_omega;
Brian Silverman55a930b2013-11-04 20:59:00 -0800220
221 // TODO(aschuh): Do this right!
222 if ((current == HIGH || high_power > low_power + 160) &&
223 ::std::abs(velocity) > 0.14) {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800224 return HIGH;
225 } else {
226 return LOW;
Austin Schuh427b3702013-11-02 13:44:09 -0700227 }
228 }
229
Austin Schuh2054f5f2013-10-27 14:54:10 -0700230 void SetGoal(double wheel, double throttle, bool quickturn, bool highgear) {
Brian Silvermande8fd552013-11-03 15:53:42 -0800231 const double kWheelNonLinearity = 0.3;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700232 // Apply a sin function that's scaled to make it feel better.
233 const double angular_range = M_PI_2 * kWheelNonLinearity;
234 wheel_ = sin(angular_range * wheel) / sin(angular_range);
235 wheel_ = sin(angular_range * wheel_) / sin(angular_range);
Austin Schuh2054f5f2013-10-27 14:54:10 -0700236 quickturn_ = quickturn;
Austin Schuh427b3702013-11-02 13:44:09 -0700237
Brian Silverman1a6590d2013-11-04 14:46:46 -0800238 static const double kThrottleDeadband = 0.05;
239 if (::std::abs(throttle) < kThrottleDeadband) {
240 throttle_ = 0;
241 } else {
242 throttle_ = copysign((::std::abs(throttle) - kThrottleDeadband) /
243 (1.0 - kThrottleDeadband), throttle);
244 }
245
Austin Schuh427b3702013-11-02 13:44:09 -0700246 // TODO(austin): Fix the upshift logic to include states.
Brian Silverman1a6590d2013-11-04 14:46:46 -0800247 Gear requested_gear;
Brian Silverman55a930b2013-11-04 20:59:00 -0800248 if (false) {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800249 const double current_left_velocity =
250 (position_.left_encoder - last_position_.left_encoder) /
251 position_time_delta_;
252 const double current_right_velocity =
253 (position_.right_encoder - last_position_.right_encoder) /
254 position_time_delta_;
255
256 Gear left_requested = ComputeGear(current_left_velocity, left_gear_);
257 Gear right_requested = ComputeGear(current_right_velocity, right_gear_);
258 requested_gear =
259 (left_requested == HIGH || right_requested == HIGH) ? HIGH : LOW;
260 } else {
261 requested_gear = highgear ? HIGH : LOW;
262 }
263
264 const Gear shift_up =
265 constants::GetValues().clutch_transmission ? HIGH : SHIFTING_UP;
266 const Gear shift_down =
267 constants::GetValues().clutch_transmission ? LOW : SHIFTING_DOWN;
Austin Schuh427b3702013-11-02 13:44:09 -0700268
269 if (left_gear_ != requested_gear) {
270 if (IsInGear(left_gear_)) {
271 if (requested_gear == HIGH) {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800272 left_gear_ = shift_up;
Austin Schuh427b3702013-11-02 13:44:09 -0700273 } else {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800274 left_gear_ = shift_down;
Austin Schuh427b3702013-11-02 13:44:09 -0700275 }
Brian Silverman55a930b2013-11-04 20:59:00 -0800276 } else {
277 if (requested_gear == HIGH && left_gear_ == SHIFTING_DOWN) {
278 left_gear_ = SHIFTING_UP;
279 } else if (requested_gear == LOW && left_gear_ == SHIFTING_UP) {
280 left_gear_ = SHIFTING_DOWN;
281 }
Austin Schuh427b3702013-11-02 13:44:09 -0700282 }
283 }
284 if (right_gear_ != requested_gear) {
285 if (IsInGear(right_gear_)) {
286 if (requested_gear == HIGH) {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800287 right_gear_ = shift_up;
Austin Schuh427b3702013-11-02 13:44:09 -0700288 } else {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800289 right_gear_ = shift_down;
Austin Schuh427b3702013-11-02 13:44:09 -0700290 }
Brian Silverman55a930b2013-11-04 20:59:00 -0800291 } else {
292 if (requested_gear == HIGH && right_gear_ == SHIFTING_DOWN) {
293 right_gear_ = SHIFTING_UP;
294 } else if (requested_gear == LOW && right_gear_ == SHIFTING_UP) {
295 right_gear_ = SHIFTING_DOWN;
296 }
Austin Schuh427b3702013-11-02 13:44:09 -0700297 }
Austin Schuh2054f5f2013-10-27 14:54:10 -0700298 }
299 }
Austin Schuh427b3702013-11-02 13:44:09 -0700300 void SetPosition(const Drivetrain::Position *position) {
301 if (position == NULL) {
302 ++stale_count_;
303 } else {
304 last_position_ = position_;
305 position_ = *position;
306 position_time_delta_ = (stale_count_ + 1) * 0.01;
307 stale_count_ = 0;
308 }
309
310 if (position) {
311 // Switch to the correct controller.
Brian Silvermande8fd552013-11-03 15:53:42 -0800312 // TODO(austin): Un-hard code 0.57
313 if (position->left_shifter_position < 0.57) {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800314 if (position->right_shifter_position < 0.57 || right_gear_ == LOW) {
Brian Silvermande8fd552013-11-03 15:53:42 -0800315 LOG(DEBUG, "Loop Left low, Right low\n");
Austin Schuh427b3702013-11-02 13:44:09 -0700316 loop_->set_controller_index(0);
317 } else {
Brian Silvermande8fd552013-11-03 15:53:42 -0800318 LOG(DEBUG, "Loop Left low, Right high\n");
Austin Schuh427b3702013-11-02 13:44:09 -0700319 loop_->set_controller_index(1);
320 }
321 } else {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800322 if (position->right_shifter_position < 0.57 || left_gear_ == LOW) {
Brian Silvermande8fd552013-11-03 15:53:42 -0800323 LOG(DEBUG, "Loop Left high, Right low\n");
Austin Schuh427b3702013-11-02 13:44:09 -0700324 loop_->set_controller_index(2);
325 } else {
Brian Silvermande8fd552013-11-03 15:53:42 -0800326 LOG(DEBUG, "Loop Left high, Right high\n");
Austin Schuh427b3702013-11-02 13:44:09 -0700327 loop_->set_controller_index(3);
328 }
329 }
Brian Silvermande8fd552013-11-03 15:53:42 -0800330 switch (left_gear_) {
331 case LOW:
332 LOG(DEBUG, "Left is in low\n");
333 break;
334 case HIGH:
335 LOG(DEBUG, "Left is in high\n");
336 break;
337 case SHIFTING_UP:
338 LOG(DEBUG, "Left is shifting up\n");
339 break;
340 case SHIFTING_DOWN:
341 LOG(DEBUG, "Left is shifting down\n");
342 break;
343 }
344 switch (right_gear_) {
345 case LOW:
346 LOG(DEBUG, "Right is in low\n");
347 break;
348 case HIGH:
349 LOG(DEBUG, "Right is in high\n");
350 break;
351 case SHIFTING_UP:
352 LOG(DEBUG, "Right is shifting up\n");
353 break;
354 case SHIFTING_DOWN:
355 LOG(DEBUG, "Right is shifting down\n");
356 break;
357 }
Austin Schuh427b3702013-11-02 13:44:09 -0700358 // TODO(austin): Constants.
359 if (position->left_shifter_position > 0.9 && left_gear_ == SHIFTING_UP) {
360 left_gear_ = HIGH;
361 }
362 if (position->left_shifter_position < 0.1 && left_gear_ == SHIFTING_DOWN) {
363 left_gear_ = LOW;
364 }
365 if (position->right_shifter_position > 0.9 && right_gear_ == SHIFTING_UP) {
366 right_gear_ = HIGH;
367 }
368 if (position->right_shifter_position < 0.1 && right_gear_ == SHIFTING_DOWN) {
369 right_gear_ = LOW;
370 }
371 }
372 }
373
Austin Schuh2054f5f2013-10-27 14:54:10 -0700374 double FilterVelocity(double throttle) {
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 throttle * 12.0 * min_FF_sum / high_min_FF_sum, -12.0, 12.0);
393 return ((adjusted_ff_voltage +
394 ttrust_ * min_K_sum * (loop_->X_hat(0, 0) + loop_->X_hat(1, 0)) /
395 2.0) /
396 (ttrust_ * min_K_sum + min_FF_sum));
397 }
398
Brian Silverman718b1d72013-10-28 16:22:45 -0700399 double MaxVelocity() {
400 const Eigen::Matrix<double, 2, 2> FF =
401 loop_->B().inverse() *
402 (Eigen::Matrix<double, 2, 2>::Identity() - loop_->A());
403
404 constexpr int kHighGearController = 3;
405 const Eigen::Matrix<double, 2, 2> FF_high =
406 loop_->controller(kHighGearController).plant.B.inverse() *
407 (Eigen::Matrix<double, 2, 2>::Identity() -
408 loop_->controller(kHighGearController).plant.A);
409
410 ::Eigen::Matrix<double, 1, 2> FF_sum = FF.colwise().sum();
411 int min_FF_sum_index;
412 const double min_FF_sum = FF_sum.minCoeff(&min_FF_sum_index);
413 //const double min_K_sum = loop_->K().col(min_FF_sum_index).sum();
414 const double high_min_FF_sum = FF_high.col(0).sum();
415
416 const double adjusted_ff_voltage = ::aos::Clip(
417 12.0 * min_FF_sum / high_min_FF_sum, -12.0, 12.0);
418 return adjusted_ff_voltage / min_FF_sum;
419 }
420
Austin Schuh2054f5f2013-10-27 14:54:10 -0700421 void Update() {
Austin Schuh427b3702013-11-02 13:44:09 -0700422 // TODO(austin): Observer for the current velocity instead of difference
423 // calculations.
Brian Silvermande8fd552013-11-03 15:53:42 -0800424 ++counter_;
Austin Schuh427b3702013-11-02 13:44:09 -0700425 const double current_left_velocity =
Brian Silvermande8fd552013-11-03 15:53:42 -0800426 (position_.left_encoder - last_position_.left_encoder) /
Austin Schuh427b3702013-11-02 13:44:09 -0700427 position_time_delta_;
428 const double current_right_velocity =
Brian Silvermande8fd552013-11-03 15:53:42 -0800429 (position_.right_encoder - last_position_.right_encoder) /
Austin Schuh427b3702013-11-02 13:44:09 -0700430 position_time_delta_;
431 const double left_motor_speed =
432 MotorSpeed(position_.left_shifter_position, current_left_velocity);
433 const double right_motor_speed =
434 MotorSpeed(position_.right_shifter_position, current_right_velocity);
Austin Schuh2054f5f2013-10-27 14:54:10 -0700435
Austin Schuh427b3702013-11-02 13:44:09 -0700436 // Reset the CIM model to the current conditions to be ready for when we shift.
437 if (IsInGear(left_gear_)) {
438 left_cim_->X_hat(0, 0) = left_motor_speed;
Brian Silvermande8fd552013-11-03 15:53:42 -0800439 LOG(DEBUG, "Setting left CIM to %f at robot speed %f\n", left_motor_speed,
440 current_left_velocity);
Austin Schuh427b3702013-11-02 13:44:09 -0700441 }
442 if (IsInGear(right_gear_)) {
Brian Silvermande8fd552013-11-03 15:53:42 -0800443 right_cim_->X_hat(0, 0) = right_motor_speed;
444 LOG(DEBUG, "Setting right CIM to %f at robot speed %f\n",
445 right_motor_speed, current_right_velocity);
Austin Schuh427b3702013-11-02 13:44:09 -0700446 }
Brian Silvermande8fd552013-11-03 15:53:42 -0800447 LOG(DEBUG, "robot speed l=%f r=%f\n", current_left_velocity,
448 current_right_velocity);
Austin Schuh2054f5f2013-10-27 14:54:10 -0700449
Austin Schuh427b3702013-11-02 13:44:09 -0700450 if (IsInGear(left_gear_) && IsInGear(right_gear_)) {
451 // FF * X = U (steady state)
452 const Eigen::Matrix<double, 2, 2> FF =
453 loop_->B().inverse() *
454 (Eigen::Matrix<double, 2, 2>::Identity() - loop_->A());
455
456 // Invert the plant to figure out how the velocity filter would have to
457 // work
458 // out in order to filter out the forwards negative inertia.
459 // This math assumes that the left and right power and velocity are
460 // equals,
461 // and that the plant is the same on the left and right.
462 const double fvel = FilterVelocity(throttle_);
463
464 const double sign_svel = wheel_ * ((fvel > 0.0) ? 1.0 : -1.0);
465 double steering_velocity;
466 if (quickturn_) {
467 steering_velocity = wheel_ * MaxVelocity();
468 } else {
469 steering_velocity = ::std::abs(fvel) * wheel_;
470 }
471 const double left_velocity = fvel - steering_velocity;
472 const double right_velocity = fvel + steering_velocity;
473
474 // Integrate velocity to get the position.
475 // This position is used to get integral control.
476 loop_->R << left_velocity, right_velocity;
477
478 if (!quickturn_) {
479 // K * R = w
480 Eigen::Matrix<double, 1, 2> equality_k;
481 equality_k << 1 + sign_svel, -(1 - sign_svel);
482 const double equality_w = 0.0;
483
484 // Construct a constraint on R by manipulating the constraint on U
485 ::aos::controls::HPolytope<2> R_poly = ::aos::controls::HPolytope<2>(
486 U_Poly_.H() * (loop_->K() + FF),
487 U_Poly_.k() + U_Poly_.H() * loop_->K() * loop_->X_hat);
488
489 // Limit R back inside the box.
490 loop_->R = CoerceGoal(R_poly, equality_k, equality_w, loop_->R);
491 }
492
493 const Eigen::Matrix<double, 2, 1> FF_volts = FF * loop_->R;
494 const Eigen::Matrix<double, 2, 1> U_ideal =
495 loop_->K() * (loop_->R - loop_->X_hat) + FF_volts;
496
497 for (int i = 0; i < 2; i++) {
498 loop_->U[i] = ::aos::Clip(U_ideal[i], -12, 12);
499 }
Brian Silvermande8fd552013-11-03 15:53:42 -0800500
501 // TODO(austin): Model this better.
502 // TODO(austin): Feed back?
503 loop_->X_hat = loop_->A() * loop_->X_hat + loop_->B() * loop_->U;
Brian Silverman718b1d72013-10-28 16:22:45 -0700504 } else {
Austin Schuh427b3702013-11-02 13:44:09 -0700505 // Any motor is not in gear. Speed match.
506 ::Eigen::Matrix<double, 1, 1> R_left;
507 R_left(0, 0) = left_motor_speed;
Brian Silvermande8fd552013-11-03 15:53:42 -0800508 const double wiggle = (static_cast<double>((counter_ % 4) / 2) - 0.5) * 3.5;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700509
Brian Silvermande8fd552013-11-03 15:53:42 -0800510 loop_->U(0, 0) =
511 ::aos::Clip((R_left / Kv)(0, 0) + wiggle, -position_.battery_voltage,
512 position_.battery_voltage);
513 right_cim_->X_hat = right_cim_->A() * right_cim_->X_hat +
514 right_cim_->B() * loop_->U(0, 0);
Austin Schuh2054f5f2013-10-27 14:54:10 -0700515
Austin Schuh427b3702013-11-02 13:44:09 -0700516 ::Eigen::Matrix<double, 1, 1> R_right;
517 R_right(0, 0) = right_motor_speed;
Brian Silvermande8fd552013-11-03 15:53:42 -0800518 loop_->U(1, 0) =
519 ::aos::Clip((R_right / Kv)(0, 0) + wiggle, -position_.battery_voltage,
520 position_.battery_voltage);
521 right_cim_->X_hat = right_cim_->A() * right_cim_->X_hat +
522 right_cim_->B() * loop_->U(1, 0);
523 loop_->U *= 12.0 / position_.battery_voltage;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700524 }
Austin Schuh2054f5f2013-10-27 14:54:10 -0700525 }
526
527 void SendMotors(Drivetrain::Output *output) {
Brian Silvermande8fd552013-11-03 15:53:42 -0800528 if (output != NULL) {
529 output->left_voltage = loop_->U(0, 0);
530 output->right_voltage = loop_->U(1, 0);
531 }
Austin Schuh427b3702013-11-02 13:44:09 -0700532 // Go in high gear if anything wants to be in high gear.
533 // TODO(austin): Seperate these.
534 if (left_gear_ == HIGH || left_gear_ == SHIFTING_UP ||
535 right_gear_ == HIGH || right_gear_ == SHIFTING_UP) {
Austin Schuh2054f5f2013-10-27 14:54:10 -0700536 shifters.MakeWithBuilder().set(false).Send();
537 } else {
538 shifters.MakeWithBuilder().set(true).Send();
539 }
540 }
541
542 private:
543 const ::aos::controls::HPolytope<2> U_Poly_;
544
545 ::std::unique_ptr<StateFeedbackLoop<2, 2, 2>> loop_;
Austin Schuh427b3702013-11-02 13:44:09 -0700546 ::std::unique_ptr<StateFeedbackLoop<1, 1, 1>> left_cim_;
547 ::std::unique_ptr<StateFeedbackLoop<1, 1, 1>> right_cim_;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700548
Austin Schuh427b3702013-11-02 13:44:09 -0700549 const double ttrust_;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700550 double wheel_;
551 double throttle_;
552 bool quickturn_;
Austin Schuh427b3702013-11-02 13:44:09 -0700553 int stale_count_;
554 double position_time_delta_;
555 Gear left_gear_;
556 Gear right_gear_;
557 Drivetrain::Position last_position_;
558 Drivetrain::Position position_;
Brian Silvermande8fd552013-11-03 15:53:42 -0800559 int counter_;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700560};
Brian Silvermancec3c8d2013-12-20 21:04:55 -0800561constexpr double PolyDrivetrain::kStallTorque;
562constexpr double PolyDrivetrain::kStallCurrent;
563constexpr double PolyDrivetrain::kFreeSpeed;
564constexpr double PolyDrivetrain::kFreeCurrent;
565constexpr double PolyDrivetrain::J;
566constexpr double PolyDrivetrain::m;
567constexpr double PolyDrivetrain::rb;
568constexpr double PolyDrivetrain::kWheelRadius;
569constexpr double PolyDrivetrain::kR;
570constexpr double PolyDrivetrain::Kv;
571constexpr double PolyDrivetrain::Kt;
572
Austin Schuh2054f5f2013-10-27 14:54:10 -0700573
brians343bc112013-02-10 01:53:46 +0000574void DrivetrainLoop::RunIteration(const Drivetrain::Goal *goal,
575 const Drivetrain::Position *position,
576 Drivetrain::Output *output,
577 Drivetrain::Status * /*status*/) {
578 // TODO(aschuh): These should be members of the class.
579 static DrivetrainMotorsSS dt_closedloop;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700580 static PolyDrivetrain dt_openloop;
brians343bc112013-02-10 01:53:46 +0000581
582 bool bad_pos = false;
Austin Schuh427b3702013-11-02 13:44:09 -0700583 if (position == nullptr) {
James Kuszmaul3f354742013-03-10 17:27:56 -0700584 LOG(WARNING, "no position\n");
brians343bc112013-02-10 01:53:46 +0000585 bad_pos = true;
586 }
587
588 double wheel = goal->steering;
589 double throttle = goal->throttle;
590 bool quickturn = goal->quickturn;
591 bool highgear = goal->highgear;
592
593 bool control_loop_driving = goal->control_loop_driving;
594 double left_goal = goal->left_goal;
595 double right_goal = goal->right_goal;
596
Austin Schuh2054f5f2013-10-27 14:54:10 -0700597 dt_closedloop.SetGoal(left_goal, goal->left_velocity_goal, right_goal,
598 goal->right_velocity_goal);
brians343bc112013-02-10 01:53:46 +0000599 if (!bad_pos) {
600 const double left_encoder = position->left_encoder;
601 const double right_encoder = position->right_encoder;
602 if (gyro.FetchLatest()) {
Brian Silverman96d9cea2013-11-12 21:10:50 -0800603 LOG(DEBUG, "gyro %f\n", gyro->angle);
Austin Schuh2054f5f2013-10-27 14:54:10 -0700604 dt_closedloop.SetPosition(left_encoder, right_encoder, gyro->angle,
605 control_loop_driving);
brians343bc112013-02-10 01:53:46 +0000606 } else {
607 dt_closedloop.SetRawPosition(left_encoder, right_encoder);
608 }
609 }
Austin Schuh427b3702013-11-02 13:44:09 -0700610 dt_openloop.SetPosition(position);
Austin Schuhf9286cd2014-02-11 00:51:09 -0800611 dt_closedloop.Update(output == NULL);
brians343bc112013-02-10 01:53:46 +0000612 dt_openloop.SetGoal(wheel, throttle, quickturn, highgear);
613 dt_openloop.Update();
Brian Silverman2845c4c2013-03-16 19:54:08 -0700614 if (control_loop_driving) {
615 dt_closedloop.SendMotors(output);
616 } else {
617 dt_openloop.SendMotors(output);
brians343bc112013-02-10 01:53:46 +0000618 }
619}
620
621} // namespace control_loops
622} // namespace frc971