blob: c97be6f6c3ecd0fe76fc4370bf7ebc5edf3c3045 [file] [log] [blame]
Austin Schuh96ce8ae2015-11-26 12:46:02 -08001#include "y2014/control_loops/drivetrain/polydrivetrain.h"
2
3#include "aos/common/logging/logging.h"
4#include "aos/common/controls/polytope.h"
5#include "aos/common/commonmath.h"
6#include "aos/common/logging/queue_logging.h"
7#include "aos/common/logging/matrix_logging.h"
8
9#include "aos/common/messages/robot_state.q.h"
10#include "frc971/control_loops/state_feedback_loop.h"
11#include "frc971/control_loops/coerce_goal.h"
12#include "y2014/constants.h"
13#include "y2014/control_loops/drivetrain/drivetrain.q.h"
14#include "y2014/control_loops/drivetrain/drivetrain_dog_motor_plant.h"
15
16#define HAVE_SHIFTERS 1
17
18namespace frc971 {
19namespace control_loops {
20
21using ::y2014::control_loops::drivetrain::kDt;
22
23PolyDrivetrain::PolyDrivetrain()
24 : U_Poly_((Eigen::Matrix<double, 4, 2>() << /*[[*/ 1, 0 /*]*/,
25 /*[*/ -1, 0 /*]*/,
26 /*[*/ 0, 1 /*]*/,
27 /*[*/ 0, -1 /*]]*/).finished(),
28 (Eigen::Matrix<double, 4, 1>() << /*[[*/ 12 /*]*/,
29 /*[*/ 12 /*]*/,
30 /*[*/ 12 /*]*/,
31 /*[*/ 12 /*]]*/).finished()),
32 loop_(new StateFeedbackLoop<2, 2, 2>(
33 constants::GetValues().make_v_drivetrain_loop())),
34 ttrust_(1.1),
35 wheel_(0.0),
36 throttle_(0.0),
37 quickturn_(false),
38 stale_count_(0),
39 position_time_delta_(kDt),
40 left_gear_(LOW),
41 right_gear_(LOW),
42 counter_(0) {
43 last_position_.Zero();
44 position_.Zero();
45}
46
47double PolyDrivetrain::MotorSpeed(
48 const constants::ShifterHallEffect &hall_effect, double shifter_position,
49 double velocity) {
50 // TODO(austin): G_high, G_low and kWheelRadius
51 const double avg_hall_effect =
52 (hall_effect.clear_high + hall_effect.clear_low) / 2.0;
53
54 if (shifter_position > avg_hall_effect) {
55 return velocity / constants::GetValues().high_gear_ratio / kWheelRadius;
56 } else {
57 return velocity / constants::GetValues().low_gear_ratio / kWheelRadius;
58 }
59}
60
61PolyDrivetrain::Gear PolyDrivetrain::ComputeGear(
62 const constants::ShifterHallEffect &hall_effect, double velocity,
63 Gear current) {
64 const double low_omega = MotorSpeed(hall_effect, 0.0, ::std::abs(velocity));
65 const double high_omega = MotorSpeed(hall_effect, 1.0, ::std::abs(velocity));
66
67 double high_torque = ((12.0 - high_omega / Kv) * Kt / kR);
68 double low_torque = ((12.0 - low_omega / Kv) * Kt / kR);
69 double high_power = high_torque * high_omega;
70 double low_power = low_torque * low_omega;
71
72 // TODO(aschuh): Do this right!
73 if ((current == HIGH || high_power > low_power + 160) &&
74 ::std::abs(velocity) > 0.14) {
75 return HIGH;
76 } else {
77 return LOW;
78 }
79}
80
81void PolyDrivetrain::SetGoal(double wheel, double throttle, bool quickturn,
82 bool highgear) {
83 const double kWheelNonLinearity = 0.3;
84 // Apply a sin function that's scaled to make it feel better.
85 const double angular_range = M_PI_2 * kWheelNonLinearity;
86
87 wheel_ = sin(angular_range * wheel) / sin(angular_range);
88 wheel_ = sin(angular_range * wheel_) / sin(angular_range);
89 quickturn_ = quickturn;
90
91 static const double kThrottleDeadband = 0.05;
92 if (::std::abs(throttle) < kThrottleDeadband) {
93 throttle_ = 0;
94 } else {
95 throttle_ = copysign(
96 (::std::abs(throttle) - kThrottleDeadband) / (1.0 - kThrottleDeadband),
97 throttle);
98 }
99
100 // TODO(austin): Fix the upshift logic to include states.
101 Gear requested_gear;
102 if (false) {
103 const auto &values = constants::GetValues();
104 const double current_left_velocity =
105 (position_.left_encoder - last_position_.left_encoder) /
106 position_time_delta_;
107 const double current_right_velocity =
108 (position_.right_encoder - last_position_.right_encoder) /
109 position_time_delta_;
110
111 Gear left_requested =
112 ComputeGear(values.left_drive, current_left_velocity, left_gear_);
113 Gear right_requested =
114 ComputeGear(values.right_drive, current_right_velocity, right_gear_);
115 requested_gear =
116 (left_requested == HIGH || right_requested == HIGH) ? HIGH : LOW;
117 } else {
118 requested_gear = highgear ? HIGH : LOW;
119 }
120
121 const Gear shift_up =
122 constants::GetValues().clutch_transmission ? HIGH : SHIFTING_UP;
123 const Gear shift_down =
124 constants::GetValues().clutch_transmission ? LOW : SHIFTING_DOWN;
125
126 if (left_gear_ != requested_gear) {
127 if (IsInGear(left_gear_)) {
128 if (requested_gear == HIGH) {
129 left_gear_ = shift_up;
130 } else {
131 left_gear_ = shift_down;
132 }
133 } else {
134 if (requested_gear == HIGH && left_gear_ == SHIFTING_DOWN) {
135 left_gear_ = SHIFTING_UP;
136 } else if (requested_gear == LOW && left_gear_ == SHIFTING_UP) {
137 left_gear_ = SHIFTING_DOWN;
138 }
139 }
140 }
141 if (right_gear_ != requested_gear) {
142 if (IsInGear(right_gear_)) {
143 if (requested_gear == HIGH) {
144 right_gear_ = shift_up;
145 } else {
146 right_gear_ = shift_down;
147 }
148 } else {
149 if (requested_gear == HIGH && right_gear_ == SHIFTING_DOWN) {
150 right_gear_ = SHIFTING_UP;
151 } else if (requested_gear == LOW && right_gear_ == SHIFTING_UP) {
152 right_gear_ = SHIFTING_DOWN;
153 }
154 }
155 }
156}
157void PolyDrivetrain::SetPosition(const DrivetrainQueue::Position *position) {
158 const auto &values = constants::GetValues();
159 if (position == NULL) {
160 ++stale_count_;
161 } else {
162 last_position_ = position_;
163 position_ = *position;
164 position_time_delta_ = (stale_count_ + 1) * kDt;
165 stale_count_ = 0;
166 }
167
168#if HAVE_SHIFTERS
169 if (position) {
170 GearLogging gear_logging;
171 // Switch to the correct controller.
172 const double left_middle_shifter_position =
173 (values.left_drive.clear_high + values.left_drive.clear_low) / 2.0;
174 const double right_middle_shifter_position =
175 (values.right_drive.clear_high + values.right_drive.clear_low) / 2.0;
176
177 if (position->left_shifter_position < left_middle_shifter_position ||
178 left_gear_ == LOW) {
179 if (position->right_shifter_position < right_middle_shifter_position ||
180 right_gear_ == LOW) {
181 gear_logging.left_loop_high = false;
182 gear_logging.right_loop_high = false;
183 loop_->set_controller_index(gear_logging.controller_index = 0);
184 } else {
185 gear_logging.left_loop_high = false;
186 gear_logging.right_loop_high = true;
187 loop_->set_controller_index(gear_logging.controller_index = 1);
188 }
189 } else {
190 if (position->right_shifter_position < right_middle_shifter_position ||
191 right_gear_ == LOW) {
192 gear_logging.left_loop_high = true;
193 gear_logging.right_loop_high = false;
194 loop_->set_controller_index(gear_logging.controller_index = 2);
195 } else {
196 gear_logging.left_loop_high = true;
197 gear_logging.right_loop_high = true;
198 loop_->set_controller_index(gear_logging.controller_index = 3);
199 }
200 }
201
202 if (position->left_shifter_position > values.left_drive.clear_high &&
203 left_gear_ == SHIFTING_UP) {
204 left_gear_ = HIGH;
205 }
206 if (position->left_shifter_position < values.left_drive.clear_low &&
207 left_gear_ == SHIFTING_DOWN) {
208 left_gear_ = LOW;
209 }
210 if (position->right_shifter_position > values.right_drive.clear_high &&
211 right_gear_ == SHIFTING_UP) {
212 right_gear_ = HIGH;
213 }
214 if (position->right_shifter_position < values.right_drive.clear_low &&
215 right_gear_ == SHIFTING_DOWN) {
216 right_gear_ = LOW;
217 }
218
219 gear_logging.left_state = left_gear_;
220 gear_logging.right_state = right_gear_;
221 LOG_STRUCT(DEBUG, "state", gear_logging);
222 }
223#else
224 (void)values;
225#endif
226}
227
228double PolyDrivetrain::FilterVelocity(double throttle) {
229 const Eigen::Matrix<double, 2, 2> FF =
230 loop_->B().inverse() *
231 (Eigen::Matrix<double, 2, 2>::Identity() - loop_->A());
232
233 constexpr int kHighGearController = 3;
234 const Eigen::Matrix<double, 2, 2> FF_high =
235 loop_->controller(kHighGearController).plant.B().inverse() *
236 (Eigen::Matrix<double, 2, 2>::Identity() -
237 loop_->controller(kHighGearController).plant.A());
238
239 ::Eigen::Matrix<double, 1, 2> FF_sum = FF.colwise().sum();
240 int min_FF_sum_index;
241 const double min_FF_sum = FF_sum.minCoeff(&min_FF_sum_index);
242 const double min_K_sum = loop_->K().col(min_FF_sum_index).sum();
243 const double high_min_FF_sum = FF_high.col(0).sum();
244
245 const double adjusted_ff_voltage =
246 ::aos::Clip(throttle * 12.0 * min_FF_sum / high_min_FF_sum, -12.0, 12.0);
247 return (adjusted_ff_voltage +
248 ttrust_ * min_K_sum * (loop_->X_hat(0, 0) + loop_->X_hat(1, 0)) /
249 2.0) /
250 (ttrust_ * min_K_sum + min_FF_sum);
251}
252
253double PolyDrivetrain::MaxVelocity() {
254 const Eigen::Matrix<double, 2, 2> FF =
255 loop_->B().inverse() *
256 (Eigen::Matrix<double, 2, 2>::Identity() - loop_->A());
257
258 constexpr int kHighGearController = 3;
259 const Eigen::Matrix<double, 2, 2> FF_high =
260 loop_->controller(kHighGearController).plant.B().inverse() *
261 (Eigen::Matrix<double, 2, 2>::Identity() -
262 loop_->controller(kHighGearController).plant.A());
263
264 ::Eigen::Matrix<double, 1, 2> FF_sum = FF.colwise().sum();
265 int min_FF_sum_index;
266 const double min_FF_sum = FF_sum.minCoeff(&min_FF_sum_index);
267 // const double min_K_sum = loop_->K().col(min_FF_sum_index).sum();
268 const double high_min_FF_sum = FF_high.col(0).sum();
269
270 const double adjusted_ff_voltage =
271 ::aos::Clip(12.0 * min_FF_sum / high_min_FF_sum, -12.0, 12.0);
272 return adjusted_ff_voltage / min_FF_sum;
273}
274
275void PolyDrivetrain::Update() {
276 const auto &values = constants::GetValues();
277 // TODO(austin): Observer for the current velocity instead of difference
278 // calculations.
279 ++counter_;
280#if HAVE_SHIFTERS
281 const double current_left_velocity =
282 (position_.left_encoder - last_position_.left_encoder) /
283 position_time_delta_;
284 const double current_right_velocity =
285 (position_.right_encoder - last_position_.right_encoder) /
286 position_time_delta_;
287 const double left_motor_speed =
288 MotorSpeed(values.left_drive, position_.left_shifter_position,
289 current_left_velocity);
290 const double right_motor_speed =
291 MotorSpeed(values.right_drive, position_.right_shifter_position,
292 current_right_velocity);
293
294 {
295 CIMLogging logging;
296
297 // Reset the CIM model to the current conditions to be ready for when we
298 // shift.
299 if (IsInGear(left_gear_)) {
300 logging.left_in_gear = true;
301 } else {
302 logging.left_in_gear = false;
303 }
304 logging.left_motor_speed = left_motor_speed;
305 logging.left_velocity = current_left_velocity;
306 if (IsInGear(right_gear_)) {
307 logging.right_in_gear = true;
308 } else {
309 logging.right_in_gear = false;
310 }
311 logging.right_motor_speed = right_motor_speed;
312 logging.right_velocity = current_right_velocity;
313
314 LOG_STRUCT(DEBUG, "currently", logging);
315 }
316#else
317 (void)values;
318#endif
319
320#if HAVE_SHIFTERS
321 if (IsInGear(left_gear_) && IsInGear(right_gear_))
322#endif
323 {
324 // FF * X = U (steady state)
325 const Eigen::Matrix<double, 2, 2> FF =
326 loop_->B().inverse() *
327 (Eigen::Matrix<double, 2, 2>::Identity() - loop_->A());
328
329 // Invert the plant to figure out how the velocity filter would have to
330 // work
331 // out in order to filter out the forwards negative inertia.
332 // This math assumes that the left and right power and velocity are
333 // equals,
334 // and that the plant is the same on the left and right.
335 const double fvel = FilterVelocity(throttle_);
336
337 const double sign_svel = wheel_ * ((fvel > 0.0) ? 1.0 : -1.0);
338 double steering_velocity;
339 if (quickturn_) {
340 steering_velocity = wheel_ * MaxVelocity();
341 } else {
342 steering_velocity = ::std::abs(fvel) * wheel_;
343 }
344 const double left_velocity = fvel - steering_velocity;
345 const double right_velocity = fvel + steering_velocity;
346
347 // Integrate velocity to get the position.
348 // This position is used to get integral control.
349 loop_->mutable_R() << left_velocity, right_velocity;
350
351 if (!quickturn_) {
352 // K * R = w
353 Eigen::Matrix<double, 1, 2> equality_k;
354 equality_k << 1 + sign_svel, -(1 - sign_svel);
355 const double equality_w = 0.0;
356
357 // Construct a constraint on R by manipulating the constraint on U
358 ::aos::controls::HPolytope<2> R_poly = ::aos::controls::HPolytope<2>(
359 U_Poly_.H() * (loop_->K() + FF),
360 U_Poly_.k() + U_Poly_.H() * loop_->K() * loop_->X_hat());
361
362 // Limit R back inside the box.
363 loop_->mutable_R() =
364 CoerceGoal(R_poly, equality_k, equality_w, loop_->R());
365 }
366
367 const Eigen::Matrix<double, 2, 1> FF_volts = FF * loop_->R();
368 const Eigen::Matrix<double, 2, 1> U_ideal =
369 loop_->K() * (loop_->R() - loop_->X_hat()) + FF_volts;
370
371 for (int i = 0; i < 2; i++) {
372 loop_->mutable_U()[i] = ::aos::Clip(U_ideal[i], -12, 12);
373 }
374
375 // TODO(austin): Model this better.
376 // TODO(austin): Feed back?
377 loop_->mutable_X_hat() =
378 loop_->A() * loop_->X_hat() + loop_->B() * loop_->U();
379#if HAVE_SHIFTERS
380 } else {
381 // Any motor is not in gear. Speed match.
382 ::Eigen::Matrix<double, 1, 1> R_left;
383 ::Eigen::Matrix<double, 1, 1> R_right;
384 R_left(0, 0) = left_motor_speed;
385 R_right(0, 0) = right_motor_speed;
386
387 const double wiggle =
388 (static_cast<double>((counter_ % 20) / 10) - 0.5) * 5.0;
389
390 loop_->mutable_U(0, 0) = ::aos::Clip(
391 (R_left / Kv)(0, 0) + (IsInGear(left_gear_) ? 0 : wiggle), -12.0, 12.0);
392 loop_->mutable_U(1, 0) =
393 ::aos::Clip((R_right / Kv)(0, 0) + (IsInGear(right_gear_) ? 0 : wiggle),
394 -12.0, 12.0);
395 loop_->mutable_U() *= 12.0 / ::aos::robot_state->voltage_battery;
396#endif
397 }
398}
399
400void PolyDrivetrain::SendMotors(DrivetrainQueue::Output *output) {
401 if (output != NULL) {
402 output->left_voltage = loop_->U(0, 0);
403 output->right_voltage = loop_->U(1, 0);
404 output->left_high = left_gear_ == HIGH || left_gear_ == SHIFTING_UP;
405 output->right_high = right_gear_ == HIGH || right_gear_ == SHIFTING_UP;
406 }
407}
408
409constexpr double PolyDrivetrain::kStallTorque;
410constexpr double PolyDrivetrain::kStallCurrent;
411constexpr double PolyDrivetrain::kFreeSpeed;
412constexpr double PolyDrivetrain::kFreeCurrent;
413constexpr double PolyDrivetrain::kWheelRadius;
414constexpr double PolyDrivetrain::kR;
415constexpr double PolyDrivetrain::Kv;
416constexpr double PolyDrivetrain::Kt;
417
418} // namespace control_loops
419} // namespace frc971