blob: 71af487092a9eaaa82e74bfbcc305fd17fb4f441 [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"
brians343bc112013-02-10 01:53:46 +000016#include "frc971/queues/GyroAngle.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();
61 double min_distance;
62 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 Schuh4352ac62013-03-19 06:23:16 +000096 loop_->Y << left, right;
brians343bc112013-02-10 01:53:46 +000097 }
Austin Schuh4352ac62013-03-19 06:23:16 +000098 void SetPosition(
99 double left, double right, double gyro, bool control_loop_driving) {
brians343bc112013-02-10 01:53:46 +0000100 // Decay the offset quickly because this gyro is great.
101 _offset = (0.25) * (right - left - gyro * width) / 2.0 + 0.75 * _offset;
Brian Silvermande8fd552013-11-03 15:53:42 -0800102 //const double angle_error = (_right_goal - _left_goal) / width - (_raw_right - _offset - _raw_left - _offset) / width;
Austin Schuh4352ac62013-03-19 06:23:16 +0000103 // TODO(aschuh): Add in the gyro.
104 _integral_offset = 0.0;
105 _offset = 0.0;
brians343bc112013-02-10 01:53:46 +0000106 _gyro = gyro;
Austin Schuh4352ac62013-03-19 06:23:16 +0000107 _control_loop_driving = control_loop_driving;
brians343bc112013-02-10 01:53:46 +0000108 SetRawPosition(left, right);
brians343bc112013-02-10 01:53:46 +0000109 }
Austin Schuh4352ac62013-03-19 06:23:16 +0000110
111 void Update(bool update_observer, bool stop_motors) {
112 loop_->Update(update_observer, stop_motors);
brians343bc112013-02-10 01:53:46 +0000113 }
114
Austin Schuh4352ac62013-03-19 06:23:16 +0000115 void SendMotors(Drivetrain::Output *output) {
116 if (output) {
117 output->left_voltage = loop_->U(0, 0);
118 output->right_voltage = loop_->U(1, 0);
brians8ad74052013-03-16 21:04:51 +0000119 }
brians343bc112013-02-10 01:53:46 +0000120 }
121 void PrintMotors() const {
122 // 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 +0000123 ::Eigen::Matrix<double, 4, 1> E = loop_->R - loop_->X_hat;
124 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 +0000125 }
126
127 private:
Austin Schuh4352ac62013-03-19 06:23:16 +0000128 ::std::unique_ptr<StateFeedbackLoop<4, 2, 2>> loop_;
129
brians343bc112013-02-10 01:53:46 +0000130 double _integral_offset;
131 double _offset;
132 double _gyro;
133 double _left_goal;
134 double _right_goal;
135 double _raw_left;
136 double _raw_right;
Austin Schuh4352ac62013-03-19 06:23:16 +0000137 bool _control_loop_driving;
brians343bc112013-02-10 01:53:46 +0000138};
139
Austin Schuh2054f5f2013-10-27 14:54:10 -0700140class PolyDrivetrain {
141 public:
Austin Schuh427b3702013-11-02 13:44:09 -0700142
143 enum Gear {
144 HIGH,
145 LOW,
146 SHIFTING_UP,
147 SHIFTING_DOWN
148 };
149 // Stall Torque in N m
150 static constexpr double kStallTorque = 2.42;
151 // Stall Current in Amps
152 static constexpr double kStallCurrent = 133;
153 // Free Speed in RPM. Used number from last year.
154 static constexpr double kFreeSpeed = 4650.0;
155 // Free Current in Amps
156 static constexpr double kFreeCurrent = 2.7;
157 // Moment of inertia of the drivetrain in kg m^2
158 // Just borrowed from last year.
159 static constexpr double J = 6.4;
160 // Mass of the robot, in kg.
161 static constexpr double m = 68;
162 // Radius of the robot, in meters (from last year).
163 static constexpr double rb = 0.617998644 / 2.0;
Brian Silverman1a6590d2013-11-04 14:46:46 -0800164 static constexpr double kWheelRadius = 0.04445;
Austin Schuh427b3702013-11-02 13:44:09 -0700165 // Resistance of the motor, divided by the number of motors.
166 static constexpr double kR = (12.0 / kStallCurrent / 4 + 0.03) / (0.93 * 0.93);
167 // Motor velocity constant
168 static constexpr double Kv =
169 ((kFreeSpeed / 60.0 * 2.0 * M_PI) / (12.0 - kR * kFreeCurrent));
170 // Torque constant
171 static constexpr double Kt = kStallTorque / kStallCurrent;
Austin Schuh427b3702013-11-02 13:44:09 -0700172
Austin Schuh2054f5f2013-10-27 14:54:10 -0700173 PolyDrivetrain()
174 : U_Poly_((Eigen::Matrix<double, 4, 2>() << /*[[*/ 1, 0 /*]*/,
175 /*[*/ -1, 0 /*]*/,
176 /*[*/ 0, 1 /*]*/,
177 /*[*/ 0, -1 /*]]*/).finished(),
178 (Eigen::Matrix<double, 4, 1>() << /*[[*/ 12 /*]*/,
179 /*[*/ 12 /*]*/,
180 /*[*/ 12 /*]*/,
181 /*[*/ 12 /*]]*/).finished()),
Brian Silverman2c590c32013-11-04 18:08:54 -0800182 loop_(new StateFeedbackLoop<2, 2, 2>(
183 constants::GetValues().make_v_drivetrain_loop())),
Austin Schuh427b3702013-11-02 13:44:09 -0700184 left_cim_(new StateFeedbackLoop<1, 1, 1>(MakeCIMLoop())),
185 right_cim_(new StateFeedbackLoop<1, 1, 1>(MakeCIMLoop())),
186 ttrust_(1.1),
187 wheel_(0.0),
188 throttle_(0.0),
189 quickturn_(false),
190 stale_count_(0),
191 position_time_delta_(0.01),
192 left_gear_(LOW),
Brian Silvermande8fd552013-11-03 15:53:42 -0800193 right_gear_(LOW),
194 counter_(0) {
Austin Schuh2054f5f2013-10-27 14:54:10 -0700195
Austin Schuh427b3702013-11-02 13:44:09 -0700196 last_position_.Zero();
197 position_.Zero();
Austin Schuh2054f5f2013-10-27 14:54:10 -0700198 }
Austin Schuh427b3702013-11-02 13:44:09 -0700199 static bool IsInGear(Gear gear) { return gear == LOW || gear == HIGH; }
200
201 static double MotorSpeed(double shifter_position, double velocity) {
202 // TODO(austin): G_high, G_low and kWheelRadius
Brian Silvermande8fd552013-11-03 15:53:42 -0800203 if (shifter_position > 0.57) {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800204 return velocity / constants::GetValues().high_gear_ratio / kWheelRadius;
Austin Schuh427b3702013-11-02 13:44:09 -0700205 } else {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800206 return velocity / constants::GetValues().low_gear_ratio / kWheelRadius;
207 }
208 }
209
210 Gear ComputeGear(double velocity, Gear current) {
211 const double low_omega = MotorSpeed(0, ::std::abs(velocity));
212 const double high_omega = MotorSpeed(1.0, ::std::abs(velocity));
213
Brian Silverman1a6590d2013-11-04 14:46:46 -0800214 double high_torque = ((12.0 - high_omega / Kv) * Kt / kR);
215 double low_torque = ((12.0 - low_omega / Kv) * Kt / kR);
216 double high_power = high_torque * high_omega;
217 double low_power = low_torque * low_omega;
Brian Silverman55a930b2013-11-04 20:59:00 -0800218
219 // TODO(aschuh): Do this right!
220 if ((current == HIGH || high_power > low_power + 160) &&
221 ::std::abs(velocity) > 0.14) {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800222 return HIGH;
223 } else {
224 return LOW;
Austin Schuh427b3702013-11-02 13:44:09 -0700225 }
226 }
227
Austin Schuh2054f5f2013-10-27 14:54:10 -0700228 void SetGoal(double wheel, double throttle, bool quickturn, bool highgear) {
Brian Silvermande8fd552013-11-03 15:53:42 -0800229 const double kWheelNonLinearity = 0.3;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700230 // Apply a sin function that's scaled to make it feel better.
231 const double angular_range = M_PI_2 * kWheelNonLinearity;
232 wheel_ = sin(angular_range * wheel) / sin(angular_range);
233 wheel_ = sin(angular_range * wheel_) / sin(angular_range);
Austin Schuh2054f5f2013-10-27 14:54:10 -0700234 quickturn_ = quickturn;
Austin Schuh427b3702013-11-02 13:44:09 -0700235
Brian Silverman1a6590d2013-11-04 14:46:46 -0800236 static const double kThrottleDeadband = 0.05;
237 if (::std::abs(throttle) < kThrottleDeadband) {
238 throttle_ = 0;
239 } else {
240 throttle_ = copysign((::std::abs(throttle) - kThrottleDeadband) /
241 (1.0 - kThrottleDeadband), throttle);
242 }
243
Austin Schuh427b3702013-11-02 13:44:09 -0700244 // TODO(austin): Fix the upshift logic to include states.
Brian Silverman1a6590d2013-11-04 14:46:46 -0800245 Gear requested_gear;
Brian Silverman55a930b2013-11-04 20:59:00 -0800246 if (false) {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800247 const double current_left_velocity =
248 (position_.left_encoder - last_position_.left_encoder) /
249 position_time_delta_;
250 const double current_right_velocity =
251 (position_.right_encoder - last_position_.right_encoder) /
252 position_time_delta_;
253
254 Gear left_requested = ComputeGear(current_left_velocity, left_gear_);
255 Gear right_requested = ComputeGear(current_right_velocity, right_gear_);
256 requested_gear =
257 (left_requested == HIGH || right_requested == HIGH) ? HIGH : LOW;
258 } else {
259 requested_gear = highgear ? HIGH : LOW;
260 }
261
262 const Gear shift_up =
263 constants::GetValues().clutch_transmission ? HIGH : SHIFTING_UP;
264 const Gear shift_down =
265 constants::GetValues().clutch_transmission ? LOW : SHIFTING_DOWN;
Austin Schuh427b3702013-11-02 13:44:09 -0700266
267 if (left_gear_ != requested_gear) {
268 if (IsInGear(left_gear_)) {
269 if (requested_gear == HIGH) {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800270 left_gear_ = shift_up;
Austin Schuh427b3702013-11-02 13:44:09 -0700271 } else {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800272 left_gear_ = shift_down;
Austin Schuh427b3702013-11-02 13:44:09 -0700273 }
Brian Silverman55a930b2013-11-04 20:59:00 -0800274 } else {
275 if (requested_gear == HIGH && left_gear_ == SHIFTING_DOWN) {
276 left_gear_ = SHIFTING_UP;
277 } else if (requested_gear == LOW && left_gear_ == SHIFTING_UP) {
278 left_gear_ = SHIFTING_DOWN;
279 }
Austin Schuh427b3702013-11-02 13:44:09 -0700280 }
281 }
282 if (right_gear_ != requested_gear) {
283 if (IsInGear(right_gear_)) {
284 if (requested_gear == HIGH) {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800285 right_gear_ = shift_up;
Austin Schuh427b3702013-11-02 13:44:09 -0700286 } else {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800287 right_gear_ = shift_down;
Austin Schuh427b3702013-11-02 13:44:09 -0700288 }
Brian Silverman55a930b2013-11-04 20:59:00 -0800289 } else {
290 if (requested_gear == HIGH && right_gear_ == SHIFTING_DOWN) {
291 right_gear_ = SHIFTING_UP;
292 } else if (requested_gear == LOW && right_gear_ == SHIFTING_UP) {
293 right_gear_ = SHIFTING_DOWN;
294 }
Austin Schuh427b3702013-11-02 13:44:09 -0700295 }
Austin Schuh2054f5f2013-10-27 14:54:10 -0700296 }
297 }
Austin Schuh427b3702013-11-02 13:44:09 -0700298 void SetPosition(const Drivetrain::Position *position) {
299 if (position == NULL) {
300 ++stale_count_;
301 } else {
302 last_position_ = position_;
303 position_ = *position;
304 position_time_delta_ = (stale_count_ + 1) * 0.01;
305 stale_count_ = 0;
306 }
307
308 if (position) {
309 // Switch to the correct controller.
Brian Silvermande8fd552013-11-03 15:53:42 -0800310 // TODO(austin): Un-hard code 0.57
311 if (position->left_shifter_position < 0.57) {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800312 if (position->right_shifter_position < 0.57 || right_gear_ == LOW) {
Brian Silvermande8fd552013-11-03 15:53:42 -0800313 LOG(DEBUG, "Loop Left low, Right low\n");
Austin Schuh427b3702013-11-02 13:44:09 -0700314 loop_->set_controller_index(0);
315 } else {
Brian Silvermande8fd552013-11-03 15:53:42 -0800316 LOG(DEBUG, "Loop Left low, Right high\n");
Austin Schuh427b3702013-11-02 13:44:09 -0700317 loop_->set_controller_index(1);
318 }
319 } else {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800320 if (position->right_shifter_position < 0.57 || left_gear_ == LOW) {
Brian Silvermande8fd552013-11-03 15:53:42 -0800321 LOG(DEBUG, "Loop Left high, Right low\n");
Austin Schuh427b3702013-11-02 13:44:09 -0700322 loop_->set_controller_index(2);
323 } else {
Brian Silvermande8fd552013-11-03 15:53:42 -0800324 LOG(DEBUG, "Loop Left high, Right high\n");
Austin Schuh427b3702013-11-02 13:44:09 -0700325 loop_->set_controller_index(3);
326 }
327 }
Brian Silvermande8fd552013-11-03 15:53:42 -0800328 switch (left_gear_) {
329 case LOW:
330 LOG(DEBUG, "Left is in low\n");
331 break;
332 case HIGH:
333 LOG(DEBUG, "Left is in high\n");
334 break;
335 case SHIFTING_UP:
336 LOG(DEBUG, "Left is shifting up\n");
337 break;
338 case SHIFTING_DOWN:
339 LOG(DEBUG, "Left is shifting down\n");
340 break;
341 }
342 switch (right_gear_) {
343 case LOW:
344 LOG(DEBUG, "Right is in low\n");
345 break;
346 case HIGH:
347 LOG(DEBUG, "Right is in high\n");
348 break;
349 case SHIFTING_UP:
350 LOG(DEBUG, "Right is shifting up\n");
351 break;
352 case SHIFTING_DOWN:
353 LOG(DEBUG, "Right is shifting down\n");
354 break;
355 }
Austin Schuh427b3702013-11-02 13:44:09 -0700356 // TODO(austin): Constants.
357 if (position->left_shifter_position > 0.9 && left_gear_ == SHIFTING_UP) {
358 left_gear_ = HIGH;
359 }
360 if (position->left_shifter_position < 0.1 && left_gear_ == SHIFTING_DOWN) {
361 left_gear_ = LOW;
362 }
363 if (position->right_shifter_position > 0.9 && right_gear_ == SHIFTING_UP) {
364 right_gear_ = HIGH;
365 }
366 if (position->right_shifter_position < 0.1 && right_gear_ == SHIFTING_DOWN) {
367 right_gear_ = LOW;
368 }
369 }
370 }
371
Austin Schuh2054f5f2013-10-27 14:54:10 -0700372 double FilterVelocity(double throttle) {
373 const Eigen::Matrix<double, 2, 2> FF =
374 loop_->B().inverse() *
375 (Eigen::Matrix<double, 2, 2>::Identity() - loop_->A());
376
377 constexpr int kHighGearController = 3;
378 const Eigen::Matrix<double, 2, 2> FF_high =
379 loop_->controller(kHighGearController).plant.B.inverse() *
380 (Eigen::Matrix<double, 2, 2>::Identity() -
381 loop_->controller(kHighGearController).plant.A);
382
383 ::Eigen::Matrix<double, 1, 2> FF_sum = FF.colwise().sum();
384 int min_FF_sum_index;
385 const double min_FF_sum = FF_sum.minCoeff(&min_FF_sum_index);
386 const double min_K_sum = loop_->K().col(min_FF_sum_index).sum();
387 const double high_min_FF_sum = FF_high.col(0).sum();
388
389 const double adjusted_ff_voltage = ::aos::Clip(
390 throttle * 12.0 * min_FF_sum / high_min_FF_sum, -12.0, 12.0);
391 return ((adjusted_ff_voltage +
392 ttrust_ * min_K_sum * (loop_->X_hat(0, 0) + loop_->X_hat(1, 0)) /
393 2.0) /
394 (ttrust_ * min_K_sum + min_FF_sum));
395 }
396
Brian Silverman718b1d72013-10-28 16:22:45 -0700397 double MaxVelocity() {
398 const Eigen::Matrix<double, 2, 2> FF =
399 loop_->B().inverse() *
400 (Eigen::Matrix<double, 2, 2>::Identity() - loop_->A());
401
402 constexpr int kHighGearController = 3;
403 const Eigen::Matrix<double, 2, 2> FF_high =
404 loop_->controller(kHighGearController).plant.B.inverse() *
405 (Eigen::Matrix<double, 2, 2>::Identity() -
406 loop_->controller(kHighGearController).plant.A);
407
408 ::Eigen::Matrix<double, 1, 2> FF_sum = FF.colwise().sum();
409 int min_FF_sum_index;
410 const double min_FF_sum = FF_sum.minCoeff(&min_FF_sum_index);
411 //const double min_K_sum = loop_->K().col(min_FF_sum_index).sum();
412 const double high_min_FF_sum = FF_high.col(0).sum();
413
414 const double adjusted_ff_voltage = ::aos::Clip(
415 12.0 * min_FF_sum / high_min_FF_sum, -12.0, 12.0);
416 return adjusted_ff_voltage / min_FF_sum;
417 }
418
Austin Schuh2054f5f2013-10-27 14:54:10 -0700419 void Update() {
Austin Schuh427b3702013-11-02 13:44:09 -0700420 // TODO(austin): Observer for the current velocity instead of difference
421 // calculations.
Brian Silvermande8fd552013-11-03 15:53:42 -0800422 ++counter_;
Austin Schuh427b3702013-11-02 13:44:09 -0700423 const double current_left_velocity =
Brian Silvermande8fd552013-11-03 15:53:42 -0800424 (position_.left_encoder - last_position_.left_encoder) /
Austin Schuh427b3702013-11-02 13:44:09 -0700425 position_time_delta_;
426 const double current_right_velocity =
Brian Silvermande8fd552013-11-03 15:53:42 -0800427 (position_.right_encoder - last_position_.right_encoder) /
Austin Schuh427b3702013-11-02 13:44:09 -0700428 position_time_delta_;
429 const double left_motor_speed =
430 MotorSpeed(position_.left_shifter_position, current_left_velocity);
431 const double right_motor_speed =
432 MotorSpeed(position_.right_shifter_position, current_right_velocity);
Austin Schuh2054f5f2013-10-27 14:54:10 -0700433
Austin Schuh427b3702013-11-02 13:44:09 -0700434 // Reset the CIM model to the current conditions to be ready for when we shift.
435 if (IsInGear(left_gear_)) {
436 left_cim_->X_hat(0, 0) = left_motor_speed;
Brian Silvermande8fd552013-11-03 15:53:42 -0800437 LOG(DEBUG, "Setting left CIM to %f at robot speed %f\n", left_motor_speed,
438 current_left_velocity);
Austin Schuh427b3702013-11-02 13:44:09 -0700439 }
440 if (IsInGear(right_gear_)) {
Brian Silvermande8fd552013-11-03 15:53:42 -0800441 right_cim_->X_hat(0, 0) = right_motor_speed;
442 LOG(DEBUG, "Setting right CIM to %f at robot speed %f\n",
443 right_motor_speed, current_right_velocity);
Austin Schuh427b3702013-11-02 13:44:09 -0700444 }
Brian Silvermande8fd552013-11-03 15:53:42 -0800445 LOG(DEBUG, "robot speed l=%f r=%f\n", current_left_velocity,
446 current_right_velocity);
Austin Schuh2054f5f2013-10-27 14:54:10 -0700447
Austin Schuh427b3702013-11-02 13:44:09 -0700448 if (IsInGear(left_gear_) && IsInGear(right_gear_)) {
449 // FF * X = U (steady state)
450 const Eigen::Matrix<double, 2, 2> FF =
451 loop_->B().inverse() *
452 (Eigen::Matrix<double, 2, 2>::Identity() - loop_->A());
453
454 // Invert the plant to figure out how the velocity filter would have to
455 // work
456 // out in order to filter out the forwards negative inertia.
457 // This math assumes that the left and right power and velocity are
458 // equals,
459 // and that the plant is the same on the left and right.
460 const double fvel = FilterVelocity(throttle_);
461
462 const double sign_svel = wheel_ * ((fvel > 0.0) ? 1.0 : -1.0);
463 double steering_velocity;
464 if (quickturn_) {
465 steering_velocity = wheel_ * MaxVelocity();
466 } else {
467 steering_velocity = ::std::abs(fvel) * wheel_;
468 }
469 const double left_velocity = fvel - steering_velocity;
470 const double right_velocity = fvel + steering_velocity;
471
472 // Integrate velocity to get the position.
473 // This position is used to get integral control.
474 loop_->R << left_velocity, right_velocity;
475
476 if (!quickturn_) {
477 // K * R = w
478 Eigen::Matrix<double, 1, 2> equality_k;
479 equality_k << 1 + sign_svel, -(1 - sign_svel);
480 const double equality_w = 0.0;
481
482 // Construct a constraint on R by manipulating the constraint on U
483 ::aos::controls::HPolytope<2> R_poly = ::aos::controls::HPolytope<2>(
484 U_Poly_.H() * (loop_->K() + FF),
485 U_Poly_.k() + U_Poly_.H() * loop_->K() * loop_->X_hat);
486
487 // Limit R back inside the box.
488 loop_->R = CoerceGoal(R_poly, equality_k, equality_w, loop_->R);
489 }
490
491 const Eigen::Matrix<double, 2, 1> FF_volts = FF * loop_->R;
492 const Eigen::Matrix<double, 2, 1> U_ideal =
493 loop_->K() * (loop_->R - loop_->X_hat) + FF_volts;
494
495 for (int i = 0; i < 2; i++) {
496 loop_->U[i] = ::aos::Clip(U_ideal[i], -12, 12);
497 }
Brian Silvermande8fd552013-11-03 15:53:42 -0800498
499 // TODO(austin): Model this better.
500 // TODO(austin): Feed back?
501 loop_->X_hat = loop_->A() * loop_->X_hat + loop_->B() * loop_->U;
Brian Silverman718b1d72013-10-28 16:22:45 -0700502 } else {
Austin Schuh427b3702013-11-02 13:44:09 -0700503 // Any motor is not in gear. Speed match.
504 ::Eigen::Matrix<double, 1, 1> R_left;
505 R_left(0, 0) = left_motor_speed;
Brian Silvermande8fd552013-11-03 15:53:42 -0800506 const double wiggle = (static_cast<double>((counter_ % 4) / 2) - 0.5) * 3.5;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700507
Brian Silvermande8fd552013-11-03 15:53:42 -0800508 loop_->U(0, 0) =
509 ::aos::Clip((R_left / Kv)(0, 0) + wiggle, -position_.battery_voltage,
510 position_.battery_voltage);
511 right_cim_->X_hat = right_cim_->A() * right_cim_->X_hat +
512 right_cim_->B() * loop_->U(0, 0);
Austin Schuh2054f5f2013-10-27 14:54:10 -0700513
Austin Schuh427b3702013-11-02 13:44:09 -0700514 ::Eigen::Matrix<double, 1, 1> R_right;
515 R_right(0, 0) = right_motor_speed;
Brian Silvermande8fd552013-11-03 15:53:42 -0800516 loop_->U(1, 0) =
517 ::aos::Clip((R_right / Kv)(0, 0) + wiggle, -position_.battery_voltage,
518 position_.battery_voltage);
519 right_cim_->X_hat = right_cim_->A() * right_cim_->X_hat +
520 right_cim_->B() * loop_->U(1, 0);
521 loop_->U *= 12.0 / position_.battery_voltage;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700522 }
Austin Schuh2054f5f2013-10-27 14:54:10 -0700523 }
524
525 void SendMotors(Drivetrain::Output *output) {
Brian Silvermande8fd552013-11-03 15:53:42 -0800526 if (output != NULL) {
527 output->left_voltage = loop_->U(0, 0);
528 output->right_voltage = loop_->U(1, 0);
529 }
Austin Schuh427b3702013-11-02 13:44:09 -0700530 // Go in high gear if anything wants to be in high gear.
531 // TODO(austin): Seperate these.
532 if (left_gear_ == HIGH || left_gear_ == SHIFTING_UP ||
533 right_gear_ == HIGH || right_gear_ == SHIFTING_UP) {
Austin Schuh2054f5f2013-10-27 14:54:10 -0700534 shifters.MakeWithBuilder().set(false).Send();
535 } else {
536 shifters.MakeWithBuilder().set(true).Send();
537 }
538 }
539
540 private:
541 const ::aos::controls::HPolytope<2> U_Poly_;
542
543 ::std::unique_ptr<StateFeedbackLoop<2, 2, 2>> loop_;
Austin Schuh427b3702013-11-02 13:44:09 -0700544 ::std::unique_ptr<StateFeedbackLoop<1, 1, 1>> left_cim_;
545 ::std::unique_ptr<StateFeedbackLoop<1, 1, 1>> right_cim_;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700546
Austin Schuh427b3702013-11-02 13:44:09 -0700547 const double ttrust_;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700548 double wheel_;
549 double throttle_;
550 bool quickturn_;
Austin Schuh427b3702013-11-02 13:44:09 -0700551 int stale_count_;
552 double position_time_delta_;
553 Gear left_gear_;
554 Gear right_gear_;
555 Drivetrain::Position last_position_;
556 Drivetrain::Position position_;
Brian Silvermande8fd552013-11-03 15:53:42 -0800557 int counter_;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700558};
559
560
brians343bc112013-02-10 01:53:46 +0000561class DrivetrainMotorsOL {
562 public:
563 DrivetrainMotorsOL() {
564 _old_wheel = 0.0;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700565 wheel_ = 0.0;
566 throttle_ = 0.0;
567 quickturn_ = false;
568 highgear_ = true;
brians343bc112013-02-10 01:53:46 +0000569 _neg_inertia_accumulator = 0.0;
570 _left_pwm = 0.0;
571 _right_pwm = 0.0;
572 }
573 void SetGoal(double wheel, double throttle, bool quickturn, bool highgear) {
Austin Schuh2054f5f2013-10-27 14:54:10 -0700574 wheel_ = wheel;
575 throttle_ = throttle;
576 quickturn_ = quickturn;
577 highgear_ = highgear;
brians343bc112013-02-10 01:53:46 +0000578 _left_pwm = 0.0;
579 _right_pwm = 0.0;
580 }
Austin Schuh427b3702013-11-02 13:44:09 -0700581 void Update() {
brians343bc112013-02-10 01:53:46 +0000582 double overPower;
583 float sensitivity = 1.7;
584 float angular_power;
585 float linear_power;
586 double wheel;
587
Austin Schuh2054f5f2013-10-27 14:54:10 -0700588 double neg_inertia = wheel_ - _old_wheel;
589 _old_wheel = wheel_;
brians343bc112013-02-10 01:53:46 +0000590
591 double wheelNonLinearity;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700592 if (highgear_) {
Brian Silverman77a76002013-03-16 20:09:00 -0700593 wheelNonLinearity = 0.1; // used to be csvReader->TURN_NONLIN_HIGH
brians343bc112013-02-10 01:53:46 +0000594 // Apply a sin function that's scaled to make it feel better.
595 const double angular_range = M_PI / 2.0 * wheelNonLinearity;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700596 wheel = sin(angular_range * wheel_) / sin(angular_range);
Brian Silverman77a76002013-03-16 20:09:00 -0700597 wheel = sin(angular_range * wheel) / sin(angular_range);
brians343bc112013-02-10 01:53:46 +0000598 } else {
Brian Silvermandf43ec22013-03-16 23:48:29 -0700599 wheelNonLinearity = 0.2; // used to be csvReader->TURN_NONLIN_LOW
brians343bc112013-02-10 01:53:46 +0000600 // Apply a sin function that's scaled to make it feel better.
601 const double angular_range = M_PI / 2.0 * wheelNonLinearity;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700602 wheel = sin(angular_range * wheel_) / sin(angular_range);
Brian Silverman77a76002013-03-16 20:09:00 -0700603 wheel = sin(angular_range * wheel) / sin(angular_range);
604 wheel = sin(angular_range * wheel) / sin(angular_range);
brians343bc112013-02-10 01:53:46 +0000605 }
606
Brian Silvermandf43ec22013-03-16 23:48:29 -0700607 static const double kThrottleDeadband = 0.05;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700608 if (::std::abs(throttle_) < kThrottleDeadband) {
609 throttle_ = 0;
Brian Silvermandf43ec22013-03-16 23:48:29 -0700610 } else {
Austin Schuh2054f5f2013-10-27 14:54:10 -0700611 throttle_ = copysign((::std::abs(throttle_) - kThrottleDeadband) /
612 (1.0 - kThrottleDeadband), throttle_);
Brian Silvermandf43ec22013-03-16 23:48:29 -0700613 }
614
brians343bc112013-02-10 01:53:46 +0000615 double neg_inertia_scalar;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700616 if (highgear_) {
Brian Silvermandf43ec22013-03-16 23:48:29 -0700617 neg_inertia_scalar = 8.0; // used to be csvReader->NEG_INTERTIA_HIGH
brians343bc112013-02-10 01:53:46 +0000618 sensitivity = 1.22; // used to be csvReader->SENSE_HIGH
619 } else {
620 if (wheel * neg_inertia > 0) {
Brian Silvermandf43ec22013-03-16 23:48:29 -0700621 neg_inertia_scalar = 5; // used to be csvReader->NEG_INERTIA_LOW_MORE
brians343bc112013-02-10 01:53:46 +0000622 } else {
Brian Silvermandf43ec22013-03-16 23:48:29 -0700623 if (::std::abs(wheel) > 0.65) {
624 neg_inertia_scalar = 5; // used to be csvReader->NEG_INTERTIA_LOW_LESS_EXT
brians343bc112013-02-10 01:53:46 +0000625 } else {
Brian Silvermandf43ec22013-03-16 23:48:29 -0700626 neg_inertia_scalar = 5; // used to be csvReader->NEG_INTERTIA_LOW_LESS
brians343bc112013-02-10 01:53:46 +0000627 }
628 }
629 sensitivity = 1.24; // used to be csvReader->SENSE_LOW
brians343bc112013-02-10 01:53:46 +0000630 }
631 double neg_inertia_power = neg_inertia * neg_inertia_scalar;
632 _neg_inertia_accumulator += neg_inertia_power;
633
634 wheel = wheel + _neg_inertia_accumulator;
635 if (_neg_inertia_accumulator > 1) {
636 _neg_inertia_accumulator -= 1;
637 } else if (_neg_inertia_accumulator < -1) {
638 _neg_inertia_accumulator += 1;
639 } else {
640 _neg_inertia_accumulator = 0;
641 }
642
Austin Schuh2054f5f2013-10-27 14:54:10 -0700643 linear_power = throttle_;
brians343bc112013-02-10 01:53:46 +0000644
Austin Schuh2054f5f2013-10-27 14:54:10 -0700645 if (quickturn_) {
brians343bc112013-02-10 01:53:46 +0000646 double qt_angular_power = wheel;
Brian Silvermandf43ec22013-03-16 23:48:29 -0700647 if (::std::abs(linear_power) < 0.2) {
brians343bc112013-02-10 01:53:46 +0000648 if (qt_angular_power > 1) qt_angular_power = 1.0;
649 if (qt_angular_power < -1) qt_angular_power = -1.0;
650 } else {
651 qt_angular_power = 0.0;
652 }
brians343bc112013-02-10 01:53:46 +0000653 overPower = 1.0;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700654 if (highgear_) {
brians343bc112013-02-10 01:53:46 +0000655 sensitivity = 1.0;
656 } else {
657 sensitivity = 1.0;
658 }
659 angular_power = wheel;
660 } else {
661 overPower = 0.0;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700662 angular_power = ::std::abs(throttle_) * wheel * sensitivity;
brians343bc112013-02-10 01:53:46 +0000663 }
664
665 _right_pwm = _left_pwm = linear_power;
666 _left_pwm += angular_power;
667 _right_pwm -= angular_power;
668
669 if (_left_pwm > 1.0) {
670 _right_pwm -= overPower*(_left_pwm - 1.0);
671 _left_pwm = 1.0;
672 } else if (_right_pwm > 1.0) {
673 _left_pwm -= overPower*(_right_pwm - 1.0);
674 _right_pwm = 1.0;
675 } else if (_left_pwm < -1.0) {
676 _right_pwm += overPower*(-1.0 - _left_pwm);
677 _left_pwm = -1.0;
678 } else if (_right_pwm < -1.0) {
679 _left_pwm += overPower*(-1.0 - _right_pwm);
680 _right_pwm = -1.0;
681 }
682 }
683
684 void SendMotors(Drivetrain::Output *output) {
Brian Silvermandf43ec22013-03-16 23:48:29 -0700685 LOG(DEBUG, "left pwm: %f right pwm: %f wheel: %f throttle: %f\n",
Austin Schuh2054f5f2013-10-27 14:54:10 -0700686 _left_pwm, _right_pwm, wheel_, throttle_);
brians8ad74052013-03-16 21:04:51 +0000687 if (output) {
688 output->left_voltage = _left_pwm * 12.0;
689 output->right_voltage = _right_pwm * 12.0;
690 }
Austin Schuh2054f5f2013-10-27 14:54:10 -0700691 if (highgear_) {
brians343bc112013-02-10 01:53:46 +0000692 shifters.MakeWithBuilder().set(false).Send();
693 } else {
694 shifters.MakeWithBuilder().set(true).Send();
695 }
696 }
697
698 private:
699 double _old_wheel;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700700 double wheel_;
701 double throttle_;
702 bool quickturn_;
703 bool highgear_;
brians343bc112013-02-10 01:53:46 +0000704 double _neg_inertia_accumulator;
705 double _left_pwm;
706 double _right_pwm;
brians343bc112013-02-10 01:53:46 +0000707};
708
709void DrivetrainLoop::RunIteration(const Drivetrain::Goal *goal,
710 const Drivetrain::Position *position,
711 Drivetrain::Output *output,
712 Drivetrain::Status * /*status*/) {
713 // TODO(aschuh): These should be members of the class.
714 static DrivetrainMotorsSS dt_closedloop;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700715 static PolyDrivetrain dt_openloop;
brians343bc112013-02-10 01:53:46 +0000716
717 bool bad_pos = false;
Austin Schuh427b3702013-11-02 13:44:09 -0700718 if (position == nullptr) {
James Kuszmaul3f354742013-03-10 17:27:56 -0700719 LOG(WARNING, "no position\n");
brians343bc112013-02-10 01:53:46 +0000720 bad_pos = true;
721 }
722
723 double wheel = goal->steering;
724 double throttle = goal->throttle;
725 bool quickturn = goal->quickturn;
726 bool highgear = goal->highgear;
727
728 bool control_loop_driving = goal->control_loop_driving;
729 double left_goal = goal->left_goal;
730 double right_goal = goal->right_goal;
731
Austin Schuh2054f5f2013-10-27 14:54:10 -0700732 dt_closedloop.SetGoal(left_goal, goal->left_velocity_goal, right_goal,
733 goal->right_velocity_goal);
brians343bc112013-02-10 01:53:46 +0000734 if (!bad_pos) {
735 const double left_encoder = position->left_encoder;
736 const double right_encoder = position->right_encoder;
737 if (gyro.FetchLatest()) {
Brian Silverman96d9cea2013-11-12 21:10:50 -0800738 LOG(DEBUG, "gyro %f\n", gyro->angle);
Austin Schuh2054f5f2013-10-27 14:54:10 -0700739 dt_closedloop.SetPosition(left_encoder, right_encoder, gyro->angle,
740 control_loop_driving);
brians343bc112013-02-10 01:53:46 +0000741 } else {
742 dt_closedloop.SetRawPosition(left_encoder, right_encoder);
743 }
744 }
Austin Schuh427b3702013-11-02 13:44:09 -0700745 dt_openloop.SetPosition(position);
Austin Schuh4352ac62013-03-19 06:23:16 +0000746 dt_closedloop.Update(position, output == NULL);
brians343bc112013-02-10 01:53:46 +0000747 dt_openloop.SetGoal(wheel, throttle, quickturn, highgear);
748 dt_openloop.Update();
Brian Silverman2845c4c2013-03-16 19:54:08 -0700749 if (control_loop_driving) {
750 dt_closedloop.SendMotors(output);
751 } else {
752 dt_openloop.SendMotors(output);
brians343bc112013-02-10 01:53:46 +0000753 }
754}
755
756} // namespace control_loops
757} // namespace frc971