Campbell Crowley | 71b5f13 | 2017-02-18 13:16:08 -0800 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <string.h> |
| 3 | #include <unistd.h> |
| 4 | #include <math.h> |
| 5 | |
Austin Schuh | 3028b1d | 2017-03-11 22:12:13 -0800 | [diff] [blame] | 6 | #include "aos/common/actions/actions.h" |
Campbell Crowley | 71b5f13 | 2017-02-18 13:16:08 -0800 | [diff] [blame] | 7 | #include "aos/common/input/driver_station_data.h" |
| 8 | #include "aos/common/logging/logging.h" |
Campbell Crowley | 71b5f13 | 2017-02-18 13:16:08 -0800 | [diff] [blame] | 9 | #include "aos/common/time.h" |
Austin Schuh | 3028b1d | 2017-03-11 22:12:13 -0800 | [diff] [blame] | 10 | #include "aos/common/util/log_interval.h" |
| 11 | #include "aos/input/joystick_input.h" |
| 12 | #include "aos/linux_code/init.h" |
Campbell Crowley | 71b5f13 | 2017-02-18 13:16:08 -0800 | [diff] [blame] | 13 | #include "frc971/autonomous/auto.q.h" |
Austin Schuh | 3028b1d | 2017-03-11 22:12:13 -0800 | [diff] [blame] | 14 | #include "frc971/autonomous/base_autonomous_actor.h" |
| 15 | #include "frc971/control_loops/drivetrain/drivetrain.q.h" |
| 16 | #include "y2017/constants.h" |
| 17 | #include "y2017/control_loops/superstructure/superstructure.q.h" |
Campbell Crowley | 71b5f13 | 2017-02-18 13:16:08 -0800 | [diff] [blame] | 18 | |
| 19 | using ::frc971::control_loops::drivetrain_queue; |
| 20 | using ::y2017::control_loops::superstructure_queue; |
| 21 | |
| 22 | using ::aos::input::driver_station::ButtonLocation; |
| 23 | using ::aos::input::driver_station::ControlBit; |
| 24 | using ::aos::input::driver_station::JoystickAxis; |
| 25 | using ::aos::input::driver_station::POVLocation; |
| 26 | |
| 27 | namespace y2017 { |
| 28 | namespace input { |
| 29 | namespace joysticks { |
| 30 | |
Austin Schuh | 51b1bae | 2017-04-09 18:31:57 -0700 | [diff] [blame] | 31 | // Keep the other versions around so we can switch quickly. |
| 32 | //#define STEERINGWHEEL |
| 33 | #define PISTOL |
| 34 | //#define XBOX |
| 35 | |
| 36 | #ifdef STEERINGWHEEL |
Campbell Crowley | 71b5f13 | 2017-02-18 13:16:08 -0800 | [diff] [blame] | 37 | const JoystickAxis kSteeringWheel(1, 1), kDriveThrottle(2, 2); |
| 38 | const ButtonLocation kQuickTurn(1, 5); |
Campbell Crowley | 71b5f13 | 2017-02-18 13:16:08 -0800 | [diff] [blame] | 39 | const ButtonLocation kTurn1(1, 7); |
| 40 | const ButtonLocation kTurn2(1, 11); |
| 41 | |
Austin Schuh | 51b1bae | 2017-04-09 18:31:57 -0700 | [diff] [blame] | 42 | #endif |
| 43 | |
| 44 | #ifdef PISTOL |
| 45 | // Pistol Grip controller |
Austin Schuh | 8ace380 | 2017-04-16 19:19:33 -0700 | [diff] [blame] | 46 | const JoystickAxis kSteeringWheel(1, 2), kDriveThrottle(1, 1); |
| 47 | const ButtonLocation kQuickTurn(1, 7); |
| 48 | const ButtonLocation kTurn1(1, 8); |
Austin Schuh | 51b1bae | 2017-04-09 18:31:57 -0700 | [diff] [blame] | 49 | |
| 50 | // Nop |
Austin Schuh | 51b1bae | 2017-04-09 18:31:57 -0700 | [diff] [blame] | 51 | const ButtonLocation kTurn2(1, 9); |
Austin Schuh | 51b1bae | 2017-04-09 18:31:57 -0700 | [diff] [blame] | 52 | #endif |
| 53 | |
| 54 | #ifdef XBOX |
| 55 | // xbox |
| 56 | const JoystickAxis kSteeringWheel(1, 5), kDriveThrottle(1, 2); |
| 57 | const ButtonLocation kQuickTurn(1, 5); |
| 58 | |
| 59 | // Nop |
| 60 | const ButtonLocation kTurn1(1, 1); |
| 61 | const ButtonLocation kTurn2(1, 2); |
| 62 | |
| 63 | #endif |
| 64 | |
| 65 | |
Austin Schuh | 55c8d30 | 2017-04-05 19:25:37 -0700 | [diff] [blame] | 66 | const ButtonLocation kGearSlotBack(2, 11); |
| 67 | |
Campbell Crowley | 71b5f13 | 2017-02-18 13:16:08 -0800 | [diff] [blame] | 68 | const ButtonLocation kIntakeDown(3, 9); |
Austin Schuh | d0629b1 | 2017-03-22 22:37:16 -0700 | [diff] [blame] | 69 | const POVLocation kIntakeUp(3, 90); |
Campbell Crowley | 71b5f13 | 2017-02-18 13:16:08 -0800 | [diff] [blame] | 70 | const ButtonLocation kIntakeIn(3, 12); |
| 71 | const ButtonLocation kIntakeOut(3, 8); |
Campbell Crowley | 71b5f13 | 2017-02-18 13:16:08 -0800 | [diff] [blame] | 72 | const ButtonLocation kFire(3, 3); |
Parker Schuh | 208a58d | 2017-04-12 20:51:38 -0700 | [diff] [blame] | 73 | const ButtonLocation kVisionDistanceShot(3, 7); |
Campbell Crowley | 71b5f13 | 2017-02-18 13:16:08 -0800 | [diff] [blame] | 74 | const ButtonLocation kMiddleShot(3, 6); |
| 75 | const POVLocation kFarShot(3, 270); |
| 76 | |
| 77 | const ButtonLocation kVisionAlign(3, 5); |
| 78 | |
| 79 | const ButtonLocation kReverseIndexer(3, 4); |
| 80 | const ButtonLocation kExtra1(3, 11); |
| 81 | const ButtonLocation kExtra2(3, 10); |
Austin Schuh | d0629b1 | 2017-03-22 22:37:16 -0700 | [diff] [blame] | 82 | const ButtonLocation kHang(3, 2); |
Campbell Crowley | 71b5f13 | 2017-02-18 13:16:08 -0800 | [diff] [blame] | 83 | |
| 84 | class Reader : public ::aos::input::JoystickInput { |
| 85 | public: |
| 86 | Reader() {} |
| 87 | |
Parker Schuh | 208a58d | 2017-04-12 20:51:38 -0700 | [diff] [blame] | 88 | enum class ShotDistance { VISION_SHOT, MIDDLE_SHOT, FAR_SHOT }; |
Austin Schuh | d0629b1 | 2017-03-22 22:37:16 -0700 | [diff] [blame] | 89 | |
Austin Schuh | 4b5f7df | 2017-04-16 20:31:12 -0700 | [diff] [blame^] | 90 | ShotDistance last_shot_distance_ = ShotDistance::VISION_SHOT; |
Austin Schuh | d0629b1 | 2017-03-22 22:37:16 -0700 | [diff] [blame] | 91 | |
Campbell Crowley | 71b5f13 | 2017-02-18 13:16:08 -0800 | [diff] [blame] | 92 | void RunIteration(const ::aos::input::driver_station::Data &data) override { |
| 93 | bool last_auto_running = auto_running_; |
| 94 | auto_running_ = data.GetControlBit(ControlBit::kAutonomous) && |
| 95 | data.GetControlBit(ControlBit::kEnabled); |
| 96 | if (auto_running_ != last_auto_running) { |
| 97 | if (auto_running_) { |
| 98 | StartAuto(); |
| 99 | } else { |
| 100 | StopAuto(); |
| 101 | } |
| 102 | } |
| 103 | |
Campbell Crowley | 71b5f13 | 2017-02-18 13:16:08 -0800 | [diff] [blame] | 104 | if (!auto_running_) { |
| 105 | HandleDrivetrain(data); |
| 106 | HandleTeleop(data); |
| 107 | } |
| 108 | |
| 109 | // Process any pending actions. |
| 110 | action_queue_.Tick(); |
| 111 | was_running_ = action_queue_.Running(); |
| 112 | } |
Austin Schuh | d0629b1 | 2017-03-22 22:37:16 -0700 | [diff] [blame] | 113 | int intake_accumulator_ = 0; |
Campbell Crowley | 71b5f13 | 2017-02-18 13:16:08 -0800 | [diff] [blame] | 114 | |
Austin Schuh | 51b1bae | 2017-04-09 18:31:57 -0700 | [diff] [blame] | 115 | double Deadband(double value, const double deadband) { |
| 116 | if (::std::abs(value) < deadband) { |
| 117 | value = 0.0; |
| 118 | } else if (value > 0.0) { |
| 119 | value = (value - deadband) / (1.0 - deadband); |
| 120 | } else { |
| 121 | value = (value + deadband) / (1.0 - deadband); |
| 122 | } |
| 123 | return value; |
| 124 | } |
| 125 | |
Campbell Crowley | 71b5f13 | 2017-02-18 13:16:08 -0800 | [diff] [blame] | 126 | void HandleDrivetrain(const ::aos::input::driver_station::Data &data) { |
| 127 | bool is_control_loop_driving = false; |
| 128 | |
Austin Schuh | 51b1bae | 2017-04-09 18:31:57 -0700 | [diff] [blame] | 129 | #ifdef STEERINGWHEEL |
Campbell Crowley | 71b5f13 | 2017-02-18 13:16:08 -0800 | [diff] [blame] | 130 | const double wheel = -data.GetAxis(kSteeringWheel); |
| 131 | const double throttle = -data.GetAxis(kDriveThrottle); |
Austin Schuh | 51b1bae | 2017-04-09 18:31:57 -0700 | [diff] [blame] | 132 | #endif |
| 133 | |
| 134 | #ifdef XBOX |
| 135 | // xbox |
| 136 | constexpr double kWheelDeadband = 0.05; |
| 137 | constexpr double kThrottleDeadband = 0.05; |
| 138 | const double wheel = |
| 139 | Deadband(-data.GetAxis(kSteeringWheel), kWheelDeadband); |
| 140 | |
| 141 | const double unmodified_throttle = |
| 142 | Deadband(-data.GetAxis(kDriveThrottle), kThrottleDeadband); |
| 143 | |
| 144 | // Apply a sin function that's scaled to make it feel better. |
| 145 | constexpr double throttle_range = M_PI_2 * 0.9; |
| 146 | |
| 147 | double throttle = ::std::sin(throttle_range * unmodified_throttle) / |
| 148 | ::std::sin(throttle_range); |
| 149 | throttle = |
| 150 | ::std::sin(throttle_range * throttle) / ::std::sin(throttle_range); |
| 151 | throttle = 2.0 * unmodified_throttle - throttle; |
| 152 | #endif |
| 153 | |
| 154 | #ifdef PISTOL |
Austin Schuh | 8ace380 | 2017-04-16 19:19:33 -0700 | [diff] [blame] | 155 | const double unscaled_wheel = data.GetAxis(kSteeringWheel); |
| 156 | double wheel; |
| 157 | if (unscaled_wheel < 0.0) { |
| 158 | wheel = unscaled_wheel / 0.484375; |
| 159 | } else { |
| 160 | wheel = unscaled_wheel / 0.385827; |
| 161 | } |
Austin Schuh | 51b1bae | 2017-04-09 18:31:57 -0700 | [diff] [blame] | 162 | |
| 163 | const double unscaled_throttle = -data.GetAxis(kDriveThrottle); |
| 164 | double unmodified_throttle; |
| 165 | if (unscaled_throttle < 0.0) { |
Austin Schuh | 8ace380 | 2017-04-16 19:19:33 -0700 | [diff] [blame] | 166 | unmodified_throttle = unscaled_throttle / 0.086614; |
Austin Schuh | 51b1bae | 2017-04-09 18:31:57 -0700 | [diff] [blame] | 167 | } else { |
Austin Schuh | 8ace380 | 2017-04-16 19:19:33 -0700 | [diff] [blame] | 168 | unmodified_throttle = unscaled_throttle / 0.265625; |
Austin Schuh | 51b1bae | 2017-04-09 18:31:57 -0700 | [diff] [blame] | 169 | } |
| 170 | unmodified_throttle = Deadband(unmodified_throttle, 0.1); |
| 171 | |
| 172 | // Apply a sin function that's scaled to make it feel better. |
| 173 | constexpr double throttle_range = M_PI_2 * 0.5; |
| 174 | |
| 175 | double throttle = ::std::sin(throttle_range * unmodified_throttle) / |
| 176 | ::std::sin(throttle_range); |
| 177 | throttle = |
| 178 | ::std::sin(throttle_range * throttle) / ::std::sin(throttle_range); |
| 179 | throttle = 2.0 * unmodified_throttle - throttle; |
| 180 | #endif |
| 181 | |
Campbell Crowley | 71b5f13 | 2017-02-18 13:16:08 -0800 | [diff] [blame] | 182 | drivetrain_queue.status.FetchLatest(); |
Austin Schuh | d0629b1 | 2017-03-22 22:37:16 -0700 | [diff] [blame] | 183 | if (drivetrain_queue.status.get()) { |
| 184 | robot_velocity_ = drivetrain_queue.status->robot_speed; |
| 185 | } |
Campbell Crowley | 71b5f13 | 2017-02-18 13:16:08 -0800 | [diff] [blame] | 186 | |
Austin Schuh | 55c8d30 | 2017-04-05 19:25:37 -0700 | [diff] [blame] | 187 | if (data.PosEdge(kTurn1) || data.PosEdge(kTurn2) || |
| 188 | data.PosEdge(kGearSlotBack)) { |
Campbell Crowley | 71b5f13 | 2017-02-18 13:16:08 -0800 | [diff] [blame] | 189 | if (drivetrain_queue.status.get()) { |
| 190 | left_goal_ = drivetrain_queue.status->estimated_left_position; |
| 191 | right_goal_ = drivetrain_queue.status->estimated_right_position; |
| 192 | } |
| 193 | } |
Austin Schuh | 51b1bae | 2017-04-09 18:31:57 -0700 | [diff] [blame] | 194 | #ifdef PISTOL |
| 195 | double current_left_goal = left_goal_ - wheel * 0.20 + throttle * 0.3; |
| 196 | double current_right_goal = right_goal_ + wheel * 0.20 + throttle * 0.3; |
| 197 | #else |
Austin Schuh | 55c8d30 | 2017-04-05 19:25:37 -0700 | [diff] [blame] | 198 | double current_left_goal = left_goal_ - wheel * 0.5 + throttle * 0.3; |
| 199 | double current_right_goal = right_goal_ + wheel * 0.5 + throttle * 0.3; |
Austin Schuh | 51b1bae | 2017-04-09 18:31:57 -0700 | [diff] [blame] | 200 | #endif |
Campbell Crowley | 71b5f13 | 2017-02-18 13:16:08 -0800 | [diff] [blame] | 201 | if (data.IsPressed(kTurn1) || data.IsPressed(kTurn2)) { |
| 202 | is_control_loop_driving = true; |
| 203 | } |
Austin Schuh | 55c8d30 | 2017-04-05 19:25:37 -0700 | [diff] [blame] | 204 | if (data.IsPressed(kGearSlotBack)) { |
| 205 | is_control_loop_driving = true; |
| 206 | current_left_goal = left_goal_ - 0.03; |
| 207 | current_right_goal = right_goal_ - 0.03; |
| 208 | } |
| 209 | auto new_drivetrain_goal = drivetrain_queue.goal.MakeMessage(); |
| 210 | new_drivetrain_goal->steering = wheel; |
| 211 | new_drivetrain_goal->throttle = throttle; |
| 212 | new_drivetrain_goal->quickturn = data.IsPressed(kQuickTurn); |
| 213 | new_drivetrain_goal->control_loop_driving = is_control_loop_driving; |
| 214 | new_drivetrain_goal->left_goal = current_left_goal; |
| 215 | new_drivetrain_goal->right_goal = current_right_goal; |
| 216 | new_drivetrain_goal->left_velocity_goal = 0; |
| 217 | new_drivetrain_goal->right_velocity_goal = 0; |
| 218 | |
| 219 | new_drivetrain_goal->linear.max_velocity = 3.0; |
| 220 | new_drivetrain_goal->linear.max_acceleration = 20.0; |
| 221 | |
| 222 | if (!new_drivetrain_goal.Send()) { |
Campbell Crowley | 71b5f13 | 2017-02-18 13:16:08 -0800 | [diff] [blame] | 223 | LOG(WARNING, "sending stick values failed\n"); |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | void HandleTeleop(const ::aos::input::driver_station::Data &data) { |
| 228 | // Default the intake to in. |
Austin Schuh | d0629b1 | 2017-03-22 22:37:16 -0700 | [diff] [blame] | 229 | intake_goal_ = 0.07; |
Adam Snaider | e0554ef | 2017-03-11 23:02:45 -0800 | [diff] [blame] | 230 | bool lights_on = false; |
Austin Schuh | d0629b1 | 2017-03-22 22:37:16 -0700 | [diff] [blame] | 231 | bool vision_track = false; |
Campbell Crowley | 71b5f13 | 2017-02-18 13:16:08 -0800 | [diff] [blame] | 232 | |
| 233 | if (!data.GetControlBit(ControlBit::kEnabled)) { |
| 234 | action_queue_.CancelAllActions(); |
| 235 | LOG(DEBUG, "Canceling\n"); |
| 236 | } |
| 237 | |
| 238 | superstructure_queue.status.FetchLatest(); |
| 239 | if (!superstructure_queue.status.get()) { |
| 240 | LOG(ERROR, "Got no superstructure status packet.\n"); |
| 241 | return; |
| 242 | } |
| 243 | |
Austin Schuh | d0629b1 | 2017-03-22 22:37:16 -0700 | [diff] [blame] | 244 | if (data.IsPressed(kIntakeUp)) { |
| 245 | intake_goal_ = 0.0; |
Austin Schuh | 3ae4743 | 2017-04-16 19:15:46 -0700 | [diff] [blame] | 246 | turret_goal_ = M_PI / 3.0; |
Austin Schuh | d0629b1 | 2017-03-22 22:37:16 -0700 | [diff] [blame] | 247 | } |
Austin Schuh | 405724e | 2017-04-09 18:34:18 -0700 | [diff] [blame] | 248 | if (data.IsPressed(kIntakeDown)) { |
| 249 | intake_goal_ = 0.235; |
| 250 | // Don't go quite so far out since we have a gear mech out now. |
| 251 | if (data.IsPressed(kIntakeUp)) { |
| 252 | intake_goal_ = 0.160; |
| 253 | } |
| 254 | } |
Austin Schuh | d0629b1 | 2017-03-22 22:37:16 -0700 | [diff] [blame] | 255 | |
Campbell Crowley | 71b5f13 | 2017-02-18 13:16:08 -0800 | [diff] [blame] | 256 | |
| 257 | if (data.IsPressed(kVisionAlign)) { |
| 258 | // Align shot using vision |
| 259 | // TODO(campbell): Add vision aligning. |
Adam Snaider | e0554ef | 2017-03-11 23:02:45 -0800 | [diff] [blame] | 260 | lights_on = true; |
Austin Schuh | d0629b1 | 2017-03-22 22:37:16 -0700 | [diff] [blame] | 261 | vision_track = true; |
| 262 | } |
| 263 | if (data.PosEdge(kMiddleShot)) { |
| 264 | turret_goal_ = -M_PI; |
| 265 | } |
| 266 | if (data.PosEdge(kFarShot)) { |
| 267 | turret_goal_ = 0.0; |
| 268 | } |
Parker Schuh | 208a58d | 2017-04-12 20:51:38 -0700 | [diff] [blame] | 269 | if (data.PosEdge(kVisionDistanceShot)) { |
| 270 | turret_goal_ = 0.0; |
| 271 | } |
Austin Schuh | d0629b1 | 2017-03-22 22:37:16 -0700 | [diff] [blame] | 272 | |
Parker Schuh | 208a58d | 2017-04-12 20:51:38 -0700 | [diff] [blame] | 273 | if (data.IsPressed(kVisionDistanceShot)) { |
| 274 | last_shot_distance_ = ShotDistance::VISION_SHOT; |
Campbell Crowley | 71b5f13 | 2017-02-18 13:16:08 -0800 | [diff] [blame] | 275 | } else if (data.IsPressed(kMiddleShot)) { |
Austin Schuh | d0629b1 | 2017-03-22 22:37:16 -0700 | [diff] [blame] | 276 | last_shot_distance_ = ShotDistance::MIDDLE_SHOT; |
Campbell Crowley | 71b5f13 | 2017-02-18 13:16:08 -0800 | [diff] [blame] | 277 | } else if (data.IsPressed(kFarShot)) { |
Austin Schuh | d0629b1 | 2017-03-22 22:37:16 -0700 | [diff] [blame] | 278 | last_shot_distance_ = ShotDistance::FAR_SHOT; |
| 279 | } |
| 280 | |
Parker Schuh | 208a58d | 2017-04-12 20:51:38 -0700 | [diff] [blame] | 281 | if (data.IsPressed(kVisionAlign) || |
Austin Schuh | d0629b1 | 2017-03-22 22:37:16 -0700 | [diff] [blame] | 282 | data.IsPressed(kMiddleShot) || data.IsPressed(kFarShot) || |
| 283 | data.IsPressed(kFire)) { |
| 284 | switch (last_shot_distance_) { |
Parker Schuh | 208a58d | 2017-04-12 20:51:38 -0700 | [diff] [blame] | 285 | case ShotDistance::VISION_SHOT: |
Austin Schuh | 4b5f7df | 2017-04-16 20:31:12 -0700 | [diff] [blame^] | 286 | hood_goal_ = 0.10; |
| 287 | shooter_velocity_ = 300.0; |
| 288 | |
Parker Schuh | 208a58d | 2017-04-12 20:51:38 -0700 | [diff] [blame] | 289 | vision_distance_shot_ = true; |
Austin Schuh | d0629b1 | 2017-03-22 22:37:16 -0700 | [diff] [blame] | 290 | break; |
| 291 | case ShotDistance::MIDDLE_SHOT: |
Austin Schuh | 405724e | 2017-04-09 18:34:18 -0700 | [diff] [blame] | 292 | hood_goal_ = 0.43 - 0.00; |
Austin Schuh | 4b5f7df | 2017-04-16 20:31:12 -0700 | [diff] [blame^] | 293 | shooter_velocity_ = 364.0; |
Parker Schuh | 208a58d | 2017-04-12 20:51:38 -0700 | [diff] [blame] | 294 | vision_distance_shot_ = false; |
Austin Schuh | d0629b1 | 2017-03-22 22:37:16 -0700 | [diff] [blame] | 295 | break; |
| 296 | case ShotDistance::FAR_SHOT: |
Austin Schuh | 4b5f7df | 2017-04-16 20:31:12 -0700 | [diff] [blame^] | 297 | hood_goal_ = 0.47; |
| 298 | shooter_velocity_ = 410.0; |
Parker Schuh | 208a58d | 2017-04-12 20:51:38 -0700 | [diff] [blame] | 299 | vision_distance_shot_ = false; |
Austin Schuh | d0629b1 | 2017-03-22 22:37:16 -0700 | [diff] [blame] | 300 | break; |
| 301 | } |
Campbell Crowley | 71b5f13 | 2017-02-18 13:16:08 -0800 | [diff] [blame] | 302 | } else { |
Austin Schuh | d0629b1 | 2017-03-22 22:37:16 -0700 | [diff] [blame] | 303 | //hood_goal_ = 0.15; |
Campbell Crowley | 71b5f13 | 2017-02-18 13:16:08 -0800 | [diff] [blame] | 304 | shooter_velocity_ = 0.0; |
| 305 | } |
| 306 | |
| 307 | if (data.IsPressed(kExtra1)) { |
Austin Schuh | d0629b1 | 2017-03-22 22:37:16 -0700 | [diff] [blame] | 308 | //turret_goal_ = -M_PI * 3.0 / 4.0; |
| 309 | turret_goal_ += 0.150; |
Campbell Crowley | 71b5f13 | 2017-02-18 13:16:08 -0800 | [diff] [blame] | 310 | } |
| 311 | if (data.IsPressed(kExtra2)) { |
Austin Schuh | d0629b1 | 2017-03-22 22:37:16 -0700 | [diff] [blame] | 312 | //turret_goal_ = M_PI * 3.0 / 4.0; |
| 313 | turret_goal_ -= 0.150; |
Campbell Crowley | 71b5f13 | 2017-02-18 13:16:08 -0800 | [diff] [blame] | 314 | } |
Austin Schuh | d0629b1 | 2017-03-22 22:37:16 -0700 | [diff] [blame] | 315 | turret_goal_ = ::std::max(::std::min(turret_goal_, M_PI), -M_PI); |
Campbell Crowley | 71b5f13 | 2017-02-18 13:16:08 -0800 | [diff] [blame] | 316 | |
| 317 | fire_ = data.IsPressed(kFire) && shooter_velocity_ != 0.0; |
Austin Schuh | d0629b1 | 2017-03-22 22:37:16 -0700 | [diff] [blame] | 318 | if (data.IsPressed(kVisionAlign)) { |
| 319 | fire_ = fire_ && superstructure_queue.status->turret.vision_tracking; |
| 320 | } |
Campbell Crowley | 71b5f13 | 2017-02-18 13:16:08 -0800 | [diff] [blame] | 321 | |
| 322 | auto new_superstructure_goal = superstructure_queue.goal.MakeMessage(); |
Austin Schuh | d0629b1 | 2017-03-22 22:37:16 -0700 | [diff] [blame] | 323 | if (data.IsPressed(kHang)) { |
| 324 | intake_goal_ = 0.23; |
| 325 | } |
| 326 | |
Campbell Crowley | 71b5f13 | 2017-02-18 13:16:08 -0800 | [diff] [blame] | 327 | new_superstructure_goal->intake.distance = intake_goal_; |
Austin Schuh | 3028b1d | 2017-03-11 22:12:13 -0800 | [diff] [blame] | 328 | new_superstructure_goal->intake.disable_intake = false; |
Campbell Crowley | 71b5f13 | 2017-02-18 13:16:08 -0800 | [diff] [blame] | 329 | new_superstructure_goal->turret.angle = turret_goal_; |
Austin Schuh | d0629b1 | 2017-03-22 22:37:16 -0700 | [diff] [blame] | 330 | new_superstructure_goal->turret.track = vision_track; |
Campbell Crowley | 71b5f13 | 2017-02-18 13:16:08 -0800 | [diff] [blame] | 331 | new_superstructure_goal->hood.angle = hood_goal_; |
| 332 | new_superstructure_goal->shooter.angular_velocity = shooter_velocity_; |
| 333 | |
Austin Schuh | 6a8131b | 2017-04-08 15:39:22 -0700 | [diff] [blame] | 334 | if (data.IsPressed(kIntakeUp)) { |
Austin Schuh | d6fa5e0 | 2017-04-12 20:52:17 -0700 | [diff] [blame] | 335 | new_superstructure_goal->intake.gear_servo = 0.30; |
Austin Schuh | 6a8131b | 2017-04-08 15:39:22 -0700 | [diff] [blame] | 336 | } else { |
| 337 | // Clamp the gear |
Austin Schuh | d6fa5e0 | 2017-04-12 20:52:17 -0700 | [diff] [blame] | 338 | new_superstructure_goal->intake.gear_servo = 0.66; |
Austin Schuh | 6a8131b | 2017-04-08 15:39:22 -0700 | [diff] [blame] | 339 | } |
| 340 | |
Campbell Crowley | 71b5f13 | 2017-02-18 13:16:08 -0800 | [diff] [blame] | 341 | new_superstructure_goal->intake.profile_params.max_velocity = 0.50; |
Campbell Crowley | 71b5f13 | 2017-02-18 13:16:08 -0800 | [diff] [blame] | 342 | new_superstructure_goal->hood.profile_params.max_velocity = 5.0; |
| 343 | |
Austin Schuh | 4b5f7df | 2017-04-16 20:31:12 -0700 | [diff] [blame^] | 344 | new_superstructure_goal->intake.profile_params.max_acceleration = 3.0; |
Austin Schuh | d0629b1 | 2017-03-22 22:37:16 -0700 | [diff] [blame] | 345 | if (vision_track) { |
| 346 | new_superstructure_goal->turret.profile_params.max_acceleration = 35.0; |
| 347 | new_superstructure_goal->turret.profile_params.max_velocity = 10.0; |
| 348 | } else { |
| 349 | new_superstructure_goal->turret.profile_params.max_acceleration = 15.0; |
| 350 | new_superstructure_goal->turret.profile_params.max_velocity = 6.0; |
| 351 | } |
Campbell Crowley | 71b5f13 | 2017-02-18 13:16:08 -0800 | [diff] [blame] | 352 | new_superstructure_goal->hood.profile_params.max_acceleration = 25.0; |
| 353 | |
Austin Schuh | 3028b1d | 2017-03-11 22:12:13 -0800 | [diff] [blame] | 354 | new_superstructure_goal->intake.voltage_rollers = 0.0; |
Adam Snaider | e0554ef | 2017-03-11 23:02:45 -0800 | [diff] [blame] | 355 | new_superstructure_goal->lights_on = lights_on; |
Austin Schuh | 3028b1d | 2017-03-11 22:12:13 -0800 | [diff] [blame] | 356 | |
Parker Schuh | 208a58d | 2017-04-12 20:51:38 -0700 | [diff] [blame] | 357 | if (data.IsPressed(kVisionAlign) && vision_distance_shot_) { |
| 358 | new_superstructure_goal->use_vision_for_shots = true; |
| 359 | } else { |
| 360 | new_superstructure_goal->use_vision_for_shots = false; |
| 361 | } |
| 362 | |
Austin Schuh | 8e4a7ee | 2017-04-05 19:26:06 -0700 | [diff] [blame] | 363 | if (superstructure_queue.status->intake.position > |
| 364 | superstructure_queue.status->intake.unprofiled_goal_position + 0.01) { |
| 365 | intake_accumulator_ = 10; |
| 366 | } |
| 367 | if (intake_accumulator_ > 0) { |
| 368 | --intake_accumulator_; |
| 369 | if (!superstructure_queue.status->intake.estopped) { |
| 370 | new_superstructure_goal->intake.voltage_rollers = 10.0; |
| 371 | } |
| 372 | } |
| 373 | |
Campbell Crowley | 71b5f13 | 2017-02-18 13:16:08 -0800 | [diff] [blame] | 374 | if (data.IsPressed(kHang)) { |
Austin Schuh | d0629b1 | 2017-03-22 22:37:16 -0700 | [diff] [blame] | 375 | new_superstructure_goal->intake.voltage_rollers = -10.0; |
Austin Schuh | 3028b1d | 2017-03-11 22:12:13 -0800 | [diff] [blame] | 376 | new_superstructure_goal->intake.disable_intake = true; |
Austin Schuh | d0629b1 | 2017-03-22 22:37:16 -0700 | [diff] [blame] | 377 | } else if (data.IsPressed(kIntakeIn) || data.IsPressed(kFire)) { |
| 378 | if (robot_velocity_ > 2.0) { |
| 379 | new_superstructure_goal->intake.voltage_rollers = 12.0; |
| 380 | } else { |
Austin Schuh | 4b5f7df | 2017-04-16 20:31:12 -0700 | [diff] [blame^] | 381 | new_superstructure_goal->intake.voltage_rollers = 11.0; |
Austin Schuh | d0629b1 | 2017-03-22 22:37:16 -0700 | [diff] [blame] | 382 | } |
Campbell Crowley | 71b5f13 | 2017-02-18 13:16:08 -0800 | [diff] [blame] | 383 | } else if (data.IsPressed(kIntakeOut)) { |
| 384 | new_superstructure_goal->intake.voltage_rollers = -8.0; |
Austin Schuh | 3028b1d | 2017-03-11 22:12:13 -0800 | [diff] [blame] | 385 | } |
| 386 | if (intake_goal_ < 0.1) { |
| 387 | new_superstructure_goal->intake.voltage_rollers = |
| 388 | ::std::min(8.0, new_superstructure_goal->intake.voltage_rollers); |
Campbell Crowley | 71b5f13 | 2017-02-18 13:16:08 -0800 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | if (data.IsPressed(kReverseIndexer)) { |
Austin Schuh | d0629b1 | 2017-03-22 22:37:16 -0700 | [diff] [blame] | 392 | new_superstructure_goal->indexer.voltage_rollers = -12.0; |
Austin Schuh | 3028b1d | 2017-03-11 22:12:13 -0800 | [diff] [blame] | 393 | new_superstructure_goal->indexer.angular_velocity = 4.0; |
Campbell Crowley | 71b5f13 | 2017-02-18 13:16:08 -0800 | [diff] [blame] | 394 | new_superstructure_goal->indexer.angular_velocity = 1.0; |
Austin Schuh | 3028b1d | 2017-03-11 22:12:13 -0800 | [diff] [blame] | 395 | } else if (fire_) { |
Austin Schuh | d0629b1 | 2017-03-22 22:37:16 -0700 | [diff] [blame] | 396 | new_superstructure_goal->indexer.voltage_rollers = 12.0; |
| 397 | switch (last_shot_distance_) { |
Parker Schuh | 208a58d | 2017-04-12 20:51:38 -0700 | [diff] [blame] | 398 | case ShotDistance::VISION_SHOT: |
Austin Schuh | 4b5f7df | 2017-04-16 20:31:12 -0700 | [diff] [blame^] | 399 | new_superstructure_goal->indexer.angular_velocity = -1.25 * M_PI; |
Austin Schuh | d0629b1 | 2017-03-22 22:37:16 -0700 | [diff] [blame] | 400 | break; |
| 401 | case ShotDistance::MIDDLE_SHOT: |
| 402 | case ShotDistance::FAR_SHOT: |
| 403 | new_superstructure_goal->indexer.angular_velocity = -2.25 * M_PI; |
| 404 | break; |
| 405 | } |
Campbell Crowley | 71b5f13 | 2017-02-18 13:16:08 -0800 | [diff] [blame] | 406 | } else { |
| 407 | new_superstructure_goal->indexer.voltage_rollers = 0.0; |
| 408 | new_superstructure_goal->indexer.angular_velocity = 0.0; |
| 409 | } |
| 410 | |
| 411 | LOG_STRUCT(DEBUG, "sending goal", *new_superstructure_goal); |
| 412 | if (!new_superstructure_goal.Send()) { |
| 413 | LOG(ERROR, "Sending superstructure goal failed.\n"); |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | private: |
Austin Schuh | 3028b1d | 2017-03-11 22:12:13 -0800 | [diff] [blame] | 418 | void StartAuto() { |
| 419 | LOG(INFO, "Starting auto mode\n"); |
| 420 | |
| 421 | ::frc971::autonomous::AutonomousActionParams params; |
| 422 | ::frc971::autonomous::auto_mode.FetchLatest(); |
| 423 | if (::frc971::autonomous::auto_mode.get() != nullptr) { |
| 424 | params.mode = ::frc971::autonomous::auto_mode->mode; |
| 425 | } else { |
| 426 | LOG(WARNING, "no auto mode values\n"); |
| 427 | params.mode = 0; |
| 428 | } |
| 429 | action_queue_.EnqueueAction( |
| 430 | ::frc971::autonomous::MakeAutonomousAction(params)); |
| 431 | } |
Campbell Crowley | 71b5f13 | 2017-02-18 13:16:08 -0800 | [diff] [blame] | 432 | |
| 433 | void StopAuto() { |
| 434 | LOG(INFO, "Stopping auto mode\n"); |
| 435 | action_queue_.CancelAllActions(); |
| 436 | } |
| 437 | |
| 438 | // Current goals to send to the robot. |
| 439 | double intake_goal_ = 0.0; |
| 440 | double turret_goal_ = 0.0; |
| 441 | double hood_goal_ = 0.3; |
| 442 | double shooter_velocity_ = 0.0; |
| 443 | |
| 444 | // Goals to send to the drivetrain in closed loop mode. |
Austin Schuh | 3028b1d | 2017-03-11 22:12:13 -0800 | [diff] [blame] | 445 | double left_goal_ = 0.0; |
| 446 | double right_goal_ = 0.0; |
Campbell Crowley | 71b5f13 | 2017-02-18 13:16:08 -0800 | [diff] [blame] | 447 | |
| 448 | bool was_running_ = false; |
| 449 | bool auto_running_ = false; |
| 450 | |
Parker Schuh | 208a58d | 2017-04-12 20:51:38 -0700 | [diff] [blame] | 451 | bool vision_distance_shot_ = false; |
Campbell Crowley | 71b5f13 | 2017-02-18 13:16:08 -0800 | [diff] [blame] | 452 | |
| 453 | bool fire_ = false; |
Austin Schuh | d0629b1 | 2017-03-22 22:37:16 -0700 | [diff] [blame] | 454 | double robot_velocity_ = 0.0; |
Campbell Crowley | 71b5f13 | 2017-02-18 13:16:08 -0800 | [diff] [blame] | 455 | |
| 456 | ::aos::common::actions::ActionQueue action_queue_; |
| 457 | }; |
| 458 | |
| 459 | } // namespace joysticks |
| 460 | } // namespace input |
| 461 | } // namespace y2017 |
| 462 | |
| 463 | int main() { |
| 464 | ::aos::Init(-1); |
| 465 | ::y2017::input::joysticks::Reader reader; |
| 466 | reader.Run(); |
| 467 | ::aos::Cleanup(); |
| 468 | } |