blob: 3e24dc22565bc4d26001e7113e02ba062ce0dd15 [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"
James Kuszmaulf254c1a2013-03-10 16:31:26 -070016#include "frc971/control_loops/drivetrain/drivetrain.q.h"
brians343bc112013-02-10 01:53:46 +000017#include "frc971/queues/GyroAngle.q.h"
18#include "frc971/queues/Piston.q.h"
19
20using frc971::sensors::gyro;
21
22namespace frc971 {
23namespace control_loops {
24
25// Width of the robot.
26const double width = 22.0 / 100.0 * 2.54;
27
Austin Schuh2054f5f2013-10-27 14:54:10 -070028Eigen::Matrix<double, 2, 1> CoerceGoal(aos::controls::HPolytope<2> &region,
29 const Eigen::Matrix<double, 1, 2> &K,
30 double w,
31 const Eigen::Matrix<double, 2, 1> &R) {
32 if (region.IsInside(R)) {
33 return R;
34 }
35 Eigen::Matrix<double, 2, 1> parallel_vector;
36 Eigen::Matrix<double, 2, 1> perpendicular_vector;
37 perpendicular_vector = K.transpose().normalized();
38 parallel_vector << perpendicular_vector(1, 0), -perpendicular_vector(0, 0);
39
40 aos::controls::HPolytope<1> t_poly(
41 region.H() * parallel_vector,
42 region.k() - region.H() * perpendicular_vector * w);
43
44 Eigen::Matrix<double, 1, Eigen::Dynamic> vertices = t_poly.Vertices();
45 if (vertices.innerSize() > 0) {
46 double min_distance_sqr = 0;
47 Eigen::Matrix<double, 2, 1> closest_point;
48 for (int i = 0; i < vertices.innerSize(); i++) {
49 Eigen::Matrix<double, 2, 1> point;
50 point = parallel_vector * vertices(0, i) + perpendicular_vector * w;
51 const double length = (R - point).squaredNorm();
52 if (i == 0 || length < min_distance_sqr) {
53 closest_point = point;
54 min_distance_sqr = length;
55 }
56 }
57 return closest_point;
58 } else {
59 Eigen::Matrix<double, 2, Eigen::Dynamic> region_vertices =
60 region.Vertices();
61 double min_distance;
62 int closest_i = 0;
63 for (int i = 0; i < region_vertices.outerSize(); i++) {
64 const double length = ::std::abs(
65 (perpendicular_vector.transpose() * (region_vertices.col(i)))(0, 0));
66 if (i == 0 || length < min_distance) {
67 closest_i = i;
68 min_distance = length;
69 }
70 }
71 return region_vertices.col(closest_i);
72 }
73}
74
Austin Schuh4352ac62013-03-19 06:23:16 +000075class DrivetrainMotorsSS {
brians343bc112013-02-10 01:53:46 +000076 public:
Austin Schuh4352ac62013-03-19 06:23:16 +000077 DrivetrainMotorsSS ()
78 : loop_(new StateFeedbackLoop<4, 2, 2>(MakeDrivetrainLoop())) {
brians343bc112013-02-10 01:53:46 +000079 _offset = 0;
80 _integral_offset = 0;
81 _left_goal = 0.0;
82 _right_goal = 0.0;
83 _raw_left = 0.0;
84 _raw_right = 0.0;
Austin Schuh4352ac62013-03-19 06:23:16 +000085 _control_loop_driving = false;
brians343bc112013-02-10 01:53:46 +000086 }
87 void SetGoal(double left, double left_velocity, double right, double right_velocity) {
88 _left_goal = left;
89 _right_goal = right;
Austin Schuh4352ac62013-03-19 06:23:16 +000090 loop_->R << left, left_velocity, right, right_velocity;
brians343bc112013-02-10 01:53:46 +000091 }
92 void SetRawPosition(double left, double right) {
93 _raw_right = right;
94 _raw_left = left;
Austin Schuh4352ac62013-03-19 06:23:16 +000095 loop_->Y << left, right;
brians343bc112013-02-10 01:53:46 +000096 }
Austin Schuh4352ac62013-03-19 06:23:16 +000097 void SetPosition(
98 double left, double right, double gyro, bool control_loop_driving) {
brians343bc112013-02-10 01:53:46 +000099 // Decay the offset quickly because this gyro is great.
100 _offset = (0.25) * (right - left - gyro * width) / 2.0 + 0.75 * _offset;
101 const double angle_error = (_right_goal - _left_goal) / width - (_raw_right - _offset - _raw_left - _offset) / width;
Austin Schuh4352ac62013-03-19 06:23:16 +0000102 // TODO(aschuh): Add in the gyro.
103 _integral_offset = 0.0;
104 _offset = 0.0;
brians343bc112013-02-10 01:53:46 +0000105 _gyro = gyro;
Austin Schuh4352ac62013-03-19 06:23:16 +0000106 _control_loop_driving = control_loop_driving;
brians343bc112013-02-10 01:53:46 +0000107 SetRawPosition(left, right);
108 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);
109 }
Austin Schuh4352ac62013-03-19 06:23:16 +0000110
111 void Update(bool update_observer, bool stop_motors) {
112 loop_->Update(update_observer, stop_motors);
brians343bc112013-02-10 01:53:46 +0000113 }
114
Austin Schuh4352ac62013-03-19 06:23:16 +0000115 void SendMotors(Drivetrain::Output *output) {
116 if (output) {
117 output->left_voltage = loop_->U(0, 0);
118 output->right_voltage = loop_->U(1, 0);
brians8ad74052013-03-16 21:04:51 +0000119 }
brians343bc112013-02-10 01:53:46 +0000120 }
121 void PrintMotors() const {
122 // 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 +0000123 ::Eigen::Matrix<double, 4, 1> E = loop_->R - loop_->X_hat;
124 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 +0000125 }
126
127 private:
Austin Schuh4352ac62013-03-19 06:23:16 +0000128 ::std::unique_ptr<StateFeedbackLoop<4, 2, 2>> loop_;
129
brians343bc112013-02-10 01:53:46 +0000130 double _integral_offset;
131 double _offset;
132 double _gyro;
133 double _left_goal;
134 double _right_goal;
135 double _raw_left;
136 double _raw_right;
Austin Schuh4352ac62013-03-19 06:23:16 +0000137 bool _control_loop_driving;
brians343bc112013-02-10 01:53:46 +0000138};
139
Austin Schuh2054f5f2013-10-27 14:54:10 -0700140class PolyDrivetrain {
141 public:
142 PolyDrivetrain()
143 : U_Poly_((Eigen::Matrix<double, 4, 2>() << /*[[*/ 1, 0 /*]*/,
144 /*[*/ -1, 0 /*]*/,
145 /*[*/ 0, 1 /*]*/,
146 /*[*/ 0, -1 /*]]*/).finished(),
147 (Eigen::Matrix<double, 4, 1>() << /*[[*/ 12 /*]*/,
148 /*[*/ 12 /*]*/,
149 /*[*/ 12 /*]*/,
150 /*[*/ 12 /*]]*/).finished()),
151 loop_(new StateFeedbackLoop<2, 2, 2>(MakeVDrivetrainLoop())) {
152
Brian Silverman718b1d72013-10-28 16:22:45 -0700153 ttrust_ = 1.1;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700154
155 wheel_ = 0.0;
156 throttle_ = 0.0;
157 quickturn_ = false;
158 highgear_ = true;
159 }
160 void SetGoal(double wheel, double throttle, bool quickturn, bool highgear) {
Brian Silverman718b1d72013-10-28 16:22:45 -0700161 const double kWheelNonLinearity = 0.4;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700162 // Apply a sin function that's scaled to make it feel better.
163 const double angular_range = M_PI_2 * kWheelNonLinearity;
164 wheel_ = sin(angular_range * wheel) / sin(angular_range);
165 wheel_ = sin(angular_range * wheel_) / sin(angular_range);
166 throttle_ = throttle;
167 quickturn_ = quickturn;
168 highgear_ = highgear;
169 if (highgear_) {
170 loop_->set_controller_index(3);
171 } else {
172 loop_->set_controller_index(0);
173 }
174 }
175 double FilterVelocity(double throttle) {
176 const Eigen::Matrix<double, 2, 2> FF =
177 loop_->B().inverse() *
178 (Eigen::Matrix<double, 2, 2>::Identity() - loop_->A());
179
180 constexpr int kHighGearController = 3;
181 const Eigen::Matrix<double, 2, 2> FF_high =
182 loop_->controller(kHighGearController).plant.B.inverse() *
183 (Eigen::Matrix<double, 2, 2>::Identity() -
184 loop_->controller(kHighGearController).plant.A);
185
186 ::Eigen::Matrix<double, 1, 2> FF_sum = FF.colwise().sum();
187 int min_FF_sum_index;
188 const double min_FF_sum = FF_sum.minCoeff(&min_FF_sum_index);
189 const double min_K_sum = loop_->K().col(min_FF_sum_index).sum();
190 const double high_min_FF_sum = FF_high.col(0).sum();
191
192 const double adjusted_ff_voltage = ::aos::Clip(
193 throttle * 12.0 * min_FF_sum / high_min_FF_sum, -12.0, 12.0);
194 return ((adjusted_ff_voltage +
195 ttrust_ * min_K_sum * (loop_->X_hat(0, 0) + loop_->X_hat(1, 0)) /
196 2.0) /
197 (ttrust_ * min_K_sum + min_FF_sum));
198 }
199
Brian Silverman718b1d72013-10-28 16:22:45 -0700200 double MaxVelocity() {
201 const Eigen::Matrix<double, 2, 2> FF =
202 loop_->B().inverse() *
203 (Eigen::Matrix<double, 2, 2>::Identity() - loop_->A());
204
205 constexpr int kHighGearController = 3;
206 const Eigen::Matrix<double, 2, 2> FF_high =
207 loop_->controller(kHighGearController).plant.B.inverse() *
208 (Eigen::Matrix<double, 2, 2>::Identity() -
209 loop_->controller(kHighGearController).plant.A);
210
211 ::Eigen::Matrix<double, 1, 2> FF_sum = FF.colwise().sum();
212 int min_FF_sum_index;
213 const double min_FF_sum = FF_sum.minCoeff(&min_FF_sum_index);
214 //const double min_K_sum = loop_->K().col(min_FF_sum_index).sum();
215 const double high_min_FF_sum = FF_high.col(0).sum();
216
217 const double adjusted_ff_voltage = ::aos::Clip(
218 12.0 * min_FF_sum / high_min_FF_sum, -12.0, 12.0);
219 return adjusted_ff_voltage / min_FF_sum;
220 }
221
Austin Schuh2054f5f2013-10-27 14:54:10 -0700222 void Update() {
223 // FF * X = U (steady state)
224 const Eigen::Matrix<double, 2, 2> FF =
225 loop_->B().inverse() *
226 (Eigen::Matrix<double, 2, 2>::Identity() - loop_->A());
227
228 // Invert the plant to figure out how the velocity filter would have to work
229 // out in order to filter out the forwards negative inertia.
230 // This math assumes that the left and right power and velocity are equals,
231 // and that the plant is the same on the left and right.
232 const double fvel = FilterVelocity(throttle_);
233
234 const double sign_svel = wheel_ * ((fvel > 0.0) ? 1.0 : -1.0);
Brian Silverman718b1d72013-10-28 16:22:45 -0700235 double steering_velocity;
236 if (quickturn_) {
237 steering_velocity = wheel_ * MaxVelocity();
238 } else {
239 steering_velocity = ::std::abs(fvel) * wheel_;
240 }
241 const double left_velocity = fvel - steering_velocity;
242 const double right_velocity = fvel + steering_velocity;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700243
244 // Integrate velocity to get the position.
245 // This position is used to get integral control.
246 loop_->R << left_velocity, right_velocity;
247
Brian Silverman718b1d72013-10-28 16:22:45 -0700248 if (!quickturn_) {
249 // K * R = w
250 Eigen::Matrix<double,1,2> equality_k;
251 equality_k << 1 + sign_svel, -(1 - sign_svel);
252 const double equality_w = 0.0;
253
Austin Schuh2054f5f2013-10-27 14:54:10 -0700254 // Construct a constraint on R by manipulating the constraint on U
255 ::aos::controls::HPolytope<2> R_poly = ::aos::controls::HPolytope<2>(
256 U_Poly_.H() * (loop_->K() + FF),
257 U_Poly_.k() + U_Poly_.H() * loop_->K() * loop_->X_hat);
258
259 // Limit R back inside the box.
Brian Silverman718b1d72013-10-28 16:22:45 -0700260 loop_->R =
Austin Schuh2054f5f2013-10-27 14:54:10 -0700261 CoerceGoal(R_poly, equality_k, equality_w, loop_->R);
Brian Silverman718b1d72013-10-28 16:22:45 -0700262 }
Austin Schuh2054f5f2013-10-27 14:54:10 -0700263
Brian Silverman718b1d72013-10-28 16:22:45 -0700264 const Eigen::Matrix<double, 2, 1> FF_volts = FF * loop_->R;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700265 const Eigen::Matrix<double, 2, 1> U_ideal =
Brian Silverman718b1d72013-10-28 16:22:45 -0700266 loop_->K() * (loop_->R - loop_->X_hat) + FF_volts;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700267
268 for (int i = 0; i < 2; i++) {
269 loop_->U[i] = ::aos::Clip(U_ideal[i], -12, 12);
270 }
271
272 // TODO(austin): Feed back?
273 loop_->X_hat = loop_->A() * loop_->X_hat + loop_->B() * loop_->U;
274 }
275
276 void SendMotors(Drivetrain::Output *output) {
277 LOG(DEBUG, "left pwm: %f right pwm: %f wheel: %f throttle: %f\n",
278 loop_->U(0, 0), loop_->U(1, 0), wheel_, throttle_);
279 output->left_voltage = loop_->U(0, 0);
280 output->right_voltage = loop_->U(1, 0);
281 if (highgear_) {
282 shifters.MakeWithBuilder().set(false).Send();
283 } else {
284 shifters.MakeWithBuilder().set(true).Send();
285 }
286 }
287
288 private:
289 const ::aos::controls::HPolytope<2> U_Poly_;
290
291 ::std::unique_ptr<StateFeedbackLoop<2, 2, 2>> loop_;
292
293 double ttrust_;
294 double wheel_;
295 double throttle_;
296 bool quickturn_;
297 bool highgear_;
298};
299
300
brians343bc112013-02-10 01:53:46 +0000301class DrivetrainMotorsOL {
302 public:
303 DrivetrainMotorsOL() {
304 _old_wheel = 0.0;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700305 wheel_ = 0.0;
306 throttle_ = 0.0;
307 quickturn_ = false;
308 highgear_ = true;
brians343bc112013-02-10 01:53:46 +0000309 _neg_inertia_accumulator = 0.0;
310 _left_pwm = 0.0;
311 _right_pwm = 0.0;
312 }
313 void SetGoal(double wheel, double throttle, bool quickturn, bool highgear) {
Austin Schuh2054f5f2013-10-27 14:54:10 -0700314 wheel_ = wheel;
315 throttle_ = throttle;
316 quickturn_ = quickturn;
317 highgear_ = highgear;
brians343bc112013-02-10 01:53:46 +0000318 _left_pwm = 0.0;
319 _right_pwm = 0.0;
320 }
321 void Update(void) {
322 double overPower;
323 float sensitivity = 1.7;
324 float angular_power;
325 float linear_power;
326 double wheel;
327
Austin Schuh2054f5f2013-10-27 14:54:10 -0700328 double neg_inertia = wheel_ - _old_wheel;
329 _old_wheel = wheel_;
brians343bc112013-02-10 01:53:46 +0000330
331 double wheelNonLinearity;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700332 if (highgear_) {
Brian Silverman77a76002013-03-16 20:09:00 -0700333 wheelNonLinearity = 0.1; // used to be csvReader->TURN_NONLIN_HIGH
brians343bc112013-02-10 01:53:46 +0000334 // Apply a sin function that's scaled to make it feel better.
335 const double angular_range = M_PI / 2.0 * wheelNonLinearity;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700336 wheel = sin(angular_range * wheel_) / sin(angular_range);
Brian Silverman77a76002013-03-16 20:09:00 -0700337 wheel = sin(angular_range * wheel) / sin(angular_range);
brians343bc112013-02-10 01:53:46 +0000338 } else {
Brian Silvermandf43ec22013-03-16 23:48:29 -0700339 wheelNonLinearity = 0.2; // used to be csvReader->TURN_NONLIN_LOW
brians343bc112013-02-10 01:53:46 +0000340 // Apply a sin function that's scaled to make it feel better.
341 const double angular_range = M_PI / 2.0 * wheelNonLinearity;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700342 wheel = sin(angular_range * wheel_) / sin(angular_range);
Brian Silverman77a76002013-03-16 20:09:00 -0700343 wheel = sin(angular_range * wheel) / sin(angular_range);
344 wheel = sin(angular_range * wheel) / sin(angular_range);
brians343bc112013-02-10 01:53:46 +0000345 }
346
Brian Silvermandf43ec22013-03-16 23:48:29 -0700347 static const double kThrottleDeadband = 0.05;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700348 if (::std::abs(throttle_) < kThrottleDeadband) {
349 throttle_ = 0;
Brian Silvermandf43ec22013-03-16 23:48:29 -0700350 } else {
Austin Schuh2054f5f2013-10-27 14:54:10 -0700351 throttle_ = copysign((::std::abs(throttle_) - kThrottleDeadband) /
352 (1.0 - kThrottleDeadband), throttle_);
Brian Silvermandf43ec22013-03-16 23:48:29 -0700353 }
354
brians343bc112013-02-10 01:53:46 +0000355 double neg_inertia_scalar;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700356 if (highgear_) {
Brian Silvermandf43ec22013-03-16 23:48:29 -0700357 neg_inertia_scalar = 8.0; // used to be csvReader->NEG_INTERTIA_HIGH
brians343bc112013-02-10 01:53:46 +0000358 sensitivity = 1.22; // used to be csvReader->SENSE_HIGH
359 } else {
360 if (wheel * neg_inertia > 0) {
Brian Silvermandf43ec22013-03-16 23:48:29 -0700361 neg_inertia_scalar = 5; // used to be csvReader->NEG_INERTIA_LOW_MORE
brians343bc112013-02-10 01:53:46 +0000362 } else {
Brian Silvermandf43ec22013-03-16 23:48:29 -0700363 if (::std::abs(wheel) > 0.65) {
364 neg_inertia_scalar = 5; // used to be csvReader->NEG_INTERTIA_LOW_LESS_EXT
brians343bc112013-02-10 01:53:46 +0000365 } else {
Brian Silvermandf43ec22013-03-16 23:48:29 -0700366 neg_inertia_scalar = 5; // used to be csvReader->NEG_INTERTIA_LOW_LESS
brians343bc112013-02-10 01:53:46 +0000367 }
368 }
369 sensitivity = 1.24; // used to be csvReader->SENSE_LOW
brians343bc112013-02-10 01:53:46 +0000370 }
371 double neg_inertia_power = neg_inertia * neg_inertia_scalar;
372 _neg_inertia_accumulator += neg_inertia_power;
373
374 wheel = wheel + _neg_inertia_accumulator;
375 if (_neg_inertia_accumulator > 1) {
376 _neg_inertia_accumulator -= 1;
377 } else if (_neg_inertia_accumulator < -1) {
378 _neg_inertia_accumulator += 1;
379 } else {
380 _neg_inertia_accumulator = 0;
381 }
382
Austin Schuh2054f5f2013-10-27 14:54:10 -0700383 linear_power = throttle_;
brians343bc112013-02-10 01:53:46 +0000384
Austin Schuh2054f5f2013-10-27 14:54:10 -0700385 if (quickturn_) {
brians343bc112013-02-10 01:53:46 +0000386 double qt_angular_power = wheel;
Brian Silvermandf43ec22013-03-16 23:48:29 -0700387 if (::std::abs(linear_power) < 0.2) {
brians343bc112013-02-10 01:53:46 +0000388 if (qt_angular_power > 1) qt_angular_power = 1.0;
389 if (qt_angular_power < -1) qt_angular_power = -1.0;
390 } else {
391 qt_angular_power = 0.0;
392 }
brians343bc112013-02-10 01:53:46 +0000393 overPower = 1.0;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700394 if (highgear_) {
brians343bc112013-02-10 01:53:46 +0000395 sensitivity = 1.0;
396 } else {
397 sensitivity = 1.0;
398 }
399 angular_power = wheel;
400 } else {
401 overPower = 0.0;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700402 angular_power = ::std::abs(throttle_) * wheel * sensitivity;
brians343bc112013-02-10 01:53:46 +0000403 }
404
405 _right_pwm = _left_pwm = linear_power;
406 _left_pwm += angular_power;
407 _right_pwm -= angular_power;
408
409 if (_left_pwm > 1.0) {
410 _right_pwm -= overPower*(_left_pwm - 1.0);
411 _left_pwm = 1.0;
412 } else if (_right_pwm > 1.0) {
413 _left_pwm -= overPower*(_right_pwm - 1.0);
414 _right_pwm = 1.0;
415 } else if (_left_pwm < -1.0) {
416 _right_pwm += overPower*(-1.0 - _left_pwm);
417 _left_pwm = -1.0;
418 } else if (_right_pwm < -1.0) {
419 _left_pwm += overPower*(-1.0 - _right_pwm);
420 _right_pwm = -1.0;
421 }
422 }
423
424 void SendMotors(Drivetrain::Output *output) {
Brian Silvermandf43ec22013-03-16 23:48:29 -0700425 LOG(DEBUG, "left pwm: %f right pwm: %f wheel: %f throttle: %f\n",
Austin Schuh2054f5f2013-10-27 14:54:10 -0700426 _left_pwm, _right_pwm, wheel_, throttle_);
brians8ad74052013-03-16 21:04:51 +0000427 if (output) {
428 output->left_voltage = _left_pwm * 12.0;
429 output->right_voltage = _right_pwm * 12.0;
430 }
Austin Schuh2054f5f2013-10-27 14:54:10 -0700431 if (highgear_) {
brians343bc112013-02-10 01:53:46 +0000432 shifters.MakeWithBuilder().set(false).Send();
433 } else {
434 shifters.MakeWithBuilder().set(true).Send();
435 }
436 }
437
438 private:
439 double _old_wheel;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700440 double wheel_;
441 double throttle_;
442 bool quickturn_;
443 bool highgear_;
brians343bc112013-02-10 01:53:46 +0000444 double _neg_inertia_accumulator;
445 double _left_pwm;
446 double _right_pwm;
brians343bc112013-02-10 01:53:46 +0000447};
448
449void DrivetrainLoop::RunIteration(const Drivetrain::Goal *goal,
450 const Drivetrain::Position *position,
451 Drivetrain::Output *output,
452 Drivetrain::Status * /*status*/) {
453 // TODO(aschuh): These should be members of the class.
454 static DrivetrainMotorsSS dt_closedloop;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700455 static PolyDrivetrain dt_openloop;
brians343bc112013-02-10 01:53:46 +0000456
457 bool bad_pos = false;
458 if (position == NULL) {
James Kuszmaul3f354742013-03-10 17:27:56 -0700459 LOG(WARNING, "no position\n");
brians343bc112013-02-10 01:53:46 +0000460 bad_pos = true;
461 }
462
463 double wheel = goal->steering;
464 double throttle = goal->throttle;
465 bool quickturn = goal->quickturn;
466 bool highgear = goal->highgear;
467
468 bool control_loop_driving = goal->control_loop_driving;
469 double left_goal = goal->left_goal;
470 double right_goal = goal->right_goal;
471
Austin Schuh2054f5f2013-10-27 14:54:10 -0700472 dt_closedloop.SetGoal(left_goal, goal->left_velocity_goal, right_goal,
473 goal->right_velocity_goal);
brians343bc112013-02-10 01:53:46 +0000474 if (!bad_pos) {
475 const double left_encoder = position->left_encoder;
476 const double right_encoder = position->right_encoder;
477 if (gyro.FetchLatest()) {
Austin Schuh2054f5f2013-10-27 14:54:10 -0700478 dt_closedloop.SetPosition(left_encoder, right_encoder, gyro->angle,
479 control_loop_driving);
brians343bc112013-02-10 01:53:46 +0000480 } else {
481 dt_closedloop.SetRawPosition(left_encoder, right_encoder);
482 }
483 }
Austin Schuh4352ac62013-03-19 06:23:16 +0000484 dt_closedloop.Update(position, output == NULL);
brians343bc112013-02-10 01:53:46 +0000485 dt_openloop.SetGoal(wheel, throttle, quickturn, highgear);
486 dt_openloop.Update();
Brian Silverman2845c4c2013-03-16 19:54:08 -0700487 if (control_loop_driving) {
488 dt_closedloop.SendMotors(output);
489 } else {
490 dt_openloop.SendMotors(output);
brians343bc112013-02-10 01:53:46 +0000491 }
492}
493
494} // namespace control_loops
495} // namespace frc971