James Kuszmaul | f254c1a | 2013-03-10 16:31:26 -0700 | [diff] [blame] | 1 | #include "frc971/control_loops/drivetrain/drivetrain.h" |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 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" |
James Kuszmaul | f254c1a | 2013-03-10 16:31:26 -0700 | [diff] [blame] | 10 | #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" |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 13 | #include "frc971/queues/GyroAngle.q.h" |
| 14 | #include "frc971/queues/Piston.q.h" |
| 15 | |
| 16 | using frc971::sensors::gyro; |
| 17 | |
| 18 | namespace frc971 { |
| 19 | namespace control_loops { |
| 20 | |
| 21 | // Width of the robot. |
| 22 | const double width = 22.0 / 100.0 * 2.54; |
| 23 | |
James Kuszmaul | f254c1a | 2013-03-10 16:31:26 -0700 | [diff] [blame] | 24 | class DrivetrainMotorsSS : public StateFeedbackLoop<4, 2, 2> { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 25 | public: |
James Kuszmaul | f254c1a | 2013-03-10 16:31:26 -0700 | [diff] [blame] | 26 | DrivetrainMotorsSS (void) |
| 27 | : StateFeedbackLoop(MakeDrivetrainLoop()) { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 28 | _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; |
Brian Silverman | df43ec2 | 2013-03-16 23:48:29 -0700 | [diff] [blame^] | 51 | } else if (::std::abs(angle_error) < M_PI / 10.0) { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 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) { |
brians | 8ad7405 | 2013-03-16 21:04:51 +0000 | [diff] [blame] | 79 | if (status) { |
| 80 | status->left_voltage = UnDeadband(U[0]); |
| 81 | status->right_voltage = UnDeadband(U[1]); |
| 82 | } |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 83 | } |
| 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 | |
| 99 | class DrivetrainMotorsOL { |
| 100 | public: |
| 101 | DrivetrainMotorsOL() { |
| 102 | _old_wheel = 0.0; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 103 | _wheel = 0.0; |
| 104 | _throttle = 0.0; |
| 105 | _quickturn = false; |
| 106 | _highgear = true; |
| 107 | _neg_inertia_accumulator = 0.0; |
| 108 | _left_pwm = 0.0; |
| 109 | _right_pwm = 0.0; |
| 110 | } |
| 111 | void SetGoal(double wheel, double throttle, bool quickturn, bool highgear) { |
| 112 | _wheel = wheel; |
| 113 | _throttle = throttle; |
| 114 | _quickturn = quickturn; |
| 115 | _highgear = highgear; |
| 116 | _left_pwm = 0.0; |
| 117 | _right_pwm = 0.0; |
| 118 | } |
| 119 | void Update(void) { |
| 120 | double overPower; |
| 121 | float sensitivity = 1.7; |
| 122 | float angular_power; |
| 123 | float linear_power; |
| 124 | double wheel; |
| 125 | |
| 126 | double neg_inertia = _wheel - _old_wheel; |
| 127 | _old_wheel = _wheel; |
| 128 | |
| 129 | double wheelNonLinearity; |
| 130 | if (_highgear) { |
Brian Silverman | 77a7600 | 2013-03-16 20:09:00 -0700 | [diff] [blame] | 131 | wheelNonLinearity = 0.1; // used to be csvReader->TURN_NONLIN_HIGH |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 132 | // Apply a sin function that's scaled to make it feel better. |
| 133 | const double angular_range = M_PI / 2.0 * wheelNonLinearity; |
| 134 | wheel = sin(angular_range * _wheel) / sin(angular_range); |
Brian Silverman | 77a7600 | 2013-03-16 20:09:00 -0700 | [diff] [blame] | 135 | wheel = sin(angular_range * wheel) / sin(angular_range); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 136 | } else { |
Brian Silverman | df43ec2 | 2013-03-16 23:48:29 -0700 | [diff] [blame^] | 137 | wheelNonLinearity = 0.2; // used to be csvReader->TURN_NONLIN_LOW |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 138 | // Apply a sin function that's scaled to make it feel better. |
| 139 | const double angular_range = M_PI / 2.0 * wheelNonLinearity; |
| 140 | wheel = sin(angular_range * _wheel) / sin(angular_range); |
Brian Silverman | 77a7600 | 2013-03-16 20:09:00 -0700 | [diff] [blame] | 141 | wheel = sin(angular_range * wheel) / sin(angular_range); |
| 142 | wheel = sin(angular_range * wheel) / sin(angular_range); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 143 | } |
| 144 | |
Brian Silverman | df43ec2 | 2013-03-16 23:48:29 -0700 | [diff] [blame^] | 145 | static const double kThrottleDeadband = 0.05; |
| 146 | if (::std::abs(_throttle) < kThrottleDeadband) { |
| 147 | _throttle = 0; |
| 148 | } else { |
| 149 | _throttle = copysign((::std::abs(_throttle) - kThrottleDeadband) / |
| 150 | (1.0 - kThrottleDeadband), _throttle); |
| 151 | } |
| 152 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 153 | double neg_inertia_scalar; |
| 154 | if (_highgear) { |
Brian Silverman | df43ec2 | 2013-03-16 23:48:29 -0700 | [diff] [blame^] | 155 | neg_inertia_scalar = 8.0; // used to be csvReader->NEG_INTERTIA_HIGH |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 156 | sensitivity = 1.22; // used to be csvReader->SENSE_HIGH |
| 157 | } else { |
| 158 | if (wheel * neg_inertia > 0) { |
Brian Silverman | df43ec2 | 2013-03-16 23:48:29 -0700 | [diff] [blame^] | 159 | neg_inertia_scalar = 5; // used to be csvReader->NEG_INERTIA_LOW_MORE |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 160 | } else { |
Brian Silverman | df43ec2 | 2013-03-16 23:48:29 -0700 | [diff] [blame^] | 161 | if (::std::abs(wheel) > 0.65) { |
| 162 | neg_inertia_scalar = 5; // used to be csvReader->NEG_INTERTIA_LOW_LESS_EXT |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 163 | } else { |
Brian Silverman | df43ec2 | 2013-03-16 23:48:29 -0700 | [diff] [blame^] | 164 | neg_inertia_scalar = 5; // used to be csvReader->NEG_INTERTIA_LOW_LESS |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | sensitivity = 1.24; // used to be csvReader->SENSE_LOW |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 168 | } |
| 169 | double neg_inertia_power = neg_inertia * neg_inertia_scalar; |
| 170 | _neg_inertia_accumulator += neg_inertia_power; |
| 171 | |
| 172 | wheel = wheel + _neg_inertia_accumulator; |
| 173 | if (_neg_inertia_accumulator > 1) { |
| 174 | _neg_inertia_accumulator -= 1; |
| 175 | } else if (_neg_inertia_accumulator < -1) { |
| 176 | _neg_inertia_accumulator += 1; |
| 177 | } else { |
| 178 | _neg_inertia_accumulator = 0; |
| 179 | } |
| 180 | |
| 181 | linear_power = _throttle; |
| 182 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 183 | if (_quickturn) { |
| 184 | double qt_angular_power = wheel; |
Brian Silverman | df43ec2 | 2013-03-16 23:48:29 -0700 | [diff] [blame^] | 185 | if (::std::abs(linear_power) < 0.2) { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 186 | if (qt_angular_power > 1) qt_angular_power = 1.0; |
| 187 | if (qt_angular_power < -1) qt_angular_power = -1.0; |
| 188 | } else { |
| 189 | qt_angular_power = 0.0; |
| 190 | } |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 191 | overPower = 1.0; |
| 192 | if (_highgear) { |
| 193 | sensitivity = 1.0; |
| 194 | } else { |
| 195 | sensitivity = 1.0; |
| 196 | } |
| 197 | angular_power = wheel; |
| 198 | } else { |
| 199 | overPower = 0.0; |
Brian Silverman | df43ec2 | 2013-03-16 23:48:29 -0700 | [diff] [blame^] | 200 | angular_power = ::std::abs(_throttle) * wheel * sensitivity; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | _right_pwm = _left_pwm = linear_power; |
| 204 | _left_pwm += angular_power; |
| 205 | _right_pwm -= angular_power; |
| 206 | |
| 207 | if (_left_pwm > 1.0) { |
| 208 | _right_pwm -= overPower*(_left_pwm - 1.0); |
| 209 | _left_pwm = 1.0; |
| 210 | } else if (_right_pwm > 1.0) { |
| 211 | _left_pwm -= overPower*(_right_pwm - 1.0); |
| 212 | _right_pwm = 1.0; |
| 213 | } else if (_left_pwm < -1.0) { |
| 214 | _right_pwm += overPower*(-1.0 - _left_pwm); |
| 215 | _left_pwm = -1.0; |
| 216 | } else if (_right_pwm < -1.0) { |
| 217 | _left_pwm += overPower*(-1.0 - _right_pwm); |
| 218 | _right_pwm = -1.0; |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | void SendMotors(Drivetrain::Output *output) { |
Brian Silverman | df43ec2 | 2013-03-16 23:48:29 -0700 | [diff] [blame^] | 223 | LOG(DEBUG, "left pwm: %f right pwm: %f wheel: %f throttle: %f\n", |
| 224 | _left_pwm, _right_pwm, _wheel, _throttle); |
brians | 8ad7405 | 2013-03-16 21:04:51 +0000 | [diff] [blame] | 225 | if (output) { |
| 226 | output->left_voltage = _left_pwm * 12.0; |
| 227 | output->right_voltage = _right_pwm * 12.0; |
| 228 | } |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 229 | if (_highgear) { |
| 230 | shifters.MakeWithBuilder().set(false).Send(); |
| 231 | } else { |
| 232 | shifters.MakeWithBuilder().set(true).Send(); |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | private: |
| 237 | double _old_wheel; |
| 238 | double _wheel; |
| 239 | double _throttle; |
| 240 | bool _quickturn; |
| 241 | bool _highgear; |
| 242 | double _neg_inertia_accumulator; |
| 243 | double _left_pwm; |
| 244 | double _right_pwm; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 245 | }; |
| 246 | |
| 247 | void DrivetrainLoop::RunIteration(const Drivetrain::Goal *goal, |
| 248 | const Drivetrain::Position *position, |
| 249 | Drivetrain::Output *output, |
| 250 | Drivetrain::Status * /*status*/) { |
| 251 | // TODO(aschuh): These should be members of the class. |
| 252 | static DrivetrainMotorsSS dt_closedloop; |
| 253 | static DrivetrainMotorsOL dt_openloop; |
| 254 | |
| 255 | bool bad_pos = false; |
| 256 | if (position == NULL) { |
James Kuszmaul | 3f35474 | 2013-03-10 17:27:56 -0700 | [diff] [blame] | 257 | LOG(WARNING, "no position\n"); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 258 | bad_pos = true; |
| 259 | } |
| 260 | |
James Kuszmaul | 3f35474 | 2013-03-10 17:27:56 -0700 | [diff] [blame] | 261 | bool bad_output = false; |
| 262 | if (output == NULL) { |
| 263 | LOG(WARNING, "no output\n"); |
| 264 | bad_output = true; |
| 265 | } |
| 266 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 267 | double wheel = goal->steering; |
| 268 | double throttle = goal->throttle; |
| 269 | bool quickturn = goal->quickturn; |
| 270 | bool highgear = goal->highgear; |
| 271 | |
| 272 | bool control_loop_driving = goal->control_loop_driving; |
| 273 | double left_goal = goal->left_goal; |
| 274 | double right_goal = goal->right_goal; |
| 275 | |
| 276 | dt_closedloop.SetGoal(left_goal, 0.0, right_goal, 0.0); |
| 277 | if (!bad_pos) { |
| 278 | const double left_encoder = position->left_encoder; |
| 279 | const double right_encoder = position->right_encoder; |
| 280 | if (gyro.FetchLatest()) { |
| 281 | dt_closedloop.SetPosition(left_encoder, right_encoder, |
| 282 | gyro->angle, control_loop_driving); |
| 283 | } else { |
| 284 | dt_closedloop.SetRawPosition(left_encoder, right_encoder); |
| 285 | } |
| 286 | } |
James Kuszmaul | 3f35474 | 2013-03-10 17:27:56 -0700 | [diff] [blame] | 287 | dt_closedloop.Update(!bad_pos, bad_pos || bad_output); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 288 | dt_openloop.SetGoal(wheel, throttle, quickturn, highgear); |
| 289 | dt_openloop.Update(); |
Brian Silverman | 2845c4c | 2013-03-16 19:54:08 -0700 | [diff] [blame] | 290 | if (control_loop_driving) { |
| 291 | dt_closedloop.SendMotors(output); |
| 292 | } else { |
| 293 | dt_openloop.SendMotors(output); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 294 | } |
| 295 | } |
| 296 | |
| 297 | } // namespace control_loops |
| 298 | } // namespace frc971 |