blob: b627cc0f44902fd0717b5a33d39049ac5e92afa2 [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) {
78 status->left_voltage = UnDeadband(U[0]);
79 status->right_voltage = UnDeadband(U[1]);
80 }
81 void PrintMotors() const {
82 // 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);
83 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);
84 }
85
86 private:
87 double _integral_offset;
88 double _offset;
89 double _gyro;
90 double _left_goal;
91 double _right_goal;
92 double _raw_left;
93 double _raw_right;
94};
95
96class DrivetrainMotorsOL {
97 public:
98 DrivetrainMotorsOL() {
99 _old_wheel = 0.0;
100 quick_stop_accumulator = 0.0;
101 _wheel = 0.0;
102 _throttle = 0.0;
103 _quickturn = false;
104 _highgear = true;
105 _neg_inertia_accumulator = 0.0;
106 _left_pwm = 0.0;
107 _right_pwm = 0.0;
108 }
109 void SetGoal(double wheel, double throttle, bool quickturn, bool highgear) {
110 _wheel = wheel;
111 _throttle = throttle;
112 _quickturn = quickturn;
113 _highgear = highgear;
114 _left_pwm = 0.0;
115 _right_pwm = 0.0;
116 }
117 void Update(void) {
118 double overPower;
119 float sensitivity = 1.7;
120 float angular_power;
121 float linear_power;
122 double wheel;
123
124 double neg_inertia = _wheel - _old_wheel;
125 _old_wheel = _wheel;
126
127 double wheelNonLinearity;
128 if (_highgear) {
129 wheelNonLinearity = 0.7; // used to be csvReader->TURN_NONLIN_HIGH
130 // Apply a sin function that's scaled to make it feel better.
131 const double angular_range = M_PI / 2.0 * wheelNonLinearity;
132 wheel = sin(angular_range * _wheel) / sin(angular_range);
133 wheel = sin(angular_range * _wheel) / sin(angular_range);
134 } else {
135 wheelNonLinearity = 0.4; // used to be csvReader->TURN_NONLIN_LOW
136 // Apply a sin function that's scaled to make it feel better.
137 const double angular_range = M_PI / 2.0 * wheelNonLinearity;
138 wheel = sin(angular_range * _wheel) / sin(angular_range);
139 wheel = sin(angular_range * _wheel) / sin(angular_range);
140 wheel = sin(angular_range * _wheel) / sin(angular_range);
141 }
142
143 double neg_inertia_scalar;
144 if (_highgear) {
145 neg_inertia_scalar = 20.0; // used to be csvReader->NEG_INTERTIA_HIGH
146 sensitivity = 1.22; // used to be csvReader->SENSE_HIGH
147 } else {
148 if (wheel * neg_inertia > 0) {
149 neg_inertia_scalar = 16; // used to be csvReader->NEG_INERTIA_LOW_MORE
150 } else {
151 if (fabs(wheel) > 0.65) {
152 neg_inertia_scalar = 16; // used to be csvReader->NEG_INTERTIA_LOW_LESS_EXT
153 } else {
154 neg_inertia_scalar = 5; // used to be csvReader->NEG_INTERTIA_LOW_LESS
155 }
156 }
157 sensitivity = 1.24; // used to be csvReader->SENSE_LOW
158
159 if (fabs(_throttle) > 0.1) { // used to be csvReader->SENSE_CUTTOFF
160 sensitivity = 1 - (1 - sensitivity) / fabs(_throttle);
161 }
162 }
163 double neg_inertia_power = neg_inertia * neg_inertia_scalar;
164 _neg_inertia_accumulator += neg_inertia_power;
165
166 wheel = wheel + _neg_inertia_accumulator;
167 if (_neg_inertia_accumulator > 1) {
168 _neg_inertia_accumulator -= 1;
169 } else if (_neg_inertia_accumulator < -1) {
170 _neg_inertia_accumulator += 1;
171 } else {
172 _neg_inertia_accumulator = 0;
173 }
174
175 linear_power = _throttle;
176
177 const double quickstop_scalar = 6;
178 if (_quickturn) {
179 double qt_angular_power = wheel;
180 const double alpha = 0.1;
181 if (fabs(linear_power) < 0.2) {
182 if (qt_angular_power > 1) qt_angular_power = 1.0;
183 if (qt_angular_power < -1) qt_angular_power = -1.0;
184 } else {
185 qt_angular_power = 0.0;
186 }
187 quick_stop_accumulator = (1 - alpha) * quick_stop_accumulator + alpha * qt_angular_power * quickstop_scalar;
188 overPower = 1.0;
189 if (_highgear) {
190 sensitivity = 1.0;
191 } else {
192 sensitivity = 1.0;
193 }
194 angular_power = wheel;
195 } else {
196 overPower = 0.0;
197 angular_power = fabs(_throttle) * wheel * sensitivity;
198 angular_power -= quick_stop_accumulator;
199 if (quick_stop_accumulator > 1) {
200 quick_stop_accumulator -= 1;
201 } else if (quick_stop_accumulator < -1) {
202 quick_stop_accumulator += 1;
203 } else {
204 quick_stop_accumulator = 0;
205 }
206 }
207
208 _right_pwm = _left_pwm = linear_power;
209 _left_pwm += angular_power;
210 _right_pwm -= angular_power;
211
212 if (_left_pwm > 1.0) {
213 _right_pwm -= overPower*(_left_pwm - 1.0);
214 _left_pwm = 1.0;
215 } else if (_right_pwm > 1.0) {
216 _left_pwm -= overPower*(_right_pwm - 1.0);
217 _right_pwm = 1.0;
218 } else if (_left_pwm < -1.0) {
219 _right_pwm += overPower*(-1.0 - _left_pwm);
220 _left_pwm = -1.0;
221 } else if (_right_pwm < -1.0) {
222 _left_pwm += overPower*(-1.0 - _right_pwm);
223 _right_pwm = -1.0;
224 }
225 }
226
227 void SendMotors(Drivetrain::Output *output) {
228 LOG(DEBUG, "left pwm: %f right pwm: %f wheel: %f throttle: %f, qa %f\n",
229 _left_pwm, _right_pwm, _wheel, _throttle, quick_stop_accumulator);
230 output->left_voltage = _left_pwm * 12.0;
231 output->right_voltage = _right_pwm * 12.0;
232 if (_highgear) {
233 shifters.MakeWithBuilder().set(false).Send();
234 } else {
235 shifters.MakeWithBuilder().set(true).Send();
236 }
237 }
238
239 private:
240 double _old_wheel;
241 double _wheel;
242 double _throttle;
243 bool _quickturn;
244 bool _highgear;
245 double _neg_inertia_accumulator;
246 double _left_pwm;
247 double _right_pwm;
248 double quick_stop_accumulator;
249};
250
251void DrivetrainLoop::RunIteration(const Drivetrain::Goal *goal,
252 const Drivetrain::Position *position,
253 Drivetrain::Output *output,
254 Drivetrain::Status * /*status*/) {
255 // TODO(aschuh): These should be members of the class.
256 static DrivetrainMotorsSS dt_closedloop;
257 static DrivetrainMotorsOL dt_openloop;
258
259 bool bad_pos = false;
260 if (position == NULL) {
261 LOG(WARNING, "no pos\n");
262 bad_pos = true;
263 }
264
265 double wheel = goal->steering;
266 double throttle = goal->throttle;
267 bool quickturn = goal->quickturn;
268 bool highgear = goal->highgear;
269
270 bool control_loop_driving = goal->control_loop_driving;
271 double left_goal = goal->left_goal;
272 double right_goal = goal->right_goal;
273
274 dt_closedloop.SetGoal(left_goal, 0.0, right_goal, 0.0);
275 if (!bad_pos) {
276 const double left_encoder = position->left_encoder;
277 const double right_encoder = position->right_encoder;
278 if (gyro.FetchLatest()) {
279 dt_closedloop.SetPosition(left_encoder, right_encoder,
280 gyro->angle, control_loop_driving);
281 } else {
282 dt_closedloop.SetRawPosition(left_encoder, right_encoder);
283 }
284 }
285 dt_closedloop.Update(!bad_pos, bad_pos || (output == NULL));
286 dt_openloop.SetGoal(wheel, throttle, quickturn, highgear);
287 dt_openloop.Update();
288 if (control_loop_driving) {
289 dt_closedloop.SendMotors(output);
290 } else {
291 dt_openloop.SendMotors(output);
292 }
293}
294
295} // namespace control_loops
296} // namespace frc971
297
298AOS_RUN_LOOP(frc971::control_loops::DrivetrainLoop)