blob: f088bff860743009a6fdd02c5042d778aa81a8b0 [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) {
Austin Schuh1dbee482016-02-28 20:12:24 -0800117 const double kWheelNonLinearity = 0.4;
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800118 // 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);
Austin Schuh1dbee482016-02-28 20:12:24 -0800133 wheel_ = 2.0 * wheel - wheel_;
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800134 quickturn_ = quickturn;
135
136 static const double kThrottleDeadband = 0.05;
137 if (::std::abs(throttle) < kThrottleDeadband) {
138 throttle_ = 0;
139 } else {
140 throttle_ = copysign(
141 (::std::abs(throttle) - kThrottleDeadband) / (1.0 - kThrottleDeadband),
142 throttle);
143 }
144
Austin Schuhb9d5e8e2015-12-27 14:08:47 -0800145 UpdateGears(highgear ? HIGH : LOW);
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800146}
Austin Schuhb9d5e8e2015-12-27 14:08:47 -0800147
Austin Schuh6197a182015-11-28 16:04:40 -0800148void PolyDrivetrain::SetPosition(
Comran Morshed5323ecb2015-12-26 20:50:55 +0000149 const ::frc971::control_loops::DrivetrainQueue::Position *position) {
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800150 if (position == NULL) {
151 ++stale_count_;
152 } else {
153 last_position_ = position_;
154 position_ = *position;
Comran Morshed5323ecb2015-12-26 20:50:55 +0000155 position_time_delta_ = (stale_count_ + 1) * dt_config_.dt;
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800156 stale_count_ = 0;
157 }
158
Comran Morshed5323ecb2015-12-26 20:50:55 +0000159 if (dt_config_.shifter_type == ShifterType::HALL_EFFECT_SHIFTER && position) {
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800160 GearLogging gear_logging;
161 // Switch to the correct controller.
162 const double left_middle_shifter_position =
Comran Morshed5323ecb2015-12-26 20:50:55 +0000163 (dt_config_.left_drive.clear_high + dt_config_.left_drive.clear_low) /
164 2.0;
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800165 const double right_middle_shifter_position =
Comran Morshed5323ecb2015-12-26 20:50:55 +0000166 (dt_config_.right_drive.clear_high + dt_config_.right_drive.clear_low) /
167 2.0;
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800168
169 if (position->left_shifter_position < left_middle_shifter_position ||
170 left_gear_ == LOW) {
171 if (position->right_shifter_position < right_middle_shifter_position ||
172 right_gear_ == LOW) {
173 gear_logging.left_loop_high = false;
174 gear_logging.right_loop_high = false;
175 loop_->set_controller_index(gear_logging.controller_index = 0);
176 } else {
177 gear_logging.left_loop_high = false;
178 gear_logging.right_loop_high = true;
179 loop_->set_controller_index(gear_logging.controller_index = 1);
180 }
181 } else {
182 if (position->right_shifter_position < right_middle_shifter_position ||
183 right_gear_ == LOW) {
184 gear_logging.left_loop_high = true;
185 gear_logging.right_loop_high = false;
186 loop_->set_controller_index(gear_logging.controller_index = 2);
187 } else {
188 gear_logging.left_loop_high = true;
189 gear_logging.right_loop_high = true;
190 loop_->set_controller_index(gear_logging.controller_index = 3);
191 }
192 }
193
Comran Morshed5323ecb2015-12-26 20:50:55 +0000194 if (position->left_shifter_position > dt_config_.left_drive.clear_high &&
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800195 left_gear_ == SHIFTING_UP) {
196 left_gear_ = HIGH;
197 }
Comran Morshed5323ecb2015-12-26 20:50:55 +0000198 if (position->left_shifter_position < dt_config_.left_drive.clear_low &&
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800199 left_gear_ == SHIFTING_DOWN) {
200 left_gear_ = LOW;
201 }
Comran Morshed5323ecb2015-12-26 20:50:55 +0000202 if (position->right_shifter_position > dt_config_.right_drive.clear_high &&
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800203 right_gear_ == SHIFTING_UP) {
204 right_gear_ = HIGH;
205 }
Comran Morshed5323ecb2015-12-26 20:50:55 +0000206 if (position->right_shifter_position < dt_config_.right_drive.clear_low &&
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800207 right_gear_ == SHIFTING_DOWN) {
208 right_gear_ = LOW;
209 }
210
211 gear_logging.left_state = left_gear_;
212 gear_logging.right_state = right_gear_;
213 LOG_STRUCT(DEBUG, "state", gear_logging);
214 }
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800215}
216
Austin Schuh41565602016-02-28 20:10:49 -0800217double PolyDrivetrain::FilterVelocity(double throttle) const {
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800218 const Eigen::Matrix<double, 2, 2> FF =
219 loop_->B().inverse() *
220 (Eigen::Matrix<double, 2, 2>::Identity() - loop_->A());
221
222 constexpr int kHighGearController = 3;
223 const Eigen::Matrix<double, 2, 2> FF_high =
224 loop_->controller(kHighGearController).plant.B().inverse() *
225 (Eigen::Matrix<double, 2, 2>::Identity() -
226 loop_->controller(kHighGearController).plant.A());
227
228 ::Eigen::Matrix<double, 1, 2> FF_sum = FF.colwise().sum();
229 int min_FF_sum_index;
230 const double min_FF_sum = FF_sum.minCoeff(&min_FF_sum_index);
231 const double min_K_sum = loop_->K().col(min_FF_sum_index).sum();
232 const double high_min_FF_sum = FF_high.col(0).sum();
233
234 const double adjusted_ff_voltage =
235 ::aos::Clip(throttle * 12.0 * min_FF_sum / high_min_FF_sum, -12.0, 12.0);
236 return (adjusted_ff_voltage +
237 ttrust_ * min_K_sum * (loop_->X_hat(0, 0) + loop_->X_hat(1, 0)) /
238 2.0) /
239 (ttrust_ * min_K_sum + min_FF_sum);
240}
241
242double PolyDrivetrain::MaxVelocity() {
243 const Eigen::Matrix<double, 2, 2> FF =
244 loop_->B().inverse() *
245 (Eigen::Matrix<double, 2, 2>::Identity() - loop_->A());
246
247 constexpr int kHighGearController = 3;
248 const Eigen::Matrix<double, 2, 2> FF_high =
249 loop_->controller(kHighGearController).plant.B().inverse() *
250 (Eigen::Matrix<double, 2, 2>::Identity() -
251 loop_->controller(kHighGearController).plant.A());
252
253 ::Eigen::Matrix<double, 1, 2> FF_sum = FF.colwise().sum();
254 int min_FF_sum_index;
255 const double min_FF_sum = FF_sum.minCoeff(&min_FF_sum_index);
256 // const double min_K_sum = loop_->K().col(min_FF_sum_index).sum();
257 const double high_min_FF_sum = FF_high.col(0).sum();
258
259 const double adjusted_ff_voltage =
260 ::aos::Clip(12.0 * min_FF_sum / high_min_FF_sum, -12.0, 12.0);
261 return adjusted_ff_voltage / min_FF_sum;
262}
263
264void PolyDrivetrain::Update() {
Comran Morshed76ca8f52016-02-21 17:26:28 +0000265 if (dt_config_.loop_type == LoopType::CLOSED_LOOP) {
Austin Schuh41565602016-02-28 20:10:49 -0800266 loop_->mutable_X_hat()(0, 0) = kf_->X_hat()(1, 0);
267 loop_->mutable_X_hat()(1, 0) = kf_->X_hat()(3, 0);
Comran Morshed76ca8f52016-02-21 17:26:28 +0000268 }
Austin Schuh0520cbf2016-01-06 19:58:36 -0800269
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800270 // TODO(austin): Observer for the current velocity instead of difference
271 // calculations.
272 ++counter_;
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800273
Austin Schuhb9d5e8e2015-12-27 14:08:47 -0800274 if (IsInGear(left_gear_) && IsInGear(right_gear_)) {
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800275 // FF * X = U (steady state)
276 const Eigen::Matrix<double, 2, 2> FF =
277 loop_->B().inverse() *
278 (Eigen::Matrix<double, 2, 2>::Identity() - loop_->A());
279
280 // Invert the plant to figure out how the velocity filter would have to
281 // work
282 // out in order to filter out the forwards negative inertia.
283 // This math assumes that the left and right power and velocity are
284 // equals,
285 // and that the plant is the same on the left and right.
286 const double fvel = FilterVelocity(throttle_);
287
288 const double sign_svel = wheel_ * ((fvel > 0.0) ? 1.0 : -1.0);
289 double steering_velocity;
290 if (quickturn_) {
291 steering_velocity = wheel_ * MaxVelocity();
292 } else {
293 steering_velocity = ::std::abs(fvel) * wheel_;
294 }
295 const double left_velocity = fvel - steering_velocity;
296 const double right_velocity = fvel + steering_velocity;
Austin Schuh41565602016-02-28 20:10:49 -0800297 goal_left_velocity_ = left_velocity;
298 goal_right_velocity_ = right_velocity;
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800299
300 // Integrate velocity to get the position.
301 // This position is used to get integral control.
302 loop_->mutable_R() << left_velocity, right_velocity;
303
304 if (!quickturn_) {
305 // K * R = w
306 Eigen::Matrix<double, 1, 2> equality_k;
307 equality_k << 1 + sign_svel, -(1 - sign_svel);
308 const double equality_w = 0.0;
309
310 // Construct a constraint on R by manipulating the constraint on U
311 ::aos::controls::HPolytope<2> R_poly = ::aos::controls::HPolytope<2>(
312 U_Poly_.H() * (loop_->K() + FF),
313 U_Poly_.k() + U_Poly_.H() * loop_->K() * loop_->X_hat());
314
315 // Limit R back inside the box.
316 loop_->mutable_R() =
317 CoerceGoal(R_poly, equality_k, equality_w, loop_->R());
318 }
319
320 const Eigen::Matrix<double, 2, 1> FF_volts = FF * loop_->R();
321 const Eigen::Matrix<double, 2, 1> U_ideal =
322 loop_->K() * (loop_->R() - loop_->X_hat()) + FF_volts;
323
324 for (int i = 0; i < 2; i++) {
325 loop_->mutable_U()[i] = ::aos::Clip(U_ideal[i], -12, 12);
326 }
Comran Morshed76ca8f52016-02-21 17:26:28 +0000327
328 if (dt_config_.loop_type == LoopType::OPEN_LOOP) {
329 loop_->mutable_X_hat() =
330 loop_->A() * loop_->X_hat() + loop_->B() * loop_->U();
331 }
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800332 } else {
Comran Morshed5323ecb2015-12-26 20:50:55 +0000333 const double current_left_velocity =
334 (position_.left_encoder - last_position_.left_encoder) /
335 position_time_delta_;
336 const double current_right_velocity =
337 (position_.right_encoder - last_position_.right_encoder) /
338 position_time_delta_;
339 const double left_motor_speed =
340 MotorSpeed(dt_config_.left_drive, position_.left_shifter_position,
Comran Morshed2091fe52016-02-19 17:27:21 +0000341 current_left_velocity, left_gear_);
Comran Morshed5323ecb2015-12-26 20:50:55 +0000342 const double right_motor_speed =
343 MotorSpeed(dt_config_.right_drive, position_.right_shifter_position,
Comran Morshed2091fe52016-02-19 17:27:21 +0000344 current_right_velocity, right_gear_);
Comran Morshed5323ecb2015-12-26 20:50:55 +0000345
346 {
347 CIMLogging logging;
348
349 // Reset the CIM model to the current conditions to be ready for when we
350 // shift.
Comran Morshed2091fe52016-02-19 17:27:21 +0000351 logging.left_in_gear = IsInGear(left_gear_);
Comran Morshed5323ecb2015-12-26 20:50:55 +0000352 logging.left_motor_speed = left_motor_speed;
353 logging.left_velocity = current_left_velocity;
Comran Morshed2091fe52016-02-19 17:27:21 +0000354
355 logging.right_in_gear = IsInGear(right_gear_);
Comran Morshed5323ecb2015-12-26 20:50:55 +0000356 logging.right_motor_speed = right_motor_speed;
357 logging.right_velocity = current_right_velocity;
358
359 LOG_STRUCT(DEBUG, "currently", logging);
360 }
Austin Schuh41565602016-02-28 20:10:49 -0800361 goal_left_velocity_ = current_left_velocity;
362 goal_right_velocity_ = current_right_velocity;
Comran Morshed5323ecb2015-12-26 20:50:55 +0000363
Comran Morshed2091fe52016-02-19 17:27:21 +0000364 // Any motor is not in gear. Speed match.
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800365 ::Eigen::Matrix<double, 1, 1> R_left;
366 ::Eigen::Matrix<double, 1, 1> R_right;
367 R_left(0, 0) = left_motor_speed;
368 R_right(0, 0) = right_motor_speed;
369
370 const double wiggle =
371 (static_cast<double>((counter_ % 20) / 10) - 0.5) * 5.0;
372
373 loop_->mutable_U(0, 0) = ::aos::Clip(
Comran Morshed5323ecb2015-12-26 20:50:55 +0000374 (R_left / dt_config_.v)(0, 0) + (IsInGear(left_gear_) ? 0 : wiggle),
375 -12.0, 12.0);
376 loop_->mutable_U(1, 0) = ::aos::Clip(
377 (R_right / dt_config_.v)(0, 0) + (IsInGear(right_gear_) ? 0 : wiggle),
378 -12.0, 12.0);
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800379 loop_->mutable_U() *= 12.0 / ::aos::robot_state->voltage_battery;
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800380 }
381}
382
Austin Schuh6197a182015-11-28 16:04:40 -0800383void PolyDrivetrain::SendMotors(
Comran Morshed5323ecb2015-12-26 20:50:55 +0000384 ::frc971::control_loops::DrivetrainQueue::Output *output) {
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800385 if (output != NULL) {
386 output->left_voltage = loop_->U(0, 0);
387 output->right_voltage = loop_->U(1, 0);
388 output->left_high = left_gear_ == HIGH || left_gear_ == SHIFTING_UP;
389 output->right_high = right_gear_ == HIGH || right_gear_ == SHIFTING_UP;
390 }
391}
392
Austin Schuh41565602016-02-28 20:10:49 -0800393void PolyDrivetrain::PopulateStatus(
394 ::frc971::control_loops::DrivetrainQueue::Status *status) {
395 status->left_velocity_goal = goal_left_velocity_;
396 status->right_velocity_goal = goal_right_velocity_;
397}
398
Austin Schuh6197a182015-11-28 16:04:40 -0800399} // namespace drivetrain
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800400} // namespace control_loops
Comran Morshed5323ecb2015-12-26 20:50:55 +0000401} // namespace frc971