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