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