blob: 74ed026fe39544e4879c1f9207af7cebbf41c769 [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;
51 } else if (std::abs(angle_error) < M_PI / 10.0) {
52 _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;
103 quick_stop_accumulator = 0.0;
104 _wheel = 0.0;
105 _throttle = 0.0;
106 _quickturn = false;
107 _highgear = true;
108 _neg_inertia_accumulator = 0.0;
109 _left_pwm = 0.0;
110 _right_pwm = 0.0;
111 }
112 void SetGoal(double wheel, double throttle, bool quickturn, bool highgear) {
113 _wheel = wheel;
114 _throttle = throttle;
115 _quickturn = quickturn;
116 _highgear = highgear;
117 _left_pwm = 0.0;
118 _right_pwm = 0.0;
119 }
120 void Update(void) {
121 double overPower;
122 float sensitivity = 1.7;
123 float angular_power;
124 float linear_power;
125 double wheel;
126
127 double neg_inertia = _wheel - _old_wheel;
128 _old_wheel = _wheel;
129
130 double wheelNonLinearity;
131 if (_highgear) {
Brian Silverman77a76002013-03-16 20:09:00 -0700132 wheelNonLinearity = 0.1; // used to be csvReader->TURN_NONLIN_HIGH
brians343bc112013-02-10 01:53:46 +0000133 // Apply a sin function that's scaled to make it feel better.
134 const double angular_range = M_PI / 2.0 * wheelNonLinearity;
135 wheel = sin(angular_range * _wheel) / sin(angular_range);
Brian Silverman77a76002013-03-16 20:09:00 -0700136 wheel = sin(angular_range * wheel) / sin(angular_range);
brians343bc112013-02-10 01:53:46 +0000137 } else {
Brian Silverman77a76002013-03-16 20:09:00 -0700138 wheelNonLinearity = 0.1; // used to be csvReader->TURN_NONLIN_LOW
brians343bc112013-02-10 01:53:46 +0000139 // Apply a sin function that's scaled to make it feel better.
140 const double angular_range = M_PI / 2.0 * wheelNonLinearity;
141 wheel = sin(angular_range * _wheel) / sin(angular_range);
Brian Silverman77a76002013-03-16 20:09:00 -0700142 wheel = sin(angular_range * wheel) / sin(angular_range);
143 wheel = sin(angular_range * wheel) / sin(angular_range);
brians343bc112013-02-10 01:53:46 +0000144 }
145
146 double neg_inertia_scalar;
147 if (_highgear) {
148 neg_inertia_scalar = 20.0; // used to be csvReader->NEG_INTERTIA_HIGH
149 sensitivity = 1.22; // used to be csvReader->SENSE_HIGH
150 } else {
151 if (wheel * neg_inertia > 0) {
152 neg_inertia_scalar = 16; // used to be csvReader->NEG_INERTIA_LOW_MORE
153 } else {
154 if (fabs(wheel) > 0.65) {
155 neg_inertia_scalar = 16; // used to be csvReader->NEG_INTERTIA_LOW_LESS_EXT
156 } else {
Brian Silverman77a76002013-03-16 20:09:00 -0700157 neg_inertia_scalar = 16; // used to be csvReader->NEG_INTERTIA_LOW_LESS
brians343bc112013-02-10 01:53:46 +0000158 }
159 }
160 sensitivity = 1.24; // used to be csvReader->SENSE_LOW
brians343bc112013-02-10 01:53:46 +0000161 }
162 double neg_inertia_power = neg_inertia * neg_inertia_scalar;
163 _neg_inertia_accumulator += neg_inertia_power;
164
165 wheel = wheel + _neg_inertia_accumulator;
166 if (_neg_inertia_accumulator > 1) {
167 _neg_inertia_accumulator -= 1;
168 } else if (_neg_inertia_accumulator < -1) {
169 _neg_inertia_accumulator += 1;
170 } else {
171 _neg_inertia_accumulator = 0;
172 }
173
174 linear_power = _throttle;
175
176 const double quickstop_scalar = 6;
177 if (_quickturn) {
178 double qt_angular_power = wheel;
179 const double alpha = 0.1;
180 if (fabs(linear_power) < 0.2) {
181 if (qt_angular_power > 1) qt_angular_power = 1.0;
182 if (qt_angular_power < -1) qt_angular_power = -1.0;
183 } else {
184 qt_angular_power = 0.0;
185 }
186 quick_stop_accumulator = (1 - alpha) * quick_stop_accumulator + alpha * qt_angular_power * quickstop_scalar;
187 overPower = 1.0;
188 if (_highgear) {
189 sensitivity = 1.0;
190 } else {
191 sensitivity = 1.0;
192 }
193 angular_power = wheel;
194 } else {
195 overPower = 0.0;
196 angular_power = fabs(_throttle) * wheel * sensitivity;
197 angular_power -= quick_stop_accumulator;
Brian Silverman77a76002013-03-16 20:09:00 -0700198#if 0
brians343bc112013-02-10 01:53:46 +0000199 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 }
Brian Silverman77a76002013-03-16 20:09:00 -0700206#endif
brians343bc112013-02-10 01:53:46 +0000207 }
208
209 _right_pwm = _left_pwm = linear_power;
210 _left_pwm += angular_power;
211 _right_pwm -= angular_power;
212
213 if (_left_pwm > 1.0) {
214 _right_pwm -= overPower*(_left_pwm - 1.0);
215 _left_pwm = 1.0;
216 } else if (_right_pwm > 1.0) {
217 _left_pwm -= overPower*(_right_pwm - 1.0);
218 _right_pwm = 1.0;
219 } else if (_left_pwm < -1.0) {
220 _right_pwm += overPower*(-1.0 - _left_pwm);
221 _left_pwm = -1.0;
222 } else if (_right_pwm < -1.0) {
223 _left_pwm += overPower*(-1.0 - _right_pwm);
224 _right_pwm = -1.0;
225 }
226 }
227
228 void SendMotors(Drivetrain::Output *output) {
229 LOG(DEBUG, "left pwm: %f right pwm: %f wheel: %f throttle: %f, qa %f\n",
230 _left_pwm, _right_pwm, _wheel, _throttle, quick_stop_accumulator);
brians8ad74052013-03-16 21:04:51 +0000231 if (output) {
232 output->left_voltage = _left_pwm * 12.0;
233 output->right_voltage = _right_pwm * 12.0;
234 }
brians343bc112013-02-10 01:53:46 +0000235 if (_highgear) {
236 shifters.MakeWithBuilder().set(false).Send();
237 } else {
238 shifters.MakeWithBuilder().set(true).Send();
239 }
240 }
241
242 private:
243 double _old_wheel;
244 double _wheel;
245 double _throttle;
246 bool _quickturn;
247 bool _highgear;
248 double _neg_inertia_accumulator;
249 double _left_pwm;
250 double _right_pwm;
251 double quick_stop_accumulator;
252};
253
254void DrivetrainLoop::RunIteration(const Drivetrain::Goal *goal,
255 const Drivetrain::Position *position,
256 Drivetrain::Output *output,
257 Drivetrain::Status * /*status*/) {
258 // TODO(aschuh): These should be members of the class.
259 static DrivetrainMotorsSS dt_closedloop;
260 static DrivetrainMotorsOL dt_openloop;
261
262 bool bad_pos = false;
263 if (position == NULL) {
James Kuszmaul3f354742013-03-10 17:27:56 -0700264 LOG(WARNING, "no position\n");
brians343bc112013-02-10 01:53:46 +0000265 bad_pos = true;
266 }
267
James Kuszmaul3f354742013-03-10 17:27:56 -0700268 bool bad_output = false;
269 if (output == NULL) {
270 LOG(WARNING, "no output\n");
271 bad_output = true;
272 }
273
brians343bc112013-02-10 01:53:46 +0000274 double wheel = goal->steering;
275 double throttle = goal->throttle;
276 bool quickturn = goal->quickturn;
277 bool highgear = goal->highgear;
278
279 bool control_loop_driving = goal->control_loop_driving;
280 double left_goal = goal->left_goal;
281 double right_goal = goal->right_goal;
282
283 dt_closedloop.SetGoal(left_goal, 0.0, right_goal, 0.0);
284 if (!bad_pos) {
285 const double left_encoder = position->left_encoder;
286 const double right_encoder = position->right_encoder;
287 if (gyro.FetchLatest()) {
288 dt_closedloop.SetPosition(left_encoder, right_encoder,
289 gyro->angle, control_loop_driving);
290 } else {
291 dt_closedloop.SetRawPosition(left_encoder, right_encoder);
292 }
293 }
James Kuszmaul3f354742013-03-10 17:27:56 -0700294 dt_closedloop.Update(!bad_pos, bad_pos || bad_output);
brians343bc112013-02-10 01:53:46 +0000295 dt_openloop.SetGoal(wheel, throttle, quickturn, highgear);
296 dt_openloop.Update();
Brian Silverman2845c4c2013-03-16 19:54:08 -0700297 if (control_loop_driving) {
298 dt_closedloop.SendMotors(output);
299 } else {
300 dt_openloop.SendMotors(output);
brians343bc112013-02-10 01:53:46 +0000301 }
302}
303
304} // namespace control_loops
305} // namespace frc971