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