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