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