blob: 9f3b94d9576b9414bf2458af0d6373ed559e870e [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 }
Brian Silverman3146b642014-03-20 14:52:46 -0700129 ::Eigen::Matrix<double, 4, 1> E = loop_->R - loop_->X_hat;
130 LOG_MATRIX(DEBUG, "E", E);
Ben Fredrickson890c3fe2014-03-02 00:15:16 +0000131 }
132
133 double GetEstimatedRobotSpeed() {
134 // lets just call the average of left and right velocities close enough
135 return (loop_->X_hat(1, 0) + loop_->X_hat(3, 0)) / 2;
136 }
137
138 double GetEstimatedLeftEncoder() {
139 // lets just call the average of left and right velocities close enough
140 return loop_->X_hat(0, 0);
141 }
142
143 double GetEstimatedRightEncoder() {
144 return loop_->X_hat(2, 0);
brians343bc112013-02-10 01:53:46 +0000145 }
146
Austin Schuh4352ac62013-03-19 06:23:16 +0000147 void SendMotors(Drivetrain::Output *output) {
148 if (output) {
149 output->left_voltage = loop_->U(0, 0);
150 output->right_voltage = loop_->U(1, 0);
brians8ad74052013-03-16 21:04:51 +0000151 }
brians343bc112013-02-10 01:53:46 +0000152 }
brians343bc112013-02-10 01:53:46 +0000153
154 private:
Austin Schuh4352ac62013-03-19 06:23:16 +0000155 ::std::unique_ptr<StateFeedbackLoop<4, 2, 2>> loop_;
156
brians343bc112013-02-10 01:53:46 +0000157 double _integral_offset;
158 double _offset;
159 double _gyro;
160 double _left_goal;
161 double _right_goal;
162 double _raw_left;
163 double _raw_right;
Austin Schuh4352ac62013-03-19 06:23:16 +0000164 bool _control_loop_driving;
brians343bc112013-02-10 01:53:46 +0000165};
166
Austin Schuh2054f5f2013-10-27 14:54:10 -0700167class PolyDrivetrain {
168 public:
Austin Schuh427b3702013-11-02 13:44:09 -0700169
170 enum Gear {
171 HIGH,
172 LOW,
173 SHIFTING_UP,
174 SHIFTING_DOWN
175 };
176 // Stall Torque in N m
177 static constexpr double kStallTorque = 2.42;
178 // Stall Current in Amps
179 static constexpr double kStallCurrent = 133;
180 // Free Speed in RPM. Used number from last year.
181 static constexpr double kFreeSpeed = 4650.0;
182 // Free Current in Amps
183 static constexpr double kFreeCurrent = 2.7;
184 // Moment of inertia of the drivetrain in kg m^2
185 // Just borrowed from last year.
186 static constexpr double J = 6.4;
187 // Mass of the robot, in kg.
188 static constexpr double m = 68;
189 // Radius of the robot, in meters (from last year).
190 static constexpr double rb = 0.617998644 / 2.0;
Brian Silverman1a6590d2013-11-04 14:46:46 -0800191 static constexpr double kWheelRadius = 0.04445;
Austin Schuh427b3702013-11-02 13:44:09 -0700192 // Resistance of the motor, divided by the number of motors.
193 static constexpr double kR = (12.0 / kStallCurrent / 4 + 0.03) / (0.93 * 0.93);
194 // Motor velocity constant
195 static constexpr double Kv =
196 ((kFreeSpeed / 60.0 * 2.0 * M_PI) / (12.0 - kR * kFreeCurrent));
197 // Torque constant
198 static constexpr double Kt = kStallTorque / kStallCurrent;
Austin Schuh427b3702013-11-02 13:44:09 -0700199
Austin Schuh2054f5f2013-10-27 14:54:10 -0700200 PolyDrivetrain()
201 : U_Poly_((Eigen::Matrix<double, 4, 2>() << /*[[*/ 1, 0 /*]*/,
202 /*[*/ -1, 0 /*]*/,
203 /*[*/ 0, 1 /*]*/,
204 /*[*/ 0, -1 /*]]*/).finished(),
205 (Eigen::Matrix<double, 4, 1>() << /*[[*/ 12 /*]*/,
206 /*[*/ 12 /*]*/,
207 /*[*/ 12 /*]*/,
208 /*[*/ 12 /*]]*/).finished()),
Brian Silverman2c590c32013-11-04 18:08:54 -0800209 loop_(new StateFeedbackLoop<2, 2, 2>(
210 constants::GetValues().make_v_drivetrain_loop())),
Austin Schuh427b3702013-11-02 13:44:09 -0700211 ttrust_(1.1),
212 wheel_(0.0),
213 throttle_(0.0),
214 quickturn_(false),
215 stale_count_(0),
216 position_time_delta_(0.01),
217 left_gear_(LOW),
Brian Silvermande8fd552013-11-03 15:53:42 -0800218 right_gear_(LOW),
219 counter_(0) {
Austin Schuh2054f5f2013-10-27 14:54:10 -0700220
Austin Schuh427b3702013-11-02 13:44:09 -0700221 last_position_.Zero();
222 position_.Zero();
Austin Schuh2054f5f2013-10-27 14:54:10 -0700223 }
Austin Schuh427b3702013-11-02 13:44:09 -0700224 static bool IsInGear(Gear gear) { return gear == LOW || gear == HIGH; }
225
Austin Schuh78d55462014-02-23 01:39:30 -0800226 static double MotorSpeed(const constants::ShifterHallEffect &hall_effect,
227 double shifter_position, double velocity) {
Austin Schuh427b3702013-11-02 13:44:09 -0700228 // TODO(austin): G_high, G_low and kWheelRadius
Austin Schuh78d55462014-02-23 01:39:30 -0800229 const double avg_hall_effect =
230 (hall_effect.clear_high + hall_effect.clear_low) / 2.0;
231
232 if (shifter_position > avg_hall_effect) {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800233 return velocity / constants::GetValues().high_gear_ratio / kWheelRadius;
Austin Schuh427b3702013-11-02 13:44:09 -0700234 } else {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800235 return velocity / constants::GetValues().low_gear_ratio / kWheelRadius;
236 }
237 }
238
Austin Schuh78d55462014-02-23 01:39:30 -0800239 Gear ComputeGear(const constants::ShifterHallEffect &hall_effect,
240 double velocity, Gear current) {
241 const double low_omega = MotorSpeed(hall_effect, 0.0, ::std::abs(velocity));
242 const double high_omega =
243 MotorSpeed(hall_effect, 1.0, ::std::abs(velocity));
Brian Silverman1a6590d2013-11-04 14:46:46 -0800244
Brian Silverman1a6590d2013-11-04 14:46:46 -0800245 double high_torque = ((12.0 - high_omega / Kv) * Kt / kR);
246 double low_torque = ((12.0 - low_omega / Kv) * Kt / kR);
247 double high_power = high_torque * high_omega;
248 double low_power = low_torque * low_omega;
Brian Silverman55a930b2013-11-04 20:59:00 -0800249
250 // TODO(aschuh): Do this right!
251 if ((current == HIGH || high_power > low_power + 160) &&
252 ::std::abs(velocity) > 0.14) {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800253 return HIGH;
254 } else {
255 return LOW;
Austin Schuh427b3702013-11-02 13:44:09 -0700256 }
257 }
258
Austin Schuh2054f5f2013-10-27 14:54:10 -0700259 void SetGoal(double wheel, double throttle, bool quickturn, bool highgear) {
Brian Silvermande8fd552013-11-03 15:53:42 -0800260 const double kWheelNonLinearity = 0.3;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700261 // Apply a sin function that's scaled to make it feel better.
262 const double angular_range = M_PI_2 * kWheelNonLinearity;
263 wheel_ = sin(angular_range * wheel) / sin(angular_range);
264 wheel_ = sin(angular_range * wheel_) / sin(angular_range);
Austin Schuh2054f5f2013-10-27 14:54:10 -0700265 quickturn_ = quickturn;
Austin Schuh427b3702013-11-02 13:44:09 -0700266
Brian Silverman1a6590d2013-11-04 14:46:46 -0800267 static const double kThrottleDeadband = 0.05;
268 if (::std::abs(throttle) < kThrottleDeadband) {
269 throttle_ = 0;
270 } else {
271 throttle_ = copysign((::std::abs(throttle) - kThrottleDeadband) /
272 (1.0 - kThrottleDeadband), throttle);
273 }
274
Austin Schuh427b3702013-11-02 13:44:09 -0700275 // TODO(austin): Fix the upshift logic to include states.
Brian Silverman1a6590d2013-11-04 14:46:46 -0800276 Gear requested_gear;
Brian Silverman55a930b2013-11-04 20:59:00 -0800277 if (false) {
Austin Schuh78d55462014-02-23 01:39:30 -0800278 const auto &values = constants::GetValues();
Brian Silverman1a6590d2013-11-04 14:46:46 -0800279 const double current_left_velocity =
280 (position_.left_encoder - last_position_.left_encoder) /
281 position_time_delta_;
282 const double current_right_velocity =
283 (position_.right_encoder - last_position_.right_encoder) /
284 position_time_delta_;
285
Austin Schuh78d55462014-02-23 01:39:30 -0800286 Gear left_requested =
287 ComputeGear(values.left_drive, current_left_velocity, left_gear_);
288 Gear right_requested =
289 ComputeGear(values.right_drive, current_right_velocity, right_gear_);
Brian Silverman1a6590d2013-11-04 14:46:46 -0800290 requested_gear =
291 (left_requested == HIGH || right_requested == HIGH) ? HIGH : LOW;
292 } else {
293 requested_gear = highgear ? HIGH : LOW;
294 }
295
296 const Gear shift_up =
297 constants::GetValues().clutch_transmission ? HIGH : SHIFTING_UP;
298 const Gear shift_down =
299 constants::GetValues().clutch_transmission ? LOW : SHIFTING_DOWN;
Austin Schuh427b3702013-11-02 13:44:09 -0700300
301 if (left_gear_ != requested_gear) {
302 if (IsInGear(left_gear_)) {
303 if (requested_gear == HIGH) {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800304 left_gear_ = shift_up;
Austin Schuh427b3702013-11-02 13:44:09 -0700305 } else {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800306 left_gear_ = shift_down;
Austin Schuh427b3702013-11-02 13:44:09 -0700307 }
Brian Silverman55a930b2013-11-04 20:59:00 -0800308 } else {
309 if (requested_gear == HIGH && left_gear_ == SHIFTING_DOWN) {
310 left_gear_ = SHIFTING_UP;
311 } else if (requested_gear == LOW && left_gear_ == SHIFTING_UP) {
312 left_gear_ = SHIFTING_DOWN;
313 }
Austin Schuh427b3702013-11-02 13:44:09 -0700314 }
315 }
316 if (right_gear_ != requested_gear) {
317 if (IsInGear(right_gear_)) {
318 if (requested_gear == HIGH) {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800319 right_gear_ = shift_up;
Austin Schuh427b3702013-11-02 13:44:09 -0700320 } else {
Brian Silverman1a6590d2013-11-04 14:46:46 -0800321 right_gear_ = shift_down;
Austin Schuh427b3702013-11-02 13:44:09 -0700322 }
Brian Silverman55a930b2013-11-04 20:59:00 -0800323 } else {
324 if (requested_gear == HIGH && right_gear_ == SHIFTING_DOWN) {
325 right_gear_ = SHIFTING_UP;
326 } else if (requested_gear == LOW && right_gear_ == SHIFTING_UP) {
327 right_gear_ = SHIFTING_DOWN;
328 }
Austin Schuh427b3702013-11-02 13:44:09 -0700329 }
Austin Schuh2054f5f2013-10-27 14:54:10 -0700330 }
331 }
Austin Schuh427b3702013-11-02 13:44:09 -0700332 void SetPosition(const Drivetrain::Position *position) {
Austin Schuh78d55462014-02-23 01:39:30 -0800333 const auto &values = constants::GetValues();
Austin Schuh427b3702013-11-02 13:44:09 -0700334 if (position == NULL) {
335 ++stale_count_;
336 } else {
337 last_position_ = position_;
338 position_ = *position;
339 position_time_delta_ = (stale_count_ + 1) * 0.01;
340 stale_count_ = 0;
341 }
342
343 if (position) {
Brian Silverman61e41fd2014-02-16 19:08:50 -0800344 GearLogging gear_logging;
Austin Schuh427b3702013-11-02 13:44:09 -0700345 // Switch to the correct controller.
Austin Schuh78d55462014-02-23 01:39:30 -0800346 const double left_middle_shifter_position =
347 (values.left_drive.clear_high + values.left_drive.clear_low) / 2.0;
348 const double right_middle_shifter_position =
349 (values.right_drive.clear_high + values.right_drive.clear_low) / 2.0;
350
351 if (position->left_shifter_position < left_middle_shifter_position) {
352 if (position->right_shifter_position < right_middle_shifter_position ||
353 right_gear_ == LOW) {
Brian Silverman61e41fd2014-02-16 19:08:50 -0800354 gear_logging.left_loop_high = false;
355 gear_logging.right_loop_high = false;
356 loop_->set_controller_index(gear_logging.controller_index = 0);
Austin Schuh427b3702013-11-02 13:44:09 -0700357 } else {
Brian Silverman61e41fd2014-02-16 19:08:50 -0800358 gear_logging.left_loop_high = false;
359 gear_logging.right_loop_high = true;
360 loop_->set_controller_index(gear_logging.controller_index = 1);
Austin Schuh427b3702013-11-02 13:44:09 -0700361 }
362 } else {
Austin Schuh78d55462014-02-23 01:39:30 -0800363 if (position->right_shifter_position < right_middle_shifter_position ||
364 left_gear_ == LOW) {
Brian Silverman61e41fd2014-02-16 19:08:50 -0800365 gear_logging.left_loop_high = true;
366 gear_logging.right_loop_high = false;
367 loop_->set_controller_index(gear_logging.controller_index = 2);
Austin Schuh427b3702013-11-02 13:44:09 -0700368 } else {
Brian Silverman61e41fd2014-02-16 19:08:50 -0800369 gear_logging.left_loop_high = true;
370 gear_logging.right_loop_high = true;
371 loop_->set_controller_index(gear_logging.controller_index = 3);
Austin Schuh427b3702013-11-02 13:44:09 -0700372 }
373 }
Brian Silverman61e41fd2014-02-16 19:08:50 -0800374
Austin Schuh427b3702013-11-02 13:44:09 -0700375 // TODO(austin): Constants.
Austin Schuh78d55462014-02-23 01:39:30 -0800376 if (position->left_shifter_position > values.left_drive.clear_high && left_gear_ == SHIFTING_UP) {
Austin Schuh427b3702013-11-02 13:44:09 -0700377 left_gear_ = HIGH;
378 }
Austin Schuh78d55462014-02-23 01:39:30 -0800379 if (position->left_shifter_position < values.left_drive.clear_low && left_gear_ == SHIFTING_DOWN) {
Austin Schuh427b3702013-11-02 13:44:09 -0700380 left_gear_ = LOW;
381 }
Austin Schuh78d55462014-02-23 01:39:30 -0800382 if (position->right_shifter_position > values.right_drive.clear_high && right_gear_ == SHIFTING_UP) {
Austin Schuh427b3702013-11-02 13:44:09 -0700383 right_gear_ = HIGH;
384 }
Austin Schuh78d55462014-02-23 01:39:30 -0800385 if (position->right_shifter_position < values.right_drive.clear_low && right_gear_ == SHIFTING_DOWN) {
Austin Schuh427b3702013-11-02 13:44:09 -0700386 right_gear_ = LOW;
387 }
Brian Silverman61e41fd2014-02-16 19:08:50 -0800388
389 gear_logging.left_state = left_gear_;
390 gear_logging.right_state = right_gear_;
391 LOG_STRUCT(DEBUG, "state", gear_logging);
Austin Schuh427b3702013-11-02 13:44:09 -0700392 }
393 }
394
Austin Schuh2054f5f2013-10-27 14:54:10 -0700395 double FilterVelocity(double throttle) {
396 const Eigen::Matrix<double, 2, 2> FF =
397 loop_->B().inverse() *
398 (Eigen::Matrix<double, 2, 2>::Identity() - loop_->A());
399
400 constexpr int kHighGearController = 3;
401 const Eigen::Matrix<double, 2, 2> FF_high =
402 loop_->controller(kHighGearController).plant.B.inverse() *
403 (Eigen::Matrix<double, 2, 2>::Identity() -
404 loop_->controller(kHighGearController).plant.A);
405
406 ::Eigen::Matrix<double, 1, 2> FF_sum = FF.colwise().sum();
407 int min_FF_sum_index;
408 const double min_FF_sum = FF_sum.minCoeff(&min_FF_sum_index);
409 const double min_K_sum = loop_->K().col(min_FF_sum_index).sum();
410 const double high_min_FF_sum = FF_high.col(0).sum();
411
412 const double adjusted_ff_voltage = ::aos::Clip(
413 throttle * 12.0 * min_FF_sum / high_min_FF_sum, -12.0, 12.0);
414 return ((adjusted_ff_voltage +
415 ttrust_ * min_K_sum * (loop_->X_hat(0, 0) + loop_->X_hat(1, 0)) /
416 2.0) /
417 (ttrust_ * min_K_sum + min_FF_sum));
418 }
419
Brian Silverman718b1d72013-10-28 16:22:45 -0700420 double MaxVelocity() {
421 const Eigen::Matrix<double, 2, 2> FF =
422 loop_->B().inverse() *
423 (Eigen::Matrix<double, 2, 2>::Identity() - loop_->A());
424
425 constexpr int kHighGearController = 3;
426 const Eigen::Matrix<double, 2, 2> FF_high =
427 loop_->controller(kHighGearController).plant.B.inverse() *
428 (Eigen::Matrix<double, 2, 2>::Identity() -
429 loop_->controller(kHighGearController).plant.A);
430
431 ::Eigen::Matrix<double, 1, 2> FF_sum = FF.colwise().sum();
432 int min_FF_sum_index;
433 const double min_FF_sum = FF_sum.minCoeff(&min_FF_sum_index);
434 //const double min_K_sum = loop_->K().col(min_FF_sum_index).sum();
435 const double high_min_FF_sum = FF_high.col(0).sum();
436
437 const double adjusted_ff_voltage = ::aos::Clip(
438 12.0 * min_FF_sum / high_min_FF_sum, -12.0, 12.0);
439 return adjusted_ff_voltage / min_FF_sum;
440 }
441
Austin Schuh2054f5f2013-10-27 14:54:10 -0700442 void Update() {
Austin Schuh78d55462014-02-23 01:39:30 -0800443 const auto &values = constants::GetValues();
Austin Schuh427b3702013-11-02 13:44:09 -0700444 // TODO(austin): Observer for the current velocity instead of difference
445 // calculations.
Brian Silvermande8fd552013-11-03 15:53:42 -0800446 ++counter_;
Austin Schuh427b3702013-11-02 13:44:09 -0700447 const double current_left_velocity =
Brian Silvermande8fd552013-11-03 15:53:42 -0800448 (position_.left_encoder - last_position_.left_encoder) /
Austin Schuh427b3702013-11-02 13:44:09 -0700449 position_time_delta_;
450 const double current_right_velocity =
Brian Silvermande8fd552013-11-03 15:53:42 -0800451 (position_.right_encoder - last_position_.right_encoder) /
Austin Schuh427b3702013-11-02 13:44:09 -0700452 position_time_delta_;
453 const double left_motor_speed =
Austin Schuh78d55462014-02-23 01:39:30 -0800454 MotorSpeed(values.left_drive, position_.left_shifter_position,
455 current_left_velocity);
Austin Schuh427b3702013-11-02 13:44:09 -0700456 const double right_motor_speed =
Austin Schuh78d55462014-02-23 01:39:30 -0800457 MotorSpeed(values.right_drive, position_.right_shifter_position,
458 current_right_velocity);
Austin Schuh2054f5f2013-10-27 14:54:10 -0700459
Brian Silverman61e41fd2014-02-16 19:08:50 -0800460 {
461 CIMLogging logging;
462
463 // Reset the CIM model to the current conditions to be ready for when we
464 // shift.
465 if (IsInGear(left_gear_)) {
466 logging.left_in_gear = true;
Brian Silverman61e41fd2014-02-16 19:08:50 -0800467 } else {
468 logging.left_in_gear = false;
469 }
470 logging.left_motor_speed = left_motor_speed;
471 logging.left_velocity = current_left_velocity;
472 if (IsInGear(right_gear_)) {
473 logging.right_in_gear = true;
Brian Silverman61e41fd2014-02-16 19:08:50 -0800474 } else {
475 logging.right_in_gear = false;
476 }
477 logging.right_motor_speed = right_motor_speed;
478 logging.right_velocity = current_right_velocity;
479
480 LOG_STRUCT(DEBUG, "currently", logging);
Austin Schuh427b3702013-11-02 13:44:09 -0700481 }
Austin Schuh2054f5f2013-10-27 14:54:10 -0700482
Austin Schuh427b3702013-11-02 13:44:09 -0700483 if (IsInGear(left_gear_) && IsInGear(right_gear_)) {
484 // FF * X = U (steady state)
485 const Eigen::Matrix<double, 2, 2> FF =
486 loop_->B().inverse() *
487 (Eigen::Matrix<double, 2, 2>::Identity() - loop_->A());
488
489 // Invert the plant to figure out how the velocity filter would have to
490 // work
491 // out in order to filter out the forwards negative inertia.
492 // This math assumes that the left and right power and velocity are
493 // equals,
494 // and that the plant is the same on the left and right.
495 const double fvel = FilterVelocity(throttle_);
496
497 const double sign_svel = wheel_ * ((fvel > 0.0) ? 1.0 : -1.0);
498 double steering_velocity;
499 if (quickturn_) {
500 steering_velocity = wheel_ * MaxVelocity();
501 } else {
502 steering_velocity = ::std::abs(fvel) * wheel_;
503 }
504 const double left_velocity = fvel - steering_velocity;
505 const double right_velocity = fvel + steering_velocity;
506
507 // Integrate velocity to get the position.
508 // This position is used to get integral control.
509 loop_->R << left_velocity, right_velocity;
510
511 if (!quickturn_) {
512 // K * R = w
513 Eigen::Matrix<double, 1, 2> equality_k;
514 equality_k << 1 + sign_svel, -(1 - sign_svel);
515 const double equality_w = 0.0;
516
517 // Construct a constraint on R by manipulating the constraint on U
518 ::aos::controls::HPolytope<2> R_poly = ::aos::controls::HPolytope<2>(
519 U_Poly_.H() * (loop_->K() + FF),
520 U_Poly_.k() + U_Poly_.H() * loop_->K() * loop_->X_hat);
521
522 // Limit R back inside the box.
523 loop_->R = CoerceGoal(R_poly, equality_k, equality_w, loop_->R);
524 }
525
526 const Eigen::Matrix<double, 2, 1> FF_volts = FF * loop_->R;
527 const Eigen::Matrix<double, 2, 1> U_ideal =
528 loop_->K() * (loop_->R - loop_->X_hat) + FF_volts;
529
530 for (int i = 0; i < 2; i++) {
531 loop_->U[i] = ::aos::Clip(U_ideal[i], -12, 12);
532 }
Brian Silvermande8fd552013-11-03 15:53:42 -0800533
534 // TODO(austin): Model this better.
535 // TODO(austin): Feed back?
536 loop_->X_hat = loop_->A() * loop_->X_hat + loop_->B() * loop_->U;
Brian Silverman718b1d72013-10-28 16:22:45 -0700537 } else {
Austin Schuh427b3702013-11-02 13:44:09 -0700538 // Any motor is not in gear. Speed match.
539 ::Eigen::Matrix<double, 1, 1> R_left;
Austin Schuh427b3702013-11-02 13:44:09 -0700540 ::Eigen::Matrix<double, 1, 1> R_right;
Austin Schuhb5afb692014-03-02 11:54:11 -0800541 R_left(0, 0) = left_motor_speed;
Austin Schuh427b3702013-11-02 13:44:09 -0700542 R_right(0, 0) = right_motor_speed;
Austin Schuhb5afb692014-03-02 11:54:11 -0800543
544 const double wiggle =
Austin Schuhaf23eaa2014-03-02 14:00:19 -0800545 (static_cast<double>((counter_ % 10) / 5) - 0.5) * 5.0;
Austin Schuhb5afb692014-03-02 11:54:11 -0800546
547 loop_->U(0, 0) = ::aos::Clip((R_left / Kv)(0, 0) + wiggle, -12.0, 12.0);
548 loop_->U(1, 0) = ::aos::Clip((R_right / Kv)(0, 0) + wiggle, -12.0, 12.0);
Austin Schuh2054f5f2013-10-27 14:54:10 -0700549 }
Austin Schuh2054f5f2013-10-27 14:54:10 -0700550 }
551
552 void SendMotors(Drivetrain::Output *output) {
Brian Silvermande8fd552013-11-03 15:53:42 -0800553 if (output != NULL) {
554 output->left_voltage = loop_->U(0, 0);
555 output->right_voltage = loop_->U(1, 0);
Brian Silverman61e41fd2014-02-16 19:08:50 -0800556 output->left_high = left_gear_ == HIGH || left_gear_ == SHIFTING_UP;
557 output->right_high = right_gear_ == HIGH || right_gear_ == SHIFTING_UP;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700558 }
559 }
560
561 private:
562 const ::aos::controls::HPolytope<2> U_Poly_;
563
564 ::std::unique_ptr<StateFeedbackLoop<2, 2, 2>> loop_;
565
Austin Schuh427b3702013-11-02 13:44:09 -0700566 const double ttrust_;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700567 double wheel_;
568 double throttle_;
569 bool quickturn_;
Austin Schuh427b3702013-11-02 13:44:09 -0700570 int stale_count_;
571 double position_time_delta_;
572 Gear left_gear_;
573 Gear right_gear_;
574 Drivetrain::Position last_position_;
575 Drivetrain::Position position_;
Brian Silvermande8fd552013-11-03 15:53:42 -0800576 int counter_;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700577};
Brian Silvermancec3c8d2013-12-20 21:04:55 -0800578constexpr double PolyDrivetrain::kStallTorque;
579constexpr double PolyDrivetrain::kStallCurrent;
580constexpr double PolyDrivetrain::kFreeSpeed;
581constexpr double PolyDrivetrain::kFreeCurrent;
582constexpr double PolyDrivetrain::J;
583constexpr double PolyDrivetrain::m;
584constexpr double PolyDrivetrain::rb;
585constexpr double PolyDrivetrain::kWheelRadius;
586constexpr double PolyDrivetrain::kR;
587constexpr double PolyDrivetrain::Kv;
588constexpr double PolyDrivetrain::Kt;
589
Austin Schuh2054f5f2013-10-27 14:54:10 -0700590
brians343bc112013-02-10 01:53:46 +0000591void DrivetrainLoop::RunIteration(const Drivetrain::Goal *goal,
592 const Drivetrain::Position *position,
593 Drivetrain::Output *output,
Ben Fredrickson890c3fe2014-03-02 00:15:16 +0000594 Drivetrain::Status * status) {
brians343bc112013-02-10 01:53:46 +0000595 // TODO(aschuh): These should be members of the class.
596 static DrivetrainMotorsSS dt_closedloop;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700597 static PolyDrivetrain dt_openloop;
brians343bc112013-02-10 01:53:46 +0000598
599 bool bad_pos = false;
Austin Schuh427b3702013-11-02 13:44:09 -0700600 if (position == nullptr) {
Brian Silverman50a9d032014-02-16 17:20:57 -0800601 LOG_INTERVAL(no_position_);
brians343bc112013-02-10 01:53:46 +0000602 bad_pos = true;
603 }
Brian Silverman9c9ab422014-02-25 13:42:53 -0800604 no_position_.Print();
brians343bc112013-02-10 01:53:46 +0000605
606 double wheel = goal->steering;
607 double throttle = goal->throttle;
608 bool quickturn = goal->quickturn;
609 bool highgear = goal->highgear;
610
611 bool control_loop_driving = goal->control_loop_driving;
612 double left_goal = goal->left_goal;
613 double right_goal = goal->right_goal;
614
Austin Schuh2054f5f2013-10-27 14:54:10 -0700615 dt_closedloop.SetGoal(left_goal, goal->left_velocity_goal, right_goal,
616 goal->right_velocity_goal);
brians343bc112013-02-10 01:53:46 +0000617 if (!bad_pos) {
618 const double left_encoder = position->left_encoder;
619 const double right_encoder = position->right_encoder;
Ben Fredrickson81ba2d52014-03-02 08:21:46 +0000620 if (othersensors.FetchLatest()) {
621 LOG(DEBUG, "using %.3f", othersensors->gyro_angle);
622 dt_closedloop.SetPosition(left_encoder, right_encoder, othersensors->gyro_angle,
Austin Schuh2054f5f2013-10-27 14:54:10 -0700623 control_loop_driving);
brians343bc112013-02-10 01:53:46 +0000624 } else {
625 dt_closedloop.SetRawPosition(left_encoder, right_encoder);
626 }
627 }
Austin Schuh427b3702013-11-02 13:44:09 -0700628 dt_openloop.SetPosition(position);
brians343bc112013-02-10 01:53:46 +0000629 dt_openloop.SetGoal(wheel, throttle, quickturn, highgear);
630 dt_openloop.Update();
Ben Fredrickson890c3fe2014-03-02 00:15:16 +0000631
Brian Silverman2845c4c2013-03-16 19:54:08 -0700632 if (control_loop_driving) {
Ben Fredrickson890c3fe2014-03-02 00:15:16 +0000633 dt_closedloop.Update(output == NULL);
Brian Silverman2845c4c2013-03-16 19:54:08 -0700634 dt_closedloop.SendMotors(output);
635 } else {
636 dt_openloop.SendMotors(output);
Austin Schuhb5afb692014-03-02 11:54:11 -0800637 if (output) {
638 dt_closedloop.SetExternalMotors(output->left_voltage,
639 output->right_voltage);
640 }
Ben Fredrickson890c3fe2014-03-02 00:15:16 +0000641 dt_closedloop.Update(output == NULL);
brians343bc112013-02-10 01:53:46 +0000642 }
Ben Fredrickson890c3fe2014-03-02 00:15:16 +0000643
644 // set the output status of the controll loop state
645 if (status) {
646 bool done = false;
647 if (goal) {
648 done = ((::std::abs(goal->left_goal -
649 dt_closedloop.GetEstimatedLeftEncoder()) <
650 constants::GetValues().drivetrain_done_distance) &&
651 (::std::abs(goal->right_goal -
652 dt_closedloop.GetEstimatedRightEncoder()) <
653 constants::GetValues().drivetrain_done_distance));
654 }
655 status->is_done = done;
656 status->robot_speed = dt_closedloop.GetEstimatedRobotSpeed();
brians343bc112013-02-10 01:53:46 +0000657 }
658}
659
660} // namespace control_loops
661} // namespace frc971