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; |
| 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) { |
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; |
| 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) { |
| 132 | wheelNonLinearity = 0.7; // used to be csvReader->TURN_NONLIN_HIGH |
| 133 | // 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); |
| 136 | wheel = sin(angular_range * _wheel) / sin(angular_range); |
| 137 | } else { |
| 138 | wheelNonLinearity = 0.4; // used to be csvReader->TURN_NONLIN_LOW |
| 139 | // 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); |
| 142 | wheel = sin(angular_range * _wheel) / sin(angular_range); |
| 143 | wheel = sin(angular_range * _wheel) / sin(angular_range); |
| 144 | } |
| 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 { |
| 157 | neg_inertia_scalar = 5; // used to be csvReader->NEG_INTERTIA_LOW_LESS |
| 158 | } |
| 159 | } |
| 160 | sensitivity = 1.24; // used to be csvReader->SENSE_LOW |
| 161 | |
| 162 | if (fabs(_throttle) > 0.1) { // used to be csvReader->SENSE_CUTTOFF |
| 163 | sensitivity = 1 - (1 - sensitivity) / fabs(_throttle); |
| 164 | } |
| 165 | } |
| 166 | double neg_inertia_power = neg_inertia * neg_inertia_scalar; |
| 167 | _neg_inertia_accumulator += neg_inertia_power; |
| 168 | |
| 169 | wheel = wheel + _neg_inertia_accumulator; |
| 170 | if (_neg_inertia_accumulator > 1) { |
| 171 | _neg_inertia_accumulator -= 1; |
| 172 | } else if (_neg_inertia_accumulator < -1) { |
| 173 | _neg_inertia_accumulator += 1; |
| 174 | } else { |
| 175 | _neg_inertia_accumulator = 0; |
| 176 | } |
| 177 | |
| 178 | linear_power = _throttle; |
| 179 | |
| 180 | const double quickstop_scalar = 6; |
| 181 | if (_quickturn) { |
| 182 | double qt_angular_power = wheel; |
| 183 | const double alpha = 0.1; |
| 184 | if (fabs(linear_power) < 0.2) { |
| 185 | if (qt_angular_power > 1) qt_angular_power = 1.0; |
| 186 | if (qt_angular_power < -1) qt_angular_power = -1.0; |
| 187 | } else { |
| 188 | qt_angular_power = 0.0; |
| 189 | } |
| 190 | quick_stop_accumulator = (1 - alpha) * quick_stop_accumulator + alpha * qt_angular_power * quickstop_scalar; |
| 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; |
| 200 | angular_power = fabs(_throttle) * wheel * sensitivity; |
| 201 | angular_power -= quick_stop_accumulator; |
| 202 | if (quick_stop_accumulator > 1) { |
| 203 | quick_stop_accumulator -= 1; |
| 204 | } else if (quick_stop_accumulator < -1) { |
| 205 | quick_stop_accumulator += 1; |
| 206 | } else { |
| 207 | quick_stop_accumulator = 0; |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | _right_pwm = _left_pwm = linear_power; |
| 212 | _left_pwm += angular_power; |
| 213 | _right_pwm -= angular_power; |
| 214 | |
| 215 | if (_left_pwm > 1.0) { |
| 216 | _right_pwm -= overPower*(_left_pwm - 1.0); |
| 217 | _left_pwm = 1.0; |
| 218 | } else if (_right_pwm > 1.0) { |
| 219 | _left_pwm -= overPower*(_right_pwm - 1.0); |
| 220 | _right_pwm = 1.0; |
| 221 | } else if (_left_pwm < -1.0) { |
| 222 | _right_pwm += overPower*(-1.0 - _left_pwm); |
| 223 | _left_pwm = -1.0; |
| 224 | } else if (_right_pwm < -1.0) { |
| 225 | _left_pwm += overPower*(-1.0 - _right_pwm); |
| 226 | _right_pwm = -1.0; |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | void SendMotors(Drivetrain::Output *output) { |
| 231 | LOG(DEBUG, "left pwm: %f right pwm: %f wheel: %f throttle: %f, qa %f\n", |
| 232 | _left_pwm, _right_pwm, _wheel, _throttle, quick_stop_accumulator); |
brians | 8ad7405 | 2013-03-16 21:04:51 +0000 | [diff] [blame] | 233 | if (output) { |
| 234 | output->left_voltage = _left_pwm * 12.0; |
| 235 | output->right_voltage = _right_pwm * 12.0; |
| 236 | } |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 237 | if (_highgear) { |
| 238 | shifters.MakeWithBuilder().set(false).Send(); |
| 239 | } else { |
| 240 | shifters.MakeWithBuilder().set(true).Send(); |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | private: |
| 245 | double _old_wheel; |
| 246 | double _wheel; |
| 247 | double _throttle; |
| 248 | bool _quickturn; |
| 249 | bool _highgear; |
| 250 | double _neg_inertia_accumulator; |
| 251 | double _left_pwm; |
| 252 | double _right_pwm; |
| 253 | double quick_stop_accumulator; |
| 254 | }; |
| 255 | |
| 256 | void DrivetrainLoop::RunIteration(const Drivetrain::Goal *goal, |
| 257 | const Drivetrain::Position *position, |
| 258 | Drivetrain::Output *output, |
| 259 | Drivetrain::Status * /*status*/) { |
| 260 | // TODO(aschuh): These should be members of the class. |
| 261 | static DrivetrainMotorsSS dt_closedloop; |
| 262 | static DrivetrainMotorsOL dt_openloop; |
| 263 | |
| 264 | bool bad_pos = false; |
| 265 | if (position == NULL) { |
James Kuszmaul | 3f35474 | 2013-03-10 17:27:56 -0700 | [diff] [blame] | 266 | LOG(WARNING, "no position\n"); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 267 | bad_pos = true; |
| 268 | } |
| 269 | |
James Kuszmaul | 3f35474 | 2013-03-10 17:27:56 -0700 | [diff] [blame] | 270 | bool bad_output = false; |
| 271 | if (output == NULL) { |
| 272 | LOG(WARNING, "no output\n"); |
| 273 | bad_output = true; |
| 274 | } |
| 275 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 276 | double wheel = goal->steering; |
| 277 | double throttle = goal->throttle; |
| 278 | bool quickturn = goal->quickturn; |
| 279 | bool highgear = goal->highgear; |
| 280 | |
| 281 | bool control_loop_driving = goal->control_loop_driving; |
| 282 | double left_goal = goal->left_goal; |
| 283 | double right_goal = goal->right_goal; |
| 284 | |
| 285 | dt_closedloop.SetGoal(left_goal, 0.0, right_goal, 0.0); |
| 286 | if (!bad_pos) { |
| 287 | const double left_encoder = position->left_encoder; |
| 288 | const double right_encoder = position->right_encoder; |
| 289 | if (gyro.FetchLatest()) { |
| 290 | dt_closedloop.SetPosition(left_encoder, right_encoder, |
| 291 | gyro->angle, control_loop_driving); |
| 292 | } else { |
| 293 | dt_closedloop.SetRawPosition(left_encoder, right_encoder); |
| 294 | } |
| 295 | } |
James Kuszmaul | 3f35474 | 2013-03-10 17:27:56 -0700 | [diff] [blame] | 296 | dt_closedloop.Update(!bad_pos, bad_pos || bad_output); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 297 | dt_openloop.SetGoal(wheel, throttle, quickturn, highgear); |
| 298 | dt_openloop.Update(); |
James Kuszmaul | 3f35474 | 2013-03-10 17:27:56 -0700 | [diff] [blame] | 299 | if (!bad_output) { |
| 300 | if (control_loop_driving) { |
| 301 | dt_closedloop.SendMotors(output); |
| 302 | } else { |
| 303 | dt_openloop.SendMotors(output); |
| 304 | } |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 305 | } |
| 306 | } |
| 307 | |
| 308 | } // namespace control_loops |
| 309 | } // namespace frc971 |