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