blob: 407c23b7112e86a5c0cffaba3eb96f89d0f017fc [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
Comran Morshed5323ecb2015-12-26 20:50:55 +000019using ::frc971::control_loops::GearLogging;
20using ::frc971::control_loops::CIMLogging;
Austin Schuh6197a182015-11-28 16:04:40 -080021using ::frc971::control_loops::CoerceGoal;
Austin Schuh96ce8ae2015-11-26 12:46:02 -080022
Austin Schuh41565602016-02-28 20:10:49 -080023PolyDrivetrain::PolyDrivetrain(const DrivetrainConfig &dt_config,
24 StateFeedbackLoop<7, 2, 3> *kf)
25 : kf_(kf),
Austin Schuh6613a072016-01-06 19:54:48 -080026 U_Poly_((Eigen::Matrix<double, 4, 2>() << /*[[*/ 1, 0 /*]*/,
Austin Schuh96ce8ae2015-11-26 12:46:02 -080027 /*[*/ -1, 0 /*]*/,
28 /*[*/ 0, 1 /*]*/,
29 /*[*/ 0, -1 /*]]*/).finished(),
30 (Eigen::Matrix<double, 4, 1>() << /*[[*/ 12 /*]*/,
31 /*[*/ 12 /*]*/,
32 /*[*/ 12 /*]*/,
33 /*[*/ 12 /*]]*/).finished()),
Comran Morshed5323ecb2015-12-26 20:50:55 +000034 loop_(new StateFeedbackLoop<2, 2, 2>(dt_config.make_v_drivetrain_loop())),
Austin Schuh96ce8ae2015-11-26 12:46:02 -080035 ttrust_(1.1),
36 wheel_(0.0),
37 throttle_(0.0),
38 quickturn_(false),
Austin Schuh94596dd2016-03-13 21:41:26 -070039 left_gear_(dt_config.default_high_gear ? Gear::HIGH : Gear::LOW),
40 right_gear_(dt_config.default_high_gear ? Gear::HIGH : Gear::LOW),
Comran Morshed5323ecb2015-12-26 20:50:55 +000041 counter_(0),
42 dt_config_(dt_config) {
Austin Schuh96ce8ae2015-11-26 12:46:02 -080043 last_position_.Zero();
44 position_.Zero();
45}
46
47double PolyDrivetrain::MotorSpeed(
48 const constants::ShifterHallEffect &hall_effect, double shifter_position,
Comran Morshed2091fe52016-02-19 17:27:21 +000049 double velocity, Gear gear) {
50 const double high_gear_speed =
51 velocity / dt_config_.high_gear_ratio / dt_config_.wheel_radius;
52 const double low_gear_speed =
53 velocity / dt_config_.low_gear_ratio / dt_config_.wheel_radius;
Austin Schuh96ce8ae2015-11-26 12:46:02 -080054
Comran Morshed2091fe52016-02-19 17:27:21 +000055 if (shifter_position < hall_effect.clear_low) {
56 // We're in low gear, so return speed for that gear.
57 return low_gear_speed;
58 } else if (shifter_position > hall_effect.clear_high) {
59 // We're in high gear, so return speed for that gear.
60 return high_gear_speed;
61 }
62
63 // Not in gear, so speed-match to destination gear.
64 switch (gear) {
Austin Schuh093535c2016-03-05 23:21:00 -080065 case Gear::HIGH:
66 case Gear::SHIFTING_UP:
Comran Morshed2091fe52016-02-19 17:27:21 +000067 return high_gear_speed;
Austin Schuh093535c2016-03-05 23:21:00 -080068 case Gear::LOW:
69 case Gear::SHIFTING_DOWN:
Austin Schuh746fc6d2016-02-21 02:53:33 -080070 default:
Comran Morshed2091fe52016-02-19 17:27:21 +000071 return low_gear_speed;
72 break;
Austin Schuh96ce8ae2015-11-26 12:46:02 -080073 }
74}
75
Austin Schuh093535c2016-03-05 23:21:00 -080076Gear PolyDrivetrain::UpdateSingleGear(Gear requested_gear, Gear current_gear) {
Austin Schuhb9d5e8e2015-12-27 14:08:47 -080077 const Gear shift_up =
Comran Morshed5323ecb2015-12-26 20:50:55 +000078 (dt_config_.shifter_type == ShifterType::HALL_EFFECT_SHIFTER)
Austin Schuh093535c2016-03-05 23:21:00 -080079 ? Gear::SHIFTING_UP
80 : Gear::HIGH;
Austin Schuhb9d5e8e2015-12-27 14:08:47 -080081 const Gear shift_down =
Comran Morshed5323ecb2015-12-26 20:50:55 +000082 (dt_config_.shifter_type == ShifterType::HALL_EFFECT_SHIFTER)
Austin Schuh093535c2016-03-05 23:21:00 -080083 ? Gear::SHIFTING_DOWN
84 : Gear::LOW;
Austin Schuhb9d5e8e2015-12-27 14:08:47 -080085 if (current_gear != requested_gear) {
86 if (IsInGear(current_gear)) {
Austin Schuh093535c2016-03-05 23:21:00 -080087 if (requested_gear == Gear::HIGH) {
88 if (current_gear != Gear::HIGH) {
Austin Schuhb9d5e8e2015-12-27 14:08:47 -080089 current_gear = shift_up;
90 }
91 } else {
Austin Schuh093535c2016-03-05 23:21:00 -080092 if (current_gear != Gear::LOW) {
Austin Schuhb9d5e8e2015-12-27 14:08:47 -080093 current_gear = shift_down;
94 }
95 }
96 } else {
Austin Schuh093535c2016-03-05 23:21:00 -080097 if (requested_gear == Gear::HIGH && current_gear == Gear::SHIFTING_DOWN) {
98 current_gear = Gear::SHIFTING_UP;
99 } else if (requested_gear == Gear::LOW &&
100 current_gear == Gear::SHIFTING_UP) {
101 current_gear = Gear::SHIFTING_DOWN;
Austin Schuhb9d5e8e2015-12-27 14:08:47 -0800102 }
103 }
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800104 }
Austin Schuhb9d5e8e2015-12-27 14:08:47 -0800105 return current_gear;
106}
107
Austin Schuh093535c2016-03-05 23:21:00 -0800108void PolyDrivetrain::SetGoal(
109 const ::frc971::control_loops::DrivetrainQueue::Goal &goal) {
110 const double wheel = goal.steering;
111 const double throttle = goal.throttle;
112 const bool quickturn = goal.quickturn;
113 const bool highgear = goal.highgear;
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800114
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800115 // Apply a sin function that's scaled to make it feel better.
Adam Snaider94a52372016-10-19 20:06:01 -0700116 const double angular_range = M_PI_2 * dt_config_.wheel_non_linearity;
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800117
118 wheel_ = sin(angular_range * wheel) / sin(angular_range);
119 wheel_ = sin(angular_range * wheel_) / sin(angular_range);
Austin Schuh1dbee482016-02-28 20:12:24 -0800120 wheel_ = 2.0 * wheel - wheel_;
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800121 quickturn_ = quickturn;
122
Adam Snaider94a52372016-10-19 20:06:01 -0700123 if (!quickturn_) {
124 wheel_ *= dt_config_.quickturn_wheel_multiplier;
125 }
126
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800127 static const double kThrottleDeadband = 0.05;
128 if (::std::abs(throttle) < kThrottleDeadband) {
129 throttle_ = 0;
130 } else {
131 throttle_ = copysign(
132 (::std::abs(throttle) - kThrottleDeadband) / (1.0 - kThrottleDeadband),
133 throttle);
134 }
135
Austin Schuh093535c2016-03-05 23:21:00 -0800136 Gear requested_gear = highgear ? Gear::HIGH : Gear::LOW;
137
138 left_gear_ = PolyDrivetrain::UpdateSingleGear(requested_gear, left_gear_);
139 right_gear_ = PolyDrivetrain::UpdateSingleGear(requested_gear, right_gear_);
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800140}
Austin Schuhb9d5e8e2015-12-27 14:08:47 -0800141
Austin Schuh6197a182015-11-28 16:04:40 -0800142void PolyDrivetrain::SetPosition(
Austin Schuh093535c2016-03-05 23:21:00 -0800143 const ::frc971::control_loops::DrivetrainQueue::Position *position,
144 Gear left_gear, Gear right_gear) {
145 left_gear_ = left_gear;
146 right_gear_ = right_gear;
147 last_position_ = position_;
148 position_ = *position;
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800149}
150
Austin Schuh41565602016-02-28 20:10:49 -0800151double PolyDrivetrain::FilterVelocity(double throttle) const {
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800152 const Eigen::Matrix<double, 2, 2> FF =
153 loop_->B().inverse() *
154 (Eigen::Matrix<double, 2, 2>::Identity() - loop_->A());
155
156 constexpr int kHighGearController = 3;
157 const Eigen::Matrix<double, 2, 2> FF_high =
158 loop_->controller(kHighGearController).plant.B().inverse() *
159 (Eigen::Matrix<double, 2, 2>::Identity() -
160 loop_->controller(kHighGearController).plant.A());
161
162 ::Eigen::Matrix<double, 1, 2> FF_sum = FF.colwise().sum();
163 int min_FF_sum_index;
164 const double min_FF_sum = FF_sum.minCoeff(&min_FF_sum_index);
165 const double min_K_sum = loop_->K().col(min_FF_sum_index).sum();
166 const double high_min_FF_sum = FF_high.col(0).sum();
167
168 const double adjusted_ff_voltage =
169 ::aos::Clip(throttle * 12.0 * min_FF_sum / high_min_FF_sum, -12.0, 12.0);
170 return (adjusted_ff_voltage +
171 ttrust_ * min_K_sum * (loop_->X_hat(0, 0) + loop_->X_hat(1, 0)) /
172 2.0) /
173 (ttrust_ * min_K_sum + min_FF_sum);
174}
175
176double PolyDrivetrain::MaxVelocity() {
177 const Eigen::Matrix<double, 2, 2> FF =
178 loop_->B().inverse() *
179 (Eigen::Matrix<double, 2, 2>::Identity() - loop_->A());
180
181 constexpr int kHighGearController = 3;
182 const Eigen::Matrix<double, 2, 2> FF_high =
183 loop_->controller(kHighGearController).plant.B().inverse() *
184 (Eigen::Matrix<double, 2, 2>::Identity() -
185 loop_->controller(kHighGearController).plant.A());
186
187 ::Eigen::Matrix<double, 1, 2> FF_sum = FF.colwise().sum();
188 int min_FF_sum_index;
189 const double min_FF_sum = FF_sum.minCoeff(&min_FF_sum_index);
190 // const double min_K_sum = loop_->K().col(min_FF_sum_index).sum();
191 const double high_min_FF_sum = FF_high.col(0).sum();
192
193 const double adjusted_ff_voltage =
194 ::aos::Clip(12.0 * min_FF_sum / high_min_FF_sum, -12.0, 12.0);
195 return adjusted_ff_voltage / min_FF_sum;
196}
197
198void PolyDrivetrain::Update() {
Comran Morshed76ca8f52016-02-21 17:26:28 +0000199 if (dt_config_.loop_type == LoopType::CLOSED_LOOP) {
Austin Schuh41565602016-02-28 20:10:49 -0800200 loop_->mutable_X_hat()(0, 0) = kf_->X_hat()(1, 0);
201 loop_->mutable_X_hat()(1, 0) = kf_->X_hat()(3, 0);
Comran Morshed76ca8f52016-02-21 17:26:28 +0000202 }
Austin Schuh0520cbf2016-01-06 19:58:36 -0800203
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800204 // TODO(austin): Observer for the current velocity instead of difference
205 // calculations.
206 ++counter_;
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800207
Austin Schuhb9d5e8e2015-12-27 14:08:47 -0800208 if (IsInGear(left_gear_) && IsInGear(right_gear_)) {
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800209 // FF * X = U (steady state)
210 const Eigen::Matrix<double, 2, 2> FF =
211 loop_->B().inverse() *
212 (Eigen::Matrix<double, 2, 2>::Identity() - loop_->A());
213
214 // Invert the plant to figure out how the velocity filter would have to
215 // work
216 // out in order to filter out the forwards negative inertia.
217 // This math assumes that the left and right power and velocity are
218 // equals,
219 // and that the plant is the same on the left and right.
220 const double fvel = FilterVelocity(throttle_);
221
222 const double sign_svel = wheel_ * ((fvel > 0.0) ? 1.0 : -1.0);
223 double steering_velocity;
224 if (quickturn_) {
225 steering_velocity = wheel_ * MaxVelocity();
226 } else {
227 steering_velocity = ::std::abs(fvel) * wheel_;
228 }
229 const double left_velocity = fvel - steering_velocity;
230 const double right_velocity = fvel + steering_velocity;
Austin Schuh41565602016-02-28 20:10:49 -0800231 goal_left_velocity_ = left_velocity;
232 goal_right_velocity_ = right_velocity;
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800233
234 // Integrate velocity to get the position.
235 // This position is used to get integral control.
236 loop_->mutable_R() << left_velocity, right_velocity;
237
238 if (!quickturn_) {
239 // K * R = w
240 Eigen::Matrix<double, 1, 2> equality_k;
241 equality_k << 1 + sign_svel, -(1 - sign_svel);
242 const double equality_w = 0.0;
243
244 // Construct a constraint on R by manipulating the constraint on U
245 ::aos::controls::HPolytope<2> R_poly = ::aos::controls::HPolytope<2>(
246 U_Poly_.H() * (loop_->K() + FF),
247 U_Poly_.k() + U_Poly_.H() * loop_->K() * loop_->X_hat());
248
249 // Limit R back inside the box.
250 loop_->mutable_R() =
251 CoerceGoal(R_poly, equality_k, equality_w, loop_->R());
252 }
253
254 const Eigen::Matrix<double, 2, 1> FF_volts = FF * loop_->R();
255 const Eigen::Matrix<double, 2, 1> U_ideal =
256 loop_->K() * (loop_->R() - loop_->X_hat()) + FF_volts;
257
258 for (int i = 0; i < 2; i++) {
259 loop_->mutable_U()[i] = ::aos::Clip(U_ideal[i], -12, 12);
260 }
Comran Morshed76ca8f52016-02-21 17:26:28 +0000261
262 if (dt_config_.loop_type == LoopType::OPEN_LOOP) {
263 loop_->mutable_X_hat() =
264 loop_->A() * loop_->X_hat() + loop_->B() * loop_->U();
265 }
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800266 } else {
Comran Morshed5323ecb2015-12-26 20:50:55 +0000267 const double current_left_velocity =
Austin Schuh093535c2016-03-05 23:21:00 -0800268 (position_.left_encoder - last_position_.left_encoder) / dt_config_.dt;
Comran Morshed5323ecb2015-12-26 20:50:55 +0000269 const double current_right_velocity =
270 (position_.right_encoder - last_position_.right_encoder) /
Austin Schuh093535c2016-03-05 23:21:00 -0800271 dt_config_.dt;
Comran Morshed5323ecb2015-12-26 20:50:55 +0000272 const double left_motor_speed =
273 MotorSpeed(dt_config_.left_drive, position_.left_shifter_position,
Comran Morshed2091fe52016-02-19 17:27:21 +0000274 current_left_velocity, left_gear_);
Comran Morshed5323ecb2015-12-26 20:50:55 +0000275 const double right_motor_speed =
276 MotorSpeed(dt_config_.right_drive, position_.right_shifter_position,
Comran Morshed2091fe52016-02-19 17:27:21 +0000277 current_right_velocity, right_gear_);
Comran Morshed5323ecb2015-12-26 20:50:55 +0000278
279 {
280 CIMLogging logging;
281
282 // Reset the CIM model to the current conditions to be ready for when we
283 // shift.
Comran Morshed2091fe52016-02-19 17:27:21 +0000284 logging.left_in_gear = IsInGear(left_gear_);
Comran Morshed5323ecb2015-12-26 20:50:55 +0000285 logging.left_motor_speed = left_motor_speed;
286 logging.left_velocity = current_left_velocity;
Comran Morshed2091fe52016-02-19 17:27:21 +0000287
288 logging.right_in_gear = IsInGear(right_gear_);
Comran Morshed5323ecb2015-12-26 20:50:55 +0000289 logging.right_motor_speed = right_motor_speed;
290 logging.right_velocity = current_right_velocity;
291
292 LOG_STRUCT(DEBUG, "currently", logging);
293 }
Austin Schuh41565602016-02-28 20:10:49 -0800294 goal_left_velocity_ = current_left_velocity;
295 goal_right_velocity_ = current_right_velocity;
Comran Morshed5323ecb2015-12-26 20:50:55 +0000296
Comran Morshed2091fe52016-02-19 17:27:21 +0000297 // Any motor is not in gear. Speed match.
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800298 ::Eigen::Matrix<double, 1, 1> R_left;
299 ::Eigen::Matrix<double, 1, 1> R_right;
300 R_left(0, 0) = left_motor_speed;
301 R_right(0, 0) = right_motor_speed;
302
303 const double wiggle =
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700304 (static_cast<double>((counter_ % 30) / 15) - 0.5) * 8.0;
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800305
306 loop_->mutable_U(0, 0) = ::aos::Clip(
Comran Morshed5323ecb2015-12-26 20:50:55 +0000307 (R_left / dt_config_.v)(0, 0) + (IsInGear(left_gear_) ? 0 : wiggle),
308 -12.0, 12.0);
309 loop_->mutable_U(1, 0) = ::aos::Clip(
310 (R_right / dt_config_.v)(0, 0) + (IsInGear(right_gear_) ? 0 : wiggle),
311 -12.0, 12.0);
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800312 loop_->mutable_U() *= 12.0 / ::aos::robot_state->voltage_battery;
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800313 }
314}
315
Austin Schuh093535c2016-03-05 23:21:00 -0800316void PolyDrivetrain::SetOutput(
Comran Morshed5323ecb2015-12-26 20:50:55 +0000317 ::frc971::control_loops::DrivetrainQueue::Output *output) {
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800318 if (output != NULL) {
319 output->left_voltage = loop_->U(0, 0);
320 output->right_voltage = loop_->U(1, 0);
Austin Schuh093535c2016-03-05 23:21:00 -0800321 output->left_high = MaybeHigh(left_gear_);
322 output->right_high = MaybeHigh(right_gear_);
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800323 }
324}
325
Austin Schuh41565602016-02-28 20:10:49 -0800326void PolyDrivetrain::PopulateStatus(
327 ::frc971::control_loops::DrivetrainQueue::Status *status) {
328 status->left_velocity_goal = goal_left_velocity_;
329 status->right_velocity_goal = goal_right_velocity_;
330}
331
Austin Schuh6197a182015-11-28 16:04:40 -0800332} // namespace drivetrain
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800333} // namespace control_loops
Comran Morshed5323ecb2015-12-26 20:50:55 +0000334} // namespace frc971