blob: d3a220bcc48b9e0e97674ac7df0a561647b51015 [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 Schuh96ce8ae2015-11-26 12:46:02 -0800113 // Apply a sin function that's scaled to make it feel better.
Adam Snaider94a52372016-10-19 20:06:01 -0700114 const double angular_range = M_PI_2 * dt_config_.wheel_non_linearity;
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800115
116 wheel_ = sin(angular_range * wheel) / sin(angular_range);
117 wheel_ = sin(angular_range * wheel_) / sin(angular_range);
Austin Schuh1dbee482016-02-28 20:12:24 -0800118 wheel_ = 2.0 * wheel - wheel_;
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800119 quickturn_ = quickturn;
120
Adam Snaider94a52372016-10-19 20:06:01 -0700121 if (!quickturn_) {
122 wheel_ *= dt_config_.quickturn_wheel_multiplier;
123 }
124
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800125 static const double kThrottleDeadband = 0.05;
126 if (::std::abs(throttle) < kThrottleDeadband) {
127 throttle_ = 0;
128 } else {
129 throttle_ = copysign(
130 (::std::abs(throttle) - kThrottleDeadband) / (1.0 - kThrottleDeadband),
131 throttle);
132 }
133
Austin Schuh093535c2016-03-05 23:21:00 -0800134 Gear requested_gear = highgear ? Gear::HIGH : Gear::LOW;
135
136 left_gear_ = PolyDrivetrain::UpdateSingleGear(requested_gear, left_gear_);
137 right_gear_ = PolyDrivetrain::UpdateSingleGear(requested_gear, right_gear_);
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800138}
Austin Schuhb9d5e8e2015-12-27 14:08:47 -0800139
Austin Schuh6197a182015-11-28 16:04:40 -0800140void PolyDrivetrain::SetPosition(
Austin Schuh093535c2016-03-05 23:21:00 -0800141 const ::frc971::control_loops::DrivetrainQueue::Position *position,
142 Gear left_gear, Gear right_gear) {
143 left_gear_ = left_gear;
144 right_gear_ = right_gear;
145 last_position_ = position_;
146 position_ = *position;
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800147}
148
Austin Schuh41565602016-02-28 20:10:49 -0800149double PolyDrivetrain::FilterVelocity(double throttle) const {
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800150 const Eigen::Matrix<double, 2, 2> FF =
151 loop_->B().inverse() *
152 (Eigen::Matrix<double, 2, 2>::Identity() - loop_->A());
153
154 constexpr int kHighGearController = 3;
155 const Eigen::Matrix<double, 2, 2> FF_high =
Austin Schuh64f17a52017-02-25 14:41:58 -0800156 loop_->controller(kHighGearController).plant.B.inverse() *
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800157 (Eigen::Matrix<double, 2, 2>::Identity() -
Austin Schuh64f17a52017-02-25 14:41:58 -0800158 loop_->controller(kHighGearController).plant.A);
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800159
160 ::Eigen::Matrix<double, 1, 2> FF_sum = FF.colwise().sum();
161 int min_FF_sum_index;
162 const double min_FF_sum = FF_sum.minCoeff(&min_FF_sum_index);
163 const double min_K_sum = loop_->K().col(min_FF_sum_index).sum();
164 const double high_min_FF_sum = FF_high.col(0).sum();
165
166 const double adjusted_ff_voltage =
167 ::aos::Clip(throttle * 12.0 * min_FF_sum / high_min_FF_sum, -12.0, 12.0);
168 return (adjusted_ff_voltage +
169 ttrust_ * min_K_sum * (loop_->X_hat(0, 0) + loop_->X_hat(1, 0)) /
170 2.0) /
171 (ttrust_ * min_K_sum + min_FF_sum);
172}
173
174double PolyDrivetrain::MaxVelocity() {
175 const Eigen::Matrix<double, 2, 2> FF =
176 loop_->B().inverse() *
177 (Eigen::Matrix<double, 2, 2>::Identity() - loop_->A());
178
179 constexpr int kHighGearController = 3;
180 const Eigen::Matrix<double, 2, 2> FF_high =
Austin Schuh64f17a52017-02-25 14:41:58 -0800181 loop_->controller(kHighGearController).plant.B.inverse() *
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800182 (Eigen::Matrix<double, 2, 2>::Identity() -
Austin Schuh64f17a52017-02-25 14:41:58 -0800183 loop_->controller(kHighGearController).plant.A);
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800184
185 ::Eigen::Matrix<double, 1, 2> FF_sum = FF.colwise().sum();
186 int min_FF_sum_index;
187 const double min_FF_sum = FF_sum.minCoeff(&min_FF_sum_index);
188 // const double min_K_sum = loop_->K().col(min_FF_sum_index).sum();
189 const double high_min_FF_sum = FF_high.col(0).sum();
190
191 const double adjusted_ff_voltage =
192 ::aos::Clip(12.0 * min_FF_sum / high_min_FF_sum, -12.0, 12.0);
193 return adjusted_ff_voltage / min_FF_sum;
194}
195
196void PolyDrivetrain::Update() {
Comran Morshed76ca8f52016-02-21 17:26:28 +0000197 if (dt_config_.loop_type == LoopType::CLOSED_LOOP) {
Austin Schuh41565602016-02-28 20:10:49 -0800198 loop_->mutable_X_hat()(0, 0) = kf_->X_hat()(1, 0);
199 loop_->mutable_X_hat()(1, 0) = kf_->X_hat()(3, 0);
Comran Morshed76ca8f52016-02-21 17:26:28 +0000200 }
Austin Schuh0520cbf2016-01-06 19:58:36 -0800201
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800202 // TODO(austin): Observer for the current velocity instead of difference
203 // calculations.
204 ++counter_;
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800205
Austin Schuhb9d5e8e2015-12-27 14:08:47 -0800206 if (IsInGear(left_gear_) && IsInGear(right_gear_)) {
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800207 // FF * X = U (steady state)
208 const Eigen::Matrix<double, 2, 2> FF =
209 loop_->B().inverse() *
210 (Eigen::Matrix<double, 2, 2>::Identity() - loop_->A());
211
212 // Invert the plant to figure out how the velocity filter would have to
213 // work
214 // out in order to filter out the forwards negative inertia.
215 // This math assumes that the left and right power and velocity are
216 // equals,
217 // and that the plant is the same on the left and right.
218 const double fvel = FilterVelocity(throttle_);
219
220 const double sign_svel = wheel_ * ((fvel > 0.0) ? 1.0 : -1.0);
221 double steering_velocity;
222 if (quickturn_) {
223 steering_velocity = wheel_ * MaxVelocity();
224 } else {
225 steering_velocity = ::std::abs(fvel) * wheel_;
226 }
227 const double left_velocity = fvel - steering_velocity;
228 const double right_velocity = fvel + steering_velocity;
Austin Schuh41565602016-02-28 20:10:49 -0800229 goal_left_velocity_ = left_velocity;
230 goal_right_velocity_ = right_velocity;
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800231
232 // Integrate velocity to get the position.
233 // This position is used to get integral control.
234 loop_->mutable_R() << left_velocity, right_velocity;
235
236 if (!quickturn_) {
237 // K * R = w
238 Eigen::Matrix<double, 1, 2> equality_k;
239 equality_k << 1 + sign_svel, -(1 - sign_svel);
240 const double equality_w = 0.0;
241
242 // Construct a constraint on R by manipulating the constraint on U
Austin Schuhc7a0a3d2016-10-15 16:22:47 -0700243 ::aos::controls::HVPolytope<2, 4, 4> R_poly_hv(
244 U_Poly_.static_H() * (loop_->K() + FF),
245 U_Poly_.static_k() + U_Poly_.static_H() * loop_->K() * loop_->X_hat(),
246 (loop_->K() + FF).inverse() *
247 ::aos::controls::ShiftPoints<2, 4>(U_Poly_.StaticVertices(),
248 loop_->K() * loop_->X_hat()));
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800249
250 // Limit R back inside the box.
251 loop_->mutable_R() =
Austin Schuhc7a0a3d2016-10-15 16:22:47 -0700252 CoerceGoal(R_poly_hv, equality_k, equality_w, loop_->R());
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800253 }
254
255 const Eigen::Matrix<double, 2, 1> FF_volts = FF * loop_->R();
256 const Eigen::Matrix<double, 2, 1> U_ideal =
257 loop_->K() * (loop_->R() - loop_->X_hat()) + FF_volts;
258
259 for (int i = 0; i < 2; i++) {
260 loop_->mutable_U()[i] = ::aos::Clip(U_ideal[i], -12, 12);
261 }
Comran Morshed76ca8f52016-02-21 17:26:28 +0000262
263 if (dt_config_.loop_type == LoopType::OPEN_LOOP) {
264 loop_->mutable_X_hat() =
265 loop_->A() * loop_->X_hat() + loop_->B() * loop_->U();
266 }
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800267 } else {
Comran Morshed5323ecb2015-12-26 20:50:55 +0000268 const double current_left_velocity =
Austin Schuh093535c2016-03-05 23:21:00 -0800269 (position_.left_encoder - last_position_.left_encoder) / dt_config_.dt;
Comran Morshed5323ecb2015-12-26 20:50:55 +0000270 const double current_right_velocity =
271 (position_.right_encoder - last_position_.right_encoder) /
Austin Schuh093535c2016-03-05 23:21:00 -0800272 dt_config_.dt;
Comran Morshed5323ecb2015-12-26 20:50:55 +0000273 const double left_motor_speed =
274 MotorSpeed(dt_config_.left_drive, position_.left_shifter_position,
Comran Morshed2091fe52016-02-19 17:27:21 +0000275 current_left_velocity, left_gear_);
Comran Morshed5323ecb2015-12-26 20:50:55 +0000276 const double right_motor_speed =
277 MotorSpeed(dt_config_.right_drive, position_.right_shifter_position,
Comran Morshed2091fe52016-02-19 17:27:21 +0000278 current_right_velocity, right_gear_);
Comran Morshed5323ecb2015-12-26 20:50:55 +0000279
280 {
281 CIMLogging logging;
282
283 // Reset the CIM model to the current conditions to be ready for when we
284 // shift.
Comran Morshed2091fe52016-02-19 17:27:21 +0000285 logging.left_in_gear = IsInGear(left_gear_);
Comran Morshed5323ecb2015-12-26 20:50:55 +0000286 logging.left_motor_speed = left_motor_speed;
287 logging.left_velocity = current_left_velocity;
Comran Morshed2091fe52016-02-19 17:27:21 +0000288
289 logging.right_in_gear = IsInGear(right_gear_);
Comran Morshed5323ecb2015-12-26 20:50:55 +0000290 logging.right_motor_speed = right_motor_speed;
291 logging.right_velocity = current_right_velocity;
292
293 LOG_STRUCT(DEBUG, "currently", logging);
294 }
Austin Schuh41565602016-02-28 20:10:49 -0800295 goal_left_velocity_ = current_left_velocity;
296 goal_right_velocity_ = current_right_velocity;
Comran Morshed5323ecb2015-12-26 20:50:55 +0000297
Comran Morshed2091fe52016-02-19 17:27:21 +0000298 // Any motor is not in gear. Speed match.
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800299 ::Eigen::Matrix<double, 1, 1> R_left;
300 ::Eigen::Matrix<double, 1, 1> R_right;
301 R_left(0, 0) = left_motor_speed;
302 R_right(0, 0) = right_motor_speed;
303
304 const double wiggle =
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700305 (static_cast<double>((counter_ % 30) / 15) - 0.5) * 8.0;
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800306
307 loop_->mutable_U(0, 0) = ::aos::Clip(
Comran Morshed5323ecb2015-12-26 20:50:55 +0000308 (R_left / dt_config_.v)(0, 0) + (IsInGear(left_gear_) ? 0 : wiggle),
309 -12.0, 12.0);
310 loop_->mutable_U(1, 0) = ::aos::Clip(
311 (R_right / dt_config_.v)(0, 0) + (IsInGear(right_gear_) ? 0 : wiggle),
312 -12.0, 12.0);
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800313 loop_->mutable_U() *= 12.0 / ::aos::robot_state->voltage_battery;
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800314 }
315}
316
Austin Schuh093535c2016-03-05 23:21:00 -0800317void PolyDrivetrain::SetOutput(
Comran Morshed5323ecb2015-12-26 20:50:55 +0000318 ::frc971::control_loops::DrivetrainQueue::Output *output) {
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800319 if (output != NULL) {
320 output->left_voltage = loop_->U(0, 0);
321 output->right_voltage = loop_->U(1, 0);
Austin Schuh093535c2016-03-05 23:21:00 -0800322 output->left_high = MaybeHigh(left_gear_);
323 output->right_high = MaybeHigh(right_gear_);
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800324 }
325}
326
Austin Schuh41565602016-02-28 20:10:49 -0800327void PolyDrivetrain::PopulateStatus(
328 ::frc971::control_loops::DrivetrainQueue::Status *status) {
329 status->left_velocity_goal = goal_left_velocity_;
330 status->right_velocity_goal = goal_right_velocity_;
331}
332
Austin Schuh6197a182015-11-28 16:04:40 -0800333} // namespace drivetrain
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800334} // namespace control_loops
Comran Morshed5323ecb2015-12-26 20:50:55 +0000335} // namespace frc971