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