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