blob: 8f261613f0fdbdd92b68c2aecf5aef7afa0be3f5 [file] [log] [blame]
Comran Morshed41ed7c22015-11-04 21:03:37 +00001#include "y2014_bot3/control_loops/drivetrain/drivetrain.h"
Comran Morshede9b12922015-11-04 19:46:48 +00002
Comran Morshede9b12922015-11-04 19:46:48 +00003#include <stdio.h>
4#include <sched.h>
Comran Morshed41ed7c22015-11-04 21:03:37 +00005#include <cmath>
Comran Morshede9b12922015-11-04 19:46:48 +00006#include <memory>
Comran Morshede9b12922015-11-04 19:46:48 +00007#include "Eigen/Dense"
8
9#include "aos/common/logging/logging.h"
10#include "aos/common/controls/polytope.h"
11#include "aos/common/commonmath.h"
12#include "aos/common/logging/queue_logging.h"
13#include "aos/common/logging/matrix_logging.h"
14
Comran Morshede9b12922015-11-04 19:46:48 +000015#include "frc971/control_loops/state_feedback_loop.h"
Comran Morshed41ed7c22015-11-04 21:03:37 +000016#include "frc971/control_loops/coerce_goal.h"
17#include "y2014_bot3/control_loops/drivetrain/polydrivetrain_cim_plant.h"
18#include "y2014_bot3/control_loops/drivetrain/drivetrain.q.h"
19#include "frc971/queues/gyro.q.h"
20#include "frc971/shifter_hall_effect.h"
21#include "y2014_bot3/control_loops/drivetrain/drivetrain_dog_motor_plant.h"
22#include "y2014_bot3/control_loops/drivetrain/polydrivetrain_dog_motor_plant.h"
Comran Morshede9b12922015-11-04 19:46:48 +000023
24using ::frc971::sensors::gyro_reading;
Comran Morshede9b12922015-11-04 19:46:48 +000025
Comran Morshed41ed7c22015-11-04 21:03:37 +000026namespace y2014_bot3 {
Comran Morshede9b12922015-11-04 19:46:48 +000027namespace control_loops {
28
29class DrivetrainMotorsSS {
30 public:
31 class LimitedDrivetrainLoop : public StateFeedbackLoop<4, 2, 2> {
32 public:
33 LimitedDrivetrainLoop(StateFeedbackLoop<4, 2, 2> &&loop)
34 : StateFeedbackLoop<4, 2, 2>(::std::move(loop)),
Comran Morshed41ed7c22015-11-04 21:03:37 +000035 U_Poly_((Eigen::Matrix<double, 4, 2>() << 1, 0, -1, 0, 0, 1, 0, -1)
36 .finished(),
37 (Eigen::Matrix<double, 4, 1>() << 12.0, 12.0, 12.0, 12.0)
38 .finished()) {
Comran Morshede9b12922015-11-04 19:46:48 +000039 ::aos::controls::HPolytope<0>::Init();
40 T << 1, -1, 1, 1;
41 T_inverse = T.inverse();
42 }
43
Comran Morshed41ed7c22015-11-04 21:03:37 +000044 bool output_was_capped() const { return output_was_capped_; }
Comran Morshede9b12922015-11-04 19:46:48 +000045
46 private:
47 virtual void CapU() {
48 const Eigen::Matrix<double, 4, 1> error = R() - X_hat();
49
50 if (::std::abs(U(0, 0)) > 12.0 || ::std::abs(U(1, 0)) > 12.0) {
Comran Morshed41ed7c22015-11-04 21:03:37 +000051 mutable_U() =
52 U() * 12.0 / ::std::max(::std::abs(U(0, 0)), ::std::abs(U(1, 0)));
53 LOG_MATRIX(DEBUG, "U is now", U());
54 // TODO(Austin): Figure out why the polytope stuff wasn't working and
55 // remove this hack.
Comran Morshede9b12922015-11-04 19:46:48 +000056 output_was_capped_ = true;
Comran Morshed41ed7c22015-11-04 21:03:37 +000057 return;
58
Comran Morshede9b12922015-11-04 19:46:48 +000059 LOG_MATRIX(DEBUG, "U at start", U());
Comran Morshed41ed7c22015-11-04 21:03:37 +000060 LOG_MATRIX(DEBUG, "R at start", R());
61 LOG_MATRIX(DEBUG, "Xhat at start", X_hat());
Comran Morshede9b12922015-11-04 19:46:48 +000062
63 Eigen::Matrix<double, 2, 2> position_K;
Comran Morshed41ed7c22015-11-04 21:03:37 +000064 position_K << K(0, 0), K(0, 2), K(1, 0), K(1, 2);
Comran Morshede9b12922015-11-04 19:46:48 +000065 Eigen::Matrix<double, 2, 2> velocity_K;
Comran Morshed41ed7c22015-11-04 21:03:37 +000066 velocity_K << K(0, 1), K(0, 3), K(1, 1), K(1, 3);
Comran Morshede9b12922015-11-04 19:46:48 +000067
68 Eigen::Matrix<double, 2, 1> position_error;
69 position_error << error(0, 0), error(2, 0);
70 const auto drive_error = T_inverse * position_error;
71 Eigen::Matrix<double, 2, 1> velocity_error;
72 velocity_error << error(1, 0), error(3, 0);
73 LOG_MATRIX(DEBUG, "error", error);
74
75 const auto &poly = U_Poly_;
76 const Eigen::Matrix<double, 4, 2> pos_poly_H =
77 poly.H() * position_K * T;
78 const Eigen::Matrix<double, 4, 1> pos_poly_k =
79 poly.k() - poly.H() * velocity_K * velocity_error;
80 const ::aos::controls::HPolytope<2> pos_poly(pos_poly_H, pos_poly_k);
81
82 Eigen::Matrix<double, 2, 1> adjusted_pos_error;
83 {
84 const auto &P = drive_error;
85
86 Eigen::Matrix<double, 1, 2> L45;
87 L45 << ::aos::sign(P(1, 0)), -::aos::sign(P(0, 0));
88 const double w45 = 0;
89
90 Eigen::Matrix<double, 1, 2> LH;
91 if (::std::abs(P(0, 0)) > ::std::abs(P(1, 0))) {
92 LH << 0, 1;
93 } else {
94 LH << 1, 0;
95 }
96 const double wh = LH.dot(P);
97
98 Eigen::Matrix<double, 2, 2> standard;
99 standard << L45, LH;
100 Eigen::Matrix<double, 2, 1> W;
101 W << w45, wh;
102 const Eigen::Matrix<double, 2, 1> intersection =
103 standard.inverse() * W;
104
105 bool is_inside_h;
Comran Morshed41ed7c22015-11-04 21:03:37 +0000106 const auto adjusted_pos_error_h = frc971::control_loops::DoCoerceGoal(
107 pos_poly, LH, wh, drive_error, &is_inside_h);
Comran Morshede9b12922015-11-04 19:46:48 +0000108 const auto adjusted_pos_error_45 =
Comran Morshed41ed7c22015-11-04 21:03:37 +0000109 frc971::control_loops::DoCoerceGoal(pos_poly, L45, w45,
110 intersection, nullptr);
Comran Morshede9b12922015-11-04 19:46:48 +0000111 if (pos_poly.IsInside(intersection)) {
112 adjusted_pos_error = adjusted_pos_error_h;
113 } else {
114 if (is_inside_h) {
115 if (adjusted_pos_error_h.norm() > adjusted_pos_error_45.norm()) {
116 adjusted_pos_error = adjusted_pos_error_h;
117 } else {
118 adjusted_pos_error = adjusted_pos_error_45;
119 }
120 } else {
121 adjusted_pos_error = adjusted_pos_error_45;
122 }
123 }
124 }
125
126 LOG_MATRIX(DEBUG, "adjusted_pos_error", adjusted_pos_error);
127 mutable_U() =
128 velocity_K * velocity_error + position_K * T * adjusted_pos_error;
129 LOG_MATRIX(DEBUG, "U is now", U());
130 } else {
131 output_was_capped_ = false;
132 }
133 }
134
135 const ::aos::controls::HPolytope<2> U_Poly_;
136 Eigen::Matrix<double, 2, 2> T, T_inverse;
Comran Morshed41ed7c22015-11-04 21:03:37 +0000137 bool output_was_capped_ = false;
Comran Morshede9b12922015-11-04 19:46:48 +0000138 };
139
140 DrivetrainMotorsSS()
141 : loop_(new LimitedDrivetrainLoop(
Comran Morshed41ed7c22015-11-04 21:03:37 +0000142 ::y2014_bot3::control_loops::MakeDrivetrainLoop())),
Comran Morshede9b12922015-11-04 19:46:48 +0000143 filtered_offset_(0.0),
144 gyro_(0.0),
145 left_goal_(0.0),
146 right_goal_(0.0),
147 raw_left_(0.0),
148 raw_right_(0.0) {
149 // Low gear on both.
150 loop_->set_controller_index(0);
151 }
152
153 void SetGoal(double left, double left_velocity, double right,
154 double right_velocity) {
155 left_goal_ = left;
156 right_goal_ = right;
157 loop_->mutable_R() << left, left_velocity, right, right_velocity;
158 }
159 void SetRawPosition(double left, double right) {
160 raw_right_ = right;
161 raw_left_ = left;
162 Eigen::Matrix<double, 2, 1> Y;
163 Y << left + filtered_offset_, right - filtered_offset_;
164 loop_->Correct(Y);
165 }
166 void SetPosition(double left, double right, double gyro) {
167 // Decay the offset quickly because this gyro is great.
Comran Morshed41ed7c22015-11-04 21:03:37 +0000168 const double offset = (right - left - gyro * kDrivetrainTurnWidth) / 2.0;
Comran Morshede9b12922015-11-04 19:46:48 +0000169 filtered_offset_ = 0.25 * offset + 0.75 * filtered_offset_;
170 gyro_ = gyro;
171 SetRawPosition(left, right);
172 }
173
174 void SetExternalMotors(double left_voltage, double right_voltage) {
175 loop_->mutable_U() << left_voltage, right_voltage;
176 }
177
178 void Update(bool stop_motors, bool enable_control_loop) {
179 if (enable_control_loop) {
180 loop_->Update(stop_motors);
181 } else {
182 if (stop_motors) {
183 loop_->mutable_U().setZero();
184 loop_->mutable_U_uncapped().setZero();
185 }
Austin Schuhc2b77742015-11-26 16:18:27 -0800186 loop_->UpdateObserver(loop_->U());
Comran Morshede9b12922015-11-04 19:46:48 +0000187 }
188 ::Eigen::Matrix<double, 4, 1> E = loop_->R() - loop_->X_hat();
189 LOG_MATRIX(DEBUG, "E", E);
190 }
191
192 double GetEstimatedRobotSpeed() const {
193 // lets just call the average of left and right velocities close enough
194 return (loop_->X_hat(1, 0) + loop_->X_hat(3, 0)) / 2;
195 }
196
Comran Morshed41ed7c22015-11-04 21:03:37 +0000197 double GetEstimatedLeftEncoder() const { return loop_->X_hat(0, 0); }
Comran Morshede9b12922015-11-04 19:46:48 +0000198
Comran Morshed41ed7c22015-11-04 21:03:37 +0000199 double GetEstimatedRightEncoder() const { return loop_->X_hat(2, 0); }
Comran Morshede9b12922015-11-04 19:46:48 +0000200
Comran Morshed41ed7c22015-11-04 21:03:37 +0000201 bool OutputWasCapped() const { return loop_->output_was_capped(); }
Comran Morshede9b12922015-11-04 19:46:48 +0000202
Comran Morshed41ed7c22015-11-04 21:03:37 +0000203 void SendMotors(DrivetrainQueue::Output *output) const {
Comran Morshede9b12922015-11-04 19:46:48 +0000204 if (output) {
205 output->left_voltage = loop_->U(0, 0);
206 output->right_voltage = loop_->U(1, 0);
207 output->left_high = false;
208 output->right_high = false;
209 }
210 }
211
212 const LimitedDrivetrainLoop &loop() const { return *loop_; }
213
214 private:
215 ::std::unique_ptr<LimitedDrivetrainLoop> loop_;
216
217 double filtered_offset_;
218 double gyro_;
219 double left_goal_;
220 double right_goal_;
221 double raw_left_;
222 double raw_right_;
223};
224
225class PolyDrivetrain {
226 public:
Comran Morshed41ed7c22015-11-04 21:03:37 +0000227 enum Gear { HIGH, LOW };
Comran Morshede9b12922015-11-04 19:46:48 +0000228 // Stall Torque in N m
229 static constexpr double kStallTorque = 2.42;
230 // Stall Current in Amps
Comran Morshed41ed7c22015-11-04 21:03:37 +0000231 static constexpr double kStallCurrent = 133.0;
Comran Morshede9b12922015-11-04 19:46:48 +0000232 // Free Speed in RPM. Used number from last year.
233 static constexpr double kFreeSpeed = 4650.0;
234 // Free Current in Amps
235 static constexpr double kFreeCurrent = 2.7;
236 // Moment of inertia of the drivetrain in kg m^2
237 // Just borrowed from last year.
Comran Morshed41ed7c22015-11-04 21:03:37 +0000238 static constexpr double J = 10;
Comran Morshede9b12922015-11-04 19:46:48 +0000239 // Mass of the robot, in kg.
240 static constexpr double m = 68;
241 // Radius of the robot, in meters (from last year).
Comran Morshed41ed7c22015-11-04 21:03:37 +0000242 static constexpr double rb = 0.66675 / 2.0;
Comran Morshede9b12922015-11-04 19:46:48 +0000243 static constexpr double kWheelRadius = 0.04445;
244 // Resistance of the motor, divided by the number of motors.
Comran Morshed41ed7c22015-11-04 21:03:37 +0000245 static constexpr double kR =
246 (12.0 / kStallCurrent / 4 + 0.03) / (0.93 * 0.93);
Comran Morshede9b12922015-11-04 19:46:48 +0000247 // Motor velocity constant
248 static constexpr double Kv =
249 ((kFreeSpeed / 60.0 * 2.0 * M_PI) / (12.0 - kR * kFreeCurrent));
250 // Torque constant
251 static constexpr double Kt = kStallTorque / kStallCurrent;
252
253 PolyDrivetrain()
254 : U_Poly_((Eigen::Matrix<double, 4, 2>() << /*[[*/ 1, 0 /*]*/,
255 /*[*/ -1, 0 /*]*/,
256 /*[*/ 0, 1 /*]*/,
257 /*[*/ 0, -1 /*]]*/).finished(),
258 (Eigen::Matrix<double, 4, 1>() << /*[[*/ 12 /*]*/,
259 /*[*/ 12 /*]*/,
260 /*[*/ 12 /*]*/,
261 /*[*/ 12 /*]]*/).finished()),
262 loop_(new StateFeedbackLoop<2, 2, 2>(
Comran Morshed41ed7c22015-11-04 21:03:37 +0000263 ::y2014_bot3::control_loops::MakeVelocityDrivetrainLoop())),
Comran Morshede9b12922015-11-04 19:46:48 +0000264 ttrust_(1.1),
265 wheel_(0.0),
266 throttle_(0.0),
267 quickturn_(false),
268 stale_count_(0),
269 position_time_delta_(0.01),
270 left_gear_(LOW),
271 right_gear_(LOW),
272 counter_(0) {
Comran Morshede9b12922015-11-04 19:46:48 +0000273 last_position_.Zero();
274 position_.Zero();
275 }
Comran Morshede9b12922015-11-04 19:46:48 +0000276
Comran Morshed41ed7c22015-11-04 21:03:37 +0000277 static double MotorSpeed(bool high_gear, double velocity) {
278 if (high_gear) {
279 return velocity / kDrivetrainHighGearRatio / kWheelRadius;
Comran Morshede9b12922015-11-04 19:46:48 +0000280 } else {
Comran Morshed41ed7c22015-11-04 21:03:37 +0000281 return velocity / kDrivetrainLowGearRatio / kWheelRadius;
Comran Morshede9b12922015-11-04 19:46:48 +0000282 }
283 }
284
285 void SetGoal(double wheel, double throttle, bool quickturn, bool highgear) {
Comran Morshed41ed7c22015-11-04 21:03:37 +0000286 const double kWheelNonLinearity = 0.5;
Comran Morshede9b12922015-11-04 19:46:48 +0000287 // Apply a sin function that's scaled to make it feel better.
288 const double angular_range = M_PI_2 * kWheelNonLinearity;
Comran Morshed41ed7c22015-11-04 21:03:37 +0000289
290 quickturn_ = quickturn;
Comran Morshede9b12922015-11-04 19:46:48 +0000291 wheel_ = sin(angular_range * wheel) / sin(angular_range);
292 wheel_ = sin(angular_range * wheel_) / sin(angular_range);
Comran Morshed41ed7c22015-11-04 21:03:37 +0000293 if (!quickturn_) {
294 wheel_ *= 1.5;
295 }
Comran Morshede9b12922015-11-04 19:46:48 +0000296
297 static const double kThrottleDeadband = 0.05;
298 if (::std::abs(throttle) < kThrottleDeadband) {
299 throttle_ = 0;
300 } else {
301 throttle_ = copysign((::std::abs(throttle) - kThrottleDeadband) /
Comran Morshed41ed7c22015-11-04 21:03:37 +0000302 (1.0 - kThrottleDeadband),
303 throttle);
Comran Morshede9b12922015-11-04 19:46:48 +0000304 }
305
Comran Morshed41ed7c22015-11-04 21:03:37 +0000306 Gear requested_gear = highgear ? HIGH : LOW;
307 left_gear_ = right_gear_ = requested_gear;
Comran Morshede9b12922015-11-04 19:46:48 +0000308 }
309
Comran Morshed41ed7c22015-11-04 21:03:37 +0000310 void SetPosition(const DrivetrainQueue::Position *position) {
Comran Morshede9b12922015-11-04 19:46:48 +0000311 if (position == NULL) {
312 ++stale_count_;
313 } else {
314 last_position_ = position_;
315 position_ = *position;
316 position_time_delta_ = (stale_count_ + 1) * 0.01;
317 stale_count_ = 0;
318 }
Comran Morshede9b12922015-11-04 19:46:48 +0000319 }
320
321 double FilterVelocity(double throttle) {
322 const Eigen::Matrix<double, 2, 2> FF =
323 loop_->B().inverse() *
324 (Eigen::Matrix<double, 2, 2>::Identity() - loop_->A());
325
326 constexpr int kHighGearController = 3;
327 const Eigen::Matrix<double, 2, 2> FF_high =
328 loop_->controller(kHighGearController).plant.B().inverse() *
329 (Eigen::Matrix<double, 2, 2>::Identity() -
330 loop_->controller(kHighGearController).plant.A());
331
332 ::Eigen::Matrix<double, 1, 2> FF_sum = FF.colwise().sum();
333 int min_FF_sum_index;
334 const double min_FF_sum = FF_sum.minCoeff(&min_FF_sum_index);
335 const double min_K_sum = loop_->K().col(min_FF_sum_index).sum();
336 const double high_min_FF_sum = FF_high.col(0).sum();
337
338 const double adjusted_ff_voltage = ::aos::Clip(
339 throttle * 12.0 * min_FF_sum / high_min_FF_sum, -12.0, 12.0);
Comran Morshed41ed7c22015-11-04 21:03:37 +0000340 return (adjusted_ff_voltage +
341 ttrust_ * min_K_sum * (loop_->X_hat(0, 0) + loop_->X_hat(1, 0)) /
342 2.0) /
343 (ttrust_ * min_K_sum + min_FF_sum);
Comran Morshede9b12922015-11-04 19:46:48 +0000344 }
345
346 double MaxVelocity() {
347 const Eigen::Matrix<double, 2, 2> FF =
348 loop_->B().inverse() *
349 (Eigen::Matrix<double, 2, 2>::Identity() - loop_->A());
350
351 constexpr int kHighGearController = 3;
352 const Eigen::Matrix<double, 2, 2> FF_high =
353 loop_->controller(kHighGearController).plant.B().inverse() *
354 (Eigen::Matrix<double, 2, 2>::Identity() -
355 loop_->controller(kHighGearController).plant.A());
356
357 ::Eigen::Matrix<double, 1, 2> FF_sum = FF.colwise().sum();
358 int min_FF_sum_index;
359 const double min_FF_sum = FF_sum.minCoeff(&min_FF_sum_index);
Comran Morshed41ed7c22015-11-04 21:03:37 +0000360 // const double min_K_sum = loop_->K().col(min_FF_sum_index).sum();
Comran Morshede9b12922015-11-04 19:46:48 +0000361 const double high_min_FF_sum = FF_high.col(0).sum();
362
Comran Morshed41ed7c22015-11-04 21:03:37 +0000363 const double adjusted_ff_voltage =
364 ::aos::Clip(12.0 * min_FF_sum / high_min_FF_sum, -12.0, 12.0);
Comran Morshede9b12922015-11-04 19:46:48 +0000365 return adjusted_ff_voltage / min_FF_sum;
366 }
367
368 void Update() {
369 // TODO(austin): Observer for the current velocity instead of difference
370 // calculations.
371 ++counter_;
Comran Morshed41ed7c22015-11-04 21:03:37 +0000372
Comran Morshede9b12922015-11-04 19:46:48 +0000373 const double current_left_velocity =
374 (position_.left_encoder - last_position_.left_encoder) /
375 position_time_delta_;
376 const double current_right_velocity =
377 (position_.right_encoder - last_position_.right_encoder) /
378 position_time_delta_;
Comran Morshed41ed7c22015-11-04 21:03:37 +0000379 const double left_motor_speed = MotorSpeed(left_gear_ == HIGH,
380 current_left_velocity);
381 const double right_motor_speed = MotorSpeed(right_gear_ == HIGH,
382 current_right_velocity);
Comran Morshede9b12922015-11-04 19:46:48 +0000383
384 {
385 CIMLogging logging;
386
387 // Reset the CIM model to the current conditions to be ready for when we
388 // shift.
Comran Morshed41ed7c22015-11-04 21:03:37 +0000389 logging.left_in_gear = true;
Comran Morshede9b12922015-11-04 19:46:48 +0000390 logging.left_motor_speed = left_motor_speed;
391 logging.left_velocity = current_left_velocity;
Comran Morshed41ed7c22015-11-04 21:03:37 +0000392
393 logging.right_in_gear = true;
Comran Morshede9b12922015-11-04 19:46:48 +0000394 logging.right_motor_speed = right_motor_speed;
395 logging.right_velocity = current_right_velocity;
396
397 LOG_STRUCT(DEBUG, "currently", logging);
398 }
399
Comran Morshed41ed7c22015-11-04 21:03:37 +0000400 {
Comran Morshede9b12922015-11-04 19:46:48 +0000401 // FF * X = U (steady state)
402 const Eigen::Matrix<double, 2, 2> FF =
403 loop_->B().inverse() *
404 (Eigen::Matrix<double, 2, 2>::Identity() - loop_->A());
405
406 // Invert the plant to figure out how the velocity filter would have to
407 // work
408 // out in order to filter out the forwards negative inertia.
409 // This math assumes that the left and right power and velocity are
410 // equals,
411 // and that the plant is the same on the left and right.
412 const double fvel = FilterVelocity(throttle_);
413
414 const double sign_svel = wheel_ * ((fvel > 0.0) ? 1.0 : -1.0);
415 double steering_velocity;
416 if (quickturn_) {
417 steering_velocity = wheel_ * MaxVelocity();
418 } else {
419 steering_velocity = ::std::abs(fvel) * wheel_;
420 }
421 const double left_velocity = fvel - steering_velocity;
422 const double right_velocity = fvel + steering_velocity;
Comran Morshed41ed7c22015-11-04 21:03:37 +0000423 LOG(DEBUG, "l=%f r=%f\n", left_velocity, right_velocity);
Comran Morshede9b12922015-11-04 19:46:48 +0000424
425 // Integrate velocity to get the position.
426 // This position is used to get integral control.
427 loop_->mutable_R() << left_velocity, right_velocity;
428
429 if (!quickturn_) {
430 // K * R = w
431 Eigen::Matrix<double, 1, 2> equality_k;
432 equality_k << 1 + sign_svel, -(1 - sign_svel);
433 const double equality_w = 0.0;
434
435 // Construct a constraint on R by manipulating the constraint on U
436 ::aos::controls::HPolytope<2> R_poly = ::aos::controls::HPolytope<2>(
437 U_Poly_.H() * (loop_->K() + FF),
438 U_Poly_.k() + U_Poly_.H() * loop_->K() * loop_->X_hat());
439
440 // Limit R back inside the box.
Comran Morshed41ed7c22015-11-04 21:03:37 +0000441 loop_->mutable_R() = frc971::control_loops::CoerceGoal(
442 R_poly, equality_k, equality_w, loop_->R());
Comran Morshede9b12922015-11-04 19:46:48 +0000443 }
444
445 const Eigen::Matrix<double, 2, 1> FF_volts = FF * loop_->R();
446 const Eigen::Matrix<double, 2, 1> U_ideal =
447 loop_->K() * (loop_->R() - loop_->X_hat()) + FF_volts;
448
449 for (int i = 0; i < 2; i++) {
450 loop_->mutable_U()[i] = ::aos::Clip(U_ideal[i], -12, 12);
451 }
452
453 // TODO(austin): Model this better.
Comran Morshed41ed7c22015-11-04 21:03:37 +0000454 // TODO(austin): Feedback?
Comran Morshede9b12922015-11-04 19:46:48 +0000455 loop_->mutable_X_hat() =
456 loop_->A() * loop_->X_hat() + loop_->B() * loop_->U();
Comran Morshede9b12922015-11-04 19:46:48 +0000457 }
458 }
459
Comran Morshed41ed7c22015-11-04 21:03:37 +0000460 void SendMotors(DrivetrainQueue::Output *output) {
Comran Morshede9b12922015-11-04 19:46:48 +0000461 if (output != NULL) {
462 output->left_voltage = loop_->U(0, 0);
463 output->right_voltage = loop_->U(1, 0);
Comran Morshed41ed7c22015-11-04 21:03:37 +0000464 output->left_high = left_gear_ == HIGH;
465 output->right_high = right_gear_ == HIGH;
Comran Morshede9b12922015-11-04 19:46:48 +0000466 }
467 }
468
469 private:
470 const ::aos::controls::HPolytope<2> U_Poly_;
471
472 ::std::unique_ptr<StateFeedbackLoop<2, 2, 2>> loop_;
473
474 const double ttrust_;
475 double wheel_;
476 double throttle_;
477 bool quickturn_;
478 int stale_count_;
479 double position_time_delta_;
480 Gear left_gear_;
481 Gear right_gear_;
Comran Morshed41ed7c22015-11-04 21:03:37 +0000482 DrivetrainQueue::Position last_position_;
483 DrivetrainQueue::Position position_;
Comran Morshede9b12922015-11-04 19:46:48 +0000484 int counter_;
485};
Comran Morshed41ed7c22015-11-04 21:03:37 +0000486
Comran Morshede9b12922015-11-04 19:46:48 +0000487constexpr double PolyDrivetrain::kStallTorque;
488constexpr double PolyDrivetrain::kStallCurrent;
489constexpr double PolyDrivetrain::kFreeSpeed;
490constexpr double PolyDrivetrain::kFreeCurrent;
491constexpr double PolyDrivetrain::J;
492constexpr double PolyDrivetrain::m;
493constexpr double PolyDrivetrain::rb;
494constexpr double PolyDrivetrain::kWheelRadius;
495constexpr double PolyDrivetrain::kR;
496constexpr double PolyDrivetrain::Kv;
497constexpr double PolyDrivetrain::Kt;
498
Comran Morshed41ed7c22015-11-04 21:03:37 +0000499void DrivetrainLoop::RunIteration(const DrivetrainQueue::Goal *goal,
500 const DrivetrainQueue::Position *position,
501 DrivetrainQueue::Output *output,
502 DrivetrainQueue::Status *status) {
Comran Morshede9b12922015-11-04 19:46:48 +0000503 // TODO(aschuh): These should be members of the class.
504 static DrivetrainMotorsSS dt_closedloop;
505 static PolyDrivetrain dt_openloop;
506
507 bool bad_pos = false;
508 if (position == nullptr) {
509 LOG_INTERVAL(no_position_);
510 bad_pos = true;
511 }
512 no_position_.Print();
513
Comran Morshed41ed7c22015-11-04 21:03:37 +0000514 bool control_loop_driving = false;
515 if (goal) {
516 double wheel = goal->steering;
517 double throttle = goal->throttle;
518 bool quickturn = goal->quickturn;
519 bool highgear = goal->highgear;
Comran Morshede9b12922015-11-04 19:46:48 +0000520
Comran Morshed41ed7c22015-11-04 21:03:37 +0000521 control_loop_driving = goal->control_loop_driving;
522 double left_goal = goal->left_goal;
523 double right_goal = goal->right_goal;
Comran Morshede9b12922015-11-04 19:46:48 +0000524
Comran Morshed41ed7c22015-11-04 21:03:37 +0000525 dt_closedloop.SetGoal(left_goal, goal->left_velocity_goal, right_goal,
526 goal->right_velocity_goal);
527 dt_openloop.SetGoal(wheel, throttle, quickturn, highgear);
528 }
529
Comran Morshede9b12922015-11-04 19:46:48 +0000530 if (!bad_pos) {
531 const double left_encoder = position->left_encoder;
532 const double right_encoder = position->right_encoder;
533 if (gyro_reading.FetchLatest()) {
534 LOG_STRUCT(DEBUG, "using", *gyro_reading.get());
535 dt_closedloop.SetPosition(left_encoder, right_encoder,
536 gyro_reading->angle);
537 } else {
538 dt_closedloop.SetRawPosition(left_encoder, right_encoder);
539 }
540 }
541 dt_openloop.SetPosition(position);
Comran Morshede9b12922015-11-04 19:46:48 +0000542 dt_openloop.Update();
543
544 if (control_loop_driving) {
545 dt_closedloop.Update(output == NULL, true);
546 dt_closedloop.SendMotors(output);
547 } else {
548 dt_openloop.SendMotors(output);
549 if (output) {
550 dt_closedloop.SetExternalMotors(output->left_voltage,
551 output->right_voltage);
552 }
553 dt_closedloop.Update(output == NULL, false);
554 }
555
556 // set the output status of the control loop state
557 if (status) {
558 bool done = false;
559 if (goal) {
560 done = ((::std::abs(goal->left_goal -
561 dt_closedloop.GetEstimatedLeftEncoder()) <
Comran Morshed41ed7c22015-11-04 21:03:37 +0000562 kDrivetrainDoneDistance) &&
Comran Morshede9b12922015-11-04 19:46:48 +0000563 (::std::abs(goal->right_goal -
564 dt_closedloop.GetEstimatedRightEncoder()) <
Comran Morshed41ed7c22015-11-04 21:03:37 +0000565 kDrivetrainDoneDistance));
Comran Morshede9b12922015-11-04 19:46:48 +0000566 }
567 status->is_done = done;
568 status->robot_speed = dt_closedloop.GetEstimatedRobotSpeed();
569 status->filtered_left_position = dt_closedloop.GetEstimatedLeftEncoder();
570 status->filtered_right_position = dt_closedloop.GetEstimatedRightEncoder();
Comran Morshed41ed7c22015-11-04 21:03:37 +0000571
572 status->filtered_left_velocity = dt_closedloop.loop().X_hat(1, 0);
573 status->filtered_right_velocity = dt_closedloop.loop().X_hat(3, 0);
Comran Morshede9b12922015-11-04 19:46:48 +0000574 status->output_was_capped = dt_closedloop.OutputWasCapped();
575 status->uncapped_left_voltage = dt_closedloop.loop().U_uncapped(0, 0);
576 status->uncapped_right_voltage = dt_closedloop.loop().U_uncapped(1, 0);
577 }
578}
579
580} // namespace control_loops
Comran Morshed41ed7c22015-11-04 21:03:37 +0000581} // namespace y2014_bot3