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