blob: 14fa825983926a57a23c9a9fa0b7ecd2582ba7a9 [file] [log] [blame]
James Kuszmaulf254c1a2013-03-10 16:31:26 -07001#include "frc971/control_loops/drivetrain/drivetrain.h"
brians343bc112013-02-10 01:53:46 +00002
3#include <stdio.h>
4#include <sched.h>
5#include <cmath>
Austin Schuh4352ac62013-03-19 06:23:16 +00006#include <memory>
Austin Schuh2054f5f2013-10-27 14:54:10 -07007#include "Eigen/Dense"
brians343bc112013-02-10 01:53:46 +00008
brians343bc112013-02-10 01:53:46 +00009#include "aos/common/logging/logging.h"
10#include "aos/common/queue.h"
Austin Schuh2054f5f2013-10-27 14:54:10 -070011#include "aos/controls/polytope.h"
12#include "aos/common/commonmath.h"
Brian Silverman61e41fd2014-02-16 19:08:50 -080013#include "aos/common/logging/queue_logging.h"
14
James Kuszmaulf254c1a2013-03-10 16:31:26 -070015#include "frc971/control_loops/state_feedback_loop.h"
Austin Schuh427b3702013-11-02 13:44:09 -070016#include "frc971/control_loops/drivetrain/polydrivetrain_cim_plant.h"
James Kuszmaulf254c1a2013-03-10 16:31:26 -070017#include "frc971/control_loops/drivetrain/drivetrain.q.h"
Ben Fredrickson81ba2d52014-03-02 08:21:46 +000018#include "frc971/queues/othersensors.q.h"
Brian Silverman1a6590d2013-11-04 14:46:46 -080019#include "frc971/constants.h"
brians343bc112013-02-10 01:53:46 +000020
Ben Fredrickson81ba2d52014-03-02 08:21:46 +000021using frc971::sensors::othersensors;
brians343bc112013-02-10 01:53:46 +000022
23namespace frc971 {
24namespace control_loops {
25
26// Width of the robot.
27const double width = 22.0 / 100.0 * 2.54;
28
Austin Schuh2054f5f2013-10-27 14:54:10 -070029Eigen::Matrix<double, 2, 1> CoerceGoal(aos::controls::HPolytope<2> &region,
30 const Eigen::Matrix<double, 1, 2> &K,
31 double w,
32 const Eigen::Matrix<double, 2, 1> &R) {
33 if (region.IsInside(R)) {
34 return R;
35 }
36 Eigen::Matrix<double, 2, 1> parallel_vector;
37 Eigen::Matrix<double, 2, 1> perpendicular_vector;
38 perpendicular_vector = K.transpose().normalized();
39 parallel_vector << perpendicular_vector(1, 0), -perpendicular_vector(0, 0);
40
41 aos::controls::HPolytope<1> t_poly(
42 region.H() * parallel_vector,
43 region.k() - region.H() * perpendicular_vector * w);
44
45 Eigen::Matrix<double, 1, Eigen::Dynamic> vertices = t_poly.Vertices();
46 if (vertices.innerSize() > 0) {
47 double min_distance_sqr = 0;
48 Eigen::Matrix<double, 2, 1> closest_point;
49 for (int i = 0; i < vertices.innerSize(); i++) {
50 Eigen::Matrix<double, 2, 1> point;
51 point = parallel_vector * vertices(0, i) + perpendicular_vector * w;
52 const double length = (R - point).squaredNorm();
53 if (i == 0 || length < min_distance_sqr) {
54 closest_point = point;
55 min_distance_sqr = length;
56 }
57 }
58 return closest_point;
59 } else {
60 Eigen::Matrix<double, 2, Eigen::Dynamic> region_vertices =
61 region.Vertices();
Brian Silverman41abe012014-02-08 18:25:02 -080062 double min_distance = INFINITY;
Austin Schuh2054f5f2013-10-27 14:54:10 -070063 int closest_i = 0;
64 for (int i = 0; i < region_vertices.outerSize(); i++) {
65 const double length = ::std::abs(
66 (perpendicular_vector.transpose() * (region_vertices.col(i)))(0, 0));
67 if (i == 0 || length < min_distance) {
68 closest_i = i;
69 min_distance = length;
70 }
71 }
72 return region_vertices.col(closest_i);
73 }
74}
75
Austin Schuh4352ac62013-03-19 06:23:16 +000076class DrivetrainMotorsSS {
brians343bc112013-02-10 01:53:46 +000077 public:
Brian Silverman2c590c32013-11-04 18:08:54 -080078 DrivetrainMotorsSS()
79 : loop_(new StateFeedbackLoop<4, 2, 2>(
80 constants::GetValues().make_drivetrain_loop())) {
brians343bc112013-02-10 01:53:46 +000081 _offset = 0;
82 _integral_offset = 0;
83 _left_goal = 0.0;
84 _right_goal = 0.0;
85 _raw_left = 0.0;
86 _raw_right = 0.0;
Austin Schuh4352ac62013-03-19 06:23:16 +000087 _control_loop_driving = false;
brians343bc112013-02-10 01:53:46 +000088 }
89 void SetGoal(double left, double left_velocity, double right, double right_velocity) {
90 _left_goal = left;
91 _right_goal = right;
Austin Schuh4352ac62013-03-19 06:23:16 +000092 loop_->R << left, left_velocity, right, right_velocity;
brians343bc112013-02-10 01:53:46 +000093 }
94 void SetRawPosition(double left, double right) {
95 _raw_right = right;
96 _raw_left = left;
Austin Schuhf9286cd2014-02-11 00:51:09 -080097 Eigen::Matrix<double, 2, 1> Y;
98 Y << left, right;
99 loop_->Correct(Y);
brians343bc112013-02-10 01:53:46 +0000100 }
Austin Schuh4352ac62013-03-19 06:23:16 +0000101 void SetPosition(
102 double left, double right, double gyro, bool control_loop_driving) {
brians343bc112013-02-10 01:53:46 +0000103 // Decay the offset quickly because this gyro is great.
104 _offset = (0.25) * (right - left - gyro * width) / 2.0 + 0.75 * _offset;
Brian Silvermande8fd552013-11-03 15:53:42 -0800105 //const double angle_error = (_right_goal - _left_goal) / width - (_raw_right - _offset - _raw_left - _offset) / width;
Austin Schuh4352ac62013-03-19 06:23:16 +0000106 // TODO(aschuh): Add in the gyro.
107 _integral_offset = 0.0;
108 _offset = 0.0;
brians343bc112013-02-10 01:53:46 +0000109 _gyro = gyro;
Austin Schuh4352ac62013-03-19 06:23:16 +0000110 _control_loop_driving = control_loop_driving;
brians343bc112013-02-10 01:53:46 +0000111 SetRawPosition(left, right);
brians343bc112013-02-10 01:53:46 +0000112 }
Austin Schuh4352ac62013-03-19 06:23:16 +0000113
Ben Fredrickson890c3fe2014-03-02 00:15:16 +0000114 void SetExternalMotors(double left_voltage, double right_voltage) {
115 loop_->U << left_voltage, right_voltage;
116 }
117
Austin Schuhf9286cd2014-02-11 00:51:09 -0800118 void Update(bool stop_motors) {
Ben Fredrickson890c3fe2014-03-02 00:15:16 +0000119 if (_control_loop_driving) {
120 loop_->Update(stop_motors);
121 } else {
122 if (stop_motors) {
123 loop_->U.setZero();
124 loop_->U_uncapped.setZero();
125 }
126 loop_->UpdateObserver();
127 }
128 }
129
130 double GetEstimatedRobotSpeed() {
131 // lets just call the average of left and right velocities close enough
132 return (loop_->X_hat(1, 0) + loop_->X_hat(3, 0)) / 2;
133 }
134
135 double GetEstimatedLeftEncoder() {
136 // lets just call the average of left and right velocities close enough
137 return loop_->X_hat(0, 0);
138 }
139
140 double GetEstimatedRightEncoder() {
141 return loop_->X_hat(2, 0);
brians343bc112013-02-10 01:53:46 +0000142 }
143
Austin Schuh4352ac62013-03-19 06:23:16 +0000144 void SendMotors(Drivetrain::Output *output) {
145 if (output) {
146 output->left_voltage = loop_->U(0, 0);
147 output->right_voltage = loop_->U(1, 0);
brians8ad74052013-03-16 21:04:51 +0000148 }
brians343bc112013-02-10 01:53:46 +0000149 }
150 void PrintMotors() const {
Austin Schuh4352ac62013-03-19 06:23:16 +0000151 ::Eigen::Matrix<double, 4, 1> E = loop_->R - loop_->X_hat;
152 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 +0000153 }
154
155 private:
Austin Schuh4352ac62013-03-19 06:23:16 +0000156 ::std::unique_ptr<StateFeedbackLoop<4, 2, 2>> loop_;
157
brians343bc112013-02-10 01:53:46 +0000158 double _integral_offset;
159 double _offset;
160 double _gyro;
161 double _left_goal;
162 double _right_goal;
163 double _raw_left;
164 double _raw_right;
Austin Schuh4352ac62013-03-19 06:23:16 +0000165 bool _control_loop_driving;
brians343bc112013-02-10 01:53:46 +0000166};
167
Austin Schuh2054f5f2013-10-27 14:54:10 -0700168class PolyDrivetrain {
169 public:
Austin Schuh427b3702013-11-02 13:44:09 -0700170
171 enum Gear {
172 HIGH,
173 LOW,
174 SHIFTING_UP,
175 SHIFTING_DOWN
176 };
177 // Stall Torque in N m
178 static constexpr double kStallTorque = 2.42;
179 // Stall Current in Amps
180 static constexpr double kStallCurrent = 133;
181 // Free Speed in RPM. Used number from last year.
182 static constexpr double kFreeSpeed = 4650.0;
183 // Free Current in Amps
184 static constexpr double kFreeCurrent = 2.7;
185 // Moment of inertia of the drivetrain in kg m^2
186 // Just borrowed from last year.
187 static constexpr double J = 6.4;
188 // Mass of the robot, in kg.
189 static constexpr double m = 68;
190 // Radius of the robot, in meters (from last year).
191 static constexpr double rb = 0.617998644 / 2.0;
Brian Silverman1a6590d2013-11-04 14:46:46 -0800192 static constexpr double kWheelRadius = 0.04445;
Austin Schuh427b3702013-11-02 13:44:09 -0700193 // Resistance of the motor, divided by the number of motors.
194 static constexpr double kR = (12.0 / kStallCurrent / 4 + 0.03) / (0.93 * 0.93);
195 // Motor velocity constant
196 static constexpr double Kv =
197 ((kFreeSpeed / 60.0 * 2.0 * M_PI) / (12.0 - kR * kFreeCurrent));
198 // Torque constant
199 static constexpr double Kt = kStallTorque / kStallCurrent;
Austin Schuh427b3702013-11-02 13:44:09 -0700200
Austin Schuh2054f5f2013-10-27 14:54:10 -0700201 PolyDrivetrain()
202 : U_Poly_((Eigen::Matrix<double, 4, 2>() << /*[[*/ 1, 0 /*]*/,
203 /*[*/ -1, 0 /*]*/,
204 /*[*/ 0, 1 /*]*/,
205 /*[*/ 0, -1 /*]]*/).finished(),
206 (Eigen::Matrix<double, 4, 1>() << /*[[*/ 12 /*]*/,
207 /*[*/ 12 /*]*/,
208 /*[*/ 12 /*]*/,
209 /*[*/ 12 /*]]*/).finished()),
Brian Silverman2c590c32013-11-04 18:08:54 -0800210 loop_(new StateFeedbackLoop<2, 2, 2>(
211 constants::GetValues().make_v_drivetrain_loop())),
Austin Schuh427b3702013-11-02 13:44:09 -0700212 left_cim_(new StateFeedbackLoop<1, 1, 1>(MakeCIMLoop())),
213 right_cim_(new StateFeedbackLoop<1, 1, 1>(MakeCIMLoop())),
214 ttrust_(1.1),
215 wheel_(0.0),
216 throttle_(0.0),
217 quickturn_(false),
218 stale_count_(0),
219 position_time_delta_(0.01),
220 left_gear_(LOW),
Brian Silvermande8fd552013-11-03 15:53:42 -0800221 right_gear_(LOW),
222 counter_(0) {
Austin Schuh2054f5f2013-10-27 14:54:10 -0700223
Austin Schuh427b3702013-11-02 13:44:09 -0700224 last_position_.Zero();
225 position_.Zero();
Austin Schuh2054f5f2013-10-27 14:54:10 -0700226 }
Austin Schuh427b3702013-11-02 13:44:09 -0700227 static bool IsInGear(Gear gear) { return gear == LOW || gear == HIGH; }
228
Austin Schuh78d55462014-02-23 01:39:30 -0800229 static double MotorSpeed(const constants::ShifterHallEffect &hall_effect,
230 double shifter_position, double velocity) {
Austin Schuh427b3702013-11-02 13:44:09 -0700231 // TODO(austin): G_high, G_low and kWheelRadius
Austin Schuh78d55462014-02-23 01:39:30 -0800232 const double avg_hall_effect =
233 (hall_effect.clear_high + hall_effect.clear_low) / 2.0;
234
235 if (shifter_position > avg_hall_effect) {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800236 return velocity / constants::GetValues().high_gear_ratio / kWheelRadius;
Austin Schuh427b3702013-11-02 13:44:09 -0700237 } else {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800238 return velocity / constants::GetValues().low_gear_ratio / kWheelRadius;
239 }
240 }
241
Austin Schuh78d55462014-02-23 01:39:30 -0800242 Gear ComputeGear(const constants::ShifterHallEffect &hall_effect,
243 double velocity, Gear current) {
244 const double low_omega = MotorSpeed(hall_effect, 0.0, ::std::abs(velocity));
245 const double high_omega =
246 MotorSpeed(hall_effect, 1.0, ::std::abs(velocity));
Brian Silverman1a6590d2013-11-04 14:46:46 -0800247
Brian Silverman1a6590d2013-11-04 14:46:46 -0800248 double high_torque = ((12.0 - high_omega / Kv) * Kt / kR);
249 double low_torque = ((12.0 - low_omega / Kv) * Kt / kR);
250 double high_power = high_torque * high_omega;
251 double low_power = low_torque * low_omega;
Brian Silverman55a930b2013-11-04 20:59:00 -0800252
253 // TODO(aschuh): Do this right!
254 if ((current == HIGH || high_power > low_power + 160) &&
255 ::std::abs(velocity) > 0.14) {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800256 return HIGH;
257 } else {
258 return LOW;
Austin Schuh427b3702013-11-02 13:44:09 -0700259 }
260 }
261
Austin Schuh2054f5f2013-10-27 14:54:10 -0700262 void SetGoal(double wheel, double throttle, bool quickturn, bool highgear) {
Brian Silvermande8fd552013-11-03 15:53:42 -0800263 const double kWheelNonLinearity = 0.3;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700264 // Apply a sin function that's scaled to make it feel better.
265 const double angular_range = M_PI_2 * kWheelNonLinearity;
266 wheel_ = sin(angular_range * wheel) / sin(angular_range);
267 wheel_ = sin(angular_range * wheel_) / sin(angular_range);
Austin Schuh2054f5f2013-10-27 14:54:10 -0700268 quickturn_ = quickturn;
Austin Schuh427b3702013-11-02 13:44:09 -0700269
Brian Silverman1a6590d2013-11-04 14:46:46 -0800270 static const double kThrottleDeadband = 0.05;
271 if (::std::abs(throttle) < kThrottleDeadband) {
272 throttle_ = 0;
273 } else {
274 throttle_ = copysign((::std::abs(throttle) - kThrottleDeadband) /
275 (1.0 - kThrottleDeadband), throttle);
276 }
277
Austin Schuh427b3702013-11-02 13:44:09 -0700278 // TODO(austin): Fix the upshift logic to include states.
Brian Silverman1a6590d2013-11-04 14:46:46 -0800279 Gear requested_gear;
Brian Silverman55a930b2013-11-04 20:59:00 -0800280 if (false) {
Austin Schuh78d55462014-02-23 01:39:30 -0800281 const auto &values = constants::GetValues();
Brian Silverman1a6590d2013-11-04 14:46:46 -0800282 const double current_left_velocity =
283 (position_.left_encoder - last_position_.left_encoder) /
284 position_time_delta_;
285 const double current_right_velocity =
286 (position_.right_encoder - last_position_.right_encoder) /
287 position_time_delta_;
288
Austin Schuh78d55462014-02-23 01:39:30 -0800289 Gear left_requested =
290 ComputeGear(values.left_drive, current_left_velocity, left_gear_);
291 Gear right_requested =
292 ComputeGear(values.right_drive, current_right_velocity, right_gear_);
Brian Silverman1a6590d2013-11-04 14:46:46 -0800293 requested_gear =
294 (left_requested == HIGH || right_requested == HIGH) ? HIGH : LOW;
295 } else {
296 requested_gear = highgear ? HIGH : LOW;
297 }
298
299 const Gear shift_up =
300 constants::GetValues().clutch_transmission ? HIGH : SHIFTING_UP;
301 const Gear shift_down =
302 constants::GetValues().clutch_transmission ? LOW : SHIFTING_DOWN;
Austin Schuh427b3702013-11-02 13:44:09 -0700303
304 if (left_gear_ != requested_gear) {
305 if (IsInGear(left_gear_)) {
306 if (requested_gear == HIGH) {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800307 left_gear_ = shift_up;
Austin Schuh427b3702013-11-02 13:44:09 -0700308 } else {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800309 left_gear_ = shift_down;
Austin Schuh427b3702013-11-02 13:44:09 -0700310 }
Brian Silverman55a930b2013-11-04 20:59:00 -0800311 } else {
312 if (requested_gear == HIGH && left_gear_ == SHIFTING_DOWN) {
313 left_gear_ = SHIFTING_UP;
314 } else if (requested_gear == LOW && left_gear_ == SHIFTING_UP) {
315 left_gear_ = SHIFTING_DOWN;
316 }
Austin Schuh427b3702013-11-02 13:44:09 -0700317 }
318 }
319 if (right_gear_ != requested_gear) {
320 if (IsInGear(right_gear_)) {
321 if (requested_gear == HIGH) {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800322 right_gear_ = shift_up;
Austin Schuh427b3702013-11-02 13:44:09 -0700323 } else {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800324 right_gear_ = shift_down;
Austin Schuh427b3702013-11-02 13:44:09 -0700325 }
Brian Silverman55a930b2013-11-04 20:59:00 -0800326 } else {
327 if (requested_gear == HIGH && right_gear_ == SHIFTING_DOWN) {
328 right_gear_ = SHIFTING_UP;
329 } else if (requested_gear == LOW && right_gear_ == SHIFTING_UP) {
330 right_gear_ = SHIFTING_DOWN;
331 }
Austin Schuh427b3702013-11-02 13:44:09 -0700332 }
Austin Schuh2054f5f2013-10-27 14:54:10 -0700333 }
334 }
Austin Schuh427b3702013-11-02 13:44:09 -0700335 void SetPosition(const Drivetrain::Position *position) {
Austin Schuh78d55462014-02-23 01:39:30 -0800336 const auto &values = constants::GetValues();
Austin Schuh427b3702013-11-02 13:44:09 -0700337 if (position == NULL) {
338 ++stale_count_;
339 } else {
340 last_position_ = position_;
341 position_ = *position;
342 position_time_delta_ = (stale_count_ + 1) * 0.01;
343 stale_count_ = 0;
344 }
345
346 if (position) {
Brian Silverman61e41fd2014-02-16 19:08:50 -0800347 GearLogging gear_logging;
Austin Schuh427b3702013-11-02 13:44:09 -0700348 // Switch to the correct controller.
Austin Schuh78d55462014-02-23 01:39:30 -0800349 const double left_middle_shifter_position =
350 (values.left_drive.clear_high + values.left_drive.clear_low) / 2.0;
351 const double right_middle_shifter_position =
352 (values.right_drive.clear_high + values.right_drive.clear_low) / 2.0;
353
354 if (position->left_shifter_position < left_middle_shifter_position) {
355 if (position->right_shifter_position < right_middle_shifter_position ||
356 right_gear_ == LOW) {
Brian Silverman61e41fd2014-02-16 19:08:50 -0800357 gear_logging.left_loop_high = false;
358 gear_logging.right_loop_high = false;
359 loop_->set_controller_index(gear_logging.controller_index = 0);
Austin Schuh427b3702013-11-02 13:44:09 -0700360 } else {
Brian Silverman61e41fd2014-02-16 19:08:50 -0800361 gear_logging.left_loop_high = false;
362 gear_logging.right_loop_high = true;
363 loop_->set_controller_index(gear_logging.controller_index = 1);
Austin Schuh427b3702013-11-02 13:44:09 -0700364 }
365 } else {
Austin Schuh78d55462014-02-23 01:39:30 -0800366 if (position->right_shifter_position < right_middle_shifter_position ||
367 left_gear_ == LOW) {
Brian Silverman61e41fd2014-02-16 19:08:50 -0800368 gear_logging.left_loop_high = true;
369 gear_logging.right_loop_high = false;
370 loop_->set_controller_index(gear_logging.controller_index = 2);
Austin Schuh427b3702013-11-02 13:44:09 -0700371 } else {
Brian Silverman61e41fd2014-02-16 19:08:50 -0800372 gear_logging.left_loop_high = true;
373 gear_logging.right_loop_high = true;
374 loop_->set_controller_index(gear_logging.controller_index = 3);
Austin Schuh427b3702013-11-02 13:44:09 -0700375 }
376 }
Brian Silverman61e41fd2014-02-16 19:08:50 -0800377
Austin Schuh427b3702013-11-02 13:44:09 -0700378 // TODO(austin): Constants.
Austin Schuh78d55462014-02-23 01:39:30 -0800379 if (position->left_shifter_position > values.left_drive.clear_high && left_gear_ == SHIFTING_UP) {
Austin Schuh427b3702013-11-02 13:44:09 -0700380 left_gear_ = HIGH;
381 }
Austin Schuh78d55462014-02-23 01:39:30 -0800382 if (position->left_shifter_position < values.left_drive.clear_low && left_gear_ == SHIFTING_DOWN) {
Austin Schuh427b3702013-11-02 13:44:09 -0700383 left_gear_ = LOW;
384 }
Austin Schuh78d55462014-02-23 01:39:30 -0800385 if (position->right_shifter_position > values.right_drive.clear_high && right_gear_ == SHIFTING_UP) {
Austin Schuh427b3702013-11-02 13:44:09 -0700386 right_gear_ = HIGH;
387 }
Austin Schuh78d55462014-02-23 01:39:30 -0800388 if (position->right_shifter_position < values.right_drive.clear_low && right_gear_ == SHIFTING_DOWN) {
Austin Schuh427b3702013-11-02 13:44:09 -0700389 right_gear_ = LOW;
390 }
Brian Silverman61e41fd2014-02-16 19:08:50 -0800391
392 gear_logging.left_state = left_gear_;
393 gear_logging.right_state = right_gear_;
394 LOG_STRUCT(DEBUG, "state", gear_logging);
Austin Schuh427b3702013-11-02 13:44:09 -0700395 }
396 }
397
Austin Schuh2054f5f2013-10-27 14:54:10 -0700398 double FilterVelocity(double throttle) {
399 const Eigen::Matrix<double, 2, 2> FF =
400 loop_->B().inverse() *
401 (Eigen::Matrix<double, 2, 2>::Identity() - loop_->A());
402
403 constexpr int kHighGearController = 3;
404 const Eigen::Matrix<double, 2, 2> FF_high =
405 loop_->controller(kHighGearController).plant.B.inverse() *
406 (Eigen::Matrix<double, 2, 2>::Identity() -
407 loop_->controller(kHighGearController).plant.A);
408
409 ::Eigen::Matrix<double, 1, 2> FF_sum = FF.colwise().sum();
410 int min_FF_sum_index;
411 const double min_FF_sum = FF_sum.minCoeff(&min_FF_sum_index);
412 const double min_K_sum = loop_->K().col(min_FF_sum_index).sum();
413 const double high_min_FF_sum = FF_high.col(0).sum();
414
415 const double adjusted_ff_voltage = ::aos::Clip(
416 throttle * 12.0 * min_FF_sum / high_min_FF_sum, -12.0, 12.0);
417 return ((adjusted_ff_voltage +
418 ttrust_ * min_K_sum * (loop_->X_hat(0, 0) + loop_->X_hat(1, 0)) /
419 2.0) /
420 (ttrust_ * min_K_sum + min_FF_sum));
421 }
422
Brian Silverman718b1d72013-10-28 16:22:45 -0700423 double MaxVelocity() {
424 const Eigen::Matrix<double, 2, 2> FF =
425 loop_->B().inverse() *
426 (Eigen::Matrix<double, 2, 2>::Identity() - loop_->A());
427
428 constexpr int kHighGearController = 3;
429 const Eigen::Matrix<double, 2, 2> FF_high =
430 loop_->controller(kHighGearController).plant.B.inverse() *
431 (Eigen::Matrix<double, 2, 2>::Identity() -
432 loop_->controller(kHighGearController).plant.A);
433
434 ::Eigen::Matrix<double, 1, 2> FF_sum = FF.colwise().sum();
435 int min_FF_sum_index;
436 const double min_FF_sum = FF_sum.minCoeff(&min_FF_sum_index);
437 //const double min_K_sum = loop_->K().col(min_FF_sum_index).sum();
438 const double high_min_FF_sum = FF_high.col(0).sum();
439
440 const double adjusted_ff_voltage = ::aos::Clip(
441 12.0 * min_FF_sum / high_min_FF_sum, -12.0, 12.0);
442 return adjusted_ff_voltage / min_FF_sum;
443 }
444
Austin Schuh2054f5f2013-10-27 14:54:10 -0700445 void Update() {
Austin Schuh78d55462014-02-23 01:39:30 -0800446 const auto &values = constants::GetValues();
Austin Schuh427b3702013-11-02 13:44:09 -0700447 // TODO(austin): Observer for the current velocity instead of difference
448 // calculations.
Brian Silvermande8fd552013-11-03 15:53:42 -0800449 ++counter_;
Austin Schuh427b3702013-11-02 13:44:09 -0700450 const double current_left_velocity =
Brian Silvermande8fd552013-11-03 15:53:42 -0800451 (position_.left_encoder - last_position_.left_encoder) /
Austin Schuh427b3702013-11-02 13:44:09 -0700452 position_time_delta_;
453 const double current_right_velocity =
Brian Silvermande8fd552013-11-03 15:53:42 -0800454 (position_.right_encoder - last_position_.right_encoder) /
Austin Schuh427b3702013-11-02 13:44:09 -0700455 position_time_delta_;
456 const double left_motor_speed =
Austin Schuh78d55462014-02-23 01:39:30 -0800457 MotorSpeed(values.left_drive, position_.left_shifter_position,
458 current_left_velocity);
Austin Schuh427b3702013-11-02 13:44:09 -0700459 const double right_motor_speed =
Austin Schuh78d55462014-02-23 01:39:30 -0800460 MotorSpeed(values.right_drive, position_.right_shifter_position,
461 current_right_velocity);
Austin Schuh2054f5f2013-10-27 14:54:10 -0700462
Brian Silverman61e41fd2014-02-16 19:08:50 -0800463 {
464 CIMLogging logging;
465
466 // Reset the CIM model to the current conditions to be ready for when we
467 // shift.
468 if (IsInGear(left_gear_)) {
469 logging.left_in_gear = true;
470 left_cim_->X_hat(0, 0) = left_motor_speed;
471 } else {
472 logging.left_in_gear = false;
473 }
474 logging.left_motor_speed = left_motor_speed;
475 logging.left_velocity = current_left_velocity;
476 if (IsInGear(right_gear_)) {
477 logging.right_in_gear = true;
478 right_cim_->X_hat(0, 0) = right_motor_speed;
479 } else {
480 logging.right_in_gear = false;
481 }
482 logging.right_motor_speed = right_motor_speed;
483 logging.right_velocity = current_right_velocity;
484
485 LOG_STRUCT(DEBUG, "currently", logging);
Austin Schuh427b3702013-11-02 13:44:09 -0700486 }
Austin Schuh2054f5f2013-10-27 14:54:10 -0700487
Austin Schuh427b3702013-11-02 13:44:09 -0700488 if (IsInGear(left_gear_) && IsInGear(right_gear_)) {
489 // FF * X = U (steady state)
490 const Eigen::Matrix<double, 2, 2> FF =
491 loop_->B().inverse() *
492 (Eigen::Matrix<double, 2, 2>::Identity() - loop_->A());
493
494 // Invert the plant to figure out how the velocity filter would have to
495 // work
496 // out in order to filter out the forwards negative inertia.
497 // This math assumes that the left and right power and velocity are
498 // equals,
499 // and that the plant is the same on the left and right.
500 const double fvel = FilterVelocity(throttle_);
501
502 const double sign_svel = wheel_ * ((fvel > 0.0) ? 1.0 : -1.0);
503 double steering_velocity;
504 if (quickturn_) {
505 steering_velocity = wheel_ * MaxVelocity();
506 } else {
507 steering_velocity = ::std::abs(fvel) * wheel_;
508 }
509 const double left_velocity = fvel - steering_velocity;
510 const double right_velocity = fvel + steering_velocity;
511
512 // Integrate velocity to get the position.
513 // This position is used to get integral control.
514 loop_->R << left_velocity, right_velocity;
515
516 if (!quickturn_) {
517 // K * R = w
518 Eigen::Matrix<double, 1, 2> equality_k;
519 equality_k << 1 + sign_svel, -(1 - sign_svel);
520 const double equality_w = 0.0;
521
522 // Construct a constraint on R by manipulating the constraint on U
523 ::aos::controls::HPolytope<2> R_poly = ::aos::controls::HPolytope<2>(
524 U_Poly_.H() * (loop_->K() + FF),
525 U_Poly_.k() + U_Poly_.H() * loop_->K() * loop_->X_hat);
526
527 // Limit R back inside the box.
528 loop_->R = CoerceGoal(R_poly, equality_k, equality_w, loop_->R);
529 }
530
531 const Eigen::Matrix<double, 2, 1> FF_volts = FF * loop_->R;
532 const Eigen::Matrix<double, 2, 1> U_ideal =
533 loop_->K() * (loop_->R - loop_->X_hat) + FF_volts;
534
535 for (int i = 0; i < 2; i++) {
536 loop_->U[i] = ::aos::Clip(U_ideal[i], -12, 12);
537 }
Brian Silvermande8fd552013-11-03 15:53:42 -0800538
539 // TODO(austin): Model this better.
540 // TODO(austin): Feed back?
541 loop_->X_hat = loop_->A() * loop_->X_hat + loop_->B() * loop_->U;
Brian Silverman718b1d72013-10-28 16:22:45 -0700542 } else {
Austin Schuh427b3702013-11-02 13:44:09 -0700543 // Any motor is not in gear. Speed match.
544 ::Eigen::Matrix<double, 1, 1> R_left;
Austin Schuh427b3702013-11-02 13:44:09 -0700545 ::Eigen::Matrix<double, 1, 1> R_right;
Austin Schuhb5afb692014-03-02 11:54:11 -0800546 R_left(0, 0) = left_motor_speed;
Austin Schuh427b3702013-11-02 13:44:09 -0700547 R_right(0, 0) = right_motor_speed;
Austin Schuhb5afb692014-03-02 11:54:11 -0800548
549 const double wiggle =
550 (static_cast<double>((counter_ % 4) / 4.0) - 0.5) * 4.0;
551
552 loop_->U(0, 0) = ::aos::Clip((R_left / Kv)(0, 0) + wiggle, -12.0, 12.0);
553 loop_->U(1, 0) = ::aos::Clip((R_right / Kv)(0, 0) + wiggle, -12.0, 12.0);
Austin Schuh2054f5f2013-10-27 14:54:10 -0700554 }
Austin Schuh2054f5f2013-10-27 14:54:10 -0700555 }
556
557 void SendMotors(Drivetrain::Output *output) {
Brian Silvermande8fd552013-11-03 15:53:42 -0800558 if (output != NULL) {
559 output->left_voltage = loop_->U(0, 0);
560 output->right_voltage = loop_->U(1, 0);
Brian Silverman61e41fd2014-02-16 19:08:50 -0800561 output->left_high = left_gear_ == HIGH || left_gear_ == SHIFTING_UP;
562 output->right_high = right_gear_ == HIGH || right_gear_ == SHIFTING_UP;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700563 }
564 }
565
566 private:
567 const ::aos::controls::HPolytope<2> U_Poly_;
568
569 ::std::unique_ptr<StateFeedbackLoop<2, 2, 2>> loop_;
Austin Schuh427b3702013-11-02 13:44:09 -0700570 ::std::unique_ptr<StateFeedbackLoop<1, 1, 1>> left_cim_;
571 ::std::unique_ptr<StateFeedbackLoop<1, 1, 1>> right_cim_;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700572
Austin Schuh427b3702013-11-02 13:44:09 -0700573 const double ttrust_;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700574 double wheel_;
575 double throttle_;
576 bool quickturn_;
Austin Schuh427b3702013-11-02 13:44:09 -0700577 int stale_count_;
578 double position_time_delta_;
579 Gear left_gear_;
580 Gear right_gear_;
581 Drivetrain::Position last_position_;
582 Drivetrain::Position position_;
Brian Silvermande8fd552013-11-03 15:53:42 -0800583 int counter_;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700584};
Brian Silvermancec3c8d2013-12-20 21:04:55 -0800585constexpr double PolyDrivetrain::kStallTorque;
586constexpr double PolyDrivetrain::kStallCurrent;
587constexpr double PolyDrivetrain::kFreeSpeed;
588constexpr double PolyDrivetrain::kFreeCurrent;
589constexpr double PolyDrivetrain::J;
590constexpr double PolyDrivetrain::m;
591constexpr double PolyDrivetrain::rb;
592constexpr double PolyDrivetrain::kWheelRadius;
593constexpr double PolyDrivetrain::kR;
594constexpr double PolyDrivetrain::Kv;
595constexpr double PolyDrivetrain::Kt;
596
Austin Schuh2054f5f2013-10-27 14:54:10 -0700597
brians343bc112013-02-10 01:53:46 +0000598void DrivetrainLoop::RunIteration(const Drivetrain::Goal *goal,
599 const Drivetrain::Position *position,
600 Drivetrain::Output *output,
Ben Fredrickson890c3fe2014-03-02 00:15:16 +0000601 Drivetrain::Status * status) {
brians343bc112013-02-10 01:53:46 +0000602 // TODO(aschuh): These should be members of the class.
603 static DrivetrainMotorsSS dt_closedloop;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700604 static PolyDrivetrain dt_openloop;
brians343bc112013-02-10 01:53:46 +0000605
606 bool bad_pos = false;
Austin Schuh427b3702013-11-02 13:44:09 -0700607 if (position == nullptr) {
Brian Silverman50a9d032014-02-16 17:20:57 -0800608 LOG_INTERVAL(no_position_);
brians343bc112013-02-10 01:53:46 +0000609 bad_pos = true;
610 }
Brian Silverman9c9ab422014-02-25 13:42:53 -0800611 no_position_.Print();
brians343bc112013-02-10 01:53:46 +0000612
613 double wheel = goal->steering;
614 double throttle = goal->throttle;
615 bool quickturn = goal->quickturn;
616 bool highgear = goal->highgear;
617
618 bool control_loop_driving = goal->control_loop_driving;
619 double left_goal = goal->left_goal;
620 double right_goal = goal->right_goal;
621
Austin Schuh2054f5f2013-10-27 14:54:10 -0700622 dt_closedloop.SetGoal(left_goal, goal->left_velocity_goal, right_goal,
623 goal->right_velocity_goal);
brians343bc112013-02-10 01:53:46 +0000624 if (!bad_pos) {
625 const double left_encoder = position->left_encoder;
626 const double right_encoder = position->right_encoder;
Ben Fredrickson81ba2d52014-03-02 08:21:46 +0000627 if (othersensors.FetchLatest()) {
628 LOG(DEBUG, "using %.3f", othersensors->gyro_angle);
629 dt_closedloop.SetPosition(left_encoder, right_encoder, othersensors->gyro_angle,
Austin Schuh2054f5f2013-10-27 14:54:10 -0700630 control_loop_driving);
brians343bc112013-02-10 01:53:46 +0000631 } else {
632 dt_closedloop.SetRawPosition(left_encoder, right_encoder);
633 }
634 }
Austin Schuh427b3702013-11-02 13:44:09 -0700635 dt_openloop.SetPosition(position);
brians343bc112013-02-10 01:53:46 +0000636 dt_openloop.SetGoal(wheel, throttle, quickturn, highgear);
637 dt_openloop.Update();
Ben Fredrickson890c3fe2014-03-02 00:15:16 +0000638
Brian Silverman2845c4c2013-03-16 19:54:08 -0700639 if (control_loop_driving) {
Ben Fredrickson890c3fe2014-03-02 00:15:16 +0000640 dt_closedloop.Update(output == NULL);
Brian Silverman2845c4c2013-03-16 19:54:08 -0700641 dt_closedloop.SendMotors(output);
642 } else {
643 dt_openloop.SendMotors(output);
Austin Schuhb5afb692014-03-02 11:54:11 -0800644 if (output) {
645 dt_closedloop.SetExternalMotors(output->left_voltage,
646 output->right_voltage);
647 }
Ben Fredrickson890c3fe2014-03-02 00:15:16 +0000648 dt_closedloop.Update(output == NULL);
brians343bc112013-02-10 01:53:46 +0000649 }
Ben Fredrickson890c3fe2014-03-02 00:15:16 +0000650
651 // set the output status of the controll loop state
652 if (status) {
653 bool done = false;
654 if (goal) {
655 done = ((::std::abs(goal->left_goal -
656 dt_closedloop.GetEstimatedLeftEncoder()) <
657 constants::GetValues().drivetrain_done_distance) &&
658 (::std::abs(goal->right_goal -
659 dt_closedloop.GetEstimatedRightEncoder()) <
660 constants::GetValues().drivetrain_done_distance));
661 }
662 status->is_done = done;
663 status->robot_speed = dt_closedloop.GetEstimatedRobotSpeed();
664 }
brians343bc112013-02-10 01:53:46 +0000665}
666
667} // namespace control_loops
668} // namespace frc971