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 | |
| 153 | ttrust_ = 1.0; |
| 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) { |
| 161 | const double kWheelNonLinearity = 0.1; |
| 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 | |
| 200 | void Update() { |
| 201 | // FF * X = U (steady state) |
| 202 | const Eigen::Matrix<double, 2, 2> FF = |
| 203 | loop_->B().inverse() * |
| 204 | (Eigen::Matrix<double, 2, 2>::Identity() - loop_->A()); |
| 205 | |
| 206 | // Invert the plant to figure out how the velocity filter would have to work |
| 207 | // out in order to filter out the forwards negative inertia. |
| 208 | // This math assumes that the left and right power and velocity are equals, |
| 209 | // and that the plant is the same on the left and right. |
| 210 | const double fvel = FilterVelocity(throttle_); |
| 211 | |
| 212 | const double sign_svel = wheel_ * ((fvel > 0.0) ? 1.0 : -1.0); |
| 213 | const double svel = ::std::abs(fvel) * wheel_; |
| 214 | const double left_velocity = fvel - svel; |
| 215 | const double right_velocity = fvel + svel; |
| 216 | |
| 217 | // K * R = w |
| 218 | Eigen::Matrix<double,1,2> equality_k; |
| 219 | equality_k << 1 + sign_svel, -(1 - sign_svel); |
| 220 | const double equality_w = 0.0; |
| 221 | |
| 222 | // Integrate velocity to get the position. |
| 223 | // This position is used to get integral control. |
| 224 | loop_->R << left_velocity, right_velocity; |
| 225 | |
| 226 | // Construct a constraint on R by manipulating the constraint on U |
| 227 | ::aos::controls::HPolytope<2> R_poly = ::aos::controls::HPolytope<2>( |
| 228 | U_Poly_.H() * (loop_->K() + FF), |
| 229 | U_Poly_.k() + U_Poly_.H() * loop_->K() * loop_->X_hat); |
| 230 | |
| 231 | // Limit R back inside the box. |
| 232 | const Eigen::Matrix<double, 2, 1> boxed_R = |
| 233 | CoerceGoal(R_poly, equality_k, equality_w, loop_->R); |
| 234 | |
| 235 | const Eigen::Matrix<double, 2, 1> FF_volts = FF * boxed_R; |
| 236 | const Eigen::Matrix<double, 2, 1> U_ideal = |
| 237 | loop_->K() * (boxed_R - loop_->X_hat) + FF_volts; |
| 238 | |
| 239 | for (int i = 0; i < 2; i++) { |
| 240 | loop_->U[i] = ::aos::Clip(U_ideal[i], -12, 12); |
| 241 | } |
| 242 | |
| 243 | // TODO(austin): Feed back? |
| 244 | loop_->X_hat = loop_->A() * loop_->X_hat + loop_->B() * loop_->U; |
| 245 | } |
| 246 | |
| 247 | void SendMotors(Drivetrain::Output *output) { |
| 248 | LOG(DEBUG, "left pwm: %f right pwm: %f wheel: %f throttle: %f\n", |
| 249 | loop_->U(0, 0), loop_->U(1, 0), wheel_, throttle_); |
| 250 | output->left_voltage = loop_->U(0, 0); |
| 251 | output->right_voltage = loop_->U(1, 0); |
| 252 | if (highgear_) { |
| 253 | shifters.MakeWithBuilder().set(false).Send(); |
| 254 | } else { |
| 255 | shifters.MakeWithBuilder().set(true).Send(); |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | private: |
| 260 | const ::aos::controls::HPolytope<2> U_Poly_; |
| 261 | |
| 262 | ::std::unique_ptr<StateFeedbackLoop<2, 2, 2>> loop_; |
| 263 | |
| 264 | double ttrust_; |
| 265 | double wheel_; |
| 266 | double throttle_; |
| 267 | bool quickturn_; |
| 268 | bool highgear_; |
| 269 | }; |
| 270 | |
| 271 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 272 | class DrivetrainMotorsOL { |
| 273 | public: |
| 274 | DrivetrainMotorsOL() { |
| 275 | _old_wheel = 0.0; |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 276 | wheel_ = 0.0; |
| 277 | throttle_ = 0.0; |
| 278 | quickturn_ = false; |
| 279 | highgear_ = true; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 280 | _neg_inertia_accumulator = 0.0; |
| 281 | _left_pwm = 0.0; |
| 282 | _right_pwm = 0.0; |
| 283 | } |
| 284 | void SetGoal(double wheel, double throttle, bool quickturn, bool highgear) { |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 285 | wheel_ = wheel; |
| 286 | throttle_ = throttle; |
| 287 | quickturn_ = quickturn; |
| 288 | highgear_ = highgear; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 289 | _left_pwm = 0.0; |
| 290 | _right_pwm = 0.0; |
| 291 | } |
| 292 | void Update(void) { |
| 293 | double overPower; |
| 294 | float sensitivity = 1.7; |
| 295 | float angular_power; |
| 296 | float linear_power; |
| 297 | double wheel; |
| 298 | |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 299 | double neg_inertia = wheel_ - _old_wheel; |
| 300 | _old_wheel = wheel_; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 301 | |
| 302 | double wheelNonLinearity; |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 303 | if (highgear_) { |
Brian Silverman | 77a7600 | 2013-03-16 20:09:00 -0700 | [diff] [blame] | 304 | wheelNonLinearity = 0.1; // used to be csvReader->TURN_NONLIN_HIGH |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 305 | // Apply a sin function that's scaled to make it feel better. |
| 306 | const double angular_range = M_PI / 2.0 * wheelNonLinearity; |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 307 | wheel = sin(angular_range * wheel_) / sin(angular_range); |
Brian Silverman | 77a7600 | 2013-03-16 20:09:00 -0700 | [diff] [blame] | 308 | wheel = sin(angular_range * wheel) / sin(angular_range); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 309 | } else { |
Brian Silverman | df43ec2 | 2013-03-16 23:48:29 -0700 | [diff] [blame] | 310 | wheelNonLinearity = 0.2; // used to be csvReader->TURN_NONLIN_LOW |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 311 | // Apply a sin function that's scaled to make it feel better. |
| 312 | const double angular_range = M_PI / 2.0 * wheelNonLinearity; |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 313 | wheel = sin(angular_range * wheel_) / sin(angular_range); |
Brian Silverman | 77a7600 | 2013-03-16 20:09:00 -0700 | [diff] [blame] | 314 | wheel = sin(angular_range * wheel) / sin(angular_range); |
| 315 | wheel = sin(angular_range * wheel) / sin(angular_range); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 316 | } |
| 317 | |
Brian Silverman | df43ec2 | 2013-03-16 23:48:29 -0700 | [diff] [blame] | 318 | static const double kThrottleDeadband = 0.05; |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 319 | if (::std::abs(throttle_) < kThrottleDeadband) { |
| 320 | throttle_ = 0; |
Brian Silverman | df43ec2 | 2013-03-16 23:48:29 -0700 | [diff] [blame] | 321 | } else { |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 322 | throttle_ = copysign((::std::abs(throttle_) - kThrottleDeadband) / |
| 323 | (1.0 - kThrottleDeadband), throttle_); |
Brian Silverman | df43ec2 | 2013-03-16 23:48:29 -0700 | [diff] [blame] | 324 | } |
| 325 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 326 | double neg_inertia_scalar; |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 327 | if (highgear_) { |
Brian Silverman | df43ec2 | 2013-03-16 23:48:29 -0700 | [diff] [blame] | 328 | neg_inertia_scalar = 8.0; // used to be csvReader->NEG_INTERTIA_HIGH |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 329 | sensitivity = 1.22; // used to be csvReader->SENSE_HIGH |
| 330 | } else { |
| 331 | if (wheel * neg_inertia > 0) { |
Brian Silverman | df43ec2 | 2013-03-16 23:48:29 -0700 | [diff] [blame] | 332 | neg_inertia_scalar = 5; // used to be csvReader->NEG_INERTIA_LOW_MORE |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 333 | } else { |
Brian Silverman | df43ec2 | 2013-03-16 23:48:29 -0700 | [diff] [blame] | 334 | if (::std::abs(wheel) > 0.65) { |
| 335 | neg_inertia_scalar = 5; // used to be csvReader->NEG_INTERTIA_LOW_LESS_EXT |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 336 | } else { |
Brian Silverman | df43ec2 | 2013-03-16 23:48:29 -0700 | [diff] [blame] | 337 | neg_inertia_scalar = 5; // used to be csvReader->NEG_INTERTIA_LOW_LESS |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 338 | } |
| 339 | } |
| 340 | sensitivity = 1.24; // used to be csvReader->SENSE_LOW |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 341 | } |
| 342 | double neg_inertia_power = neg_inertia * neg_inertia_scalar; |
| 343 | _neg_inertia_accumulator += neg_inertia_power; |
| 344 | |
| 345 | wheel = wheel + _neg_inertia_accumulator; |
| 346 | if (_neg_inertia_accumulator > 1) { |
| 347 | _neg_inertia_accumulator -= 1; |
| 348 | } else if (_neg_inertia_accumulator < -1) { |
| 349 | _neg_inertia_accumulator += 1; |
| 350 | } else { |
| 351 | _neg_inertia_accumulator = 0; |
| 352 | } |
| 353 | |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 354 | linear_power = throttle_; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 355 | |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 356 | if (quickturn_) { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 357 | double qt_angular_power = wheel; |
Brian Silverman | df43ec2 | 2013-03-16 23:48:29 -0700 | [diff] [blame] | 358 | if (::std::abs(linear_power) < 0.2) { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 359 | if (qt_angular_power > 1) qt_angular_power = 1.0; |
| 360 | if (qt_angular_power < -1) qt_angular_power = -1.0; |
| 361 | } else { |
| 362 | qt_angular_power = 0.0; |
| 363 | } |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 364 | overPower = 1.0; |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 365 | if (highgear_) { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 366 | sensitivity = 1.0; |
| 367 | } else { |
| 368 | sensitivity = 1.0; |
| 369 | } |
| 370 | angular_power = wheel; |
| 371 | } else { |
| 372 | overPower = 0.0; |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 373 | angular_power = ::std::abs(throttle_) * wheel * sensitivity; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | _right_pwm = _left_pwm = linear_power; |
| 377 | _left_pwm += angular_power; |
| 378 | _right_pwm -= angular_power; |
| 379 | |
| 380 | if (_left_pwm > 1.0) { |
| 381 | _right_pwm -= overPower*(_left_pwm - 1.0); |
| 382 | _left_pwm = 1.0; |
| 383 | } else if (_right_pwm > 1.0) { |
| 384 | _left_pwm -= overPower*(_right_pwm - 1.0); |
| 385 | _right_pwm = 1.0; |
| 386 | } else if (_left_pwm < -1.0) { |
| 387 | _right_pwm += overPower*(-1.0 - _left_pwm); |
| 388 | _left_pwm = -1.0; |
| 389 | } else if (_right_pwm < -1.0) { |
| 390 | _left_pwm += overPower*(-1.0 - _right_pwm); |
| 391 | _right_pwm = -1.0; |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | void SendMotors(Drivetrain::Output *output) { |
Brian Silverman | df43ec2 | 2013-03-16 23:48:29 -0700 | [diff] [blame] | 396 | 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] | 397 | _left_pwm, _right_pwm, wheel_, throttle_); |
brians | 8ad7405 | 2013-03-16 21:04:51 +0000 | [diff] [blame] | 398 | if (output) { |
| 399 | output->left_voltage = _left_pwm * 12.0; |
| 400 | output->right_voltage = _right_pwm * 12.0; |
| 401 | } |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 402 | if (highgear_) { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 403 | shifters.MakeWithBuilder().set(false).Send(); |
| 404 | } else { |
| 405 | shifters.MakeWithBuilder().set(true).Send(); |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | private: |
| 410 | double _old_wheel; |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 411 | double wheel_; |
| 412 | double throttle_; |
| 413 | bool quickturn_; |
| 414 | bool highgear_; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 415 | double _neg_inertia_accumulator; |
| 416 | double _left_pwm; |
| 417 | double _right_pwm; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 418 | }; |
| 419 | |
| 420 | void DrivetrainLoop::RunIteration(const Drivetrain::Goal *goal, |
| 421 | const Drivetrain::Position *position, |
| 422 | Drivetrain::Output *output, |
| 423 | Drivetrain::Status * /*status*/) { |
| 424 | // TODO(aschuh): These should be members of the class. |
| 425 | static DrivetrainMotorsSS dt_closedloop; |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 426 | static PolyDrivetrain dt_openloop; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 427 | |
| 428 | bool bad_pos = false; |
| 429 | if (position == NULL) { |
James Kuszmaul | 3f35474 | 2013-03-10 17:27:56 -0700 | [diff] [blame] | 430 | LOG(WARNING, "no position\n"); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 431 | bad_pos = true; |
| 432 | } |
| 433 | |
| 434 | double wheel = goal->steering; |
| 435 | double throttle = goal->throttle; |
| 436 | bool quickturn = goal->quickturn; |
| 437 | bool highgear = goal->highgear; |
| 438 | |
| 439 | bool control_loop_driving = goal->control_loop_driving; |
| 440 | double left_goal = goal->left_goal; |
| 441 | double right_goal = goal->right_goal; |
| 442 | |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 443 | dt_closedloop.SetGoal(left_goal, goal->left_velocity_goal, right_goal, |
| 444 | goal->right_velocity_goal); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 445 | if (!bad_pos) { |
| 446 | const double left_encoder = position->left_encoder; |
| 447 | const double right_encoder = position->right_encoder; |
| 448 | if (gyro.FetchLatest()) { |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 449 | dt_closedloop.SetPosition(left_encoder, right_encoder, gyro->angle, |
| 450 | control_loop_driving); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 451 | } else { |
| 452 | dt_closedloop.SetRawPosition(left_encoder, right_encoder); |
| 453 | } |
| 454 | } |
Austin Schuh | 4352ac6 | 2013-03-19 06:23:16 +0000 | [diff] [blame] | 455 | dt_closedloop.Update(position, output == NULL); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 456 | dt_openloop.SetGoal(wheel, throttle, quickturn, highgear); |
| 457 | dt_openloop.Update(); |
Brian Silverman | 2845c4c | 2013-03-16 19:54:08 -0700 | [diff] [blame] | 458 | if (control_loop_driving) { |
| 459 | dt_closedloop.SendMotors(output); |
| 460 | } else { |
| 461 | dt_openloop.SendMotors(output); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 462 | } |
| 463 | } |
| 464 | |
| 465 | } // namespace control_loops |
| 466 | } // namespace frc971 |