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" |
| 25 | #include "y2019/control_loops/superstructure/superstructure.q.h" |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame] | 26 | #include "y2019/status_light.q.h" |
Tyler Chatow | e024145 | 2019-03-08 21:07:50 -0800 | [diff] [blame] | 27 | #include "y2019/vision.pb.h" |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 28 | |
| 29 | using ::y2019::control_loops::superstructure::superstructure_queue; |
James Kuszmaul | d8deb68 | 2019-03-10 10:38:42 -0700 | [diff] [blame] | 30 | using ::frc971::control_loops::drivetrain::localizer_control; |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 31 | using ::aos::input::driver_station::ButtonLocation; |
| 32 | using ::aos::input::driver_station::ControlBit; |
| 33 | using ::aos::input::driver_station::JoystickAxis; |
| 34 | using ::aos::input::driver_station::POVLocation; |
Tyler Chatow | e024145 | 2019-03-08 21:07:50 -0800 | [diff] [blame] | 35 | using ::aos::events::ProtoTXUdpSocket; |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 36 | |
Austin Schuh | aab7e16 | 2019-03-13 22:44:58 -0700 | [diff] [blame] | 37 | namespace chrono = ::std::chrono; |
| 38 | |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 39 | namespace y2019 { |
| 40 | namespace input { |
| 41 | namespace joysticks { |
| 42 | |
Tyler Chatow | e024145 | 2019-03-08 21:07:50 -0800 | [diff] [blame] | 43 | using google::protobuf::StringPrintf; |
| 44 | |
Austin Schuh | 1a17e13 | 2019-02-17 15:05:06 -0800 | [diff] [blame] | 45 | const ButtonLocation kSuctionBall(3, 13); |
| 46 | const ButtonLocation kSuctionHatch(3, 12); |
| 47 | const ButtonLocation kDeployStilt(3, 8); |
Sabina Davis | 069cbca | 2019-03-08 23:35:18 -0800 | [diff] [blame] | 48 | const ButtonLocation kHalfStilt(3, 6); |
Austin Schuh | 1a17e13 | 2019-02-17 15:05:06 -0800 | [diff] [blame] | 49 | const ButtonLocation kFallOver(3, 9); |
Austin Schuh | 2cf16b8 | 2019-02-15 23:23:22 -0800 | [diff] [blame] | 50 | |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 51 | struct ElevatorWristPosition { |
| 52 | double elevator; |
| 53 | double wrist; |
| 54 | }; |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 55 | |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 56 | const ButtonLocation kRocketForwardLower(5, 1); |
| 57 | const ButtonLocation kRocketForwardMiddle(5, 2); |
| 58 | const ButtonLocation kRocketForwardUpper(5, 4); |
| 59 | const ButtonLocation kCargoForward(5, 3); |
| 60 | |
| 61 | const POVLocation kRocketBackwardUnpressed(5, -1); |
| 62 | const POVLocation kRocketBackwardLower(5, 180); |
| 63 | const POVLocation kRocketBackwardMiddle(5, 90); |
| 64 | const POVLocation kRocketBackwardUpper(5, 0); |
| 65 | const POVLocation kCargoBackward(5, 270); |
| 66 | |
| 67 | const ButtonLocation kPanelSwitch(5, 7); |
| 68 | const ButtonLocation kCargoSwitch(5, 8); |
| 69 | |
| 70 | const ButtonLocation kBallHPIntakeForward(5, 6); |
| 71 | const ButtonLocation kBallHPIntakeBackward(5, 5); |
| 72 | const JoystickAxis kBallOutake(5, 3); |
| 73 | const JoystickAxis kBallIntake(5, 4); |
| 74 | |
| 75 | const ButtonLocation kPanelHPIntakeForward(5, 6); |
| 76 | const ButtonLocation kPanelHPIntakeBackward(5, 5); |
| 77 | |
| 78 | const ButtonLocation kRelease(2, 4); |
James Kuszmaul | d8deb68 | 2019-03-10 10:38:42 -0700 | [diff] [blame] | 79 | const ButtonLocation kResetLocalizer(4, 3); |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 80 | |
Tyler Chatow | 07c906b | 2019-03-09 21:29:06 -0800 | [diff] [blame] | 81 | const ButtonLocation kAutoPanel(3, 10); |
| 82 | const ButtonLocation kAutoPanelIntermediate(4, 6); |
| 83 | |
| 84 | const ElevatorWristPosition kAutoPanelPos{0.0, -M_PI / 2.0}; |
| 85 | const ElevatorWristPosition kAutoPanelIntermediatePos{0.34, -M_PI / 2.0}; |
| 86 | |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 87 | const ElevatorWristPosition kStowPos{0.36, 0.0}; |
| 88 | |
| 89 | const ElevatorWristPosition kPanelHPIntakeForwrdPos{0.04, M_PI / 2.0}; |
Sabina Davis | e6fe6c5 | 2019-03-03 15:48:51 -0800 | [diff] [blame] | 90 | const ElevatorWristPosition kPanelHPIntakeBackwardPos{0.05, -M_PI / 2.0}; |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 91 | |
| 92 | const ElevatorWristPosition kPanelForwardLowerPos{0.0, M_PI / 2.0}; |
| 93 | const ElevatorWristPosition kPanelBackwardLowerPos{0.0, -M_PI / 2.0}; |
| 94 | |
Sabina Davis | e48004f | 2019-03-02 23:15:24 -0800 | [diff] [blame] | 95 | const ElevatorWristPosition kPanelForwardMiddlePos{0.75, M_PI / 2.0}; |
| 96 | const ElevatorWristPosition kPanelBackwardMiddlePos{0.78, -M_PI / 2.0}; |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 97 | |
Sabina Davis | e48004f | 2019-03-02 23:15:24 -0800 | [diff] [blame] | 98 | const ElevatorWristPosition kPanelForwardUpperPos{1.51, M_PI / 2.0}; |
| 99 | const ElevatorWristPosition kPanelBackwardUpperPos{1.50, -M_PI / 2.0}; |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 100 | |
Sabina Davis | e6fe6c5 | 2019-03-03 15:48:51 -0800 | [diff] [blame] | 101 | const ElevatorWristPosition kPanelCargoForwardPos{0.0, M_PI / 2.0}; |
| 102 | const ElevatorWristPosition kPanelCargoBackwardPos{0.0, -M_PI / 2.0}; |
| 103 | |
| 104 | const ElevatorWristPosition kBallForwardLowerPos{0.46, M_PI / 2.0}; |
| 105 | const ElevatorWristPosition kBallBackwardLowerPos{0.15, -M_PI / 2.0}; |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 106 | |
Sabina Davis | e48004f | 2019-03-02 23:15:24 -0800 | [diff] [blame] | 107 | const ElevatorWristPosition kBallForwardMiddlePos{1.16, 1.546}; |
| 108 | const ElevatorWristPosition kBallBackwardMiddlePos{0.876021, -1.546}; |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 109 | |
Sabina Davis | e48004f | 2019-03-02 23:15:24 -0800 | [diff] [blame] | 110 | const ElevatorWristPosition kBallForwardUpperPos{1.50, 0.961}; |
| 111 | const ElevatorWristPosition kBallBackwardUpperPos{1.41, -1.217}; |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 112 | |
Sabina Davis | e48004f | 2019-03-02 23:15:24 -0800 | [diff] [blame] | 113 | const ElevatorWristPosition kBallCargoForwardPos{0.699044, 1.353}; |
| 114 | const ElevatorWristPosition kBallCargoBackwardPos{0.828265, -1.999}; |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 115 | |
Sabina Davis | e6fe6c5 | 2019-03-03 15:48:51 -0800 | [diff] [blame] | 116 | const ElevatorWristPosition kBallHPIntakeForwardPos{0.55, 1.097}; |
| 117 | const ElevatorWristPosition kBallHPIntakeBackwardPos{0.89, -2.018}; |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 118 | |
| 119 | const ElevatorWristPosition kBallIntakePos{0.29, 2.14}; |
Austin Schuh | 2cf16b8 | 2019-02-15 23:23:22 -0800 | [diff] [blame] | 120 | |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 121 | class Reader : public ::aos::input::ActionJoystickInput { |
| 122 | public: |
| 123 | Reader(::aos::EventLoop *event_loop) |
| 124 | : ::aos::input::ActionJoystickInput( |
| 125 | event_loop, |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 126 | ::y2019::control_loops::drivetrain::GetDrivetrainConfig()) { |
Austin Schuh | 30cc40a | 2019-03-12 21:02:13 -0700 | [diff] [blame] | 127 | // Set teleop to immediately resume after auto ends for sandstorm mode. |
| 128 | set_run_teleop_in_auto(true); |
| 129 | |
Tyler Chatow | e024145 | 2019-03-08 21:07:50 -0800 | [diff] [blame] | 130 | const uint16_t team = ::aos::network::GetTeamNumber(); |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 131 | superstructure_queue.goal.FetchLatest(); |
| 132 | if (superstructure_queue.goal.get()) { |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame] | 133 | grab_piece_ = superstructure_queue.goal->suction.grab_piece; |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 134 | } |
Tyler Chatow | e024145 | 2019-03-08 21:07:50 -0800 | [diff] [blame] | 135 | video_tx_.reset(new ProtoTXUdpSocket<VisionControl>( |
| 136 | StringPrintf("10.%d.%d.179", team / 100, team % 100), 5000)); |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 137 | } |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 138 | |
Austin Schuh | a78857f | 2019-03-13 22:43:41 -0700 | [diff] [blame^] | 139 | void AutoEnded() override { |
| 140 | LOG(INFO, "Auto ended, assuming disc and have piece\n"); |
| 141 | grab_piece_ = true; |
| 142 | switch_ball_ = false; |
| 143 | } |
| 144 | |
| 145 | void HandleTeleop(const ::aos::input::driver_station::Data &data) override { |
Austin Schuh | aab7e16 | 2019-03-13 22:44:58 -0700 | [diff] [blame] | 146 | ::aos::monotonic_clock::time_point monotonic_now = |
| 147 | ::aos::monotonic_clock::now(); |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 148 | superstructure_queue.position.FetchLatest(); |
| 149 | superstructure_queue.status.FetchLatest(); |
| 150 | if (!superstructure_queue.status.get() || |
| 151 | !superstructure_queue.position.get()) { |
| 152 | LOG(ERROR, "Got no superstructure status packet.\n"); |
| 153 | return; |
| 154 | } |
| 155 | |
Austin Schuh | aab7e16 | 2019-03-13 22:44:58 -0700 | [diff] [blame] | 156 | if (!superstructure_queue.status->has_piece) { |
| 157 | last_not_has_piece_ = monotonic_now; |
| 158 | } |
| 159 | |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 160 | auto new_superstructure_goal = superstructure_queue.goal.MakeMessage(); |
| 161 | |
James Kuszmaul | d8deb68 | 2019-03-10 10:38:42 -0700 | [diff] [blame] | 162 | if (data.PosEdge(kResetLocalizer)) { |
| 163 | auto localizer_resetter = localizer_control.MakeMessage(); |
| 164 | localizer_resetter->x = 0.4; |
| 165 | localizer_resetter->y = 3.4; |
| 166 | localizer_resetter->theta = 0.0; |
| 167 | if (!localizer_resetter.Send()) { |
| 168 | LOG(ERROR, "Failed to reset localizer.\n"); |
| 169 | } |
| 170 | } |
| 171 | |
Austin Schuh | 1a17e13 | 2019-02-17 15:05:06 -0800 | [diff] [blame] | 172 | if (data.IsPressed(kSuctionBall)) { |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame] | 173 | grab_piece_ = true; |
Austin Schuh | 1a17e13 | 2019-02-17 15:05:06 -0800 | [diff] [blame] | 174 | } else if (data.IsPressed(kSuctionHatch)) { |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame] | 175 | grab_piece_ = true; |
Austin Schuh | 1a17e13 | 2019-02-17 15:05:06 -0800 | [diff] [blame] | 176 | } else if (data.IsPressed(kRelease) || |
| 177 | !superstructure_queue.status->has_piece) { |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame] | 178 | grab_piece_ = false; |
Austin Schuh | 1a17e13 | 2019-02-17 15:05:06 -0800 | [diff] [blame] | 179 | } |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 180 | |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 181 | if (data.IsPressed(kRocketBackwardUnpressed)) { |
| 182 | elevator_wrist_pos_ = kStowPos; |
| 183 | } |
| 184 | new_superstructure_goal->intake.unsafe_goal = -1.2; |
| 185 | new_superstructure_goal->roller_voltage = 0.0; |
| 186 | |
| 187 | const bool kDoBallIntake = data.GetAxis(kBallIntake) > 0.9; |
| 188 | const bool kDoBallOutake = data.GetAxis(kBallOutake) > 0.9; |
| 189 | |
| 190 | if (data.IsPressed(kPanelSwitch)) { |
| 191 | switch_ball_ = false; |
| 192 | } else if (data.IsPressed(kCargoSwitch)) { |
| 193 | switch_ball_ = true; |
| 194 | } |
| 195 | |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 196 | // TODO(sabina): max height please? |
Austin Schuh | 77ac321 | 2019-02-19 16:50:14 -0800 | [diff] [blame] | 197 | if (data.IsPressed(kFallOver)) { |
Austin Schuh | 1a17e13 | 2019-02-17 15:05:06 -0800 | [diff] [blame] | 198 | new_superstructure_goal->stilts.unsafe_goal = 0.71; |
Michael Schuh | 587dcb5 | 2019-02-28 21:31:03 -0800 | [diff] [blame] | 199 | new_superstructure_goal->stilts.profile_params.max_velocity = 0.65; |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 200 | new_superstructure_goal->stilts.profile_params.max_acceleration = 2.0; |
Austin Schuh | 77ac321 | 2019-02-19 16:50:14 -0800 | [diff] [blame] | 201 | } else if (data.IsPressed(kDeployStilt)) { |
| 202 | new_superstructure_goal->stilts.unsafe_goal = 0.50; |
Michael Schuh | 587dcb5 | 2019-02-28 21:31:03 -0800 | [diff] [blame] | 203 | new_superstructure_goal->stilts.profile_params.max_velocity = 0.65; |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 204 | if (stilts_was_above_) { |
| 205 | new_superstructure_goal->stilts.profile_params.max_acceleration = 0.75; |
| 206 | } else { |
| 207 | new_superstructure_goal->stilts.profile_params.max_acceleration = 2.0; |
| 208 | } |
Sabina Davis | 069cbca | 2019-03-08 23:35:18 -0800 | [diff] [blame] | 209 | } else if (data.IsPressed(kHalfStilt)) { |
| 210 | new_superstructure_goal->stilts.unsafe_goal = 0.345; |
| 211 | new_superstructure_goal->stilts.profile_params.max_velocity = 0.65; |
| 212 | new_superstructure_goal->stilts.profile_params.max_acceleration = 0.75; |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 213 | } else { |
Sabina Davis | bfdeb36 | 2019-03-09 16:20:01 -0800 | [diff] [blame] | 214 | new_superstructure_goal->stilts.unsafe_goal = 0.005; |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 215 | new_superstructure_goal->stilts.profile_params.max_velocity = 0.25; |
| 216 | new_superstructure_goal->stilts.profile_params.max_acceleration = 2.0; |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 217 | } |
| 218 | |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 219 | if (superstructure_queue.status->stilts.position > 0.65) { |
| 220 | stilts_was_above_ = true; |
| 221 | } else if (superstructure_queue.status->stilts.position < 0.1) { |
| 222 | stilts_was_above_ = false; |
Austin Schuh | 2cf16b8 | 2019-02-15 23:23:22 -0800 | [diff] [blame] | 223 | } |
| 224 | |
Tyler Chatow | 07c906b | 2019-03-09 21:29:06 -0800 | [diff] [blame] | 225 | if (data.IsPressed(kAutoPanel)) { |
| 226 | elevator_wrist_pos_ = kAutoPanelPos; |
| 227 | } else if (data.IsPressed(kAutoPanelIntermediate)) { |
| 228 | elevator_wrist_pos_ = kAutoPanelIntermediatePos; |
| 229 | } |
| 230 | |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 231 | if (switch_ball_) { |
| 232 | if (superstructure_queue.status->has_piece) { |
| 233 | new_superstructure_goal->wrist.profile_params.max_acceleration = 20; |
Austin Schuh | 1a17e13 | 2019-02-17 15:05:06 -0800 | [diff] [blame] | 234 | } |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 235 | |
| 236 | // Go to intake position and apply vacuum |
| 237 | if (data.IsPressed(kBallHPIntakeForward)) { |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame] | 238 | grab_piece_ = true; |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 239 | elevator_wrist_pos_ = kBallHPIntakeForwardPos; |
| 240 | } else if (data.IsPressed(kBallHPIntakeBackward)) { |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame] | 241 | grab_piece_ = true; |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 242 | elevator_wrist_pos_ = kBallHPIntakeBackwardPos; |
| 243 | } |
| 244 | |
| 245 | // Go to elevator/wrist position. Overrides intake position if pressed so |
| 246 | // we can re-grab the ball. |
| 247 | if (data.IsPressed(kRocketForwardLower)) { |
| 248 | elevator_wrist_pos_ = kBallForwardLowerPos; |
| 249 | } else if (data.IsPressed(kRocketBackwardLower)) { |
| 250 | elevator_wrist_pos_ = kBallBackwardLowerPos; |
| 251 | } else if (data.IsPressed(kRocketForwardMiddle)) { |
| 252 | elevator_wrist_pos_ = kBallForwardMiddlePos; |
| 253 | } else if (data.IsPressed(kRocketBackwardMiddle)) { |
| 254 | elevator_wrist_pos_ = kBallBackwardMiddlePos; |
| 255 | } else if (data.IsPressed(kRocketForwardUpper)) { |
| 256 | elevator_wrist_pos_ = kBallForwardUpperPos; |
| 257 | } else if (data.IsPressed(kRocketBackwardUpper)) { |
| 258 | elevator_wrist_pos_ = kBallBackwardUpperPos; |
| 259 | } else if (data.IsPressed(kCargoForward)) { |
| 260 | elevator_wrist_pos_ = kBallCargoForwardPos; |
| 261 | } else if (data.IsPressed(kCargoBackward)) { |
| 262 | elevator_wrist_pos_ = kBallCargoBackwardPos; |
| 263 | } |
Austin Schuh | 1a17e13 | 2019-02-17 15:05:06 -0800 | [diff] [blame] | 264 | } else { |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 265 | if (data.IsPressed(kPanelHPIntakeForward)) { |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame] | 266 | grab_piece_ = true; |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 267 | elevator_wrist_pos_ = kPanelHPIntakeForwrdPos; |
| 268 | } else if (data.IsPressed(kPanelHPIntakeBackward)) { |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame] | 269 | grab_piece_ = true; |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 270 | elevator_wrist_pos_ = kPanelHPIntakeBackwardPos; |
| 271 | } |
| 272 | |
| 273 | // Go to elevator/wrist position. Overrides intake position if pressed so |
| 274 | // we can re-grab the panel. |
| 275 | if (data.IsPressed(kRocketForwardLower)) { |
| 276 | elevator_wrist_pos_ = kPanelForwardLowerPos; |
| 277 | } else if (data.IsPressed(kRocketBackwardLower)) { |
| 278 | elevator_wrist_pos_ = kPanelBackwardLowerPos; |
| 279 | } else if (data.IsPressed(kRocketForwardMiddle)) { |
| 280 | elevator_wrist_pos_ = kPanelForwardMiddlePos; |
| 281 | } else if (data.IsPressed(kRocketBackwardMiddle)) { |
| 282 | elevator_wrist_pos_ = kPanelBackwardMiddlePos; |
| 283 | } else if (data.IsPressed(kRocketForwardUpper)) { |
| 284 | elevator_wrist_pos_ = kPanelForwardUpperPos; |
| 285 | } else if (data.IsPressed(kRocketBackwardUpper)) { |
| 286 | elevator_wrist_pos_ = kPanelBackwardUpperPos; |
Sabina Davis | e6fe6c5 | 2019-03-03 15:48:51 -0800 | [diff] [blame] | 287 | } else if (data.IsPressed(kCargoForward)) { |
| 288 | elevator_wrist_pos_ = kPanelCargoForwardPos; |
| 289 | } else if (data.IsPressed(kCargoBackward)) { |
| 290 | elevator_wrist_pos_ = kPanelCargoBackwardPos; |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 291 | } |
Austin Schuh | 1a17e13 | 2019-02-17 15:05:06 -0800 | [diff] [blame] | 292 | } |
| 293 | |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 294 | if (switch_ball_) { |
| 295 | if (kDoBallOutake || |
Austin Schuh | aab7e16 | 2019-03-13 22:44:58 -0700 | [diff] [blame] | 296 | (kDoBallIntake && |
| 297 | monotonic_now < last_not_has_piece_ + chrono::milliseconds(100))) { |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 298 | new_superstructure_goal->intake.unsafe_goal = 0.959327; |
| 299 | } |
Austin Schuh | 23a5163 | 2019-02-19 16:50:36 -0800 | [diff] [blame] | 300 | |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 301 | if (kDoBallIntake && !superstructure_queue.status->has_piece) { |
| 302 | elevator_wrist_pos_ = kBallIntakePos; |
| 303 | new_superstructure_goal->roller_voltage = 9.0; |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame] | 304 | grab_piece_ = true; |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 305 | } else { |
| 306 | if (kDoBallOutake) { |
| 307 | new_superstructure_goal->roller_voltage = -6.0; |
| 308 | } else { |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 309 | new_superstructure_goal->roller_voltage = 0.0; |
| 310 | } |
| 311 | } |
Austin Schuh | 23a5163 | 2019-02-19 16:50:36 -0800 | [diff] [blame] | 312 | } |
Austin Schuh | 1a17e13 | 2019-02-17 15:05:06 -0800 | [diff] [blame] | 313 | |
| 314 | if (data.IsPressed(kRelease)) { |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame] | 315 | grab_piece_ = false; |
Austin Schuh | 1a17e13 | 2019-02-17 15:05:06 -0800 | [diff] [blame] | 316 | } |
| 317 | |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame] | 318 | if (switch_ball_) { |
| 319 | new_superstructure_goal->suction.gamepiece_mode = 0; |
| 320 | } else { |
| 321 | new_superstructure_goal->suction.gamepiece_mode = 1; |
| 322 | } |
| 323 | |
Tyler Chatow | e024145 | 2019-03-08 21:07:50 -0800 | [diff] [blame] | 324 | vision_control_.set_flip_image(elevator_wrist_pos_.wrist < 0); |
| 325 | |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame] | 326 | new_superstructure_goal->suction.grab_piece = grab_piece_; |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 327 | |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 328 | new_superstructure_goal->elevator.unsafe_goal = |
| 329 | elevator_wrist_pos_.elevator; |
| 330 | new_superstructure_goal->wrist.unsafe_goal = elevator_wrist_pos_.wrist; |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 331 | |
| 332 | LOG_STRUCT(DEBUG, "sending goal", *new_superstructure_goal); |
| 333 | if (!new_superstructure_goal.Send()) { |
| 334 | LOG(ERROR, "Sending superstructure goal failed.\n"); |
| 335 | } |
Tyler Chatow | e024145 | 2019-03-08 21:07:50 -0800 | [diff] [blame] | 336 | |
Austin Schuh | aab7e16 | 2019-03-13 22:44:58 -0700 | [diff] [blame] | 337 | if (monotonic_now > |
| 338 | last_vision_control_ + ::std::chrono::milliseconds(50)) { |
Tyler Chatow | 4fedeea | 2019-03-10 15:33:36 -0700 | [diff] [blame] | 339 | video_tx_->Send(vision_control_); |
Austin Schuh | aab7e16 | 2019-03-13 22:44:58 -0700 | [diff] [blame] | 340 | last_vision_control_ = monotonic_now; |
Tyler Chatow | 4fedeea | 2019-03-10 15:33:36 -0700 | [diff] [blame] | 341 | } |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | private: |
| 345 | // Current goals here. |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 346 | ElevatorWristPosition elevator_wrist_pos_ = kStowPos; |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame] | 347 | bool grab_piece_ = false; |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 348 | |
| 349 | bool switch_ball_ = false; |
| 350 | bool stilts_was_above_ = false; |
Tyler Chatow | e024145 | 2019-03-08 21:07:50 -0800 | [diff] [blame] | 351 | |
| 352 | VisionControl vision_control_; |
| 353 | ::std::unique_ptr<ProtoTXUdpSocket<VisionControl>> video_tx_; |
Tyler Chatow | 4fedeea | 2019-03-10 15:33:36 -0700 | [diff] [blame] | 354 | ::aos::monotonic_clock::time_point last_vision_control_ = |
| 355 | ::aos::monotonic_clock::time_point::min(); |
Austin Schuh | aab7e16 | 2019-03-13 22:44:58 -0700 | [diff] [blame] | 356 | |
| 357 | // Time at which we last did not have a game piece. |
| 358 | ::aos::monotonic_clock::time_point last_not_has_piece_ = |
| 359 | ::aos::monotonic_clock::time_point::min(); |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 360 | }; |
| 361 | |
| 362 | } // namespace joysticks |
| 363 | } // namespace input |
| 364 | } // namespace y2019 |
| 365 | |
| 366 | int main() { |
| 367 | ::aos::Init(-1); |
| 368 | ::aos::ShmEventLoop event_loop; |
| 369 | ::y2019::input::joysticks::Reader reader(&event_loop); |
| 370 | reader.Run(); |
| 371 | ::aos::Cleanup(); |
| 372 | } |