blob: 598d635098015859e9773b81d17433f187b1349d [file] [log] [blame]
Brian Silvermanc71537c2016-01-01 13:43:14 -08001#include "y2012/control_loops/drivetrain/polydrivetrain.h"
2
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"
12#include "y2012/control_loops/drivetrain/drivetrain.q.h"
13#include "y2012/control_loops/drivetrain/drivetrain_dog_motor_plant.h"
14#include "y2012/control_loops/drivetrain/polydrivetrain_dog_motor_plant.h"
15
16#define HAVE_SHIFTERS 1
17
18namespace y2012 {
19namespace control_loops {
20namespace drivetrain {
21
22using ::y2012::control_loops::GearLogging;
23using ::y2012::control_loops::CIMLogging;
24using ::frc971::control_loops::CoerceGoal;
25
26PolyDrivetrain::PolyDrivetrain()
27 : U_Poly_((Eigen::Matrix<double, 4, 2>() << /*[[*/ 1, 0 /*]*/,
28 /*[*/ -1, 0 /*]*/,
29 /*[*/ 0, 1 /*]*/,
30 /*[*/ 0, -1 /*]]*/).finished(),
31 (Eigen::Matrix<double, 4, 1>() << /*[[*/ 12 /*]*/,
32 /*[*/ 12 /*]*/,
33 /*[*/ 12 /*]*/,
34 /*[*/ 12 /*]]*/).finished()),
35 loop_(new StateFeedbackLoop<2, 2, 2>(
36 ::y2012::control_loops::drivetrain::MakeVelocityDrivetrainLoop())),
37 ttrust_(1.1),
38 wheel_(0.0),
39 throttle_(0.0),
40 quickturn_(false),
41 stale_count_(0),
42 position_time_delta_(kDt),
43 left_gear_(LOW),
44 right_gear_(LOW),
45 counter_(0) {
46 last_position_.Zero();
47 position_.Zero();
48}
49
50double PolyDrivetrain::MotorSpeed(bool high_gear, double velocity) {
51 if (high_gear) {
52 return velocity / kHighGearRatio / kWheelRadius;
53 } else {
54 return velocity / kLowGearRatio / kWheelRadius;
55 }
56}
57
58void PolyDrivetrain::SetGoal(double wheel, double throttle, bool quickturn,
59 bool highgear) {
60 const double kWheelNonLinearity = 0.3;
61 // Apply a sin function that's scaled to make it feel better.
62 const double angular_range = M_PI_2 * kWheelNonLinearity;
63
64 wheel_ = sin(angular_range * wheel) / sin(angular_range);
65 wheel_ = sin(angular_range * wheel_) / sin(angular_range);
66 quickturn_ = quickturn;
67
68 static const double kThrottleDeadband = 0.05;
69 if (::std::abs(throttle) < kThrottleDeadband) {
70 throttle_ = 0;
71 } else {
72 throttle_ = copysign(
73 (::std::abs(throttle) - kThrottleDeadband) / (1.0 - kThrottleDeadband),
74 throttle);
75 }
76
77 // TODO(austin): Fix the upshift logic to include states.
78 Gear requested_gear = highgear ? HIGH : LOW;
79
80 const Gear shift_up = HIGH;
81 const Gear shift_down = LOW;
82
83 if (left_gear_ != requested_gear) {
84 if (IsInGear(left_gear_)) {
85 if (requested_gear == HIGH) {
86 left_gear_ = shift_up;
87 } else {
88 left_gear_ = shift_down;
89 }
90 } else {
91 if (requested_gear == HIGH && left_gear_ == SHIFTING_DOWN) {
92 left_gear_ = SHIFTING_UP;
93 } else if (requested_gear == LOW && left_gear_ == SHIFTING_UP) {
94 left_gear_ = SHIFTING_DOWN;
95 }
96 }
97 }
98 if (right_gear_ != requested_gear) {
99 if (IsInGear(right_gear_)) {
100 if (requested_gear == HIGH) {
101 right_gear_ = shift_up;
102 } else {
103 right_gear_ = shift_down;
104 }
105 } else {
106 if (requested_gear == HIGH && right_gear_ == SHIFTING_DOWN) {
107 right_gear_ = SHIFTING_UP;
108 } else if (requested_gear == LOW && right_gear_ == SHIFTING_UP) {
109 right_gear_ = SHIFTING_DOWN;
110 }
111 }
112 }
113}
114void PolyDrivetrain::SetPosition(
115 const ::y2012::control_loops::DrivetrainQueue::Position *position) {
116 if (position == NULL) {
117 ++stale_count_;
118 } else {
119 last_position_ = position_;
120 position_ = *position;
121 position_time_delta_ = (stale_count_ + 1) * kDt;
122 stale_count_ = 0;
123 }
124
125#if HAVE_SHIFTERS
126 if (position) {
127 if (left_gear_ == LOW) {
128 if (right_gear_ == LOW) {
129 loop_->set_controller_index(0);
130 } else {
131 loop_->set_controller_index(1);
132 }
133 } else {
134 if (right_gear_ == LOW) {
135 loop_->set_controller_index(2);
136 } else {
137 loop_->set_controller_index(3);
138 }
139 }
140 }
141#endif
142}
143
144double PolyDrivetrain::FilterVelocity(double throttle) {
145 const Eigen::Matrix<double, 2, 2> FF =
146 loop_->B().inverse() *
147 (Eigen::Matrix<double, 2, 2>::Identity() - loop_->A());
148
149 constexpr int kHighGearController = 3;
150 const Eigen::Matrix<double, 2, 2> FF_high =
151 loop_->controller(kHighGearController).plant.B().inverse() *
152 (Eigen::Matrix<double, 2, 2>::Identity() -
153 loop_->controller(kHighGearController).plant.A());
154
155 ::Eigen::Matrix<double, 1, 2> FF_sum = FF.colwise().sum();
156 int min_FF_sum_index;
157 const double min_FF_sum = FF_sum.minCoeff(&min_FF_sum_index);
158 const double min_K_sum = loop_->K().col(min_FF_sum_index).sum();
159 const double high_min_FF_sum = FF_high.col(0).sum();
160
161 const double adjusted_ff_voltage =
162 ::aos::Clip(throttle * 12.0 * min_FF_sum / high_min_FF_sum, -12.0, 12.0);
163 return (adjusted_ff_voltage +
164 ttrust_ * min_K_sum * (loop_->X_hat(0, 0) + loop_->X_hat(1, 0)) /
165 2.0) /
166 (ttrust_ * min_K_sum + min_FF_sum);
167}
168
169double PolyDrivetrain::MaxVelocity() {
170 const Eigen::Matrix<double, 2, 2> FF =
171 loop_->B().inverse() *
172 (Eigen::Matrix<double, 2, 2>::Identity() - loop_->A());
173
174 constexpr int kHighGearController = 3;
175 const Eigen::Matrix<double, 2, 2> FF_high =
176 loop_->controller(kHighGearController).plant.B().inverse() *
177 (Eigen::Matrix<double, 2, 2>::Identity() -
178 loop_->controller(kHighGearController).plant.A());
179
180 ::Eigen::Matrix<double, 1, 2> FF_sum = FF.colwise().sum();
181 int min_FF_sum_index;
182 const double min_FF_sum = FF_sum.minCoeff(&min_FF_sum_index);
183 // const double min_K_sum = loop_->K().col(min_FF_sum_index).sum();
184 const double high_min_FF_sum = FF_high.col(0).sum();
185
186 const double adjusted_ff_voltage =
187 ::aos::Clip(12.0 * min_FF_sum / high_min_FF_sum, -12.0, 12.0);
188 return adjusted_ff_voltage / min_FF_sum;
189}
190
191void PolyDrivetrain::Update() {
192 // TODO(austin): Observer for the current velocity instead of difference
193 // calculations.
194 ++counter_;
195#if HAVE_SHIFTERS
196 const double current_left_velocity =
197 (position_.left_encoder - last_position_.left_encoder) /
198 position_time_delta_;
199 const double current_right_velocity =
200 (position_.right_encoder - last_position_.right_encoder) /
201 position_time_delta_;
202 const double left_motor_speed =
203 MotorSpeed(left_gear_ == HIGH, current_left_velocity);
204 const double right_motor_speed =
205 MotorSpeed(right_gear_ == HIGH, current_right_velocity);
206
207 {
208 CIMLogging logging;
209
210 // Reset the CIM model to the current conditions to be ready for when we
211 // shift.
212 if (IsInGear(left_gear_)) {
213 logging.left_in_gear = true;
214 } else {
215 logging.left_in_gear = false;
216 }
217 logging.left_motor_speed = left_motor_speed;
218 logging.left_velocity = current_left_velocity;
219 if (IsInGear(right_gear_)) {
220 logging.right_in_gear = true;
221 } else {
222 logging.right_in_gear = false;
223 }
224 logging.right_motor_speed = right_motor_speed;
225 logging.right_velocity = current_right_velocity;
226
227 LOG_STRUCT(DEBUG, "currently", logging);
228 }
229#endif
230
231#if HAVE_SHIFTERS
232 if (IsInGear(left_gear_) && IsInGear(right_gear_))
233#endif
234 {
235 // FF * X = U (steady state)
236 const Eigen::Matrix<double, 2, 2> FF =
237 loop_->B().inverse() *
238 (Eigen::Matrix<double, 2, 2>::Identity() - loop_->A());
239
240 // Invert the plant to figure out how the velocity filter would have to
241 // work
242 // out in order to filter out the forwards negative inertia.
243 // This math assumes that the left and right power and velocity are
244 // equals,
245 // and that the plant is the same on the left and right.
246 const double fvel = FilterVelocity(throttle_);
247
248 const double sign_svel = wheel_ * ((fvel > 0.0) ? 1.0 : -1.0);
249 double steering_velocity;
250 if (quickturn_) {
251 steering_velocity = wheel_ * MaxVelocity();
252 } else {
253 steering_velocity = ::std::abs(fvel) * wheel_;
254 }
255 const double left_velocity = fvel - steering_velocity;
256 const double right_velocity = fvel + steering_velocity;
257
258 // Integrate velocity to get the position.
259 // This position is used to get integral control.
260 loop_->mutable_R() << left_velocity, right_velocity;
261
262 if (!quickturn_) {
263 // K * R = w
264 Eigen::Matrix<double, 1, 2> equality_k;
265 equality_k << 1 + sign_svel, -(1 - sign_svel);
266 const double equality_w = 0.0;
267
268 // Construct a constraint on R by manipulating the constraint on U
269 ::aos::controls::HPolytope<2> R_poly = ::aos::controls::HPolytope<2>(
270 U_Poly_.H() * (loop_->K() + FF),
271 U_Poly_.k() + U_Poly_.H() * loop_->K() * loop_->X_hat());
272
273 // Limit R back inside the box.
274 loop_->mutable_R() =
275 CoerceGoal(R_poly, equality_k, equality_w, loop_->R());
276 }
277
278 const Eigen::Matrix<double, 2, 1> FF_volts = FF * loop_->R();
279 const Eigen::Matrix<double, 2, 1> U_ideal =
280 loop_->K() * (loop_->R() - loop_->X_hat()) + FF_volts;
281
282 for (int i = 0; i < 2; i++) {
283 loop_->mutable_U()[i] = ::aos::Clip(U_ideal[i], -12, 12);
284 }
285
286 // TODO(austin): Model this better.
287 // TODO(austin): Feed back?
288 loop_->mutable_X_hat() =
289 loop_->A() * loop_->X_hat() + loop_->B() * loop_->U();
290#if HAVE_SHIFTERS
291 } else {
292 // Any motor is not in gear. Speed match.
293 ::Eigen::Matrix<double, 1, 1> R_left;
294 ::Eigen::Matrix<double, 1, 1> R_right;
295 R_left(0, 0) = left_motor_speed;
296 R_right(0, 0) = right_motor_speed;
297
298 const double wiggle =
299 (static_cast<double>((counter_ % 20) / 10) - 0.5) * 5.0;
300
301 loop_->mutable_U(0, 0) = ::aos::Clip(
302 (R_left / Kv)(0, 0) + (IsInGear(left_gear_) ? 0 : wiggle), -12.0, 12.0);
303 loop_->mutable_U(1, 0) =
304 ::aos::Clip((R_right / Kv)(0, 0) + (IsInGear(right_gear_) ? 0 : wiggle),
305 -12.0, 12.0);
306 loop_->mutable_U() *= 12.0 / ::aos::robot_state->voltage_battery;
307#endif
308 }
309}
310
311void PolyDrivetrain::SendMotors(
312 ::y2012::control_loops::DrivetrainQueue::Output *output) {
313 if (output != NULL) {
314 output->left_voltage = loop_->U(0, 0);
315 output->right_voltage = loop_->U(1, 0);
316 output->left_high = left_gear_ == HIGH || left_gear_ == SHIFTING_UP;
317 output->right_high = right_gear_ == HIGH || right_gear_ == SHIFTING_UP;
318 }
319}
320
321constexpr double PolyDrivetrain::kStallTorque;
322constexpr double PolyDrivetrain::kStallCurrent;
323constexpr double PolyDrivetrain::kFreeSpeed;
324constexpr double PolyDrivetrain::kFreeCurrent;
325constexpr double PolyDrivetrain::kWheelRadius;
326constexpr double PolyDrivetrain::kR;
327constexpr double PolyDrivetrain::Kv;
328constexpr double PolyDrivetrain::Kt;
329
330} // namespace drivetrain
331} // namespace control_loops
332} // namespace y2012