Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 1 | #include <unistd.h> |
James Kuszmaul | 7077d34 | 2021-06-09 20:23:58 -0700 | [diff] [blame] | 2 | |
Tyler Chatow | 4fedeea | 2019-03-10 15:33:36 -0700 | [diff] [blame] | 3 | #include <chrono> |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 4 | #include <cmath> |
| 5 | #include <cstdio> |
| 6 | #include <cstring> |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 7 | |
Adam Snaider | 13d48d9 | 2023-08-03 12:20:15 -0700 | [diff] [blame] | 8 | #include "absl/strings/str_format.h" |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 9 | |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 10 | #include "aos/actions/actions.h" |
| 11 | #include "aos/init.h" |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 12 | #include "aos/logging/logging.h" |
Tyler Chatow | e024145 | 2019-03-08 21:07:50 -0800 | [diff] [blame] | 13 | #include "aos/network/team_number.h" |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 14 | #include "aos/util/log_interval.h" |
Tyler Chatow | e024145 | 2019-03-08 21:07:50 -0800 | [diff] [blame] | 15 | #include "aos/vision/events/udp.h" |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 16 | #include "frc971/autonomous/base_autonomous_actor.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 17 | #include "frc971/control_loops/drivetrain/localizer_generated.h" |
James Kuszmaul | 7077d34 | 2021-06-09 20:23:58 -0700 | [diff] [blame] | 18 | #include "frc971/input/action_joystick_input.h" |
| 19 | #include "frc971/input/driver_station_data.h" |
| 20 | #include "frc971/input/drivetrain_input.h" |
| 21 | #include "frc971/input/joystick_input.h" |
Austin Schuh | ed5b26d | 2019-12-05 20:51:59 -0800 | [diff] [blame] | 22 | #include "y2019/camera_log_generated.h" |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 23 | #include "y2019/control_loops/drivetrain/drivetrain_base.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 24 | #include "y2019/control_loops/drivetrain/target_selector_generated.h" |
| 25 | #include "y2019/control_loops/superstructure/superstructure_goal_generated.h" |
| 26 | #include "y2019/control_loops/superstructure/superstructure_position_generated.h" |
| 27 | #include "y2019/control_loops/superstructure/superstructure_status_generated.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 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 30 | using aos::events::ProtoTXUdpSocket; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 31 | using frc971::CreateProfileParameters; |
| 32 | using frc971::control_loops::CreateStaticZeroingSingleDOFProfiledSubsystemGoal; |
| 33 | using frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal; |
| 34 | using frc971::control_loops::drivetrain::LocalizerControl; |
James Kuszmaul | 7077d34 | 2021-06-09 20:23:58 -0700 | [diff] [blame] | 35 | using frc971::input::driver_station::ButtonLocation; |
| 36 | using frc971::input::driver_station::ControlBit; |
| 37 | using frc971::input::driver_station::JoystickAxis; |
| 38 | using frc971::input::driver_station::POVLocation; |
James Kuszmaul | 2457eb7 | 2019-12-15 15:37:18 -0800 | [diff] [blame] | 39 | using y2019::control_loops::superstructure::CreateSuctionGoal; |
James Kuszmaul | 7077d34 | 2021-06-09 20:23:58 -0700 | [diff] [blame] | 40 | using y2019::control_loops::superstructure::SuctionGoal; |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 41 | |
Austin Schuh | aab7e16 | 2019-03-13 22:44:58 -0700 | [diff] [blame] | 42 | namespace chrono = ::std::chrono; |
| 43 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 44 | namespace y2019::input::joysticks { |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 45 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 46 | namespace superstructure = y2019::control_loops::superstructure; |
| 47 | |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 48 | struct ElevatorWristPosition { |
| 49 | double elevator; |
| 50 | double wrist; |
| 51 | }; |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 52 | |
Tyler Chatow | 5eeee90 | 2019-04-12 20:47:48 -0700 | [diff] [blame] | 53 | const ButtonLocation kSuctionBall(4, 2); |
| 54 | const ButtonLocation kSuctionHatch(3, 15); |
| 55 | const ButtonLocation kDeployStilt(4, 1); |
| 56 | const ButtonLocation kHalfStilt(4, 3); |
| 57 | const ButtonLocation kFallOver(3, 16); |
| 58 | |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 59 | const ButtonLocation kRocketForwardLower(5, 1); |
| 60 | const ButtonLocation kRocketForwardMiddle(5, 2); |
| 61 | const ButtonLocation kRocketForwardUpper(5, 4); |
| 62 | const ButtonLocation kCargoForward(5, 3); |
| 63 | |
| 64 | const POVLocation kRocketBackwardUnpressed(5, -1); |
| 65 | const POVLocation kRocketBackwardLower(5, 180); |
| 66 | const POVLocation kRocketBackwardMiddle(5, 90); |
| 67 | const POVLocation kRocketBackwardUpper(5, 0); |
| 68 | const POVLocation kCargoBackward(5, 270); |
| 69 | |
| 70 | const ButtonLocation kPanelSwitch(5, 7); |
| 71 | const ButtonLocation kCargoSwitch(5, 8); |
| 72 | |
| 73 | const ButtonLocation kBallHPIntakeForward(5, 6); |
| 74 | const ButtonLocation kBallHPIntakeBackward(5, 5); |
| 75 | const JoystickAxis kBallOutake(5, 3); |
| 76 | const JoystickAxis kBallIntake(5, 4); |
| 77 | |
| 78 | const ButtonLocation kPanelHPIntakeForward(5, 6); |
| 79 | const ButtonLocation kPanelHPIntakeBackward(5, 5); |
| 80 | |
| 81 | const ButtonLocation kRelease(2, 4); |
James Kuszmaul | ccc368b | 2019-04-11 20:00:07 -0700 | [diff] [blame] | 82 | // Reuse quickturn for the cancel button. |
| 83 | const ButtonLocation kCancelAutoMode(2, 3); |
Tyler Chatow | 5eeee90 | 2019-04-12 20:47:48 -0700 | [diff] [blame] | 84 | const ButtonLocation kReleaseButtonBoard(3, 4); |
| 85 | const ButtonLocation kResetLocalizerLeftForwards(3, 10); |
| 86 | const ButtonLocation kResetLocalizerLeftBackwards(3, 9); |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 87 | |
Tyler Chatow | 5eeee90 | 2019-04-12 20:47:48 -0700 | [diff] [blame] | 88 | const ButtonLocation kResetLocalizerRightForwards(3, 8); |
| 89 | const ButtonLocation kResetLocalizerRightBackwards(3, 7); |
Tyler Chatow | 07c906b | 2019-03-09 21:29:06 -0800 | [diff] [blame] | 90 | |
James Kuszmaul | 518640d | 2019-04-13 15:50:50 -0700 | [diff] [blame] | 91 | const ButtonLocation kResetLocalizerLeft(3, 11); |
| 92 | const ButtonLocation kResetLocalizerRight(3, 13); |
| 93 | |
Tyler Chatow | 5eeee90 | 2019-04-12 20:47:48 -0700 | [diff] [blame] | 94 | const ButtonLocation kNearCargoHint(3, 3); |
| 95 | const ButtonLocation kMidCargoHint(3, 5); |
| 96 | const ButtonLocation kFarCargoHint(3, 6); |
James Kuszmaul | 7d1ef44 | 2019-03-23 20:20:50 -0700 | [diff] [blame] | 97 | |
James Kuszmaul | 518640d | 2019-04-13 15:50:50 -0700 | [diff] [blame] | 98 | const JoystickAxis kCargoSelectorY(5, 6); |
| 99 | const JoystickAxis kCargoSelectorX(5, 5); |
| 100 | |
Tyler Chatow | 5eeee90 | 2019-04-12 20:47:48 -0700 | [diff] [blame] | 101 | const ButtonLocation kCameraLog(3, 14); |
Austin Schuh | 4e2629d | 2019-03-28 14:44:37 -0700 | [diff] [blame] | 102 | |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 103 | const ElevatorWristPosition kStowPos{0.36, 0.0}; |
Austin Schuh | 02be8b9 | 2019-03-24 16:04:14 -0700 | [diff] [blame] | 104 | const ElevatorWristPosition kClimbPos{0.0, M_PI / 4.0}; |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 105 | |
Austin Schuh | 139f59d | 2019-03-17 18:16:13 -0700 | [diff] [blame] | 106 | const ElevatorWristPosition kPanelHPIntakeForwrdPos{0.01, M_PI / 2.0}; |
| 107 | const ElevatorWristPosition kPanelHPIntakeBackwardPos{0.015, -M_PI / 2.0}; |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 108 | |
| 109 | const ElevatorWristPosition kPanelForwardLowerPos{0.0, M_PI / 2.0}; |
| 110 | const ElevatorWristPosition kPanelBackwardLowerPos{0.0, -M_PI / 2.0}; |
| 111 | |
Sabina Davis | e48004f | 2019-03-02 23:15:24 -0800 | [diff] [blame] | 112 | const ElevatorWristPosition kPanelForwardMiddlePos{0.75, M_PI / 2.0}; |
| 113 | const ElevatorWristPosition kPanelBackwardMiddlePos{0.78, -M_PI / 2.0}; |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 114 | |
Sabina Davis | e48004f | 2019-03-02 23:15:24 -0800 | [diff] [blame] | 115 | const ElevatorWristPosition kPanelForwardUpperPos{1.51, M_PI / 2.0}; |
| 116 | const ElevatorWristPosition kPanelBackwardUpperPos{1.50, -M_PI / 2.0}; |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 117 | |
Sabina Davis | e6fe6c5 | 2019-03-03 15:48:51 -0800 | [diff] [blame] | 118 | const ElevatorWristPosition kPanelCargoForwardPos{0.0, M_PI / 2.0}; |
| 119 | const ElevatorWristPosition kPanelCargoBackwardPos{0.0, -M_PI / 2.0}; |
| 120 | |
Austin Schuh | 6c5deb1 | 2019-03-24 16:48:32 -0700 | [diff] [blame] | 121 | const ElevatorWristPosition kBallForwardLowerPos{0.21, 1.27}; |
Austin Schuh | 20dc050 | 2019-03-30 07:24:07 -0700 | [diff] [blame] | 122 | const ElevatorWristPosition kBallBackwardLowerPos{0.43, -1.99}; |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 123 | |
Austin Schuh | 20dc050 | 2019-03-30 07:24:07 -0700 | [diff] [blame] | 124 | const ElevatorWristPosition kBallForwardMiddlePos{0.925, 1.21}; |
| 125 | const ElevatorWristPosition kBallBackwardMiddlePos{1.19, -1.98}; |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 126 | |
Austin Schuh | 139f59d | 2019-03-17 18:16:13 -0700 | [diff] [blame] | 127 | const ElevatorWristPosition kBallForwardUpperPos{1.51, 0.961}; |
| 128 | const ElevatorWristPosition kBallBackwardUpperPos{1.44, -1.217}; |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 129 | |
Austin Schuh | 20dc050 | 2019-03-30 07:24:07 -0700 | [diff] [blame] | 130 | const ElevatorWristPosition kBallCargoForwardPos{0.59, 1.2}; |
| 131 | const ElevatorWristPosition kBallCargoBackwardPos{0.868265, -2.1}; |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 132 | |
Sabina Davis | e6fe6c5 | 2019-03-03 15:48:51 -0800 | [diff] [blame] | 133 | const ElevatorWristPosition kBallHPIntakeForwardPos{0.55, 1.097}; |
| 134 | const ElevatorWristPosition kBallHPIntakeBackwardPos{0.89, -2.018}; |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 135 | |
Austin Schuh | 20dc050 | 2019-03-30 07:24:07 -0700 | [diff] [blame] | 136 | const ElevatorWristPosition kBallIntakePos{0.309, 2.13}; |
Austin Schuh | 2cf16b8 | 2019-02-15 23:23:22 -0800 | [diff] [blame] | 137 | |
James Kuszmaul | 7077d34 | 2021-06-09 20:23:58 -0700 | [diff] [blame] | 138 | class Reader : public ::frc971::input::ActionJoystickInput { |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 139 | public: |
| 140 | Reader(::aos::EventLoop *event_loop) |
James Kuszmaul | 7077d34 | 2021-06-09 20:23:58 -0700 | [diff] [blame] | 141 | : ::frc971::input::ActionJoystickInput( |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 142 | event_loop, |
James Kuszmaul | ccc368b | 2019-04-11 20:00:07 -0700 | [diff] [blame] | 143 | ::y2019::control_loops::drivetrain::GetDrivetrainConfig(), |
James Kuszmaul | 7077d34 | 2021-06-09 20:23:58 -0700 | [diff] [blame] | 144 | ::frc971::input::DrivetrainInputReader::InputType::kPistol, |
James Kuszmaul | ccc368b | 2019-04-11 20:00:07 -0700 | [diff] [blame] | 145 | {.run_teleop_in_auto = true, |
Austin Schuh | c087b10 | 2019-05-12 15:33:12 -0700 | [diff] [blame] | 146 | .cancel_auto_button = kCancelAutoMode}), |
| 147 | target_selector_hint_sender_( |
| 148 | event_loop->MakeSender< |
| 149 | ::y2019::control_loops::drivetrain::TargetSelectorHint>( |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 150 | "/drivetrain")), |
Austin Schuh | eb99d07 | 2019-05-12 21:03:38 -0700 | [diff] [blame] | 151 | localizer_control_sender_( |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 152 | event_loop->MakeSender<LocalizerControl>("/drivetrain")), |
Austin Schuh | 5671a8c | 2019-05-19 17:01:04 -0700 | [diff] [blame] | 153 | camera_log_sender_( |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 154 | event_loop->MakeSender<::y2019::CameraLog>("/camera")), |
| 155 | superstructure_goal_fetcher_( |
| 156 | event_loop->MakeFetcher<superstructure::Goal>("/superstructure")), |
| 157 | superstructure_goal_sender_( |
| 158 | event_loop->MakeSender<superstructure::Goal>("/superstructure")), |
Austin Schuh | 170f495 | 2019-06-29 18:58:30 -0700 | [diff] [blame] | 159 | superstructure_position_fetcher_( |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 160 | event_loop->MakeFetcher<superstructure::Position>( |
| 161 | "/superstructure")), |
Austin Schuh | 170f495 | 2019-06-29 18:58:30 -0700 | [diff] [blame] | 162 | superstructure_status_fetcher_( |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 163 | event_loop->MakeFetcher<superstructure::Status>( |
| 164 | "/superstructure")) { |
Tyler Chatow | e024145 | 2019-03-08 21:07:50 -0800 | [diff] [blame] | 165 | const uint16_t team = ::aos::network::GetTeamNumber(); |
Austin Schuh | 170f495 | 2019-06-29 18:58:30 -0700 | [diff] [blame] | 166 | superstructure_goal_fetcher_.Fetch(); |
| 167 | if (superstructure_goal_fetcher_.get()) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 168 | grab_piece_ = superstructure_goal_fetcher_->has_suction() |
| 169 | ? superstructure_goal_fetcher_->suction()->grab_piece() |
| 170 | : false; |
| 171 | switch_ball_ = |
| 172 | superstructure_goal_fetcher_->has_suction() |
| 173 | ? (superstructure_goal_fetcher_->suction()->gamepiece_mode() == 0) |
| 174 | : true; |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 175 | } |
Tyler Chatow | e024145 | 2019-03-08 21:07:50 -0800 | [diff] [blame] | 176 | video_tx_.reset(new ProtoTXUdpSocket<VisionControl>( |
Adam Snaider | 13d48d9 | 2023-08-03 12:20:15 -0700 | [diff] [blame] | 177 | absl::StrFormat("10.%d.%d.179", team / 100, team % 100), 5000)); |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 178 | } |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 179 | |
Austin Schuh | a78857f | 2019-03-13 22:43:41 -0700 | [diff] [blame] | 180 | void AutoEnded() override { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 181 | AOS_LOG(INFO, "Auto ended, assuming disc and have piece\n"); |
Austin Schuh | a78857f | 2019-03-13 22:43:41 -0700 | [diff] [blame] | 182 | grab_piece_ = true; |
| 183 | switch_ball_ = false; |
| 184 | } |
| 185 | |
James Kuszmaul | 7077d34 | 2021-06-09 20:23:58 -0700 | [diff] [blame] | 186 | void HandleTeleop( |
| 187 | const ::frc971::input::driver_station::Data &data) override { |
Austin Schuh | aab7e16 | 2019-03-13 22:44:58 -0700 | [diff] [blame] | 188 | ::aos::monotonic_clock::time_point monotonic_now = |
| 189 | ::aos::monotonic_clock::now(); |
Austin Schuh | 170f495 | 2019-06-29 18:58:30 -0700 | [diff] [blame] | 190 | superstructure_position_fetcher_.Fetch(); |
| 191 | superstructure_status_fetcher_.Fetch(); |
| 192 | if (!superstructure_status_fetcher_.get() || |
| 193 | !superstructure_position_fetcher_.get()) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 194 | AOS_LOG(ERROR, "Got no superstructure status or position packet.\n"); |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 195 | return; |
| 196 | } |
| 197 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 198 | CHECK(superstructure_status_fetcher_->has_stilts()); |
| 199 | |
| 200 | if (!superstructure_status_fetcher_->has_piece()) { |
Austin Schuh | aab7e16 | 2019-03-13 22:44:58 -0700 | [diff] [blame] | 201 | last_not_has_piece_ = monotonic_now; |
| 202 | } |
| 203 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 204 | auto main_superstructure_goal_builder = |
| 205 | superstructure_goal_sender_.MakeBuilder(); |
| 206 | |
| 207 | flatbuffers::Offset<superstructure::Goal> superstructure_goal_offset; |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 208 | |
James Kuszmaul | 7d1ef44 | 2019-03-23 20:20:50 -0700 | [diff] [blame] | 209 | { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 210 | flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal> |
| 211 | elevator_offset = CreateStaticZeroingSingleDOFProfiledSubsystemGoal( |
| 212 | *main_superstructure_goal_builder.fbb(), 0.0, |
| 213 | CreateProfileParameters(*main_superstructure_goal_builder.fbb(), |
| 214 | 0.0, 0.0)); |
| 215 | |
| 216 | flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal> |
| 217 | intake_offset = CreateStaticZeroingSingleDOFProfiledSubsystemGoal( |
| 218 | *main_superstructure_goal_builder.fbb(), -1.2); |
| 219 | |
| 220 | flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal> |
| 221 | wrist_offset = CreateStaticZeroingSingleDOFProfiledSubsystemGoal( |
| 222 | *main_superstructure_goal_builder.fbb(), 0.0, |
| 223 | CreateProfileParameters(*main_superstructure_goal_builder.fbb(), |
| 224 | 0.0, 0.0)); |
| 225 | |
| 226 | flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal> |
| 227 | stilts_offset = CreateStaticZeroingSingleDOFProfiledSubsystemGoal( |
| 228 | *main_superstructure_goal_builder.fbb(), 0.0, |
| 229 | CreateProfileParameters(*main_superstructure_goal_builder.fbb(), |
| 230 | 0.0, 0.0)); |
| 231 | |
James Kuszmaul | 2457eb7 | 2019-12-15 15:37:18 -0800 | [diff] [blame] | 232 | flatbuffers::Offset<SuctionGoal> suction_offset = |
| 233 | CreateSuctionGoal(*main_superstructure_goal_builder.fbb(), false, 0); |
| 234 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 235 | superstructure::Goal::Builder superstructure_goal_builder = |
| 236 | main_superstructure_goal_builder.MakeBuilder<superstructure::Goal>(); |
| 237 | |
| 238 | superstructure_goal_builder.add_elevator(elevator_offset); |
| 239 | superstructure_goal_builder.add_intake(intake_offset); |
| 240 | superstructure_goal_builder.add_wrist(wrist_offset); |
| 241 | superstructure_goal_builder.add_stilts(stilts_offset); |
James Kuszmaul | 2457eb7 | 2019-12-15 15:37:18 -0800 | [diff] [blame] | 242 | superstructure_goal_builder.add_suction(suction_offset); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 243 | superstructure_goal_builder.add_roller_voltage(0.0); |
| 244 | |
| 245 | superstructure_goal_offset = superstructure_goal_builder.Finish(); |
| 246 | } |
Austin Schuh | 6bdcc37 | 2024-06-27 14:49:11 -0700 | [diff] [blame] | 247 | superstructure::Goal *mutable_superstructure_goal = |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 248 | GetMutableTemporaryPointer(*main_superstructure_goal_builder.fbb(), |
Austin Schuh | 6bdcc37 | 2024-06-27 14:49:11 -0700 | [diff] [blame] | 249 | superstructure_goal_offset); |
| 250 | CHECK(mutable_superstructure_goal != nullptr); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 251 | |
| 252 | { |
| 253 | auto builder = target_selector_hint_sender_.MakeBuilder(); |
| 254 | control_loops::drivetrain::TargetSelectorHint::Builder |
| 255 | target_selector_hint_builder = |
| 256 | builder |
| 257 | .MakeBuilder<control_loops::drivetrain::TargetSelectorHint>(); |
James Kuszmaul | 7d1ef44 | 2019-03-23 20:20:50 -0700 | [diff] [blame] | 258 | if (data.IsPressed(kNearCargoHint)) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 259 | target_selector_hint_builder.add_suggested_target( |
Austin Schuh | 872723c | 2019-12-25 14:38:09 -0800 | [diff] [blame] | 260 | control_loops::drivetrain::SelectionHint::NEAR_SHIP); |
James Kuszmaul | 7d1ef44 | 2019-03-23 20:20:50 -0700 | [diff] [blame] | 261 | } else if (data.IsPressed(kMidCargoHint)) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 262 | target_selector_hint_builder.add_suggested_target( |
Austin Schuh | 872723c | 2019-12-25 14:38:09 -0800 | [diff] [blame] | 263 | control_loops::drivetrain::SelectionHint::MID_SHIP); |
James Kuszmaul | 7d1ef44 | 2019-03-23 20:20:50 -0700 | [diff] [blame] | 264 | } else if (data.IsPressed(kFarCargoHint)) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 265 | target_selector_hint_builder.add_suggested_target( |
Austin Schuh | 872723c | 2019-12-25 14:38:09 -0800 | [diff] [blame] | 266 | control_loops::drivetrain::SelectionHint::FAR_SHIP); |
James Kuszmaul | 7d1ef44 | 2019-03-23 20:20:50 -0700 | [diff] [blame] | 267 | } else { |
James Kuszmaul | 518640d | 2019-04-13 15:50:50 -0700 | [diff] [blame] | 268 | const double cargo_joy_y = data.GetAxis(kCargoSelectorY); |
| 269 | const double cargo_joy_x = data.GetAxis(kCargoSelectorX); |
| 270 | if (cargo_joy_y > 0.5) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 271 | target_selector_hint_builder.add_suggested_target( |
James Kuszmaul | 7077d34 | 2021-06-09 20:23:58 -0700 | [diff] [blame] | 272 | control_loops::drivetrain::SelectionHint::NEAR_SHIP); |
James Kuszmaul | 1b822b4 | 2019-04-14 14:27:35 -0700 | [diff] [blame] | 273 | } else if (cargo_joy_y < -0.5) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 274 | target_selector_hint_builder.add_suggested_target( |
Austin Schuh | 872723c | 2019-12-25 14:38:09 -0800 | [diff] [blame] | 275 | control_loops::drivetrain::SelectionHint::FAR_SHIP); |
James Kuszmaul | 518640d | 2019-04-13 15:50:50 -0700 | [diff] [blame] | 276 | } else if (::std::abs(cargo_joy_x) > 0.5) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 277 | target_selector_hint_builder.add_suggested_target( |
James Kuszmaul | 7077d34 | 2021-06-09 20:23:58 -0700 | [diff] [blame] | 278 | control_loops::drivetrain::SelectionHint::MID_SHIP); |
James Kuszmaul | 518640d | 2019-04-13 15:50:50 -0700 | [diff] [blame] | 279 | } else { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 280 | target_selector_hint_builder.add_suggested_target( |
James Kuszmaul | 7077d34 | 2021-06-09 20:23:58 -0700 | [diff] [blame] | 281 | control_loops::drivetrain::SelectionHint::NONE); |
James Kuszmaul | 518640d | 2019-04-13 15:50:50 -0700 | [diff] [blame] | 282 | } |
James Kuszmaul | 7d1ef44 | 2019-03-23 20:20:50 -0700 | [diff] [blame] | 283 | } |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 284 | if (builder.Send(target_selector_hint_builder.Finish()) != |
| 285 | aos::RawSender::Error::kOk) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 286 | AOS_LOG(ERROR, "Failed to send target selector hint.\n"); |
James Kuszmaul | 7d1ef44 | 2019-03-23 20:20:50 -0700 | [diff] [blame] | 287 | } |
| 288 | } |
| 289 | |
James Kuszmaul | 518640d | 2019-04-13 15:50:50 -0700 | [diff] [blame] | 290 | if (data.PosEdge(kResetLocalizerLeft)) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 291 | auto builder = localizer_control_sender_.MakeBuilder(); |
James Kuszmaul | 518640d | 2019-04-13 15:50:50 -0700 | [diff] [blame] | 292 | // Start at the left feeder station. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 293 | LocalizerControl::Builder localizer_control_builder = |
| 294 | builder.MakeBuilder<LocalizerControl>(); |
| 295 | localizer_control_builder.add_x(0.6); |
| 296 | localizer_control_builder.add_y(3.4); |
| 297 | localizer_control_builder.add_keep_current_theta(true); |
James Kuszmaul | 518640d | 2019-04-13 15:50:50 -0700 | [diff] [blame] | 298 | |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 299 | if (builder.Send(localizer_control_builder.Finish()) != |
| 300 | aos::RawSender::Error::kOk) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 301 | AOS_LOG(ERROR, "Failed to reset localizer.\n"); |
James Kuszmaul | 518640d | 2019-04-13 15:50:50 -0700 | [diff] [blame] | 302 | } |
| 303 | } |
| 304 | |
| 305 | if (data.PosEdge(kResetLocalizerRight)) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 306 | auto builder = localizer_control_sender_.MakeBuilder(); |
| 307 | // Start at the right feeder station. |
| 308 | LocalizerControl::Builder localizer_control_builder = |
| 309 | builder.MakeBuilder<LocalizerControl>(); |
| 310 | localizer_control_builder.add_x(0.6); |
| 311 | localizer_control_builder.add_y(-3.4); |
| 312 | localizer_control_builder.add_keep_current_theta(true); |
James Kuszmaul | 518640d | 2019-04-13 15:50:50 -0700 | [diff] [blame] | 313 | |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 314 | if (builder.Send(localizer_control_builder.Finish()) != |
| 315 | aos::RawSender::Error::kOk) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 316 | AOS_LOG(ERROR, "Failed to reset localizer.\n"); |
James Kuszmaul | 518640d | 2019-04-13 15:50:50 -0700 | [diff] [blame] | 317 | } |
| 318 | } |
| 319 | |
Austin Schuh | bb1df6f | 2019-03-17 18:15:43 -0700 | [diff] [blame] | 320 | if (data.PosEdge(kResetLocalizerLeftForwards)) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 321 | auto builder = localizer_control_sender_.MakeBuilder(); |
Austin Schuh | 3632f8d | 2019-03-22 21:14:14 -0700 | [diff] [blame] | 322 | // Start at the left feeder station. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 323 | LocalizerControl::Builder localizer_control_builder = |
| 324 | builder.MakeBuilder<LocalizerControl>(); |
| 325 | localizer_control_builder.add_x(0.4); |
| 326 | localizer_control_builder.add_y(3.4); |
| 327 | localizer_control_builder.add_theta(0.0); |
Austin Schuh | bb1df6f | 2019-03-17 18:15:43 -0700 | [diff] [blame] | 328 | |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 329 | if (builder.Send(localizer_control_builder.Finish()) != |
| 330 | aos::RawSender::Error::kOk) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 331 | AOS_LOG(ERROR, "Failed to reset localizer.\n"); |
Austin Schuh | bb1df6f | 2019-03-17 18:15:43 -0700 | [diff] [blame] | 332 | } |
| 333 | } |
| 334 | |
| 335 | if (data.PosEdge(kResetLocalizerLeftBackwards)) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 336 | auto builder = localizer_control_sender_.MakeBuilder(); |
Austin Schuh | bb1df6f | 2019-03-17 18:15:43 -0700 | [diff] [blame] | 337 | // Start at the left feeder station. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 338 | LocalizerControl::Builder localizer_control_builder = |
| 339 | builder.MakeBuilder<LocalizerControl>(); |
| 340 | localizer_control_builder.add_x(0.4); |
| 341 | localizer_control_builder.add_y(3.4); |
| 342 | localizer_control_builder.add_theta(M_PI); |
Austin Schuh | bb1df6f | 2019-03-17 18:15:43 -0700 | [diff] [blame] | 343 | |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 344 | if (builder.Send(localizer_control_builder.Finish()) != |
| 345 | aos::RawSender::Error::kOk) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 346 | AOS_LOG(ERROR, "Failed to reset localizer.\n"); |
Austin Schuh | bb1df6f | 2019-03-17 18:15:43 -0700 | [diff] [blame] | 347 | } |
| 348 | } |
| 349 | |
Austin Schuh | bb1df6f | 2019-03-17 18:15:43 -0700 | [diff] [blame] | 350 | if (data.PosEdge(kResetLocalizerRightForwards)) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 351 | auto builder = localizer_control_sender_.MakeBuilder(); |
Austin Schuh | 3632f8d | 2019-03-22 21:14:14 -0700 | [diff] [blame] | 352 | // Start at the right feeder station. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 353 | LocalizerControl::Builder localizer_control_builder = |
| 354 | builder.MakeBuilder<LocalizerControl>(); |
| 355 | localizer_control_builder.add_x(0.4); |
| 356 | localizer_control_builder.add_y(-3.4); |
| 357 | localizer_control_builder.add_theta(0.0); |
Austin Schuh | bb1df6f | 2019-03-17 18:15:43 -0700 | [diff] [blame] | 358 | |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 359 | if (builder.Send(localizer_control_builder.Finish()) != |
| 360 | aos::RawSender::Error::kOk) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 361 | AOS_LOG(ERROR, "Failed to reset localizer.\n"); |
Austin Schuh | bb1df6f | 2019-03-17 18:15:43 -0700 | [diff] [blame] | 362 | } |
| 363 | } |
| 364 | |
| 365 | if (data.PosEdge(kResetLocalizerRightBackwards)) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 366 | auto builder = localizer_control_sender_.MakeBuilder(); |
Austin Schuh | 3632f8d | 2019-03-22 21:14:14 -0700 | [diff] [blame] | 367 | // Start at the right feeder station. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 368 | LocalizerControl::Builder localizer_control_builder = |
| 369 | builder.MakeBuilder<LocalizerControl>(); |
| 370 | localizer_control_builder.add_x(0.4); |
| 371 | localizer_control_builder.add_y(-3.4); |
| 372 | localizer_control_builder.add_theta(M_PI); |
Austin Schuh | bb1df6f | 2019-03-17 18:15:43 -0700 | [diff] [blame] | 373 | |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 374 | if (builder.Send(localizer_control_builder.Finish()) != |
| 375 | aos::RawSender::Error::kOk) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 376 | AOS_LOG(ERROR, "Failed to reset localizer.\n"); |
James Kuszmaul | d8deb68 | 2019-03-10 10:38:42 -0700 | [diff] [blame] | 377 | } |
| 378 | } |
| 379 | |
James Kuszmaul | 1373886 | 2019-04-14 10:48:00 -0700 | [diff] [blame] | 380 | if (data.PosEdge(kRelease) && |
| 381 | monotonic_now > |
| 382 | last_release_button_press_ + ::std::chrono::milliseconds(500)) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 383 | if (superstructure_status_fetcher_->has_piece()) { |
James Kuszmaul | 1373886 | 2019-04-14 10:48:00 -0700 | [diff] [blame] | 384 | release_mode_ = ReleaseButtonMode::kRelease; |
| 385 | } else { |
| 386 | release_mode_ = ReleaseButtonMode::kBallIntake; |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | if (data.IsPressed(kRelease)) { |
| 391 | last_release_button_press_ = monotonic_now; |
| 392 | } |
| 393 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 394 | AOS_LOG(INFO, "has_piece: %d\n", |
| 395 | superstructure_status_fetcher_->has_piece()); |
Austin Schuh | 1a17e13 | 2019-02-17 15:05:06 -0800 | [diff] [blame] | 396 | if (data.IsPressed(kSuctionBall)) { |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame] | 397 | grab_piece_ = true; |
Austin Schuh | 1a17e13 | 2019-02-17 15:05:06 -0800 | [diff] [blame] | 398 | } else if (data.IsPressed(kSuctionHatch)) { |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame] | 399 | grab_piece_ = true; |
James Kuszmaul | 1373886 | 2019-04-14 10:48:00 -0700 | [diff] [blame] | 400 | } else if ((release_mode_ == ReleaseButtonMode::kRelease && |
| 401 | data.IsPressed(kRelease)) || |
Tyler Chatow | 5eeee90 | 2019-04-12 20:47:48 -0700 | [diff] [blame] | 402 | data.IsPressed(kReleaseButtonBoard) || |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 403 | !superstructure_status_fetcher_->has_piece()) { |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame] | 404 | grab_piece_ = false; |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 405 | AOS_LOG(INFO, "releasing due to other thing\n"); |
Austin Schuh | 1a17e13 | 2019-02-17 15:05:06 -0800 | [diff] [blame] | 406 | } |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 407 | |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 408 | if (data.IsPressed(kRocketBackwardUnpressed)) { |
| 409 | elevator_wrist_pos_ = kStowPos; |
| 410 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 411 | mutable_superstructure_goal->mutable_intake()->mutate_unsafe_goal(-1.2); |
| 412 | mutable_superstructure_goal->mutate_roller_voltage(0.0); |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 413 | |
James Kuszmaul | 1373886 | 2019-04-14 10:48:00 -0700 | [diff] [blame] | 414 | const bool kDoBallIntake = |
| 415 | (!climbed_ && release_mode_ == ReleaseButtonMode::kBallIntake && |
| 416 | data.IsPressed(kRelease)) || |
| 417 | data.GetAxis(kBallIntake) > 0.9; |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 418 | const bool kDoBallOutake = data.GetAxis(kBallOutake) > 0.9; |
| 419 | |
| 420 | if (data.IsPressed(kPanelSwitch)) { |
| 421 | switch_ball_ = false; |
| 422 | } else if (data.IsPressed(kCargoSwitch)) { |
| 423 | switch_ball_ = true; |
| 424 | } |
| 425 | |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 426 | if (switch_ball_) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 427 | if (superstructure_status_fetcher_->has_piece()) { |
| 428 | mutable_superstructure_goal->mutable_wrist() |
| 429 | ->mutable_profile_params() |
| 430 | ->mutate_max_acceleration(20); |
Austin Schuh | 1a17e13 | 2019-02-17 15:05:06 -0800 | [diff] [blame] | 431 | } |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 432 | |
| 433 | // Go to intake position and apply vacuum |
| 434 | if (data.IsPressed(kBallHPIntakeForward)) { |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame] | 435 | grab_piece_ = true; |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 436 | elevator_wrist_pos_ = kBallHPIntakeForwardPos; |
| 437 | } else if (data.IsPressed(kBallHPIntakeBackward)) { |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame] | 438 | grab_piece_ = true; |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 439 | elevator_wrist_pos_ = kBallHPIntakeBackwardPos; |
| 440 | } |
| 441 | |
| 442 | // Go to elevator/wrist position. Overrides intake position if pressed so |
| 443 | // we can re-grab the ball. |
| 444 | if (data.IsPressed(kRocketForwardLower)) { |
| 445 | elevator_wrist_pos_ = kBallForwardLowerPos; |
| 446 | } else if (data.IsPressed(kRocketBackwardLower)) { |
| 447 | elevator_wrist_pos_ = kBallBackwardLowerPos; |
| 448 | } else if (data.IsPressed(kRocketForwardMiddle)) { |
| 449 | elevator_wrist_pos_ = kBallForwardMiddlePos; |
| 450 | } else if (data.IsPressed(kRocketBackwardMiddle)) { |
| 451 | elevator_wrist_pos_ = kBallBackwardMiddlePos; |
| 452 | } else if (data.IsPressed(kRocketForwardUpper)) { |
| 453 | elevator_wrist_pos_ = kBallForwardUpperPos; |
| 454 | } else if (data.IsPressed(kRocketBackwardUpper)) { |
| 455 | elevator_wrist_pos_ = kBallBackwardUpperPos; |
| 456 | } else if (data.IsPressed(kCargoForward)) { |
| 457 | elevator_wrist_pos_ = kBallCargoForwardPos; |
| 458 | } else if (data.IsPressed(kCargoBackward)) { |
| 459 | elevator_wrist_pos_ = kBallCargoBackwardPos; |
| 460 | } |
Austin Schuh | 1a17e13 | 2019-02-17 15:05:06 -0800 | [diff] [blame] | 461 | } else { |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 462 | if (data.IsPressed(kPanelHPIntakeForward)) { |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame] | 463 | grab_piece_ = true; |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 464 | elevator_wrist_pos_ = kPanelHPIntakeForwrdPos; |
| 465 | } else if (data.IsPressed(kPanelHPIntakeBackward)) { |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame] | 466 | grab_piece_ = true; |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 467 | elevator_wrist_pos_ = kPanelHPIntakeBackwardPos; |
| 468 | } |
| 469 | |
| 470 | // Go to elevator/wrist position. Overrides intake position if pressed so |
| 471 | // we can re-grab the panel. |
| 472 | if (data.IsPressed(kRocketForwardLower)) { |
| 473 | elevator_wrist_pos_ = kPanelForwardLowerPos; |
| 474 | } else if (data.IsPressed(kRocketBackwardLower)) { |
| 475 | elevator_wrist_pos_ = kPanelBackwardLowerPos; |
| 476 | } else if (data.IsPressed(kRocketForwardMiddle)) { |
| 477 | elevator_wrist_pos_ = kPanelForwardMiddlePos; |
| 478 | } else if (data.IsPressed(kRocketBackwardMiddle)) { |
| 479 | elevator_wrist_pos_ = kPanelBackwardMiddlePos; |
| 480 | } else if (data.IsPressed(kRocketForwardUpper)) { |
| 481 | elevator_wrist_pos_ = kPanelForwardUpperPos; |
| 482 | } else if (data.IsPressed(kRocketBackwardUpper)) { |
| 483 | elevator_wrist_pos_ = kPanelBackwardUpperPos; |
Sabina Davis | e6fe6c5 | 2019-03-03 15:48:51 -0800 | [diff] [blame] | 484 | } else if (data.IsPressed(kCargoForward)) { |
| 485 | elevator_wrist_pos_ = kPanelCargoForwardPos; |
| 486 | } else if (data.IsPressed(kCargoBackward)) { |
| 487 | elevator_wrist_pos_ = kPanelCargoBackwardPos; |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 488 | } |
Austin Schuh | 1a17e13 | 2019-02-17 15:05:06 -0800 | [diff] [blame] | 489 | } |
| 490 | |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 491 | if (switch_ball_) { |
| 492 | if (kDoBallOutake || |
Austin Schuh | aab7e16 | 2019-03-13 22:44:58 -0700 | [diff] [blame] | 493 | (kDoBallIntake && |
Austin Schuh | 20dc050 | 2019-03-30 07:24:07 -0700 | [diff] [blame] | 494 | monotonic_now < last_not_has_piece_ + chrono::milliseconds(200))) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 495 | mutable_superstructure_goal->mutable_intake()->mutate_unsafe_goal(0.83); |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 496 | } |
Austin Schuh | 23a5163 | 2019-02-19 16:50:36 -0800 | [diff] [blame] | 497 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 498 | if (kDoBallIntake && !superstructure_status_fetcher_->has_piece()) { |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 499 | elevator_wrist_pos_ = kBallIntakePos; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 500 | mutable_superstructure_goal->mutate_roller_voltage(9.0); |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame] | 501 | grab_piece_ = true; |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 502 | } else { |
| 503 | if (kDoBallOutake) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 504 | mutable_superstructure_goal->mutate_roller_voltage(-6.0); |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 505 | } else { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 506 | mutable_superstructure_goal->mutate_roller_voltage(0.0); |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 507 | } |
| 508 | } |
Austin Schuh | 23a5163 | 2019-02-19 16:50:36 -0800 | [diff] [blame] | 509 | } |
Austin Schuh | 1a17e13 | 2019-02-17 15:05:06 -0800 | [diff] [blame] | 510 | |
Austin Schuh | 457bde3 | 2019-03-17 18:16:41 -0700 | [diff] [blame] | 511 | constexpr double kDeployStiltPosition = 0.5; |
| 512 | |
Austin Schuh | 457bde3 | 2019-03-17 18:16:41 -0700 | [diff] [blame] | 513 | if (data.IsPressed(kFallOver)) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 514 | mutable_superstructure_goal->mutable_stilts()->mutate_unsafe_goal(0.71); |
| 515 | mutable_superstructure_goal->mutable_stilts() |
| 516 | ->mutable_profile_params() |
| 517 | ->mutate_max_velocity(0.65); |
| 518 | mutable_superstructure_goal->mutable_stilts() |
| 519 | ->mutable_profile_params() |
| 520 | ->mutate_max_acceleration(1.25); |
Austin Schuh | e2f2248 | 2019-04-13 23:05:43 -0700 | [diff] [blame] | 521 | } else if (data.IsPressed(kHalfStilt)) { |
| 522 | was_above_ = false; |
James Kuszmaul | 7077d34 | 2021-06-09 20:23:58 -0700 | [diff] [blame] | 523 | mutable_superstructure_goal->mutable_stilts()->mutate_unsafe_goal(0.345); |
| 524 | mutable_superstructure_goal->mutable_stilts() |
| 525 | ->mutable_profile_params() |
| 526 | ->mutate_max_velocity(0.65); |
Austin Schuh | e2f2248 | 2019-04-13 23:05:43 -0700 | [diff] [blame] | 527 | } else if (data.IsPressed(kDeployStilt) || was_above_) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 528 | mutable_superstructure_goal->mutable_stilts()->mutate_unsafe_goal( |
| 529 | kDeployStiltPosition); |
| 530 | mutable_superstructure_goal->mutable_stilts() |
| 531 | ->mutable_profile_params() |
| 532 | ->mutate_max_velocity(0.65); |
| 533 | mutable_superstructure_goal->mutable_stilts() |
| 534 | ->mutable_profile_params() |
| 535 | ->mutate_max_acceleration(2.0); |
Austin Schuh | 457bde3 | 2019-03-17 18:16:41 -0700 | [diff] [blame] | 536 | } else { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 537 | mutable_superstructure_goal->mutable_stilts()->mutate_unsafe_goal(0.005); |
| 538 | mutable_superstructure_goal->mutable_stilts() |
| 539 | ->mutable_profile_params() |
| 540 | ->mutate_max_velocity(0.65); |
| 541 | mutable_superstructure_goal->mutable_stilts() |
| 542 | ->mutable_profile_params() |
| 543 | ->mutate_max_acceleration(2.0); |
Austin Schuh | 457bde3 | 2019-03-17 18:16:41 -0700 | [diff] [blame] | 544 | } |
| 545 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 546 | if (superstructure_status_fetcher_->stilts()->position() > 0.1) { |
Austin Schuh | 02be8b9 | 2019-03-24 16:04:14 -0700 | [diff] [blame] | 547 | elevator_wrist_pos_ = kClimbPos; |
James Kuszmaul | 1373886 | 2019-04-14 10:48:00 -0700 | [diff] [blame] | 548 | climbed_ = true; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 549 | mutable_superstructure_goal->mutable_wrist() |
| 550 | ->mutable_profile_params() |
| 551 | ->mutate_max_acceleration(10); |
| 552 | mutable_superstructure_goal->mutable_elevator() |
| 553 | ->mutable_profile_params() |
| 554 | ->mutate_max_acceleration(6); |
Austin Schuh | 02be8b9 | 2019-03-24 16:04:14 -0700 | [diff] [blame] | 555 | } |
| 556 | |
Austin Schuh | e2f2248 | 2019-04-13 23:05:43 -0700 | [diff] [blame] | 557 | // If we've been asked to go above deploy and made it up that high, latch |
| 558 | // was_above. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 559 | if (mutable_superstructure_goal->stilts()->unsafe_goal() > |
| 560 | kDeployStiltPosition && |
| 561 | superstructure_status_fetcher_->stilts()->position() >= |
Austin Schuh | 170f495 | 2019-06-29 18:58:30 -0700 | [diff] [blame] | 562 | kDeployStiltPosition) { |
Austin Schuh | e2f2248 | 2019-04-13 23:05:43 -0700 | [diff] [blame] | 563 | was_above_ = true; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 564 | } else if ((superstructure_position_fetcher_->platform_left_detect() && |
| 565 | superstructure_position_fetcher_->platform_right_detect()) && |
Austin Schuh | e2f2248 | 2019-04-13 23:05:43 -0700 | [diff] [blame] | 566 | !data.IsPressed(kDeployStilt) && !data.IsPressed(kFallOver)) { |
Austin Schuh | e2f2248 | 2019-04-13 23:05:43 -0700 | [diff] [blame] | 567 | was_above_ = false; |
| 568 | } |
| 569 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 570 | if (superstructure_status_fetcher_->stilts()->position() > |
Austin Schuh | 170f495 | 2019-06-29 18:58:30 -0700 | [diff] [blame] | 571 | kDeployStiltPosition && |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 572 | mutable_superstructure_goal->stilts()->unsafe_goal() == |
| 573 | kDeployStiltPosition) { |
| 574 | mutable_superstructure_goal->mutable_stilts() |
| 575 | ->mutable_profile_params() |
| 576 | ->mutate_max_velocity(0.30); |
| 577 | mutable_superstructure_goal->mutable_stilts() |
| 578 | ->mutable_profile_params() |
| 579 | ->mutate_max_acceleration(0.75); |
Austin Schuh | 457bde3 | 2019-03-17 18:16:41 -0700 | [diff] [blame] | 580 | } |
| 581 | |
James Kuszmaul | 1373886 | 2019-04-14 10:48:00 -0700 | [diff] [blame] | 582 | if ((release_mode_ == ReleaseButtonMode::kRelease && |
| 583 | data.IsPressed(kRelease)) || |
| 584 | data.IsPressed(kReleaseButtonBoard)) { |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame] | 585 | grab_piece_ = false; |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 586 | AOS_LOG(INFO, "Releasing due to button\n"); |
Austin Schuh | 1a17e13 | 2019-02-17 15:05:06 -0800 | [diff] [blame] | 587 | } |
| 588 | |
Austin Schuh | 6bdcc37 | 2024-06-27 14:49:11 -0700 | [diff] [blame] | 589 | auto mutable_suction = mutable_superstructure_goal->mutable_suction(); |
| 590 | CHECK(mutable_suction != nullptr); |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame] | 591 | if (switch_ball_) { |
Austin Schuh | 6bdcc37 | 2024-06-27 14:49:11 -0700 | [diff] [blame] | 592 | mutable_suction->mutate_gamepiece_mode(0); |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame] | 593 | } else { |
Austin Schuh | 6bdcc37 | 2024-06-27 14:49:11 -0700 | [diff] [blame] | 594 | mutable_suction->mutate_gamepiece_mode(1); |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame] | 595 | } |
| 596 | |
Tyler Chatow | e024145 | 2019-03-08 21:07:50 -0800 | [diff] [blame] | 597 | vision_control_.set_flip_image(elevator_wrist_pos_.wrist < 0); |
| 598 | |
Austin Schuh | 6bdcc37 | 2024-06-27 14:49:11 -0700 | [diff] [blame] | 599 | mutable_suction->mutate_grab_piece(grab_piece_); |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 600 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 601 | mutable_superstructure_goal->mutable_elevator()->mutate_unsafe_goal( |
| 602 | elevator_wrist_pos_.elevator); |
| 603 | mutable_superstructure_goal->mutable_wrist()->mutate_unsafe_goal( |
| 604 | elevator_wrist_pos_.wrist); |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 605 | |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 606 | if (main_superstructure_goal_builder.Send(superstructure_goal_offset) != |
| 607 | aos::RawSender::Error::kOk) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 608 | AOS_LOG(ERROR, "Sending superstructure goal failed.\n"); |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 609 | } |
Tyler Chatow | e024145 | 2019-03-08 21:07:50 -0800 | [diff] [blame] | 610 | |
Austin Schuh | aab7e16 | 2019-03-13 22:44:58 -0700 | [diff] [blame] | 611 | if (monotonic_now > |
| 612 | last_vision_control_ + ::std::chrono::milliseconds(50)) { |
Tyler Chatow | 4fedeea | 2019-03-10 15:33:36 -0700 | [diff] [blame] | 613 | video_tx_->Send(vision_control_); |
Austin Schuh | aab7e16 | 2019-03-13 22:44:58 -0700 | [diff] [blame] | 614 | last_vision_control_ = monotonic_now; |
Tyler Chatow | 4fedeea | 2019-03-10 15:33:36 -0700 | [diff] [blame] | 615 | } |
Austin Schuh | 4e2629d | 2019-03-28 14:44:37 -0700 | [diff] [blame] | 616 | |
| 617 | { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 618 | auto builder = camera_log_sender_.MakeBuilder(); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 619 | (void)builder.Send( |
| 620 | CreateCameraLog(*builder.fbb(), data.IsPressed(kCameraLog))); |
Austin Schuh | 4e2629d | 2019-03-28 14:44:37 -0700 | [diff] [blame] | 621 | } |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 622 | } |
| 623 | |
| 624 | private: |
Austin Schuh | c087b10 | 2019-05-12 15:33:12 -0700 | [diff] [blame] | 625 | ::aos::Sender<::y2019::control_loops::drivetrain::TargetSelectorHint> |
| 626 | target_selector_hint_sender_; |
| 627 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 628 | ::aos::Sender<LocalizerControl> localizer_control_sender_; |
Austin Schuh | eb99d07 | 2019-05-12 21:03:38 -0700 | [diff] [blame] | 629 | |
Austin Schuh | 5671a8c | 2019-05-19 17:01:04 -0700 | [diff] [blame] | 630 | ::aos::Sender<::y2019::CameraLog> camera_log_sender_; |
| 631 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 632 | ::aos::Fetcher<superstructure::Goal> superstructure_goal_fetcher_; |
Austin Schuh | 170f495 | 2019-06-29 18:58:30 -0700 | [diff] [blame] | 633 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 634 | ::aos::Sender<superstructure::Goal> superstructure_goal_sender_; |
Austin Schuh | 170f495 | 2019-06-29 18:58:30 -0700 | [diff] [blame] | 635 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 636 | ::aos::Fetcher<superstructure::Position> superstructure_position_fetcher_; |
| 637 | ::aos::Fetcher<superstructure::Status> superstructure_status_fetcher_; |
Austin Schuh | 170f495 | 2019-06-29 18:58:30 -0700 | [diff] [blame] | 638 | |
Austin Schuh | e2f2248 | 2019-04-13 23:05:43 -0700 | [diff] [blame] | 639 | // Bool to track if we've been above the deploy position. Once this bool is |
| 640 | // set, don't let the stilts retract until we see the platform. |
| 641 | bool was_above_ = false; |
| 642 | |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 643 | // Current goals here. |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 644 | ElevatorWristPosition elevator_wrist_pos_ = kStowPos; |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame] | 645 | bool grab_piece_ = false; |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 646 | |
| 647 | bool switch_ball_ = false; |
Tyler Chatow | e024145 | 2019-03-08 21:07:50 -0800 | [diff] [blame] | 648 | |
James Kuszmaul | 1373886 | 2019-04-14 10:48:00 -0700 | [diff] [blame] | 649 | bool climbed_ = false; |
| 650 | |
| 651 | enum class ReleaseButtonMode { |
| 652 | kBallIntake, |
| 653 | kRelease, |
| 654 | }; |
| 655 | |
| 656 | ReleaseButtonMode release_mode_ = ReleaseButtonMode::kRelease; |
| 657 | aos::monotonic_clock::time_point last_release_button_press_ = |
| 658 | aos::monotonic_clock::min_time; |
| 659 | |
Tyler Chatow | e024145 | 2019-03-08 21:07:50 -0800 | [diff] [blame] | 660 | VisionControl vision_control_; |
| 661 | ::std::unique_ptr<ProtoTXUdpSocket<VisionControl>> video_tx_; |
Tyler Chatow | 4fedeea | 2019-03-10 15:33:36 -0700 | [diff] [blame] | 662 | ::aos::monotonic_clock::time_point last_vision_control_ = |
| 663 | ::aos::monotonic_clock::time_point::min(); |
Austin Schuh | aab7e16 | 2019-03-13 22:44:58 -0700 | [diff] [blame] | 664 | |
| 665 | // Time at which we last did not have a game piece. |
| 666 | ::aos::monotonic_clock::time_point last_not_has_piece_ = |
| 667 | ::aos::monotonic_clock::time_point::min(); |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 668 | }; |
| 669 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 670 | } // namespace y2019::input::joysticks |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 671 | |
Austin Schuh | 094d09b | 2020-11-20 23:26:52 -0800 | [diff] [blame] | 672 | int main(int argc, char **argv) { |
| 673 | ::aos::InitGoogle(&argc, &argv); |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 674 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 675 | aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
Austin Schuh | c5fa6d9 | 2022-02-25 14:36:28 -0800 | [diff] [blame] | 676 | aos::configuration::ReadConfig("aos_config.json"); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 677 | |
| 678 | ::aos::ShmEventLoop event_loop(&config.message()); |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 679 | ::y2019::input::joysticks::Reader reader(&event_loop); |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 680 | |
| 681 | event_loop.Run(); |
| 682 | |
Austin Schuh | ae87e31 | 2020-08-01 16:15:01 -0700 | [diff] [blame] | 683 | return 0; |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 684 | } |