blob: 5fc4b17421ae75308d1a306a2b1345052e71ca34 [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
Austin Schuh96ce8ae2015-11-26 12:46:02 -08003#include "aos/common/commonmath.h"
Austin Schuhc5fceb82017-02-25 16:24:12 -08004#include "aos/common/controls/polytope.h"
5#include "aos/common/logging/logging.h"
Austin Schuh96ce8ae2015-11-26 12:46:02 -08006#include "aos/common/logging/matrix_logging.h"
Austin Schuhc5fceb82017-02-25 16:24:12 -08007#include "aos/common/logging/queue_logging.h"
Austin Schuh96ce8ae2015-11-26 12:46:02 -08008
9#include "aos/common/messages/robot_state.q.h"
Austin Schuh96ce8ae2015-11-26 12:46:02 -080010#include "frc971/control_loops/coerce_goal.h"
Comran Morshed5323ecb2015-12-26 20:50:55 +000011#include "frc971/control_loops/drivetrain/drivetrain.q.h"
12#include "frc971/control_loops/drivetrain/drivetrain_config.h"
Austin Schuhc5fceb82017-02-25 16:24:12 -080013#include "frc971/control_loops/state_feedback_loop.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
Austin Schuh41565602016-02-28 20:10:49 -080019PolyDrivetrain::PolyDrivetrain(const DrivetrainConfig &dt_config,
20 StateFeedbackLoop<7, 2, 3> *kf)
21 : kf_(kf),
Austin Schuh6613a072016-01-06 19:54:48 -080022 U_Poly_((Eigen::Matrix<double, 4, 2>() << /*[[*/ 1, 0 /*]*/,
Austin Schuh96ce8ae2015-11-26 12:46:02 -080023 /*[*/ -1, 0 /*]*/,
24 /*[*/ 0, 1 /*]*/,
Austin Schuhc5fceb82017-02-25 16:24:12 -080025 /*[*/ 0, -1 /*]]*/)
26 .finished(),
Austin Schuh96ce8ae2015-11-26 12:46:02 -080027 (Eigen::Matrix<double, 4, 1>() << /*[[*/ 12 /*]*/,
28 /*[*/ 12 /*]*/,
29 /*[*/ 12 /*]*/,
Austin Schuhc5fceb82017-02-25 16:24:12 -080030 /*[*/ 12 /*]]*/)
31 .finished(),
Austin Schuhc7a0a3d2016-10-15 16:22:47 -070032 (Eigen::Matrix<double, 2, 4>() << /*[[*/ 12, 12, -12, -12 /*]*/,
Austin Schuhc5fceb82017-02-25 16:24:12 -080033 /*[*/ -12, 12, 12, -12 /*]*/)
34 .finished()),
Comran Morshed5323ecb2015-12-26 20:50:55 +000035 loop_(new StateFeedbackLoop<2, 2, 2>(dt_config.make_v_drivetrain_loop())),
Austin Schuh96ce8ae2015-11-26 12:46:02 -080036 ttrust_(1.1),
37 wheel_(0.0),
38 throttle_(0.0),
39 quickturn_(false),
Austin Schuh94596dd2016-03-13 21:41:26 -070040 left_gear_(dt_config.default_high_gear ? Gear::HIGH : Gear::LOW),
41 right_gear_(dt_config.default_high_gear ? Gear::HIGH : 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) {
Austin Schuh093535c2016-03-05 23:21:00 -080066 case Gear::HIGH:
67 case Gear::SHIFTING_UP:
Comran Morshed2091fe52016-02-19 17:27:21 +000068 return high_gear_speed;
Austin Schuh093535c2016-03-05 23:21:00 -080069 case Gear::LOW:
70 case Gear::SHIFTING_DOWN:
Austin Schuh746fc6d2016-02-21 02:53:33 -080071 default:
Comran Morshed2091fe52016-02-19 17:27:21 +000072 return low_gear_speed;
73 break;
Austin Schuh96ce8ae2015-11-26 12:46:02 -080074 }
75}
76
Austin Schuh093535c2016-03-05 23:21:00 -080077Gear PolyDrivetrain::UpdateSingleGear(Gear requested_gear, 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)
Austin Schuh093535c2016-03-05 23:21:00 -080080 ? Gear::SHIFTING_UP
81 : Gear::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)
Austin Schuh093535c2016-03-05 23:21:00 -080084 ? Gear::SHIFTING_DOWN
85 : Gear::LOW;
Austin Schuhb9d5e8e2015-12-27 14:08:47 -080086 if (current_gear != requested_gear) {
87 if (IsInGear(current_gear)) {
Austin Schuh093535c2016-03-05 23:21:00 -080088 if (requested_gear == Gear::HIGH) {
89 if (current_gear != Gear::HIGH) {
Austin Schuhb9d5e8e2015-12-27 14:08:47 -080090 current_gear = shift_up;
91 }
92 } else {
Austin Schuh093535c2016-03-05 23:21:00 -080093 if (current_gear != Gear::LOW) {
Austin Schuhb9d5e8e2015-12-27 14:08:47 -080094 current_gear = shift_down;
95 }
96 }
97 } else {
Austin Schuh093535c2016-03-05 23:21:00 -080098 if (requested_gear == Gear::HIGH && current_gear == Gear::SHIFTING_DOWN) {
99 current_gear = Gear::SHIFTING_UP;
100 } else if (requested_gear == Gear::LOW &&
101 current_gear == Gear::SHIFTING_UP) {
102 current_gear = Gear::SHIFTING_DOWN;
Austin Schuhb9d5e8e2015-12-27 14:08:47 -0800103 }
104 }
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800105 }
Austin Schuhb9d5e8e2015-12-27 14:08:47 -0800106 return current_gear;
107}
108
Austin Schuh093535c2016-03-05 23:21:00 -0800109void PolyDrivetrain::SetGoal(
110 const ::frc971::control_loops::DrivetrainQueue::Goal &goal) {
111 const double wheel = goal.steering;
112 const double throttle = goal.throttle;
113 const bool quickturn = goal.quickturn;
114 const bool highgear = goal.highgear;
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800115
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800116 // Apply a sin function that's scaled to make it feel better.
Adam Snaider94a52372016-10-19 20:06:01 -0700117 const double angular_range = M_PI_2 * dt_config_.wheel_non_linearity;
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800118
119 wheel_ = sin(angular_range * wheel) / sin(angular_range);
120 wheel_ = sin(angular_range * wheel_) / sin(angular_range);
Austin Schuh1dbee482016-02-28 20:12:24 -0800121 wheel_ = 2.0 * wheel - wheel_;
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800122 quickturn_ = quickturn;
123
Adam Snaider94a52372016-10-19 20:06:01 -0700124 if (!quickturn_) {
125 wheel_ *= dt_config_.quickturn_wheel_multiplier;
126 }
127
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800128 static const double kThrottleDeadband = 0.05;
129 if (::std::abs(throttle) < kThrottleDeadband) {
130 throttle_ = 0;
131 } else {
132 throttle_ = copysign(
133 (::std::abs(throttle) - kThrottleDeadband) / (1.0 - kThrottleDeadband),
134 throttle);
135 }
136
Austin Schuh093535c2016-03-05 23:21:00 -0800137 Gear requested_gear = highgear ? Gear::HIGH : Gear::LOW;
138
139 left_gear_ = PolyDrivetrain::UpdateSingleGear(requested_gear, left_gear_);
140 right_gear_ = PolyDrivetrain::UpdateSingleGear(requested_gear, right_gear_);
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800141}
Austin Schuhb9d5e8e2015-12-27 14:08:47 -0800142
Austin Schuh6197a182015-11-28 16:04:40 -0800143void PolyDrivetrain::SetPosition(
Austin Schuh093535c2016-03-05 23:21:00 -0800144 const ::frc971::control_loops::DrivetrainQueue::Position *position,
145 Gear left_gear, Gear right_gear) {
146 left_gear_ = left_gear;
147 right_gear_ = right_gear;
148 last_position_ = position_;
149 position_ = *position;
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800150}
151
Austin Schuh41565602016-02-28 20:10:49 -0800152double PolyDrivetrain::FilterVelocity(double throttle) const {
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800153 const Eigen::Matrix<double, 2, 2> FF =
Austin Schuhc5fceb82017-02-25 16:24:12 -0800154 loop_->plant().B().inverse() *
155 (Eigen::Matrix<double, 2, 2>::Identity() - loop_->plant().A());
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800156
157 constexpr int kHighGearController = 3;
158 const Eigen::Matrix<double, 2, 2> FF_high =
Austin Schuhc5fceb82017-02-25 16:24:12 -0800159 loop_->plant().coefficients(kHighGearController).B.inverse() *
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800160 (Eigen::Matrix<double, 2, 2>::Identity() -
Austin Schuhc5fceb82017-02-25 16:24:12 -0800161 loop_->plant().coefficients(kHighGearController).A);
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800162
163 ::Eigen::Matrix<double, 1, 2> FF_sum = FF.colwise().sum();
164 int min_FF_sum_index;
165 const double min_FF_sum = FF_sum.minCoeff(&min_FF_sum_index);
166 const double min_K_sum = loop_->K().col(min_FF_sum_index).sum();
167 const double high_min_FF_sum = FF_high.col(0).sum();
168
169 const double adjusted_ff_voltage =
170 ::aos::Clip(throttle * 12.0 * min_FF_sum / high_min_FF_sum, -12.0, 12.0);
171 return (adjusted_ff_voltage +
172 ttrust_ * min_K_sum * (loop_->X_hat(0, 0) + loop_->X_hat(1, 0)) /
173 2.0) /
174 (ttrust_ * min_K_sum + min_FF_sum);
175}
176
177double PolyDrivetrain::MaxVelocity() {
178 const Eigen::Matrix<double, 2, 2> FF =
Austin Schuhc5fceb82017-02-25 16:24:12 -0800179 loop_->plant().B().inverse() *
180 (Eigen::Matrix<double, 2, 2>::Identity() - loop_->plant().A());
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800181
182 constexpr int kHighGearController = 3;
183 const Eigen::Matrix<double, 2, 2> FF_high =
Austin Schuhc5fceb82017-02-25 16:24:12 -0800184 loop_->plant().coefficients(kHighGearController).B.inverse() *
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800185 (Eigen::Matrix<double, 2, 2>::Identity() -
Austin Schuhc5fceb82017-02-25 16:24:12 -0800186 loop_->plant().coefficients(kHighGearController).A);
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800187
188 ::Eigen::Matrix<double, 1, 2> FF_sum = FF.colwise().sum();
189 int min_FF_sum_index;
190 const double min_FF_sum = FF_sum.minCoeff(&min_FF_sum_index);
191 // const double min_K_sum = loop_->K().col(min_FF_sum_index).sum();
192 const double high_min_FF_sum = FF_high.col(0).sum();
193
194 const double adjusted_ff_voltage =
195 ::aos::Clip(12.0 * min_FF_sum / high_min_FF_sum, -12.0, 12.0);
196 return adjusted_ff_voltage / min_FF_sum;
197}
198
199void PolyDrivetrain::Update() {
Comran Morshed76ca8f52016-02-21 17:26:28 +0000200 if (dt_config_.loop_type == LoopType::CLOSED_LOOP) {
Austin Schuh41565602016-02-28 20:10:49 -0800201 loop_->mutable_X_hat()(0, 0) = kf_->X_hat()(1, 0);
202 loop_->mutable_X_hat()(1, 0) = kf_->X_hat()(3, 0);
Comran Morshed76ca8f52016-02-21 17:26:28 +0000203 }
Austin Schuh0520cbf2016-01-06 19:58:36 -0800204
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800205 // TODO(austin): Observer for the current velocity instead of difference
206 // calculations.
207 ++counter_;
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800208
Austin Schuhb9d5e8e2015-12-27 14:08:47 -0800209 if (IsInGear(left_gear_) && IsInGear(right_gear_)) {
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800210 // FF * X = U (steady state)
211 const Eigen::Matrix<double, 2, 2> FF =
Austin Schuhc5fceb82017-02-25 16:24:12 -0800212 loop_->plant().B().inverse() *
213 (Eigen::Matrix<double, 2, 2>::Identity() - loop_->plant().A());
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800214
215 // Invert the plant to figure out how the velocity filter would have to
216 // work
217 // out in order to filter out the forwards negative inertia.
218 // This math assumes that the left and right power and velocity are
219 // equals,
220 // and that the plant is the same on the left and right.
221 const double fvel = FilterVelocity(throttle_);
222
223 const double sign_svel = wheel_ * ((fvel > 0.0) ? 1.0 : -1.0);
224 double steering_velocity;
225 if (quickturn_) {
226 steering_velocity = wheel_ * MaxVelocity();
227 } else {
228 steering_velocity = ::std::abs(fvel) * wheel_;
229 }
230 const double left_velocity = fvel - steering_velocity;
231 const double right_velocity = fvel + steering_velocity;
Austin Schuh41565602016-02-28 20:10:49 -0800232 goal_left_velocity_ = left_velocity;
233 goal_right_velocity_ = right_velocity;
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800234
235 // Integrate velocity to get the position.
236 // This position is used to get integral control.
237 loop_->mutable_R() << left_velocity, right_velocity;
238
239 if (!quickturn_) {
240 // K * R = w
241 Eigen::Matrix<double, 1, 2> equality_k;
242 equality_k << 1 + sign_svel, -(1 - sign_svel);
243 const double equality_w = 0.0;
244
245 // Construct a constraint on R by manipulating the constraint on U
Austin Schuhc7a0a3d2016-10-15 16:22:47 -0700246 ::aos::controls::HVPolytope<2, 4, 4> R_poly_hv(
247 U_Poly_.static_H() * (loop_->K() + FF),
248 U_Poly_.static_k() + U_Poly_.static_H() * loop_->K() * loop_->X_hat(),
249 (loop_->K() + FF).inverse() *
250 ::aos::controls::ShiftPoints<2, 4>(U_Poly_.StaticVertices(),
251 loop_->K() * loop_->X_hat()));
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800252
253 // Limit R back inside the box.
254 loop_->mutable_R() =
Austin Schuhc7a0a3d2016-10-15 16:22:47 -0700255 CoerceGoal(R_poly_hv, equality_k, equality_w, loop_->R());
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800256 }
257
258 const Eigen::Matrix<double, 2, 1> FF_volts = FF * loop_->R();
259 const Eigen::Matrix<double, 2, 1> U_ideal =
260 loop_->K() * (loop_->R() - loop_->X_hat()) + FF_volts;
261
262 for (int i = 0; i < 2; i++) {
263 loop_->mutable_U()[i] = ::aos::Clip(U_ideal[i], -12, 12);
264 }
Comran Morshed76ca8f52016-02-21 17:26:28 +0000265
266 if (dt_config_.loop_type == LoopType::OPEN_LOOP) {
267 loop_->mutable_X_hat() =
Austin Schuhc5fceb82017-02-25 16:24:12 -0800268 loop_->plant().A() * loop_->X_hat() + loop_->plant().B() * loop_->U();
Comran Morshed76ca8f52016-02-21 17:26:28 +0000269 }
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800270 } else {
Comran Morshed5323ecb2015-12-26 20:50:55 +0000271 const double current_left_velocity =
Austin Schuh093535c2016-03-05 23:21:00 -0800272 (position_.left_encoder - last_position_.left_encoder) / dt_config_.dt;
Comran Morshed5323ecb2015-12-26 20:50:55 +0000273 const double current_right_velocity =
274 (position_.right_encoder - last_position_.right_encoder) /
Austin Schuh093535c2016-03-05 23:21:00 -0800275 dt_config_.dt;
Comran Morshed5323ecb2015-12-26 20:50:55 +0000276 const double left_motor_speed =
277 MotorSpeed(dt_config_.left_drive, position_.left_shifter_position,
Comran Morshed2091fe52016-02-19 17:27:21 +0000278 current_left_velocity, left_gear_);
Comran Morshed5323ecb2015-12-26 20:50:55 +0000279 const double right_motor_speed =
280 MotorSpeed(dt_config_.right_drive, position_.right_shifter_position,
Comran Morshed2091fe52016-02-19 17:27:21 +0000281 current_right_velocity, right_gear_);
Comran Morshed5323ecb2015-12-26 20:50:55 +0000282
283 {
284 CIMLogging logging;
285
286 // Reset the CIM model to the current conditions to be ready for when we
287 // shift.
Comran Morshed2091fe52016-02-19 17:27:21 +0000288 logging.left_in_gear = IsInGear(left_gear_);
Comran Morshed5323ecb2015-12-26 20:50:55 +0000289 logging.left_motor_speed = left_motor_speed;
290 logging.left_velocity = current_left_velocity;
Comran Morshed2091fe52016-02-19 17:27:21 +0000291
292 logging.right_in_gear = IsInGear(right_gear_);
Comran Morshed5323ecb2015-12-26 20:50:55 +0000293 logging.right_motor_speed = right_motor_speed;
294 logging.right_velocity = current_right_velocity;
295
296 LOG_STRUCT(DEBUG, "currently", logging);
297 }
Austin Schuh41565602016-02-28 20:10:49 -0800298 goal_left_velocity_ = current_left_velocity;
299 goal_right_velocity_ = current_right_velocity;
Comran Morshed5323ecb2015-12-26 20:50:55 +0000300
Comran Morshed2091fe52016-02-19 17:27:21 +0000301 // Any motor is not in gear. Speed match.
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800302 ::Eigen::Matrix<double, 1, 1> R_left;
303 ::Eigen::Matrix<double, 1, 1> R_right;
304 R_left(0, 0) = left_motor_speed;
305 R_right(0, 0) = right_motor_speed;
306
307 const double wiggle =
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700308 (static_cast<double>((counter_ % 30) / 15) - 0.5) * 8.0;
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800309
310 loop_->mutable_U(0, 0) = ::aos::Clip(
Comran Morshed5323ecb2015-12-26 20:50:55 +0000311 (R_left / dt_config_.v)(0, 0) + (IsInGear(left_gear_) ? 0 : wiggle),
312 -12.0, 12.0);
313 loop_->mutable_U(1, 0) = ::aos::Clip(
314 (R_right / dt_config_.v)(0, 0) + (IsInGear(right_gear_) ? 0 : wiggle),
315 -12.0, 12.0);
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800316 loop_->mutable_U() *= 12.0 / ::aos::robot_state->voltage_battery;
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800317 }
318}
319
Austin Schuh093535c2016-03-05 23:21:00 -0800320void PolyDrivetrain::SetOutput(
Comran Morshed5323ecb2015-12-26 20:50:55 +0000321 ::frc971::control_loops::DrivetrainQueue::Output *output) {
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800322 if (output != NULL) {
323 output->left_voltage = loop_->U(0, 0);
324 output->right_voltage = loop_->U(1, 0);
Austin Schuh093535c2016-03-05 23:21:00 -0800325 output->left_high = MaybeHigh(left_gear_);
326 output->right_high = MaybeHigh(right_gear_);
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800327 }
328}
329
Austin Schuh41565602016-02-28 20:10:49 -0800330void PolyDrivetrain::PopulateStatus(
331 ::frc971::control_loops::DrivetrainQueue::Status *status) {
332 status->left_velocity_goal = goal_left_velocity_;
333 status->right_velocity_goal = goal_right_velocity_;
334}
335
Austin Schuh6197a182015-11-28 16:04:40 -0800336} // namespace drivetrain
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800337} // namespace control_loops
Comran Morshed5323ecb2015-12-26 20:50:55 +0000338} // namespace frc971