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> |
Austin Schuh | 4352ac6 | 2013-03-19 06:23:16 +0000 | [diff] [blame] | 6 | #include <memory> |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 7 | |
| 8 | #include "aos/aos_core.h" |
| 9 | #include "aos/common/logging/logging.h" |
| 10 | #include "aos/common/queue.h" |
James Kuszmaul | f254c1a | 2013-03-10 16:31:26 -0700 | [diff] [blame] | 11 | #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" |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 14 | #include "frc971/queues/GyroAngle.q.h" |
| 15 | #include "frc971/queues/Piston.q.h" |
| 16 | |
| 17 | using frc971::sensors::gyro; |
| 18 | |
| 19 | namespace frc971 { |
| 20 | namespace control_loops { |
| 21 | |
| 22 | // Width of the robot. |
| 23 | const double width = 22.0 / 100.0 * 2.54; |
| 24 | |
Austin Schuh | 4352ac6 | 2013-03-19 06:23:16 +0000 | [diff] [blame] | 25 | class DrivetrainMotorsSS { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 26 | public: |
Austin Schuh | 4352ac6 | 2013-03-19 06:23:16 +0000 | [diff] [blame] | 27 | DrivetrainMotorsSS () |
| 28 | : loop_(new StateFeedbackLoop<4, 2, 2>(MakeDrivetrainLoop())) { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 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; |
Austin Schuh | 4352ac6 | 2013-03-19 06:23:16 +0000 | [diff] [blame] | 35 | _control_loop_driving = false; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 36 | } |
| 37 | void SetGoal(double left, double left_velocity, double right, double right_velocity) { |
| 38 | _left_goal = left; |
| 39 | _right_goal = right; |
Austin Schuh | 4352ac6 | 2013-03-19 06:23:16 +0000 | [diff] [blame] | 40 | loop_->R << left, left_velocity, right, right_velocity; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 41 | } |
| 42 | void SetRawPosition(double left, double right) { |
| 43 | _raw_right = right; |
| 44 | _raw_left = left; |
Austin Schuh | 4352ac6 | 2013-03-19 06:23:16 +0000 | [diff] [blame] | 45 | loop_->Y << left, right; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 46 | } |
Austin Schuh | 4352ac6 | 2013-03-19 06:23:16 +0000 | [diff] [blame] | 47 | void SetPosition( |
| 48 | double left, double right, double gyro, bool control_loop_driving) { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 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; |
Austin Schuh | 4352ac6 | 2013-03-19 06:23:16 +0000 | [diff] [blame] | 52 | // TODO(aschuh): Add in the gyro. |
| 53 | _integral_offset = 0.0; |
| 54 | _offset = 0.0; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 55 | _gyro = gyro; |
Austin Schuh | 4352ac6 | 2013-03-19 06:23:16 +0000 | [diff] [blame] | 56 | _control_loop_driving = control_loop_driving; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 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 | } |
Austin Schuh | 4352ac6 | 2013-03-19 06:23:16 +0000 | [diff] [blame] | 60 | |
| 61 | void Update(bool update_observer, bool stop_motors) { |
| 62 | loop_->Update(update_observer, stop_motors); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 63 | } |
| 64 | |
Austin Schuh | 4352ac6 | 2013-03-19 06:23:16 +0000 | [diff] [blame] | 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); |
brians | 8ad7405 | 2013-03-16 21:04:51 +0000 | [diff] [blame] | 69 | } |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 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); |
Austin Schuh | 4352ac6 | 2013-03-19 06:23:16 +0000 | [diff] [blame] | 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)); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | private: |
Austin Schuh | 4352ac6 | 2013-03-19 06:23:16 +0000 | [diff] [blame] | 78 | ::std::unique_ptr<StateFeedbackLoop<4, 2, 2>> loop_; |
| 79 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 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; |
Austin Schuh | 4352ac6 | 2013-03-19 06:23:16 +0000 | [diff] [blame] | 87 | bool _control_loop_driving; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 88 | }; |
| 89 | |
| 90 | class DrivetrainMotorsOL { |
| 91 | public: |
| 92 | DrivetrainMotorsOL() { |
| 93 | _old_wheel = 0.0; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 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) { |
Brian Silverman | 77a7600 | 2013-03-16 20:09:00 -0700 | [diff] [blame] | 122 | wheelNonLinearity = 0.1; // used to be csvReader->TURN_NONLIN_HIGH |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 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); |
Brian Silverman | 77a7600 | 2013-03-16 20:09:00 -0700 | [diff] [blame] | 126 | wheel = sin(angular_range * wheel) / sin(angular_range); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 127 | } else { |
Brian Silverman | df43ec2 | 2013-03-16 23:48:29 -0700 | [diff] [blame] | 128 | wheelNonLinearity = 0.2; // used to be csvReader->TURN_NONLIN_LOW |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 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); |
Brian Silverman | 77a7600 | 2013-03-16 20:09:00 -0700 | [diff] [blame] | 132 | wheel = sin(angular_range * wheel) / sin(angular_range); |
| 133 | wheel = sin(angular_range * wheel) / sin(angular_range); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 134 | } |
| 135 | |
Brian Silverman | df43ec2 | 2013-03-16 23:48:29 -0700 | [diff] [blame] | 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 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 144 | double neg_inertia_scalar; |
| 145 | if (_highgear) { |
Brian Silverman | df43ec2 | 2013-03-16 23:48:29 -0700 | [diff] [blame] | 146 | neg_inertia_scalar = 8.0; // used to be csvReader->NEG_INTERTIA_HIGH |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 147 | sensitivity = 1.22; // used to be csvReader->SENSE_HIGH |
| 148 | } else { |
| 149 | if (wheel * neg_inertia > 0) { |
Brian Silverman | df43ec2 | 2013-03-16 23:48:29 -0700 | [diff] [blame] | 150 | neg_inertia_scalar = 5; // used to be csvReader->NEG_INERTIA_LOW_MORE |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 151 | } else { |
Brian Silverman | df43ec2 | 2013-03-16 23:48:29 -0700 | [diff] [blame] | 152 | if (::std::abs(wheel) > 0.65) { |
| 153 | neg_inertia_scalar = 5; // used to be csvReader->NEG_INTERTIA_LOW_LESS_EXT |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 154 | } else { |
Brian Silverman | df43ec2 | 2013-03-16 23:48:29 -0700 | [diff] [blame] | 155 | neg_inertia_scalar = 5; // used to be csvReader->NEG_INTERTIA_LOW_LESS |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | sensitivity = 1.24; // used to be csvReader->SENSE_LOW |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 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 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 174 | if (_quickturn) { |
| 175 | double qt_angular_power = wheel; |
Brian Silverman | df43ec2 | 2013-03-16 23:48:29 -0700 | [diff] [blame] | 176 | if (::std::abs(linear_power) < 0.2) { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 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 | } |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 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; |
Brian Silverman | df43ec2 | 2013-03-16 23:48:29 -0700 | [diff] [blame] | 191 | angular_power = ::std::abs(_throttle) * wheel * sensitivity; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 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) { |
Brian Silverman | df43ec2 | 2013-03-16 23:48:29 -0700 | [diff] [blame] | 214 | LOG(DEBUG, "left pwm: %f right pwm: %f wheel: %f throttle: %f\n", |
| 215 | _left_pwm, _right_pwm, _wheel, _throttle); |
brians | 8ad7405 | 2013-03-16 21:04:51 +0000 | [diff] [blame] | 216 | if (output) { |
| 217 | output->left_voltage = _left_pwm * 12.0; |
| 218 | output->right_voltage = _right_pwm * 12.0; |
| 219 | } |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 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; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 236 | }; |
| 237 | |
| 238 | void 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 Kuszmaul | 3f35474 | 2013-03-10 17:27:56 -0700 | [diff] [blame] | 248 | LOG(WARNING, "no position\n"); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 249 | bad_pos = true; |
| 250 | } |
| 251 | |
James Kuszmaul | 3f35474 | 2013-03-10 17:27:56 -0700 | [diff] [blame] | 252 | bool bad_output = false; |
| 253 | if (output == NULL) { |
| 254 | LOG(WARNING, "no output\n"); |
| 255 | bad_output = true; |
| 256 | } |
| 257 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 258 | 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 Schuh | 4352ac6 | 2013-03-19 06:23:16 +0000 | [diff] [blame] | 267 | dt_closedloop.SetGoal(left_goal, goal->left_velocity_goal, |
| 268 | right_goal, goal->right_velocity_goal); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 269 | 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 Schuh | 4352ac6 | 2013-03-19 06:23:16 +0000 | [diff] [blame] | 279 | dt_closedloop.Update(position, output == NULL); |
| 280 | //dt_closedloop.PrintMotors(); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 281 | dt_openloop.SetGoal(wheel, throttle, quickturn, highgear); |
| 282 | dt_openloop.Update(); |
Brian Silverman | 2845c4c | 2013-03-16 19:54:08 -0700 | [diff] [blame] | 283 | if (control_loop_driving) { |
| 284 | dt_closedloop.SendMotors(output); |
| 285 | } else { |
| 286 | dt_openloop.SendMotors(output); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 287 | } |
| 288 | } |
| 289 | |
| 290 | } // namespace control_loops |
| 291 | } // namespace frc971 |