blob: 548113e520437ae2c1df484fdae48bb3637d664b [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
brians343bc112013-02-10 01:53:46 +00008#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
Austin Schuh4352ac62013-03-19 06:23:16 +000024class DrivetrainMotorsSS {
brians343bc112013-02-10 01:53:46 +000025 public:
Austin Schuh4352ac62013-03-19 06:23:16 +000026 DrivetrainMotorsSS ()
27 : loop_(new StateFeedbackLoop<4, 2, 2>(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;
Austin Schuh4352ac62013-03-19 06:23:16 +000034 _control_loop_driving = false;
brians343bc112013-02-10 01:53:46 +000035 }
36 void SetGoal(double left, double left_velocity, double right, double right_velocity) {
37 _left_goal = left;
38 _right_goal = right;
Austin Schuh4352ac62013-03-19 06:23:16 +000039 loop_->R << left, left_velocity, right, right_velocity;
brians343bc112013-02-10 01:53:46 +000040 }
41 void SetRawPosition(double left, double right) {
42 _raw_right = right;
43 _raw_left = left;
Austin Schuh4352ac62013-03-19 06:23:16 +000044 loop_->Y << left, right;
brians343bc112013-02-10 01:53:46 +000045 }
Austin Schuh4352ac62013-03-19 06:23:16 +000046 void SetPosition(
47 double left, double right, double gyro, bool control_loop_driving) {
brians343bc112013-02-10 01:53:46 +000048 // Decay the offset quickly because this gyro is great.
49 _offset = (0.25) * (right - left - gyro * width) / 2.0 + 0.75 * _offset;
50 const double angle_error = (_right_goal - _left_goal) / width - (_raw_right - _offset - _raw_left - _offset) / width;
Austin Schuh4352ac62013-03-19 06:23:16 +000051 // TODO(aschuh): Add in the gyro.
52 _integral_offset = 0.0;
53 _offset = 0.0;
brians343bc112013-02-10 01:53:46 +000054 _gyro = gyro;
Austin Schuh4352ac62013-03-19 06:23:16 +000055 _control_loop_driving = control_loop_driving;
brians343bc112013-02-10 01:53:46 +000056 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 }
Austin Schuh4352ac62013-03-19 06:23:16 +000059
60 void Update(bool update_observer, bool stop_motors) {
61 loop_->Update(update_observer, stop_motors);
brians343bc112013-02-10 01:53:46 +000062 }
63
Austin Schuh4352ac62013-03-19 06:23:16 +000064 void SendMotors(Drivetrain::Output *output) {
65 if (output) {
66 output->left_voltage = loop_->U(0, 0);
67 output->right_voltage = loop_->U(1, 0);
brians8ad74052013-03-16 21:04:51 +000068 }
brians343bc112013-02-10 01:53:46 +000069 }
70 void PrintMotors() const {
71 // 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 +000072 ::Eigen::Matrix<double, 4, 1> E = loop_->R - loop_->X_hat;
73 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 +000074 }
75
76 private:
Austin Schuh4352ac62013-03-19 06:23:16 +000077 ::std::unique_ptr<StateFeedbackLoop<4, 2, 2>> loop_;
78
brians343bc112013-02-10 01:53:46 +000079 double _integral_offset;
80 double _offset;
81 double _gyro;
82 double _left_goal;
83 double _right_goal;
84 double _raw_left;
85 double _raw_right;
Austin Schuh4352ac62013-03-19 06:23:16 +000086 bool _control_loop_driving;
brians343bc112013-02-10 01:53:46 +000087};
88
89class DrivetrainMotorsOL {
90 public:
91 DrivetrainMotorsOL() {
92 _old_wheel = 0.0;
brians343bc112013-02-10 01:53:46 +000093 _wheel = 0.0;
94 _throttle = 0.0;
95 _quickturn = false;
96 _highgear = true;
97 _neg_inertia_accumulator = 0.0;
98 _left_pwm = 0.0;
99 _right_pwm = 0.0;
100 }
101 void SetGoal(double wheel, double throttle, bool quickturn, bool highgear) {
102 _wheel = wheel;
103 _throttle = throttle;
104 _quickturn = quickturn;
105 _highgear = highgear;
106 _left_pwm = 0.0;
107 _right_pwm = 0.0;
108 }
109 void Update(void) {
110 double overPower;
111 float sensitivity = 1.7;
112 float angular_power;
113 float linear_power;
114 double wheel;
115
116 double neg_inertia = _wheel - _old_wheel;
117 _old_wheel = _wheel;
118
119 double wheelNonLinearity;
120 if (_highgear) {
Brian Silverman77a76002013-03-16 20:09:00 -0700121 wheelNonLinearity = 0.1; // used to be csvReader->TURN_NONLIN_HIGH
brians343bc112013-02-10 01:53:46 +0000122 // Apply a sin function that's scaled to make it feel better.
123 const double angular_range = M_PI / 2.0 * wheelNonLinearity;
124 wheel = sin(angular_range * _wheel) / sin(angular_range);
Brian Silverman77a76002013-03-16 20:09:00 -0700125 wheel = sin(angular_range * wheel) / sin(angular_range);
brians343bc112013-02-10 01:53:46 +0000126 } else {
Brian Silvermandf43ec22013-03-16 23:48:29 -0700127 wheelNonLinearity = 0.2; // used to be csvReader->TURN_NONLIN_LOW
brians343bc112013-02-10 01:53:46 +0000128 // Apply a sin function that's scaled to make it feel better.
129 const double angular_range = M_PI / 2.0 * wheelNonLinearity;
130 wheel = sin(angular_range * _wheel) / sin(angular_range);
Brian Silverman77a76002013-03-16 20:09:00 -0700131 wheel = sin(angular_range * wheel) / sin(angular_range);
132 wheel = sin(angular_range * wheel) / sin(angular_range);
brians343bc112013-02-10 01:53:46 +0000133 }
134
Brian Silvermandf43ec22013-03-16 23:48:29 -0700135 static const double kThrottleDeadband = 0.05;
136 if (::std::abs(_throttle) < kThrottleDeadband) {
137 _throttle = 0;
138 } else {
139 _throttle = copysign((::std::abs(_throttle) - kThrottleDeadband) /
140 (1.0 - kThrottleDeadband), _throttle);
141 }
142
brians343bc112013-02-10 01:53:46 +0000143 double neg_inertia_scalar;
144 if (_highgear) {
Brian Silvermandf43ec22013-03-16 23:48:29 -0700145 neg_inertia_scalar = 8.0; // used to be csvReader->NEG_INTERTIA_HIGH
brians343bc112013-02-10 01:53:46 +0000146 sensitivity = 1.22; // used to be csvReader->SENSE_HIGH
147 } else {
148 if (wheel * neg_inertia > 0) {
Brian Silvermandf43ec22013-03-16 23:48:29 -0700149 neg_inertia_scalar = 5; // used to be csvReader->NEG_INERTIA_LOW_MORE
brians343bc112013-02-10 01:53:46 +0000150 } else {
Brian Silvermandf43ec22013-03-16 23:48:29 -0700151 if (::std::abs(wheel) > 0.65) {
152 neg_inertia_scalar = 5; // used to be csvReader->NEG_INTERTIA_LOW_LESS_EXT
brians343bc112013-02-10 01:53:46 +0000153 } else {
Brian Silvermandf43ec22013-03-16 23:48:29 -0700154 neg_inertia_scalar = 5; // used to be csvReader->NEG_INTERTIA_LOW_LESS
brians343bc112013-02-10 01:53:46 +0000155 }
156 }
157 sensitivity = 1.24; // used to be csvReader->SENSE_LOW
brians343bc112013-02-10 01:53:46 +0000158 }
159 double neg_inertia_power = neg_inertia * neg_inertia_scalar;
160 _neg_inertia_accumulator += neg_inertia_power;
161
162 wheel = wheel + _neg_inertia_accumulator;
163 if (_neg_inertia_accumulator > 1) {
164 _neg_inertia_accumulator -= 1;
165 } else if (_neg_inertia_accumulator < -1) {
166 _neg_inertia_accumulator += 1;
167 } else {
168 _neg_inertia_accumulator = 0;
169 }
170
171 linear_power = _throttle;
172
brians343bc112013-02-10 01:53:46 +0000173 if (_quickturn) {
174 double qt_angular_power = wheel;
Brian Silvermandf43ec22013-03-16 23:48:29 -0700175 if (::std::abs(linear_power) < 0.2) {
brians343bc112013-02-10 01:53:46 +0000176 if (qt_angular_power > 1) qt_angular_power = 1.0;
177 if (qt_angular_power < -1) qt_angular_power = -1.0;
178 } else {
179 qt_angular_power = 0.0;
180 }
brians343bc112013-02-10 01:53:46 +0000181 overPower = 1.0;
182 if (_highgear) {
183 sensitivity = 1.0;
184 } else {
185 sensitivity = 1.0;
186 }
187 angular_power = wheel;
188 } else {
189 overPower = 0.0;
Brian Silvermandf43ec22013-03-16 23:48:29 -0700190 angular_power = ::std::abs(_throttle) * wheel * sensitivity;
brians343bc112013-02-10 01:53:46 +0000191 }
192
193 _right_pwm = _left_pwm = linear_power;
194 _left_pwm += angular_power;
195 _right_pwm -= angular_power;
196
197 if (_left_pwm > 1.0) {
198 _right_pwm -= overPower*(_left_pwm - 1.0);
199 _left_pwm = 1.0;
200 } else if (_right_pwm > 1.0) {
201 _left_pwm -= overPower*(_right_pwm - 1.0);
202 _right_pwm = 1.0;
203 } else if (_left_pwm < -1.0) {
204 _right_pwm += overPower*(-1.0 - _left_pwm);
205 _left_pwm = -1.0;
206 } else if (_right_pwm < -1.0) {
207 _left_pwm += overPower*(-1.0 - _right_pwm);
208 _right_pwm = -1.0;
209 }
210 }
211
212 void SendMotors(Drivetrain::Output *output) {
Brian Silvermandf43ec22013-03-16 23:48:29 -0700213 LOG(DEBUG, "left pwm: %f right pwm: %f wheel: %f throttle: %f\n",
214 _left_pwm, _right_pwm, _wheel, _throttle);
brians8ad74052013-03-16 21:04:51 +0000215 if (output) {
216 output->left_voltage = _left_pwm * 12.0;
217 output->right_voltage = _right_pwm * 12.0;
218 }
brians343bc112013-02-10 01:53:46 +0000219 if (_highgear) {
220 shifters.MakeWithBuilder().set(false).Send();
221 } else {
222 shifters.MakeWithBuilder().set(true).Send();
223 }
224 }
225
226 private:
227 double _old_wheel;
228 double _wheel;
229 double _throttle;
230 bool _quickturn;
231 bool _highgear;
232 double _neg_inertia_accumulator;
233 double _left_pwm;
234 double _right_pwm;
brians343bc112013-02-10 01:53:46 +0000235};
236
237void DrivetrainLoop::RunIteration(const Drivetrain::Goal *goal,
238 const Drivetrain::Position *position,
239 Drivetrain::Output *output,
240 Drivetrain::Status * /*status*/) {
241 // TODO(aschuh): These should be members of the class.
242 static DrivetrainMotorsSS dt_closedloop;
243 static DrivetrainMotorsOL dt_openloop;
244
245 bool bad_pos = false;
246 if (position == NULL) {
James Kuszmaul3f354742013-03-10 17:27:56 -0700247 LOG(WARNING, "no position\n");
brians343bc112013-02-10 01:53:46 +0000248 bad_pos = true;
249 }
250
251 double wheel = goal->steering;
252 double throttle = goal->throttle;
253 bool quickturn = goal->quickturn;
254 bool highgear = goal->highgear;
255
256 bool control_loop_driving = goal->control_loop_driving;
257 double left_goal = goal->left_goal;
258 double right_goal = goal->right_goal;
259
Austin Schuh4352ac62013-03-19 06:23:16 +0000260 dt_closedloop.SetGoal(left_goal, goal->left_velocity_goal,
261 right_goal, goal->right_velocity_goal);
brians343bc112013-02-10 01:53:46 +0000262 if (!bad_pos) {
263 const double left_encoder = position->left_encoder;
264 const double right_encoder = position->right_encoder;
265 if (gyro.FetchLatest()) {
266 dt_closedloop.SetPosition(left_encoder, right_encoder,
267 gyro->angle, control_loop_driving);
268 } else {
269 dt_closedloop.SetRawPosition(left_encoder, right_encoder);
270 }
271 }
Austin Schuh4352ac62013-03-19 06:23:16 +0000272 dt_closedloop.Update(position, output == NULL);
273 //dt_closedloop.PrintMotors();
brians343bc112013-02-10 01:53:46 +0000274 dt_openloop.SetGoal(wheel, throttle, quickturn, highgear);
275 dt_openloop.Update();
Brian Silverman2845c4c2013-03-16 19:54:08 -0700276 if (control_loop_driving) {
277 dt_closedloop.SendMotors(output);
278 } else {
279 dt_openloop.SendMotors(output);
brians343bc112013-02-10 01:53:46 +0000280 }
281}
282
283} // namespace control_loops
284} // namespace frc971