Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 1 | #include "frc971/control_loops/drivetrain/polydrivetrain.h" |
Austin Schuh | 96ce8ae | 2015-11-26 12:46:02 -0800 | [diff] [blame] | 2 | |
| 3 | #include "aos/common/logging/logging.h" |
| 4 | #include "aos/common/controls/polytope.h" |
| 5 | #include "aos/common/commonmath.h" |
| 6 | #include "aos/common/logging/queue_logging.h" |
| 7 | #include "aos/common/logging/matrix_logging.h" |
| 8 | |
| 9 | #include "aos/common/messages/robot_state.q.h" |
| 10 | #include "frc971/control_loops/state_feedback_loop.h" |
| 11 | #include "frc971/control_loops/coerce_goal.h" |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 12 | #include "frc971/control_loops/drivetrain/drivetrain.q.h" |
| 13 | #include "frc971/control_loops/drivetrain/drivetrain_config.h" |
Austin Schuh | 96ce8ae | 2015-11-26 12:46:02 -0800 | [diff] [blame] | 14 | |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 15 | namespace frc971 { |
Austin Schuh | 96ce8ae | 2015-11-26 12:46:02 -0800 | [diff] [blame] | 16 | namespace control_loops { |
Austin Schuh | 6197a18 | 2015-11-28 16:04:40 -0800 | [diff] [blame] | 17 | namespace drivetrain { |
Austin Schuh | 96ce8ae | 2015-11-26 12:46:02 -0800 | [diff] [blame] | 18 | |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 19 | using ::frc971::control_loops::GearLogging; |
| 20 | using ::frc971::control_loops::CIMLogging; |
Austin Schuh | 6197a18 | 2015-11-28 16:04:40 -0800 | [diff] [blame] | 21 | using ::frc971::control_loops::CoerceGoal; |
Austin Schuh | 96ce8ae | 2015-11-26 12:46:02 -0800 | [diff] [blame] | 22 | |
Austin Schuh | 4156560 | 2016-02-28 20:10:49 -0800 | [diff] [blame] | 23 | PolyDrivetrain::PolyDrivetrain(const DrivetrainConfig &dt_config, |
| 24 | StateFeedbackLoop<7, 2, 3> *kf) |
| 25 | : kf_(kf), |
Austin Schuh | 6613a07 | 2016-01-06 19:54:48 -0800 | [diff] [blame] | 26 | U_Poly_((Eigen::Matrix<double, 4, 2>() << /*[[*/ 1, 0 /*]*/, |
Austin Schuh | 96ce8ae | 2015-11-26 12:46:02 -0800 | [diff] [blame] | 27 | /*[*/ -1, 0 /*]*/, |
| 28 | /*[*/ 0, 1 /*]*/, |
| 29 | /*[*/ 0, -1 /*]]*/).finished(), |
| 30 | (Eigen::Matrix<double, 4, 1>() << /*[[*/ 12 /*]*/, |
| 31 | /*[*/ 12 /*]*/, |
| 32 | /*[*/ 12 /*]*/, |
| 33 | /*[*/ 12 /*]]*/).finished()), |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 34 | loop_(new StateFeedbackLoop<2, 2, 2>(dt_config.make_v_drivetrain_loop())), |
Austin Schuh | 96ce8ae | 2015-11-26 12:46:02 -0800 | [diff] [blame] | 35 | ttrust_(1.1), |
| 36 | wheel_(0.0), |
| 37 | throttle_(0.0), |
| 38 | quickturn_(false), |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 39 | left_gear_(Gear::LOW), |
| 40 | right_gear_(Gear::LOW), |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 41 | counter_(0), |
| 42 | dt_config_(dt_config) { |
Austin Schuh | 96ce8ae | 2015-11-26 12:46:02 -0800 | [diff] [blame] | 43 | last_position_.Zero(); |
| 44 | position_.Zero(); |
| 45 | } |
| 46 | |
| 47 | double PolyDrivetrain::MotorSpeed( |
| 48 | const constants::ShifterHallEffect &hall_effect, double shifter_position, |
Comran Morshed | 2091fe5 | 2016-02-19 17:27:21 +0000 | [diff] [blame] | 49 | double velocity, Gear gear) { |
| 50 | const double high_gear_speed = |
| 51 | velocity / dt_config_.high_gear_ratio / dt_config_.wheel_radius; |
| 52 | const double low_gear_speed = |
| 53 | velocity / dt_config_.low_gear_ratio / dt_config_.wheel_radius; |
Austin Schuh | 96ce8ae | 2015-11-26 12:46:02 -0800 | [diff] [blame] | 54 | |
Comran Morshed | 2091fe5 | 2016-02-19 17:27:21 +0000 | [diff] [blame] | 55 | if (shifter_position < hall_effect.clear_low) { |
| 56 | // We're in low gear, so return speed for that gear. |
| 57 | return low_gear_speed; |
| 58 | } else if (shifter_position > hall_effect.clear_high) { |
| 59 | // We're in high gear, so return speed for that gear. |
| 60 | return high_gear_speed; |
| 61 | } |
| 62 | |
| 63 | // Not in gear, so speed-match to destination gear. |
| 64 | switch (gear) { |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 65 | case Gear::HIGH: |
| 66 | case Gear::SHIFTING_UP: |
Comran Morshed | 2091fe5 | 2016-02-19 17:27:21 +0000 | [diff] [blame] | 67 | return high_gear_speed; |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 68 | case Gear::LOW: |
| 69 | case Gear::SHIFTING_DOWN: |
Austin Schuh | 746fc6d | 2016-02-21 02:53:33 -0800 | [diff] [blame] | 70 | default: |
Comran Morshed | 2091fe5 | 2016-02-19 17:27:21 +0000 | [diff] [blame] | 71 | return low_gear_speed; |
| 72 | break; |
Austin Schuh | 96ce8ae | 2015-11-26 12:46:02 -0800 | [diff] [blame] | 73 | } |
| 74 | } |
| 75 | |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 76 | Gear PolyDrivetrain::UpdateSingleGear(Gear requested_gear, Gear current_gear) { |
Austin Schuh | b9d5e8e | 2015-12-27 14:08:47 -0800 | [diff] [blame] | 77 | const Gear shift_up = |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 78 | (dt_config_.shifter_type == ShifterType::HALL_EFFECT_SHIFTER) |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 79 | ? Gear::SHIFTING_UP |
| 80 | : Gear::HIGH; |
Austin Schuh | b9d5e8e | 2015-12-27 14:08:47 -0800 | [diff] [blame] | 81 | const Gear shift_down = |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 82 | (dt_config_.shifter_type == ShifterType::HALL_EFFECT_SHIFTER) |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 83 | ? Gear::SHIFTING_DOWN |
| 84 | : Gear::LOW; |
Austin Schuh | b9d5e8e | 2015-12-27 14:08:47 -0800 | [diff] [blame] | 85 | if (current_gear != requested_gear) { |
| 86 | if (IsInGear(current_gear)) { |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 87 | if (requested_gear == Gear::HIGH) { |
| 88 | if (current_gear != Gear::HIGH) { |
Austin Schuh | b9d5e8e | 2015-12-27 14:08:47 -0800 | [diff] [blame] | 89 | current_gear = shift_up; |
| 90 | } |
| 91 | } else { |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 92 | if (current_gear != Gear::LOW) { |
Austin Schuh | b9d5e8e | 2015-12-27 14:08:47 -0800 | [diff] [blame] | 93 | current_gear = shift_down; |
| 94 | } |
| 95 | } |
| 96 | } else { |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 97 | if (requested_gear == Gear::HIGH && current_gear == Gear::SHIFTING_DOWN) { |
| 98 | current_gear = Gear::SHIFTING_UP; |
| 99 | } else if (requested_gear == Gear::LOW && |
| 100 | current_gear == Gear::SHIFTING_UP) { |
| 101 | current_gear = Gear::SHIFTING_DOWN; |
Austin Schuh | b9d5e8e | 2015-12-27 14:08:47 -0800 | [diff] [blame] | 102 | } |
| 103 | } |
Austin Schuh | 96ce8ae | 2015-11-26 12:46:02 -0800 | [diff] [blame] | 104 | } |
Austin Schuh | b9d5e8e | 2015-12-27 14:08:47 -0800 | [diff] [blame] | 105 | return current_gear; |
| 106 | } |
| 107 | |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 108 | void PolyDrivetrain::SetGoal( |
| 109 | const ::frc971::control_loops::DrivetrainQueue::Goal &goal) { |
| 110 | const double wheel = goal.steering; |
| 111 | const double throttle = goal.throttle; |
| 112 | const bool quickturn = goal.quickturn; |
| 113 | const bool highgear = goal.highgear; |
Austin Schuh | 96ce8ae | 2015-11-26 12:46:02 -0800 | [diff] [blame] | 114 | |
Austin Schuh | 64f3a43 | 2016-03-13 15:30:27 -0700 | [diff] [blame^] | 115 | const double kWheelNonLinearity = 0.55; |
Austin Schuh | 96ce8ae | 2015-11-26 12:46:02 -0800 | [diff] [blame] | 116 | // Apply a sin function that's scaled to make it feel better. |
| 117 | const double angular_range = M_PI_2 * kWheelNonLinearity; |
| 118 | |
| 119 | wheel_ = sin(angular_range * wheel) / sin(angular_range); |
| 120 | wheel_ = sin(angular_range * wheel_) / sin(angular_range); |
Austin Schuh | 1dbee48 | 2016-02-28 20:12:24 -0800 | [diff] [blame] | 121 | wheel_ = 2.0 * wheel - wheel_; |
Austin Schuh | 96ce8ae | 2015-11-26 12:46:02 -0800 | [diff] [blame] | 122 | quickturn_ = quickturn; |
| 123 | |
| 124 | static const double kThrottleDeadband = 0.05; |
| 125 | if (::std::abs(throttle) < kThrottleDeadband) { |
| 126 | throttle_ = 0; |
| 127 | } else { |
| 128 | throttle_ = copysign( |
| 129 | (::std::abs(throttle) - kThrottleDeadband) / (1.0 - kThrottleDeadband), |
| 130 | throttle); |
| 131 | } |
| 132 | |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 133 | Gear requested_gear = highgear ? Gear::HIGH : Gear::LOW; |
| 134 | |
| 135 | left_gear_ = PolyDrivetrain::UpdateSingleGear(requested_gear, left_gear_); |
| 136 | right_gear_ = PolyDrivetrain::UpdateSingleGear(requested_gear, right_gear_); |
Austin Schuh | 96ce8ae | 2015-11-26 12:46:02 -0800 | [diff] [blame] | 137 | } |
Austin Schuh | b9d5e8e | 2015-12-27 14:08:47 -0800 | [diff] [blame] | 138 | |
Austin Schuh | 6197a18 | 2015-11-28 16:04:40 -0800 | [diff] [blame] | 139 | void PolyDrivetrain::SetPosition( |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 140 | const ::frc971::control_loops::DrivetrainQueue::Position *position, |
| 141 | Gear left_gear, Gear right_gear) { |
| 142 | left_gear_ = left_gear; |
| 143 | right_gear_ = right_gear; |
| 144 | last_position_ = position_; |
| 145 | position_ = *position; |
Austin Schuh | 96ce8ae | 2015-11-26 12:46:02 -0800 | [diff] [blame] | 146 | } |
| 147 | |
Austin Schuh | 4156560 | 2016-02-28 20:10:49 -0800 | [diff] [blame] | 148 | double PolyDrivetrain::FilterVelocity(double throttle) const { |
Austin Schuh | 96ce8ae | 2015-11-26 12:46:02 -0800 | [diff] [blame] | 149 | const Eigen::Matrix<double, 2, 2> FF = |
| 150 | loop_->B().inverse() * |
| 151 | (Eigen::Matrix<double, 2, 2>::Identity() - loop_->A()); |
| 152 | |
| 153 | constexpr int kHighGearController = 3; |
| 154 | const Eigen::Matrix<double, 2, 2> FF_high = |
| 155 | loop_->controller(kHighGearController).plant.B().inverse() * |
| 156 | (Eigen::Matrix<double, 2, 2>::Identity() - |
| 157 | loop_->controller(kHighGearController).plant.A()); |
| 158 | |
| 159 | ::Eigen::Matrix<double, 1, 2> FF_sum = FF.colwise().sum(); |
| 160 | int min_FF_sum_index; |
| 161 | const double min_FF_sum = FF_sum.minCoeff(&min_FF_sum_index); |
| 162 | const double min_K_sum = loop_->K().col(min_FF_sum_index).sum(); |
| 163 | const double high_min_FF_sum = FF_high.col(0).sum(); |
| 164 | |
| 165 | const double adjusted_ff_voltage = |
| 166 | ::aos::Clip(throttle * 12.0 * min_FF_sum / high_min_FF_sum, -12.0, 12.0); |
| 167 | return (adjusted_ff_voltage + |
| 168 | ttrust_ * min_K_sum * (loop_->X_hat(0, 0) + loop_->X_hat(1, 0)) / |
| 169 | 2.0) / |
| 170 | (ttrust_ * min_K_sum + min_FF_sum); |
| 171 | } |
| 172 | |
| 173 | double PolyDrivetrain::MaxVelocity() { |
| 174 | const Eigen::Matrix<double, 2, 2> FF = |
| 175 | loop_->B().inverse() * |
| 176 | (Eigen::Matrix<double, 2, 2>::Identity() - loop_->A()); |
| 177 | |
| 178 | constexpr int kHighGearController = 3; |
| 179 | const Eigen::Matrix<double, 2, 2> FF_high = |
| 180 | loop_->controller(kHighGearController).plant.B().inverse() * |
| 181 | (Eigen::Matrix<double, 2, 2>::Identity() - |
| 182 | loop_->controller(kHighGearController).plant.A()); |
| 183 | |
| 184 | ::Eigen::Matrix<double, 1, 2> FF_sum = FF.colwise().sum(); |
| 185 | int min_FF_sum_index; |
| 186 | const double min_FF_sum = FF_sum.minCoeff(&min_FF_sum_index); |
| 187 | // const double min_K_sum = loop_->K().col(min_FF_sum_index).sum(); |
| 188 | const double high_min_FF_sum = FF_high.col(0).sum(); |
| 189 | |
| 190 | const double adjusted_ff_voltage = |
| 191 | ::aos::Clip(12.0 * min_FF_sum / high_min_FF_sum, -12.0, 12.0); |
| 192 | return adjusted_ff_voltage / min_FF_sum; |
| 193 | } |
| 194 | |
| 195 | void PolyDrivetrain::Update() { |
Comran Morshed | 76ca8f5 | 2016-02-21 17:26:28 +0000 | [diff] [blame] | 196 | if (dt_config_.loop_type == LoopType::CLOSED_LOOP) { |
Austin Schuh | 4156560 | 2016-02-28 20:10:49 -0800 | [diff] [blame] | 197 | loop_->mutable_X_hat()(0, 0) = kf_->X_hat()(1, 0); |
| 198 | loop_->mutable_X_hat()(1, 0) = kf_->X_hat()(3, 0); |
Comran Morshed | 76ca8f5 | 2016-02-21 17:26:28 +0000 | [diff] [blame] | 199 | } |
Austin Schuh | 0520cbf | 2016-01-06 19:58:36 -0800 | [diff] [blame] | 200 | |
Austin Schuh | 96ce8ae | 2015-11-26 12:46:02 -0800 | [diff] [blame] | 201 | // TODO(austin): Observer for the current velocity instead of difference |
| 202 | // calculations. |
| 203 | ++counter_; |
Austin Schuh | 96ce8ae | 2015-11-26 12:46:02 -0800 | [diff] [blame] | 204 | |
Austin Schuh | b9d5e8e | 2015-12-27 14:08:47 -0800 | [diff] [blame] | 205 | if (IsInGear(left_gear_) && IsInGear(right_gear_)) { |
Austin Schuh | 96ce8ae | 2015-11-26 12:46:02 -0800 | [diff] [blame] | 206 | // FF * X = U (steady state) |
| 207 | const Eigen::Matrix<double, 2, 2> FF = |
| 208 | loop_->B().inverse() * |
| 209 | (Eigen::Matrix<double, 2, 2>::Identity() - loop_->A()); |
| 210 | |
| 211 | // Invert the plant to figure out how the velocity filter would have to |
| 212 | // work |
| 213 | // out in order to filter out the forwards negative inertia. |
| 214 | // This math assumes that the left and right power and velocity are |
| 215 | // equals, |
| 216 | // and that the plant is the same on the left and right. |
| 217 | const double fvel = FilterVelocity(throttle_); |
| 218 | |
| 219 | const double sign_svel = wheel_ * ((fvel > 0.0) ? 1.0 : -1.0); |
| 220 | double steering_velocity; |
| 221 | if (quickturn_) { |
| 222 | steering_velocity = wheel_ * MaxVelocity(); |
| 223 | } else { |
| 224 | steering_velocity = ::std::abs(fvel) * wheel_; |
| 225 | } |
| 226 | const double left_velocity = fvel - steering_velocity; |
| 227 | const double right_velocity = fvel + steering_velocity; |
Austin Schuh | 4156560 | 2016-02-28 20:10:49 -0800 | [diff] [blame] | 228 | goal_left_velocity_ = left_velocity; |
| 229 | goal_right_velocity_ = right_velocity; |
Austin Schuh | 96ce8ae | 2015-11-26 12:46:02 -0800 | [diff] [blame] | 230 | |
| 231 | // Integrate velocity to get the position. |
| 232 | // This position is used to get integral control. |
| 233 | loop_->mutable_R() << left_velocity, right_velocity; |
| 234 | |
| 235 | if (!quickturn_) { |
| 236 | // K * R = w |
| 237 | Eigen::Matrix<double, 1, 2> equality_k; |
| 238 | equality_k << 1 + sign_svel, -(1 - sign_svel); |
| 239 | const double equality_w = 0.0; |
| 240 | |
| 241 | // Construct a constraint on R by manipulating the constraint on U |
| 242 | ::aos::controls::HPolytope<2> R_poly = ::aos::controls::HPolytope<2>( |
| 243 | U_Poly_.H() * (loop_->K() + FF), |
| 244 | U_Poly_.k() + U_Poly_.H() * loop_->K() * loop_->X_hat()); |
| 245 | |
| 246 | // Limit R back inside the box. |
| 247 | loop_->mutable_R() = |
| 248 | CoerceGoal(R_poly, equality_k, equality_w, loop_->R()); |
| 249 | } |
| 250 | |
| 251 | const Eigen::Matrix<double, 2, 1> FF_volts = FF * loop_->R(); |
| 252 | const Eigen::Matrix<double, 2, 1> U_ideal = |
| 253 | loop_->K() * (loop_->R() - loop_->X_hat()) + FF_volts; |
| 254 | |
| 255 | for (int i = 0; i < 2; i++) { |
| 256 | loop_->mutable_U()[i] = ::aos::Clip(U_ideal[i], -12, 12); |
| 257 | } |
Comran Morshed | 76ca8f5 | 2016-02-21 17:26:28 +0000 | [diff] [blame] | 258 | |
| 259 | if (dt_config_.loop_type == LoopType::OPEN_LOOP) { |
| 260 | loop_->mutable_X_hat() = |
| 261 | loop_->A() * loop_->X_hat() + loop_->B() * loop_->U(); |
| 262 | } |
Austin Schuh | 96ce8ae | 2015-11-26 12:46:02 -0800 | [diff] [blame] | 263 | } else { |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 264 | const double current_left_velocity = |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 265 | (position_.left_encoder - last_position_.left_encoder) / dt_config_.dt; |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 266 | const double current_right_velocity = |
| 267 | (position_.right_encoder - last_position_.right_encoder) / |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 268 | dt_config_.dt; |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 269 | const double left_motor_speed = |
| 270 | MotorSpeed(dt_config_.left_drive, position_.left_shifter_position, |
Comran Morshed | 2091fe5 | 2016-02-19 17:27:21 +0000 | [diff] [blame] | 271 | current_left_velocity, left_gear_); |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 272 | const double right_motor_speed = |
| 273 | MotorSpeed(dt_config_.right_drive, position_.right_shifter_position, |
Comran Morshed | 2091fe5 | 2016-02-19 17:27:21 +0000 | [diff] [blame] | 274 | current_right_velocity, right_gear_); |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 275 | |
| 276 | { |
| 277 | CIMLogging logging; |
| 278 | |
| 279 | // Reset the CIM model to the current conditions to be ready for when we |
| 280 | // shift. |
Comran Morshed | 2091fe5 | 2016-02-19 17:27:21 +0000 | [diff] [blame] | 281 | logging.left_in_gear = IsInGear(left_gear_); |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 282 | logging.left_motor_speed = left_motor_speed; |
| 283 | logging.left_velocity = current_left_velocity; |
Comran Morshed | 2091fe5 | 2016-02-19 17:27:21 +0000 | [diff] [blame] | 284 | |
| 285 | logging.right_in_gear = IsInGear(right_gear_); |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 286 | logging.right_motor_speed = right_motor_speed; |
| 287 | logging.right_velocity = current_right_velocity; |
| 288 | |
| 289 | LOG_STRUCT(DEBUG, "currently", logging); |
| 290 | } |
Austin Schuh | 4156560 | 2016-02-28 20:10:49 -0800 | [diff] [blame] | 291 | goal_left_velocity_ = current_left_velocity; |
| 292 | goal_right_velocity_ = current_right_velocity; |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 293 | |
Comran Morshed | 2091fe5 | 2016-02-19 17:27:21 +0000 | [diff] [blame] | 294 | // Any motor is not in gear. Speed match. |
Austin Schuh | 96ce8ae | 2015-11-26 12:46:02 -0800 | [diff] [blame] | 295 | ::Eigen::Matrix<double, 1, 1> R_left; |
| 296 | ::Eigen::Matrix<double, 1, 1> R_right; |
| 297 | R_left(0, 0) = left_motor_speed; |
| 298 | R_right(0, 0) = right_motor_speed; |
| 299 | |
| 300 | const double wiggle = |
| 301 | (static_cast<double>((counter_ % 20) / 10) - 0.5) * 5.0; |
| 302 | |
| 303 | loop_->mutable_U(0, 0) = ::aos::Clip( |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 304 | (R_left / dt_config_.v)(0, 0) + (IsInGear(left_gear_) ? 0 : wiggle), |
| 305 | -12.0, 12.0); |
| 306 | loop_->mutable_U(1, 0) = ::aos::Clip( |
| 307 | (R_right / dt_config_.v)(0, 0) + (IsInGear(right_gear_) ? 0 : wiggle), |
| 308 | -12.0, 12.0); |
Austin Schuh | 96ce8ae | 2015-11-26 12:46:02 -0800 | [diff] [blame] | 309 | loop_->mutable_U() *= 12.0 / ::aos::robot_state->voltage_battery; |
Austin Schuh | 96ce8ae | 2015-11-26 12:46:02 -0800 | [diff] [blame] | 310 | } |
| 311 | } |
| 312 | |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 313 | void PolyDrivetrain::SetOutput( |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 314 | ::frc971::control_loops::DrivetrainQueue::Output *output) { |
Austin Schuh | 96ce8ae | 2015-11-26 12:46:02 -0800 | [diff] [blame] | 315 | if (output != NULL) { |
| 316 | output->left_voltage = loop_->U(0, 0); |
| 317 | output->right_voltage = loop_->U(1, 0); |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 318 | output->left_high = MaybeHigh(left_gear_); |
| 319 | output->right_high = MaybeHigh(right_gear_); |
Austin Schuh | 96ce8ae | 2015-11-26 12:46:02 -0800 | [diff] [blame] | 320 | } |
| 321 | } |
| 322 | |
Austin Schuh | 4156560 | 2016-02-28 20:10:49 -0800 | [diff] [blame] | 323 | void PolyDrivetrain::PopulateStatus( |
| 324 | ::frc971::control_loops::DrivetrainQueue::Status *status) { |
| 325 | status->left_velocity_goal = goal_left_velocity_; |
| 326 | status->right_velocity_goal = goal_right_velocity_; |
| 327 | } |
| 328 | |
Austin Schuh | 6197a18 | 2015-11-28 16:04:40 -0800 | [diff] [blame] | 329 | } // namespace drivetrain |
Austin Schuh | 96ce8ae | 2015-11-26 12:46:02 -0800 | [diff] [blame] | 330 | } // namespace control_loops |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 331 | } // namespace frc971 |