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