blob: c0c5983a6b201246636c47d7f6f533118d0bd522 [file] [log] [blame]
James Kuszmaulf254c1a2013-03-10 16:31:26 -07001#include "frc971/control_loops/drivetrain/drivetrain.h"
brians343bc112013-02-10 01:53:46 +00002
3#include <stdio.h>
4#include <sched.h>
5#include <cmath>
Austin Schuh4352ac62013-03-19 06:23:16 +00006#include <memory>
Austin Schuh2054f5f2013-10-27 14:54:10 -07007#include "Eigen/Dense"
brians343bc112013-02-10 01:53:46 +00008
brians343bc112013-02-10 01:53:46 +00009#include "aos/common/logging/logging.h"
10#include "aos/common/queue.h"
Austin Schuh2054f5f2013-10-27 14:54:10 -070011#include "aos/controls/polytope.h"
12#include "aos/common/commonmath.h"
James Kuszmaulf254c1a2013-03-10 16:31:26 -070013#include "frc971/control_loops/state_feedback_loop.h"
14#include "frc971/control_loops/drivetrain/drivetrain_motor_plant.h"
Austin Schuh2054f5f2013-10-27 14:54:10 -070015#include "frc971/control_loops/drivetrain/polydrivetrain_motor_plant.h"
Austin Schuh427b3702013-11-02 13:44:09 -070016#include "frc971/control_loops/drivetrain/polydrivetrain_cim_plant.h"
James Kuszmaulf254c1a2013-03-10 16:31:26 -070017#include "frc971/control_loops/drivetrain/drivetrain.q.h"
brians343bc112013-02-10 01:53:46 +000018#include "frc971/queues/GyroAngle.q.h"
19#include "frc971/queues/Piston.q.h"
20
21using frc971::sensors::gyro;
22
23namespace frc971 {
24namespace control_loops {
25
26// Width of the robot.
27const double width = 22.0 / 100.0 * 2.54;
28
Austin Schuh2054f5f2013-10-27 14:54:10 -070029Eigen::Matrix<double, 2, 1> CoerceGoal(aos::controls::HPolytope<2> &region,
30 const Eigen::Matrix<double, 1, 2> &K,
31 double w,
32 const Eigen::Matrix<double, 2, 1> &R) {
33 if (region.IsInside(R)) {
34 return R;
35 }
36 Eigen::Matrix<double, 2, 1> parallel_vector;
37 Eigen::Matrix<double, 2, 1> perpendicular_vector;
38 perpendicular_vector = K.transpose().normalized();
39 parallel_vector << perpendicular_vector(1, 0), -perpendicular_vector(0, 0);
40
41 aos::controls::HPolytope<1> t_poly(
42 region.H() * parallel_vector,
43 region.k() - region.H() * perpendicular_vector * w);
44
45 Eigen::Matrix<double, 1, Eigen::Dynamic> vertices = t_poly.Vertices();
46 if (vertices.innerSize() > 0) {
47 double min_distance_sqr = 0;
48 Eigen::Matrix<double, 2, 1> closest_point;
49 for (int i = 0; i < vertices.innerSize(); i++) {
50 Eigen::Matrix<double, 2, 1> point;
51 point = parallel_vector * vertices(0, i) + perpendicular_vector * w;
52 const double length = (R - point).squaredNorm();
53 if (i == 0 || length < min_distance_sqr) {
54 closest_point = point;
55 min_distance_sqr = length;
56 }
57 }
58 return closest_point;
59 } else {
60 Eigen::Matrix<double, 2, Eigen::Dynamic> region_vertices =
61 region.Vertices();
62 double min_distance;
63 int closest_i = 0;
64 for (int i = 0; i < region_vertices.outerSize(); i++) {
65 const double length = ::std::abs(
66 (perpendicular_vector.transpose() * (region_vertices.col(i)))(0, 0));
67 if (i == 0 || length < min_distance) {
68 closest_i = i;
69 min_distance = length;
70 }
71 }
72 return region_vertices.col(closest_i);
73 }
74}
75
Austin Schuh4352ac62013-03-19 06:23:16 +000076class DrivetrainMotorsSS {
brians343bc112013-02-10 01:53:46 +000077 public:
Austin Schuh4352ac62013-03-19 06:23:16 +000078 DrivetrainMotorsSS ()
79 : loop_(new StateFeedbackLoop<4, 2, 2>(MakeDrivetrainLoop())) {
brians343bc112013-02-10 01:53:46 +000080 _offset = 0;
81 _integral_offset = 0;
82 _left_goal = 0.0;
83 _right_goal = 0.0;
84 _raw_left = 0.0;
85 _raw_right = 0.0;
Austin Schuh4352ac62013-03-19 06:23:16 +000086 _control_loop_driving = false;
brians343bc112013-02-10 01:53:46 +000087 }
88 void SetGoal(double left, double left_velocity, double right, double right_velocity) {
89 _left_goal = left;
90 _right_goal = right;
Austin Schuh4352ac62013-03-19 06:23:16 +000091 loop_->R << left, left_velocity, right, right_velocity;
brians343bc112013-02-10 01:53:46 +000092 }
93 void SetRawPosition(double left, double right) {
94 _raw_right = right;
95 _raw_left = left;
Austin Schuh4352ac62013-03-19 06:23:16 +000096 loop_->Y << left, right;
brians343bc112013-02-10 01:53:46 +000097 }
Austin Schuh4352ac62013-03-19 06:23:16 +000098 void SetPosition(
99 double left, double right, double gyro, bool control_loop_driving) {
brians343bc112013-02-10 01:53:46 +0000100 // Decay the offset quickly because this gyro is great.
101 _offset = (0.25) * (right - left - gyro * width) / 2.0 + 0.75 * _offset;
102 const double angle_error = (_right_goal - _left_goal) / width - (_raw_right - _offset - _raw_left - _offset) / width;
Austin Schuh4352ac62013-03-19 06:23:16 +0000103 // TODO(aschuh): Add in the gyro.
104 _integral_offset = 0.0;
105 _offset = 0.0;
brians343bc112013-02-10 01:53:46 +0000106 _gyro = gyro;
Austin Schuh4352ac62013-03-19 06:23:16 +0000107 _control_loop_driving = control_loop_driving;
brians343bc112013-02-10 01:53:46 +0000108 SetRawPosition(left, right);
109 LOG(DEBUG, "Left %f->%f Right %f->%f Gyro %f aerror %f ioff %f\n", left + _offset, _left_goal, right - _offset, _right_goal, gyro, angle_error, _integral_offset);
110 }
Austin Schuh4352ac62013-03-19 06:23:16 +0000111
112 void Update(bool update_observer, bool stop_motors) {
113 loop_->Update(update_observer, stop_motors);
brians343bc112013-02-10 01:53:46 +0000114 }
115
Austin Schuh4352ac62013-03-19 06:23:16 +0000116 void SendMotors(Drivetrain::Output *output) {
117 if (output) {
118 output->left_voltage = loop_->U(0, 0);
119 output->right_voltage = loop_->U(1, 0);
brians8ad74052013-03-16 21:04:51 +0000120 }
brians343bc112013-02-10 01:53:46 +0000121 }
122 void PrintMotors() const {
123 // LOG(DEBUG, "Left Power %f Right Power %f lg %f rg %f le %f re %f gyro %f\n", U[0], U[1], R[0], R[2], Y[0], Y[1], _gyro);
Austin Schuh4352ac62013-03-19 06:23:16 +0000124 ::Eigen::Matrix<double, 4, 1> E = loop_->R - loop_->X_hat;
125 LOG(DEBUG, "E[0, 0]: %f E[1, 0] %f E[2, 0] %f E[3, 0] %f\n", E(0, 0), E(1, 0), E(2, 0), E(3, 0));
brians343bc112013-02-10 01:53:46 +0000126 }
127
128 private:
Austin Schuh4352ac62013-03-19 06:23:16 +0000129 ::std::unique_ptr<StateFeedbackLoop<4, 2, 2>> loop_;
130
brians343bc112013-02-10 01:53:46 +0000131 double _integral_offset;
132 double _offset;
133 double _gyro;
134 double _left_goal;
135 double _right_goal;
136 double _raw_left;
137 double _raw_right;
Austin Schuh4352ac62013-03-19 06:23:16 +0000138 bool _control_loop_driving;
brians343bc112013-02-10 01:53:46 +0000139};
140
Austin Schuh2054f5f2013-10-27 14:54:10 -0700141class PolyDrivetrain {
142 public:
Austin Schuh427b3702013-11-02 13:44:09 -0700143
144 enum Gear {
145 HIGH,
146 LOW,
147 SHIFTING_UP,
148 SHIFTING_DOWN
149 };
150 // Stall Torque in N m
151 static constexpr double kStallTorque = 2.42;
152 // Stall Current in Amps
153 static constexpr double kStallCurrent = 133;
154 // Free Speed in RPM. Used number from last year.
155 static constexpr double kFreeSpeed = 4650.0;
156 // Free Current in Amps
157 static constexpr double kFreeCurrent = 2.7;
158 // Moment of inertia of the drivetrain in kg m^2
159 // Just borrowed from last year.
160 static constexpr double J = 6.4;
161 // Mass of the robot, in kg.
162 static constexpr double m = 68;
163 // Radius of the robot, in meters (from last year).
164 static constexpr double rb = 0.617998644 / 2.0;
165 static constexpr double kWheelRadius = .04445;
166 // Resistance of the motor, divided by the number of motors.
167 static constexpr double kR = (12.0 / kStallCurrent / 4 + 0.03) / (0.93 * 0.93);
168 // Motor velocity constant
169 static constexpr double Kv =
170 ((kFreeSpeed / 60.0 * 2.0 * M_PI) / (12.0 - kR * kFreeCurrent));
171 // Torque constant
172 static constexpr double Kt = kStallTorque / kStallCurrent;
173 // Gear ratios
174 static constexpr double G_low = 16.0 / 60.0 * 19.0 / 50.0;
175 static constexpr double G_high = 28.0 / 48.0 * 19.0 / 50.0;
176
Austin Schuh2054f5f2013-10-27 14:54:10 -0700177 PolyDrivetrain()
178 : U_Poly_((Eigen::Matrix<double, 4, 2>() << /*[[*/ 1, 0 /*]*/,
179 /*[*/ -1, 0 /*]*/,
180 /*[*/ 0, 1 /*]*/,
181 /*[*/ 0, -1 /*]]*/).finished(),
182 (Eigen::Matrix<double, 4, 1>() << /*[[*/ 12 /*]*/,
183 /*[*/ 12 /*]*/,
184 /*[*/ 12 /*]*/,
185 /*[*/ 12 /*]]*/).finished()),
Austin Schuh427b3702013-11-02 13:44:09 -0700186 loop_(new StateFeedbackLoop<2, 2, 2>(MakeVDrivetrainLoop())),
187 left_cim_(new StateFeedbackLoop<1, 1, 1>(MakeCIMLoop())),
188 right_cim_(new StateFeedbackLoop<1, 1, 1>(MakeCIMLoop())),
189 ttrust_(1.1),
190 wheel_(0.0),
191 throttle_(0.0),
192 quickturn_(false),
193 stale_count_(0),
194 position_time_delta_(0.01),
195 left_gear_(LOW),
196 right_gear_(LOW) {
Austin Schuh2054f5f2013-10-27 14:54:10 -0700197
Austin Schuh427b3702013-11-02 13:44:09 -0700198 last_position_.Zero();
199 position_.Zero();
Austin Schuh2054f5f2013-10-27 14:54:10 -0700200 }
Austin Schuh427b3702013-11-02 13:44:09 -0700201 static bool IsInGear(Gear gear) { return gear == LOW || gear == HIGH; }
202
203 static double MotorSpeed(double shifter_position, double velocity) {
204 // TODO(austin): G_high, G_low and kWheelRadius
205 if (shifter_position > 0.5) {
206 return velocity / G_high / kWheelRadius;
207 } else {
208 return velocity / G_low / kWheelRadius;
209 }
210 }
211
Austin Schuh2054f5f2013-10-27 14:54:10 -0700212 void SetGoal(double wheel, double throttle, bool quickturn, bool highgear) {
Brian Silverman718b1d72013-10-28 16:22:45 -0700213 const double kWheelNonLinearity = 0.4;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700214 // Apply a sin function that's scaled to make it feel better.
215 const double angular_range = M_PI_2 * kWheelNonLinearity;
216 wheel_ = sin(angular_range * wheel) / sin(angular_range);
217 wheel_ = sin(angular_range * wheel_) / sin(angular_range);
218 throttle_ = throttle;
219 quickturn_ = quickturn;
Austin Schuh427b3702013-11-02 13:44:09 -0700220
221 // TODO(austin): Fix the upshift logic to include states.
222 const Gear requested_gear = highgear ? HIGH : LOW;
223
224 if (left_gear_ != requested_gear) {
225 if (IsInGear(left_gear_)) {
226 if (requested_gear == HIGH) {
227 left_gear_ = SHIFTING_UP;
228 } else {
229 left_gear_ = SHIFTING_DOWN;
230 }
231 }
232 }
233 if (right_gear_ != requested_gear) {
234 if (IsInGear(right_gear_)) {
235 if (requested_gear == HIGH) {
236 right_gear_ = SHIFTING_UP;
237 } else {
238 right_gear_ = SHIFTING_DOWN;
239 }
240 }
Austin Schuh2054f5f2013-10-27 14:54:10 -0700241 }
242 }
Austin Schuh427b3702013-11-02 13:44:09 -0700243 void SetPosition(const Drivetrain::Position *position) {
244 if (position == NULL) {
245 ++stale_count_;
246 } else {
247 last_position_ = position_;
248 position_ = *position;
249 position_time_delta_ = (stale_count_ + 1) * 0.01;
250 stale_count_ = 0;
251 }
252
253 if (position) {
254 // Switch to the correct controller.
255 // TODO(austin): Un-hard code 0.5
256 if (position->left_shifter_position < 0.5) {
257 if (position->right_shifter_position < 0.5) {
258 loop_->set_controller_index(0);
259 } else {
260 loop_->set_controller_index(1);
261 }
262 } else {
263 if (position->right_shifter_position < 0.5) {
264 loop_->set_controller_index(2);
265 } else {
266 loop_->set_controller_index(3);
267 }
268 }
269 // TODO(austin): Constants.
270 if (position->left_shifter_position > 0.9 && left_gear_ == SHIFTING_UP) {
271 left_gear_ = HIGH;
272 }
273 if (position->left_shifter_position < 0.1 && left_gear_ == SHIFTING_DOWN) {
274 left_gear_ = LOW;
275 }
276 if (position->right_shifter_position > 0.9 && right_gear_ == SHIFTING_UP) {
277 right_gear_ = HIGH;
278 }
279 if (position->right_shifter_position < 0.1 && right_gear_ == SHIFTING_DOWN) {
280 right_gear_ = LOW;
281 }
282 }
283 }
284
Austin Schuh2054f5f2013-10-27 14:54:10 -0700285 double FilterVelocity(double throttle) {
286 const Eigen::Matrix<double, 2, 2> FF =
287 loop_->B().inverse() *
288 (Eigen::Matrix<double, 2, 2>::Identity() - loop_->A());
289
290 constexpr int kHighGearController = 3;
291 const Eigen::Matrix<double, 2, 2> FF_high =
292 loop_->controller(kHighGearController).plant.B.inverse() *
293 (Eigen::Matrix<double, 2, 2>::Identity() -
294 loop_->controller(kHighGearController).plant.A);
295
296 ::Eigen::Matrix<double, 1, 2> FF_sum = FF.colwise().sum();
297 int min_FF_sum_index;
298 const double min_FF_sum = FF_sum.minCoeff(&min_FF_sum_index);
299 const double min_K_sum = loop_->K().col(min_FF_sum_index).sum();
300 const double high_min_FF_sum = FF_high.col(0).sum();
301
302 const double adjusted_ff_voltage = ::aos::Clip(
303 throttle * 12.0 * min_FF_sum / high_min_FF_sum, -12.0, 12.0);
304 return ((adjusted_ff_voltage +
305 ttrust_ * min_K_sum * (loop_->X_hat(0, 0) + loop_->X_hat(1, 0)) /
306 2.0) /
307 (ttrust_ * min_K_sum + min_FF_sum));
308 }
309
Brian Silverman718b1d72013-10-28 16:22:45 -0700310 double MaxVelocity() {
311 const Eigen::Matrix<double, 2, 2> FF =
312 loop_->B().inverse() *
313 (Eigen::Matrix<double, 2, 2>::Identity() - loop_->A());
314
315 constexpr int kHighGearController = 3;
316 const Eigen::Matrix<double, 2, 2> FF_high =
317 loop_->controller(kHighGearController).plant.B.inverse() *
318 (Eigen::Matrix<double, 2, 2>::Identity() -
319 loop_->controller(kHighGearController).plant.A);
320
321 ::Eigen::Matrix<double, 1, 2> FF_sum = FF.colwise().sum();
322 int min_FF_sum_index;
323 const double min_FF_sum = FF_sum.minCoeff(&min_FF_sum_index);
324 //const double min_K_sum = loop_->K().col(min_FF_sum_index).sum();
325 const double high_min_FF_sum = FF_high.col(0).sum();
326
327 const double adjusted_ff_voltage = ::aos::Clip(
328 12.0 * min_FF_sum / high_min_FF_sum, -12.0, 12.0);
329 return adjusted_ff_voltage / min_FF_sum;
330 }
331
Austin Schuh2054f5f2013-10-27 14:54:10 -0700332 void Update() {
Austin Schuh427b3702013-11-02 13:44:09 -0700333 // TODO(austin): Observer for the current velocity instead of difference
334 // calculations.
335 const double current_left_velocity =
336 (position_.left_encoder - last_position_.left_encoder) * 100.0 /
337 position_time_delta_;
338 const double current_right_velocity =
339 (position_.right_encoder - last_position_.right_encoder) * 100.0 /
340 position_time_delta_;
341 const double left_motor_speed =
342 MotorSpeed(position_.left_shifter_position, current_left_velocity);
343 const double right_motor_speed =
344 MotorSpeed(position_.right_shifter_position, current_right_velocity);
Austin Schuh2054f5f2013-10-27 14:54:10 -0700345
Austin Schuh427b3702013-11-02 13:44:09 -0700346 // Reset the CIM model to the current conditions to be ready for when we shift.
347 if (IsInGear(left_gear_)) {
348 left_cim_->X_hat(0, 0) = left_motor_speed;
349 }
350 if (IsInGear(right_gear_)) {
351 right_cim_->X_hat(1, 0) = right_motor_speed;
352 }
Austin Schuh2054f5f2013-10-27 14:54:10 -0700353
Austin Schuh427b3702013-11-02 13:44:09 -0700354 if (IsInGear(left_gear_) && IsInGear(right_gear_)) {
355 // FF * X = U (steady state)
356 const Eigen::Matrix<double, 2, 2> FF =
357 loop_->B().inverse() *
358 (Eigen::Matrix<double, 2, 2>::Identity() - loop_->A());
359
360 // Invert the plant to figure out how the velocity filter would have to
361 // work
362 // out in order to filter out the forwards negative inertia.
363 // This math assumes that the left and right power and velocity are
364 // equals,
365 // and that the plant is the same on the left and right.
366 const double fvel = FilterVelocity(throttle_);
367
368 const double sign_svel = wheel_ * ((fvel > 0.0) ? 1.0 : -1.0);
369 double steering_velocity;
370 if (quickturn_) {
371 steering_velocity = wheel_ * MaxVelocity();
372 } else {
373 steering_velocity = ::std::abs(fvel) * wheel_;
374 }
375 const double left_velocity = fvel - steering_velocity;
376 const double right_velocity = fvel + steering_velocity;
377
378 // Integrate velocity to get the position.
379 // This position is used to get integral control.
380 loop_->R << left_velocity, right_velocity;
381
382 if (!quickturn_) {
383 // K * R = w
384 Eigen::Matrix<double, 1, 2> equality_k;
385 equality_k << 1 + sign_svel, -(1 - sign_svel);
386 const double equality_w = 0.0;
387
388 // Construct a constraint on R by manipulating the constraint on U
389 ::aos::controls::HPolytope<2> R_poly = ::aos::controls::HPolytope<2>(
390 U_Poly_.H() * (loop_->K() + FF),
391 U_Poly_.k() + U_Poly_.H() * loop_->K() * loop_->X_hat);
392
393 // Limit R back inside the box.
394 loop_->R = CoerceGoal(R_poly, equality_k, equality_w, loop_->R);
395 }
396
397 const Eigen::Matrix<double, 2, 1> FF_volts = FF * loop_->R;
398 const Eigen::Matrix<double, 2, 1> U_ideal =
399 loop_->K() * (loop_->R - loop_->X_hat) + FF_volts;
400
401 for (int i = 0; i < 2; i++) {
402 loop_->U[i] = ::aos::Clip(U_ideal[i], -12, 12);
403 }
Brian Silverman718b1d72013-10-28 16:22:45 -0700404 } else {
Austin Schuh427b3702013-11-02 13:44:09 -0700405 // Any motor is not in gear. Speed match.
406 ::Eigen::Matrix<double, 1, 1> R_left;
407 R_left(0, 0) = left_motor_speed;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700408
Austin Schuh427b3702013-11-02 13:44:09 -0700409 // TODO(austin): Use battery volts here at some point.
410 loop_->U(0, 0) = ::aos::Clip(
411 (left_cim_->K() * (R_left - left_cim_->X_hat) + R_left / Kv)(0, 0), -12, 12);
412 right_cim_->X_hat = right_cim_->A() * right_cim_->X_hat + right_cim_->B() * loop_->U(0, 0);
Austin Schuh2054f5f2013-10-27 14:54:10 -0700413
Austin Schuh427b3702013-11-02 13:44:09 -0700414 ::Eigen::Matrix<double, 1, 1> R_right;
415 R_right(0, 0) = right_motor_speed;
416 loop_->U(1, 0) = ::aos::Clip(
417 (right_cim_->K() * (R_right - right_cim_->X_hat) + R_right / Kv)(0, 0), -12,
418 12);
419 right_cim_->X_hat = right_cim_->A() * right_cim_->X_hat + right_cim_->B() * loop_->U(1, 0);
Brian Silverman718b1d72013-10-28 16:22:45 -0700420 }
Austin Schuh2054f5f2013-10-27 14:54:10 -0700421
Austin Schuh427b3702013-11-02 13:44:09 -0700422 if (IsInGear(left_gear_) && IsInGear(right_gear_)) {
423 // TODO(austin): Model this better.
424 // TODO(austin): Feed back?
425 loop_->X_hat = loop_->A() * loop_->X_hat + loop_->B() * loop_->U;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700426 }
Austin Schuh2054f5f2013-10-27 14:54:10 -0700427 }
428
429 void SendMotors(Drivetrain::Output *output) {
430 LOG(DEBUG, "left pwm: %f right pwm: %f wheel: %f throttle: %f\n",
431 loop_->U(0, 0), loop_->U(1, 0), wheel_, throttle_);
432 output->left_voltage = loop_->U(0, 0);
433 output->right_voltage = loop_->U(1, 0);
Austin Schuh427b3702013-11-02 13:44:09 -0700434 // Go in high gear if anything wants to be in high gear.
435 // TODO(austin): Seperate these.
436 if (left_gear_ == HIGH || left_gear_ == SHIFTING_UP ||
437 right_gear_ == HIGH || right_gear_ == SHIFTING_UP) {
Austin Schuh2054f5f2013-10-27 14:54:10 -0700438 shifters.MakeWithBuilder().set(false).Send();
439 } else {
440 shifters.MakeWithBuilder().set(true).Send();
441 }
442 }
443
444 private:
445 const ::aos::controls::HPolytope<2> U_Poly_;
446
447 ::std::unique_ptr<StateFeedbackLoop<2, 2, 2>> loop_;
Austin Schuh427b3702013-11-02 13:44:09 -0700448 ::std::unique_ptr<StateFeedbackLoop<1, 1, 1>> left_cim_;
449 ::std::unique_ptr<StateFeedbackLoop<1, 1, 1>> right_cim_;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700450
Austin Schuh427b3702013-11-02 13:44:09 -0700451 const double ttrust_;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700452 double wheel_;
453 double throttle_;
454 bool quickturn_;
Austin Schuh427b3702013-11-02 13:44:09 -0700455 int stale_count_;
456 double position_time_delta_;
457 Gear left_gear_;
458 Gear right_gear_;
459 Drivetrain::Position last_position_;
460 Drivetrain::Position position_;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700461};
462
463
brians343bc112013-02-10 01:53:46 +0000464class DrivetrainMotorsOL {
465 public:
466 DrivetrainMotorsOL() {
467 _old_wheel = 0.0;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700468 wheel_ = 0.0;
469 throttle_ = 0.0;
470 quickturn_ = false;
471 highgear_ = true;
brians343bc112013-02-10 01:53:46 +0000472 _neg_inertia_accumulator = 0.0;
473 _left_pwm = 0.0;
474 _right_pwm = 0.0;
475 }
476 void SetGoal(double wheel, double throttle, bool quickturn, bool highgear) {
Austin Schuh2054f5f2013-10-27 14:54:10 -0700477 wheel_ = wheel;
478 throttle_ = throttle;
479 quickturn_ = quickturn;
480 highgear_ = highgear;
brians343bc112013-02-10 01:53:46 +0000481 _left_pwm = 0.0;
482 _right_pwm = 0.0;
483 }
Austin Schuh427b3702013-11-02 13:44:09 -0700484 void Update() {
brians343bc112013-02-10 01:53:46 +0000485 double overPower;
486 float sensitivity = 1.7;
487 float angular_power;
488 float linear_power;
489 double wheel;
490
Austin Schuh2054f5f2013-10-27 14:54:10 -0700491 double neg_inertia = wheel_ - _old_wheel;
492 _old_wheel = wheel_;
brians343bc112013-02-10 01:53:46 +0000493
494 double wheelNonLinearity;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700495 if (highgear_) {
Brian Silverman77a76002013-03-16 20:09:00 -0700496 wheelNonLinearity = 0.1; // used to be csvReader->TURN_NONLIN_HIGH
brians343bc112013-02-10 01:53:46 +0000497 // Apply a sin function that's scaled to make it feel better.
498 const double angular_range = M_PI / 2.0 * wheelNonLinearity;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700499 wheel = sin(angular_range * wheel_) / sin(angular_range);
Brian Silverman77a76002013-03-16 20:09:00 -0700500 wheel = sin(angular_range * wheel) / sin(angular_range);
brians343bc112013-02-10 01:53:46 +0000501 } else {
Brian Silvermandf43ec22013-03-16 23:48:29 -0700502 wheelNonLinearity = 0.2; // used to be csvReader->TURN_NONLIN_LOW
brians343bc112013-02-10 01:53:46 +0000503 // Apply a sin function that's scaled to make it feel better.
504 const double angular_range = M_PI / 2.0 * wheelNonLinearity;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700505 wheel = sin(angular_range * wheel_) / sin(angular_range);
Brian Silverman77a76002013-03-16 20:09:00 -0700506 wheel = sin(angular_range * wheel) / sin(angular_range);
507 wheel = sin(angular_range * wheel) / sin(angular_range);
brians343bc112013-02-10 01:53:46 +0000508 }
509
Brian Silvermandf43ec22013-03-16 23:48:29 -0700510 static const double kThrottleDeadband = 0.05;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700511 if (::std::abs(throttle_) < kThrottleDeadband) {
512 throttle_ = 0;
Brian Silvermandf43ec22013-03-16 23:48:29 -0700513 } else {
Austin Schuh2054f5f2013-10-27 14:54:10 -0700514 throttle_ = copysign((::std::abs(throttle_) - kThrottleDeadband) /
515 (1.0 - kThrottleDeadband), throttle_);
Brian Silvermandf43ec22013-03-16 23:48:29 -0700516 }
517
brians343bc112013-02-10 01:53:46 +0000518 double neg_inertia_scalar;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700519 if (highgear_) {
Brian Silvermandf43ec22013-03-16 23:48:29 -0700520 neg_inertia_scalar = 8.0; // used to be csvReader->NEG_INTERTIA_HIGH
brians343bc112013-02-10 01:53:46 +0000521 sensitivity = 1.22; // used to be csvReader->SENSE_HIGH
522 } else {
523 if (wheel * neg_inertia > 0) {
Brian Silvermandf43ec22013-03-16 23:48:29 -0700524 neg_inertia_scalar = 5; // used to be csvReader->NEG_INERTIA_LOW_MORE
brians343bc112013-02-10 01:53:46 +0000525 } else {
Brian Silvermandf43ec22013-03-16 23:48:29 -0700526 if (::std::abs(wheel) > 0.65) {
527 neg_inertia_scalar = 5; // used to be csvReader->NEG_INTERTIA_LOW_LESS_EXT
brians343bc112013-02-10 01:53:46 +0000528 } else {
Brian Silvermandf43ec22013-03-16 23:48:29 -0700529 neg_inertia_scalar = 5; // used to be csvReader->NEG_INTERTIA_LOW_LESS
brians343bc112013-02-10 01:53:46 +0000530 }
531 }
532 sensitivity = 1.24; // used to be csvReader->SENSE_LOW
brians343bc112013-02-10 01:53:46 +0000533 }
534 double neg_inertia_power = neg_inertia * neg_inertia_scalar;
535 _neg_inertia_accumulator += neg_inertia_power;
536
537 wheel = wheel + _neg_inertia_accumulator;
538 if (_neg_inertia_accumulator > 1) {
539 _neg_inertia_accumulator -= 1;
540 } else if (_neg_inertia_accumulator < -1) {
541 _neg_inertia_accumulator += 1;
542 } else {
543 _neg_inertia_accumulator = 0;
544 }
545
Austin Schuh2054f5f2013-10-27 14:54:10 -0700546 linear_power = throttle_;
brians343bc112013-02-10 01:53:46 +0000547
Austin Schuh2054f5f2013-10-27 14:54:10 -0700548 if (quickturn_) {
brians343bc112013-02-10 01:53:46 +0000549 double qt_angular_power = wheel;
Brian Silvermandf43ec22013-03-16 23:48:29 -0700550 if (::std::abs(linear_power) < 0.2) {
brians343bc112013-02-10 01:53:46 +0000551 if (qt_angular_power > 1) qt_angular_power = 1.0;
552 if (qt_angular_power < -1) qt_angular_power = -1.0;
553 } else {
554 qt_angular_power = 0.0;
555 }
brians343bc112013-02-10 01:53:46 +0000556 overPower = 1.0;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700557 if (highgear_) {
brians343bc112013-02-10 01:53:46 +0000558 sensitivity = 1.0;
559 } else {
560 sensitivity = 1.0;
561 }
562 angular_power = wheel;
563 } else {
564 overPower = 0.0;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700565 angular_power = ::std::abs(throttle_) * wheel * sensitivity;
brians343bc112013-02-10 01:53:46 +0000566 }
567
568 _right_pwm = _left_pwm = linear_power;
569 _left_pwm += angular_power;
570 _right_pwm -= angular_power;
571
572 if (_left_pwm > 1.0) {
573 _right_pwm -= overPower*(_left_pwm - 1.0);
574 _left_pwm = 1.0;
575 } else if (_right_pwm > 1.0) {
576 _left_pwm -= overPower*(_right_pwm - 1.0);
577 _right_pwm = 1.0;
578 } else if (_left_pwm < -1.0) {
579 _right_pwm += overPower*(-1.0 - _left_pwm);
580 _left_pwm = -1.0;
581 } else if (_right_pwm < -1.0) {
582 _left_pwm += overPower*(-1.0 - _right_pwm);
583 _right_pwm = -1.0;
584 }
585 }
586
587 void SendMotors(Drivetrain::Output *output) {
Brian Silvermandf43ec22013-03-16 23:48:29 -0700588 LOG(DEBUG, "left pwm: %f right pwm: %f wheel: %f throttle: %f\n",
Austin Schuh2054f5f2013-10-27 14:54:10 -0700589 _left_pwm, _right_pwm, wheel_, throttle_);
brians8ad74052013-03-16 21:04:51 +0000590 if (output) {
591 output->left_voltage = _left_pwm * 12.0;
592 output->right_voltage = _right_pwm * 12.0;
593 }
Austin Schuh2054f5f2013-10-27 14:54:10 -0700594 if (highgear_) {
brians343bc112013-02-10 01:53:46 +0000595 shifters.MakeWithBuilder().set(false).Send();
596 } else {
597 shifters.MakeWithBuilder().set(true).Send();
598 }
599 }
600
601 private:
602 double _old_wheel;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700603 double wheel_;
604 double throttle_;
605 bool quickturn_;
606 bool highgear_;
brians343bc112013-02-10 01:53:46 +0000607 double _neg_inertia_accumulator;
608 double _left_pwm;
609 double _right_pwm;
brians343bc112013-02-10 01:53:46 +0000610};
611
612void DrivetrainLoop::RunIteration(const Drivetrain::Goal *goal,
613 const Drivetrain::Position *position,
614 Drivetrain::Output *output,
615 Drivetrain::Status * /*status*/) {
616 // TODO(aschuh): These should be members of the class.
617 static DrivetrainMotorsSS dt_closedloop;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700618 static PolyDrivetrain dt_openloop;
brians343bc112013-02-10 01:53:46 +0000619
620 bool bad_pos = false;
Austin Schuh427b3702013-11-02 13:44:09 -0700621 if (position == nullptr) {
James Kuszmaul3f354742013-03-10 17:27:56 -0700622 LOG(WARNING, "no position\n");
brians343bc112013-02-10 01:53:46 +0000623 bad_pos = true;
624 }
625
626 double wheel = goal->steering;
627 double throttle = goal->throttle;
628 bool quickturn = goal->quickturn;
629 bool highgear = goal->highgear;
630
631 bool control_loop_driving = goal->control_loop_driving;
632 double left_goal = goal->left_goal;
633 double right_goal = goal->right_goal;
634
Austin Schuh2054f5f2013-10-27 14:54:10 -0700635 dt_closedloop.SetGoal(left_goal, goal->left_velocity_goal, right_goal,
636 goal->right_velocity_goal);
brians343bc112013-02-10 01:53:46 +0000637 if (!bad_pos) {
638 const double left_encoder = position->left_encoder;
639 const double right_encoder = position->right_encoder;
640 if (gyro.FetchLatest()) {
Austin Schuh2054f5f2013-10-27 14:54:10 -0700641 dt_closedloop.SetPosition(left_encoder, right_encoder, gyro->angle,
642 control_loop_driving);
brians343bc112013-02-10 01:53:46 +0000643 } else {
644 dt_closedloop.SetRawPosition(left_encoder, right_encoder);
645 }
646 }
Austin Schuh427b3702013-11-02 13:44:09 -0700647 dt_openloop.SetPosition(position);
Austin Schuh4352ac62013-03-19 06:23:16 +0000648 dt_closedloop.Update(position, output == NULL);
brians343bc112013-02-10 01:53:46 +0000649 dt_openloop.SetGoal(wheel, throttle, quickturn, highgear);
650 dt_openloop.Update();
Brian Silverman2845c4c2013-03-16 19:54:08 -0700651 if (control_loop_driving) {
652 dt_closedloop.SendMotors(output);
653 } else {
654 dt_openloop.SendMotors(output);
brians343bc112013-02-10 01:53:46 +0000655 }
656}
657
658} // namespace control_loops
659} // namespace frc971