blob: 8925a40234ff84b768144a8c54928d5cee9577d1 [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 Schuh093535c2016-03-05 23:21:00 -080039 left_gear_(Gear::LOW),
40 right_gear_(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 Schuh1dbee482016-02-28 20:12:24 -0800115 const double kWheelNonLinearity = 0.4;
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800116 // Apply a sin function that's scaled to make it feel better.
117 const double angular_range = M_PI_2 * kWheelNonLinearity;
118
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
124 static const double kThrottleDeadband = 0.05;
125 if (::std::abs(throttle) < kThrottleDeadband) {
126 throttle_ = 0;
127 } else {
128 throttle_ = copysign(
129 (::std::abs(throttle) - kThrottleDeadband) / (1.0 - kThrottleDeadband),
130 throttle);
131 }
132
Austin Schuh093535c2016-03-05 23:21:00 -0800133 Gear requested_gear = highgear ? Gear::HIGH : Gear::LOW;
134
135 left_gear_ = PolyDrivetrain::UpdateSingleGear(requested_gear, left_gear_);
136 right_gear_ = PolyDrivetrain::UpdateSingleGear(requested_gear, right_gear_);
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800137}
Austin Schuhb9d5e8e2015-12-27 14:08:47 -0800138
Austin Schuh6197a182015-11-28 16:04:40 -0800139void PolyDrivetrain::SetPosition(
Austin Schuh093535c2016-03-05 23:21:00 -0800140 const ::frc971::control_loops::DrivetrainQueue::Position *position,
141 Gear left_gear, Gear right_gear) {
142 left_gear_ = left_gear;
143 right_gear_ = right_gear;
144 last_position_ = position_;
145 position_ = *position;
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800146}
147
Austin Schuh41565602016-02-28 20:10:49 -0800148double PolyDrivetrain::FilterVelocity(double throttle) const {
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800149 const Eigen::Matrix<double, 2, 2> FF =
150 loop_->B().inverse() *
151 (Eigen::Matrix<double, 2, 2>::Identity() - loop_->A());
152
153 constexpr int kHighGearController = 3;
154 const Eigen::Matrix<double, 2, 2> FF_high =
155 loop_->controller(kHighGearController).plant.B().inverse() *
156 (Eigen::Matrix<double, 2, 2>::Identity() -
157 loop_->controller(kHighGearController).plant.A());
158
159 ::Eigen::Matrix<double, 1, 2> FF_sum = FF.colwise().sum();
160 int min_FF_sum_index;
161 const double min_FF_sum = FF_sum.minCoeff(&min_FF_sum_index);
162 const double min_K_sum = loop_->K().col(min_FF_sum_index).sum();
163 const double high_min_FF_sum = FF_high.col(0).sum();
164
165 const double adjusted_ff_voltage =
166 ::aos::Clip(throttle * 12.0 * min_FF_sum / high_min_FF_sum, -12.0, 12.0);
167 return (adjusted_ff_voltage +
168 ttrust_ * min_K_sum * (loop_->X_hat(0, 0) + loop_->X_hat(1, 0)) /
169 2.0) /
170 (ttrust_ * min_K_sum + min_FF_sum);
171}
172
173double PolyDrivetrain::MaxVelocity() {
174 const Eigen::Matrix<double, 2, 2> FF =
175 loop_->B().inverse() *
176 (Eigen::Matrix<double, 2, 2>::Identity() - loop_->A());
177
178 constexpr int kHighGearController = 3;
179 const Eigen::Matrix<double, 2, 2> FF_high =
180 loop_->controller(kHighGearController).plant.B().inverse() *
181 (Eigen::Matrix<double, 2, 2>::Identity() -
182 loop_->controller(kHighGearController).plant.A());
183
184 ::Eigen::Matrix<double, 1, 2> FF_sum = FF.colwise().sum();
185 int min_FF_sum_index;
186 const double min_FF_sum = FF_sum.minCoeff(&min_FF_sum_index);
187 // const double min_K_sum = loop_->K().col(min_FF_sum_index).sum();
188 const double high_min_FF_sum = FF_high.col(0).sum();
189
190 const double adjusted_ff_voltage =
191 ::aos::Clip(12.0 * min_FF_sum / high_min_FF_sum, -12.0, 12.0);
192 return adjusted_ff_voltage / min_FF_sum;
193}
194
195void PolyDrivetrain::Update() {
Comran Morshed76ca8f52016-02-21 17:26:28 +0000196 if (dt_config_.loop_type == LoopType::CLOSED_LOOP) {
Austin Schuh41565602016-02-28 20:10:49 -0800197 loop_->mutable_X_hat()(0, 0) = kf_->X_hat()(1, 0);
198 loop_->mutable_X_hat()(1, 0) = kf_->X_hat()(3, 0);
Comran Morshed76ca8f52016-02-21 17:26:28 +0000199 }
Austin Schuh0520cbf2016-01-06 19:58:36 -0800200
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800201 // TODO(austin): Observer for the current velocity instead of difference
202 // calculations.
203 ++counter_;
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800204
Austin Schuhb9d5e8e2015-12-27 14:08:47 -0800205 if (IsInGear(left_gear_) && IsInGear(right_gear_)) {
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800206 // FF * X = U (steady state)
207 const Eigen::Matrix<double, 2, 2> FF =
208 loop_->B().inverse() *
209 (Eigen::Matrix<double, 2, 2>::Identity() - loop_->A());
210
211 // Invert the plant to figure out how the velocity filter would have to
212 // work
213 // out in order to filter out the forwards negative inertia.
214 // This math assumes that the left and right power and velocity are
215 // equals,
216 // and that the plant is the same on the left and right.
217 const double fvel = FilterVelocity(throttle_);
218
219 const double sign_svel = wheel_ * ((fvel > 0.0) ? 1.0 : -1.0);
220 double steering_velocity;
221 if (quickturn_) {
222 steering_velocity = wheel_ * MaxVelocity();
223 } else {
224 steering_velocity = ::std::abs(fvel) * wheel_;
225 }
226 const double left_velocity = fvel - steering_velocity;
227 const double right_velocity = fvel + steering_velocity;
Austin Schuh41565602016-02-28 20:10:49 -0800228 goal_left_velocity_ = left_velocity;
229 goal_right_velocity_ = right_velocity;
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800230
231 // Integrate velocity to get the position.
232 // This position is used to get integral control.
233 loop_->mutable_R() << left_velocity, right_velocity;
234
235 if (!quickturn_) {
236 // K * R = w
237 Eigen::Matrix<double, 1, 2> equality_k;
238 equality_k << 1 + sign_svel, -(1 - sign_svel);
239 const double equality_w = 0.0;
240
241 // Construct a constraint on R by manipulating the constraint on U
242 ::aos::controls::HPolytope<2> R_poly = ::aos::controls::HPolytope<2>(
243 U_Poly_.H() * (loop_->K() + FF),
244 U_Poly_.k() + U_Poly_.H() * loop_->K() * loop_->X_hat());
245
246 // Limit R back inside the box.
247 loop_->mutable_R() =
248 CoerceGoal(R_poly, equality_k, equality_w, loop_->R());
249 }
250
251 const Eigen::Matrix<double, 2, 1> FF_volts = FF * loop_->R();
252 const Eigen::Matrix<double, 2, 1> U_ideal =
253 loop_->K() * (loop_->R() - loop_->X_hat()) + FF_volts;
254
255 for (int i = 0; i < 2; i++) {
256 loop_->mutable_U()[i] = ::aos::Clip(U_ideal[i], -12, 12);
257 }
Comran Morshed76ca8f52016-02-21 17:26:28 +0000258
259 if (dt_config_.loop_type == LoopType::OPEN_LOOP) {
260 loop_->mutable_X_hat() =
261 loop_->A() * loop_->X_hat() + loop_->B() * loop_->U();
262 }
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800263 } else {
Comran Morshed5323ecb2015-12-26 20:50:55 +0000264 const double current_left_velocity =
Austin Schuh093535c2016-03-05 23:21:00 -0800265 (position_.left_encoder - last_position_.left_encoder) / dt_config_.dt;
Comran Morshed5323ecb2015-12-26 20:50:55 +0000266 const double current_right_velocity =
267 (position_.right_encoder - last_position_.right_encoder) /
Austin Schuh093535c2016-03-05 23:21:00 -0800268 dt_config_.dt;
Comran Morshed5323ecb2015-12-26 20:50:55 +0000269 const double left_motor_speed =
270 MotorSpeed(dt_config_.left_drive, position_.left_shifter_position,
Comran Morshed2091fe52016-02-19 17:27:21 +0000271 current_left_velocity, left_gear_);
Comran Morshed5323ecb2015-12-26 20:50:55 +0000272 const double right_motor_speed =
273 MotorSpeed(dt_config_.right_drive, position_.right_shifter_position,
Comran Morshed2091fe52016-02-19 17:27:21 +0000274 current_right_velocity, right_gear_);
Comran Morshed5323ecb2015-12-26 20:50:55 +0000275
276 {
277 CIMLogging logging;
278
279 // Reset the CIM model to the current conditions to be ready for when we
280 // shift.
Comran Morshed2091fe52016-02-19 17:27:21 +0000281 logging.left_in_gear = IsInGear(left_gear_);
Comran Morshed5323ecb2015-12-26 20:50:55 +0000282 logging.left_motor_speed = left_motor_speed;
283 logging.left_velocity = current_left_velocity;
Comran Morshed2091fe52016-02-19 17:27:21 +0000284
285 logging.right_in_gear = IsInGear(right_gear_);
Comran Morshed5323ecb2015-12-26 20:50:55 +0000286 logging.right_motor_speed = right_motor_speed;
287 logging.right_velocity = current_right_velocity;
288
289 LOG_STRUCT(DEBUG, "currently", logging);
290 }
Austin Schuh41565602016-02-28 20:10:49 -0800291 goal_left_velocity_ = current_left_velocity;
292 goal_right_velocity_ = current_right_velocity;
Comran Morshed5323ecb2015-12-26 20:50:55 +0000293
Comran Morshed2091fe52016-02-19 17:27:21 +0000294 // Any motor is not in gear. Speed match.
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800295 ::Eigen::Matrix<double, 1, 1> R_left;
296 ::Eigen::Matrix<double, 1, 1> R_right;
297 R_left(0, 0) = left_motor_speed;
298 R_right(0, 0) = right_motor_speed;
299
300 const double wiggle =
301 (static_cast<double>((counter_ % 20) / 10) - 0.5) * 5.0;
302
303 loop_->mutable_U(0, 0) = ::aos::Clip(
Comran Morshed5323ecb2015-12-26 20:50:55 +0000304 (R_left / dt_config_.v)(0, 0) + (IsInGear(left_gear_) ? 0 : wiggle),
305 -12.0, 12.0);
306 loop_->mutable_U(1, 0) = ::aos::Clip(
307 (R_right / dt_config_.v)(0, 0) + (IsInGear(right_gear_) ? 0 : wiggle),
308 -12.0, 12.0);
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800309 loop_->mutable_U() *= 12.0 / ::aos::robot_state->voltage_battery;
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800310 }
311}
312
Austin Schuh093535c2016-03-05 23:21:00 -0800313void PolyDrivetrain::SetOutput(
Comran Morshed5323ecb2015-12-26 20:50:55 +0000314 ::frc971::control_loops::DrivetrainQueue::Output *output) {
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800315 if (output != NULL) {
316 output->left_voltage = loop_->U(0, 0);
317 output->right_voltage = loop_->U(1, 0);
Austin Schuh093535c2016-03-05 23:21:00 -0800318 output->left_high = MaybeHigh(left_gear_);
319 output->right_high = MaybeHigh(right_gear_);
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800320 }
321}
322
Austin Schuh41565602016-02-28 20:10:49 -0800323void PolyDrivetrain::PopulateStatus(
324 ::frc971::control_loops::DrivetrainQueue::Status *status) {
325 status->left_velocity_goal = goal_left_velocity_;
326 status->right_velocity_goal = goal_right_velocity_;
327}
328
Austin Schuh6197a182015-11-28 16:04:40 -0800329} // namespace drivetrain
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800330} // namespace control_loops
Comran Morshed5323ecb2015-12-26 20:50:55 +0000331} // namespace frc971