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