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