blob: cefb1ddca7f9e5e34d799edb847c6fe871f22f02 [file] [log] [blame]
Comran Morshed5323ecb2015-12-26 20:50:55 +00001#include "frc971/control_loops/drivetrain/polydrivetrain.h"
Austin Schuh96ce8ae2015-11-26 12:46:02 -08002
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"
Comran Morshed5323ecb2015-12-26 20:50:55 +000012#include "frc971/control_loops/drivetrain/drivetrain.q.h"
13#include "frc971/control_loops/drivetrain/drivetrain_config.h"
Austin Schuh96ce8ae2015-11-26 12:46:02 -080014
Comran Morshed5323ecb2015-12-26 20:50:55 +000015namespace frc971 {
Austin Schuh96ce8ae2015-11-26 12:46:02 -080016namespace control_loops {
Austin Schuh6197a182015-11-28 16:04:40 -080017namespace drivetrain {
Austin Schuh96ce8ae2015-11-26 12:46:02 -080018
Comran Morshed5323ecb2015-12-26 20:50:55 +000019using ::frc971::control_loops::GearLogging;
20using ::frc971::control_loops::CIMLogging;
Austin Schuh6197a182015-11-28 16:04:40 -080021using ::frc971::control_loops::CoerceGoal;
Austin Schuh96ce8ae2015-11-26 12:46:02 -080022
Comran Morshed5323ecb2015-12-26 20:50:55 +000023PolyDrivetrain::PolyDrivetrain(const DrivetrainConfig &dt_config)
24 : kf_(dt_config.make_kf_drivetrain_loop()),
Austin Schuh6613a072016-01-06 19:54:48 -080025 U_Poly_((Eigen::Matrix<double, 4, 2>() << /*[[*/ 1, 0 /*]*/,
Austin Schuh96ce8ae2015-11-26 12:46:02 -080026 /*[*/ -1, 0 /*]*/,
27 /*[*/ 0, 1 /*]*/,
28 /*[*/ 0, -1 /*]]*/).finished(),
29 (Eigen::Matrix<double, 4, 1>() << /*[[*/ 12 /*]*/,
30 /*[*/ 12 /*]*/,
31 /*[*/ 12 /*]*/,
32 /*[*/ 12 /*]]*/).finished()),
Comran Morshed5323ecb2015-12-26 20:50:55 +000033 loop_(new StateFeedbackLoop<2, 2, 2>(dt_config.make_v_drivetrain_loop())),
Austin Schuh96ce8ae2015-11-26 12:46:02 -080034 ttrust_(1.1),
35 wheel_(0.0),
36 throttle_(0.0),
37 quickturn_(false),
38 stale_count_(0),
Comran Morshed5323ecb2015-12-26 20:50:55 +000039 position_time_delta_(dt_config.dt),
Austin Schuh96ce8ae2015-11-26 12:46:02 -080040 left_gear_(LOW),
41 right_gear_(LOW),
Comran Morshed5323ecb2015-12-26 20:50:55 +000042 counter_(0),
43 dt_config_(dt_config) {
Austin Schuh96ce8ae2015-11-26 12:46:02 -080044 last_position_.Zero();
45 position_.Zero();
46}
47
48double PolyDrivetrain::MotorSpeed(
49 const constants::ShifterHallEffect &hall_effect, double shifter_position,
Comran Morshed2091fe52016-02-19 17:27:21 +000050 double velocity, Gear gear) {
51 const double high_gear_speed =
52 velocity / dt_config_.high_gear_ratio / dt_config_.wheel_radius;
53 const double low_gear_speed =
54 velocity / dt_config_.low_gear_ratio / dt_config_.wheel_radius;
Austin Schuh96ce8ae2015-11-26 12:46:02 -080055
Comran Morshed2091fe52016-02-19 17:27:21 +000056 if (shifter_position < hall_effect.clear_low) {
57 // We're in low gear, so return speed for that gear.
58 return low_gear_speed;
59 } else if (shifter_position > hall_effect.clear_high) {
60 // We're in high gear, so return speed for that gear.
61 return high_gear_speed;
62 }
63
64 // Not in gear, so speed-match to destination gear.
65 switch (gear) {
66 case HIGH:
67 case SHIFTING_UP:
68 return high_gear_speed;
69 case LOW:
70 case SHIFTING_DOWN:
71 return low_gear_speed;
72 break;
Austin Schuh96ce8ae2015-11-26 12:46:02 -080073 }
74}
75
Comran Morshed5323ecb2015-12-26 20:50:55 +000076PolyDrivetrain::Gear PolyDrivetrain::UpdateSingleGear(Gear requested_gear,
77 Gear current_gear) {
Austin Schuhb9d5e8e2015-12-27 14:08:47 -080078 const Gear shift_up =
Comran Morshed5323ecb2015-12-26 20:50:55 +000079 (dt_config_.shifter_type == ShifterType::HALL_EFFECT_SHIFTER)
80 ? SHIFTING_UP
81 : HIGH;
Austin Schuhb9d5e8e2015-12-27 14:08:47 -080082 const Gear shift_down =
Comran Morshed5323ecb2015-12-26 20:50:55 +000083 (dt_config_.shifter_type == ShifterType::HALL_EFFECT_SHIFTER)
84 ? SHIFTING_DOWN
85 : LOW;
Austin Schuhb9d5e8e2015-12-27 14:08:47 -080086 if (current_gear != requested_gear) {
87 if (IsInGear(current_gear)) {
88 if (requested_gear == HIGH) {
89 if (current_gear != HIGH) {
90 current_gear = shift_up;
91 }
92 } else {
93 if (current_gear != LOW) {
94 current_gear = shift_down;
95 }
96 }
97 } else {
98 if (requested_gear == HIGH && current_gear == SHIFTING_DOWN) {
99 current_gear = SHIFTING_UP;
100 } else if (requested_gear == LOW && current_gear == SHIFTING_UP) {
101 current_gear = SHIFTING_DOWN;
102 }
103 }
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800104 }
Austin Schuhb9d5e8e2015-12-27 14:08:47 -0800105 return current_gear;
106}
107
108void PolyDrivetrain::UpdateGears(Gear requested_gear) {
109 left_gear_ = UpdateSingleGear(requested_gear, left_gear_);
110 right_gear_ = UpdateSingleGear(requested_gear, right_gear_);
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800111}
112
113void PolyDrivetrain::SetGoal(double wheel, double throttle, bool quickturn,
114 bool highgear) {
115 const double kWheelNonLinearity = 0.3;
116 // Apply a sin function that's scaled to make it feel better.
117 const double angular_range = M_PI_2 * kWheelNonLinearity;
118
Comran Morshed5323ecb2015-12-26 20:50:55 +0000119 if (dt_config_.shifter_type == ShifterType::SIMPLE_SHIFTER) {
120 // Force the right controller for simple shifters since we assume that
121 // gear switching is instantaneous.
122 if (highgear) {
123 loop_->set_controller_index(3);
124 } else {
125 loop_->set_controller_index(0);
126 }
127 }
128
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800129 wheel_ = sin(angular_range * wheel) / sin(angular_range);
130 wheel_ = sin(angular_range * wheel_) / sin(angular_range);
131 quickturn_ = quickturn;
132
133 static const double kThrottleDeadband = 0.05;
134 if (::std::abs(throttle) < kThrottleDeadband) {
135 throttle_ = 0;
136 } else {
137 throttle_ = copysign(
138 (::std::abs(throttle) - kThrottleDeadband) / (1.0 - kThrottleDeadband),
139 throttle);
140 }
141
Austin Schuhb9d5e8e2015-12-27 14:08:47 -0800142 UpdateGears(highgear ? HIGH : LOW);
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800143}
Austin Schuhb9d5e8e2015-12-27 14:08:47 -0800144
Austin Schuh6197a182015-11-28 16:04:40 -0800145void PolyDrivetrain::SetPosition(
Comran Morshed5323ecb2015-12-26 20:50:55 +0000146 const ::frc971::control_loops::DrivetrainQueue::Position *position) {
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800147 if (position == NULL) {
148 ++stale_count_;
149 } else {
150 last_position_ = position_;
151 position_ = *position;
Comran Morshed5323ecb2015-12-26 20:50:55 +0000152 position_time_delta_ = (stale_count_ + 1) * dt_config_.dt;
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800153 stale_count_ = 0;
154 }
155
Comran Morshed5323ecb2015-12-26 20:50:55 +0000156 if (dt_config_.shifter_type == ShifterType::HALL_EFFECT_SHIFTER && position) {
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800157 GearLogging gear_logging;
158 // Switch to the correct controller.
159 const double left_middle_shifter_position =
Comran Morshed5323ecb2015-12-26 20:50:55 +0000160 (dt_config_.left_drive.clear_high + dt_config_.left_drive.clear_low) /
161 2.0;
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800162 const double right_middle_shifter_position =
Comran Morshed5323ecb2015-12-26 20:50:55 +0000163 (dt_config_.right_drive.clear_high + dt_config_.right_drive.clear_low) /
164 2.0;
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800165
166 if (position->left_shifter_position < left_middle_shifter_position ||
167 left_gear_ == LOW) {
168 if (position->right_shifter_position < right_middle_shifter_position ||
169 right_gear_ == LOW) {
170 gear_logging.left_loop_high = false;
171 gear_logging.right_loop_high = false;
172 loop_->set_controller_index(gear_logging.controller_index = 0);
173 } else {
174 gear_logging.left_loop_high = false;
175 gear_logging.right_loop_high = true;
176 loop_->set_controller_index(gear_logging.controller_index = 1);
177 }
178 } else {
179 if (position->right_shifter_position < right_middle_shifter_position ||
180 right_gear_ == LOW) {
181 gear_logging.left_loop_high = true;
182 gear_logging.right_loop_high = false;
183 loop_->set_controller_index(gear_logging.controller_index = 2);
184 } else {
185 gear_logging.left_loop_high = true;
186 gear_logging.right_loop_high = true;
187 loop_->set_controller_index(gear_logging.controller_index = 3);
188 }
189 }
190
Comran Morshed5323ecb2015-12-26 20:50:55 +0000191 if (position->left_shifter_position > dt_config_.left_drive.clear_high &&
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800192 left_gear_ == SHIFTING_UP) {
193 left_gear_ = HIGH;
194 }
Comran Morshed5323ecb2015-12-26 20:50:55 +0000195 if (position->left_shifter_position < dt_config_.left_drive.clear_low &&
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800196 left_gear_ == SHIFTING_DOWN) {
197 left_gear_ = LOW;
198 }
Comran Morshed5323ecb2015-12-26 20:50:55 +0000199 if (position->right_shifter_position > dt_config_.right_drive.clear_high &&
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800200 right_gear_ == SHIFTING_UP) {
201 right_gear_ = HIGH;
202 }
Comran Morshed5323ecb2015-12-26 20:50:55 +0000203 if (position->right_shifter_position < dt_config_.right_drive.clear_low &&
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800204 right_gear_ == SHIFTING_DOWN) {
205 right_gear_ = LOW;
206 }
207
208 gear_logging.left_state = left_gear_;
209 gear_logging.right_state = right_gear_;
210 LOG_STRUCT(DEBUG, "state", gear_logging);
211 }
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800212}
213
214double PolyDrivetrain::FilterVelocity(double throttle) {
215 const Eigen::Matrix<double, 2, 2> FF =
216 loop_->B().inverse() *
217 (Eigen::Matrix<double, 2, 2>::Identity() - loop_->A());
218
219 constexpr int kHighGearController = 3;
220 const Eigen::Matrix<double, 2, 2> FF_high =
221 loop_->controller(kHighGearController).plant.B().inverse() *
222 (Eigen::Matrix<double, 2, 2>::Identity() -
223 loop_->controller(kHighGearController).plant.A());
224
225 ::Eigen::Matrix<double, 1, 2> FF_sum = FF.colwise().sum();
226 int min_FF_sum_index;
227 const double min_FF_sum = FF_sum.minCoeff(&min_FF_sum_index);
228 const double min_K_sum = loop_->K().col(min_FF_sum_index).sum();
229 const double high_min_FF_sum = FF_high.col(0).sum();
230
231 const double adjusted_ff_voltage =
232 ::aos::Clip(throttle * 12.0 * min_FF_sum / high_min_FF_sum, -12.0, 12.0);
233 return (adjusted_ff_voltage +
234 ttrust_ * min_K_sum * (loop_->X_hat(0, 0) + loop_->X_hat(1, 0)) /
235 2.0) /
236 (ttrust_ * min_K_sum + min_FF_sum);
237}
238
239double PolyDrivetrain::MaxVelocity() {
240 const Eigen::Matrix<double, 2, 2> FF =
241 loop_->B().inverse() *
242 (Eigen::Matrix<double, 2, 2>::Identity() - loop_->A());
243
244 constexpr int kHighGearController = 3;
245 const Eigen::Matrix<double, 2, 2> FF_high =
246 loop_->controller(kHighGearController).plant.B().inverse() *
247 (Eigen::Matrix<double, 2, 2>::Identity() -
248 loop_->controller(kHighGearController).plant.A());
249
250 ::Eigen::Matrix<double, 1, 2> FF_sum = FF.colwise().sum();
251 int min_FF_sum_index;
252 const double min_FF_sum = FF_sum.minCoeff(&min_FF_sum_index);
253 // const double min_K_sum = loop_->K().col(min_FF_sum_index).sum();
254 const double high_min_FF_sum = FF_high.col(0).sum();
255
256 const double adjusted_ff_voltage =
257 ::aos::Clip(12.0 * min_FF_sum / high_min_FF_sum, -12.0, 12.0);
258 return adjusted_ff_voltage / min_FF_sum;
259}
260
261void PolyDrivetrain::Update() {
Comran Morshed5323ecb2015-12-26 20:50:55 +0000262 loop_->mutable_X_hat()(0, 0) = kf_.X_hat()(1, 0);
263 loop_->mutable_X_hat()(1, 0) = kf_.X_hat()(3, 0);
Austin Schuh0520cbf2016-01-06 19:58:36 -0800264
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800265 // TODO(austin): Observer for the current velocity instead of difference
266 // calculations.
267 ++counter_;
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800268
Austin Schuhb9d5e8e2015-12-27 14:08:47 -0800269 if (IsInGear(left_gear_) && IsInGear(right_gear_)) {
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800270 // FF * X = U (steady state)
271 const Eigen::Matrix<double, 2, 2> FF =
272 loop_->B().inverse() *
273 (Eigen::Matrix<double, 2, 2>::Identity() - loop_->A());
274
275 // Invert the plant to figure out how the velocity filter would have to
276 // work
277 // out in order to filter out the forwards negative inertia.
278 // This math assumes that the left and right power and velocity are
279 // equals,
280 // and that the plant is the same on the left and right.
281 const double fvel = FilterVelocity(throttle_);
282
283 const double sign_svel = wheel_ * ((fvel > 0.0) ? 1.0 : -1.0);
284 double steering_velocity;
285 if (quickturn_) {
286 steering_velocity = wheel_ * MaxVelocity();
287 } else {
288 steering_velocity = ::std::abs(fvel) * wheel_;
289 }
290 const double left_velocity = fvel - steering_velocity;
291 const double right_velocity = fvel + steering_velocity;
292
293 // Integrate velocity to get the position.
294 // This position is used to get integral control.
295 loop_->mutable_R() << left_velocity, right_velocity;
296
297 if (!quickturn_) {
298 // K * R = w
299 Eigen::Matrix<double, 1, 2> equality_k;
300 equality_k << 1 + sign_svel, -(1 - sign_svel);
301 const double equality_w = 0.0;
302
303 // Construct a constraint on R by manipulating the constraint on U
304 ::aos::controls::HPolytope<2> R_poly = ::aos::controls::HPolytope<2>(
305 U_Poly_.H() * (loop_->K() + FF),
306 U_Poly_.k() + U_Poly_.H() * loop_->K() * loop_->X_hat());
307
308 // Limit R back inside the box.
309 loop_->mutable_R() =
310 CoerceGoal(R_poly, equality_k, equality_w, loop_->R());
311 }
312
313 const Eigen::Matrix<double, 2, 1> FF_volts = FF * loop_->R();
314 const Eigen::Matrix<double, 2, 1> U_ideal =
315 loop_->K() * (loop_->R() - loop_->X_hat()) + FF_volts;
316
317 for (int i = 0; i < 2; i++) {
318 loop_->mutable_U()[i] = ::aos::Clip(U_ideal[i], -12, 12);
319 }
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800320 } else {
Comran Morshed5323ecb2015-12-26 20:50:55 +0000321 const double current_left_velocity =
322 (position_.left_encoder - last_position_.left_encoder) /
323 position_time_delta_;
324 const double current_right_velocity =
325 (position_.right_encoder - last_position_.right_encoder) /
326 position_time_delta_;
327 const double left_motor_speed =
328 MotorSpeed(dt_config_.left_drive, position_.left_shifter_position,
Comran Morshed2091fe52016-02-19 17:27:21 +0000329 current_left_velocity, left_gear_);
Comran Morshed5323ecb2015-12-26 20:50:55 +0000330 const double right_motor_speed =
331 MotorSpeed(dt_config_.right_drive, position_.right_shifter_position,
Comran Morshed2091fe52016-02-19 17:27:21 +0000332 current_right_velocity, right_gear_);
Comran Morshed5323ecb2015-12-26 20:50:55 +0000333
334 {
335 CIMLogging logging;
336
337 // Reset the CIM model to the current conditions to be ready for when we
338 // shift.
Comran Morshed2091fe52016-02-19 17:27:21 +0000339 logging.left_in_gear = IsInGear(left_gear_);
Comran Morshed5323ecb2015-12-26 20:50:55 +0000340 logging.left_motor_speed = left_motor_speed;
341 logging.left_velocity = current_left_velocity;
Comran Morshed2091fe52016-02-19 17:27:21 +0000342
343 logging.right_in_gear = IsInGear(right_gear_);
Comran Morshed5323ecb2015-12-26 20:50:55 +0000344 logging.right_motor_speed = right_motor_speed;
345 logging.right_velocity = current_right_velocity;
346
347 LOG_STRUCT(DEBUG, "currently", logging);
348 }
349
Comran Morshed2091fe52016-02-19 17:27:21 +0000350 // Any motor is not in gear. Speed match.
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800351 ::Eigen::Matrix<double, 1, 1> R_left;
352 ::Eigen::Matrix<double, 1, 1> R_right;
353 R_left(0, 0) = left_motor_speed;
354 R_right(0, 0) = right_motor_speed;
355
356 const double wiggle =
357 (static_cast<double>((counter_ % 20) / 10) - 0.5) * 5.0;
358
359 loop_->mutable_U(0, 0) = ::aos::Clip(
Comran Morshed5323ecb2015-12-26 20:50:55 +0000360 (R_left / dt_config_.v)(0, 0) + (IsInGear(left_gear_) ? 0 : wiggle),
361 -12.0, 12.0);
362 loop_->mutable_U(1, 0) = ::aos::Clip(
363 (R_right / dt_config_.v)(0, 0) + (IsInGear(right_gear_) ? 0 : wiggle),
364 -12.0, 12.0);
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800365 loop_->mutable_U() *= 12.0 / ::aos::robot_state->voltage_battery;
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800366 }
367}
368
Austin Schuh6197a182015-11-28 16:04:40 -0800369void PolyDrivetrain::SendMotors(
Comran Morshed5323ecb2015-12-26 20:50:55 +0000370 ::frc971::control_loops::DrivetrainQueue::Output *output) {
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800371 if (output != NULL) {
372 output->left_voltage = loop_->U(0, 0);
373 output->right_voltage = loop_->U(1, 0);
374 output->left_high = left_gear_ == HIGH || left_gear_ == SHIFTING_UP;
375 output->right_high = right_gear_ == HIGH || right_gear_ == SHIFTING_UP;
376 }
377}
378
Austin Schuh6197a182015-11-28 16:04:40 -0800379} // namespace drivetrain
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800380} // namespace control_loops
Comran Morshed5323ecb2015-12-26 20:50:55 +0000381} // namespace frc971