blob: ae313e5b22c5e303e1303e280f0c748a6bcb6972 [file] [log] [blame]
Daniel Pettiaece37f2014-10-25 17:13:44 -07001#include "bot3/control_loops/drivetrain/drivetrain.h"
2
3#include <math.h>
4#include <stdio.h>
5#include <sched.h>
6
7#include <functional>
8#include <memory>
9
10#include "Eigen/Dense"
11
12#include "aos/common/logging/logging.h"
13#include "aos/common/controls/polytope.h"
14#include "aos/common/commonmath.h"
15#include "aos/common/logging/queue_logging.h"
16#include "aos/common/logging/matrix_logging.h"
17
18#include "bot3/control_loops/drivetrain/drivetrain.q.h"
19#include "bot3/control_loops/drivetrain/drivetrain_constants.h"
20#include "bot3/control_loops/drivetrain/drivetrain_dog_motor_plant.h"
21#include "bot3/control_loops/drivetrain/polydrivetrain_cim_plant.h"
22#include "bot3/control_loops/drivetrain/polydrivetrain_dog_motor_plant.h"
23#include "frc971/control_loops/coerce_goal.h"
24#include "frc971/control_loops/state_feedback_loop.h"
25#include "frc971/queues/other_sensors.q.h"
26
27using ::frc971::sensors::gyro_reading;
28using ::frc971::control_loops::DoCoerceGoal;
29using ::frc971::control_loops::CoerceGoal;
30
31namespace bot3 {
32namespace control_loops {
33
34class DrivetrainMotorsSS {
35 public:
36 class LimitedDrivetrainLoop : public StateFeedbackLoop<4, 2, 2> {
37 public:
38 LimitedDrivetrainLoop(StateFeedbackLoop<4, 2, 2> &&loop)
39 : StateFeedbackLoop<4, 2, 2>(::std::move(loop)),
40 U_Poly_((Eigen::Matrix<double, 4, 2>() << 1, 0,
41 -1, 0,
42 0, 1,
43 0, -1).finished(),
44 (Eigen::Matrix<double, 4, 1>() << 12.0, 12.0,
45 12.0, 12.0).finished()) {
46 ::aos::controls::HPolytope<0>::Init();
47 T << 1, -1, 1, 1;
48 T_inverse = T.inverse();
49 }
50
51 bool output_was_capped() const {
52 return output_was_capped_;
53 }
54
55 private:
56 virtual void CapU() {
57 const Eigen::Matrix<double, 4, 1> error = R() - X_hat();
58
59 if (::std::abs(U(0, 0)) > 12.0 || ::std::abs(U(1, 0)) > 12.0) {
60 output_was_capped_ = true;
61 LOG_MATRIX(DEBUG, "U at start", U());
62
63 Eigen::Matrix<double, 2, 2> position_K;
64 position_K << K(0, 0), K(0, 2),
65 K(1, 0), K(1, 2);
66 Eigen::Matrix<double, 2, 2> velocity_K;
67 velocity_K << K(0, 1), K(0, 3),
68 K(1, 1), K(1, 3);
69
70 Eigen::Matrix<double, 2, 1> position_error;
71 position_error << error(0, 0), error(2, 0);
72 const auto drive_error = T_inverse * position_error;
73 Eigen::Matrix<double, 2, 1> velocity_error;
74 velocity_error << error(1, 0), error(3, 0);
75 LOG_MATRIX(DEBUG, "error", error);
76
77 const auto &poly = U_Poly_;
78 const Eigen::Matrix<double, 4, 2> pos_poly_H =
79 poly.H() * position_K * T;
80 const Eigen::Matrix<double, 4, 1> pos_poly_k =
81 poly.k() - poly.H() * velocity_K * velocity_error;
82 const ::aos::controls::HPolytope<2> pos_poly(pos_poly_H, pos_poly_k);
83
84 Eigen::Matrix<double, 2, 1> adjusted_pos_error;
85 {
86 const auto &P = drive_error;
87
88 Eigen::Matrix<double, 1, 2> L45;
89 L45 << ::aos::sign(P(1, 0)), -::aos::sign(P(0, 0));
90 const double w45 = 0;
91
92 Eigen::Matrix<double, 1, 2> LH;
93 if (::std::abs(P(0, 0)) > ::std::abs(P(1, 0))) {
94 LH << 0, 1;
95 } else {
96 LH << 1, 0;
97 }
98 const double wh = LH.dot(P);
99
100 Eigen::Matrix<double, 2, 2> standard;
101 standard << L45, LH;
102 Eigen::Matrix<double, 2, 1> W;
103 W << w45, wh;
104 const Eigen::Matrix<double, 2, 1> intersection =
105 standard.inverse() * W;
106
107 bool is_inside_h;
108 const auto adjusted_pos_error_h =
109 DoCoerceGoal(pos_poly, LH, wh, drive_error, &is_inside_h);
110 const auto adjusted_pos_error_45 =
111 DoCoerceGoal(pos_poly, L45, w45, intersection, nullptr);
112 if (pos_poly.IsInside(intersection)) {
113 adjusted_pos_error = adjusted_pos_error_h;
114 } else {
115 if (is_inside_h) {
116 if (adjusted_pos_error_h.norm() > adjusted_pos_error_45.norm()) {
117 adjusted_pos_error = adjusted_pos_error_h;
118 } else {
119 adjusted_pos_error = adjusted_pos_error_45;
120 }
121 } else {
122 adjusted_pos_error = adjusted_pos_error_45;
123 }
124 }
125 }
126
127 LOG_MATRIX(DEBUG, "adjusted_pos_error", adjusted_pos_error);
128 mutable_U() =
129 velocity_K * velocity_error + position_K * T * adjusted_pos_error;
130 LOG_MATRIX(DEBUG, "U is now", U());
131 } else {
132 output_was_capped_ = false;
133 }
134 }
135
136 const ::aos::controls::HPolytope<2> U_Poly_;
137 Eigen::Matrix<double, 2, 2> T, T_inverse;
138 bool output_was_capped_ = false;;
139 };
140
141 DrivetrainMotorsSS()
142 : loop_(new LimitedDrivetrainLoop(
143 MakeDrivetrainLoop())),
144 filtered_offset_(0.0),
145 gyro_(0.0),
146 left_goal_(0.0),
147 right_goal_(0.0),
148 raw_left_(0.0),
149 raw_right_(0.0) {
150 // Low gear on both.
151 loop_->set_controller_index(0);
152 }
153
154 void SetGoal(double left, double left_velocity, double right,
155 double right_velocity) {
156 left_goal_ = left;
157 right_goal_ = right;
158 loop_->mutable_R() << left, left_velocity, right, right_velocity;
159 }
160 void SetRawPosition(double left, double right) {
161 raw_right_ = right;
162 raw_left_ = left;
163 Eigen::Matrix<double, 2, 1> Y;
164 Y << left + filtered_offset_, right - filtered_offset_;
165 loop_->Correct(Y);
166 }
167 void SetPosition(double left, double right, double gyro) {
168 // Decay the offset quickly because this gyro is great.
169 const double offset =
170 (right - left - gyro * kBot3TurnWidth) / 2.0;
171 // TODO(brians): filtered_offset_ = offset first time around.
172 filtered_offset_ = 0.25 * offset + 0.75 * filtered_offset_;
173 gyro_ = gyro;
174 SetRawPosition(left, right);
175 }
176
177 void SetExternalMotors(double left_voltage, double right_voltage) {
178 loop_->mutable_U() << left_voltage, right_voltage;
179 }
180
181 void Update(bool stop_motors, bool enable_control_loop) {
182 if (enable_control_loop) {
183 loop_->Update(stop_motors);
184 } else {
185 if (stop_motors) {
186 loop_->mutable_U().setZero();
187 loop_->mutable_U_uncapped().setZero();
188 }
189 loop_->UpdateObserver();
190 }
191 ::Eigen::Matrix<double, 4, 1> E = loop_->R() - loop_->X_hat();
192 LOG_MATRIX(DEBUG, "E", E);
193 }
194
195 double GetEstimatedRobotSpeed() const {
196 // lets just call the average of left and right velocities close enough
197 return (loop_->X_hat(1, 0) + loop_->X_hat(3, 0)) / 2;
198 }
199
200 double GetEstimatedLeftEncoder() const {
201 return loop_->X_hat(0, 0);
202 }
203
204 double GetEstimatedRightEncoder() const {
205 return loop_->X_hat(2, 0);
206 }
207
208 bool OutputWasCapped() const {
209 return loop_->output_was_capped();
210 }
211
212 void SendMotors(Drivetrain::Output *output) const {
213 if (output) {
214 output->left_voltage = loop_->U(0, 0);
215 output->right_voltage = loop_->U(1, 0);
216 output->left_high = false;
217 output->right_high = false;
218 }
219 }
220
221 const LimitedDrivetrainLoop &loop() const { return *loop_; }
222
223 private:
224 ::std::unique_ptr<LimitedDrivetrainLoop> loop_;
225
226 double filtered_offset_;
227 double gyro_;
228 double left_goal_;
229 double right_goal_;
230 double raw_left_;
231 double raw_right_;
232};
233
234class PolyDrivetrain {
235 public:
236
237 enum Gear {
238 HIGH,
239 LOW,
240 SHIFTING_UP,
241 SHIFTING_DOWN
242 };
243 // Stall Torque in N m
244 static constexpr double kStallTorque = 2.42;
245 // Stall Current in Amps
246 static constexpr double kStallCurrent = 133;
247 // Free Speed in RPM. Used number from last year.
248 static constexpr double kFreeSpeed = 4650.0;
249 // Free Current in Amps
250 static constexpr double kFreeCurrent = 2.7;
251 // Moment of inertia of the drivetrain in kg m^2
252 // Just borrowed from last year.
253 static constexpr double J = 6.4;
254 // Mass of the robot, in kg.
255 static constexpr double m = 68;
256 // Radius of the robot, in meters (from last year).
257 static constexpr double rb = 0.617998644 / 2.0;
258 static constexpr double kWheelRadius = 0.04445;
259 // Resistance of the motor, divided by the number of motors.
260 static constexpr double kR = (12.0 / kStallCurrent / 4 + 0.03) / (0.93 * 0.93);
261 // Motor velocity constant
262 static constexpr double Kv =
263 ((kFreeSpeed / 60.0 * 2.0 * M_PI) / (12.0 - kR * kFreeCurrent));
264 // Torque constant
265 static constexpr double Kt = kStallTorque / kStallCurrent;
266
267 PolyDrivetrain()
268 : U_Poly_((Eigen::Matrix<double, 4, 2>() << /*[[*/ 1, 0 /*]*/,
269 /*[*/ -1, 0 /*]*/,
270 /*[*/ 0, 1 /*]*/,
271 /*[*/ 0, -1 /*]]*/).finished(),
272 (Eigen::Matrix<double, 4, 1>() << /*[[*/ 12 /*]*/,
273 /*[*/ 12 /*]*/,
274 /*[*/ 12 /*]*/,
275 /*[*/ 12 /*]]*/).finished()),
276 loop_(new StateFeedbackLoop<2, 2, 2>(
277 MakeVelocityDrivetrainLoop())),
278 ttrust_(1.1),
279 wheel_(0.0),
280 throttle_(0.0),
281 quickturn_(false),
282 stale_count_(0),
283 position_time_delta_(0.01),
284 left_gear_(LOW),
285 right_gear_(LOW),
286 counter_(0) {
287
288 last_position_.Zero();
289 position_.Zero();
290 }
291 static bool IsInGear(Gear gear) { return gear == LOW || gear == HIGH; }
292
293 static double MotorSpeed(const ::frc971::constants::ShifterHallEffect &hall_effect,
294 double shifter_position, double velocity) {
295 // TODO(austin): G_high, G_low and kWheelRadius
296 const double avg_hall_effect =
297 (hall_effect.clear_high + hall_effect.clear_low) / 2.0;
298
299 if (shifter_position > avg_hall_effect) {
300 return velocity / kBot3HighGearRatio / kWheelRadius;
301 } else {
302 return velocity / kBot3LowGearRatio / kWheelRadius;
303 }
304 }
305
306 Gear ComputeGear(const ::frc971::constants::ShifterHallEffect &hall_effect,
307 double velocity, Gear current) {
308 const double low_omega = MotorSpeed(hall_effect, 0.0, ::std::abs(velocity));
309 const double high_omega =
310 MotorSpeed(hall_effect, 1.0, ::std::abs(velocity));
311
312 double high_torque = ((12.0 - high_omega / Kv) * Kt / kR);
313 double low_torque = ((12.0 - low_omega / Kv) * Kt / kR);
314 double high_power = high_torque * high_omega;
315 double low_power = low_torque * low_omega;
316
317 // TODO(aschuh): Do this right!
318 if ((current == HIGH || high_power > low_power + 160) &&
319 ::std::abs(velocity) > 0.14) {
320 return HIGH;
321 } else {
322 return LOW;
323 }
324 }
325
326 void SetGoal(double wheel, double throttle, bool quickturn, bool highgear) {
327 const double kWheelNonLinearity = 0.3;
328 // Apply a sin function that's scaled to make it feel better.
329 const double angular_range = M_PI_2 * kWheelNonLinearity;
330 wheel_ = sin(angular_range * wheel) / sin(angular_range);
331 wheel_ = sin(angular_range * wheel_) / sin(angular_range);
332 quickturn_ = quickturn;
333
334 static const double kThrottleDeadband = 0.05;
335 if (::std::abs(throttle) < kThrottleDeadband) {
336 throttle_ = 0;
337 } else {
338 throttle_ = copysign((::std::abs(throttle) - kThrottleDeadband) /
339 (1.0 - kThrottleDeadband), throttle);
340 }
341
342 // TODO(austin): Fix the upshift logic to include states.
343 Gear requested_gear;
344 if (false) {
345 const double current_left_velocity =
346 (position_.left_encoder - last_position_.left_encoder) /
347 position_time_delta_;
348 const double current_right_velocity =
349 (position_.right_encoder - last_position_.right_encoder) /
350 position_time_delta_;
351
352 Gear left_requested =
353 ComputeGear(kBot3LeftDriveShifter, current_left_velocity, left_gear_);
354 Gear right_requested =
355 ComputeGear(kBot3RightDriveShifter, current_right_velocity, right_gear_);
356 requested_gear =
357 (left_requested == HIGH || right_requested == HIGH) ? HIGH : LOW;
358 } else {
359 requested_gear = highgear ? HIGH : LOW;
360 }
361
362 const Gear shift_up = SHIFTING_UP;
363 const Gear shift_down = SHIFTING_DOWN;
364
365 if (left_gear_ != requested_gear) {
366 if (IsInGear(left_gear_)) {
367 if (requested_gear == HIGH) {
368 left_gear_ = shift_up;
369 } else {
370 left_gear_ = shift_down;
371 }
372 } else {
373 if (requested_gear == HIGH && left_gear_ == SHIFTING_DOWN) {
374 left_gear_ = SHIFTING_UP;
375 } else if (requested_gear == LOW && left_gear_ == SHIFTING_UP) {
376 left_gear_ = SHIFTING_DOWN;
377 }
378 }
379 }
380 if (right_gear_ != requested_gear) {
381 if (IsInGear(right_gear_)) {
382 if (requested_gear == HIGH) {
383 right_gear_ = shift_up;
384 } else {
385 right_gear_ = shift_down;
386 }
387 } else {
388 if (requested_gear == HIGH && right_gear_ == SHIFTING_DOWN) {
389 right_gear_ = SHIFTING_UP;
390 } else if (requested_gear == LOW && right_gear_ == SHIFTING_UP) {
391 right_gear_ = SHIFTING_DOWN;
392 }
393 }
394 }
395 }
396 void SetPosition(const Drivetrain::Position *position) {
397 if (position == NULL) {
398 ++stale_count_;
399 } else {
400 last_position_ = position_;
401 position_ = *position;
402 position_time_delta_ = (stale_count_ + 1) * 0.01;
403 stale_count_ = 0;
404 }
405
406 if (position) {
407 GearLogging gear_logging;
408 // Switch to the correct controller.
409 const double left_middle_shifter_position =
410 (kBot3LeftDriveShifter.clear_high +
411 kBot3LeftDriveShifter.clear_low) / 2.0;
412 const double right_middle_shifter_position =
413 (kBot3RightDriveShifter.clear_high +
414 kBot3RightDriveShifter.clear_low) / 2.0;
415
416 if (position->left_shifter_position < left_middle_shifter_position) {
417 if (position->right_shifter_position < right_middle_shifter_position ||
418 right_gear_ == LOW) {
419 gear_logging.left_loop_high = false;
420 gear_logging.right_loop_high = false;
421 loop_->set_controller_index(gear_logging.controller_index = 0);
422 } else {
423 gear_logging.left_loop_high = false;
424 gear_logging.right_loop_high = true;
425 loop_->set_controller_index(gear_logging.controller_index = 1);
426 }
427 } else {
428 if (position->right_shifter_position < right_middle_shifter_position ||
429 left_gear_ == LOW) {
430 gear_logging.left_loop_high = true;
431 gear_logging.right_loop_high = false;
432 loop_->set_controller_index(gear_logging.controller_index = 2);
433 } else {
434 gear_logging.left_loop_high = true;
435 gear_logging.right_loop_high = true;
436 loop_->set_controller_index(gear_logging.controller_index = 3);
437 }
438 }
439
440 // TODO(austin): Constants.
441 if (position->left_shifter_position >
442 kBot3LeftDriveShifter.clear_high &&
443 left_gear_ == SHIFTING_UP) {
444 left_gear_ = HIGH;
445 }
446 if (position->left_shifter_position <
447 kBot3LeftDriveShifter.clear_low &&
448 left_gear_ == SHIFTING_DOWN) {
449 left_gear_ = LOW;
450 }
451 if (position->right_shifter_position >
452 kBot3RightDriveShifter.clear_high &&
453 right_gear_ == SHIFTING_UP) {
454 right_gear_ = HIGH;
455 }
456 if (position->right_shifter_position <
457 kBot3RightDriveShifter.clear_low &&
458 right_gear_ == SHIFTING_DOWN) {
459 right_gear_ = LOW;
460 }
461
462 gear_logging.left_state = left_gear_;
463 gear_logging.right_state = right_gear_;
464 LOG_STRUCT(DEBUG, "state", gear_logging);
465 }
466 }
467
468 double FilterVelocity(double throttle) {
469 const Eigen::Matrix<double, 2, 2> FF =
470 loop_->B().inverse() *
471 (Eigen::Matrix<double, 2, 2>::Identity() - loop_->A());
472
473 constexpr int kHighGearController = 3;
474 const Eigen::Matrix<double, 2, 2> FF_high =
475 loop_->controller(kHighGearController).plant.B().inverse() *
476 (Eigen::Matrix<double, 2, 2>::Identity() -
477 loop_->controller(kHighGearController).plant.A());
478
479 ::Eigen::Matrix<double, 1, 2> FF_sum = FF.colwise().sum();
480 int min_FF_sum_index;
481 const double min_FF_sum = FF_sum.minCoeff(&min_FF_sum_index);
482 const double min_K_sum = loop_->K().col(min_FF_sum_index).sum();
483 const double high_min_FF_sum = FF_high.col(0).sum();
484
485 const double adjusted_ff_voltage = ::aos::Clip(
486 throttle * 12.0 * min_FF_sum / high_min_FF_sum, -12.0, 12.0);
487 return ((adjusted_ff_voltage +
488 ttrust_ * min_K_sum * (loop_->X_hat(0, 0) + loop_->X_hat(1, 0)) / 2.0) /
489 (ttrust_ * min_K_sum + min_FF_sum));
490 }
491
492 double MaxVelocity() {
493 const Eigen::Matrix<double, 2, 2> FF =
494 loop_->B().inverse() *
495 (Eigen::Matrix<double, 2, 2>::Identity() - loop_->A());
496
497 constexpr int kHighGearController = 3;
498 const Eigen::Matrix<double, 2, 2> FF_high =
499 loop_->controller(kHighGearController).plant.B().inverse() *
500 (Eigen::Matrix<double, 2, 2>::Identity() -
501 loop_->controller(kHighGearController).plant.A());
502
503 ::Eigen::Matrix<double, 1, 2> FF_sum = FF.colwise().sum();
504 int min_FF_sum_index;
505 const double min_FF_sum = FF_sum.minCoeff(&min_FF_sum_index);
506 //const double min_K_sum = loop_->K().col(min_FF_sum_index).sum();
507 const double high_min_FF_sum = FF_high.col(0).sum();
508
509 const double adjusted_ff_voltage = ::aos::Clip(
510 12.0 * min_FF_sum / high_min_FF_sum, -12.0, 12.0);
511 return adjusted_ff_voltage / min_FF_sum;
512 }
513
514 void Update() {
515 // TODO(austin): Observer for the current velocity instead of difference
516 // calculations.
517 ++counter_;
518 const double current_left_velocity =
519 (position_.left_encoder - last_position_.left_encoder) /
520 position_time_delta_;
521 const double current_right_velocity =
522 (position_.right_encoder - last_position_.right_encoder) /
523 position_time_delta_;
524 const double left_motor_speed =
525 MotorSpeed(kBot3LeftDriveShifter,
526 position_.left_shifter_position,
527 current_left_velocity);
528 const double right_motor_speed =
529 MotorSpeed(kBot3RightDriveShifter,
530 position_.right_shifter_position,
531 current_right_velocity);
532
533 {
534 CIMLogging logging;
535
536 // Reset the CIM model to the current conditions to be ready for when we
537 // shift.
538 if (IsInGear(left_gear_)) {
539 logging.left_in_gear = true;
540 } else {
541 logging.left_in_gear = false;
542 }
543 logging.left_motor_speed = left_motor_speed;
544 logging.left_velocity = current_left_velocity;
545 if (IsInGear(right_gear_)) {
546 logging.right_in_gear = true;
547 } else {
548 logging.right_in_gear = false;
549 }
550 logging.right_motor_speed = right_motor_speed;
551 logging.right_velocity = current_right_velocity;
552
553 LOG_STRUCT(DEBUG, "currently", logging);
554 }
555
556 if (IsInGear(left_gear_) && IsInGear(right_gear_)) {
557 // FF * X = U (steady state)
558 const Eigen::Matrix<double, 2, 2> FF =
559 loop_->B().inverse() *
560 (Eigen::Matrix<double, 2, 2>::Identity() - loop_->A());
561
562 // Invert the plant to figure out how the velocity filter would have to
563 // work
564 // out in order to filter out the forwards negative inertia.
565 // This math assumes that the left and right power and velocity are
566 // equals,
567 // and that the plant is the same on the left and right.
568 const double fvel = FilterVelocity(throttle_);
569
570 const double sign_svel = wheel_ * ((fvel > 0.0) ? 1.0 : -1.0);
571 double steering_velocity;
572 if (quickturn_) {
573 steering_velocity = wheel_ * MaxVelocity();
574 } else {
575 steering_velocity = ::std::abs(fvel) * wheel_;
576 }
577 const double left_velocity = fvel - steering_velocity;
578 const double right_velocity = fvel + steering_velocity;
579
580 // Integrate velocity to get the position.
581 // This position is used to get integral control.
582 loop_->mutable_R() << left_velocity, right_velocity;
583
584 if (!quickturn_) {
585 // K * R = w
586 Eigen::Matrix<double, 1, 2> equality_k;
587 equality_k << 1 + sign_svel, -(1 - sign_svel);
588 const double equality_w = 0.0;
589
590 // Construct a constraint on R by manipulating the constraint on U
591 ::aos::controls::HPolytope<2> R_poly = ::aos::controls::HPolytope<2>(
592 U_Poly_.H() * (loop_->K() + FF),
593 U_Poly_.k() + U_Poly_.H() * loop_->K() * loop_->X_hat());
594
595 // Limit R back inside the box.
596 loop_->mutable_R() =
597 CoerceGoal(R_poly, equality_k, equality_w, loop_->R());
598 }
599
600 const Eigen::Matrix<double, 2, 1> FF_volts = FF * loop_->R();
601 const Eigen::Matrix<double, 2, 1> U_ideal =
602 loop_->K() * (loop_->R() - loop_->X_hat()) + FF_volts;
603
604 for (int i = 0; i < 2; i++) {
605 loop_->mutable_U()[i] = ::aos::Clip(U_ideal[i], -12, 12);
606 }
607
608 // TODO(austin): Model this better.
609 // TODO(austin): Feed back?
610 loop_->mutable_X_hat() =
611 loop_->A() * loop_->X_hat() + loop_->B() * loop_->U();
612 } else {
613 // Any motor is not in gear. Speed match.
614 ::Eigen::Matrix<double, 1, 1> R_left;
615 ::Eigen::Matrix<double, 1, 1> R_right;
616 R_left(0, 0) = left_motor_speed;
617 R_right(0, 0) = right_motor_speed;
618
619 const double wiggle =
620 (static_cast<double>((counter_ % 20) / 10) - 0.5) * 5.0;
621
622 loop_->mutable_U(0, 0) = ::aos::Clip(
623 (R_left / Kv)(0, 0) + (IsInGear(left_gear_) ? 0 : wiggle),
624 -12.0, 12.0);
625 loop_->mutable_U(1, 0) = ::aos::Clip(
626 (R_right / Kv)(0, 0) + (IsInGear(right_gear_) ? 0 : wiggle),
627 -12.0, 12.0);
628 loop_->mutable_U() *= 12.0 / position_.battery_voltage;
629 }
630 }
631
632 void SendMotors(Drivetrain::Output *output) {
633 if (output != NULL) {
634 output->left_voltage = loop_->U(0, 0);
635 output->right_voltage = loop_->U(1, 0);
636 output->left_high = left_gear_ == HIGH || left_gear_ == SHIFTING_UP;
637 output->right_high = right_gear_ == HIGH || right_gear_ == SHIFTING_UP;
638 }
639 }
640
641 private:
642 const ::aos::controls::HPolytope<2> U_Poly_;
643
644 ::std::unique_ptr<StateFeedbackLoop<2, 2, 2>> loop_;
645
646 const double ttrust_;
647 double wheel_;
648 double throttle_;
649 bool quickturn_;
650 int stale_count_;
651 double position_time_delta_;
652 Gear left_gear_;
653 Gear right_gear_;
654 Drivetrain::Position last_position_;
655 Drivetrain::Position position_;
656 int counter_;
657};
658constexpr double PolyDrivetrain::kStallTorque;
659constexpr double PolyDrivetrain::kStallCurrent;
660constexpr double PolyDrivetrain::kFreeSpeed;
661constexpr double PolyDrivetrain::kFreeCurrent;
662constexpr double PolyDrivetrain::J;
663constexpr double PolyDrivetrain::m;
664constexpr double PolyDrivetrain::rb;
665constexpr double PolyDrivetrain::kWheelRadius;
666constexpr double PolyDrivetrain::kR;
667constexpr double PolyDrivetrain::Kv;
668constexpr double PolyDrivetrain::Kt;
669
670
671void DrivetrainLoop::RunIteration(const Drivetrain::Goal *goal,
672 const Drivetrain::Position *position,
673 Drivetrain::Output *output,
674 Drivetrain::Status * status) {
675 // TODO(aschuh): These should be members of the class.
676 static DrivetrainMotorsSS dt_closedloop;
677 static PolyDrivetrain dt_openloop;
678
679 bool bad_pos = false;
680 if (position == nullptr) {
681 LOG_INTERVAL(no_position_);
682 bad_pos = true;
683 }
684 no_position_.Print();
685
686 double wheel = goal->steering;
687 double throttle = goal->throttle;
688 bool quickturn = goal->quickturn;
689 bool highgear = goal->highgear;
690
691 bool control_loop_driving = goal->control_loop_driving;
692 double left_goal = goal->left_goal;
693 double right_goal = goal->right_goal;
694
695 dt_closedloop.SetGoal(left_goal, goal->left_velocity_goal, right_goal,
696 goal->right_velocity_goal);
697 if (!bad_pos) {
698 const double left_encoder = position->left_encoder;
699 const double right_encoder = position->right_encoder;
700 if (gyro_reading.FetchLatest()) {
701 LOG_STRUCT(DEBUG, "using", *gyro_reading.get());
702 dt_closedloop.SetPosition(left_encoder, right_encoder,
703 gyro_reading->angle);
704 } else {
705 dt_closedloop.SetRawPosition(left_encoder, right_encoder);
706 }
707 }
708 dt_openloop.SetPosition(position);
709 dt_openloop.SetGoal(wheel, throttle, quickturn, highgear);
710 dt_openloop.Update();
711
712 if (control_loop_driving) {
713 dt_closedloop.Update(output == NULL, true);
714 dt_closedloop.SendMotors(output);
715 } else {
716 dt_openloop.SendMotors(output);
717 if (output) {
718 dt_closedloop.SetExternalMotors(output->left_voltage,
719 output->right_voltage);
720 }
721 dt_closedloop.Update(output == NULL, false);
722 }
723
724 // set the output status of the control loop state
725 if (status) {
726 bool done = false;
727 if (goal) {
728 done = ((::std::abs(goal->left_goal -
729 dt_closedloop.GetEstimatedLeftEncoder()) <
730 kBot3DrivetrainDoneDistance) &&
731 (::std::abs(goal->right_goal -
732 dt_closedloop.GetEstimatedRightEncoder()) <
733 kBot3DrivetrainDoneDistance));
734 }
735 status->is_done = done;
736 status->robot_speed = dt_closedloop.GetEstimatedRobotSpeed();
737 status->filtered_left_position = dt_closedloop.GetEstimatedLeftEncoder();
738 status->filtered_right_position = dt_closedloop.GetEstimatedRightEncoder();
739 status->output_was_capped = dt_closedloop.OutputWasCapped();
740 status->uncapped_left_voltage = dt_closedloop.loop().U_uncapped(0, 0);
741 status->uncapped_right_voltage = dt_closedloop.loop().U_uncapped(1, 0);
742 }
743}
744
745} // namespace control_loops
746} // namespace bot3