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> |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 7 | #include "Eigen/Dense" |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 8 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 9 | #include "aos/common/logging/logging.h" |
| 10 | #include "aos/common/queue.h" |
Brian | a6553ed | 2014-04-02 21:26:46 -0700 | [diff] [blame^] | 11 | #include "aos/common/controls/polytope.h" |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 12 | #include "aos/common/commonmath.h" |
Brian Silverman | 61e41fd | 2014-02-16 19:08:50 -0800 | [diff] [blame] | 13 | #include "aos/common/logging/queue_logging.h" |
Brian Silverman | fd5e2a3 | 2014-02-22 20:02:39 -0800 | [diff] [blame] | 14 | #include "aos/common/logging/matrix_logging.h" |
Brian Silverman | 61e41fd | 2014-02-16 19:08:50 -0800 | [diff] [blame] | 15 | |
James Kuszmaul | f254c1a | 2013-03-10 16:31:26 -0700 | [diff] [blame] | 16 | #include "frc971/control_loops/state_feedback_loop.h" |
James Kuszmaul | fb0e0ae | 2014-03-25 07:04:47 -0700 | [diff] [blame] | 17 | #include "frc971/control_loops/coerce_goal.h" |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 18 | #include "frc971/control_loops/drivetrain/polydrivetrain_cim_plant.h" |
James Kuszmaul | f254c1a | 2013-03-10 16:31:26 -0700 | [diff] [blame] | 19 | #include "frc971/control_loops/drivetrain/drivetrain.q.h" |
Brian Silverman | 6bf0d3c | 2014-03-08 12:52:54 -0800 | [diff] [blame] | 20 | #include "frc971/queues/other_sensors.q.h" |
Brian Silverman | 1a6590d | 2013-11-04 14:46:46 -0800 | [diff] [blame] | 21 | #include "frc971/constants.h" |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 22 | |
Brian Silverman | 6bf0d3c | 2014-03-08 12:52:54 -0800 | [diff] [blame] | 23 | using frc971::sensors::gyro_reading; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 24 | |
| 25 | namespace frc971 { |
| 26 | namespace control_loops { |
| 27 | |
| 28 | // Width of the robot. |
| 29 | const double width = 22.0 / 100.0 * 2.54; |
| 30 | |
Austin Schuh | 4352ac6 | 2013-03-19 06:23:16 +0000 | [diff] [blame] | 31 | class DrivetrainMotorsSS { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 32 | public: |
Brian Silverman | 2c590c3 | 2013-11-04 18:08:54 -0800 | [diff] [blame] | 33 | DrivetrainMotorsSS() |
| 34 | : loop_(new StateFeedbackLoop<4, 2, 2>( |
| 35 | constants::GetValues().make_drivetrain_loop())) { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 36 | _offset = 0; |
| 37 | _integral_offset = 0; |
| 38 | _left_goal = 0.0; |
| 39 | _right_goal = 0.0; |
| 40 | _raw_left = 0.0; |
| 41 | _raw_right = 0.0; |
Austin Schuh | 4352ac6 | 2013-03-19 06:23:16 +0000 | [diff] [blame] | 42 | _control_loop_driving = false; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 43 | } |
| 44 | void SetGoal(double left, double left_velocity, double right, double right_velocity) { |
| 45 | _left_goal = left; |
| 46 | _right_goal = right; |
Austin Schuh | 4352ac6 | 2013-03-19 06:23:16 +0000 | [diff] [blame] | 47 | loop_->R << left, left_velocity, right, right_velocity; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 48 | } |
| 49 | void SetRawPosition(double left, double right) { |
| 50 | _raw_right = right; |
| 51 | _raw_left = left; |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 52 | Eigen::Matrix<double, 2, 1> Y; |
| 53 | Y << left, right; |
| 54 | loop_->Correct(Y); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 55 | } |
Austin Schuh | 4352ac6 | 2013-03-19 06:23:16 +0000 | [diff] [blame] | 56 | void SetPosition( |
| 57 | double left, double right, double gyro, bool control_loop_driving) { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 58 | // Decay the offset quickly because this gyro is great. |
| 59 | _offset = (0.25) * (right - left - gyro * width) / 2.0 + 0.75 * _offset; |
Brian Silverman | de8fd55 | 2013-11-03 15:53:42 -0800 | [diff] [blame] | 60 | //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] | 61 | // TODO(aschuh): Add in the gyro. |
| 62 | _integral_offset = 0.0; |
| 63 | _offset = 0.0; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 64 | _gyro = gyro; |
Austin Schuh | 4352ac6 | 2013-03-19 06:23:16 +0000 | [diff] [blame] | 65 | _control_loop_driving = control_loop_driving; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 66 | SetRawPosition(left, right); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 67 | } |
Austin Schuh | 4352ac6 | 2013-03-19 06:23:16 +0000 | [diff] [blame] | 68 | |
Ben Fredrickson | 890c3fe | 2014-03-02 00:15:16 +0000 | [diff] [blame] | 69 | void SetExternalMotors(double left_voltage, double right_voltage) { |
| 70 | loop_->U << left_voltage, right_voltage; |
| 71 | } |
| 72 | |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 73 | void Update(bool stop_motors) { |
Ben Fredrickson | 890c3fe | 2014-03-02 00:15:16 +0000 | [diff] [blame] | 74 | if (_control_loop_driving) { |
| 75 | loop_->Update(stop_motors); |
| 76 | } else { |
| 77 | if (stop_motors) { |
| 78 | loop_->U.setZero(); |
| 79 | loop_->U_uncapped.setZero(); |
| 80 | } |
| 81 | loop_->UpdateObserver(); |
| 82 | } |
Brian Silverman | 3146b64 | 2014-03-20 14:52:46 -0700 | [diff] [blame] | 83 | ::Eigen::Matrix<double, 4, 1> E = loop_->R - loop_->X_hat; |
| 84 | LOG_MATRIX(DEBUG, "E", E); |
Ben Fredrickson | 890c3fe | 2014-03-02 00:15:16 +0000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | double GetEstimatedRobotSpeed() { |
| 88 | // lets just call the average of left and right velocities close enough |
| 89 | return (loop_->X_hat(1, 0) + loop_->X_hat(3, 0)) / 2; |
| 90 | } |
| 91 | |
| 92 | double GetEstimatedLeftEncoder() { |
| 93 | // lets just call the average of left and right velocities close enough |
| 94 | return loop_->X_hat(0, 0); |
| 95 | } |
| 96 | |
| 97 | double GetEstimatedRightEncoder() { |
| 98 | return loop_->X_hat(2, 0); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Austin Schuh | 4352ac6 | 2013-03-19 06:23:16 +0000 | [diff] [blame] | 101 | void SendMotors(Drivetrain::Output *output) { |
| 102 | if (output) { |
| 103 | output->left_voltage = loop_->U(0, 0); |
| 104 | output->right_voltage = loop_->U(1, 0); |
brians | 8ad7405 | 2013-03-16 21:04:51 +0000 | [diff] [blame] | 105 | } |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 106 | } |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 107 | |
| 108 | private: |
Austin Schuh | 4352ac6 | 2013-03-19 06:23:16 +0000 | [diff] [blame] | 109 | ::std::unique_ptr<StateFeedbackLoop<4, 2, 2>> loop_; |
| 110 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 111 | double _integral_offset; |
| 112 | double _offset; |
| 113 | double _gyro; |
| 114 | double _left_goal; |
| 115 | double _right_goal; |
| 116 | double _raw_left; |
| 117 | double _raw_right; |
Austin Schuh | 4352ac6 | 2013-03-19 06:23:16 +0000 | [diff] [blame] | 118 | bool _control_loop_driving; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 119 | }; |
| 120 | |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 121 | class PolyDrivetrain { |
| 122 | public: |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 123 | |
| 124 | enum Gear { |
| 125 | HIGH, |
| 126 | LOW, |
| 127 | SHIFTING_UP, |
| 128 | SHIFTING_DOWN |
| 129 | }; |
| 130 | // Stall Torque in N m |
| 131 | static constexpr double kStallTorque = 2.42; |
| 132 | // Stall Current in Amps |
| 133 | static constexpr double kStallCurrent = 133; |
| 134 | // Free Speed in RPM. Used number from last year. |
| 135 | static constexpr double kFreeSpeed = 4650.0; |
| 136 | // Free Current in Amps |
| 137 | static constexpr double kFreeCurrent = 2.7; |
| 138 | // Moment of inertia of the drivetrain in kg m^2 |
| 139 | // Just borrowed from last year. |
| 140 | static constexpr double J = 6.4; |
| 141 | // Mass of the robot, in kg. |
| 142 | static constexpr double m = 68; |
| 143 | // Radius of the robot, in meters (from last year). |
| 144 | static constexpr double rb = 0.617998644 / 2.0; |
Brian Silverman | 1a6590d | 2013-11-04 14:46:46 -0800 | [diff] [blame] | 145 | static constexpr double kWheelRadius = 0.04445; |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 146 | // Resistance of the motor, divided by the number of motors. |
| 147 | static constexpr double kR = (12.0 / kStallCurrent / 4 + 0.03) / (0.93 * 0.93); |
| 148 | // Motor velocity constant |
| 149 | static constexpr double Kv = |
| 150 | ((kFreeSpeed / 60.0 * 2.0 * M_PI) / (12.0 - kR * kFreeCurrent)); |
| 151 | // Torque constant |
| 152 | static constexpr double Kt = kStallTorque / kStallCurrent; |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 153 | |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 154 | PolyDrivetrain() |
| 155 | : U_Poly_((Eigen::Matrix<double, 4, 2>() << /*[[*/ 1, 0 /*]*/, |
| 156 | /*[*/ -1, 0 /*]*/, |
| 157 | /*[*/ 0, 1 /*]*/, |
| 158 | /*[*/ 0, -1 /*]]*/).finished(), |
| 159 | (Eigen::Matrix<double, 4, 1>() << /*[[*/ 12 /*]*/, |
| 160 | /*[*/ 12 /*]*/, |
| 161 | /*[*/ 12 /*]*/, |
| 162 | /*[*/ 12 /*]]*/).finished()), |
Brian Silverman | 2c590c3 | 2013-11-04 18:08:54 -0800 | [diff] [blame] | 163 | loop_(new StateFeedbackLoop<2, 2, 2>( |
| 164 | constants::GetValues().make_v_drivetrain_loop())), |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 165 | ttrust_(1.1), |
| 166 | wheel_(0.0), |
| 167 | throttle_(0.0), |
| 168 | quickturn_(false), |
| 169 | stale_count_(0), |
| 170 | position_time_delta_(0.01), |
| 171 | left_gear_(LOW), |
Brian Silverman | de8fd55 | 2013-11-03 15:53:42 -0800 | [diff] [blame] | 172 | right_gear_(LOW), |
| 173 | counter_(0) { |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 174 | |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 175 | last_position_.Zero(); |
| 176 | position_.Zero(); |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 177 | } |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 178 | static bool IsInGear(Gear gear) { return gear == LOW || gear == HIGH; } |
| 179 | |
Austin Schuh | 78d5546 | 2014-02-23 01:39:30 -0800 | [diff] [blame] | 180 | static double MotorSpeed(const constants::ShifterHallEffect &hall_effect, |
| 181 | double shifter_position, double velocity) { |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 182 | // TODO(austin): G_high, G_low and kWheelRadius |
Austin Schuh | 78d5546 | 2014-02-23 01:39:30 -0800 | [diff] [blame] | 183 | const double avg_hall_effect = |
| 184 | (hall_effect.clear_high + hall_effect.clear_low) / 2.0; |
| 185 | |
| 186 | if (shifter_position > avg_hall_effect) { |
Brian Silverman | 1a6590d | 2013-11-04 14:46:46 -0800 | [diff] [blame] | 187 | return velocity / constants::GetValues().high_gear_ratio / kWheelRadius; |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 188 | } else { |
Brian Silverman | 1a6590d | 2013-11-04 14:46:46 -0800 | [diff] [blame] | 189 | return velocity / constants::GetValues().low_gear_ratio / kWheelRadius; |
| 190 | } |
| 191 | } |
| 192 | |
Austin Schuh | 78d5546 | 2014-02-23 01:39:30 -0800 | [diff] [blame] | 193 | Gear ComputeGear(const constants::ShifterHallEffect &hall_effect, |
| 194 | double velocity, Gear current) { |
| 195 | const double low_omega = MotorSpeed(hall_effect, 0.0, ::std::abs(velocity)); |
| 196 | const double high_omega = |
| 197 | MotorSpeed(hall_effect, 1.0, ::std::abs(velocity)); |
Brian Silverman | 1a6590d | 2013-11-04 14:46:46 -0800 | [diff] [blame] | 198 | |
Brian Silverman | 1a6590d | 2013-11-04 14:46:46 -0800 | [diff] [blame] | 199 | double high_torque = ((12.0 - high_omega / Kv) * Kt / kR); |
| 200 | double low_torque = ((12.0 - low_omega / Kv) * Kt / kR); |
| 201 | double high_power = high_torque * high_omega; |
| 202 | double low_power = low_torque * low_omega; |
Brian Silverman | 55a930b | 2013-11-04 20:59:00 -0800 | [diff] [blame] | 203 | |
| 204 | // TODO(aschuh): Do this right! |
| 205 | if ((current == HIGH || high_power > low_power + 160) && |
| 206 | ::std::abs(velocity) > 0.14) { |
Brian Silverman | 1a6590d | 2013-11-04 14:46:46 -0800 | [diff] [blame] | 207 | return HIGH; |
| 208 | } else { |
| 209 | return LOW; |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 210 | } |
| 211 | } |
| 212 | |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 213 | void SetGoal(double wheel, double throttle, bool quickturn, bool highgear) { |
Brian Silverman | de8fd55 | 2013-11-03 15:53:42 -0800 | [diff] [blame] | 214 | const double kWheelNonLinearity = 0.3; |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 215 | // Apply a sin function that's scaled to make it feel better. |
| 216 | const double angular_range = M_PI_2 * kWheelNonLinearity; |
| 217 | wheel_ = sin(angular_range * wheel) / sin(angular_range); |
| 218 | wheel_ = sin(angular_range * wheel_) / sin(angular_range); |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 219 | quickturn_ = quickturn; |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 220 | |
Brian Silverman | 1a6590d | 2013-11-04 14:46:46 -0800 | [diff] [blame] | 221 | static const double kThrottleDeadband = 0.05; |
| 222 | if (::std::abs(throttle) < kThrottleDeadband) { |
| 223 | throttle_ = 0; |
| 224 | } else { |
| 225 | throttle_ = copysign((::std::abs(throttle) - kThrottleDeadband) / |
| 226 | (1.0 - kThrottleDeadband), throttle); |
| 227 | } |
| 228 | |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 229 | // TODO(austin): Fix the upshift logic to include states. |
Brian Silverman | 1a6590d | 2013-11-04 14:46:46 -0800 | [diff] [blame] | 230 | Gear requested_gear; |
Brian Silverman | 55a930b | 2013-11-04 20:59:00 -0800 | [diff] [blame] | 231 | if (false) { |
Austin Schuh | 78d5546 | 2014-02-23 01:39:30 -0800 | [diff] [blame] | 232 | const auto &values = constants::GetValues(); |
Brian Silverman | 1a6590d | 2013-11-04 14:46:46 -0800 | [diff] [blame] | 233 | const double current_left_velocity = |
| 234 | (position_.left_encoder - last_position_.left_encoder) / |
| 235 | position_time_delta_; |
| 236 | const double current_right_velocity = |
| 237 | (position_.right_encoder - last_position_.right_encoder) / |
| 238 | position_time_delta_; |
| 239 | |
Austin Schuh | 78d5546 | 2014-02-23 01:39:30 -0800 | [diff] [blame] | 240 | Gear left_requested = |
| 241 | ComputeGear(values.left_drive, current_left_velocity, left_gear_); |
| 242 | Gear right_requested = |
| 243 | ComputeGear(values.right_drive, current_right_velocity, right_gear_); |
Brian Silverman | 1a6590d | 2013-11-04 14:46:46 -0800 | [diff] [blame] | 244 | requested_gear = |
| 245 | (left_requested == HIGH || right_requested == HIGH) ? HIGH : LOW; |
| 246 | } else { |
| 247 | requested_gear = highgear ? HIGH : LOW; |
| 248 | } |
| 249 | |
| 250 | const Gear shift_up = |
| 251 | constants::GetValues().clutch_transmission ? HIGH : SHIFTING_UP; |
| 252 | const Gear shift_down = |
| 253 | constants::GetValues().clutch_transmission ? LOW : SHIFTING_DOWN; |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 254 | |
| 255 | if (left_gear_ != requested_gear) { |
| 256 | if (IsInGear(left_gear_)) { |
| 257 | if (requested_gear == HIGH) { |
Brian Silverman | 1a6590d | 2013-11-04 14:46:46 -0800 | [diff] [blame] | 258 | left_gear_ = shift_up; |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 259 | } else { |
Brian Silverman | 1a6590d | 2013-11-04 14:46:46 -0800 | [diff] [blame] | 260 | left_gear_ = shift_down; |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 261 | } |
Brian Silverman | 55a930b | 2013-11-04 20:59:00 -0800 | [diff] [blame] | 262 | } else { |
| 263 | if (requested_gear == HIGH && left_gear_ == SHIFTING_DOWN) { |
| 264 | left_gear_ = SHIFTING_UP; |
| 265 | } else if (requested_gear == LOW && left_gear_ == SHIFTING_UP) { |
| 266 | left_gear_ = SHIFTING_DOWN; |
| 267 | } |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 268 | } |
| 269 | } |
| 270 | if (right_gear_ != requested_gear) { |
| 271 | if (IsInGear(right_gear_)) { |
| 272 | if (requested_gear == HIGH) { |
Brian Silverman | 1a6590d | 2013-11-04 14:46:46 -0800 | [diff] [blame] | 273 | right_gear_ = shift_up; |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 274 | } else { |
Brian Silverman | 1a6590d | 2013-11-04 14:46:46 -0800 | [diff] [blame] | 275 | right_gear_ = shift_down; |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 276 | } |
Brian Silverman | 55a930b | 2013-11-04 20:59:00 -0800 | [diff] [blame] | 277 | } else { |
| 278 | if (requested_gear == HIGH && right_gear_ == SHIFTING_DOWN) { |
| 279 | right_gear_ = SHIFTING_UP; |
| 280 | } else if (requested_gear == LOW && right_gear_ == SHIFTING_UP) { |
| 281 | right_gear_ = SHIFTING_DOWN; |
| 282 | } |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 283 | } |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 284 | } |
| 285 | } |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 286 | void SetPosition(const Drivetrain::Position *position) { |
Austin Schuh | 78d5546 | 2014-02-23 01:39:30 -0800 | [diff] [blame] | 287 | const auto &values = constants::GetValues(); |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 288 | if (position == NULL) { |
| 289 | ++stale_count_; |
| 290 | } else { |
| 291 | last_position_ = position_; |
| 292 | position_ = *position; |
| 293 | position_time_delta_ = (stale_count_ + 1) * 0.01; |
| 294 | stale_count_ = 0; |
| 295 | } |
| 296 | |
| 297 | if (position) { |
Brian Silverman | 61e41fd | 2014-02-16 19:08:50 -0800 | [diff] [blame] | 298 | GearLogging gear_logging; |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 299 | // Switch to the correct controller. |
Austin Schuh | 78d5546 | 2014-02-23 01:39:30 -0800 | [diff] [blame] | 300 | const double left_middle_shifter_position = |
| 301 | (values.left_drive.clear_high + values.left_drive.clear_low) / 2.0; |
| 302 | const double right_middle_shifter_position = |
| 303 | (values.right_drive.clear_high + values.right_drive.clear_low) / 2.0; |
| 304 | |
| 305 | if (position->left_shifter_position < left_middle_shifter_position) { |
| 306 | if (position->right_shifter_position < right_middle_shifter_position || |
| 307 | right_gear_ == LOW) { |
Brian Silverman | 61e41fd | 2014-02-16 19:08:50 -0800 | [diff] [blame] | 308 | gear_logging.left_loop_high = false; |
| 309 | gear_logging.right_loop_high = false; |
| 310 | loop_->set_controller_index(gear_logging.controller_index = 0); |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 311 | } else { |
Brian Silverman | 61e41fd | 2014-02-16 19:08:50 -0800 | [diff] [blame] | 312 | gear_logging.left_loop_high = false; |
| 313 | gear_logging.right_loop_high = true; |
| 314 | loop_->set_controller_index(gear_logging.controller_index = 1); |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 315 | } |
| 316 | } else { |
Austin Schuh | 78d5546 | 2014-02-23 01:39:30 -0800 | [diff] [blame] | 317 | if (position->right_shifter_position < right_middle_shifter_position || |
| 318 | left_gear_ == LOW) { |
Brian Silverman | 61e41fd | 2014-02-16 19:08:50 -0800 | [diff] [blame] | 319 | gear_logging.left_loop_high = true; |
| 320 | gear_logging.right_loop_high = false; |
| 321 | loop_->set_controller_index(gear_logging.controller_index = 2); |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 322 | } else { |
Brian Silverman | 61e41fd | 2014-02-16 19:08:50 -0800 | [diff] [blame] | 323 | gear_logging.left_loop_high = true; |
| 324 | gear_logging.right_loop_high = true; |
| 325 | loop_->set_controller_index(gear_logging.controller_index = 3); |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 326 | } |
| 327 | } |
Brian Silverman | 61e41fd | 2014-02-16 19:08:50 -0800 | [diff] [blame] | 328 | |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 329 | // TODO(austin): Constants. |
Austin Schuh | 78d5546 | 2014-02-23 01:39:30 -0800 | [diff] [blame] | 330 | if (position->left_shifter_position > values.left_drive.clear_high && left_gear_ == SHIFTING_UP) { |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 331 | left_gear_ = HIGH; |
| 332 | } |
Austin Schuh | 78d5546 | 2014-02-23 01:39:30 -0800 | [diff] [blame] | 333 | if (position->left_shifter_position < values.left_drive.clear_low && left_gear_ == SHIFTING_DOWN) { |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 334 | left_gear_ = LOW; |
| 335 | } |
Austin Schuh | 78d5546 | 2014-02-23 01:39:30 -0800 | [diff] [blame] | 336 | if (position->right_shifter_position > values.right_drive.clear_high && right_gear_ == SHIFTING_UP) { |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 337 | right_gear_ = HIGH; |
| 338 | } |
Austin Schuh | 78d5546 | 2014-02-23 01:39:30 -0800 | [diff] [blame] | 339 | if (position->right_shifter_position < values.right_drive.clear_low && right_gear_ == SHIFTING_DOWN) { |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 340 | right_gear_ = LOW; |
| 341 | } |
Brian Silverman | 61e41fd | 2014-02-16 19:08:50 -0800 | [diff] [blame] | 342 | |
| 343 | gear_logging.left_state = left_gear_; |
| 344 | gear_logging.right_state = right_gear_; |
| 345 | LOG_STRUCT(DEBUG, "state", gear_logging); |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 346 | } |
| 347 | } |
| 348 | |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 349 | double FilterVelocity(double throttle) { |
| 350 | const Eigen::Matrix<double, 2, 2> FF = |
| 351 | loop_->B().inverse() * |
| 352 | (Eigen::Matrix<double, 2, 2>::Identity() - loop_->A()); |
| 353 | |
| 354 | constexpr int kHighGearController = 3; |
| 355 | const Eigen::Matrix<double, 2, 2> FF_high = |
| 356 | loop_->controller(kHighGearController).plant.B.inverse() * |
| 357 | (Eigen::Matrix<double, 2, 2>::Identity() - |
| 358 | loop_->controller(kHighGearController).plant.A); |
| 359 | |
| 360 | ::Eigen::Matrix<double, 1, 2> FF_sum = FF.colwise().sum(); |
| 361 | int min_FF_sum_index; |
| 362 | const double min_FF_sum = FF_sum.minCoeff(&min_FF_sum_index); |
| 363 | const double min_K_sum = loop_->K().col(min_FF_sum_index).sum(); |
| 364 | const double high_min_FF_sum = FF_high.col(0).sum(); |
| 365 | |
| 366 | const double adjusted_ff_voltage = ::aos::Clip( |
| 367 | throttle * 12.0 * min_FF_sum / high_min_FF_sum, -12.0, 12.0); |
| 368 | return ((adjusted_ff_voltage + |
| 369 | ttrust_ * min_K_sum * (loop_->X_hat(0, 0) + loop_->X_hat(1, 0)) / |
| 370 | 2.0) / |
| 371 | (ttrust_ * min_K_sum + min_FF_sum)); |
| 372 | } |
| 373 | |
Brian Silverman | 718b1d7 | 2013-10-28 16:22:45 -0700 | [diff] [blame] | 374 | double MaxVelocity() { |
| 375 | const Eigen::Matrix<double, 2, 2> FF = |
| 376 | loop_->B().inverse() * |
| 377 | (Eigen::Matrix<double, 2, 2>::Identity() - loop_->A()); |
| 378 | |
| 379 | constexpr int kHighGearController = 3; |
| 380 | const Eigen::Matrix<double, 2, 2> FF_high = |
| 381 | loop_->controller(kHighGearController).plant.B.inverse() * |
| 382 | (Eigen::Matrix<double, 2, 2>::Identity() - |
| 383 | loop_->controller(kHighGearController).plant.A); |
| 384 | |
| 385 | ::Eigen::Matrix<double, 1, 2> FF_sum = FF.colwise().sum(); |
| 386 | int min_FF_sum_index; |
| 387 | const double min_FF_sum = FF_sum.minCoeff(&min_FF_sum_index); |
| 388 | //const double min_K_sum = loop_->K().col(min_FF_sum_index).sum(); |
| 389 | const double high_min_FF_sum = FF_high.col(0).sum(); |
| 390 | |
| 391 | const double adjusted_ff_voltage = ::aos::Clip( |
| 392 | 12.0 * min_FF_sum / high_min_FF_sum, -12.0, 12.0); |
| 393 | return adjusted_ff_voltage / min_FF_sum; |
| 394 | } |
| 395 | |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 396 | void Update() { |
Austin Schuh | 78d5546 | 2014-02-23 01:39:30 -0800 | [diff] [blame] | 397 | const auto &values = constants::GetValues(); |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 398 | // TODO(austin): Observer for the current velocity instead of difference |
| 399 | // calculations. |
Brian Silverman | de8fd55 | 2013-11-03 15:53:42 -0800 | [diff] [blame] | 400 | ++counter_; |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 401 | const double current_left_velocity = |
Brian Silverman | de8fd55 | 2013-11-03 15:53:42 -0800 | [diff] [blame] | 402 | (position_.left_encoder - last_position_.left_encoder) / |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 403 | position_time_delta_; |
| 404 | const double current_right_velocity = |
Brian Silverman | de8fd55 | 2013-11-03 15:53:42 -0800 | [diff] [blame] | 405 | (position_.right_encoder - last_position_.right_encoder) / |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 406 | position_time_delta_; |
| 407 | const double left_motor_speed = |
Austin Schuh | 78d5546 | 2014-02-23 01:39:30 -0800 | [diff] [blame] | 408 | MotorSpeed(values.left_drive, position_.left_shifter_position, |
| 409 | current_left_velocity); |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 410 | const double right_motor_speed = |
Austin Schuh | 78d5546 | 2014-02-23 01:39:30 -0800 | [diff] [blame] | 411 | MotorSpeed(values.right_drive, position_.right_shifter_position, |
| 412 | current_right_velocity); |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 413 | |
Brian Silverman | 61e41fd | 2014-02-16 19:08:50 -0800 | [diff] [blame] | 414 | { |
| 415 | CIMLogging logging; |
| 416 | |
| 417 | // Reset the CIM model to the current conditions to be ready for when we |
| 418 | // shift. |
| 419 | if (IsInGear(left_gear_)) { |
| 420 | logging.left_in_gear = true; |
Brian Silverman | 61e41fd | 2014-02-16 19:08:50 -0800 | [diff] [blame] | 421 | } else { |
| 422 | logging.left_in_gear = false; |
| 423 | } |
| 424 | logging.left_motor_speed = left_motor_speed; |
| 425 | logging.left_velocity = current_left_velocity; |
| 426 | if (IsInGear(right_gear_)) { |
| 427 | logging.right_in_gear = true; |
Brian Silverman | 61e41fd | 2014-02-16 19:08:50 -0800 | [diff] [blame] | 428 | } else { |
| 429 | logging.right_in_gear = false; |
| 430 | } |
| 431 | logging.right_motor_speed = right_motor_speed; |
| 432 | logging.right_velocity = current_right_velocity; |
| 433 | |
| 434 | LOG_STRUCT(DEBUG, "currently", logging); |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 435 | } |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 436 | |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 437 | if (IsInGear(left_gear_) && IsInGear(right_gear_)) { |
| 438 | // FF * X = U (steady state) |
| 439 | const Eigen::Matrix<double, 2, 2> FF = |
| 440 | loop_->B().inverse() * |
| 441 | (Eigen::Matrix<double, 2, 2>::Identity() - loop_->A()); |
| 442 | |
| 443 | // Invert the plant to figure out how the velocity filter would have to |
| 444 | // work |
| 445 | // out in order to filter out the forwards negative inertia. |
| 446 | // This math assumes that the left and right power and velocity are |
| 447 | // equals, |
| 448 | // and that the plant is the same on the left and right. |
| 449 | const double fvel = FilterVelocity(throttle_); |
| 450 | |
| 451 | const double sign_svel = wheel_ * ((fvel > 0.0) ? 1.0 : -1.0); |
| 452 | double steering_velocity; |
| 453 | if (quickturn_) { |
| 454 | steering_velocity = wheel_ * MaxVelocity(); |
| 455 | } else { |
| 456 | steering_velocity = ::std::abs(fvel) * wheel_; |
| 457 | } |
| 458 | const double left_velocity = fvel - steering_velocity; |
| 459 | const double right_velocity = fvel + steering_velocity; |
| 460 | |
| 461 | // Integrate velocity to get the position. |
| 462 | // This position is used to get integral control. |
| 463 | loop_->R << left_velocity, right_velocity; |
| 464 | |
| 465 | if (!quickturn_) { |
| 466 | // K * R = w |
| 467 | Eigen::Matrix<double, 1, 2> equality_k; |
| 468 | equality_k << 1 + sign_svel, -(1 - sign_svel); |
| 469 | const double equality_w = 0.0; |
| 470 | |
| 471 | // Construct a constraint on R by manipulating the constraint on U |
| 472 | ::aos::controls::HPolytope<2> R_poly = ::aos::controls::HPolytope<2>( |
| 473 | U_Poly_.H() * (loop_->K() + FF), |
| 474 | U_Poly_.k() + U_Poly_.H() * loop_->K() * loop_->X_hat); |
| 475 | |
| 476 | // Limit R back inside the box. |
| 477 | loop_->R = CoerceGoal(R_poly, equality_k, equality_w, loop_->R); |
| 478 | } |
| 479 | |
| 480 | const Eigen::Matrix<double, 2, 1> FF_volts = FF * loop_->R; |
| 481 | const Eigen::Matrix<double, 2, 1> U_ideal = |
| 482 | loop_->K() * (loop_->R - loop_->X_hat) + FF_volts; |
| 483 | |
| 484 | for (int i = 0; i < 2; i++) { |
| 485 | loop_->U[i] = ::aos::Clip(U_ideal[i], -12, 12); |
| 486 | } |
Brian Silverman | de8fd55 | 2013-11-03 15:53:42 -0800 | [diff] [blame] | 487 | |
| 488 | // TODO(austin): Model this better. |
| 489 | // TODO(austin): Feed back? |
| 490 | loop_->X_hat = loop_->A() * loop_->X_hat + loop_->B() * loop_->U; |
Brian Silverman | 718b1d7 | 2013-10-28 16:22:45 -0700 | [diff] [blame] | 491 | } else { |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 492 | // Any motor is not in gear. Speed match. |
| 493 | ::Eigen::Matrix<double, 1, 1> R_left; |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 494 | ::Eigen::Matrix<double, 1, 1> R_right; |
Austin Schuh | b5afb69 | 2014-03-02 11:54:11 -0800 | [diff] [blame] | 495 | R_left(0, 0) = left_motor_speed; |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 496 | R_right(0, 0) = right_motor_speed; |
Austin Schuh | b5afb69 | 2014-03-02 11:54:11 -0800 | [diff] [blame] | 497 | |
| 498 | const double wiggle = |
Brian Silverman | f970f2c | 2014-03-22 19:34:30 -0700 | [diff] [blame] | 499 | (static_cast<double>((counter_ % 20) / 10) - 0.5) * 5.0; |
Austin Schuh | b5afb69 | 2014-03-02 11:54:11 -0800 | [diff] [blame] | 500 | |
Brian Silverman | 5665832 | 2014-03-22 16:57:22 -0700 | [diff] [blame] | 501 | loop_->U(0, 0) = ::aos::Clip( |
| 502 | (R_left / Kv)(0, 0) + (IsInGear(left_gear_) ? 0 : wiggle), |
| 503 | -12.0, 12.0); |
| 504 | loop_->U(1, 0) = ::aos::Clip( |
| 505 | (R_right / Kv)(0, 0) + (IsInGear(right_gear_) ? 0 : wiggle), |
| 506 | -12.0, 12.0); |
Brian Silverman | de8fd55 | 2013-11-03 15:53:42 -0800 | [diff] [blame] | 507 | loop_->U *= 12.0 / position_.battery_voltage; |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 508 | } |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | void SendMotors(Drivetrain::Output *output) { |
Brian Silverman | de8fd55 | 2013-11-03 15:53:42 -0800 | [diff] [blame] | 512 | if (output != NULL) { |
| 513 | output->left_voltage = loop_->U(0, 0); |
| 514 | output->right_voltage = loop_->U(1, 0); |
Brian Silverman | 61e41fd | 2014-02-16 19:08:50 -0800 | [diff] [blame] | 515 | output->left_high = left_gear_ == HIGH || left_gear_ == SHIFTING_UP; |
| 516 | output->right_high = right_gear_ == HIGH || right_gear_ == SHIFTING_UP; |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 517 | } |
| 518 | } |
| 519 | |
| 520 | private: |
| 521 | const ::aos::controls::HPolytope<2> U_Poly_; |
| 522 | |
| 523 | ::std::unique_ptr<StateFeedbackLoop<2, 2, 2>> loop_; |
| 524 | |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 525 | const double ttrust_; |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 526 | double wheel_; |
| 527 | double throttle_; |
| 528 | bool quickturn_; |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 529 | int stale_count_; |
| 530 | double position_time_delta_; |
| 531 | Gear left_gear_; |
| 532 | Gear right_gear_; |
| 533 | Drivetrain::Position last_position_; |
| 534 | Drivetrain::Position position_; |
Brian Silverman | de8fd55 | 2013-11-03 15:53:42 -0800 | [diff] [blame] | 535 | int counter_; |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 536 | }; |
Brian Silverman | cec3c8d | 2013-12-20 21:04:55 -0800 | [diff] [blame] | 537 | constexpr double PolyDrivetrain::kStallTorque; |
| 538 | constexpr double PolyDrivetrain::kStallCurrent; |
| 539 | constexpr double PolyDrivetrain::kFreeSpeed; |
| 540 | constexpr double PolyDrivetrain::kFreeCurrent; |
| 541 | constexpr double PolyDrivetrain::J; |
| 542 | constexpr double PolyDrivetrain::m; |
| 543 | constexpr double PolyDrivetrain::rb; |
| 544 | constexpr double PolyDrivetrain::kWheelRadius; |
| 545 | constexpr double PolyDrivetrain::kR; |
| 546 | constexpr double PolyDrivetrain::Kv; |
| 547 | constexpr double PolyDrivetrain::Kt; |
| 548 | |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 549 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 550 | void DrivetrainLoop::RunIteration(const Drivetrain::Goal *goal, |
| 551 | const Drivetrain::Position *position, |
| 552 | Drivetrain::Output *output, |
Ben Fredrickson | 890c3fe | 2014-03-02 00:15:16 +0000 | [diff] [blame] | 553 | Drivetrain::Status * status) { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 554 | // TODO(aschuh): These should be members of the class. |
| 555 | static DrivetrainMotorsSS dt_closedloop; |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 556 | static PolyDrivetrain dt_openloop; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 557 | |
| 558 | bool bad_pos = false; |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 559 | if (position == nullptr) { |
Brian Silverman | 50a9d03 | 2014-02-16 17:20:57 -0800 | [diff] [blame] | 560 | LOG_INTERVAL(no_position_); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 561 | bad_pos = true; |
| 562 | } |
Brian Silverman | 9c9ab42 | 2014-02-25 13:42:53 -0800 | [diff] [blame] | 563 | no_position_.Print(); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 564 | |
| 565 | double wheel = goal->steering; |
| 566 | double throttle = goal->throttle; |
| 567 | bool quickturn = goal->quickturn; |
| 568 | bool highgear = goal->highgear; |
| 569 | |
| 570 | bool control_loop_driving = goal->control_loop_driving; |
| 571 | double left_goal = goal->left_goal; |
| 572 | double right_goal = goal->right_goal; |
| 573 | |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 574 | dt_closedloop.SetGoal(left_goal, goal->left_velocity_goal, right_goal, |
| 575 | goal->right_velocity_goal); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 576 | if (!bad_pos) { |
| 577 | const double left_encoder = position->left_encoder; |
| 578 | const double right_encoder = position->right_encoder; |
Brian Silverman | 6bf0d3c | 2014-03-08 12:52:54 -0800 | [diff] [blame] | 579 | if (gyro_reading.FetchLatest()) { |
Brian Silverman | 74e5b4e | 2014-03-09 16:58:58 -0700 | [diff] [blame] | 580 | LOG_STRUCT(DEBUG, "using", *gyro_reading.get()); |
| 581 | dt_closedloop.SetPosition(left_encoder, right_encoder, |
| 582 | gyro_reading->angle, control_loop_driving); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 583 | } else { |
| 584 | dt_closedloop.SetRawPosition(left_encoder, right_encoder); |
| 585 | } |
| 586 | } |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 587 | dt_openloop.SetPosition(position); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 588 | dt_openloop.SetGoal(wheel, throttle, quickturn, highgear); |
| 589 | dt_openloop.Update(); |
Ben Fredrickson | 890c3fe | 2014-03-02 00:15:16 +0000 | [diff] [blame] | 590 | |
Brian Silverman | 2845c4c | 2013-03-16 19:54:08 -0700 | [diff] [blame] | 591 | if (control_loop_driving) { |
Ben Fredrickson | 890c3fe | 2014-03-02 00:15:16 +0000 | [diff] [blame] | 592 | dt_closedloop.Update(output == NULL); |
Brian Silverman | 2845c4c | 2013-03-16 19:54:08 -0700 | [diff] [blame] | 593 | dt_closedloop.SendMotors(output); |
| 594 | } else { |
| 595 | dt_openloop.SendMotors(output); |
Austin Schuh | b5afb69 | 2014-03-02 11:54:11 -0800 | [diff] [blame] | 596 | if (output) { |
| 597 | dt_closedloop.SetExternalMotors(output->left_voltage, |
| 598 | output->right_voltage); |
| 599 | } |
Ben Fredrickson | 890c3fe | 2014-03-02 00:15:16 +0000 | [diff] [blame] | 600 | dt_closedloop.Update(output == NULL); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 601 | } |
Ben Fredrickson | 890c3fe | 2014-03-02 00:15:16 +0000 | [diff] [blame] | 602 | |
| 603 | // set the output status of the controll loop state |
| 604 | if (status) { |
| 605 | bool done = false; |
| 606 | if (goal) { |
| 607 | done = ((::std::abs(goal->left_goal - |
| 608 | dt_closedloop.GetEstimatedLeftEncoder()) < |
| 609 | constants::GetValues().drivetrain_done_distance) && |
| 610 | (::std::abs(goal->right_goal - |
| 611 | dt_closedloop.GetEstimatedRightEncoder()) < |
| 612 | constants::GetValues().drivetrain_done_distance)); |
| 613 | } |
| 614 | status->is_done = done; |
| 615 | status->robot_speed = dt_closedloop.GetEstimatedRobotSpeed(); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 616 | } |
| 617 | } |
| 618 | |
| 619 | } // namespace control_loops |
| 620 | } // namespace frc971 |