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