blob: 79e2864968e48b3a07f97c5d5cde83fd5db108fb [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>
brians343bc112013-02-10 01:53:46 +00007
8#include "aos/aos_core.h"
9#include "aos/common/logging/logging.h"
10#include "aos/common/queue.h"
James Kuszmaulf254c1a2013-03-10 16:31:26 -070011#include "frc971/control_loops/state_feedback_loop.h"
12#include "frc971/control_loops/drivetrain/drivetrain_motor_plant.h"
13#include "frc971/control_loops/drivetrain/drivetrain.q.h"
brians343bc112013-02-10 01:53:46 +000014#include "frc971/queues/GyroAngle.q.h"
15#include "frc971/queues/Piston.q.h"
16
17using frc971::sensors::gyro;
18
19namespace frc971 {
20namespace control_loops {
21
22// Width of the robot.
23const double width = 22.0 / 100.0 * 2.54;
24
Austin Schuh4352ac62013-03-19 06:23:16 +000025class DrivetrainMotorsSS {
brians343bc112013-02-10 01:53:46 +000026 public:
Austin Schuh4352ac62013-03-19 06:23:16 +000027 DrivetrainMotorsSS ()
28 : loop_(new StateFeedbackLoop<4, 2, 2>(MakeDrivetrainLoop())) {
brians343bc112013-02-10 01:53:46 +000029 _offset = 0;
30 _integral_offset = 0;
31 _left_goal = 0.0;
32 _right_goal = 0.0;
33 _raw_left = 0.0;
34 _raw_right = 0.0;
Austin Schuh4352ac62013-03-19 06:23:16 +000035 _control_loop_driving = false;
brians343bc112013-02-10 01:53:46 +000036 }
37 void SetGoal(double left, double left_velocity, double right, double right_velocity) {
38 _left_goal = left;
39 _right_goal = right;
Austin Schuh4352ac62013-03-19 06:23:16 +000040 loop_->R << left, left_velocity, right, right_velocity;
brians343bc112013-02-10 01:53:46 +000041 }
42 void SetRawPosition(double left, double right) {
43 _raw_right = right;
44 _raw_left = left;
Austin Schuh4352ac62013-03-19 06:23:16 +000045 loop_->Y << left, right;
brians343bc112013-02-10 01:53:46 +000046 }
Austin Schuh4352ac62013-03-19 06:23:16 +000047 void SetPosition(
48 double left, double right, double gyro, bool control_loop_driving) {
brians343bc112013-02-10 01:53:46 +000049 // Decay the offset quickly because this gyro is great.
50 _offset = (0.25) * (right - left - gyro * width) / 2.0 + 0.75 * _offset;
51 const double angle_error = (_right_goal - _left_goal) / width - (_raw_right - _offset - _raw_left - _offset) / width;
Austin Schuh4352ac62013-03-19 06:23:16 +000052 // TODO(aschuh): Add in the gyro.
53 _integral_offset = 0.0;
54 _offset = 0.0;
brians343bc112013-02-10 01:53:46 +000055 _gyro = gyro;
Austin Schuh4352ac62013-03-19 06:23:16 +000056 _control_loop_driving = control_loop_driving;
brians343bc112013-02-10 01:53:46 +000057 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 }
Austin Schuh4352ac62013-03-19 06:23:16 +000060
61 void Update(bool update_observer, bool stop_motors) {
62 loop_->Update(update_observer, stop_motors);
brians343bc112013-02-10 01:53:46 +000063 }
64
Austin Schuh4352ac62013-03-19 06:23:16 +000065 void SendMotors(Drivetrain::Output *output) {
66 if (output) {
67 output->left_voltage = loop_->U(0, 0);
68 output->right_voltage = loop_->U(1, 0);
brians8ad74052013-03-16 21:04:51 +000069 }
brians343bc112013-02-10 01:53:46 +000070 }
71 void PrintMotors() const {
72 // 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 +000073 ::Eigen::Matrix<double, 4, 1> E = loop_->R - loop_->X_hat;
74 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 +000075 }
76
77 private:
Austin Schuh4352ac62013-03-19 06:23:16 +000078 ::std::unique_ptr<StateFeedbackLoop<4, 2, 2>> loop_;
79
brians343bc112013-02-10 01:53:46 +000080 double _integral_offset;
81 double _offset;
82 double _gyro;
83 double _left_goal;
84 double _right_goal;
85 double _raw_left;
86 double _raw_right;
Austin Schuh4352ac62013-03-19 06:23:16 +000087 bool _control_loop_driving;
brians343bc112013-02-10 01:53:46 +000088};
89
90class DrivetrainMotorsOL {
91 public:
92 DrivetrainMotorsOL() {
93 _old_wheel = 0.0;
brians343bc112013-02-10 01:53:46 +000094 _wheel = 0.0;
95 _throttle = 0.0;
96 _quickturn = false;
97 _highgear = true;
98 _neg_inertia_accumulator = 0.0;
99 _left_pwm = 0.0;
100 _right_pwm = 0.0;
101 }
102 void SetGoal(double wheel, double throttle, bool quickturn, bool highgear) {
103 _wheel = wheel;
104 _throttle = throttle;
105 _quickturn = quickturn;
106 _highgear = highgear;
107 _left_pwm = 0.0;
108 _right_pwm = 0.0;
109 }
110 void Update(void) {
111 double overPower;
112 float sensitivity = 1.7;
113 float angular_power;
114 float linear_power;
115 double wheel;
116
117 double neg_inertia = _wheel - _old_wheel;
118 _old_wheel = _wheel;
119
120 double wheelNonLinearity;
121 if (_highgear) {
Brian Silverman77a76002013-03-16 20:09:00 -0700122 wheelNonLinearity = 0.1; // used to be csvReader->TURN_NONLIN_HIGH
brians343bc112013-02-10 01:53:46 +0000123 // Apply a sin function that's scaled to make it feel better.
124 const double angular_range = M_PI / 2.0 * wheelNonLinearity;
125 wheel = sin(angular_range * _wheel) / sin(angular_range);
Brian Silverman77a76002013-03-16 20:09:00 -0700126 wheel = sin(angular_range * wheel) / sin(angular_range);
brians343bc112013-02-10 01:53:46 +0000127 } else {
Brian Silvermandf43ec22013-03-16 23:48:29 -0700128 wheelNonLinearity = 0.2; // used to be csvReader->TURN_NONLIN_LOW
brians343bc112013-02-10 01:53:46 +0000129 // Apply a sin function that's scaled to make it feel better.
130 const double angular_range = M_PI / 2.0 * wheelNonLinearity;
131 wheel = sin(angular_range * _wheel) / sin(angular_range);
Brian Silverman77a76002013-03-16 20:09:00 -0700132 wheel = sin(angular_range * wheel) / sin(angular_range);
133 wheel = sin(angular_range * wheel) / sin(angular_range);
brians343bc112013-02-10 01:53:46 +0000134 }
135
Brian Silvermandf43ec22013-03-16 23:48:29 -0700136 static const double kThrottleDeadband = 0.05;
137 if (::std::abs(_throttle) < kThrottleDeadband) {
138 _throttle = 0;
139 } else {
140 _throttle = copysign((::std::abs(_throttle) - kThrottleDeadband) /
141 (1.0 - kThrottleDeadband), _throttle);
142 }
143
brians343bc112013-02-10 01:53:46 +0000144 double neg_inertia_scalar;
145 if (_highgear) {
Brian Silvermandf43ec22013-03-16 23:48:29 -0700146 neg_inertia_scalar = 8.0; // used to be csvReader->NEG_INTERTIA_HIGH
brians343bc112013-02-10 01:53:46 +0000147 sensitivity = 1.22; // used to be csvReader->SENSE_HIGH
148 } else {
149 if (wheel * neg_inertia > 0) {
Brian Silvermandf43ec22013-03-16 23:48:29 -0700150 neg_inertia_scalar = 5; // used to be csvReader->NEG_INERTIA_LOW_MORE
brians343bc112013-02-10 01:53:46 +0000151 } else {
Brian Silvermandf43ec22013-03-16 23:48:29 -0700152 if (::std::abs(wheel) > 0.65) {
153 neg_inertia_scalar = 5; // used to be csvReader->NEG_INTERTIA_LOW_LESS_EXT
brians343bc112013-02-10 01:53:46 +0000154 } else {
Brian Silvermandf43ec22013-03-16 23:48:29 -0700155 neg_inertia_scalar = 5; // used to be csvReader->NEG_INTERTIA_LOW_LESS
brians343bc112013-02-10 01:53:46 +0000156 }
157 }
158 sensitivity = 1.24; // used to be csvReader->SENSE_LOW
brians343bc112013-02-10 01:53:46 +0000159 }
160 double neg_inertia_power = neg_inertia * neg_inertia_scalar;
161 _neg_inertia_accumulator += neg_inertia_power;
162
163 wheel = wheel + _neg_inertia_accumulator;
164 if (_neg_inertia_accumulator > 1) {
165 _neg_inertia_accumulator -= 1;
166 } else if (_neg_inertia_accumulator < -1) {
167 _neg_inertia_accumulator += 1;
168 } else {
169 _neg_inertia_accumulator = 0;
170 }
171
172 linear_power = _throttle;
173
brians343bc112013-02-10 01:53:46 +0000174 if (_quickturn) {
175 double qt_angular_power = wheel;
Brian Silvermandf43ec22013-03-16 23:48:29 -0700176 if (::std::abs(linear_power) < 0.2) {
brians343bc112013-02-10 01:53:46 +0000177 if (qt_angular_power > 1) qt_angular_power = 1.0;
178 if (qt_angular_power < -1) qt_angular_power = -1.0;
179 } else {
180 qt_angular_power = 0.0;
181 }
brians343bc112013-02-10 01:53:46 +0000182 overPower = 1.0;
183 if (_highgear) {
184 sensitivity = 1.0;
185 } else {
186 sensitivity = 1.0;
187 }
188 angular_power = wheel;
189 } else {
190 overPower = 0.0;
Brian Silvermandf43ec22013-03-16 23:48:29 -0700191 angular_power = ::std::abs(_throttle) * wheel * sensitivity;
brians343bc112013-02-10 01:53:46 +0000192 }
193
194 _right_pwm = _left_pwm = linear_power;
195 _left_pwm += angular_power;
196 _right_pwm -= angular_power;
197
198 if (_left_pwm > 1.0) {
199 _right_pwm -= overPower*(_left_pwm - 1.0);
200 _left_pwm = 1.0;
201 } else if (_right_pwm > 1.0) {
202 _left_pwm -= overPower*(_right_pwm - 1.0);
203 _right_pwm = 1.0;
204 } else if (_left_pwm < -1.0) {
205 _right_pwm += overPower*(-1.0 - _left_pwm);
206 _left_pwm = -1.0;
207 } else if (_right_pwm < -1.0) {
208 _left_pwm += overPower*(-1.0 - _right_pwm);
209 _right_pwm = -1.0;
210 }
211 }
212
213 void SendMotors(Drivetrain::Output *output) {
Brian Silvermandf43ec22013-03-16 23:48:29 -0700214 LOG(DEBUG, "left pwm: %f right pwm: %f wheel: %f throttle: %f\n",
215 _left_pwm, _right_pwm, _wheel, _throttle);
brians8ad74052013-03-16 21:04:51 +0000216 if (output) {
217 output->left_voltage = _left_pwm * 12.0;
218 output->right_voltage = _right_pwm * 12.0;
219 }
brians343bc112013-02-10 01:53:46 +0000220 if (_highgear) {
221 shifters.MakeWithBuilder().set(false).Send();
222 } else {
223 shifters.MakeWithBuilder().set(true).Send();
224 }
225 }
226
227 private:
228 double _old_wheel;
229 double _wheel;
230 double _throttle;
231 bool _quickturn;
232 bool _highgear;
233 double _neg_inertia_accumulator;
234 double _left_pwm;
235 double _right_pwm;
brians343bc112013-02-10 01:53:46 +0000236};
237
238void DrivetrainLoop::RunIteration(const Drivetrain::Goal *goal,
239 const Drivetrain::Position *position,
240 Drivetrain::Output *output,
241 Drivetrain::Status * /*status*/) {
242 // TODO(aschuh): These should be members of the class.
243 static DrivetrainMotorsSS dt_closedloop;
244 static DrivetrainMotorsOL dt_openloop;
245
246 bool bad_pos = false;
247 if (position == NULL) {
James Kuszmaul3f354742013-03-10 17:27:56 -0700248 LOG(WARNING, "no position\n");
brians343bc112013-02-10 01:53:46 +0000249 bad_pos = true;
250 }
251
James Kuszmaul3f354742013-03-10 17:27:56 -0700252 bool bad_output = false;
253 if (output == NULL) {
254 LOG(WARNING, "no output\n");
255 bad_output = true;
256 }
257
brians343bc112013-02-10 01:53:46 +0000258 double wheel = goal->steering;
259 double throttle = goal->throttle;
260 bool quickturn = goal->quickturn;
261 bool highgear = goal->highgear;
262
263 bool control_loop_driving = goal->control_loop_driving;
264 double left_goal = goal->left_goal;
265 double right_goal = goal->right_goal;
266
Austin Schuh4352ac62013-03-19 06:23:16 +0000267 dt_closedloop.SetGoal(left_goal, goal->left_velocity_goal,
268 right_goal, goal->right_velocity_goal);
brians343bc112013-02-10 01:53:46 +0000269 if (!bad_pos) {
270 const double left_encoder = position->left_encoder;
271 const double right_encoder = position->right_encoder;
272 if (gyro.FetchLatest()) {
273 dt_closedloop.SetPosition(left_encoder, right_encoder,
274 gyro->angle, control_loop_driving);
275 } else {
276 dt_closedloop.SetRawPosition(left_encoder, right_encoder);
277 }
278 }
Austin Schuh4352ac62013-03-19 06:23:16 +0000279 dt_closedloop.Update(position, output == NULL);
280 //dt_closedloop.PrintMotors();
brians343bc112013-02-10 01:53:46 +0000281 dt_openloop.SetGoal(wheel, throttle, quickturn, highgear);
282 dt_openloop.Update();
Brian Silverman2845c4c2013-03-16 19:54:08 -0700283 if (control_loop_driving) {
284 dt_closedloop.SendMotors(output);
285 } else {
286 dt_openloop.SendMotors(output);
brians343bc112013-02-10 01:53:46 +0000287 }
288}
289
290} // namespace control_loops
291} // namespace frc971