blob: 16ca10a3c2c74cb3446078f7051c2ec736dd2480 [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
26PolyDrivetrain::PolyDrivetrain()
27 : U_Poly_((Eigen::Matrix<double, 4, 2>() << /*[[*/ 1, 0 /*]*/,
28 /*[*/ -1, 0 /*]*/,
29 /*[*/ 0, 1 /*]*/,
30 /*[*/ 0, -1 /*]]*/).finished(),
31 (Eigen::Matrix<double, 4, 1>() << /*[[*/ 12 /*]*/,
32 /*[*/ 12 /*]*/,
33 /*[*/ 12 /*]*/,
34 /*[*/ 12 /*]]*/).finished()),
35 loop_(new StateFeedbackLoop<2, 2, 2>(
36 constants::GetValues().make_v_drivetrain_loop())),
37 ttrust_(1.1),
38 wheel_(0.0),
39 throttle_(0.0),
40 quickturn_(false),
41 stale_count_(0),
42 position_time_delta_(kDt),
43 left_gear_(LOW),
44 right_gear_(LOW),
45 counter_(0) {
46 last_position_.Zero();
47 position_.Zero();
48}
49
50double PolyDrivetrain::MotorSpeed(
51 const constants::ShifterHallEffect &hall_effect, double shifter_position,
52 double velocity) {
Austin Schuh96ce8ae2015-11-26 12:46:02 -080053 const double avg_hall_effect =
54 (hall_effect.clear_high + hall_effect.clear_low) / 2.0;
55
56 if (shifter_position > avg_hall_effect) {
57 return velocity / constants::GetValues().high_gear_ratio / kWheelRadius;
58 } else {
59 return velocity / constants::GetValues().low_gear_ratio / kWheelRadius;
60 }
61}
62
Austin Schuhb9d5e8e2015-12-27 14:08:47 -080063PolyDrivetrain::Gear PolyDrivetrain::UpdateSingleGear(
64 Gear requested_gear, Gear current_gear) {
65 const Gear shift_up =
66 constants::GetValues().clutch_transmission ? HIGH : SHIFTING_UP;
67 const Gear shift_down =
68 constants::GetValues().clutch_transmission ? LOW : SHIFTING_DOWN;
Austin Schuh96ce8ae2015-11-26 12:46:02 -080069
Austin Schuhb9d5e8e2015-12-27 14:08:47 -080070 if (current_gear != requested_gear) {
71 if (IsInGear(current_gear)) {
72 if (requested_gear == HIGH) {
73 if (current_gear != HIGH) {
74 current_gear = shift_up;
75 }
76 } else {
77 if (current_gear != LOW) {
78 current_gear = shift_down;
79 }
80 }
81 } else {
82 if (requested_gear == HIGH && current_gear == SHIFTING_DOWN) {
83 current_gear = SHIFTING_UP;
84 } else if (requested_gear == LOW && current_gear == SHIFTING_UP) {
85 current_gear = SHIFTING_DOWN;
86 }
87 }
Austin Schuh96ce8ae2015-11-26 12:46:02 -080088 }
Austin Schuhb9d5e8e2015-12-27 14:08:47 -080089 return current_gear;
90}
91
92void PolyDrivetrain::UpdateGears(Gear requested_gear) {
93 left_gear_ = UpdateSingleGear(requested_gear, left_gear_);
94 right_gear_ = UpdateSingleGear(requested_gear, right_gear_);
Austin Schuh96ce8ae2015-11-26 12:46:02 -080095}
96
97void PolyDrivetrain::SetGoal(double wheel, double throttle, bool quickturn,
98 bool highgear) {
99 const double kWheelNonLinearity = 0.3;
100 // Apply a sin function that's scaled to make it feel better.
101 const double angular_range = M_PI_2 * kWheelNonLinearity;
102
103 wheel_ = sin(angular_range * wheel) / sin(angular_range);
104 wheel_ = sin(angular_range * wheel_) / sin(angular_range);
105 quickturn_ = quickturn;
106
107 static const double kThrottleDeadband = 0.05;
108 if (::std::abs(throttle) < kThrottleDeadband) {
109 throttle_ = 0;
110 } else {
111 throttle_ = copysign(
112 (::std::abs(throttle) - kThrottleDeadband) / (1.0 - kThrottleDeadband),
113 throttle);
114 }
115
Austin Schuhb9d5e8e2015-12-27 14:08:47 -0800116 UpdateGears(highgear ? HIGH : LOW);
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800117}
Austin Schuhb9d5e8e2015-12-27 14:08:47 -0800118
Austin Schuh6197a182015-11-28 16:04:40 -0800119void PolyDrivetrain::SetPosition(
Brian Silvermanb601d892015-12-20 18:24:38 -0500120 const ::y2014::control_loops::DrivetrainQueue::Position *position) {
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800121 if (position == NULL) {
122 ++stale_count_;
123 } else {
124 last_position_ = position_;
125 position_ = *position;
126 position_time_delta_ = (stale_count_ + 1) * kDt;
127 stale_count_ = 0;
128 }
129
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800130 if (position) {
Austin Schuhb9d5e8e2015-12-27 14:08:47 -0800131 const auto &values = constants::GetValues();
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800132 GearLogging gear_logging;
133 // Switch to the correct controller.
134 const double left_middle_shifter_position =
135 (values.left_drive.clear_high + values.left_drive.clear_low) / 2.0;
136 const double right_middle_shifter_position =
137 (values.right_drive.clear_high + values.right_drive.clear_low) / 2.0;
138
139 if (position->left_shifter_position < left_middle_shifter_position ||
140 left_gear_ == LOW) {
141 if (position->right_shifter_position < right_middle_shifter_position ||
142 right_gear_ == LOW) {
143 gear_logging.left_loop_high = false;
144 gear_logging.right_loop_high = false;
145 loop_->set_controller_index(gear_logging.controller_index = 0);
146 } else {
147 gear_logging.left_loop_high = false;
148 gear_logging.right_loop_high = true;
149 loop_->set_controller_index(gear_logging.controller_index = 1);
150 }
151 } else {
152 if (position->right_shifter_position < right_middle_shifter_position ||
153 right_gear_ == LOW) {
154 gear_logging.left_loop_high = true;
155 gear_logging.right_loop_high = false;
156 loop_->set_controller_index(gear_logging.controller_index = 2);
157 } else {
158 gear_logging.left_loop_high = true;
159 gear_logging.right_loop_high = true;
160 loop_->set_controller_index(gear_logging.controller_index = 3);
161 }
162 }
163
164 if (position->left_shifter_position > values.left_drive.clear_high &&
165 left_gear_ == SHIFTING_UP) {
166 left_gear_ = HIGH;
167 }
168 if (position->left_shifter_position < values.left_drive.clear_low &&
169 left_gear_ == SHIFTING_DOWN) {
170 left_gear_ = LOW;
171 }
172 if (position->right_shifter_position > values.right_drive.clear_high &&
173 right_gear_ == SHIFTING_UP) {
174 right_gear_ = HIGH;
175 }
176 if (position->right_shifter_position < values.right_drive.clear_low &&
177 right_gear_ == SHIFTING_DOWN) {
178 right_gear_ = LOW;
179 }
180
181 gear_logging.left_state = left_gear_;
182 gear_logging.right_state = right_gear_;
183 LOG_STRUCT(DEBUG, "state", gear_logging);
184 }
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800185}
186
187double PolyDrivetrain::FilterVelocity(double throttle) {
188 const Eigen::Matrix<double, 2, 2> FF =
189 loop_->B().inverse() *
190 (Eigen::Matrix<double, 2, 2>::Identity() - loop_->A());
191
192 constexpr int kHighGearController = 3;
193 const Eigen::Matrix<double, 2, 2> FF_high =
194 loop_->controller(kHighGearController).plant.B().inverse() *
195 (Eigen::Matrix<double, 2, 2>::Identity() -
196 loop_->controller(kHighGearController).plant.A());
197
198 ::Eigen::Matrix<double, 1, 2> FF_sum = FF.colwise().sum();
199 int min_FF_sum_index;
200 const double min_FF_sum = FF_sum.minCoeff(&min_FF_sum_index);
201 const double min_K_sum = loop_->K().col(min_FF_sum_index).sum();
202 const double high_min_FF_sum = FF_high.col(0).sum();
203
204 const double adjusted_ff_voltage =
205 ::aos::Clip(throttle * 12.0 * min_FF_sum / high_min_FF_sum, -12.0, 12.0);
206 return (adjusted_ff_voltage +
207 ttrust_ * min_K_sum * (loop_->X_hat(0, 0) + loop_->X_hat(1, 0)) /
208 2.0) /
209 (ttrust_ * min_K_sum + min_FF_sum);
210}
211
212double PolyDrivetrain::MaxVelocity() {
213 const Eigen::Matrix<double, 2, 2> FF =
214 loop_->B().inverse() *
215 (Eigen::Matrix<double, 2, 2>::Identity() - loop_->A());
216
217 constexpr int kHighGearController = 3;
218 const Eigen::Matrix<double, 2, 2> FF_high =
219 loop_->controller(kHighGearController).plant.B().inverse() *
220 (Eigen::Matrix<double, 2, 2>::Identity() -
221 loop_->controller(kHighGearController).plant.A());
222
223 ::Eigen::Matrix<double, 1, 2> FF_sum = FF.colwise().sum();
224 int min_FF_sum_index;
225 const double min_FF_sum = FF_sum.minCoeff(&min_FF_sum_index);
226 // const double min_K_sum = loop_->K().col(min_FF_sum_index).sum();
227 const double high_min_FF_sum = FF_high.col(0).sum();
228
229 const double adjusted_ff_voltage =
230 ::aos::Clip(12.0 * min_FF_sum / high_min_FF_sum, -12.0, 12.0);
231 return adjusted_ff_voltage / min_FF_sum;
232}
233
234void PolyDrivetrain::Update() {
235 const auto &values = constants::GetValues();
236 // TODO(austin): Observer for the current velocity instead of difference
237 // calculations.
238 ++counter_;
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800239 const double current_left_velocity =
240 (position_.left_encoder - last_position_.left_encoder) /
241 position_time_delta_;
242 const double current_right_velocity =
243 (position_.right_encoder - last_position_.right_encoder) /
244 position_time_delta_;
245 const double left_motor_speed =
246 MotorSpeed(values.left_drive, position_.left_shifter_position,
247 current_left_velocity);
248 const double right_motor_speed =
249 MotorSpeed(values.right_drive, position_.right_shifter_position,
250 current_right_velocity);
251
252 {
253 CIMLogging logging;
254
255 // Reset the CIM model to the current conditions to be ready for when we
256 // shift.
257 if (IsInGear(left_gear_)) {
258 logging.left_in_gear = true;
259 } else {
260 logging.left_in_gear = false;
261 }
262 logging.left_motor_speed = left_motor_speed;
263 logging.left_velocity = current_left_velocity;
264 if (IsInGear(right_gear_)) {
265 logging.right_in_gear = true;
266 } else {
267 logging.right_in_gear = false;
268 }
269 logging.right_motor_speed = right_motor_speed;
270 logging.right_velocity = current_right_velocity;
271
272 LOG_STRUCT(DEBUG, "currently", logging);
273 }
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800274
Austin Schuhb9d5e8e2015-12-27 14:08:47 -0800275 if (IsInGear(left_gear_) && IsInGear(right_gear_)) {
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800276 // FF * X = U (steady state)
277 const Eigen::Matrix<double, 2, 2> FF =
278 loop_->B().inverse() *
279 (Eigen::Matrix<double, 2, 2>::Identity() - loop_->A());
280
281 // Invert the plant to figure out how the velocity filter would have to
282 // work
283 // out in order to filter out the forwards negative inertia.
284 // This math assumes that the left and right power and velocity are
285 // equals,
286 // and that the plant is the same on the left and right.
287 const double fvel = FilterVelocity(throttle_);
288
289 const double sign_svel = wheel_ * ((fvel > 0.0) ? 1.0 : -1.0);
290 double steering_velocity;
291 if (quickturn_) {
292 steering_velocity = wheel_ * MaxVelocity();
293 } else {
294 steering_velocity = ::std::abs(fvel) * wheel_;
295 }
296 const double left_velocity = fvel - steering_velocity;
297 const double right_velocity = fvel + steering_velocity;
298
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 }
326
327 // TODO(austin): Model this better.
328 // TODO(austin): Feed back?
329 loop_->mutable_X_hat() =
330 loop_->A() * loop_->X_hat() + loop_->B() * loop_->U();
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800331 } else {
332 // Any motor is not in gear. Speed match.
333 ::Eigen::Matrix<double, 1, 1> R_left;
334 ::Eigen::Matrix<double, 1, 1> R_right;
335 R_left(0, 0) = left_motor_speed;
336 R_right(0, 0) = right_motor_speed;
337
338 const double wiggle =
339 (static_cast<double>((counter_ % 20) / 10) - 0.5) * 5.0;
340
341 loop_->mutable_U(0, 0) = ::aos::Clip(
342 (R_left / Kv)(0, 0) + (IsInGear(left_gear_) ? 0 : wiggle), -12.0, 12.0);
343 loop_->mutable_U(1, 0) =
344 ::aos::Clip((R_right / Kv)(0, 0) + (IsInGear(right_gear_) ? 0 : wiggle),
345 -12.0, 12.0);
346 loop_->mutable_U() *= 12.0 / ::aos::robot_state->voltage_battery;
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800347 }
348}
349
Austin Schuh6197a182015-11-28 16:04:40 -0800350void PolyDrivetrain::SendMotors(
Brian Silvermanb601d892015-12-20 18:24:38 -0500351 ::y2014::control_loops::DrivetrainQueue::Output *output) {
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800352 if (output != NULL) {
353 output->left_voltage = loop_->U(0, 0);
354 output->right_voltage = loop_->U(1, 0);
355 output->left_high = left_gear_ == HIGH || left_gear_ == SHIFTING_UP;
356 output->right_high = right_gear_ == HIGH || right_gear_ == SHIFTING_UP;
357 }
358}
359
360constexpr double PolyDrivetrain::kStallTorque;
361constexpr double PolyDrivetrain::kStallCurrent;
362constexpr double PolyDrivetrain::kFreeSpeed;
363constexpr double PolyDrivetrain::kFreeCurrent;
364constexpr double PolyDrivetrain::kWheelRadius;
365constexpr double PolyDrivetrain::kR;
366constexpr double PolyDrivetrain::Kv;
367constexpr double PolyDrivetrain::Kt;
368
Austin Schuh6197a182015-11-28 16:04:40 -0800369} // namespace drivetrain
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800370} // namespace control_loops
Austin Schuh6197a182015-11-28 16:04:40 -0800371} // namespace y2014