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" |
James Kuszmaul | f254c1a | 2013-03-10 16:31:26 -0700 | [diff] [blame] | 16 | #include "frc971/control_loops/drivetrain/drivetrain.q.h" |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 17 | #include "frc971/queues/GyroAngle.q.h" |
| 18 | #include "frc971/queues/Piston.q.h" |
| 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: |
Austin Schuh | 4352ac6 | 2013-03-19 06:23:16 +0000 | [diff] [blame] | 77 | DrivetrainMotorsSS () |
| 78 | : loop_(new StateFeedbackLoop<4, 2, 2>(MakeDrivetrainLoop())) { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 79 | _offset = 0; |
| 80 | _integral_offset = 0; |
| 81 | _left_goal = 0.0; |
| 82 | _right_goal = 0.0; |
| 83 | _raw_left = 0.0; |
| 84 | _raw_right = 0.0; |
Austin Schuh | 4352ac6 | 2013-03-19 06:23:16 +0000 | [diff] [blame] | 85 | _control_loop_driving = false; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 86 | } |
| 87 | void SetGoal(double left, double left_velocity, double right, double right_velocity) { |
| 88 | _left_goal = left; |
| 89 | _right_goal = right; |
Austin Schuh | 4352ac6 | 2013-03-19 06:23:16 +0000 | [diff] [blame] | 90 | loop_->R << left, left_velocity, right, right_velocity; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 91 | } |
| 92 | void SetRawPosition(double left, double right) { |
| 93 | _raw_right = right; |
| 94 | _raw_left = left; |
Austin Schuh | 4352ac6 | 2013-03-19 06:23:16 +0000 | [diff] [blame] | 95 | loop_->Y << left, right; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 96 | } |
Austin Schuh | 4352ac6 | 2013-03-19 06:23:16 +0000 | [diff] [blame] | 97 | void SetPosition( |
| 98 | double left, double right, double gyro, bool control_loop_driving) { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 99 | // Decay the offset quickly because this gyro is great. |
| 100 | _offset = (0.25) * (right - left - gyro * width) / 2.0 + 0.75 * _offset; |
| 101 | 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] | 102 | // TODO(aschuh): Add in the gyro. |
| 103 | _integral_offset = 0.0; |
| 104 | _offset = 0.0; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 105 | _gyro = gyro; |
Austin Schuh | 4352ac6 | 2013-03-19 06:23:16 +0000 | [diff] [blame] | 106 | _control_loop_driving = control_loop_driving; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 107 | SetRawPosition(left, right); |
| 108 | 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); |
| 109 | } |
Austin Schuh | 4352ac6 | 2013-03-19 06:23:16 +0000 | [diff] [blame] | 110 | |
| 111 | void Update(bool update_observer, bool stop_motors) { |
| 112 | loop_->Update(update_observer, stop_motors); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 113 | } |
| 114 | |
Austin Schuh | 4352ac6 | 2013-03-19 06:23:16 +0000 | [diff] [blame] | 115 | void SendMotors(Drivetrain::Output *output) { |
| 116 | if (output) { |
| 117 | output->left_voltage = loop_->U(0, 0); |
| 118 | output->right_voltage = loop_->U(1, 0); |
brians | 8ad7405 | 2013-03-16 21:04:51 +0000 | [diff] [blame] | 119 | } |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 120 | } |
| 121 | void PrintMotors() const { |
| 122 | // 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] | 123 | ::Eigen::Matrix<double, 4, 1> E = loop_->R - loop_->X_hat; |
| 124 | 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] | 125 | } |
| 126 | |
| 127 | private: |
Austin Schuh | 4352ac6 | 2013-03-19 06:23:16 +0000 | [diff] [blame] | 128 | ::std::unique_ptr<StateFeedbackLoop<4, 2, 2>> loop_; |
| 129 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 130 | double _integral_offset; |
| 131 | double _offset; |
| 132 | double _gyro; |
| 133 | double _left_goal; |
| 134 | double _right_goal; |
| 135 | double _raw_left; |
| 136 | double _raw_right; |
Austin Schuh | 4352ac6 | 2013-03-19 06:23:16 +0000 | [diff] [blame] | 137 | bool _control_loop_driving; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 138 | }; |
| 139 | |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 140 | class PolyDrivetrain { |
| 141 | public: |
| 142 | PolyDrivetrain() |
| 143 | : U_Poly_((Eigen::Matrix<double, 4, 2>() << /*[[*/ 1, 0 /*]*/, |
| 144 | /*[*/ -1, 0 /*]*/, |
| 145 | /*[*/ 0, 1 /*]*/, |
| 146 | /*[*/ 0, -1 /*]]*/).finished(), |
| 147 | (Eigen::Matrix<double, 4, 1>() << /*[[*/ 12 /*]*/, |
| 148 | /*[*/ 12 /*]*/, |
| 149 | /*[*/ 12 /*]*/, |
| 150 | /*[*/ 12 /*]]*/).finished()), |
| 151 | loop_(new StateFeedbackLoop<2, 2, 2>(MakeVDrivetrainLoop())) { |
| 152 | |
Brian Silverman | 718b1d7 | 2013-10-28 16:22:45 -0700 | [diff] [blame^] | 153 | ttrust_ = 1.1; |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 154 | |
| 155 | wheel_ = 0.0; |
| 156 | throttle_ = 0.0; |
| 157 | quickturn_ = false; |
| 158 | highgear_ = true; |
| 159 | } |
| 160 | void SetGoal(double wheel, double throttle, bool quickturn, bool highgear) { |
Brian Silverman | 718b1d7 | 2013-10-28 16:22:45 -0700 | [diff] [blame^] | 161 | const double kWheelNonLinearity = 0.4; |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 162 | // Apply a sin function that's scaled to make it feel better. |
| 163 | const double angular_range = M_PI_2 * kWheelNonLinearity; |
| 164 | wheel_ = sin(angular_range * wheel) / sin(angular_range); |
| 165 | wheel_ = sin(angular_range * wheel_) / sin(angular_range); |
| 166 | throttle_ = throttle; |
| 167 | quickturn_ = quickturn; |
| 168 | highgear_ = highgear; |
| 169 | if (highgear_) { |
| 170 | loop_->set_controller_index(3); |
| 171 | } else { |
| 172 | loop_->set_controller_index(0); |
| 173 | } |
| 174 | } |
| 175 | double FilterVelocity(double throttle) { |
| 176 | const Eigen::Matrix<double, 2, 2> FF = |
| 177 | loop_->B().inverse() * |
| 178 | (Eigen::Matrix<double, 2, 2>::Identity() - loop_->A()); |
| 179 | |
| 180 | constexpr int kHighGearController = 3; |
| 181 | const Eigen::Matrix<double, 2, 2> FF_high = |
| 182 | loop_->controller(kHighGearController).plant.B.inverse() * |
| 183 | (Eigen::Matrix<double, 2, 2>::Identity() - |
| 184 | loop_->controller(kHighGearController).plant.A); |
| 185 | |
| 186 | ::Eigen::Matrix<double, 1, 2> FF_sum = FF.colwise().sum(); |
| 187 | int min_FF_sum_index; |
| 188 | const double min_FF_sum = FF_sum.minCoeff(&min_FF_sum_index); |
| 189 | const double min_K_sum = loop_->K().col(min_FF_sum_index).sum(); |
| 190 | const double high_min_FF_sum = FF_high.col(0).sum(); |
| 191 | |
| 192 | const double adjusted_ff_voltage = ::aos::Clip( |
| 193 | throttle * 12.0 * min_FF_sum / high_min_FF_sum, -12.0, 12.0); |
| 194 | return ((adjusted_ff_voltage + |
| 195 | ttrust_ * min_K_sum * (loop_->X_hat(0, 0) + loop_->X_hat(1, 0)) / |
| 196 | 2.0) / |
| 197 | (ttrust_ * min_K_sum + min_FF_sum)); |
| 198 | } |
| 199 | |
Brian Silverman | 718b1d7 | 2013-10-28 16:22:45 -0700 | [diff] [blame^] | 200 | double MaxVelocity() { |
| 201 | const Eigen::Matrix<double, 2, 2> FF = |
| 202 | loop_->B().inverse() * |
| 203 | (Eigen::Matrix<double, 2, 2>::Identity() - loop_->A()); |
| 204 | |
| 205 | constexpr int kHighGearController = 3; |
| 206 | const Eigen::Matrix<double, 2, 2> FF_high = |
| 207 | loop_->controller(kHighGearController).plant.B.inverse() * |
| 208 | (Eigen::Matrix<double, 2, 2>::Identity() - |
| 209 | loop_->controller(kHighGearController).plant.A); |
| 210 | |
| 211 | ::Eigen::Matrix<double, 1, 2> FF_sum = FF.colwise().sum(); |
| 212 | int min_FF_sum_index; |
| 213 | const double min_FF_sum = FF_sum.minCoeff(&min_FF_sum_index); |
| 214 | //const double min_K_sum = loop_->K().col(min_FF_sum_index).sum(); |
| 215 | const double high_min_FF_sum = FF_high.col(0).sum(); |
| 216 | |
| 217 | const double adjusted_ff_voltage = ::aos::Clip( |
| 218 | 12.0 * min_FF_sum / high_min_FF_sum, -12.0, 12.0); |
| 219 | return adjusted_ff_voltage / min_FF_sum; |
| 220 | } |
| 221 | |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 222 | void Update() { |
| 223 | // FF * X = U (steady state) |
| 224 | const Eigen::Matrix<double, 2, 2> FF = |
| 225 | loop_->B().inverse() * |
| 226 | (Eigen::Matrix<double, 2, 2>::Identity() - loop_->A()); |
| 227 | |
| 228 | // Invert the plant to figure out how the velocity filter would have to work |
| 229 | // out in order to filter out the forwards negative inertia. |
| 230 | // This math assumes that the left and right power and velocity are equals, |
| 231 | // and that the plant is the same on the left and right. |
| 232 | const double fvel = FilterVelocity(throttle_); |
| 233 | |
| 234 | const double sign_svel = wheel_ * ((fvel > 0.0) ? 1.0 : -1.0); |
Brian Silverman | 718b1d7 | 2013-10-28 16:22:45 -0700 | [diff] [blame^] | 235 | double steering_velocity; |
| 236 | if (quickturn_) { |
| 237 | steering_velocity = wheel_ * MaxVelocity(); |
| 238 | } else { |
| 239 | steering_velocity = ::std::abs(fvel) * wheel_; |
| 240 | } |
| 241 | const double left_velocity = fvel - steering_velocity; |
| 242 | const double right_velocity = fvel + steering_velocity; |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 243 | |
| 244 | // Integrate velocity to get the position. |
| 245 | // This position is used to get integral control. |
| 246 | loop_->R << left_velocity, right_velocity; |
| 247 | |
Brian Silverman | 718b1d7 | 2013-10-28 16:22:45 -0700 | [diff] [blame^] | 248 | if (!quickturn_) { |
| 249 | // K * R = w |
| 250 | Eigen::Matrix<double,1,2> equality_k; |
| 251 | equality_k << 1 + sign_svel, -(1 - sign_svel); |
| 252 | const double equality_w = 0.0; |
| 253 | |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 254 | // Construct a constraint on R by manipulating the constraint on U |
| 255 | ::aos::controls::HPolytope<2> R_poly = ::aos::controls::HPolytope<2>( |
| 256 | U_Poly_.H() * (loop_->K() + FF), |
| 257 | U_Poly_.k() + U_Poly_.H() * loop_->K() * loop_->X_hat); |
| 258 | |
| 259 | // Limit R back inside the box. |
Brian Silverman | 718b1d7 | 2013-10-28 16:22:45 -0700 | [diff] [blame^] | 260 | loop_->R = |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 261 | CoerceGoal(R_poly, equality_k, equality_w, loop_->R); |
Brian Silverman | 718b1d7 | 2013-10-28 16:22:45 -0700 | [diff] [blame^] | 262 | } |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 263 | |
Brian Silverman | 718b1d7 | 2013-10-28 16:22:45 -0700 | [diff] [blame^] | 264 | const Eigen::Matrix<double, 2, 1> FF_volts = FF * loop_->R; |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 265 | const Eigen::Matrix<double, 2, 1> U_ideal = |
Brian Silverman | 718b1d7 | 2013-10-28 16:22:45 -0700 | [diff] [blame^] | 266 | loop_->K() * (loop_->R - loop_->X_hat) + FF_volts; |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 267 | |
| 268 | for (int i = 0; i < 2; i++) { |
| 269 | loop_->U[i] = ::aos::Clip(U_ideal[i], -12, 12); |
| 270 | } |
| 271 | |
| 272 | // TODO(austin): Feed back? |
| 273 | loop_->X_hat = loop_->A() * loop_->X_hat + loop_->B() * loop_->U; |
| 274 | } |
| 275 | |
| 276 | void SendMotors(Drivetrain::Output *output) { |
| 277 | LOG(DEBUG, "left pwm: %f right pwm: %f wheel: %f throttle: %f\n", |
| 278 | loop_->U(0, 0), loop_->U(1, 0), wheel_, throttle_); |
| 279 | output->left_voltage = loop_->U(0, 0); |
| 280 | output->right_voltage = loop_->U(1, 0); |
| 281 | if (highgear_) { |
| 282 | shifters.MakeWithBuilder().set(false).Send(); |
| 283 | } else { |
| 284 | shifters.MakeWithBuilder().set(true).Send(); |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | private: |
| 289 | const ::aos::controls::HPolytope<2> U_Poly_; |
| 290 | |
| 291 | ::std::unique_ptr<StateFeedbackLoop<2, 2, 2>> loop_; |
| 292 | |
| 293 | double ttrust_; |
| 294 | double wheel_; |
| 295 | double throttle_; |
| 296 | bool quickturn_; |
| 297 | bool highgear_; |
| 298 | }; |
| 299 | |
| 300 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 301 | class DrivetrainMotorsOL { |
| 302 | public: |
| 303 | DrivetrainMotorsOL() { |
| 304 | _old_wheel = 0.0; |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 305 | wheel_ = 0.0; |
| 306 | throttle_ = 0.0; |
| 307 | quickturn_ = false; |
| 308 | highgear_ = true; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 309 | _neg_inertia_accumulator = 0.0; |
| 310 | _left_pwm = 0.0; |
| 311 | _right_pwm = 0.0; |
| 312 | } |
| 313 | void SetGoal(double wheel, double throttle, bool quickturn, bool highgear) { |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 314 | wheel_ = wheel; |
| 315 | throttle_ = throttle; |
| 316 | quickturn_ = quickturn; |
| 317 | highgear_ = highgear; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 318 | _left_pwm = 0.0; |
| 319 | _right_pwm = 0.0; |
| 320 | } |
| 321 | void Update(void) { |
| 322 | double overPower; |
| 323 | float sensitivity = 1.7; |
| 324 | float angular_power; |
| 325 | float linear_power; |
| 326 | double wheel; |
| 327 | |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 328 | double neg_inertia = wheel_ - _old_wheel; |
| 329 | _old_wheel = wheel_; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 330 | |
| 331 | double wheelNonLinearity; |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 332 | if (highgear_) { |
Brian Silverman | 77a7600 | 2013-03-16 20:09:00 -0700 | [diff] [blame] | 333 | wheelNonLinearity = 0.1; // used to be csvReader->TURN_NONLIN_HIGH |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 334 | // Apply a sin function that's scaled to make it feel better. |
| 335 | const double angular_range = M_PI / 2.0 * wheelNonLinearity; |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 336 | wheel = sin(angular_range * wheel_) / sin(angular_range); |
Brian Silverman | 77a7600 | 2013-03-16 20:09:00 -0700 | [diff] [blame] | 337 | wheel = sin(angular_range * wheel) / sin(angular_range); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 338 | } else { |
Brian Silverman | df43ec2 | 2013-03-16 23:48:29 -0700 | [diff] [blame] | 339 | wheelNonLinearity = 0.2; // used to be csvReader->TURN_NONLIN_LOW |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 340 | // Apply a sin function that's scaled to make it feel better. |
| 341 | const double angular_range = M_PI / 2.0 * wheelNonLinearity; |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 342 | wheel = sin(angular_range * wheel_) / sin(angular_range); |
Brian Silverman | 77a7600 | 2013-03-16 20:09:00 -0700 | [diff] [blame] | 343 | wheel = sin(angular_range * wheel) / sin(angular_range); |
| 344 | wheel = sin(angular_range * wheel) / sin(angular_range); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 345 | } |
| 346 | |
Brian Silverman | df43ec2 | 2013-03-16 23:48:29 -0700 | [diff] [blame] | 347 | static const double kThrottleDeadband = 0.05; |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 348 | if (::std::abs(throttle_) < kThrottleDeadband) { |
| 349 | throttle_ = 0; |
Brian Silverman | df43ec2 | 2013-03-16 23:48:29 -0700 | [diff] [blame] | 350 | } else { |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 351 | throttle_ = copysign((::std::abs(throttle_) - kThrottleDeadband) / |
| 352 | (1.0 - kThrottleDeadband), throttle_); |
Brian Silverman | df43ec2 | 2013-03-16 23:48:29 -0700 | [diff] [blame] | 353 | } |
| 354 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 355 | double neg_inertia_scalar; |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 356 | if (highgear_) { |
Brian Silverman | df43ec2 | 2013-03-16 23:48:29 -0700 | [diff] [blame] | 357 | neg_inertia_scalar = 8.0; // used to be csvReader->NEG_INTERTIA_HIGH |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 358 | sensitivity = 1.22; // used to be csvReader->SENSE_HIGH |
| 359 | } else { |
| 360 | if (wheel * neg_inertia > 0) { |
Brian Silverman | df43ec2 | 2013-03-16 23:48:29 -0700 | [diff] [blame] | 361 | neg_inertia_scalar = 5; // used to be csvReader->NEG_INERTIA_LOW_MORE |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 362 | } else { |
Brian Silverman | df43ec2 | 2013-03-16 23:48:29 -0700 | [diff] [blame] | 363 | if (::std::abs(wheel) > 0.65) { |
| 364 | neg_inertia_scalar = 5; // used to be csvReader->NEG_INTERTIA_LOW_LESS_EXT |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 365 | } else { |
Brian Silverman | df43ec2 | 2013-03-16 23:48:29 -0700 | [diff] [blame] | 366 | neg_inertia_scalar = 5; // used to be csvReader->NEG_INTERTIA_LOW_LESS |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 367 | } |
| 368 | } |
| 369 | sensitivity = 1.24; // used to be csvReader->SENSE_LOW |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 370 | } |
| 371 | double neg_inertia_power = neg_inertia * neg_inertia_scalar; |
| 372 | _neg_inertia_accumulator += neg_inertia_power; |
| 373 | |
| 374 | wheel = wheel + _neg_inertia_accumulator; |
| 375 | if (_neg_inertia_accumulator > 1) { |
| 376 | _neg_inertia_accumulator -= 1; |
| 377 | } else if (_neg_inertia_accumulator < -1) { |
| 378 | _neg_inertia_accumulator += 1; |
| 379 | } else { |
| 380 | _neg_inertia_accumulator = 0; |
| 381 | } |
| 382 | |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 383 | linear_power = throttle_; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 384 | |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 385 | if (quickturn_) { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 386 | double qt_angular_power = wheel; |
Brian Silverman | df43ec2 | 2013-03-16 23:48:29 -0700 | [diff] [blame] | 387 | if (::std::abs(linear_power) < 0.2) { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 388 | if (qt_angular_power > 1) qt_angular_power = 1.0; |
| 389 | if (qt_angular_power < -1) qt_angular_power = -1.0; |
| 390 | } else { |
| 391 | qt_angular_power = 0.0; |
| 392 | } |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 393 | overPower = 1.0; |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 394 | if (highgear_) { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 395 | sensitivity = 1.0; |
| 396 | } else { |
| 397 | sensitivity = 1.0; |
| 398 | } |
| 399 | angular_power = wheel; |
| 400 | } else { |
| 401 | overPower = 0.0; |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 402 | angular_power = ::std::abs(throttle_) * wheel * sensitivity; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | _right_pwm = _left_pwm = linear_power; |
| 406 | _left_pwm += angular_power; |
| 407 | _right_pwm -= angular_power; |
| 408 | |
| 409 | if (_left_pwm > 1.0) { |
| 410 | _right_pwm -= overPower*(_left_pwm - 1.0); |
| 411 | _left_pwm = 1.0; |
| 412 | } else if (_right_pwm > 1.0) { |
| 413 | _left_pwm -= overPower*(_right_pwm - 1.0); |
| 414 | _right_pwm = 1.0; |
| 415 | } else if (_left_pwm < -1.0) { |
| 416 | _right_pwm += overPower*(-1.0 - _left_pwm); |
| 417 | _left_pwm = -1.0; |
| 418 | } else if (_right_pwm < -1.0) { |
| 419 | _left_pwm += overPower*(-1.0 - _right_pwm); |
| 420 | _right_pwm = -1.0; |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | void SendMotors(Drivetrain::Output *output) { |
Brian Silverman | df43ec2 | 2013-03-16 23:48:29 -0700 | [diff] [blame] | 425 | 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] | 426 | _left_pwm, _right_pwm, wheel_, throttle_); |
brians | 8ad7405 | 2013-03-16 21:04:51 +0000 | [diff] [blame] | 427 | if (output) { |
| 428 | output->left_voltage = _left_pwm * 12.0; |
| 429 | output->right_voltage = _right_pwm * 12.0; |
| 430 | } |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 431 | if (highgear_) { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 432 | shifters.MakeWithBuilder().set(false).Send(); |
| 433 | } else { |
| 434 | shifters.MakeWithBuilder().set(true).Send(); |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | private: |
| 439 | double _old_wheel; |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 440 | double wheel_; |
| 441 | double throttle_; |
| 442 | bool quickturn_; |
| 443 | bool highgear_; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 444 | double _neg_inertia_accumulator; |
| 445 | double _left_pwm; |
| 446 | double _right_pwm; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 447 | }; |
| 448 | |
| 449 | void DrivetrainLoop::RunIteration(const Drivetrain::Goal *goal, |
| 450 | const Drivetrain::Position *position, |
| 451 | Drivetrain::Output *output, |
| 452 | Drivetrain::Status * /*status*/) { |
| 453 | // TODO(aschuh): These should be members of the class. |
| 454 | static DrivetrainMotorsSS dt_closedloop; |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 455 | static PolyDrivetrain dt_openloop; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 456 | |
| 457 | bool bad_pos = false; |
| 458 | if (position == NULL) { |
James Kuszmaul | 3f35474 | 2013-03-10 17:27:56 -0700 | [diff] [blame] | 459 | LOG(WARNING, "no position\n"); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 460 | bad_pos = true; |
| 461 | } |
| 462 | |
| 463 | double wheel = goal->steering; |
| 464 | double throttle = goal->throttle; |
| 465 | bool quickturn = goal->quickturn; |
| 466 | bool highgear = goal->highgear; |
| 467 | |
| 468 | bool control_loop_driving = goal->control_loop_driving; |
| 469 | double left_goal = goal->left_goal; |
| 470 | double right_goal = goal->right_goal; |
| 471 | |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 472 | dt_closedloop.SetGoal(left_goal, goal->left_velocity_goal, right_goal, |
| 473 | goal->right_velocity_goal); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 474 | if (!bad_pos) { |
| 475 | const double left_encoder = position->left_encoder; |
| 476 | const double right_encoder = position->right_encoder; |
| 477 | if (gyro.FetchLatest()) { |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 478 | dt_closedloop.SetPosition(left_encoder, right_encoder, gyro->angle, |
| 479 | control_loop_driving); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 480 | } else { |
| 481 | dt_closedloop.SetRawPosition(left_encoder, right_encoder); |
| 482 | } |
| 483 | } |
Austin Schuh | 4352ac6 | 2013-03-19 06:23:16 +0000 | [diff] [blame] | 484 | dt_closedloop.Update(position, output == NULL); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 485 | dt_openloop.SetGoal(wheel, throttle, quickturn, highgear); |
| 486 | dt_openloop.Update(); |
Brian Silverman | 2845c4c | 2013-03-16 19:54:08 -0700 | [diff] [blame] | 487 | if (control_loop_driving) { |
| 488 | dt_closedloop.SendMotors(output); |
| 489 | } else { |
| 490 | dt_openloop.SendMotors(output); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 491 | } |
| 492 | } |
| 493 | |
| 494 | } // namespace control_loops |
| 495 | } // namespace frc971 |