blob: f7affb9cddaf6757369e422ee2d8634ee2f39ae2 [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"
Brian Silvermanfd5e2a32014-02-22 20:02:39 -080014#include "aos/common/logging/matrix_logging.h"
Brian Silverman61e41fd2014-02-16 19:08:50 -080015
James Kuszmaulf254c1a2013-03-10 16:31:26 -070016#include "frc971/control_loops/state_feedback_loop.h"
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"
Ben Fredrickson81ba2d52014-03-02 08:21:46 +000019#include "frc971/queues/othersensors.q.h"
Brian Silverman1a6590d2013-11-04 14:46:46 -080020#include "frc971/constants.h"
brians343bc112013-02-10 01:53:46 +000021
Ben Fredrickson81ba2d52014-03-02 08:21:46 +000022using frc971::sensors::othersensors;
brians343bc112013-02-10 01:53:46 +000023
24namespace frc971 {
25namespace control_loops {
26
27// Width of the robot.
28const double width = 22.0 / 100.0 * 2.54;
29
Austin Schuh2054f5f2013-10-27 14:54:10 -070030Eigen::Matrix<double, 2, 1> CoerceGoal(aos::controls::HPolytope<2> &region,
31 const Eigen::Matrix<double, 1, 2> &K,
32 double w,
33 const Eigen::Matrix<double, 2, 1> &R) {
34 if (region.IsInside(R)) {
35 return R;
36 }
37 Eigen::Matrix<double, 2, 1> parallel_vector;
38 Eigen::Matrix<double, 2, 1> perpendicular_vector;
39 perpendicular_vector = K.transpose().normalized();
40 parallel_vector << perpendicular_vector(1, 0), -perpendicular_vector(0, 0);
41
42 aos::controls::HPolytope<1> t_poly(
43 region.H() * parallel_vector,
44 region.k() - region.H() * perpendicular_vector * w);
45
46 Eigen::Matrix<double, 1, Eigen::Dynamic> vertices = t_poly.Vertices();
47 if (vertices.innerSize() > 0) {
48 double min_distance_sqr = 0;
49 Eigen::Matrix<double, 2, 1> closest_point;
50 for (int i = 0; i < vertices.innerSize(); i++) {
51 Eigen::Matrix<double, 2, 1> point;
52 point = parallel_vector * vertices(0, i) + perpendicular_vector * w;
53 const double length = (R - point).squaredNorm();
54 if (i == 0 || length < min_distance_sqr) {
55 closest_point = point;
56 min_distance_sqr = length;
57 }
58 }
59 return closest_point;
60 } else {
61 Eigen::Matrix<double, 2, Eigen::Dynamic> region_vertices =
62 region.Vertices();
Brian Silverman41abe012014-02-08 18:25:02 -080063 double min_distance = INFINITY;
Austin Schuh2054f5f2013-10-27 14:54:10 -070064 int closest_i = 0;
65 for (int i = 0; i < region_vertices.outerSize(); i++) {
66 const double length = ::std::abs(
67 (perpendicular_vector.transpose() * (region_vertices.col(i)))(0, 0));
68 if (i == 0 || length < min_distance) {
69 closest_i = i;
70 min_distance = length;
71 }
72 }
73 return region_vertices.col(closest_i);
74 }
75}
76
Austin Schuh4352ac62013-03-19 06:23:16 +000077class DrivetrainMotorsSS {
brians343bc112013-02-10 01:53:46 +000078 public:
Brian Silverman2c590c32013-11-04 18:08:54 -080079 DrivetrainMotorsSS()
80 : loop_(new StateFeedbackLoop<4, 2, 2>(
81 constants::GetValues().make_drivetrain_loop())) {
brians343bc112013-02-10 01:53:46 +000082 _offset = 0;
83 _integral_offset = 0;
84 _left_goal = 0.0;
85 _right_goal = 0.0;
86 _raw_left = 0.0;
87 _raw_right = 0.0;
Austin Schuh4352ac62013-03-19 06:23:16 +000088 _control_loop_driving = false;
brians343bc112013-02-10 01:53:46 +000089 }
90 void SetGoal(double left, double left_velocity, double right, double right_velocity) {
91 _left_goal = left;
92 _right_goal = right;
Austin Schuh4352ac62013-03-19 06:23:16 +000093 loop_->R << left, left_velocity, right, right_velocity;
brians343bc112013-02-10 01:53:46 +000094 }
95 void SetRawPosition(double left, double right) {
96 _raw_right = right;
97 _raw_left = left;
Austin Schuhf9286cd2014-02-11 00:51:09 -080098 Eigen::Matrix<double, 2, 1> Y;
99 Y << left, right;
100 loop_->Correct(Y);
brians343bc112013-02-10 01:53:46 +0000101 }
Austin Schuh4352ac62013-03-19 06:23:16 +0000102 void SetPosition(
103 double left, double right, double gyro, bool control_loop_driving) {
brians343bc112013-02-10 01:53:46 +0000104 // Decay the offset quickly because this gyro is great.
105 _offset = (0.25) * (right - left - gyro * width) / 2.0 + 0.75 * _offset;
Brian Silvermande8fd552013-11-03 15:53:42 -0800106 //const double angle_error = (_right_goal - _left_goal) / width - (_raw_right - _offset - _raw_left - _offset) / width;
Austin Schuh4352ac62013-03-19 06:23:16 +0000107 // TODO(aschuh): Add in the gyro.
108 _integral_offset = 0.0;
109 _offset = 0.0;
brians343bc112013-02-10 01:53:46 +0000110 _gyro = gyro;
Austin Schuh4352ac62013-03-19 06:23:16 +0000111 _control_loop_driving = control_loop_driving;
brians343bc112013-02-10 01:53:46 +0000112 SetRawPosition(left, right);
brians343bc112013-02-10 01:53:46 +0000113 }
Austin Schuh4352ac62013-03-19 06:23:16 +0000114
Ben Fredrickson890c3fe2014-03-02 00:15:16 +0000115 void SetExternalMotors(double left_voltage, double right_voltage) {
116 loop_->U << left_voltage, right_voltage;
117 }
118
Austin Schuhf9286cd2014-02-11 00:51:09 -0800119 void Update(bool stop_motors) {
Ben Fredrickson890c3fe2014-03-02 00:15:16 +0000120 if (_control_loop_driving) {
121 loop_->Update(stop_motors);
122 } else {
123 if (stop_motors) {
124 loop_->U.setZero();
125 loop_->U_uncapped.setZero();
126 }
127 loop_->UpdateObserver();
128 }
129 }
130
131 double GetEstimatedRobotSpeed() {
132 // lets just call the average of left and right velocities close enough
133 return (loop_->X_hat(1, 0) + loop_->X_hat(3, 0)) / 2;
134 }
135
136 double GetEstimatedLeftEncoder() {
137 // lets just call the average of left and right velocities close enough
138 return loop_->X_hat(0, 0);
139 }
140
141 double GetEstimatedRightEncoder() {
142 return loop_->X_hat(2, 0);
brians343bc112013-02-10 01:53:46 +0000143 }
144
Austin Schuh4352ac62013-03-19 06:23:16 +0000145 void SendMotors(Drivetrain::Output *output) {
146 if (output) {
147 output->left_voltage = loop_->U(0, 0);
148 output->right_voltage = loop_->U(1, 0);
brians8ad74052013-03-16 21:04:51 +0000149 }
brians343bc112013-02-10 01:53:46 +0000150 }
151 void PrintMotors() const {
Austin Schuh4352ac62013-03-19 06:23:16 +0000152 ::Eigen::Matrix<double, 4, 1> E = loop_->R - loop_->X_hat;
Brian Silvermanfd5e2a32014-02-22 20:02:39 -0800153 LOG_MATRIX(DEBUG, "E", E);
brians343bc112013-02-10 01:53:46 +0000154 }
155
156 private:
Austin Schuh4352ac62013-03-19 06:23:16 +0000157 ::std::unique_ptr<StateFeedbackLoop<4, 2, 2>> loop_;
158
brians343bc112013-02-10 01:53:46 +0000159 double _integral_offset;
160 double _offset;
161 double _gyro;
162 double _left_goal;
163 double _right_goal;
164 double _raw_left;
165 double _raw_right;
Austin Schuh4352ac62013-03-19 06:23:16 +0000166 bool _control_loop_driving;
brians343bc112013-02-10 01:53:46 +0000167};
168
Austin Schuh2054f5f2013-10-27 14:54:10 -0700169class PolyDrivetrain {
170 public:
Austin Schuh427b3702013-11-02 13:44:09 -0700171
172 enum Gear {
173 HIGH,
174 LOW,
175 SHIFTING_UP,
176 SHIFTING_DOWN
177 };
178 // Stall Torque in N m
179 static constexpr double kStallTorque = 2.42;
180 // Stall Current in Amps
181 static constexpr double kStallCurrent = 133;
182 // Free Speed in RPM. Used number from last year.
183 static constexpr double kFreeSpeed = 4650.0;
184 // Free Current in Amps
185 static constexpr double kFreeCurrent = 2.7;
186 // Moment of inertia of the drivetrain in kg m^2
187 // Just borrowed from last year.
188 static constexpr double J = 6.4;
189 // Mass of the robot, in kg.
190 static constexpr double m = 68;
191 // Radius of the robot, in meters (from last year).
192 static constexpr double rb = 0.617998644 / 2.0;
Brian Silverman1a6590d2013-11-04 14:46:46 -0800193 static constexpr double kWheelRadius = 0.04445;
Austin Schuh427b3702013-11-02 13:44:09 -0700194 // Resistance of the motor, divided by the number of motors.
195 static constexpr double kR = (12.0 / kStallCurrent / 4 + 0.03) / (0.93 * 0.93);
196 // Motor velocity constant
197 static constexpr double Kv =
198 ((kFreeSpeed / 60.0 * 2.0 * M_PI) / (12.0 - kR * kFreeCurrent));
199 // Torque constant
200 static constexpr double Kt = kStallTorque / kStallCurrent;
Austin Schuh427b3702013-11-02 13:44:09 -0700201
Austin Schuh2054f5f2013-10-27 14:54:10 -0700202 PolyDrivetrain()
203 : U_Poly_((Eigen::Matrix<double, 4, 2>() << /*[[*/ 1, 0 /*]*/,
204 /*[*/ -1, 0 /*]*/,
205 /*[*/ 0, 1 /*]*/,
206 /*[*/ 0, -1 /*]]*/).finished(),
207 (Eigen::Matrix<double, 4, 1>() << /*[[*/ 12 /*]*/,
208 /*[*/ 12 /*]*/,
209 /*[*/ 12 /*]*/,
210 /*[*/ 12 /*]]*/).finished()),
Brian Silverman2c590c32013-11-04 18:08:54 -0800211 loop_(new StateFeedbackLoop<2, 2, 2>(
212 constants::GetValues().make_v_drivetrain_loop())),
Austin Schuh427b3702013-11-02 13:44:09 -0700213 ttrust_(1.1),
214 wheel_(0.0),
215 throttle_(0.0),
216 quickturn_(false),
217 stale_count_(0),
218 position_time_delta_(0.01),
219 left_gear_(LOW),
Brian Silvermande8fd552013-11-03 15:53:42 -0800220 right_gear_(LOW),
221 counter_(0) {
Austin Schuh2054f5f2013-10-27 14:54:10 -0700222
Austin Schuh427b3702013-11-02 13:44:09 -0700223 last_position_.Zero();
224 position_.Zero();
Austin Schuh2054f5f2013-10-27 14:54:10 -0700225 }
Austin Schuh427b3702013-11-02 13:44:09 -0700226 static bool IsInGear(Gear gear) { return gear == LOW || gear == HIGH; }
227
Austin Schuh78d55462014-02-23 01:39:30 -0800228 static double MotorSpeed(const constants::ShifterHallEffect &hall_effect,
229 double shifter_position, double velocity) {
Austin Schuh427b3702013-11-02 13:44:09 -0700230 // TODO(austin): G_high, G_low and kWheelRadius
Austin Schuh78d55462014-02-23 01:39:30 -0800231 const double avg_hall_effect =
232 (hall_effect.clear_high + hall_effect.clear_low) / 2.0;
233
234 if (shifter_position > avg_hall_effect) {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800235 return velocity / constants::GetValues().high_gear_ratio / kWheelRadius;
Austin Schuh427b3702013-11-02 13:44:09 -0700236 } else {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800237 return velocity / constants::GetValues().low_gear_ratio / kWheelRadius;
238 }
239 }
240
Austin Schuh78d55462014-02-23 01:39:30 -0800241 Gear ComputeGear(const constants::ShifterHallEffect &hall_effect,
242 double velocity, Gear current) {
243 const double low_omega = MotorSpeed(hall_effect, 0.0, ::std::abs(velocity));
244 const double high_omega =
245 MotorSpeed(hall_effect, 1.0, ::std::abs(velocity));
Brian Silverman1a6590d2013-11-04 14:46:46 -0800246
Brian Silverman1a6590d2013-11-04 14:46:46 -0800247 double high_torque = ((12.0 - high_omega / Kv) * Kt / kR);
248 double low_torque = ((12.0 - low_omega / Kv) * Kt / kR);
249 double high_power = high_torque * high_omega;
250 double low_power = low_torque * low_omega;
Brian Silverman55a930b2013-11-04 20:59:00 -0800251
252 // TODO(aschuh): Do this right!
253 if ((current == HIGH || high_power > low_power + 160) &&
254 ::std::abs(velocity) > 0.14) {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800255 return HIGH;
256 } else {
257 return LOW;
Austin Schuh427b3702013-11-02 13:44:09 -0700258 }
259 }
260
Austin Schuh2054f5f2013-10-27 14:54:10 -0700261 void SetGoal(double wheel, double throttle, bool quickturn, bool highgear) {
Brian Silvermande8fd552013-11-03 15:53:42 -0800262 const double kWheelNonLinearity = 0.3;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700263 // Apply a sin function that's scaled to make it feel better.
264 const double angular_range = M_PI_2 * kWheelNonLinearity;
265 wheel_ = sin(angular_range * wheel) / sin(angular_range);
266 wheel_ = sin(angular_range * wheel_) / sin(angular_range);
Austin Schuh2054f5f2013-10-27 14:54:10 -0700267 quickturn_ = quickturn;
Austin Schuh427b3702013-11-02 13:44:09 -0700268
Brian Silverman1a6590d2013-11-04 14:46:46 -0800269 static const double kThrottleDeadband = 0.05;
270 if (::std::abs(throttle) < kThrottleDeadband) {
271 throttle_ = 0;
272 } else {
273 throttle_ = copysign((::std::abs(throttle) - kThrottleDeadband) /
274 (1.0 - kThrottleDeadband), throttle);
275 }
276
Austin Schuh427b3702013-11-02 13:44:09 -0700277 // TODO(austin): Fix the upshift logic to include states.
Brian Silverman1a6590d2013-11-04 14:46:46 -0800278 Gear requested_gear;
Brian Silverman55a930b2013-11-04 20:59:00 -0800279 if (false) {
Austin Schuh78d55462014-02-23 01:39:30 -0800280 const auto &values = constants::GetValues();
Brian Silverman1a6590d2013-11-04 14:46:46 -0800281 const double current_left_velocity =
282 (position_.left_encoder - last_position_.left_encoder) /
283 position_time_delta_;
284 const double current_right_velocity =
285 (position_.right_encoder - last_position_.right_encoder) /
286 position_time_delta_;
287
Austin Schuh78d55462014-02-23 01:39:30 -0800288 Gear left_requested =
289 ComputeGear(values.left_drive, current_left_velocity, left_gear_);
290 Gear right_requested =
291 ComputeGear(values.right_drive, current_right_velocity, right_gear_);
Brian Silverman1a6590d2013-11-04 14:46:46 -0800292 requested_gear =
293 (left_requested == HIGH || right_requested == HIGH) ? HIGH : LOW;
294 } else {
295 requested_gear = highgear ? HIGH : LOW;
296 }
297
298 const Gear shift_up =
299 constants::GetValues().clutch_transmission ? HIGH : SHIFTING_UP;
300 const Gear shift_down =
301 constants::GetValues().clutch_transmission ? LOW : SHIFTING_DOWN;
Austin Schuh427b3702013-11-02 13:44:09 -0700302
303 if (left_gear_ != requested_gear) {
304 if (IsInGear(left_gear_)) {
305 if (requested_gear == HIGH) {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800306 left_gear_ = shift_up;
Austin Schuh427b3702013-11-02 13:44:09 -0700307 } else {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800308 left_gear_ = shift_down;
Austin Schuh427b3702013-11-02 13:44:09 -0700309 }
Brian Silverman55a930b2013-11-04 20:59:00 -0800310 } else {
311 if (requested_gear == HIGH && left_gear_ == SHIFTING_DOWN) {
312 left_gear_ = SHIFTING_UP;
313 } else if (requested_gear == LOW && left_gear_ == SHIFTING_UP) {
314 left_gear_ = SHIFTING_DOWN;
315 }
Austin Schuh427b3702013-11-02 13:44:09 -0700316 }
317 }
318 if (right_gear_ != requested_gear) {
319 if (IsInGear(right_gear_)) {
320 if (requested_gear == HIGH) {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800321 right_gear_ = shift_up;
Austin Schuh427b3702013-11-02 13:44:09 -0700322 } else {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800323 right_gear_ = shift_down;
Austin Schuh427b3702013-11-02 13:44:09 -0700324 }
Brian Silverman55a930b2013-11-04 20:59:00 -0800325 } else {
326 if (requested_gear == HIGH && right_gear_ == SHIFTING_DOWN) {
327 right_gear_ = SHIFTING_UP;
328 } else if (requested_gear == LOW && right_gear_ == SHIFTING_UP) {
329 right_gear_ = SHIFTING_DOWN;
330 }
Austin Schuh427b3702013-11-02 13:44:09 -0700331 }
Austin Schuh2054f5f2013-10-27 14:54:10 -0700332 }
333 }
Austin Schuh427b3702013-11-02 13:44:09 -0700334 void SetPosition(const Drivetrain::Position *position) {
Austin Schuh78d55462014-02-23 01:39:30 -0800335 const auto &values = constants::GetValues();
Austin Schuh427b3702013-11-02 13:44:09 -0700336 if (position == NULL) {
337 ++stale_count_;
338 } else {
339 last_position_ = position_;
340 position_ = *position;
341 position_time_delta_ = (stale_count_ + 1) * 0.01;
342 stale_count_ = 0;
343 }
344
345 if (position) {
Brian Silverman61e41fd2014-02-16 19:08:50 -0800346 GearLogging gear_logging;
Austin Schuh427b3702013-11-02 13:44:09 -0700347 // Switch to the correct controller.
Austin Schuh78d55462014-02-23 01:39:30 -0800348 const double left_middle_shifter_position =
349 (values.left_drive.clear_high + values.left_drive.clear_low) / 2.0;
350 const double right_middle_shifter_position =
351 (values.right_drive.clear_high + values.right_drive.clear_low) / 2.0;
352
353 if (position->left_shifter_position < left_middle_shifter_position) {
354 if (position->right_shifter_position < right_middle_shifter_position ||
355 right_gear_ == LOW) {
Brian Silverman61e41fd2014-02-16 19:08:50 -0800356 gear_logging.left_loop_high = false;
357 gear_logging.right_loop_high = false;
358 loop_->set_controller_index(gear_logging.controller_index = 0);
Austin Schuh427b3702013-11-02 13:44:09 -0700359 } else {
Brian Silverman61e41fd2014-02-16 19:08:50 -0800360 gear_logging.left_loop_high = false;
361 gear_logging.right_loop_high = true;
362 loop_->set_controller_index(gear_logging.controller_index = 1);
Austin Schuh427b3702013-11-02 13:44:09 -0700363 }
364 } else {
Austin Schuh78d55462014-02-23 01:39:30 -0800365 if (position->right_shifter_position < right_middle_shifter_position ||
366 left_gear_ == LOW) {
Brian Silverman61e41fd2014-02-16 19:08:50 -0800367 gear_logging.left_loop_high = true;
368 gear_logging.right_loop_high = false;
369 loop_->set_controller_index(gear_logging.controller_index = 2);
Austin Schuh427b3702013-11-02 13:44:09 -0700370 } else {
Brian Silverman61e41fd2014-02-16 19:08:50 -0800371 gear_logging.left_loop_high = true;
372 gear_logging.right_loop_high = true;
373 loop_->set_controller_index(gear_logging.controller_index = 3);
Austin Schuh427b3702013-11-02 13:44:09 -0700374 }
375 }
Brian Silverman61e41fd2014-02-16 19:08:50 -0800376
Austin Schuh427b3702013-11-02 13:44:09 -0700377 // TODO(austin): Constants.
Austin Schuh78d55462014-02-23 01:39:30 -0800378 if (position->left_shifter_position > values.left_drive.clear_high && left_gear_ == SHIFTING_UP) {
Austin Schuh427b3702013-11-02 13:44:09 -0700379 left_gear_ = HIGH;
380 }
Austin Schuh78d55462014-02-23 01:39:30 -0800381 if (position->left_shifter_position < values.left_drive.clear_low && left_gear_ == SHIFTING_DOWN) {
Austin Schuh427b3702013-11-02 13:44:09 -0700382 left_gear_ = LOW;
383 }
Austin Schuh78d55462014-02-23 01:39:30 -0800384 if (position->right_shifter_position > values.right_drive.clear_high && right_gear_ == SHIFTING_UP) {
Austin Schuh427b3702013-11-02 13:44:09 -0700385 right_gear_ = HIGH;
386 }
Austin Schuh78d55462014-02-23 01:39:30 -0800387 if (position->right_shifter_position < values.right_drive.clear_low && right_gear_ == SHIFTING_DOWN) {
Austin Schuh427b3702013-11-02 13:44:09 -0700388 right_gear_ = LOW;
389 }
Brian Silverman61e41fd2014-02-16 19:08:50 -0800390
391 gear_logging.left_state = left_gear_;
392 gear_logging.right_state = right_gear_;
393 LOG_STRUCT(DEBUG, "state", gear_logging);
Austin Schuh427b3702013-11-02 13:44:09 -0700394 }
395 }
396
Austin Schuh2054f5f2013-10-27 14:54:10 -0700397 double FilterVelocity(double throttle) {
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 throttle * 12.0 * min_FF_sum / high_min_FF_sum, -12.0, 12.0);
416 return ((adjusted_ff_voltage +
417 ttrust_ * min_K_sum * (loop_->X_hat(0, 0) + loop_->X_hat(1, 0)) /
418 2.0) /
419 (ttrust_ * min_K_sum + min_FF_sum));
420 }
421
Brian Silverman718b1d72013-10-28 16:22:45 -0700422 double MaxVelocity() {
423 const Eigen::Matrix<double, 2, 2> FF =
424 loop_->B().inverse() *
425 (Eigen::Matrix<double, 2, 2>::Identity() - loop_->A());
426
427 constexpr int kHighGearController = 3;
428 const Eigen::Matrix<double, 2, 2> FF_high =
429 loop_->controller(kHighGearController).plant.B.inverse() *
430 (Eigen::Matrix<double, 2, 2>::Identity() -
431 loop_->controller(kHighGearController).plant.A);
432
433 ::Eigen::Matrix<double, 1, 2> FF_sum = FF.colwise().sum();
434 int min_FF_sum_index;
435 const double min_FF_sum = FF_sum.minCoeff(&min_FF_sum_index);
436 //const double min_K_sum = loop_->K().col(min_FF_sum_index).sum();
437 const double high_min_FF_sum = FF_high.col(0).sum();
438
439 const double adjusted_ff_voltage = ::aos::Clip(
440 12.0 * min_FF_sum / high_min_FF_sum, -12.0, 12.0);
441 return adjusted_ff_voltage / min_FF_sum;
442 }
443
Austin Schuh2054f5f2013-10-27 14:54:10 -0700444 void Update() {
Austin Schuh78d55462014-02-23 01:39:30 -0800445 const auto &values = constants::GetValues();
Austin Schuh427b3702013-11-02 13:44:09 -0700446 // TODO(austin): Observer for the current velocity instead of difference
447 // calculations.
Brian Silvermande8fd552013-11-03 15:53:42 -0800448 ++counter_;
Austin Schuh427b3702013-11-02 13:44:09 -0700449 const double current_left_velocity =
Brian Silvermande8fd552013-11-03 15:53:42 -0800450 (position_.left_encoder - last_position_.left_encoder) /
Austin Schuh427b3702013-11-02 13:44:09 -0700451 position_time_delta_;
452 const double current_right_velocity =
Brian Silvermande8fd552013-11-03 15:53:42 -0800453 (position_.right_encoder - last_position_.right_encoder) /
Austin Schuh427b3702013-11-02 13:44:09 -0700454 position_time_delta_;
455 const double left_motor_speed =
Austin Schuh78d55462014-02-23 01:39:30 -0800456 MotorSpeed(values.left_drive, position_.left_shifter_position,
457 current_left_velocity);
Austin Schuh427b3702013-11-02 13:44:09 -0700458 const double right_motor_speed =
Austin Schuh78d55462014-02-23 01:39:30 -0800459 MotorSpeed(values.right_drive, position_.right_shifter_position,
460 current_right_velocity);
Austin Schuh2054f5f2013-10-27 14:54:10 -0700461
Brian Silverman61e41fd2014-02-16 19:08:50 -0800462 {
463 CIMLogging logging;
464
465 // Reset the CIM model to the current conditions to be ready for when we
466 // shift.
467 if (IsInGear(left_gear_)) {
468 logging.left_in_gear = true;
Brian Silverman61e41fd2014-02-16 19:08:50 -0800469 } else {
470 logging.left_in_gear = false;
471 }
472 logging.left_motor_speed = left_motor_speed;
473 logging.left_velocity = current_left_velocity;
474 if (IsInGear(right_gear_)) {
475 logging.right_in_gear = true;
Brian Silverman61e41fd2014-02-16 19:08:50 -0800476 } else {
477 logging.right_in_gear = false;
478 }
479 logging.right_motor_speed = right_motor_speed;
480 logging.right_velocity = current_right_velocity;
481
482 LOG_STRUCT(DEBUG, "currently", logging);
Austin Schuh427b3702013-11-02 13:44:09 -0700483 }
Austin Schuh2054f5f2013-10-27 14:54:10 -0700484
Austin Schuh427b3702013-11-02 13:44:09 -0700485 if (IsInGear(left_gear_) && IsInGear(right_gear_)) {
486 // FF * X = U (steady state)
487 const Eigen::Matrix<double, 2, 2> FF =
488 loop_->B().inverse() *
489 (Eigen::Matrix<double, 2, 2>::Identity() - loop_->A());
490
491 // Invert the plant to figure out how the velocity filter would have to
492 // work
493 // out in order to filter out the forwards negative inertia.
494 // This math assumes that the left and right power and velocity are
495 // equals,
496 // and that the plant is the same on the left and right.
497 const double fvel = FilterVelocity(throttle_);
498
499 const double sign_svel = wheel_ * ((fvel > 0.0) ? 1.0 : -1.0);
500 double steering_velocity;
501 if (quickturn_) {
502 steering_velocity = wheel_ * MaxVelocity();
503 } else {
504 steering_velocity = ::std::abs(fvel) * wheel_;
505 }
506 const double left_velocity = fvel - steering_velocity;
507 const double right_velocity = fvel + steering_velocity;
508
509 // Integrate velocity to get the position.
510 // This position is used to get integral control.
511 loop_->R << left_velocity, right_velocity;
512
513 if (!quickturn_) {
514 // K * R = w
515 Eigen::Matrix<double, 1, 2> equality_k;
516 equality_k << 1 + sign_svel, -(1 - sign_svel);
517 const double equality_w = 0.0;
518
519 // Construct a constraint on R by manipulating the constraint on U
520 ::aos::controls::HPolytope<2> R_poly = ::aos::controls::HPolytope<2>(
521 U_Poly_.H() * (loop_->K() + FF),
522 U_Poly_.k() + U_Poly_.H() * loop_->K() * loop_->X_hat);
523
524 // Limit R back inside the box.
525 loop_->R = CoerceGoal(R_poly, equality_k, equality_w, loop_->R);
526 }
527
528 const Eigen::Matrix<double, 2, 1> FF_volts = FF * loop_->R;
529 const Eigen::Matrix<double, 2, 1> U_ideal =
530 loop_->K() * (loop_->R - loop_->X_hat) + FF_volts;
531
532 for (int i = 0; i < 2; i++) {
533 loop_->U[i] = ::aos::Clip(U_ideal[i], -12, 12);
534 }
Brian Silvermande8fd552013-11-03 15:53:42 -0800535
536 // TODO(austin): Model this better.
537 // TODO(austin): Feed back?
538 loop_->X_hat = loop_->A() * loop_->X_hat + loop_->B() * loop_->U;
Brian Silverman718b1d72013-10-28 16:22:45 -0700539 } else {
Austin Schuh427b3702013-11-02 13:44:09 -0700540 // Any motor is not in gear. Speed match.
541 ::Eigen::Matrix<double, 1, 1> R_left;
Austin Schuh427b3702013-11-02 13:44:09 -0700542 ::Eigen::Matrix<double, 1, 1> R_right;
Austin Schuhb5afb692014-03-02 11:54:11 -0800543 R_left(0, 0) = left_motor_speed;
Austin Schuh427b3702013-11-02 13:44:09 -0700544 R_right(0, 0) = right_motor_speed;
Austin Schuhb5afb692014-03-02 11:54:11 -0800545
546 const double wiggle =
Austin Schuhaf23eaa2014-03-02 14:00:19 -0800547 (static_cast<double>((counter_ % 10) / 5) - 0.5) * 5.0;
Austin Schuhb5afb692014-03-02 11:54:11 -0800548
549 loop_->U(0, 0) = ::aos::Clip((R_left / Kv)(0, 0) + wiggle, -12.0, 12.0);
550 loop_->U(1, 0) = ::aos::Clip((R_right / Kv)(0, 0) + wiggle, -12.0, 12.0);
Austin Schuh2054f5f2013-10-27 14:54:10 -0700551 }
Austin Schuh2054f5f2013-10-27 14:54:10 -0700552 }
553
554 void SendMotors(Drivetrain::Output *output) {
Brian Silvermande8fd552013-11-03 15:53:42 -0800555 if (output != NULL) {
556 output->left_voltage = loop_->U(0, 0);
557 output->right_voltage = loop_->U(1, 0);
Brian Silverman61e41fd2014-02-16 19:08:50 -0800558 output->left_high = left_gear_ == HIGH || left_gear_ == SHIFTING_UP;
559 output->right_high = right_gear_ == HIGH || right_gear_ == SHIFTING_UP;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700560 }
561 }
562
563 private:
564 const ::aos::controls::HPolytope<2> U_Poly_;
565
566 ::std::unique_ptr<StateFeedbackLoop<2, 2, 2>> loop_;
567
Austin Schuh427b3702013-11-02 13:44:09 -0700568 const double ttrust_;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700569 double wheel_;
570 double throttle_;
571 bool quickturn_;
Austin Schuh427b3702013-11-02 13:44:09 -0700572 int stale_count_;
573 double position_time_delta_;
574 Gear left_gear_;
575 Gear right_gear_;
576 Drivetrain::Position last_position_;
577 Drivetrain::Position position_;
Brian Silvermande8fd552013-11-03 15:53:42 -0800578 int counter_;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700579};
Brian Silvermancec3c8d2013-12-20 21:04:55 -0800580constexpr double PolyDrivetrain::kStallTorque;
581constexpr double PolyDrivetrain::kStallCurrent;
582constexpr double PolyDrivetrain::kFreeSpeed;
583constexpr double PolyDrivetrain::kFreeCurrent;
584constexpr double PolyDrivetrain::J;
585constexpr double PolyDrivetrain::m;
586constexpr double PolyDrivetrain::rb;
587constexpr double PolyDrivetrain::kWheelRadius;
588constexpr double PolyDrivetrain::kR;
589constexpr double PolyDrivetrain::Kv;
590constexpr double PolyDrivetrain::Kt;
591
Austin Schuh2054f5f2013-10-27 14:54:10 -0700592
brians343bc112013-02-10 01:53:46 +0000593void DrivetrainLoop::RunIteration(const Drivetrain::Goal *goal,
594 const Drivetrain::Position *position,
595 Drivetrain::Output *output,
Ben Fredrickson890c3fe2014-03-02 00:15:16 +0000596 Drivetrain::Status * status) {
brians343bc112013-02-10 01:53:46 +0000597 // TODO(aschuh): These should be members of the class.
598 static DrivetrainMotorsSS dt_closedloop;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700599 static PolyDrivetrain dt_openloop;
brians343bc112013-02-10 01:53:46 +0000600
601 bool bad_pos = false;
Austin Schuh427b3702013-11-02 13:44:09 -0700602 if (position == nullptr) {
Brian Silverman50a9d032014-02-16 17:20:57 -0800603 LOG_INTERVAL(no_position_);
brians343bc112013-02-10 01:53:46 +0000604 bad_pos = true;
605 }
Brian Silverman9c9ab422014-02-25 13:42:53 -0800606 no_position_.Print();
brians343bc112013-02-10 01:53:46 +0000607
608 double wheel = goal->steering;
609 double throttle = goal->throttle;
610 bool quickturn = goal->quickturn;
611 bool highgear = goal->highgear;
612
613 bool control_loop_driving = goal->control_loop_driving;
614 double left_goal = goal->left_goal;
615 double right_goal = goal->right_goal;
616
Austin Schuh2054f5f2013-10-27 14:54:10 -0700617 dt_closedloop.SetGoal(left_goal, goal->left_velocity_goal, right_goal,
618 goal->right_velocity_goal);
brians343bc112013-02-10 01:53:46 +0000619 if (!bad_pos) {
620 const double left_encoder = position->left_encoder;
621 const double right_encoder = position->right_encoder;
Ben Fredrickson81ba2d52014-03-02 08:21:46 +0000622 if (othersensors.FetchLatest()) {
623 LOG(DEBUG, "using %.3f", othersensors->gyro_angle);
624 dt_closedloop.SetPosition(left_encoder, right_encoder, othersensors->gyro_angle,
Austin Schuh2054f5f2013-10-27 14:54:10 -0700625 control_loop_driving);
brians343bc112013-02-10 01:53:46 +0000626 } else {
627 dt_closedloop.SetRawPosition(left_encoder, right_encoder);
628 }
629 }
Austin Schuh427b3702013-11-02 13:44:09 -0700630 dt_openloop.SetPosition(position);
brians343bc112013-02-10 01:53:46 +0000631 dt_openloop.SetGoal(wheel, throttle, quickturn, highgear);
632 dt_openloop.Update();
Ben Fredrickson890c3fe2014-03-02 00:15:16 +0000633
Brian Silverman2845c4c2013-03-16 19:54:08 -0700634 if (control_loop_driving) {
Ben Fredrickson890c3fe2014-03-02 00:15:16 +0000635 dt_closedloop.Update(output == NULL);
Brian Silverman2845c4c2013-03-16 19:54:08 -0700636 dt_closedloop.SendMotors(output);
637 } else {
638 dt_openloop.SendMotors(output);
Austin Schuhb5afb692014-03-02 11:54:11 -0800639 if (output) {
640 dt_closedloop.SetExternalMotors(output->left_voltage,
641 output->right_voltage);
642 }
Ben Fredrickson890c3fe2014-03-02 00:15:16 +0000643 dt_closedloop.Update(output == NULL);
brians343bc112013-02-10 01:53:46 +0000644 }
Ben Fredrickson890c3fe2014-03-02 00:15:16 +0000645
646 // set the output status of the controll loop state
647 if (status) {
648 bool done = false;
649 if (goal) {
650 done = ((::std::abs(goal->left_goal -
651 dt_closedloop.GetEstimatedLeftEncoder()) <
652 constants::GetValues().drivetrain_done_distance) &&
653 (::std::abs(goal->right_goal -
654 dt_closedloop.GetEstimatedRightEncoder()) <
655 constants::GetValues().drivetrain_done_distance));
656 }
657 status->is_done = done;
658 status->robot_speed = dt_closedloop.GetEstimatedRobotSpeed();
brians343bc112013-02-10 01:53:46 +0000659 }
660}
661
662} // namespace control_loops
663} // namespace frc971