blob: 63e671881c0a10d74412cfc8b811d8e648e3bf90 [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>
6
7#include "aos/aos_core.h"
8#include "aos/common/logging/logging.h"
9#include "aos/common/queue.h"
James Kuszmaulf254c1a2013-03-10 16:31:26 -070010#include "frc971/control_loops/state_feedback_loop.h"
11#include "frc971/control_loops/drivetrain/drivetrain_motor_plant.h"
12#include "frc971/control_loops/drivetrain/drivetrain.q.h"
brians343bc112013-02-10 01:53:46 +000013#include "frc971/queues/GyroAngle.q.h"
14#include "frc971/queues/Piston.q.h"
15
16using frc971::sensors::gyro;
17
18namespace frc971 {
19namespace control_loops {
20
21// Width of the robot.
22const double width = 22.0 / 100.0 * 2.54;
23
James Kuszmaulf254c1a2013-03-10 16:31:26 -070024class DrivetrainMotorsSS : public StateFeedbackLoop<4, 2, 2> {
brians343bc112013-02-10 01:53:46 +000025 public:
James Kuszmaulf254c1a2013-03-10 16:31:26 -070026 DrivetrainMotorsSS (void)
27 : StateFeedbackLoop(MakeDrivetrainLoop()) {
brians343bc112013-02-10 01:53:46 +000028 _offset = 0;
29 _integral_offset = 0;
30 _left_goal = 0.0;
31 _right_goal = 0.0;
32 _raw_left = 0.0;
33 _raw_right = 0.0;
34 }
35 void SetGoal(double left, double left_velocity, double right, double right_velocity) {
36 _left_goal = left;
37 _right_goal = right;
38 R << left + _integral_offset * width / 2.0, left_velocity, right - _integral_offset * width / 2.0, right_velocity;
39 }
40 void SetRawPosition(double left, double right) {
41 _raw_right = right;
42 _raw_left = left;
43 Y << left + _offset + _integral_offset, right - _offset + _integral_offset;
44 }
45 void SetPosition(double left, double right, double gyro, bool control_loop_driving) {
46 // Decay the offset quickly because this gyro is great.
47 _offset = (0.25) * (right - left - gyro * width) / 2.0 + 0.75 * _offset;
48 const double angle_error = (_right_goal - _left_goal) / width - (_raw_right - _offset - _raw_left - _offset) / width;
49 if (!control_loop_driving) {
50 _integral_offset = 0.0;
Brian Silvermandf43ec22013-03-16 23:48:29 -070051 } else if (::std::abs(angle_error) < M_PI / 10.0) {
brians343bc112013-02-10 01:53:46 +000052 _integral_offset -= angle_error * 0.010;
53 } else {
54 _integral_offset *= 0.97;
55 }
56 _gyro = gyro;
57 SetRawPosition(left, right);
58 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);
59 }
60 double UnDeadband(double value) {
61 const double positive_deadband_power = 0.15 * 12;
62 const double negative_deadband_power = 0.09 * 12;
63 if (value > 0) {
64 value += positive_deadband_power;
65 }
66 if (value < 0) {
67 value -= negative_deadband_power;
68 }
69 if (value > 12.0) {
70 value = 12.0;
71 }
72 if (value < -12.0) {
73 value = -12.0;
74 }
75 return value;
76 }
77
78 void SendMotors(Drivetrain::Output *status) {
brians8ad74052013-03-16 21:04:51 +000079 if (status) {
80 status->left_voltage = UnDeadband(U[0]);
81 status->right_voltage = UnDeadband(U[1]);
82 }
brians343bc112013-02-10 01:53:46 +000083 }
84 void PrintMotors() const {
85 // 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);
86 LOG(DEBUG, "lg %f rg %f le %f re %f gyro %f off %f\n", R[0], R[2], Y[0], Y[1], _gyro * 180.0 / M_PI, _offset);
87 }
88
89 private:
90 double _integral_offset;
91 double _offset;
92 double _gyro;
93 double _left_goal;
94 double _right_goal;
95 double _raw_left;
96 double _raw_right;
97};
98
99class DrivetrainMotorsOL {
100 public:
101 DrivetrainMotorsOL() {
102 _old_wheel = 0.0;
brians343bc112013-02-10 01:53:46 +0000103 _wheel = 0.0;
104 _throttle = 0.0;
105 _quickturn = false;
106 _highgear = true;
107 _neg_inertia_accumulator = 0.0;
108 _left_pwm = 0.0;
109 _right_pwm = 0.0;
110 }
111 void SetGoal(double wheel, double throttle, bool quickturn, bool highgear) {
112 _wheel = wheel;
113 _throttle = throttle;
114 _quickturn = quickturn;
115 _highgear = highgear;
116 _left_pwm = 0.0;
117 _right_pwm = 0.0;
118 }
119 void Update(void) {
120 double overPower;
121 float sensitivity = 1.7;
122 float angular_power;
123 float linear_power;
124 double wheel;
125
126 double neg_inertia = _wheel - _old_wheel;
127 _old_wheel = _wheel;
128
129 double wheelNonLinearity;
130 if (_highgear) {
Brian Silverman77a76002013-03-16 20:09:00 -0700131 wheelNonLinearity = 0.1; // used to be csvReader->TURN_NONLIN_HIGH
brians343bc112013-02-10 01:53:46 +0000132 // Apply a sin function that's scaled to make it feel better.
133 const double angular_range = M_PI / 2.0 * wheelNonLinearity;
134 wheel = sin(angular_range * _wheel) / sin(angular_range);
Brian Silverman77a76002013-03-16 20:09:00 -0700135 wheel = sin(angular_range * wheel) / sin(angular_range);
brians343bc112013-02-10 01:53:46 +0000136 } else {
Brian Silvermandf43ec22013-03-16 23:48:29 -0700137 wheelNonLinearity = 0.2; // used to be csvReader->TURN_NONLIN_LOW
brians343bc112013-02-10 01:53:46 +0000138 // Apply a sin function that's scaled to make it feel better.
139 const double angular_range = M_PI / 2.0 * wheelNonLinearity;
140 wheel = sin(angular_range * _wheel) / sin(angular_range);
Brian Silverman77a76002013-03-16 20:09:00 -0700141 wheel = sin(angular_range * wheel) / sin(angular_range);
142 wheel = sin(angular_range * wheel) / sin(angular_range);
brians343bc112013-02-10 01:53:46 +0000143 }
144
Brian Silvermandf43ec22013-03-16 23:48:29 -0700145 static const double kThrottleDeadband = 0.05;
146 if (::std::abs(_throttle) < kThrottleDeadband) {
147 _throttle = 0;
148 } else {
149 _throttle = copysign((::std::abs(_throttle) - kThrottleDeadband) /
150 (1.0 - kThrottleDeadband), _throttle);
151 }
152
brians343bc112013-02-10 01:53:46 +0000153 double neg_inertia_scalar;
154 if (_highgear) {
Brian Silvermandf43ec22013-03-16 23:48:29 -0700155 neg_inertia_scalar = 8.0; // used to be csvReader->NEG_INTERTIA_HIGH
brians343bc112013-02-10 01:53:46 +0000156 sensitivity = 1.22; // used to be csvReader->SENSE_HIGH
157 } else {
158 if (wheel * neg_inertia > 0) {
Brian Silvermandf43ec22013-03-16 23:48:29 -0700159 neg_inertia_scalar = 5; // used to be csvReader->NEG_INERTIA_LOW_MORE
brians343bc112013-02-10 01:53:46 +0000160 } else {
Brian Silvermandf43ec22013-03-16 23:48:29 -0700161 if (::std::abs(wheel) > 0.65) {
162 neg_inertia_scalar = 5; // used to be csvReader->NEG_INTERTIA_LOW_LESS_EXT
brians343bc112013-02-10 01:53:46 +0000163 } else {
Brian Silvermandf43ec22013-03-16 23:48:29 -0700164 neg_inertia_scalar = 5; // used to be csvReader->NEG_INTERTIA_LOW_LESS
brians343bc112013-02-10 01:53:46 +0000165 }
166 }
167 sensitivity = 1.24; // used to be csvReader->SENSE_LOW
brians343bc112013-02-10 01:53:46 +0000168 }
169 double neg_inertia_power = neg_inertia * neg_inertia_scalar;
170 _neg_inertia_accumulator += neg_inertia_power;
171
172 wheel = wheel + _neg_inertia_accumulator;
173 if (_neg_inertia_accumulator > 1) {
174 _neg_inertia_accumulator -= 1;
175 } else if (_neg_inertia_accumulator < -1) {
176 _neg_inertia_accumulator += 1;
177 } else {
178 _neg_inertia_accumulator = 0;
179 }
180
181 linear_power = _throttle;
182
brians343bc112013-02-10 01:53:46 +0000183 if (_quickturn) {
184 double qt_angular_power = wheel;
Brian Silvermandf43ec22013-03-16 23:48:29 -0700185 if (::std::abs(linear_power) < 0.2) {
brians343bc112013-02-10 01:53:46 +0000186 if (qt_angular_power > 1) qt_angular_power = 1.0;
187 if (qt_angular_power < -1) qt_angular_power = -1.0;
188 } else {
189 qt_angular_power = 0.0;
190 }
brians343bc112013-02-10 01:53:46 +0000191 overPower = 1.0;
192 if (_highgear) {
193 sensitivity = 1.0;
194 } else {
195 sensitivity = 1.0;
196 }
197 angular_power = wheel;
198 } else {
199 overPower = 0.0;
Brian Silvermandf43ec22013-03-16 23:48:29 -0700200 angular_power = ::std::abs(_throttle) * wheel * sensitivity;
brians343bc112013-02-10 01:53:46 +0000201 }
202
203 _right_pwm = _left_pwm = linear_power;
204 _left_pwm += angular_power;
205 _right_pwm -= angular_power;
206
207 if (_left_pwm > 1.0) {
208 _right_pwm -= overPower*(_left_pwm - 1.0);
209 _left_pwm = 1.0;
210 } else if (_right_pwm > 1.0) {
211 _left_pwm -= overPower*(_right_pwm - 1.0);
212 _right_pwm = 1.0;
213 } else if (_left_pwm < -1.0) {
214 _right_pwm += overPower*(-1.0 - _left_pwm);
215 _left_pwm = -1.0;
216 } else if (_right_pwm < -1.0) {
217 _left_pwm += overPower*(-1.0 - _right_pwm);
218 _right_pwm = -1.0;
219 }
220 }
221
222 void SendMotors(Drivetrain::Output *output) {
Brian Silvermandf43ec22013-03-16 23:48:29 -0700223 LOG(DEBUG, "left pwm: %f right pwm: %f wheel: %f throttle: %f\n",
224 _left_pwm, _right_pwm, _wheel, _throttle);
brians8ad74052013-03-16 21:04:51 +0000225 if (output) {
226 output->left_voltage = _left_pwm * 12.0;
227 output->right_voltage = _right_pwm * 12.0;
228 }
brians343bc112013-02-10 01:53:46 +0000229 if (_highgear) {
230 shifters.MakeWithBuilder().set(false).Send();
231 } else {
232 shifters.MakeWithBuilder().set(true).Send();
233 }
234 }
235
236 private:
237 double _old_wheel;
238 double _wheel;
239 double _throttle;
240 bool _quickturn;
241 bool _highgear;
242 double _neg_inertia_accumulator;
243 double _left_pwm;
244 double _right_pwm;
brians343bc112013-02-10 01:53:46 +0000245};
246
247void DrivetrainLoop::RunIteration(const Drivetrain::Goal *goal,
248 const Drivetrain::Position *position,
249 Drivetrain::Output *output,
250 Drivetrain::Status * /*status*/) {
251 // TODO(aschuh): These should be members of the class.
252 static DrivetrainMotorsSS dt_closedloop;
253 static DrivetrainMotorsOL dt_openloop;
254
255 bool bad_pos = false;
256 if (position == NULL) {
James Kuszmaul3f354742013-03-10 17:27:56 -0700257 LOG(WARNING, "no position\n");
brians343bc112013-02-10 01:53:46 +0000258 bad_pos = true;
259 }
260
James Kuszmaul3f354742013-03-10 17:27:56 -0700261 bool bad_output = false;
262 if (output == NULL) {
263 LOG(WARNING, "no output\n");
264 bad_output = true;
265 }
266
brians343bc112013-02-10 01:53:46 +0000267 double wheel = goal->steering;
268 double throttle = goal->throttle;
269 bool quickturn = goal->quickturn;
270 bool highgear = goal->highgear;
271
272 bool control_loop_driving = goal->control_loop_driving;
273 double left_goal = goal->left_goal;
274 double right_goal = goal->right_goal;
275
276 dt_closedloop.SetGoal(left_goal, 0.0, right_goal, 0.0);
277 if (!bad_pos) {
278 const double left_encoder = position->left_encoder;
279 const double right_encoder = position->right_encoder;
280 if (gyro.FetchLatest()) {
281 dt_closedloop.SetPosition(left_encoder, right_encoder,
282 gyro->angle, control_loop_driving);
283 } else {
284 dt_closedloop.SetRawPosition(left_encoder, right_encoder);
285 }
286 }
James Kuszmaul3f354742013-03-10 17:27:56 -0700287 dt_closedloop.Update(!bad_pos, bad_pos || bad_output);
brians343bc112013-02-10 01:53:46 +0000288 dt_openloop.SetGoal(wheel, throttle, quickturn, highgear);
289 dt_openloop.Update();
Brian Silverman2845c4c2013-03-16 19:54:08 -0700290 if (control_loop_driving) {
291 dt_closedloop.SendMotors(output);
292 } else {
293 dt_openloop.SendMotors(output);
brians343bc112013-02-10 01:53:46 +0000294 }
295}
296
297} // namespace control_loops
298} // namespace frc971