Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 1 | #include <math.h> |
| 2 | #include <stdio.h> |
| 3 | #include <string.h> |
| 4 | #include <unistd.h> |
Tyler Chatow | 4fedeea | 2019-03-10 15:33:36 -0700 | [diff] [blame] | 5 | #include <chrono> |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 6 | |
| 7 | #include "aos/actions/actions.h" |
| 8 | #include "aos/init.h" |
| 9 | #include "aos/input/action_joystick_input.h" |
| 10 | #include "aos/input/driver_station_data.h" |
| 11 | #include "aos/input/drivetrain_input.h" |
| 12 | #include "aos/input/joystick_input.h" |
| 13 | #include "aos/logging/logging.h" |
| 14 | #include "aos/logging/logging.h" |
Tyler Chatow | e024145 | 2019-03-08 21:07:50 -0800 | [diff] [blame] | 15 | #include "aos/network/team_number.h" |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 16 | #include "aos/util/log_interval.h" |
Tyler Chatow | e024145 | 2019-03-08 21:07:50 -0800 | [diff] [blame] | 17 | #include "aos/vision/events/udp.h" |
| 18 | #include "external/com_google_protobuf/src/google/protobuf/stubs/stringprintf.h" |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 19 | #include "frc971/autonomous/auto.q.h" |
| 20 | #include "frc971/autonomous/base_autonomous_actor.h" |
| 21 | #include "frc971/control_loops/drivetrain/drivetrain.q.h" |
James Kuszmaul | d8deb68 | 2019-03-10 10:38:42 -0700 | [diff] [blame] | 22 | #include "frc971/control_loops/drivetrain/localizer.q.h" |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 23 | |
| 24 | #include "y2019/control_loops/drivetrain/drivetrain_base.h" |
James Kuszmaul | 7d1ef44 | 2019-03-23 20:20:50 -0700 | [diff] [blame] | 25 | #include "y2019/control_loops/drivetrain/target_selector.q.h" |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 26 | #include "y2019/control_loops/superstructure/superstructure.q.h" |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame] | 27 | #include "y2019/status_light.q.h" |
Tyler Chatow | e024145 | 2019-03-08 21:07:50 -0800 | [diff] [blame] | 28 | #include "y2019/vision.pb.h" |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 29 | |
| 30 | using ::y2019::control_loops::superstructure::superstructure_queue; |
James Kuszmaul | 7d1ef44 | 2019-03-23 20:20:50 -0700 | [diff] [blame] | 31 | using ::y2019::control_loops::drivetrain::target_selector_hint; |
James Kuszmaul | d8deb68 | 2019-03-10 10:38:42 -0700 | [diff] [blame] | 32 | using ::frc971::control_loops::drivetrain::localizer_control; |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 33 | using ::aos::input::driver_station::ButtonLocation; |
| 34 | using ::aos::input::driver_station::ControlBit; |
| 35 | using ::aos::input::driver_station::JoystickAxis; |
| 36 | using ::aos::input::driver_station::POVLocation; |
Tyler Chatow | e024145 | 2019-03-08 21:07:50 -0800 | [diff] [blame] | 37 | using ::aos::events::ProtoTXUdpSocket; |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 38 | |
Austin Schuh | aab7e16 | 2019-03-13 22:44:58 -0700 | [diff] [blame] | 39 | namespace chrono = ::std::chrono; |
| 40 | |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 41 | namespace y2019 { |
| 42 | namespace input { |
| 43 | namespace joysticks { |
| 44 | |
Tyler Chatow | e024145 | 2019-03-08 21:07:50 -0800 | [diff] [blame] | 45 | using google::protobuf::StringPrintf; |
| 46 | |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 47 | struct ElevatorWristPosition { |
| 48 | double elevator; |
| 49 | double wrist; |
| 50 | }; |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 51 | |
Tyler Chatow | 5eeee90 | 2019-04-12 20:47:48 -0700 | [diff] [blame] | 52 | const ButtonLocation kSuctionBall(4, 2); |
| 53 | const ButtonLocation kSuctionHatch(3, 15); |
| 54 | const ButtonLocation kDeployStilt(4, 1); |
| 55 | const ButtonLocation kHalfStilt(4, 3); |
| 56 | const ButtonLocation kFallOver(3, 16); |
| 57 | |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 58 | const ButtonLocation kRocketForwardLower(5, 1); |
| 59 | const ButtonLocation kRocketForwardMiddle(5, 2); |
| 60 | const ButtonLocation kRocketForwardUpper(5, 4); |
| 61 | const ButtonLocation kCargoForward(5, 3); |
| 62 | |
| 63 | const POVLocation kRocketBackwardUnpressed(5, -1); |
| 64 | const POVLocation kRocketBackwardLower(5, 180); |
| 65 | const POVLocation kRocketBackwardMiddle(5, 90); |
| 66 | const POVLocation kRocketBackwardUpper(5, 0); |
| 67 | const POVLocation kCargoBackward(5, 270); |
| 68 | |
| 69 | const ButtonLocation kPanelSwitch(5, 7); |
| 70 | const ButtonLocation kCargoSwitch(5, 8); |
| 71 | |
| 72 | const ButtonLocation kBallHPIntakeForward(5, 6); |
| 73 | const ButtonLocation kBallHPIntakeBackward(5, 5); |
| 74 | const JoystickAxis kBallOutake(5, 3); |
| 75 | const JoystickAxis kBallIntake(5, 4); |
| 76 | |
| 77 | const ButtonLocation kPanelHPIntakeForward(5, 6); |
| 78 | const ButtonLocation kPanelHPIntakeBackward(5, 5); |
| 79 | |
| 80 | const ButtonLocation kRelease(2, 4); |
James Kuszmaul | ccc368b | 2019-04-11 20:00:07 -0700 | [diff] [blame] | 81 | // Reuse quickturn for the cancel button. |
| 82 | const ButtonLocation kCancelAutoMode(2, 3); |
Tyler Chatow | 5eeee90 | 2019-04-12 20:47:48 -0700 | [diff] [blame] | 83 | const ButtonLocation kReleaseButtonBoard(3, 4); |
| 84 | const ButtonLocation kResetLocalizerLeftForwards(3, 10); |
| 85 | const ButtonLocation kResetLocalizerLeftBackwards(3, 9); |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 86 | |
Tyler Chatow | 5eeee90 | 2019-04-12 20:47:48 -0700 | [diff] [blame] | 87 | const ButtonLocation kResetLocalizerRightForwards(3, 8); |
| 88 | const ButtonLocation kResetLocalizerRightBackwards(3, 7); |
Tyler Chatow | 07c906b | 2019-03-09 21:29:06 -0800 | [diff] [blame] | 89 | |
James Kuszmaul | 518640d | 2019-04-13 15:50:50 -0700 | [diff] [blame] | 90 | const ButtonLocation kResetLocalizerLeft(3, 11); |
| 91 | const ButtonLocation kResetLocalizerRight(3, 13); |
| 92 | |
Tyler Chatow | 5eeee90 | 2019-04-12 20:47:48 -0700 | [diff] [blame] | 93 | const ButtonLocation kNearCargoHint(3, 3); |
| 94 | const ButtonLocation kMidCargoHint(3, 5); |
| 95 | const ButtonLocation kFarCargoHint(3, 6); |
James Kuszmaul | 7d1ef44 | 2019-03-23 20:20:50 -0700 | [diff] [blame] | 96 | |
James Kuszmaul | 518640d | 2019-04-13 15:50:50 -0700 | [diff] [blame] | 97 | const JoystickAxis kCargoSelectorY(5, 6); |
| 98 | const JoystickAxis kCargoSelectorX(5, 5); |
| 99 | |
Tyler Chatow | 5eeee90 | 2019-04-12 20:47:48 -0700 | [diff] [blame] | 100 | const ButtonLocation kCameraLog(3, 14); |
Austin Schuh | 4e2629d | 2019-03-28 14:44:37 -0700 | [diff] [blame] | 101 | |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 102 | const ElevatorWristPosition kStowPos{0.36, 0.0}; |
Austin Schuh | 02be8b9 | 2019-03-24 16:04:14 -0700 | [diff] [blame] | 103 | const ElevatorWristPosition kClimbPos{0.0, M_PI / 4.0}; |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 104 | |
Austin Schuh | 139f59d | 2019-03-17 18:16:13 -0700 | [diff] [blame] | 105 | const ElevatorWristPosition kPanelHPIntakeForwrdPos{0.01, M_PI / 2.0}; |
| 106 | const ElevatorWristPosition kPanelHPIntakeBackwardPos{0.015, -M_PI / 2.0}; |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 107 | |
| 108 | const ElevatorWristPosition kPanelForwardLowerPos{0.0, M_PI / 2.0}; |
| 109 | const ElevatorWristPosition kPanelBackwardLowerPos{0.0, -M_PI / 2.0}; |
| 110 | |
Sabina Davis | e48004f | 2019-03-02 23:15:24 -0800 | [diff] [blame] | 111 | const ElevatorWristPosition kPanelForwardMiddlePos{0.75, M_PI / 2.0}; |
| 112 | const ElevatorWristPosition kPanelBackwardMiddlePos{0.78, -M_PI / 2.0}; |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 113 | |
Sabina Davis | e48004f | 2019-03-02 23:15:24 -0800 | [diff] [blame] | 114 | const ElevatorWristPosition kPanelForwardUpperPos{1.51, M_PI / 2.0}; |
| 115 | const ElevatorWristPosition kPanelBackwardUpperPos{1.50, -M_PI / 2.0}; |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 116 | |
Sabina Davis | e6fe6c5 | 2019-03-03 15:48:51 -0800 | [diff] [blame] | 117 | const ElevatorWristPosition kPanelCargoForwardPos{0.0, M_PI / 2.0}; |
| 118 | const ElevatorWristPosition kPanelCargoBackwardPos{0.0, -M_PI / 2.0}; |
| 119 | |
Austin Schuh | 6c5deb1 | 2019-03-24 16:48:32 -0700 | [diff] [blame] | 120 | const ElevatorWristPosition kBallForwardLowerPos{0.21, 1.27}; |
Austin Schuh | 20dc050 | 2019-03-30 07:24:07 -0700 | [diff] [blame] | 121 | const ElevatorWristPosition kBallBackwardLowerPos{0.43, -1.99}; |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 122 | |
Austin Schuh | 20dc050 | 2019-03-30 07:24:07 -0700 | [diff] [blame] | 123 | const ElevatorWristPosition kBallForwardMiddlePos{0.925, 1.21}; |
| 124 | const ElevatorWristPosition kBallBackwardMiddlePos{1.19, -1.98}; |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 125 | |
Austin Schuh | 139f59d | 2019-03-17 18:16:13 -0700 | [diff] [blame] | 126 | const ElevatorWristPosition kBallForwardUpperPos{1.51, 0.961}; |
| 127 | const ElevatorWristPosition kBallBackwardUpperPos{1.44, -1.217}; |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 128 | |
Austin Schuh | 20dc050 | 2019-03-30 07:24:07 -0700 | [diff] [blame] | 129 | const ElevatorWristPosition kBallCargoForwardPos{0.59, 1.2}; |
| 130 | const ElevatorWristPosition kBallCargoBackwardPos{0.868265, -2.1}; |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 131 | |
Sabina Davis | e6fe6c5 | 2019-03-03 15:48:51 -0800 | [diff] [blame] | 132 | const ElevatorWristPosition kBallHPIntakeForwardPos{0.55, 1.097}; |
| 133 | const ElevatorWristPosition kBallHPIntakeBackwardPos{0.89, -2.018}; |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 134 | |
Austin Schuh | 20dc050 | 2019-03-30 07:24:07 -0700 | [diff] [blame] | 135 | const ElevatorWristPosition kBallIntakePos{0.309, 2.13}; |
Austin Schuh | 2cf16b8 | 2019-02-15 23:23:22 -0800 | [diff] [blame] | 136 | |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 137 | class Reader : public ::aos::input::ActionJoystickInput { |
| 138 | public: |
| 139 | Reader(::aos::EventLoop *event_loop) |
| 140 | : ::aos::input::ActionJoystickInput( |
| 141 | event_loop, |
James Kuszmaul | ccc368b | 2019-04-11 20:00:07 -0700 | [diff] [blame] | 142 | ::y2019::control_loops::drivetrain::GetDrivetrainConfig(), |
| 143 | {.run_teleop_in_auto = true, |
| 144 | .cancel_auto_button = kCancelAutoMode}) { |
Tyler Chatow | e024145 | 2019-03-08 21:07:50 -0800 | [diff] [blame] | 145 | const uint16_t team = ::aos::network::GetTeamNumber(); |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 146 | superstructure_queue.goal.FetchLatest(); |
| 147 | if (superstructure_queue.goal.get()) { |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame] | 148 | grab_piece_ = superstructure_queue.goal->suction.grab_piece; |
Austin Schuh | bb1df6f | 2019-03-17 18:15:43 -0700 | [diff] [blame] | 149 | switch_ball_ = superstructure_queue.goal->suction.gamepiece_mode == 0; |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 150 | } |
Tyler Chatow | e024145 | 2019-03-08 21:07:50 -0800 | [diff] [blame] | 151 | video_tx_.reset(new ProtoTXUdpSocket<VisionControl>( |
| 152 | StringPrintf("10.%d.%d.179", team / 100, team % 100), 5000)); |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 153 | } |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 154 | |
Austin Schuh | a78857f | 2019-03-13 22:43:41 -0700 | [diff] [blame] | 155 | void AutoEnded() override { |
| 156 | LOG(INFO, "Auto ended, assuming disc and have piece\n"); |
| 157 | grab_piece_ = true; |
| 158 | switch_ball_ = false; |
| 159 | } |
| 160 | |
| 161 | void HandleTeleop(const ::aos::input::driver_station::Data &data) override { |
Austin Schuh | aab7e16 | 2019-03-13 22:44:58 -0700 | [diff] [blame] | 162 | ::aos::monotonic_clock::time_point monotonic_now = |
| 163 | ::aos::monotonic_clock::now(); |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 164 | superstructure_queue.position.FetchLatest(); |
| 165 | superstructure_queue.status.FetchLatest(); |
| 166 | if (!superstructure_queue.status.get() || |
| 167 | !superstructure_queue.position.get()) { |
Austin Schuh | e2f2248 | 2019-04-13 23:05:43 -0700 | [diff] [blame] | 168 | LOG(ERROR, "Got no superstructure status or position packet.\n"); |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 169 | return; |
| 170 | } |
| 171 | |
Austin Schuh | aab7e16 | 2019-03-13 22:44:58 -0700 | [diff] [blame] | 172 | if (!superstructure_queue.status->has_piece) { |
| 173 | last_not_has_piece_ = monotonic_now; |
| 174 | } |
| 175 | |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 176 | auto new_superstructure_goal = superstructure_queue.goal.MakeMessage(); |
| 177 | |
James Kuszmaul | 7d1ef44 | 2019-03-23 20:20:50 -0700 | [diff] [blame] | 178 | { |
| 179 | auto target_hint = target_selector_hint.MakeMessage(); |
| 180 | if (data.IsPressed(kNearCargoHint)) { |
| 181 | target_hint->suggested_target = 1; |
| 182 | } else if (data.IsPressed(kMidCargoHint)) { |
| 183 | target_hint->suggested_target = 2; |
| 184 | } else if (data.IsPressed(kFarCargoHint)) { |
| 185 | target_hint->suggested_target = 3; |
| 186 | } else { |
James Kuszmaul | 518640d | 2019-04-13 15:50:50 -0700 | [diff] [blame] | 187 | const double cargo_joy_y = data.GetAxis(kCargoSelectorY); |
| 188 | const double cargo_joy_x = data.GetAxis(kCargoSelectorX); |
| 189 | if (cargo_joy_y > 0.5) { |
| 190 | target_hint->suggested_target = 3; |
| 191 | } else if (cargo_joy_y < -0.5) { |
| 192 | target_hint->suggested_target = 1; |
| 193 | } else if (::std::abs(cargo_joy_x) > 0.5) { |
| 194 | target_hint->suggested_target = 2; |
| 195 | } else { |
| 196 | target_hint->suggested_target = 0; |
| 197 | } |
James Kuszmaul | 7d1ef44 | 2019-03-23 20:20:50 -0700 | [diff] [blame] | 198 | } |
| 199 | if (!target_hint.Send()) { |
| 200 | LOG(ERROR, "Failed to send target selector hint.\n"); |
| 201 | } |
| 202 | } |
| 203 | |
James Kuszmaul | 518640d | 2019-04-13 15:50:50 -0700 | [diff] [blame] | 204 | if (data.PosEdge(kResetLocalizerLeft)) { |
| 205 | auto localizer_resetter = localizer_control.MakeMessage(); |
| 206 | // Start at the left feeder station. |
| 207 | localizer_resetter->x = 0.6; |
| 208 | localizer_resetter->y = 3.4; |
| 209 | localizer_resetter->keep_current_theta = true; |
| 210 | |
| 211 | if (!localizer_resetter.Send()) { |
| 212 | LOG(ERROR, "Failed to reset localizer.\n"); |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | if (data.PosEdge(kResetLocalizerRight)) { |
| 217 | auto localizer_resetter = localizer_control.MakeMessage(); |
| 218 | // Start at the left feeder station. |
| 219 | localizer_resetter->x = 0.6; |
| 220 | localizer_resetter->y = -3.4; |
| 221 | localizer_resetter->keep_current_theta = true; |
| 222 | |
| 223 | if (!localizer_resetter.Send()) { |
| 224 | LOG(ERROR, "Failed to reset localizer.\n"); |
| 225 | } |
| 226 | } |
| 227 | |
Austin Schuh | bb1df6f | 2019-03-17 18:15:43 -0700 | [diff] [blame] | 228 | if (data.PosEdge(kResetLocalizerLeftForwards)) { |
James Kuszmaul | d8deb68 | 2019-03-10 10:38:42 -0700 | [diff] [blame] | 229 | auto localizer_resetter = localizer_control.MakeMessage(); |
Austin Schuh | 3632f8d | 2019-03-22 21:14:14 -0700 | [diff] [blame] | 230 | // Start at the left feeder station. |
James Kuszmaul | d8deb68 | 2019-03-10 10:38:42 -0700 | [diff] [blame] | 231 | localizer_resetter->x = 0.4; |
| 232 | localizer_resetter->y = 3.4; |
| 233 | localizer_resetter->theta = 0.0; |
Austin Schuh | bb1df6f | 2019-03-17 18:15:43 -0700 | [diff] [blame] | 234 | |
| 235 | if (!localizer_resetter.Send()) { |
| 236 | LOG(ERROR, "Failed to reset localizer.\n"); |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | if (data.PosEdge(kResetLocalizerLeftBackwards)) { |
| 241 | auto localizer_resetter = localizer_control.MakeMessage(); |
| 242 | // Start at the left feeder station. |
| 243 | localizer_resetter->x = 0.4; |
| 244 | localizer_resetter->y = 3.4; |
| 245 | localizer_resetter->theta = M_PI; |
| 246 | |
| 247 | if (!localizer_resetter.Send()) { |
| 248 | LOG(ERROR, "Failed to reset localizer.\n"); |
| 249 | } |
| 250 | } |
| 251 | |
Austin Schuh | bb1df6f | 2019-03-17 18:15:43 -0700 | [diff] [blame] | 252 | if (data.PosEdge(kResetLocalizerRightForwards)) { |
| 253 | auto localizer_resetter = localizer_control.MakeMessage(); |
Austin Schuh | 3632f8d | 2019-03-22 21:14:14 -0700 | [diff] [blame] | 254 | // Start at the right feeder station. |
Austin Schuh | bb1df6f | 2019-03-17 18:15:43 -0700 | [diff] [blame] | 255 | localizer_resetter->x = 0.4; |
| 256 | localizer_resetter->y = -3.4; |
| 257 | localizer_resetter->theta = 0.0; |
| 258 | |
| 259 | if (!localizer_resetter.Send()) { |
| 260 | LOG(ERROR, "Failed to reset localizer.\n"); |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | if (data.PosEdge(kResetLocalizerRightBackwards)) { |
| 265 | auto localizer_resetter = localizer_control.MakeMessage(); |
Austin Schuh | 3632f8d | 2019-03-22 21:14:14 -0700 | [diff] [blame] | 266 | // Start at the right feeder station. |
Austin Schuh | bb1df6f | 2019-03-17 18:15:43 -0700 | [diff] [blame] | 267 | localizer_resetter->x = 0.4; |
| 268 | localizer_resetter->y = -3.4; |
| 269 | localizer_resetter->theta = M_PI; |
| 270 | |
James Kuszmaul | d8deb68 | 2019-03-10 10:38:42 -0700 | [diff] [blame] | 271 | if (!localizer_resetter.Send()) { |
| 272 | LOG(ERROR, "Failed to reset localizer.\n"); |
| 273 | } |
| 274 | } |
| 275 | |
James Kuszmaul | 1373886 | 2019-04-14 10:48:00 -0700 | [diff] [blame] | 276 | if (data.PosEdge(kRelease) && |
| 277 | monotonic_now > |
| 278 | last_release_button_press_ + ::std::chrono::milliseconds(500)) { |
| 279 | if (superstructure_queue.status->has_piece) { |
| 280 | release_mode_ = ReleaseButtonMode::kRelease; |
| 281 | } else { |
| 282 | release_mode_ = ReleaseButtonMode::kBallIntake; |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | if (data.IsPressed(kRelease)) { |
| 287 | last_release_button_press_ = monotonic_now; |
| 288 | } |
| 289 | |
Austin Schuh | 1a17e13 | 2019-02-17 15:05:06 -0800 | [diff] [blame] | 290 | if (data.IsPressed(kSuctionBall)) { |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame] | 291 | grab_piece_ = true; |
Austin Schuh | 1a17e13 | 2019-02-17 15:05:06 -0800 | [diff] [blame] | 292 | } else if (data.IsPressed(kSuctionHatch)) { |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame] | 293 | grab_piece_ = true; |
James Kuszmaul | 1373886 | 2019-04-14 10:48:00 -0700 | [diff] [blame] | 294 | } else if ((release_mode_ == ReleaseButtonMode::kRelease && |
| 295 | data.IsPressed(kRelease)) || |
Tyler Chatow | 5eeee90 | 2019-04-12 20:47:48 -0700 | [diff] [blame] | 296 | data.IsPressed(kReleaseButtonBoard) || |
Austin Schuh | 1a17e13 | 2019-02-17 15:05:06 -0800 | [diff] [blame] | 297 | !superstructure_queue.status->has_piece) { |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame] | 298 | grab_piece_ = false; |
Austin Schuh | 1a17e13 | 2019-02-17 15:05:06 -0800 | [diff] [blame] | 299 | } |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 300 | |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 301 | if (data.IsPressed(kRocketBackwardUnpressed)) { |
| 302 | elevator_wrist_pos_ = kStowPos; |
| 303 | } |
| 304 | new_superstructure_goal->intake.unsafe_goal = -1.2; |
| 305 | new_superstructure_goal->roller_voltage = 0.0; |
| 306 | |
James Kuszmaul | 1373886 | 2019-04-14 10:48:00 -0700 | [diff] [blame] | 307 | const bool kDoBallIntake = |
| 308 | (!climbed_ && release_mode_ == ReleaseButtonMode::kBallIntake && |
| 309 | data.IsPressed(kRelease)) || |
| 310 | data.GetAxis(kBallIntake) > 0.9; |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 311 | const bool kDoBallOutake = data.GetAxis(kBallOutake) > 0.9; |
| 312 | |
| 313 | if (data.IsPressed(kPanelSwitch)) { |
| 314 | switch_ball_ = false; |
| 315 | } else if (data.IsPressed(kCargoSwitch)) { |
| 316 | switch_ball_ = true; |
| 317 | } |
| 318 | |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 319 | if (switch_ball_) { |
| 320 | if (superstructure_queue.status->has_piece) { |
| 321 | new_superstructure_goal->wrist.profile_params.max_acceleration = 20; |
Austin Schuh | 1a17e13 | 2019-02-17 15:05:06 -0800 | [diff] [blame] | 322 | } |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 323 | |
| 324 | // Go to intake position and apply vacuum |
| 325 | if (data.IsPressed(kBallHPIntakeForward)) { |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame] | 326 | grab_piece_ = true; |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 327 | elevator_wrist_pos_ = kBallHPIntakeForwardPos; |
| 328 | } else if (data.IsPressed(kBallHPIntakeBackward)) { |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame] | 329 | grab_piece_ = true; |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 330 | elevator_wrist_pos_ = kBallHPIntakeBackwardPos; |
| 331 | } |
| 332 | |
| 333 | // Go to elevator/wrist position. Overrides intake position if pressed so |
| 334 | // we can re-grab the ball. |
| 335 | if (data.IsPressed(kRocketForwardLower)) { |
| 336 | elevator_wrist_pos_ = kBallForwardLowerPos; |
| 337 | } else if (data.IsPressed(kRocketBackwardLower)) { |
| 338 | elevator_wrist_pos_ = kBallBackwardLowerPos; |
| 339 | } else if (data.IsPressed(kRocketForwardMiddle)) { |
| 340 | elevator_wrist_pos_ = kBallForwardMiddlePos; |
| 341 | } else if (data.IsPressed(kRocketBackwardMiddle)) { |
| 342 | elevator_wrist_pos_ = kBallBackwardMiddlePos; |
| 343 | } else if (data.IsPressed(kRocketForwardUpper)) { |
| 344 | elevator_wrist_pos_ = kBallForwardUpperPos; |
| 345 | } else if (data.IsPressed(kRocketBackwardUpper)) { |
| 346 | elevator_wrist_pos_ = kBallBackwardUpperPos; |
| 347 | } else if (data.IsPressed(kCargoForward)) { |
| 348 | elevator_wrist_pos_ = kBallCargoForwardPos; |
| 349 | } else if (data.IsPressed(kCargoBackward)) { |
| 350 | elevator_wrist_pos_ = kBallCargoBackwardPos; |
| 351 | } |
Austin Schuh | 1a17e13 | 2019-02-17 15:05:06 -0800 | [diff] [blame] | 352 | } else { |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 353 | if (data.IsPressed(kPanelHPIntakeForward)) { |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame] | 354 | grab_piece_ = true; |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 355 | elevator_wrist_pos_ = kPanelHPIntakeForwrdPos; |
| 356 | } else if (data.IsPressed(kPanelHPIntakeBackward)) { |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame] | 357 | grab_piece_ = true; |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 358 | elevator_wrist_pos_ = kPanelHPIntakeBackwardPos; |
| 359 | } |
| 360 | |
| 361 | // Go to elevator/wrist position. Overrides intake position if pressed so |
| 362 | // we can re-grab the panel. |
| 363 | if (data.IsPressed(kRocketForwardLower)) { |
| 364 | elevator_wrist_pos_ = kPanelForwardLowerPos; |
| 365 | } else if (data.IsPressed(kRocketBackwardLower)) { |
| 366 | elevator_wrist_pos_ = kPanelBackwardLowerPos; |
| 367 | } else if (data.IsPressed(kRocketForwardMiddle)) { |
| 368 | elevator_wrist_pos_ = kPanelForwardMiddlePos; |
| 369 | } else if (data.IsPressed(kRocketBackwardMiddle)) { |
| 370 | elevator_wrist_pos_ = kPanelBackwardMiddlePos; |
| 371 | } else if (data.IsPressed(kRocketForwardUpper)) { |
| 372 | elevator_wrist_pos_ = kPanelForwardUpperPos; |
| 373 | } else if (data.IsPressed(kRocketBackwardUpper)) { |
| 374 | elevator_wrist_pos_ = kPanelBackwardUpperPos; |
Sabina Davis | e6fe6c5 | 2019-03-03 15:48:51 -0800 | [diff] [blame] | 375 | } else if (data.IsPressed(kCargoForward)) { |
| 376 | elevator_wrist_pos_ = kPanelCargoForwardPos; |
| 377 | } else if (data.IsPressed(kCargoBackward)) { |
| 378 | elevator_wrist_pos_ = kPanelCargoBackwardPos; |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 379 | } |
Austin Schuh | 1a17e13 | 2019-02-17 15:05:06 -0800 | [diff] [blame] | 380 | } |
| 381 | |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 382 | if (switch_ball_) { |
| 383 | if (kDoBallOutake || |
Austin Schuh | aab7e16 | 2019-03-13 22:44:58 -0700 | [diff] [blame] | 384 | (kDoBallIntake && |
Austin Schuh | 20dc050 | 2019-03-30 07:24:07 -0700 | [diff] [blame] | 385 | monotonic_now < last_not_has_piece_ + chrono::milliseconds(200))) { |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 386 | new_superstructure_goal->intake.unsafe_goal = 0.959327; |
| 387 | } |
Austin Schuh | 23a5163 | 2019-02-19 16:50:36 -0800 | [diff] [blame] | 388 | |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 389 | if (kDoBallIntake && !superstructure_queue.status->has_piece) { |
| 390 | elevator_wrist_pos_ = kBallIntakePos; |
| 391 | new_superstructure_goal->roller_voltage = 9.0; |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame] | 392 | grab_piece_ = true; |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 393 | } else { |
| 394 | if (kDoBallOutake) { |
| 395 | new_superstructure_goal->roller_voltage = -6.0; |
| 396 | } else { |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 397 | new_superstructure_goal->roller_voltage = 0.0; |
| 398 | } |
| 399 | } |
Austin Schuh | 23a5163 | 2019-02-19 16:50:36 -0800 | [diff] [blame] | 400 | } |
Austin Schuh | 1a17e13 | 2019-02-17 15:05:06 -0800 | [diff] [blame] | 401 | |
Austin Schuh | 457bde3 | 2019-03-17 18:16:41 -0700 | [diff] [blame] | 402 | constexpr double kDeployStiltPosition = 0.5; |
| 403 | |
Austin Schuh | 457bde3 | 2019-03-17 18:16:41 -0700 | [diff] [blame] | 404 | if (data.IsPressed(kFallOver)) { |
| 405 | new_superstructure_goal->stilts.unsafe_goal = 0.71; |
| 406 | new_superstructure_goal->stilts.profile_params.max_velocity = 0.65; |
| 407 | new_superstructure_goal->stilts.profile_params.max_acceleration = 1.25; |
Austin Schuh | e2f2248 | 2019-04-13 23:05:43 -0700 | [diff] [blame] | 408 | } else if (data.IsPressed(kHalfStilt)) { |
| 409 | was_above_ = false; |
| 410 | new_superstructure_goal->stilts.unsafe_goal = 0.345; |
| 411 | new_superstructure_goal->stilts.profile_params.max_velocity = 0.65; |
| 412 | } else if (data.IsPressed(kDeployStilt) || was_above_) { |
Austin Schuh | 457bde3 | 2019-03-17 18:16:41 -0700 | [diff] [blame] | 413 | new_superstructure_goal->stilts.unsafe_goal = kDeployStiltPosition; |
| 414 | new_superstructure_goal->stilts.profile_params.max_velocity = 0.65; |
| 415 | new_superstructure_goal->stilts.profile_params.max_acceleration = 2.0; |
Austin Schuh | 457bde3 | 2019-03-17 18:16:41 -0700 | [diff] [blame] | 416 | } else { |
| 417 | new_superstructure_goal->stilts.unsafe_goal = 0.005; |
| 418 | new_superstructure_goal->stilts.profile_params.max_velocity = 0.65; |
| 419 | new_superstructure_goal->stilts.profile_params.max_acceleration = 2.0; |
| 420 | } |
| 421 | |
James Kuszmaul | 0448f14 | 2019-03-24 16:22:34 -0700 | [diff] [blame] | 422 | if (superstructure_queue.status->stilts.position > 0.1) { |
Austin Schuh | 02be8b9 | 2019-03-24 16:04:14 -0700 | [diff] [blame] | 423 | elevator_wrist_pos_ = kClimbPos; |
James Kuszmaul | 1373886 | 2019-04-14 10:48:00 -0700 | [diff] [blame] | 424 | climbed_ = true; |
Austin Schuh | d8a18bb | 2019-03-24 19:21:38 -0700 | [diff] [blame] | 425 | new_superstructure_goal->wrist.profile_params.max_acceleration = 10; |
| 426 | new_superstructure_goal->elevator.profile_params.max_acceleration = 6; |
Austin Schuh | 02be8b9 | 2019-03-24 16:04:14 -0700 | [diff] [blame] | 427 | } |
| 428 | |
Austin Schuh | e2f2248 | 2019-04-13 23:05:43 -0700 | [diff] [blame] | 429 | // If we've been asked to go above deploy and made it up that high, latch |
| 430 | // was_above. |
| 431 | if (new_superstructure_goal->stilts.unsafe_goal > kDeployStiltPosition && |
| 432 | superstructure_queue.status->stilts.position >= kDeployStiltPosition) { |
| 433 | was_above_ = true; |
| 434 | } else if ((superstructure_queue.position->platform_left_detect || |
| 435 | superstructure_queue.position->platform_right_detect) && |
| 436 | !data.IsPressed(kDeployStilt) && !data.IsPressed(kFallOver)) { |
| 437 | // TODO(austin): Should make this && rather than || |
| 438 | was_above_ = false; |
| 439 | } |
| 440 | |
Austin Schuh | 457bde3 | 2019-03-17 18:16:41 -0700 | [diff] [blame] | 441 | if (superstructure_queue.status->stilts.position > kDeployStiltPosition && |
| 442 | new_superstructure_goal->stilts.unsafe_goal == kDeployStiltPosition) { |
| 443 | new_superstructure_goal->stilts.profile_params.max_velocity = 0.30; |
| 444 | new_superstructure_goal->stilts.profile_params.max_acceleration = 0.75; |
| 445 | } |
| 446 | |
James Kuszmaul | 1373886 | 2019-04-14 10:48:00 -0700 | [diff] [blame] | 447 | if ((release_mode_ == ReleaseButtonMode::kRelease && |
| 448 | data.IsPressed(kRelease)) || |
| 449 | data.IsPressed(kReleaseButtonBoard)) { |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame] | 450 | grab_piece_ = false; |
Austin Schuh | 1a17e13 | 2019-02-17 15:05:06 -0800 | [diff] [blame] | 451 | } |
| 452 | |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame] | 453 | if (switch_ball_) { |
| 454 | new_superstructure_goal->suction.gamepiece_mode = 0; |
| 455 | } else { |
| 456 | new_superstructure_goal->suction.gamepiece_mode = 1; |
| 457 | } |
| 458 | |
Tyler Chatow | e024145 | 2019-03-08 21:07:50 -0800 | [diff] [blame] | 459 | vision_control_.set_flip_image(elevator_wrist_pos_.wrist < 0); |
| 460 | |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame] | 461 | new_superstructure_goal->suction.grab_piece = grab_piece_; |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 462 | |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 463 | new_superstructure_goal->elevator.unsafe_goal = |
| 464 | elevator_wrist_pos_.elevator; |
| 465 | new_superstructure_goal->wrist.unsafe_goal = elevator_wrist_pos_.wrist; |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 466 | |
| 467 | LOG_STRUCT(DEBUG, "sending goal", *new_superstructure_goal); |
| 468 | if (!new_superstructure_goal.Send()) { |
| 469 | LOG(ERROR, "Sending superstructure goal failed.\n"); |
| 470 | } |
Tyler Chatow | e024145 | 2019-03-08 21:07:50 -0800 | [diff] [blame] | 471 | |
Austin Schuh | aab7e16 | 2019-03-13 22:44:58 -0700 | [diff] [blame] | 472 | if (monotonic_now > |
| 473 | last_vision_control_ + ::std::chrono::milliseconds(50)) { |
Tyler Chatow | 4fedeea | 2019-03-10 15:33:36 -0700 | [diff] [blame] | 474 | video_tx_->Send(vision_control_); |
Austin Schuh | aab7e16 | 2019-03-13 22:44:58 -0700 | [diff] [blame] | 475 | last_vision_control_ = monotonic_now; |
Tyler Chatow | 4fedeea | 2019-03-10 15:33:36 -0700 | [diff] [blame] | 476 | } |
Austin Schuh | 4e2629d | 2019-03-28 14:44:37 -0700 | [diff] [blame] | 477 | |
| 478 | { |
| 479 | auto camera_log_message = camera_log.MakeMessage(); |
| 480 | camera_log_message->log = data.IsPressed(kCameraLog); |
| 481 | LOG_STRUCT(DEBUG, "camera_log", *camera_log_message); |
| 482 | camera_log_message.Send(); |
| 483 | } |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | private: |
Austin Schuh | a964406 | 2019-03-28 14:31:52 -0700 | [diff] [blame] | 487 | uint32_t GetAutonomousMode() override { |
| 488 | ::frc971::autonomous::auto_mode.FetchLatest(); |
| 489 | if (::frc971::autonomous::auto_mode.get() == nullptr) { |
| 490 | LOG(WARNING, "no auto mode values\n"); |
| 491 | return 0; |
| 492 | } |
| 493 | return ::frc971::autonomous::auto_mode->mode; |
| 494 | } |
| 495 | |
Austin Schuh | e2f2248 | 2019-04-13 23:05:43 -0700 | [diff] [blame] | 496 | // Bool to track if we've been above the deploy position. Once this bool is |
| 497 | // set, don't let the stilts retract until we see the platform. |
| 498 | bool was_above_ = false; |
| 499 | |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 500 | // Current goals here. |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 501 | ElevatorWristPosition elevator_wrist_pos_ = kStowPos; |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame] | 502 | bool grab_piece_ = false; |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 503 | |
| 504 | bool switch_ball_ = false; |
Tyler Chatow | e024145 | 2019-03-08 21:07:50 -0800 | [diff] [blame] | 505 | |
James Kuszmaul | 1373886 | 2019-04-14 10:48:00 -0700 | [diff] [blame] | 506 | bool climbed_ = false; |
| 507 | |
| 508 | enum class ReleaseButtonMode { |
| 509 | kBallIntake, |
| 510 | kRelease, |
| 511 | }; |
| 512 | |
| 513 | ReleaseButtonMode release_mode_ = ReleaseButtonMode::kRelease; |
| 514 | aos::monotonic_clock::time_point last_release_button_press_ = |
| 515 | aos::monotonic_clock::min_time; |
| 516 | |
Tyler Chatow | e024145 | 2019-03-08 21:07:50 -0800 | [diff] [blame] | 517 | VisionControl vision_control_; |
| 518 | ::std::unique_ptr<ProtoTXUdpSocket<VisionControl>> video_tx_; |
Tyler Chatow | 4fedeea | 2019-03-10 15:33:36 -0700 | [diff] [blame] | 519 | ::aos::monotonic_clock::time_point last_vision_control_ = |
| 520 | ::aos::monotonic_clock::time_point::min(); |
Austin Schuh | aab7e16 | 2019-03-13 22:44:58 -0700 | [diff] [blame] | 521 | |
| 522 | // Time at which we last did not have a game piece. |
| 523 | ::aos::monotonic_clock::time_point last_not_has_piece_ = |
| 524 | ::aos::monotonic_clock::time_point::min(); |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 525 | }; |
| 526 | |
| 527 | } // namespace joysticks |
| 528 | } // namespace input |
| 529 | } // namespace y2019 |
| 530 | |
| 531 | int main() { |
| 532 | ::aos::Init(-1); |
| 533 | ::aos::ShmEventLoop event_loop; |
| 534 | ::y2019::input::joysticks::Reader reader(&event_loop); |
| 535 | reader.Run(); |
| 536 | ::aos::Cleanup(); |
| 537 | } |