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