blob: bcae82d97d8de26dcaca5dd5fd290891d2231c87 [file] [log] [blame]
brians343bc112013-02-10 01:53:46 +00001#include "frc971/control_loops/DriveTrain.h"
2
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"
10#include "frc971/control_loops/DriveTrain.mat"
11#include "frc971/control_loops/DriveTrain.q.h"
12#include "frc971/queues/GyroAngle.q.h"
13#include "frc971/queues/Piston.q.h"
14
15using frc971::sensors::gyro;
16
17namespace frc971 {
18namespace control_loops {
19
20// Width of the robot.
21const double width = 22.0 / 100.0 * 2.54;
22
23class DrivetrainMotorsSS : public MatrixClass {
24 public:
25 DrivetrainMotorsSS (void) {
26 MATRIX_INIT;
27 _offset = 0;
28 _integral_offset = 0;
29 _left_goal = 0.0;
30 _right_goal = 0.0;
31 _raw_left = 0.0;
32 _raw_right = 0.0;
33 }
34 void SetGoal(double left, double left_velocity, double right, double right_velocity) {
35 _left_goal = left;
36 _right_goal = right;
37 R << left + _integral_offset * width / 2.0, left_velocity, right - _integral_offset * width / 2.0, right_velocity;
38 }
39 void SetRawPosition(double left, double right) {
40 _raw_right = right;
41 _raw_left = left;
42 Y << left + _offset + _integral_offset, right - _offset + _integral_offset;
43 }
44 void SetPosition(double left, double right, double gyro, bool control_loop_driving) {
45 // Decay the offset quickly because this gyro is great.
46 _offset = (0.25) * (right - left - gyro * width) / 2.0 + 0.75 * _offset;
47 const double angle_error = (_right_goal - _left_goal) / width - (_raw_right - _offset - _raw_left - _offset) / width;
48 if (!control_loop_driving) {
49 _integral_offset = 0.0;
50 } else if (std::abs(angle_error) < M_PI / 10.0) {
51 _integral_offset -= angle_error * 0.010;
52 } else {
53 _integral_offset *= 0.97;
54 }
55 _gyro = gyro;
56 SetRawPosition(left, right);
57 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);
58 }
59 double UnDeadband(double value) {
60 const double positive_deadband_power = 0.15 * 12;
61 const double negative_deadband_power = 0.09 * 12;
62 if (value > 0) {
63 value += positive_deadband_power;
64 }
65 if (value < 0) {
66 value -= negative_deadband_power;
67 }
68 if (value > 12.0) {
69 value = 12.0;
70 }
71 if (value < -12.0) {
72 value = -12.0;
73 }
74 return value;
75 }
76
77 void SendMotors(Drivetrain::Output *status) {
brians8ad74052013-03-16 21:04:51 +000078 if (status) {
79 status->left_voltage = UnDeadband(U[0]);
80 status->right_voltage = UnDeadband(U[1]);
81 }
brians343bc112013-02-10 01:53:46 +000082 }
83 void PrintMotors() const {
84 // 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);
85 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);
86 }
87
88 private:
89 double _integral_offset;
90 double _offset;
91 double _gyro;
92 double _left_goal;
93 double _right_goal;
94 double _raw_left;
95 double _raw_right;
96};
97
98class DrivetrainMotorsOL {
99 public:
100 DrivetrainMotorsOL() {
101 _old_wheel = 0.0;
102 quick_stop_accumulator = 0.0;
103 _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) {
131 wheelNonLinearity = 0.7; // used to be csvReader->TURN_NONLIN_HIGH
132 // 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);
135 wheel = sin(angular_range * _wheel) / sin(angular_range);
136 } else {
137 wheelNonLinearity = 0.4; // used to be csvReader->TURN_NONLIN_LOW
138 // 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);
141 wheel = sin(angular_range * _wheel) / sin(angular_range);
142 wheel = sin(angular_range * _wheel) / sin(angular_range);
143 }
144
145 double neg_inertia_scalar;
146 if (_highgear) {
147 neg_inertia_scalar = 20.0; // used to be csvReader->NEG_INTERTIA_HIGH
148 sensitivity = 1.22; // used to be csvReader->SENSE_HIGH
149 } else {
150 if (wheel * neg_inertia > 0) {
151 neg_inertia_scalar = 16; // used to be csvReader->NEG_INERTIA_LOW_MORE
152 } else {
153 if (fabs(wheel) > 0.65) {
154 neg_inertia_scalar = 16; // used to be csvReader->NEG_INTERTIA_LOW_LESS_EXT
155 } else {
156 neg_inertia_scalar = 5; // used to be csvReader->NEG_INTERTIA_LOW_LESS
157 }
158 }
159 sensitivity = 1.24; // used to be csvReader->SENSE_LOW
160
161 if (fabs(_throttle) > 0.1) { // used to be csvReader->SENSE_CUTTOFF
162 sensitivity = 1 - (1 - sensitivity) / fabs(_throttle);
163 }
164 }
165 double neg_inertia_power = neg_inertia * neg_inertia_scalar;
166 _neg_inertia_accumulator += neg_inertia_power;
167
168 wheel = wheel + _neg_inertia_accumulator;
169 if (_neg_inertia_accumulator > 1) {
170 _neg_inertia_accumulator -= 1;
171 } else if (_neg_inertia_accumulator < -1) {
172 _neg_inertia_accumulator += 1;
173 } else {
174 _neg_inertia_accumulator = 0;
175 }
176
177 linear_power = _throttle;
178
179 const double quickstop_scalar = 6;
180 if (_quickturn) {
181 double qt_angular_power = wheel;
182 const double alpha = 0.1;
183 if (fabs(linear_power) < 0.2) {
184 if (qt_angular_power > 1) qt_angular_power = 1.0;
185 if (qt_angular_power < -1) qt_angular_power = -1.0;
186 } else {
187 qt_angular_power = 0.0;
188 }
189 quick_stop_accumulator = (1 - alpha) * quick_stop_accumulator + alpha * qt_angular_power * quickstop_scalar;
190 overPower = 1.0;
191 if (_highgear) {
192 sensitivity = 1.0;
193 } else {
194 sensitivity = 1.0;
195 }
196 angular_power = wheel;
197 } else {
198 overPower = 0.0;
199 angular_power = fabs(_throttle) * wheel * sensitivity;
200 angular_power -= quick_stop_accumulator;
201 if (quick_stop_accumulator > 1) {
202 quick_stop_accumulator -= 1;
203 } else if (quick_stop_accumulator < -1) {
204 quick_stop_accumulator += 1;
205 } else {
206 quick_stop_accumulator = 0;
207 }
208 }
209
210 _right_pwm = _left_pwm = linear_power;
211 _left_pwm += angular_power;
212 _right_pwm -= angular_power;
213
214 if (_left_pwm > 1.0) {
215 _right_pwm -= overPower*(_left_pwm - 1.0);
216 _left_pwm = 1.0;
217 } else if (_right_pwm > 1.0) {
218 _left_pwm -= overPower*(_right_pwm - 1.0);
219 _right_pwm = 1.0;
220 } else if (_left_pwm < -1.0) {
221 _right_pwm += overPower*(-1.0 - _left_pwm);
222 _left_pwm = -1.0;
223 } else if (_right_pwm < -1.0) {
224 _left_pwm += overPower*(-1.0 - _right_pwm);
225 _right_pwm = -1.0;
226 }
227 }
228
229 void SendMotors(Drivetrain::Output *output) {
230 LOG(DEBUG, "left pwm: %f right pwm: %f wheel: %f throttle: %f, qa %f\n",
231 _left_pwm, _right_pwm, _wheel, _throttle, quick_stop_accumulator);
brians8ad74052013-03-16 21:04:51 +0000232 if (output) {
233 output->left_voltage = _left_pwm * 12.0;
234 output->right_voltage = _right_pwm * 12.0;
235 }
brians343bc112013-02-10 01:53:46 +0000236 if (_highgear) {
237 shifters.MakeWithBuilder().set(false).Send();
238 } else {
239 shifters.MakeWithBuilder().set(true).Send();
240 }
241 }
242
243 private:
244 double _old_wheel;
245 double _wheel;
246 double _throttle;
247 bool _quickturn;
248 bool _highgear;
249 double _neg_inertia_accumulator;
250 double _left_pwm;
251 double _right_pwm;
252 double quick_stop_accumulator;
253};
254
255void DrivetrainLoop::RunIteration(const Drivetrain::Goal *goal,
256 const Drivetrain::Position *position,
257 Drivetrain::Output *output,
258 Drivetrain::Status * /*status*/) {
259 // TODO(aschuh): These should be members of the class.
260 static DrivetrainMotorsSS dt_closedloop;
261 static DrivetrainMotorsOL dt_openloop;
262
263 bool bad_pos = false;
264 if (position == NULL) {
265 LOG(WARNING, "no pos\n");
266 bad_pos = true;
267 }
268
269 double wheel = goal->steering;
270 double throttle = goal->throttle;
271 bool quickturn = goal->quickturn;
272 bool highgear = goal->highgear;
273
274 bool control_loop_driving = goal->control_loop_driving;
275 double left_goal = goal->left_goal;
276 double right_goal = goal->right_goal;
277
278 dt_closedloop.SetGoal(left_goal, 0.0, right_goal, 0.0);
279 if (!bad_pos) {
280 const double left_encoder = position->left_encoder;
281 const double right_encoder = position->right_encoder;
282 if (gyro.FetchLatest()) {
283 dt_closedloop.SetPosition(left_encoder, right_encoder,
284 gyro->angle, control_loop_driving);
285 } else {
286 dt_closedloop.SetRawPosition(left_encoder, right_encoder);
287 }
288 }
289 dt_closedloop.Update(!bad_pos, bad_pos || (output == NULL));
290 dt_openloop.SetGoal(wheel, throttle, quickturn, highgear);
291 dt_openloop.Update();
292 if (control_loop_driving) {
293 dt_closedloop.SendMotors(output);
294 } else {
295 dt_openloop.SendMotors(output);
296 }
297}
298
299} // namespace control_loops
300} // namespace frc971
301
302AOS_RUN_LOOP(frc971::control_loops::DrivetrainLoop)