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