blob: b0cb8b420469dcfc33264bbedc5d62269122bc40 [file] [log] [blame]
Daniel Petti1f448512013-10-19 19:35:55 +00001#include "bot3/control_loops/drivetrain/drivetrain.h"
2
3#include <stdio.h>
4#include <sched.h>
5#include <cmath>
6#include <memory>
7
8#include "aos/common/logging/logging.h"
9#include "aos/common/queue.h"
10#include "bot3/control_loops/drivetrain/drivetrain_motor_plant.h"
11#include "bot3/control_loops/drivetrain/drivetrain.q.h"
12#include "frc971/control_loops/state_feedback_loop.h"
13#include "frc971/queues/GyroAngle.q.h"
14#include "frc971/queues/Piston.q.h"
15
16using frc971::sensors::gyro;
17using ::frc971::control_loops::shifters;
18
19namespace bot3 {
20namespace control_loops {
21
22// Width of the robot.
23const double width = 22.0 / 100.0 * 2.54;
24
25class DrivetrainMotorsSS {
26 public:
27 DrivetrainMotorsSS ()
28 : loop_(new StateFeedbackLoop<4, 2, 2>(MakeDrivetrainLoop())) {
29 _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;
35 _control_loop_driving = false;
36 }
37 void SetGoal(double left, double left_velocity, double right, double right_velocity) {
38 _left_goal = left;
39 _right_goal = right;
40 loop_->R << left, left_velocity, right, right_velocity;
41 }
42 void SetRawPosition(double left, double right) {
43 _raw_right = right;
44 _raw_left = left;
45 loop_->Y << left, right;
46 }
47 void SetPosition(
48 double left, double right, double gyro, bool control_loop_driving) {
49 // 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;
52 // TODO(aschuh): Add in the gyro.
53 _integral_offset = 0.0;
54 _offset = 0.0;
55 _gyro = gyro;
56 _control_loop_driving = control_loop_driving;
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
61 void Update(bool update_observer, bool stop_motors) {
62 loop_->Update(update_observer, stop_motors);
63 }
64
65 void SendMotors(Drivetrain::Output *output) {
66 if (output) {
67 output->left_voltage = loop_->U(0, 0);
68 output->right_voltage = loop_->U(1, 0);
69 }
70 }
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);
73 ::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));
75 }
76
77 private:
78 ::std::unique_ptr<StateFeedbackLoop<4, 2, 2>> loop_;
79
80 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;
87 bool _control_loop_driving;
88};
89
90class DrivetrainMotorsOL {
91 public:
92 DrivetrainMotorsOL() {
93 _old_wheel = 0.0;
94 _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) {
122 wheelNonLinearity = 0.1; // used to be csvReader->TURN_NONLIN_HIGH
123 // 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);
126 wheel = sin(angular_range * wheel) / sin(angular_range);
127 } else {
128 wheelNonLinearity = 0.2; // used to be csvReader->TURN_NONLIN_LOW
129 // 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);
132 wheel = sin(angular_range * wheel) / sin(angular_range);
133 wheel = sin(angular_range * wheel) / sin(angular_range);
134 }
135
136 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
144 double neg_inertia_scalar;
145 if (_highgear) {
146 neg_inertia_scalar = 8.0; // used to be csvReader->NEG_INTERTIA_HIGH
147 sensitivity = 1.22; // used to be csvReader->SENSE_HIGH
148 } else {
149 if (wheel * neg_inertia > 0) {
150 neg_inertia_scalar = 5; // used to be csvReader->NEG_INERTIA_LOW_MORE
151 } else {
152 if (::std::abs(wheel) > 0.65) {
153 neg_inertia_scalar = 5; // used to be csvReader->NEG_INTERTIA_LOW_LESS_EXT
154 } else {
155 neg_inertia_scalar = 5; // used to be csvReader->NEG_INTERTIA_LOW_LESS
156 }
157 }
158 sensitivity = 1.24; // used to be csvReader->SENSE_LOW
159 }
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
174 if (_quickturn) {
175 double qt_angular_power = wheel;
176 if (::std::abs(linear_power) < 0.2) {
177 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 }
182 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;
191 angular_power = ::std::abs(_throttle) * wheel * sensitivity;
192 }
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) {
214 LOG(DEBUG, "left pwm: %f right pwm: %f wheel: %f throttle: %f\n",
215 _left_pwm, _right_pwm, _wheel, _throttle);
216 if (output) {
217 output->left_voltage = _left_pwm * 12.0;
218 output->right_voltage = _right_pwm * 12.0;
219 }
220 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;
236};
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) {
248 LOG(WARNING, "no position\n");
249 bad_pos = true;
250 }
251
252 double wheel = goal->steering;
253 double throttle = goal->throttle;
254 bool quickturn = goal->quickturn;
255 bool highgear = goal->highgear;
256
257 bool control_loop_driving = goal->control_loop_driving;
258 double left_goal = goal->left_goal;
259 double right_goal = goal->right_goal;
260
261 dt_closedloop.SetGoal(left_goal, goal->left_velocity_goal,
262 right_goal, goal->right_velocity_goal);
263 if (!bad_pos) {
264 const double left_encoder = position->left_encoder;
265 const double right_encoder = position->right_encoder;
266 if (gyro.FetchLatest()) {
267 dt_closedloop.SetPosition(left_encoder, right_encoder,
268 gyro->angle, control_loop_driving);
269 } else {
270 dt_closedloop.SetRawPosition(left_encoder, right_encoder);
271 }
272 }
273 dt_closedloop.Update(position, output == NULL);
274 //dt_closedloop.PrintMotors();
275 dt_openloop.SetGoal(wheel, throttle, quickturn, highgear);
276 dt_openloop.Update();
277 if (control_loop_driving) {
278 dt_closedloop.SendMotors(output);
279 } else {
280 dt_openloop.SendMotors(output);
281 }
282}
283
284} // namespace control_loops
285} // namespace bot3