blob: 7287df1fe112604339d9f56b5869f070589b2b3b [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
Austin Schuh41565602016-02-28 20:10:49 -080023PolyDrivetrain::PolyDrivetrain(const DrivetrainConfig &dt_config,
24 StateFeedbackLoop<7, 2, 3> *kf)
25 : kf_(kf),
Austin Schuh6613a072016-01-06 19:54:48 -080026 U_Poly_((Eigen::Matrix<double, 4, 2>() << /*[[*/ 1, 0 /*]*/,
Austin Schuh96ce8ae2015-11-26 12:46:02 -080027 /*[*/ -1, 0 /*]*/,
28 /*[*/ 0, 1 /*]*/,
29 /*[*/ 0, -1 /*]]*/).finished(),
30 (Eigen::Matrix<double, 4, 1>() << /*[[*/ 12 /*]*/,
31 /*[*/ 12 /*]*/,
32 /*[*/ 12 /*]*/,
33 /*[*/ 12 /*]]*/).finished()),
Comran Morshed5323ecb2015-12-26 20:50:55 +000034 loop_(new StateFeedbackLoop<2, 2, 2>(dt_config.make_v_drivetrain_loop())),
Austin Schuh96ce8ae2015-11-26 12:46:02 -080035 ttrust_(1.1),
36 wheel_(0.0),
37 throttle_(0.0),
38 quickturn_(false),
39 stale_count_(0),
Comran Morshed5323ecb2015-12-26 20:50:55 +000040 position_time_delta_(dt_config.dt),
Austin Schuh96ce8ae2015-11-26 12:46:02 -080041 left_gear_(LOW),
42 right_gear_(LOW),
Comran Morshed5323ecb2015-12-26 20:50:55 +000043 counter_(0),
44 dt_config_(dt_config) {
Austin Schuh96ce8ae2015-11-26 12:46:02 -080045 last_position_.Zero();
46 position_.Zero();
47}
48
49double PolyDrivetrain::MotorSpeed(
50 const constants::ShifterHallEffect &hall_effect, double shifter_position,
Comran Morshed2091fe52016-02-19 17:27:21 +000051 double velocity, Gear gear) {
52 const double high_gear_speed =
53 velocity / dt_config_.high_gear_ratio / dt_config_.wheel_radius;
54 const double low_gear_speed =
55 velocity / dt_config_.low_gear_ratio / dt_config_.wheel_radius;
Austin Schuh96ce8ae2015-11-26 12:46:02 -080056
Comran Morshed2091fe52016-02-19 17:27:21 +000057 if (shifter_position < hall_effect.clear_low) {
58 // We're in low gear, so return speed for that gear.
59 return low_gear_speed;
60 } else if (shifter_position > hall_effect.clear_high) {
61 // We're in high gear, so return speed for that gear.
62 return high_gear_speed;
63 }
64
65 // Not in gear, so speed-match to destination gear.
66 switch (gear) {
67 case HIGH:
68 case SHIFTING_UP:
69 return high_gear_speed;
70 case LOW:
71 case SHIFTING_DOWN:
Austin Schuh746fc6d2016-02-21 02:53:33 -080072 default:
Comran Morshed2091fe52016-02-19 17:27:21 +000073 return low_gear_speed;
74 break;
Austin Schuh96ce8ae2015-11-26 12:46:02 -080075 }
76}
77
Comran Morshed5323ecb2015-12-26 20:50:55 +000078PolyDrivetrain::Gear PolyDrivetrain::UpdateSingleGear(Gear requested_gear,
79 Gear current_gear) {
Austin Schuhb9d5e8e2015-12-27 14:08:47 -080080 const Gear shift_up =
Comran Morshed5323ecb2015-12-26 20:50:55 +000081 (dt_config_.shifter_type == ShifterType::HALL_EFFECT_SHIFTER)
82 ? SHIFTING_UP
83 : HIGH;
Austin Schuhb9d5e8e2015-12-27 14:08:47 -080084 const Gear shift_down =
Comran Morshed5323ecb2015-12-26 20:50:55 +000085 (dt_config_.shifter_type == ShifterType::HALL_EFFECT_SHIFTER)
86 ? SHIFTING_DOWN
87 : LOW;
Austin Schuhb9d5e8e2015-12-27 14:08:47 -080088 if (current_gear != requested_gear) {
89 if (IsInGear(current_gear)) {
90 if (requested_gear == HIGH) {
91 if (current_gear != HIGH) {
92 current_gear = shift_up;
93 }
94 } else {
95 if (current_gear != LOW) {
96 current_gear = shift_down;
97 }
98 }
99 } else {
100 if (requested_gear == HIGH && current_gear == SHIFTING_DOWN) {
101 current_gear = SHIFTING_UP;
102 } else if (requested_gear == LOW && current_gear == SHIFTING_UP) {
103 current_gear = SHIFTING_DOWN;
104 }
105 }
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800106 }
Austin Schuhb9d5e8e2015-12-27 14:08:47 -0800107 return current_gear;
108}
109
110void PolyDrivetrain::UpdateGears(Gear requested_gear) {
111 left_gear_ = UpdateSingleGear(requested_gear, left_gear_);
112 right_gear_ = UpdateSingleGear(requested_gear, right_gear_);
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800113}
114
115void PolyDrivetrain::SetGoal(double wheel, double throttle, bool quickturn,
116 bool highgear) {
117 const double kWheelNonLinearity = 0.3;
118 // Apply a sin function that's scaled to make it feel better.
119 const double angular_range = M_PI_2 * kWheelNonLinearity;
120
Comran Morshed5323ecb2015-12-26 20:50:55 +0000121 if (dt_config_.shifter_type == ShifterType::SIMPLE_SHIFTER) {
122 // Force the right controller for simple shifters since we assume that
123 // gear switching is instantaneous.
124 if (highgear) {
125 loop_->set_controller_index(3);
126 } else {
127 loop_->set_controller_index(0);
128 }
129 }
130
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800131 wheel_ = sin(angular_range * wheel) / sin(angular_range);
132 wheel_ = sin(angular_range * wheel_) / sin(angular_range);
133 quickturn_ = quickturn;
134
135 static const double kThrottleDeadband = 0.05;
136 if (::std::abs(throttle) < kThrottleDeadband) {
137 throttle_ = 0;
138 } else {
139 throttle_ = copysign(
140 (::std::abs(throttle) - kThrottleDeadband) / (1.0 - kThrottleDeadband),
141 throttle);
142 }
143
Austin Schuhb9d5e8e2015-12-27 14:08:47 -0800144 UpdateGears(highgear ? HIGH : LOW);
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800145}
Austin Schuhb9d5e8e2015-12-27 14:08:47 -0800146
Austin Schuh6197a182015-11-28 16:04:40 -0800147void PolyDrivetrain::SetPosition(
Comran Morshed5323ecb2015-12-26 20:50:55 +0000148 const ::frc971::control_loops::DrivetrainQueue::Position *position) {
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800149 if (position == NULL) {
150 ++stale_count_;
151 } else {
152 last_position_ = position_;
153 position_ = *position;
Comran Morshed5323ecb2015-12-26 20:50:55 +0000154 position_time_delta_ = (stale_count_ + 1) * dt_config_.dt;
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800155 stale_count_ = 0;
156 }
157
Comran Morshed5323ecb2015-12-26 20:50:55 +0000158 if (dt_config_.shifter_type == ShifterType::HALL_EFFECT_SHIFTER && position) {
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800159 GearLogging gear_logging;
160 // Switch to the correct controller.
161 const double left_middle_shifter_position =
Comran Morshed5323ecb2015-12-26 20:50:55 +0000162 (dt_config_.left_drive.clear_high + dt_config_.left_drive.clear_low) /
163 2.0;
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800164 const double right_middle_shifter_position =
Comran Morshed5323ecb2015-12-26 20:50:55 +0000165 (dt_config_.right_drive.clear_high + dt_config_.right_drive.clear_low) /
166 2.0;
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800167
168 if (position->left_shifter_position < left_middle_shifter_position ||
169 left_gear_ == LOW) {
170 if (position->right_shifter_position < right_middle_shifter_position ||
171 right_gear_ == LOW) {
172 gear_logging.left_loop_high = false;
173 gear_logging.right_loop_high = false;
174 loop_->set_controller_index(gear_logging.controller_index = 0);
175 } else {
176 gear_logging.left_loop_high = false;
177 gear_logging.right_loop_high = true;
178 loop_->set_controller_index(gear_logging.controller_index = 1);
179 }
180 } else {
181 if (position->right_shifter_position < right_middle_shifter_position ||
182 right_gear_ == LOW) {
183 gear_logging.left_loop_high = true;
184 gear_logging.right_loop_high = false;
185 loop_->set_controller_index(gear_logging.controller_index = 2);
186 } else {
187 gear_logging.left_loop_high = true;
188 gear_logging.right_loop_high = true;
189 loop_->set_controller_index(gear_logging.controller_index = 3);
190 }
191 }
192
Comran Morshed5323ecb2015-12-26 20:50:55 +0000193 if (position->left_shifter_position > dt_config_.left_drive.clear_high &&
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800194 left_gear_ == SHIFTING_UP) {
195 left_gear_ = HIGH;
196 }
Comran Morshed5323ecb2015-12-26 20:50:55 +0000197 if (position->left_shifter_position < dt_config_.left_drive.clear_low &&
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800198 left_gear_ == SHIFTING_DOWN) {
199 left_gear_ = LOW;
200 }
Comran Morshed5323ecb2015-12-26 20:50:55 +0000201 if (position->right_shifter_position > dt_config_.right_drive.clear_high &&
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800202 right_gear_ == SHIFTING_UP) {
203 right_gear_ = HIGH;
204 }
Comran Morshed5323ecb2015-12-26 20:50:55 +0000205 if (position->right_shifter_position < dt_config_.right_drive.clear_low &&
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800206 right_gear_ == SHIFTING_DOWN) {
207 right_gear_ = LOW;
208 }
209
210 gear_logging.left_state = left_gear_;
211 gear_logging.right_state = right_gear_;
212 LOG_STRUCT(DEBUG, "state", gear_logging);
213 }
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800214}
215
Austin Schuh41565602016-02-28 20:10:49 -0800216double PolyDrivetrain::FilterVelocity(double throttle) const {
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800217 const Eigen::Matrix<double, 2, 2> FF =
218 loop_->B().inverse() *
219 (Eigen::Matrix<double, 2, 2>::Identity() - loop_->A());
220
221 constexpr int kHighGearController = 3;
222 const Eigen::Matrix<double, 2, 2> FF_high =
223 loop_->controller(kHighGearController).plant.B().inverse() *
224 (Eigen::Matrix<double, 2, 2>::Identity() -
225 loop_->controller(kHighGearController).plant.A());
226
227 ::Eigen::Matrix<double, 1, 2> FF_sum = FF.colwise().sum();
228 int min_FF_sum_index;
229 const double min_FF_sum = FF_sum.minCoeff(&min_FF_sum_index);
230 const double min_K_sum = loop_->K().col(min_FF_sum_index).sum();
231 const double high_min_FF_sum = FF_high.col(0).sum();
232
233 const double adjusted_ff_voltage =
234 ::aos::Clip(throttle * 12.0 * min_FF_sum / high_min_FF_sum, -12.0, 12.0);
235 return (adjusted_ff_voltage +
236 ttrust_ * min_K_sum * (loop_->X_hat(0, 0) + loop_->X_hat(1, 0)) /
237 2.0) /
238 (ttrust_ * min_K_sum + min_FF_sum);
239}
240
241double PolyDrivetrain::MaxVelocity() {
242 const Eigen::Matrix<double, 2, 2> FF =
243 loop_->B().inverse() *
244 (Eigen::Matrix<double, 2, 2>::Identity() - loop_->A());
245
246 constexpr int kHighGearController = 3;
247 const Eigen::Matrix<double, 2, 2> FF_high =
248 loop_->controller(kHighGearController).plant.B().inverse() *
249 (Eigen::Matrix<double, 2, 2>::Identity() -
250 loop_->controller(kHighGearController).plant.A());
251
252 ::Eigen::Matrix<double, 1, 2> FF_sum = FF.colwise().sum();
253 int min_FF_sum_index;
254 const double min_FF_sum = FF_sum.minCoeff(&min_FF_sum_index);
255 // const double min_K_sum = loop_->K().col(min_FF_sum_index).sum();
256 const double high_min_FF_sum = FF_high.col(0).sum();
257
258 const double adjusted_ff_voltage =
259 ::aos::Clip(12.0 * min_FF_sum / high_min_FF_sum, -12.0, 12.0);
260 return adjusted_ff_voltage / min_FF_sum;
261}
262
263void PolyDrivetrain::Update() {
Comran Morshed76ca8f52016-02-21 17:26:28 +0000264 if (dt_config_.loop_type == LoopType::CLOSED_LOOP) {
Austin Schuh41565602016-02-28 20:10:49 -0800265 loop_->mutable_X_hat()(0, 0) = kf_->X_hat()(1, 0);
266 loop_->mutable_X_hat()(1, 0) = kf_->X_hat()(3, 0);
Comran Morshed76ca8f52016-02-21 17:26:28 +0000267 }
Austin Schuh0520cbf2016-01-06 19:58:36 -0800268
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800269 // TODO(austin): Observer for the current velocity instead of difference
270 // calculations.
271 ++counter_;
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800272
Austin Schuhb9d5e8e2015-12-27 14:08:47 -0800273 if (IsInGear(left_gear_) && IsInGear(right_gear_)) {
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800274 // FF * X = U (steady state)
275 const Eigen::Matrix<double, 2, 2> FF =
276 loop_->B().inverse() *
277 (Eigen::Matrix<double, 2, 2>::Identity() - loop_->A());
278
279 // Invert the plant to figure out how the velocity filter would have to
280 // work
281 // out in order to filter out the forwards negative inertia.
282 // This math assumes that the left and right power and velocity are
283 // equals,
284 // and that the plant is the same on the left and right.
285 const double fvel = FilterVelocity(throttle_);
286
287 const double sign_svel = wheel_ * ((fvel > 0.0) ? 1.0 : -1.0);
288 double steering_velocity;
289 if (quickturn_) {
290 steering_velocity = wheel_ * MaxVelocity();
291 } else {
292 steering_velocity = ::std::abs(fvel) * wheel_;
293 }
294 const double left_velocity = fvel - steering_velocity;
295 const double right_velocity = fvel + steering_velocity;
Austin Schuh41565602016-02-28 20:10:49 -0800296 goal_left_velocity_ = left_velocity;
297 goal_right_velocity_ = right_velocity;
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800298
299 // Integrate velocity to get the position.
300 // This position is used to get integral control.
301 loop_->mutable_R() << left_velocity, right_velocity;
302
303 if (!quickturn_) {
304 // K * R = w
305 Eigen::Matrix<double, 1, 2> equality_k;
306 equality_k << 1 + sign_svel, -(1 - sign_svel);
307 const double equality_w = 0.0;
308
309 // Construct a constraint on R by manipulating the constraint on U
310 ::aos::controls::HPolytope<2> R_poly = ::aos::controls::HPolytope<2>(
311 U_Poly_.H() * (loop_->K() + FF),
312 U_Poly_.k() + U_Poly_.H() * loop_->K() * loop_->X_hat());
313
314 // Limit R back inside the box.
315 loop_->mutable_R() =
316 CoerceGoal(R_poly, equality_k, equality_w, loop_->R());
317 }
318
319 const Eigen::Matrix<double, 2, 1> FF_volts = FF * loop_->R();
320 const Eigen::Matrix<double, 2, 1> U_ideal =
321 loop_->K() * (loop_->R() - loop_->X_hat()) + FF_volts;
322
323 for (int i = 0; i < 2; i++) {
324 loop_->mutable_U()[i] = ::aos::Clip(U_ideal[i], -12, 12);
325 }
Comran Morshed76ca8f52016-02-21 17:26:28 +0000326
327 if (dt_config_.loop_type == LoopType::OPEN_LOOP) {
328 loop_->mutable_X_hat() =
329 loop_->A() * loop_->X_hat() + loop_->B() * loop_->U();
330 }
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800331 } else {
Comran Morshed5323ecb2015-12-26 20:50:55 +0000332 const double current_left_velocity =
333 (position_.left_encoder - last_position_.left_encoder) /
334 position_time_delta_;
335 const double current_right_velocity =
336 (position_.right_encoder - last_position_.right_encoder) /
337 position_time_delta_;
338 const double left_motor_speed =
339 MotorSpeed(dt_config_.left_drive, position_.left_shifter_position,
Comran Morshed2091fe52016-02-19 17:27:21 +0000340 current_left_velocity, left_gear_);
Comran Morshed5323ecb2015-12-26 20:50:55 +0000341 const double right_motor_speed =
342 MotorSpeed(dt_config_.right_drive, position_.right_shifter_position,
Comran Morshed2091fe52016-02-19 17:27:21 +0000343 current_right_velocity, right_gear_);
Comran Morshed5323ecb2015-12-26 20:50:55 +0000344
345 {
346 CIMLogging logging;
347
348 // Reset the CIM model to the current conditions to be ready for when we
349 // shift.
Comran Morshed2091fe52016-02-19 17:27:21 +0000350 logging.left_in_gear = IsInGear(left_gear_);
Comran Morshed5323ecb2015-12-26 20:50:55 +0000351 logging.left_motor_speed = left_motor_speed;
352 logging.left_velocity = current_left_velocity;
Comran Morshed2091fe52016-02-19 17:27:21 +0000353
354 logging.right_in_gear = IsInGear(right_gear_);
Comran Morshed5323ecb2015-12-26 20:50:55 +0000355 logging.right_motor_speed = right_motor_speed;
356 logging.right_velocity = current_right_velocity;
357
358 LOG_STRUCT(DEBUG, "currently", logging);
359 }
Austin Schuh41565602016-02-28 20:10:49 -0800360 goal_left_velocity_ = current_left_velocity;
361 goal_right_velocity_ = current_right_velocity;
Comran Morshed5323ecb2015-12-26 20:50:55 +0000362
Comran Morshed2091fe52016-02-19 17:27:21 +0000363 // Any motor is not in gear. Speed match.
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800364 ::Eigen::Matrix<double, 1, 1> R_left;
365 ::Eigen::Matrix<double, 1, 1> R_right;
366 R_left(0, 0) = left_motor_speed;
367 R_right(0, 0) = right_motor_speed;
368
369 const double wiggle =
370 (static_cast<double>((counter_ % 20) / 10) - 0.5) * 5.0;
371
372 loop_->mutable_U(0, 0) = ::aos::Clip(
Comran Morshed5323ecb2015-12-26 20:50:55 +0000373 (R_left / dt_config_.v)(0, 0) + (IsInGear(left_gear_) ? 0 : wiggle),
374 -12.0, 12.0);
375 loop_->mutable_U(1, 0) = ::aos::Clip(
376 (R_right / dt_config_.v)(0, 0) + (IsInGear(right_gear_) ? 0 : wiggle),
377 -12.0, 12.0);
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800378 loop_->mutable_U() *= 12.0 / ::aos::robot_state->voltage_battery;
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800379 }
380}
381
Austin Schuh6197a182015-11-28 16:04:40 -0800382void PolyDrivetrain::SendMotors(
Comran Morshed5323ecb2015-12-26 20:50:55 +0000383 ::frc971::control_loops::DrivetrainQueue::Output *output) {
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800384 if (output != NULL) {
385 output->left_voltage = loop_->U(0, 0);
386 output->right_voltage = loop_->U(1, 0);
387 output->left_high = left_gear_ == HIGH || left_gear_ == SHIFTING_UP;
388 output->right_high = right_gear_ == HIGH || right_gear_ == SHIFTING_UP;
389 }
390}
391
Austin Schuh41565602016-02-28 20:10:49 -0800392void PolyDrivetrain::PopulateStatus(
393 ::frc971::control_loops::DrivetrainQueue::Status *status) {
394 status->left_velocity_goal = goal_left_velocity_;
395 status->right_velocity_goal = goal_right_velocity_;
396}
397
Austin Schuh6197a182015-11-28 16:04:40 -0800398} // namespace drivetrain
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800399} // namespace control_loops
Comran Morshed5323ecb2015-12-26 20:50:55 +0000400} // namespace frc971