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