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" |
| 14 | #include "frc971/control_loops/drivetrain/drivetrain_motor_plant.h" |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 15 | #include "frc971/control_loops/drivetrain/polydrivetrain_motor_plant.h" |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 16 | #include "frc971/control_loops/drivetrain/polydrivetrain_cim_plant.h" |
James Kuszmaul | f254c1a | 2013-03-10 16:31:26 -0700 | [diff] [blame] | 17 | #include "frc971/control_loops/drivetrain/drivetrain.q.h" |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 18 | #include "frc971/queues/GyroAngle.q.h" |
| 19 | #include "frc971/queues/Piston.q.h" |
| 20 | |
| 21 | using frc971::sensors::gyro; |
| 22 | |
| 23 | namespace frc971 { |
| 24 | namespace control_loops { |
| 25 | |
| 26 | // Width of the robot. |
| 27 | const double width = 22.0 / 100.0 * 2.54; |
| 28 | |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 29 | Eigen::Matrix<double, 2, 1> CoerceGoal(aos::controls::HPolytope<2> ®ion, |
| 30 | const Eigen::Matrix<double, 1, 2> &K, |
| 31 | double w, |
| 32 | const Eigen::Matrix<double, 2, 1> &R) { |
| 33 | if (region.IsInside(R)) { |
| 34 | return R; |
| 35 | } |
| 36 | Eigen::Matrix<double, 2, 1> parallel_vector; |
| 37 | Eigen::Matrix<double, 2, 1> perpendicular_vector; |
| 38 | perpendicular_vector = K.transpose().normalized(); |
| 39 | parallel_vector << perpendicular_vector(1, 0), -perpendicular_vector(0, 0); |
| 40 | |
| 41 | aos::controls::HPolytope<1> t_poly( |
| 42 | region.H() * parallel_vector, |
| 43 | region.k() - region.H() * perpendicular_vector * w); |
| 44 | |
| 45 | Eigen::Matrix<double, 1, Eigen::Dynamic> vertices = t_poly.Vertices(); |
| 46 | if (vertices.innerSize() > 0) { |
| 47 | double min_distance_sqr = 0; |
| 48 | Eigen::Matrix<double, 2, 1> closest_point; |
| 49 | for (int i = 0; i < vertices.innerSize(); i++) { |
| 50 | Eigen::Matrix<double, 2, 1> point; |
| 51 | point = parallel_vector * vertices(0, i) + perpendicular_vector * w; |
| 52 | const double length = (R - point).squaredNorm(); |
| 53 | if (i == 0 || length < min_distance_sqr) { |
| 54 | closest_point = point; |
| 55 | min_distance_sqr = length; |
| 56 | } |
| 57 | } |
| 58 | return closest_point; |
| 59 | } else { |
| 60 | Eigen::Matrix<double, 2, Eigen::Dynamic> region_vertices = |
| 61 | region.Vertices(); |
| 62 | double min_distance; |
| 63 | int closest_i = 0; |
| 64 | for (int i = 0; i < region_vertices.outerSize(); i++) { |
| 65 | const double length = ::std::abs( |
| 66 | (perpendicular_vector.transpose() * (region_vertices.col(i)))(0, 0)); |
| 67 | if (i == 0 || length < min_distance) { |
| 68 | closest_i = i; |
| 69 | min_distance = length; |
| 70 | } |
| 71 | } |
| 72 | return region_vertices.col(closest_i); |
| 73 | } |
| 74 | } |
| 75 | |
Austin Schuh | 4352ac6 | 2013-03-19 06:23:16 +0000 | [diff] [blame] | 76 | class DrivetrainMotorsSS { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 77 | public: |
Austin Schuh | 4352ac6 | 2013-03-19 06:23:16 +0000 | [diff] [blame] | 78 | DrivetrainMotorsSS () |
| 79 | : loop_(new StateFeedbackLoop<4, 2, 2>(MakeDrivetrainLoop())) { |
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; |
| 165 | static constexpr double kWheelRadius = .04445; |
| 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; |
| 173 | // Gear ratios |
| 174 | static constexpr double G_low = 16.0 / 60.0 * 19.0 / 50.0; |
| 175 | static constexpr double G_high = 28.0 / 48.0 * 19.0 / 50.0; |
| 176 | |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 177 | PolyDrivetrain() |
| 178 | : U_Poly_((Eigen::Matrix<double, 4, 2>() << /*[[*/ 1, 0 /*]*/, |
| 179 | /*[*/ -1, 0 /*]*/, |
| 180 | /*[*/ 0, 1 /*]*/, |
| 181 | /*[*/ 0, -1 /*]]*/).finished(), |
| 182 | (Eigen::Matrix<double, 4, 1>() << /*[[*/ 12 /*]*/, |
| 183 | /*[*/ 12 /*]*/, |
| 184 | /*[*/ 12 /*]*/, |
| 185 | /*[*/ 12 /*]]*/).finished()), |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 186 | loop_(new StateFeedbackLoop<2, 2, 2>(MakeVDrivetrainLoop())), |
| 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) { |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 207 | return velocity / G_high / kWheelRadius; |
| 208 | } else { |
| 209 | return velocity / G_low / kWheelRadius; |
| 210 | } |
| 211 | } |
| 212 | |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 213 | void SetGoal(double wheel, double throttle, bool quickturn, bool highgear) { |
Brian Silverman | de8fd55 | 2013-11-03 15:53:42 -0800 | [diff] [blame^] | 214 | const double kWheelNonLinearity = 0.3; |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 215 | // Apply a sin function that's scaled to make it feel better. |
| 216 | const double angular_range = M_PI_2 * kWheelNonLinearity; |
| 217 | wheel_ = sin(angular_range * wheel) / sin(angular_range); |
| 218 | wheel_ = sin(angular_range * wheel_) / sin(angular_range); |
| 219 | throttle_ = throttle; |
| 220 | quickturn_ = quickturn; |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 221 | |
| 222 | // TODO(austin): Fix the upshift logic to include states. |
| 223 | const Gear requested_gear = highgear ? HIGH : LOW; |
| 224 | |
| 225 | if (left_gear_ != requested_gear) { |
| 226 | if (IsInGear(left_gear_)) { |
| 227 | if (requested_gear == HIGH) { |
| 228 | left_gear_ = SHIFTING_UP; |
| 229 | } else { |
| 230 | left_gear_ = SHIFTING_DOWN; |
| 231 | } |
| 232 | } |
| 233 | } |
| 234 | if (right_gear_ != requested_gear) { |
| 235 | if (IsInGear(right_gear_)) { |
| 236 | if (requested_gear == HIGH) { |
| 237 | right_gear_ = SHIFTING_UP; |
| 238 | } else { |
| 239 | right_gear_ = SHIFTING_DOWN; |
| 240 | } |
| 241 | } |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 242 | } |
| 243 | } |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 244 | void SetPosition(const Drivetrain::Position *position) { |
| 245 | if (position == NULL) { |
| 246 | ++stale_count_; |
| 247 | } else { |
| 248 | last_position_ = position_; |
| 249 | position_ = *position; |
| 250 | position_time_delta_ = (stale_count_ + 1) * 0.01; |
| 251 | stale_count_ = 0; |
| 252 | } |
| 253 | |
| 254 | if (position) { |
| 255 | // Switch to the correct controller. |
Brian Silverman | de8fd55 | 2013-11-03 15:53:42 -0800 | [diff] [blame^] | 256 | // TODO(austin): Un-hard code 0.57 |
| 257 | if (position->left_shifter_position < 0.57) { |
| 258 | if (position->right_shifter_position < 0.57) { |
| 259 | LOG(DEBUG, "Loop Left low, Right low\n"); |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 260 | loop_->set_controller_index(0); |
| 261 | } else { |
Brian Silverman | de8fd55 | 2013-11-03 15:53:42 -0800 | [diff] [blame^] | 262 | LOG(DEBUG, "Loop Left low, Right high\n"); |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 263 | loop_->set_controller_index(1); |
| 264 | } |
| 265 | } else { |
Brian Silverman | de8fd55 | 2013-11-03 15:53:42 -0800 | [diff] [blame^] | 266 | if (position->right_shifter_position < 0.57) { |
| 267 | LOG(DEBUG, "Loop Left high, Right low\n"); |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 268 | loop_->set_controller_index(2); |
| 269 | } else { |
Brian Silverman | de8fd55 | 2013-11-03 15:53:42 -0800 | [diff] [blame^] | 270 | LOG(DEBUG, "Loop Left high, Right high\n"); |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 271 | loop_->set_controller_index(3); |
| 272 | } |
| 273 | } |
Brian Silverman | de8fd55 | 2013-11-03 15:53:42 -0800 | [diff] [blame^] | 274 | switch (left_gear_) { |
| 275 | case LOW: |
| 276 | LOG(DEBUG, "Left is in low\n"); |
| 277 | break; |
| 278 | case HIGH: |
| 279 | LOG(DEBUG, "Left is in high\n"); |
| 280 | break; |
| 281 | case SHIFTING_UP: |
| 282 | LOG(DEBUG, "Left is shifting up\n"); |
| 283 | break; |
| 284 | case SHIFTING_DOWN: |
| 285 | LOG(DEBUG, "Left is shifting down\n"); |
| 286 | break; |
| 287 | } |
| 288 | switch (right_gear_) { |
| 289 | case LOW: |
| 290 | LOG(DEBUG, "Right is in low\n"); |
| 291 | break; |
| 292 | case HIGH: |
| 293 | LOG(DEBUG, "Right is in high\n"); |
| 294 | break; |
| 295 | case SHIFTING_UP: |
| 296 | LOG(DEBUG, "Right is shifting up\n"); |
| 297 | break; |
| 298 | case SHIFTING_DOWN: |
| 299 | LOG(DEBUG, "Right is shifting down\n"); |
| 300 | break; |
| 301 | } |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 302 | // TODO(austin): Constants. |
| 303 | if (position->left_shifter_position > 0.9 && left_gear_ == SHIFTING_UP) { |
| 304 | left_gear_ = HIGH; |
| 305 | } |
| 306 | if (position->left_shifter_position < 0.1 && left_gear_ == SHIFTING_DOWN) { |
| 307 | left_gear_ = LOW; |
| 308 | } |
| 309 | if (position->right_shifter_position > 0.9 && right_gear_ == SHIFTING_UP) { |
| 310 | right_gear_ = HIGH; |
| 311 | } |
| 312 | if (position->right_shifter_position < 0.1 && right_gear_ == SHIFTING_DOWN) { |
| 313 | right_gear_ = LOW; |
| 314 | } |
| 315 | } |
| 316 | } |
| 317 | |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 318 | double FilterVelocity(double throttle) { |
| 319 | const Eigen::Matrix<double, 2, 2> FF = |
| 320 | loop_->B().inverse() * |
| 321 | (Eigen::Matrix<double, 2, 2>::Identity() - loop_->A()); |
| 322 | |
| 323 | constexpr int kHighGearController = 3; |
| 324 | const Eigen::Matrix<double, 2, 2> FF_high = |
| 325 | loop_->controller(kHighGearController).plant.B.inverse() * |
| 326 | (Eigen::Matrix<double, 2, 2>::Identity() - |
| 327 | loop_->controller(kHighGearController).plant.A); |
| 328 | |
| 329 | ::Eigen::Matrix<double, 1, 2> FF_sum = FF.colwise().sum(); |
| 330 | int min_FF_sum_index; |
| 331 | const double min_FF_sum = FF_sum.minCoeff(&min_FF_sum_index); |
| 332 | const double min_K_sum = loop_->K().col(min_FF_sum_index).sum(); |
| 333 | const double high_min_FF_sum = FF_high.col(0).sum(); |
| 334 | |
| 335 | const double adjusted_ff_voltage = ::aos::Clip( |
| 336 | throttle * 12.0 * min_FF_sum / high_min_FF_sum, -12.0, 12.0); |
| 337 | return ((adjusted_ff_voltage + |
| 338 | ttrust_ * min_K_sum * (loop_->X_hat(0, 0) + loop_->X_hat(1, 0)) / |
| 339 | 2.0) / |
| 340 | (ttrust_ * min_K_sum + min_FF_sum)); |
| 341 | } |
| 342 | |
Brian Silverman | 718b1d7 | 2013-10-28 16:22:45 -0700 | [diff] [blame] | 343 | double MaxVelocity() { |
| 344 | const Eigen::Matrix<double, 2, 2> FF = |
| 345 | loop_->B().inverse() * |
| 346 | (Eigen::Matrix<double, 2, 2>::Identity() - loop_->A()); |
| 347 | |
| 348 | constexpr int kHighGearController = 3; |
| 349 | const Eigen::Matrix<double, 2, 2> FF_high = |
| 350 | loop_->controller(kHighGearController).plant.B.inverse() * |
| 351 | (Eigen::Matrix<double, 2, 2>::Identity() - |
| 352 | loop_->controller(kHighGearController).plant.A); |
| 353 | |
| 354 | ::Eigen::Matrix<double, 1, 2> FF_sum = FF.colwise().sum(); |
| 355 | int min_FF_sum_index; |
| 356 | const double min_FF_sum = FF_sum.minCoeff(&min_FF_sum_index); |
| 357 | //const double min_K_sum = loop_->K().col(min_FF_sum_index).sum(); |
| 358 | const double high_min_FF_sum = FF_high.col(0).sum(); |
| 359 | |
| 360 | const double adjusted_ff_voltage = ::aos::Clip( |
| 361 | 12.0 * min_FF_sum / high_min_FF_sum, -12.0, 12.0); |
| 362 | return adjusted_ff_voltage / min_FF_sum; |
| 363 | } |
| 364 | |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 365 | void Update() { |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 366 | // TODO(austin): Observer for the current velocity instead of difference |
| 367 | // calculations. |
Brian Silverman | de8fd55 | 2013-11-03 15:53:42 -0800 | [diff] [blame^] | 368 | ++counter_; |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 369 | const double current_left_velocity = |
Brian Silverman | de8fd55 | 2013-11-03 15:53:42 -0800 | [diff] [blame^] | 370 | (position_.left_encoder - last_position_.left_encoder) / |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 371 | position_time_delta_; |
| 372 | const double current_right_velocity = |
Brian Silverman | de8fd55 | 2013-11-03 15:53:42 -0800 | [diff] [blame^] | 373 | (position_.right_encoder - last_position_.right_encoder) / |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 374 | position_time_delta_; |
| 375 | const double left_motor_speed = |
| 376 | MotorSpeed(position_.left_shifter_position, current_left_velocity); |
| 377 | const double right_motor_speed = |
| 378 | MotorSpeed(position_.right_shifter_position, current_right_velocity); |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 379 | |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 380 | // Reset the CIM model to the current conditions to be ready for when we shift. |
| 381 | if (IsInGear(left_gear_)) { |
| 382 | left_cim_->X_hat(0, 0) = left_motor_speed; |
Brian Silverman | de8fd55 | 2013-11-03 15:53:42 -0800 | [diff] [blame^] | 383 | LOG(DEBUG, "Setting left CIM to %f at robot speed %f\n", left_motor_speed, |
| 384 | current_left_velocity); |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 385 | } |
| 386 | if (IsInGear(right_gear_)) { |
Brian Silverman | de8fd55 | 2013-11-03 15:53:42 -0800 | [diff] [blame^] | 387 | right_cim_->X_hat(0, 0) = right_motor_speed; |
| 388 | LOG(DEBUG, "Setting right CIM to %f at robot speed %f\n", |
| 389 | right_motor_speed, current_right_velocity); |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 390 | } |
Brian Silverman | de8fd55 | 2013-11-03 15:53:42 -0800 | [diff] [blame^] | 391 | LOG(DEBUG, "robot speed l=%f r=%f\n", current_left_velocity, |
| 392 | current_right_velocity); |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 393 | |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 394 | if (IsInGear(left_gear_) && IsInGear(right_gear_)) { |
| 395 | // FF * X = U (steady state) |
| 396 | const Eigen::Matrix<double, 2, 2> FF = |
| 397 | loop_->B().inverse() * |
| 398 | (Eigen::Matrix<double, 2, 2>::Identity() - loop_->A()); |
| 399 | |
| 400 | // Invert the plant to figure out how the velocity filter would have to |
| 401 | // work |
| 402 | // out in order to filter out the forwards negative inertia. |
| 403 | // This math assumes that the left and right power and velocity are |
| 404 | // equals, |
| 405 | // and that the plant is the same on the left and right. |
| 406 | const double fvel = FilterVelocity(throttle_); |
| 407 | |
| 408 | const double sign_svel = wheel_ * ((fvel > 0.0) ? 1.0 : -1.0); |
| 409 | double steering_velocity; |
| 410 | if (quickturn_) { |
| 411 | steering_velocity = wheel_ * MaxVelocity(); |
| 412 | } else { |
| 413 | steering_velocity = ::std::abs(fvel) * wheel_; |
| 414 | } |
| 415 | const double left_velocity = fvel - steering_velocity; |
| 416 | const double right_velocity = fvel + steering_velocity; |
| 417 | |
| 418 | // Integrate velocity to get the position. |
| 419 | // This position is used to get integral control. |
| 420 | loop_->R << left_velocity, right_velocity; |
| 421 | |
| 422 | if (!quickturn_) { |
| 423 | // K * R = w |
| 424 | Eigen::Matrix<double, 1, 2> equality_k; |
| 425 | equality_k << 1 + sign_svel, -(1 - sign_svel); |
| 426 | const double equality_w = 0.0; |
| 427 | |
| 428 | // Construct a constraint on R by manipulating the constraint on U |
| 429 | ::aos::controls::HPolytope<2> R_poly = ::aos::controls::HPolytope<2>( |
| 430 | U_Poly_.H() * (loop_->K() + FF), |
| 431 | U_Poly_.k() + U_Poly_.H() * loop_->K() * loop_->X_hat); |
| 432 | |
| 433 | // Limit R back inside the box. |
| 434 | loop_->R = CoerceGoal(R_poly, equality_k, equality_w, loop_->R); |
| 435 | } |
| 436 | |
| 437 | const Eigen::Matrix<double, 2, 1> FF_volts = FF * loop_->R; |
| 438 | const Eigen::Matrix<double, 2, 1> U_ideal = |
| 439 | loop_->K() * (loop_->R - loop_->X_hat) + FF_volts; |
| 440 | |
| 441 | for (int i = 0; i < 2; i++) { |
| 442 | loop_->U[i] = ::aos::Clip(U_ideal[i], -12, 12); |
| 443 | } |
Brian Silverman | de8fd55 | 2013-11-03 15:53:42 -0800 | [diff] [blame^] | 444 | |
| 445 | // TODO(austin): Model this better. |
| 446 | // TODO(austin): Feed back? |
| 447 | loop_->X_hat = loop_->A() * loop_->X_hat + loop_->B() * loop_->U; |
Brian Silverman | 718b1d7 | 2013-10-28 16:22:45 -0700 | [diff] [blame] | 448 | } else { |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 449 | // Any motor is not in gear. Speed match. |
| 450 | ::Eigen::Matrix<double, 1, 1> R_left; |
| 451 | R_left(0, 0) = left_motor_speed; |
Brian Silverman | de8fd55 | 2013-11-03 15:53:42 -0800 | [diff] [blame^] | 452 | 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] | 453 | |
Brian Silverman | de8fd55 | 2013-11-03 15:53:42 -0800 | [diff] [blame^] | 454 | loop_->U(0, 0) = |
| 455 | ::aos::Clip((R_left / Kv)(0, 0) + wiggle, -position_.battery_voltage, |
| 456 | position_.battery_voltage); |
| 457 | right_cim_->X_hat = right_cim_->A() * right_cim_->X_hat + |
| 458 | right_cim_->B() * loop_->U(0, 0); |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 459 | |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 460 | ::Eigen::Matrix<double, 1, 1> R_right; |
| 461 | R_right(0, 0) = right_motor_speed; |
Brian Silverman | de8fd55 | 2013-11-03 15:53:42 -0800 | [diff] [blame^] | 462 | loop_->U(1, 0) = |
| 463 | ::aos::Clip((R_right / Kv)(0, 0) + wiggle, -position_.battery_voltage, |
| 464 | position_.battery_voltage); |
| 465 | right_cim_->X_hat = right_cim_->A() * right_cim_->X_hat + |
| 466 | right_cim_->B() * loop_->U(1, 0); |
| 467 | loop_->U *= 12.0 / position_.battery_voltage; |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 468 | } |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | void SendMotors(Drivetrain::Output *output) { |
| 472 | LOG(DEBUG, "left pwm: %f right pwm: %f wheel: %f throttle: %f\n", |
| 473 | loop_->U(0, 0), loop_->U(1, 0), wheel_, throttle_); |
Brian Silverman | de8fd55 | 2013-11-03 15:53:42 -0800 | [diff] [blame^] | 474 | if (output != NULL) { |
| 475 | output->left_voltage = loop_->U(0, 0); |
| 476 | output->right_voltage = loop_->U(1, 0); |
| 477 | } |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 478 | // Go in high gear if anything wants to be in high gear. |
| 479 | // TODO(austin): Seperate these. |
| 480 | if (left_gear_ == HIGH || left_gear_ == SHIFTING_UP || |
| 481 | right_gear_ == HIGH || right_gear_ == SHIFTING_UP) { |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 482 | shifters.MakeWithBuilder().set(false).Send(); |
| 483 | } else { |
| 484 | shifters.MakeWithBuilder().set(true).Send(); |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | private: |
| 489 | const ::aos::controls::HPolytope<2> U_Poly_; |
| 490 | |
| 491 | ::std::unique_ptr<StateFeedbackLoop<2, 2, 2>> loop_; |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 492 | ::std::unique_ptr<StateFeedbackLoop<1, 1, 1>> left_cim_; |
| 493 | ::std::unique_ptr<StateFeedbackLoop<1, 1, 1>> right_cim_; |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 494 | |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 495 | const double ttrust_; |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 496 | double wheel_; |
| 497 | double throttle_; |
| 498 | bool quickturn_; |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 499 | int stale_count_; |
| 500 | double position_time_delta_; |
| 501 | Gear left_gear_; |
| 502 | Gear right_gear_; |
| 503 | Drivetrain::Position last_position_; |
| 504 | Drivetrain::Position position_; |
Brian Silverman | de8fd55 | 2013-11-03 15:53:42 -0800 | [diff] [blame^] | 505 | int counter_; |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 506 | }; |
| 507 | |
| 508 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 509 | class DrivetrainMotorsOL { |
| 510 | public: |
| 511 | DrivetrainMotorsOL() { |
| 512 | _old_wheel = 0.0; |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 513 | wheel_ = 0.0; |
| 514 | throttle_ = 0.0; |
| 515 | quickturn_ = false; |
| 516 | highgear_ = true; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 517 | _neg_inertia_accumulator = 0.0; |
| 518 | _left_pwm = 0.0; |
| 519 | _right_pwm = 0.0; |
| 520 | } |
| 521 | void SetGoal(double wheel, double throttle, bool quickturn, bool highgear) { |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 522 | wheel_ = wheel; |
| 523 | throttle_ = throttle; |
| 524 | quickturn_ = quickturn; |
| 525 | highgear_ = highgear; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 526 | _left_pwm = 0.0; |
| 527 | _right_pwm = 0.0; |
| 528 | } |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 529 | void Update() { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 530 | double overPower; |
| 531 | float sensitivity = 1.7; |
| 532 | float angular_power; |
| 533 | float linear_power; |
| 534 | double wheel; |
| 535 | |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 536 | double neg_inertia = wheel_ - _old_wheel; |
| 537 | _old_wheel = wheel_; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 538 | |
| 539 | double wheelNonLinearity; |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 540 | if (highgear_) { |
Brian Silverman | 77a7600 | 2013-03-16 20:09:00 -0700 | [diff] [blame] | 541 | wheelNonLinearity = 0.1; // used to be csvReader->TURN_NONLIN_HIGH |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 542 | // Apply a sin function that's scaled to make it feel better. |
| 543 | const double angular_range = M_PI / 2.0 * wheelNonLinearity; |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 544 | wheel = sin(angular_range * wheel_) / sin(angular_range); |
Brian Silverman | 77a7600 | 2013-03-16 20:09:00 -0700 | [diff] [blame] | 545 | wheel = sin(angular_range * wheel) / sin(angular_range); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 546 | } else { |
Brian Silverman | df43ec2 | 2013-03-16 23:48:29 -0700 | [diff] [blame] | 547 | wheelNonLinearity = 0.2; // used to be csvReader->TURN_NONLIN_LOW |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 548 | // Apply a sin function that's scaled to make it feel better. |
| 549 | const double angular_range = M_PI / 2.0 * wheelNonLinearity; |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 550 | wheel = sin(angular_range * wheel_) / sin(angular_range); |
Brian Silverman | 77a7600 | 2013-03-16 20:09:00 -0700 | [diff] [blame] | 551 | wheel = sin(angular_range * wheel) / sin(angular_range); |
| 552 | wheel = sin(angular_range * wheel) / sin(angular_range); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 553 | } |
| 554 | |
Brian Silverman | df43ec2 | 2013-03-16 23:48:29 -0700 | [diff] [blame] | 555 | static const double kThrottleDeadband = 0.05; |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 556 | if (::std::abs(throttle_) < kThrottleDeadband) { |
| 557 | throttle_ = 0; |
Brian Silverman | df43ec2 | 2013-03-16 23:48:29 -0700 | [diff] [blame] | 558 | } else { |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 559 | throttle_ = copysign((::std::abs(throttle_) - kThrottleDeadband) / |
| 560 | (1.0 - kThrottleDeadband), throttle_); |
Brian Silverman | df43ec2 | 2013-03-16 23:48:29 -0700 | [diff] [blame] | 561 | } |
| 562 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 563 | double neg_inertia_scalar; |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 564 | if (highgear_) { |
Brian Silverman | df43ec2 | 2013-03-16 23:48:29 -0700 | [diff] [blame] | 565 | neg_inertia_scalar = 8.0; // used to be csvReader->NEG_INTERTIA_HIGH |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 566 | sensitivity = 1.22; // used to be csvReader->SENSE_HIGH |
| 567 | } else { |
| 568 | if (wheel * neg_inertia > 0) { |
Brian Silverman | df43ec2 | 2013-03-16 23:48:29 -0700 | [diff] [blame] | 569 | neg_inertia_scalar = 5; // used to be csvReader->NEG_INERTIA_LOW_MORE |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 570 | } else { |
Brian Silverman | df43ec2 | 2013-03-16 23:48:29 -0700 | [diff] [blame] | 571 | if (::std::abs(wheel) > 0.65) { |
| 572 | neg_inertia_scalar = 5; // used to be csvReader->NEG_INTERTIA_LOW_LESS_EXT |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 573 | } else { |
Brian Silverman | df43ec2 | 2013-03-16 23:48:29 -0700 | [diff] [blame] | 574 | neg_inertia_scalar = 5; // used to be csvReader->NEG_INTERTIA_LOW_LESS |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 575 | } |
| 576 | } |
| 577 | sensitivity = 1.24; // used to be csvReader->SENSE_LOW |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 578 | } |
| 579 | double neg_inertia_power = neg_inertia * neg_inertia_scalar; |
| 580 | _neg_inertia_accumulator += neg_inertia_power; |
| 581 | |
| 582 | wheel = wheel + _neg_inertia_accumulator; |
| 583 | if (_neg_inertia_accumulator > 1) { |
| 584 | _neg_inertia_accumulator -= 1; |
| 585 | } else if (_neg_inertia_accumulator < -1) { |
| 586 | _neg_inertia_accumulator += 1; |
| 587 | } else { |
| 588 | _neg_inertia_accumulator = 0; |
| 589 | } |
| 590 | |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 591 | linear_power = throttle_; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 592 | |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 593 | if (quickturn_) { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 594 | double qt_angular_power = wheel; |
Brian Silverman | df43ec2 | 2013-03-16 23:48:29 -0700 | [diff] [blame] | 595 | if (::std::abs(linear_power) < 0.2) { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 596 | if (qt_angular_power > 1) qt_angular_power = 1.0; |
| 597 | if (qt_angular_power < -1) qt_angular_power = -1.0; |
| 598 | } else { |
| 599 | qt_angular_power = 0.0; |
| 600 | } |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 601 | overPower = 1.0; |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 602 | if (highgear_) { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 603 | sensitivity = 1.0; |
| 604 | } else { |
| 605 | sensitivity = 1.0; |
| 606 | } |
| 607 | angular_power = wheel; |
| 608 | } else { |
| 609 | overPower = 0.0; |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 610 | angular_power = ::std::abs(throttle_) * wheel * sensitivity; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | _right_pwm = _left_pwm = linear_power; |
| 614 | _left_pwm += angular_power; |
| 615 | _right_pwm -= angular_power; |
| 616 | |
| 617 | if (_left_pwm > 1.0) { |
| 618 | _right_pwm -= overPower*(_left_pwm - 1.0); |
| 619 | _left_pwm = 1.0; |
| 620 | } else if (_right_pwm > 1.0) { |
| 621 | _left_pwm -= overPower*(_right_pwm - 1.0); |
| 622 | _right_pwm = 1.0; |
| 623 | } else if (_left_pwm < -1.0) { |
| 624 | _right_pwm += overPower*(-1.0 - _left_pwm); |
| 625 | _left_pwm = -1.0; |
| 626 | } else if (_right_pwm < -1.0) { |
| 627 | _left_pwm += overPower*(-1.0 - _right_pwm); |
| 628 | _right_pwm = -1.0; |
| 629 | } |
| 630 | } |
| 631 | |
| 632 | void SendMotors(Drivetrain::Output *output) { |
Brian Silverman | df43ec2 | 2013-03-16 23:48:29 -0700 | [diff] [blame] | 633 | 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] | 634 | _left_pwm, _right_pwm, wheel_, throttle_); |
brians | 8ad7405 | 2013-03-16 21:04:51 +0000 | [diff] [blame] | 635 | if (output) { |
| 636 | output->left_voltage = _left_pwm * 12.0; |
| 637 | output->right_voltage = _right_pwm * 12.0; |
| 638 | } |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 639 | if (highgear_) { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 640 | shifters.MakeWithBuilder().set(false).Send(); |
| 641 | } else { |
| 642 | shifters.MakeWithBuilder().set(true).Send(); |
| 643 | } |
| 644 | } |
| 645 | |
| 646 | private: |
| 647 | double _old_wheel; |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 648 | double wheel_; |
| 649 | double throttle_; |
| 650 | bool quickturn_; |
| 651 | bool highgear_; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 652 | double _neg_inertia_accumulator; |
| 653 | double _left_pwm; |
| 654 | double _right_pwm; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 655 | }; |
| 656 | |
| 657 | void DrivetrainLoop::RunIteration(const Drivetrain::Goal *goal, |
| 658 | const Drivetrain::Position *position, |
| 659 | Drivetrain::Output *output, |
| 660 | Drivetrain::Status * /*status*/) { |
| 661 | // TODO(aschuh): These should be members of the class. |
| 662 | static DrivetrainMotorsSS dt_closedloop; |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 663 | static PolyDrivetrain dt_openloop; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 664 | |
| 665 | bool bad_pos = false; |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 666 | if (position == nullptr) { |
James Kuszmaul | 3f35474 | 2013-03-10 17:27:56 -0700 | [diff] [blame] | 667 | LOG(WARNING, "no position\n"); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 668 | bad_pos = true; |
| 669 | } |
| 670 | |
| 671 | double wheel = goal->steering; |
| 672 | double throttle = goal->throttle; |
| 673 | bool quickturn = goal->quickturn; |
| 674 | bool highgear = goal->highgear; |
| 675 | |
| 676 | bool control_loop_driving = goal->control_loop_driving; |
| 677 | double left_goal = goal->left_goal; |
| 678 | double right_goal = goal->right_goal; |
| 679 | |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 680 | dt_closedloop.SetGoal(left_goal, goal->left_velocity_goal, right_goal, |
| 681 | goal->right_velocity_goal); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 682 | if (!bad_pos) { |
| 683 | const double left_encoder = position->left_encoder; |
| 684 | const double right_encoder = position->right_encoder; |
| 685 | if (gyro.FetchLatest()) { |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 686 | dt_closedloop.SetPosition(left_encoder, right_encoder, gyro->angle, |
| 687 | control_loop_driving); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 688 | } else { |
| 689 | dt_closedloop.SetRawPosition(left_encoder, right_encoder); |
| 690 | } |
| 691 | } |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 692 | dt_openloop.SetPosition(position); |
Austin Schuh | 4352ac6 | 2013-03-19 06:23:16 +0000 | [diff] [blame] | 693 | dt_closedloop.Update(position, output == NULL); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 694 | dt_openloop.SetGoal(wheel, throttle, quickturn, highgear); |
| 695 | dt_openloop.Update(); |
Brian Silverman | 2845c4c | 2013-03-16 19:54:08 -0700 | [diff] [blame] | 696 | if (control_loop_driving) { |
| 697 | dt_closedloop.SendMotors(output); |
| 698 | } else { |
| 699 | dt_openloop.SendMotors(output); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 700 | } |
| 701 | } |
| 702 | |
| 703 | } // namespace control_loops |
| 704 | } // namespace frc971 |