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