blob: 014a1aef074c0c79db6911106ccafc4b12f02b56 [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
Austin Schuh6197a182015-11-28 16:04:40 -080018namespace y2014 {
Austin Schuh96ce8ae2015-11-26 12:46:02 -080019namespace control_loops {
Austin Schuh6197a182015-11-28 16:04:40 -080020namespace drivetrain {
Austin Schuh96ce8ae2015-11-26 12:46:02 -080021
Brian Silvermanb601d892015-12-20 18:24:38 -050022using ::y2014::control_loops::GearLogging;
23using ::y2014::control_loops::CIMLogging;
Austin Schuh6197a182015-11-28 16:04:40 -080024using ::frc971::control_loops::CoerceGoal;
Austin Schuh96ce8ae2015-11-26 12:46:02 -080025
Austin Schuh6613a072016-01-06 19:54:48 -080026PolyDrivetrain::PolyDrivetrain(StateFeedbackLoop<7, 2, 3> *kf)
27 : kf_(kf),
28 U_Poly_((Eigen::Matrix<double, 4, 2>() << /*[[*/ 1, 0 /*]*/,
Austin Schuh96ce8ae2015-11-26 12:46:02 -080029 /*[*/ -1, 0 /*]*/,
30 /*[*/ 0, 1 /*]*/,
31 /*[*/ 0, -1 /*]]*/).finished(),
32 (Eigen::Matrix<double, 4, 1>() << /*[[*/ 12 /*]*/,
33 /*[*/ 12 /*]*/,
34 /*[*/ 12 /*]*/,
35 /*[*/ 12 /*]]*/).finished()),
36 loop_(new StateFeedbackLoop<2, 2, 2>(
37 constants::GetValues().make_v_drivetrain_loop())),
38 ttrust_(1.1),
39 wheel_(0.0),
40 throttle_(0.0),
41 quickturn_(false),
42 stale_count_(0),
43 position_time_delta_(kDt),
44 left_gear_(LOW),
45 right_gear_(LOW),
46 counter_(0) {
47 last_position_.Zero();
48 position_.Zero();
49}
50
51double PolyDrivetrain::MotorSpeed(
52 const constants::ShifterHallEffect &hall_effect, double shifter_position,
53 double velocity) {
Austin Schuh96ce8ae2015-11-26 12:46:02 -080054 const double avg_hall_effect =
55 (hall_effect.clear_high + hall_effect.clear_low) / 2.0;
56
57 if (shifter_position > avg_hall_effect) {
58 return velocity / constants::GetValues().high_gear_ratio / kWheelRadius;
59 } else {
60 return velocity / constants::GetValues().low_gear_ratio / kWheelRadius;
61 }
62}
63
Austin Schuhb9d5e8e2015-12-27 14:08:47 -080064PolyDrivetrain::Gear PolyDrivetrain::UpdateSingleGear(
65 Gear requested_gear, Gear current_gear) {
66 const Gear shift_up =
67 constants::GetValues().clutch_transmission ? HIGH : SHIFTING_UP;
68 const Gear shift_down =
69 constants::GetValues().clutch_transmission ? LOW : SHIFTING_DOWN;
Austin Schuh96ce8ae2015-11-26 12:46:02 -080070
Austin Schuhb9d5e8e2015-12-27 14:08:47 -080071 if (current_gear != requested_gear) {
72 if (IsInGear(current_gear)) {
73 if (requested_gear == HIGH) {
74 if (current_gear != HIGH) {
75 current_gear = shift_up;
76 }
77 } else {
78 if (current_gear != LOW) {
79 current_gear = shift_down;
80 }
81 }
82 } else {
83 if (requested_gear == HIGH && current_gear == SHIFTING_DOWN) {
84 current_gear = SHIFTING_UP;
85 } else if (requested_gear == LOW && current_gear == SHIFTING_UP) {
86 current_gear = SHIFTING_DOWN;
87 }
88 }
Austin Schuh96ce8ae2015-11-26 12:46:02 -080089 }
Austin Schuhb9d5e8e2015-12-27 14:08:47 -080090 return current_gear;
91}
92
93void PolyDrivetrain::UpdateGears(Gear requested_gear) {
94 left_gear_ = UpdateSingleGear(requested_gear, left_gear_);
95 right_gear_ = UpdateSingleGear(requested_gear, right_gear_);
Austin Schuh96ce8ae2015-11-26 12:46:02 -080096}
97
98void PolyDrivetrain::SetGoal(double wheel, double throttle, bool quickturn,
99 bool highgear) {
100 const double kWheelNonLinearity = 0.3;
101 // Apply a sin function that's scaled to make it feel better.
102 const double angular_range = M_PI_2 * kWheelNonLinearity;
103
104 wheel_ = sin(angular_range * wheel) / sin(angular_range);
105 wheel_ = sin(angular_range * wheel_) / sin(angular_range);
106 quickturn_ = quickturn;
107
108 static const double kThrottleDeadband = 0.05;
109 if (::std::abs(throttle) < kThrottleDeadband) {
110 throttle_ = 0;
111 } else {
112 throttle_ = copysign(
113 (::std::abs(throttle) - kThrottleDeadband) / (1.0 - kThrottleDeadband),
114 throttle);
115 }
116
Austin Schuhb9d5e8e2015-12-27 14:08:47 -0800117 UpdateGears(highgear ? HIGH : LOW);
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800118}
Austin Schuhb9d5e8e2015-12-27 14:08:47 -0800119
Austin Schuh6197a182015-11-28 16:04:40 -0800120void PolyDrivetrain::SetPosition(
Brian Silvermanb601d892015-12-20 18:24:38 -0500121 const ::y2014::control_loops::DrivetrainQueue::Position *position) {
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800122 if (position == NULL) {
123 ++stale_count_;
124 } else {
125 last_position_ = position_;
126 position_ = *position;
127 position_time_delta_ = (stale_count_ + 1) * kDt;
128 stale_count_ = 0;
129 }
130
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800131 if (position) {
Austin Schuhb9d5e8e2015-12-27 14:08:47 -0800132 const auto &values = constants::GetValues();
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800133 GearLogging gear_logging;
134 // Switch to the correct controller.
135 const double left_middle_shifter_position =
136 (values.left_drive.clear_high + values.left_drive.clear_low) / 2.0;
137 const double right_middle_shifter_position =
138 (values.right_drive.clear_high + values.right_drive.clear_low) / 2.0;
139
140 if (position->left_shifter_position < left_middle_shifter_position ||
141 left_gear_ == LOW) {
142 if (position->right_shifter_position < right_middle_shifter_position ||
143 right_gear_ == LOW) {
144 gear_logging.left_loop_high = false;
145 gear_logging.right_loop_high = false;
146 loop_->set_controller_index(gear_logging.controller_index = 0);
147 } else {
148 gear_logging.left_loop_high = false;
149 gear_logging.right_loop_high = true;
150 loop_->set_controller_index(gear_logging.controller_index = 1);
151 }
152 } else {
153 if (position->right_shifter_position < right_middle_shifter_position ||
154 right_gear_ == LOW) {
155 gear_logging.left_loop_high = true;
156 gear_logging.right_loop_high = false;
157 loop_->set_controller_index(gear_logging.controller_index = 2);
158 } else {
159 gear_logging.left_loop_high = true;
160 gear_logging.right_loop_high = true;
161 loop_->set_controller_index(gear_logging.controller_index = 3);
162 }
163 }
164
165 if (position->left_shifter_position > values.left_drive.clear_high &&
166 left_gear_ == SHIFTING_UP) {
167 left_gear_ = HIGH;
168 }
169 if (position->left_shifter_position < values.left_drive.clear_low &&
170 left_gear_ == SHIFTING_DOWN) {
171 left_gear_ = LOW;
172 }
173 if (position->right_shifter_position > values.right_drive.clear_high &&
174 right_gear_ == SHIFTING_UP) {
175 right_gear_ = HIGH;
176 }
177 if (position->right_shifter_position < values.right_drive.clear_low &&
178 right_gear_ == SHIFTING_DOWN) {
179 right_gear_ = LOW;
180 }
181
182 gear_logging.left_state = left_gear_;
183 gear_logging.right_state = right_gear_;
184 LOG_STRUCT(DEBUG, "state", gear_logging);
185 }
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800186}
187
188double PolyDrivetrain::FilterVelocity(double throttle) {
189 const Eigen::Matrix<double, 2, 2> FF =
190 loop_->B().inverse() *
191 (Eigen::Matrix<double, 2, 2>::Identity() - loop_->A());
192
193 constexpr int kHighGearController = 3;
194 const Eigen::Matrix<double, 2, 2> FF_high =
195 loop_->controller(kHighGearController).plant.B().inverse() *
196 (Eigen::Matrix<double, 2, 2>::Identity() -
197 loop_->controller(kHighGearController).plant.A());
198
199 ::Eigen::Matrix<double, 1, 2> FF_sum = FF.colwise().sum();
200 int min_FF_sum_index;
201 const double min_FF_sum = FF_sum.minCoeff(&min_FF_sum_index);
202 const double min_K_sum = loop_->K().col(min_FF_sum_index).sum();
203 const double high_min_FF_sum = FF_high.col(0).sum();
204
205 const double adjusted_ff_voltage =
206 ::aos::Clip(throttle * 12.0 * min_FF_sum / high_min_FF_sum, -12.0, 12.0);
207 return (adjusted_ff_voltage +
208 ttrust_ * min_K_sum * (loop_->X_hat(0, 0) + loop_->X_hat(1, 0)) /
209 2.0) /
210 (ttrust_ * min_K_sum + min_FF_sum);
211}
212
213double PolyDrivetrain::MaxVelocity() {
214 const Eigen::Matrix<double, 2, 2> FF =
215 loop_->B().inverse() *
216 (Eigen::Matrix<double, 2, 2>::Identity() - loop_->A());
217
218 constexpr int kHighGearController = 3;
219 const Eigen::Matrix<double, 2, 2> FF_high =
220 loop_->controller(kHighGearController).plant.B().inverse() *
221 (Eigen::Matrix<double, 2, 2>::Identity() -
222 loop_->controller(kHighGearController).plant.A());
223
224 ::Eigen::Matrix<double, 1, 2> FF_sum = FF.colwise().sum();
225 int min_FF_sum_index;
226 const double min_FF_sum = FF_sum.minCoeff(&min_FF_sum_index);
227 // const double min_K_sum = loop_->K().col(min_FF_sum_index).sum();
228 const double high_min_FF_sum = FF_high.col(0).sum();
229
230 const double adjusted_ff_voltage =
231 ::aos::Clip(12.0 * min_FF_sum / high_min_FF_sum, -12.0, 12.0);
232 return adjusted_ff_voltage / min_FF_sum;
233}
234
235void PolyDrivetrain::Update() {
Austin Schuh0520cbf2016-01-06 19:58:36 -0800236 loop_->mutable_X_hat()(0, 0) = kf_->X_hat()(1, 0);
237 loop_->mutable_X_hat()(1, 0) = kf_->X_hat()(3, 0);
238
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800239 const auto &values = constants::GetValues();
240 // TODO(austin): Observer for the current velocity instead of difference
241 // calculations.
242 ++counter_;
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800243 const double current_left_velocity =
244 (position_.left_encoder - last_position_.left_encoder) /
245 position_time_delta_;
246 const double current_right_velocity =
247 (position_.right_encoder - last_position_.right_encoder) /
248 position_time_delta_;
249 const double left_motor_speed =
250 MotorSpeed(values.left_drive, position_.left_shifter_position,
251 current_left_velocity);
252 const double right_motor_speed =
253 MotorSpeed(values.right_drive, position_.right_shifter_position,
254 current_right_velocity);
255
256 {
257 CIMLogging logging;
258
259 // Reset the CIM model to the current conditions to be ready for when we
260 // shift.
261 if (IsInGear(left_gear_)) {
262 logging.left_in_gear = true;
263 } else {
264 logging.left_in_gear = false;
265 }
266 logging.left_motor_speed = left_motor_speed;
267 logging.left_velocity = current_left_velocity;
268 if (IsInGear(right_gear_)) {
269 logging.right_in_gear = true;
270 } else {
271 logging.right_in_gear = false;
272 }
273 logging.right_motor_speed = right_motor_speed;
274 logging.right_velocity = current_right_velocity;
275
276 LOG_STRUCT(DEBUG, "currently", logging);
277 }
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800278
Austin Schuhb9d5e8e2015-12-27 14:08:47 -0800279 if (IsInGear(left_gear_) && IsInGear(right_gear_)) {
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800280 // FF * X = U (steady state)
281 const Eigen::Matrix<double, 2, 2> FF =
282 loop_->B().inverse() *
283 (Eigen::Matrix<double, 2, 2>::Identity() - loop_->A());
284
285 // Invert the plant to figure out how the velocity filter would have to
286 // work
287 // out in order to filter out the forwards negative inertia.
288 // This math assumes that the left and right power and velocity are
289 // equals,
290 // and that the plant is the same on the left and right.
291 const double fvel = FilterVelocity(throttle_);
292
293 const double sign_svel = wheel_ * ((fvel > 0.0) ? 1.0 : -1.0);
294 double steering_velocity;
295 if (quickturn_) {
296 steering_velocity = wheel_ * MaxVelocity();
297 } else {
298 steering_velocity = ::std::abs(fvel) * wheel_;
299 }
300 const double left_velocity = fvel - steering_velocity;
301 const double right_velocity = fvel + steering_velocity;
302
303 // Integrate velocity to get the position.
304 // This position is used to get integral control.
305 loop_->mutable_R() << left_velocity, right_velocity;
306
307 if (!quickturn_) {
308 // K * R = w
309 Eigen::Matrix<double, 1, 2> equality_k;
310 equality_k << 1 + sign_svel, -(1 - sign_svel);
311 const double equality_w = 0.0;
312
313 // Construct a constraint on R by manipulating the constraint on U
314 ::aos::controls::HPolytope<2> R_poly = ::aos::controls::HPolytope<2>(
315 U_Poly_.H() * (loop_->K() + FF),
316 U_Poly_.k() + U_Poly_.H() * loop_->K() * loop_->X_hat());
317
318 // Limit R back inside the box.
319 loop_->mutable_R() =
320 CoerceGoal(R_poly, equality_k, equality_w, loop_->R());
321 }
322
323 const Eigen::Matrix<double, 2, 1> FF_volts = FF * loop_->R();
324 const Eigen::Matrix<double, 2, 1> U_ideal =
325 loop_->K() * (loop_->R() - loop_->X_hat()) + FF_volts;
326
327 for (int i = 0; i < 2; i++) {
328 loop_->mutable_U()[i] = ::aos::Clip(U_ideal[i], -12, 12);
329 }
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800330 } else {
331 // Any motor is not in gear. Speed match.
332 ::Eigen::Matrix<double, 1, 1> R_left;
333 ::Eigen::Matrix<double, 1, 1> R_right;
334 R_left(0, 0) = left_motor_speed;
335 R_right(0, 0) = right_motor_speed;
336
337 const double wiggle =
338 (static_cast<double>((counter_ % 20) / 10) - 0.5) * 5.0;
339
340 loop_->mutable_U(0, 0) = ::aos::Clip(
341 (R_left / Kv)(0, 0) + (IsInGear(left_gear_) ? 0 : wiggle), -12.0, 12.0);
342 loop_->mutable_U(1, 0) =
343 ::aos::Clip((R_right / Kv)(0, 0) + (IsInGear(right_gear_) ? 0 : wiggle),
344 -12.0, 12.0);
345 loop_->mutable_U() *= 12.0 / ::aos::robot_state->voltage_battery;
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800346 }
347}
348
Austin Schuh6197a182015-11-28 16:04:40 -0800349void PolyDrivetrain::SendMotors(
Brian Silvermanb601d892015-12-20 18:24:38 -0500350 ::y2014::control_loops::DrivetrainQueue::Output *output) {
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800351 if (output != NULL) {
352 output->left_voltage = loop_->U(0, 0);
353 output->right_voltage = loop_->U(1, 0);
354 output->left_high = left_gear_ == HIGH || left_gear_ == SHIFTING_UP;
355 output->right_high = right_gear_ == HIGH || right_gear_ == SHIFTING_UP;
356 }
357}
358
359constexpr double PolyDrivetrain::kStallTorque;
360constexpr double PolyDrivetrain::kStallCurrent;
361constexpr double PolyDrivetrain::kFreeSpeed;
362constexpr double PolyDrivetrain::kFreeCurrent;
363constexpr double PolyDrivetrain::kWheelRadius;
364constexpr double PolyDrivetrain::kR;
365constexpr double PolyDrivetrain::Kv;
366constexpr double PolyDrivetrain::Kt;
367
Austin Schuh6197a182015-11-28 16:04:40 -0800368} // namespace drivetrain
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800369} // namespace control_loops
Austin Schuh6197a182015-11-28 16:04:40 -0800370} // namespace y2014