blob: 6d3de592af767f085940027ee9814be773eaf56e [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
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 /*]*/,
25 /*[*/ 0, -1 /*]]*/).finished(),
26 (Eigen::Matrix<double, 4, 1>() << /*[[*/ 12 /*]*/,
27 /*[*/ 12 /*]*/,
28 /*[*/ 12 /*]*/,
Austin Schuhc7a0a3d2016-10-15 16:22:47 -070029 /*[*/ 12 /*]]*/).finished(),
30 (Eigen::Matrix<double, 2, 4>() << /*[[*/ 12, 12, -12, -12 /*]*/,
31 /*[*/ -12, 12, 12, -12 /*]*/).finished()),
Comran Morshed5323ecb2015-12-26 20:50:55 +000032 loop_(new StateFeedbackLoop<2, 2, 2>(dt_config.make_v_drivetrain_loop())),
Austin Schuh96ce8ae2015-11-26 12:46:02 -080033 ttrust_(1.1),
34 wheel_(0.0),
35 throttle_(0.0),
36 quickturn_(false),
Austin Schuh94596dd2016-03-13 21:41:26 -070037 left_gear_(dt_config.default_high_gear ? Gear::HIGH : Gear::LOW),
38 right_gear_(dt_config.default_high_gear ? Gear::HIGH : Gear::LOW),
Comran Morshed5323ecb2015-12-26 20:50:55 +000039 counter_(0),
40 dt_config_(dt_config) {
Austin Schuh96ce8ae2015-11-26 12:46:02 -080041 last_position_.Zero();
42 position_.Zero();
43}
44
45double PolyDrivetrain::MotorSpeed(
46 const constants::ShifterHallEffect &hall_effect, double shifter_position,
Comran Morshed2091fe52016-02-19 17:27:21 +000047 double velocity, Gear gear) {
48 const double high_gear_speed =
49 velocity / dt_config_.high_gear_ratio / dt_config_.wheel_radius;
50 const double low_gear_speed =
51 velocity / dt_config_.low_gear_ratio / dt_config_.wheel_radius;
Austin Schuh96ce8ae2015-11-26 12:46:02 -080052
Comran Morshed2091fe52016-02-19 17:27:21 +000053 if (shifter_position < hall_effect.clear_low) {
54 // We're in low gear, so return speed for that gear.
55 return low_gear_speed;
56 } else if (shifter_position > hall_effect.clear_high) {
57 // We're in high gear, so return speed for that gear.
58 return high_gear_speed;
59 }
60
61 // Not in gear, so speed-match to destination gear.
62 switch (gear) {
Austin Schuh093535c2016-03-05 23:21:00 -080063 case Gear::HIGH:
64 case Gear::SHIFTING_UP:
Comran Morshed2091fe52016-02-19 17:27:21 +000065 return high_gear_speed;
Austin Schuh093535c2016-03-05 23:21:00 -080066 case Gear::LOW:
67 case Gear::SHIFTING_DOWN:
Austin Schuh746fc6d2016-02-21 02:53:33 -080068 default:
Comran Morshed2091fe52016-02-19 17:27:21 +000069 return low_gear_speed;
70 break;
Austin Schuh96ce8ae2015-11-26 12:46:02 -080071 }
72}
73
Austin Schuh093535c2016-03-05 23:21:00 -080074Gear PolyDrivetrain::UpdateSingleGear(Gear requested_gear, Gear current_gear) {
Austin Schuhb9d5e8e2015-12-27 14:08:47 -080075 const Gear shift_up =
Comran Morshed5323ecb2015-12-26 20:50:55 +000076 (dt_config_.shifter_type == ShifterType::HALL_EFFECT_SHIFTER)
Austin Schuh093535c2016-03-05 23:21:00 -080077 ? Gear::SHIFTING_UP
78 : Gear::HIGH;
Austin Schuhb9d5e8e2015-12-27 14:08:47 -080079 const Gear shift_down =
Comran Morshed5323ecb2015-12-26 20:50:55 +000080 (dt_config_.shifter_type == ShifterType::HALL_EFFECT_SHIFTER)
Austin Schuh093535c2016-03-05 23:21:00 -080081 ? Gear::SHIFTING_DOWN
82 : Gear::LOW;
Austin Schuhb9d5e8e2015-12-27 14:08:47 -080083 if (current_gear != requested_gear) {
84 if (IsInGear(current_gear)) {
Austin Schuh093535c2016-03-05 23:21:00 -080085 if (requested_gear == Gear::HIGH) {
86 if (current_gear != Gear::HIGH) {
Austin Schuhb9d5e8e2015-12-27 14:08:47 -080087 current_gear = shift_up;
88 }
89 } else {
Austin Schuh093535c2016-03-05 23:21:00 -080090 if (current_gear != Gear::LOW) {
Austin Schuhb9d5e8e2015-12-27 14:08:47 -080091 current_gear = shift_down;
92 }
93 }
94 } else {
Austin Schuh093535c2016-03-05 23:21:00 -080095 if (requested_gear == Gear::HIGH && current_gear == Gear::SHIFTING_DOWN) {
96 current_gear = Gear::SHIFTING_UP;
97 } else if (requested_gear == Gear::LOW &&
98 current_gear == Gear::SHIFTING_UP) {
99 current_gear = Gear::SHIFTING_DOWN;
Austin Schuhb9d5e8e2015-12-27 14:08:47 -0800100 }
101 }
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800102 }
Austin Schuhb9d5e8e2015-12-27 14:08:47 -0800103 return current_gear;
104}
105
Austin Schuh093535c2016-03-05 23:21:00 -0800106void PolyDrivetrain::SetGoal(
107 const ::frc971::control_loops::DrivetrainQueue::Goal &goal) {
108 const double wheel = goal.steering;
109 const double throttle = goal.throttle;
110 const bool quickturn = goal.quickturn;
111 const bool highgear = goal.highgear;
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800112
Austin Schuh731c06d2016-04-03 21:36:07 -0700113 const double kWheelNonLinearity = 0.25;
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800114 // Apply a sin function that's scaled to make it feel better.
115 const double angular_range = M_PI_2 * kWheelNonLinearity;
116
117 wheel_ = sin(angular_range * wheel) / sin(angular_range);
118 wheel_ = sin(angular_range * wheel_) / sin(angular_range);
Austin Schuh1dbee482016-02-28 20:12:24 -0800119 wheel_ = 2.0 * wheel - wheel_;
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800120 quickturn_ = quickturn;
121
122 static const double kThrottleDeadband = 0.05;
123 if (::std::abs(throttle) < kThrottleDeadband) {
124 throttle_ = 0;
125 } else {
126 throttle_ = copysign(
127 (::std::abs(throttle) - kThrottleDeadband) / (1.0 - kThrottleDeadband),
128 throttle);
129 }
130
Austin Schuh093535c2016-03-05 23:21:00 -0800131 Gear requested_gear = highgear ? Gear::HIGH : Gear::LOW;
132
133 left_gear_ = PolyDrivetrain::UpdateSingleGear(requested_gear, left_gear_);
134 right_gear_ = PolyDrivetrain::UpdateSingleGear(requested_gear, right_gear_);
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800135}
Austin Schuhb9d5e8e2015-12-27 14:08:47 -0800136
Austin Schuh6197a182015-11-28 16:04:40 -0800137void PolyDrivetrain::SetPosition(
Austin Schuh093535c2016-03-05 23:21:00 -0800138 const ::frc971::control_loops::DrivetrainQueue::Position *position,
139 Gear left_gear, Gear right_gear) {
140 left_gear_ = left_gear;
141 right_gear_ = right_gear;
142 last_position_ = position_;
143 position_ = *position;
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800144}
145
Austin Schuh41565602016-02-28 20:10:49 -0800146double PolyDrivetrain::FilterVelocity(double throttle) const {
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800147 const Eigen::Matrix<double, 2, 2> FF =
148 loop_->B().inverse() *
149 (Eigen::Matrix<double, 2, 2>::Identity() - loop_->A());
150
151 constexpr int kHighGearController = 3;
152 const Eigen::Matrix<double, 2, 2> FF_high =
153 loop_->controller(kHighGearController).plant.B().inverse() *
154 (Eigen::Matrix<double, 2, 2>::Identity() -
155 loop_->controller(kHighGearController).plant.A());
156
157 ::Eigen::Matrix<double, 1, 2> FF_sum = FF.colwise().sum();
158 int min_FF_sum_index;
159 const double min_FF_sum = FF_sum.minCoeff(&min_FF_sum_index);
160 const double min_K_sum = loop_->K().col(min_FF_sum_index).sum();
161 const double high_min_FF_sum = FF_high.col(0).sum();
162
163 const double adjusted_ff_voltage =
164 ::aos::Clip(throttle * 12.0 * min_FF_sum / high_min_FF_sum, -12.0, 12.0);
165 return (adjusted_ff_voltage +
166 ttrust_ * min_K_sum * (loop_->X_hat(0, 0) + loop_->X_hat(1, 0)) /
167 2.0) /
168 (ttrust_ * min_K_sum + min_FF_sum);
169}
170
171double PolyDrivetrain::MaxVelocity() {
172 const Eigen::Matrix<double, 2, 2> FF =
173 loop_->B().inverse() *
174 (Eigen::Matrix<double, 2, 2>::Identity() - loop_->A());
175
176 constexpr int kHighGearController = 3;
177 const Eigen::Matrix<double, 2, 2> FF_high =
178 loop_->controller(kHighGearController).plant.B().inverse() *
179 (Eigen::Matrix<double, 2, 2>::Identity() -
180 loop_->controller(kHighGearController).plant.A());
181
182 ::Eigen::Matrix<double, 1, 2> FF_sum = FF.colwise().sum();
183 int min_FF_sum_index;
184 const double min_FF_sum = FF_sum.minCoeff(&min_FF_sum_index);
185 // const double min_K_sum = loop_->K().col(min_FF_sum_index).sum();
186 const double high_min_FF_sum = FF_high.col(0).sum();
187
188 const double adjusted_ff_voltage =
189 ::aos::Clip(12.0 * min_FF_sum / high_min_FF_sum, -12.0, 12.0);
190 return adjusted_ff_voltage / min_FF_sum;
191}
192
193void PolyDrivetrain::Update() {
Comran Morshed76ca8f52016-02-21 17:26:28 +0000194 if (dt_config_.loop_type == LoopType::CLOSED_LOOP) {
Austin Schuh41565602016-02-28 20:10:49 -0800195 loop_->mutable_X_hat()(0, 0) = kf_->X_hat()(1, 0);
196 loop_->mutable_X_hat()(1, 0) = kf_->X_hat()(3, 0);
Comran Morshed76ca8f52016-02-21 17:26:28 +0000197 }
Austin Schuh0520cbf2016-01-06 19:58:36 -0800198
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800199 // TODO(austin): Observer for the current velocity instead of difference
200 // calculations.
201 ++counter_;
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800202
Austin Schuhb9d5e8e2015-12-27 14:08:47 -0800203 if (IsInGear(left_gear_) && IsInGear(right_gear_)) {
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800204 // FF * X = U (steady state)
205 const Eigen::Matrix<double, 2, 2> FF =
206 loop_->B().inverse() *
207 (Eigen::Matrix<double, 2, 2>::Identity() - loop_->A());
208
209 // Invert the plant to figure out how the velocity filter would have to
210 // work
211 // out in order to filter out the forwards negative inertia.
212 // This math assumes that the left and right power and velocity are
213 // equals,
214 // and that the plant is the same on the left and right.
215 const double fvel = FilterVelocity(throttle_);
216
217 const double sign_svel = wheel_ * ((fvel > 0.0) ? 1.0 : -1.0);
218 double steering_velocity;
219 if (quickturn_) {
220 steering_velocity = wheel_ * MaxVelocity();
221 } else {
222 steering_velocity = ::std::abs(fvel) * wheel_;
223 }
224 const double left_velocity = fvel - steering_velocity;
225 const double right_velocity = fvel + steering_velocity;
Austin Schuh41565602016-02-28 20:10:49 -0800226 goal_left_velocity_ = left_velocity;
227 goal_right_velocity_ = right_velocity;
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800228
229 // Integrate velocity to get the position.
230 // This position is used to get integral control.
231 loop_->mutable_R() << left_velocity, right_velocity;
232
233 if (!quickturn_) {
234 // K * R = w
235 Eigen::Matrix<double, 1, 2> equality_k;
236 equality_k << 1 + sign_svel, -(1 - sign_svel);
237 const double equality_w = 0.0;
238
239 // Construct a constraint on R by manipulating the constraint on U
Austin Schuhc7a0a3d2016-10-15 16:22:47 -0700240 ::aos::controls::HVPolytope<2, 4, 4> R_poly_hv(
241 U_Poly_.static_H() * (loop_->K() + FF),
242 U_Poly_.static_k() + U_Poly_.static_H() * loop_->K() * loop_->X_hat(),
243 (loop_->K() + FF).inverse() *
244 ::aos::controls::ShiftPoints<2, 4>(U_Poly_.StaticVertices(),
245 loop_->K() * loop_->X_hat()));
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800246
247 // Limit R back inside the box.
248 loop_->mutable_R() =
Austin Schuhc7a0a3d2016-10-15 16:22:47 -0700249 CoerceGoal(R_poly_hv, equality_k, equality_w, loop_->R());
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800250 }
251
252 const Eigen::Matrix<double, 2, 1> FF_volts = FF * loop_->R();
253 const Eigen::Matrix<double, 2, 1> U_ideal =
254 loop_->K() * (loop_->R() - loop_->X_hat()) + FF_volts;
255
256 for (int i = 0; i < 2; i++) {
257 loop_->mutable_U()[i] = ::aos::Clip(U_ideal[i], -12, 12);
258 }
Comran Morshed76ca8f52016-02-21 17:26:28 +0000259
260 if (dt_config_.loop_type == LoopType::OPEN_LOOP) {
261 loop_->mutable_X_hat() =
262 loop_->A() * loop_->X_hat() + loop_->B() * loop_->U();
263 }
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800264 } else {
Comran Morshed5323ecb2015-12-26 20:50:55 +0000265 const double current_left_velocity =
Austin Schuh093535c2016-03-05 23:21:00 -0800266 (position_.left_encoder - last_position_.left_encoder) / dt_config_.dt;
Comran Morshed5323ecb2015-12-26 20:50:55 +0000267 const double current_right_velocity =
268 (position_.right_encoder - last_position_.right_encoder) /
Austin Schuh093535c2016-03-05 23:21:00 -0800269 dt_config_.dt;
Comran Morshed5323ecb2015-12-26 20:50:55 +0000270 const double left_motor_speed =
271 MotorSpeed(dt_config_.left_drive, position_.left_shifter_position,
Comran Morshed2091fe52016-02-19 17:27:21 +0000272 current_left_velocity, left_gear_);
Comran Morshed5323ecb2015-12-26 20:50:55 +0000273 const double right_motor_speed =
274 MotorSpeed(dt_config_.right_drive, position_.right_shifter_position,
Comran Morshed2091fe52016-02-19 17:27:21 +0000275 current_right_velocity, right_gear_);
Comran Morshed5323ecb2015-12-26 20:50:55 +0000276
277 {
278 CIMLogging logging;
279
280 // Reset the CIM model to the current conditions to be ready for when we
281 // shift.
Comran Morshed2091fe52016-02-19 17:27:21 +0000282 logging.left_in_gear = IsInGear(left_gear_);
Comran Morshed5323ecb2015-12-26 20:50:55 +0000283 logging.left_motor_speed = left_motor_speed;
284 logging.left_velocity = current_left_velocity;
Comran Morshed2091fe52016-02-19 17:27:21 +0000285
286 logging.right_in_gear = IsInGear(right_gear_);
Comran Morshed5323ecb2015-12-26 20:50:55 +0000287 logging.right_motor_speed = right_motor_speed;
288 logging.right_velocity = current_right_velocity;
289
290 LOG_STRUCT(DEBUG, "currently", logging);
291 }
Austin Schuh41565602016-02-28 20:10:49 -0800292 goal_left_velocity_ = current_left_velocity;
293 goal_right_velocity_ = current_right_velocity;
Comran Morshed5323ecb2015-12-26 20:50:55 +0000294
Comran Morshed2091fe52016-02-19 17:27:21 +0000295 // Any motor is not in gear. Speed match.
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800296 ::Eigen::Matrix<double, 1, 1> R_left;
297 ::Eigen::Matrix<double, 1, 1> R_right;
298 R_left(0, 0) = left_motor_speed;
299 R_right(0, 0) = right_motor_speed;
300
301 const double wiggle =
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700302 (static_cast<double>((counter_ % 30) / 15) - 0.5) * 8.0;
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800303
304 loop_->mutable_U(0, 0) = ::aos::Clip(
Comran Morshed5323ecb2015-12-26 20:50:55 +0000305 (R_left / dt_config_.v)(0, 0) + (IsInGear(left_gear_) ? 0 : wiggle),
306 -12.0, 12.0);
307 loop_->mutable_U(1, 0) = ::aos::Clip(
308 (R_right / dt_config_.v)(0, 0) + (IsInGear(right_gear_) ? 0 : wiggle),
309 -12.0, 12.0);
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800310 loop_->mutable_U() *= 12.0 / ::aos::robot_state->voltage_battery;
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800311 }
312}
313
Austin Schuh093535c2016-03-05 23:21:00 -0800314void PolyDrivetrain::SetOutput(
Comran Morshed5323ecb2015-12-26 20:50:55 +0000315 ::frc971::control_loops::DrivetrainQueue::Output *output) {
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800316 if (output != NULL) {
317 output->left_voltage = loop_->U(0, 0);
318 output->right_voltage = loop_->U(1, 0);
Austin Schuh093535c2016-03-05 23:21:00 -0800319 output->left_high = MaybeHigh(left_gear_);
320 output->right_high = MaybeHigh(right_gear_);
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800321 }
322}
323
Austin Schuh41565602016-02-28 20:10:49 -0800324void PolyDrivetrain::PopulateStatus(
325 ::frc971::control_loops::DrivetrainQueue::Status *status) {
326 status->left_velocity_goal = goal_left_velocity_;
327 status->right_velocity_goal = goal_right_velocity_;
328}
329
Austin Schuh6197a182015-11-28 16:04:40 -0800330} // namespace drivetrain
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800331} // namespace control_loops
Comran Morshed5323ecb2015-12-26 20:50:55 +0000332} // namespace frc971