Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 1 | #include <math.h> |
| 2 | #include <stdio.h> |
| 3 | #include <string.h> |
| 4 | #include <unistd.h> |
James Kuszmaul | 7077d34 | 2021-06-09 20:23:58 -0700 | [diff] [blame] | 5 | |
Tyler Chatow | 4fedeea | 2019-03-10 15:33:36 -0700 | [diff] [blame] | 6 | #include <chrono> |
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 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 286 | if (!builder.Send(target_selector_hint_builder.Finish())) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 287 | AOS_LOG(ERROR, "Failed to send target selector hint.\n"); |
James Kuszmaul | 7d1ef44 | 2019-03-23 20:20:50 -0700 | [diff] [blame] | 288 | } |
| 289 | } |
| 290 | |
James Kuszmaul | 518640d | 2019-04-13 15:50:50 -0700 | [diff] [blame] | 291 | if (data.PosEdge(kResetLocalizerLeft)) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 292 | auto builder = localizer_control_sender_.MakeBuilder(); |
James Kuszmaul | 518640d | 2019-04-13 15:50:50 -0700 | [diff] [blame] | 293 | // Start at the left feeder station. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 294 | LocalizerControl::Builder localizer_control_builder = |
| 295 | builder.MakeBuilder<LocalizerControl>(); |
| 296 | localizer_control_builder.add_x(0.6); |
| 297 | localizer_control_builder.add_y(3.4); |
| 298 | localizer_control_builder.add_keep_current_theta(true); |
James Kuszmaul | 518640d | 2019-04-13 15:50:50 -0700 | [diff] [blame] | 299 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 300 | if (!builder.Send(localizer_control_builder.Finish())) { |
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 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 314 | if (!builder.Send(localizer_control_builder.Finish())) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 315 | AOS_LOG(ERROR, "Failed to reset localizer.\n"); |
James Kuszmaul | 518640d | 2019-04-13 15:50:50 -0700 | [diff] [blame] | 316 | } |
| 317 | } |
| 318 | |
Austin Schuh | bb1df6f | 2019-03-17 18:15:43 -0700 | [diff] [blame] | 319 | if (data.PosEdge(kResetLocalizerLeftForwards)) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 320 | auto builder = localizer_control_sender_.MakeBuilder(); |
Austin Schuh | 3632f8d | 2019-03-22 21:14:14 -0700 | [diff] [blame] | 321 | // Start at the left feeder station. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 322 | LocalizerControl::Builder localizer_control_builder = |
| 323 | builder.MakeBuilder<LocalizerControl>(); |
| 324 | localizer_control_builder.add_x(0.4); |
| 325 | localizer_control_builder.add_y(3.4); |
| 326 | localizer_control_builder.add_theta(0.0); |
Austin Schuh | bb1df6f | 2019-03-17 18:15:43 -0700 | [diff] [blame] | 327 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 328 | if (!builder.Send(localizer_control_builder.Finish())) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 329 | AOS_LOG(ERROR, "Failed to reset localizer.\n"); |
Austin Schuh | bb1df6f | 2019-03-17 18:15:43 -0700 | [diff] [blame] | 330 | } |
| 331 | } |
| 332 | |
| 333 | if (data.PosEdge(kResetLocalizerLeftBackwards)) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 334 | auto builder = localizer_control_sender_.MakeBuilder(); |
Austin Schuh | bb1df6f | 2019-03-17 18:15:43 -0700 | [diff] [blame] | 335 | // Start at the left feeder station. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 336 | LocalizerControl::Builder localizer_control_builder = |
| 337 | builder.MakeBuilder<LocalizerControl>(); |
| 338 | localizer_control_builder.add_x(0.4); |
| 339 | localizer_control_builder.add_y(3.4); |
| 340 | localizer_control_builder.add_theta(M_PI); |
Austin Schuh | bb1df6f | 2019-03-17 18:15:43 -0700 | [diff] [blame] | 341 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 342 | if (!builder.Send(localizer_control_builder.Finish())) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 343 | AOS_LOG(ERROR, "Failed to reset localizer.\n"); |
Austin Schuh | bb1df6f | 2019-03-17 18:15:43 -0700 | [diff] [blame] | 344 | } |
| 345 | } |
| 346 | |
Austin Schuh | bb1df6f | 2019-03-17 18:15:43 -0700 | [diff] [blame] | 347 | if (data.PosEdge(kResetLocalizerRightForwards)) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 348 | auto builder = localizer_control_sender_.MakeBuilder(); |
Austin Schuh | 3632f8d | 2019-03-22 21:14:14 -0700 | [diff] [blame] | 349 | // Start at the right feeder station. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 350 | LocalizerControl::Builder localizer_control_builder = |
| 351 | builder.MakeBuilder<LocalizerControl>(); |
| 352 | localizer_control_builder.add_x(0.4); |
| 353 | localizer_control_builder.add_y(-3.4); |
| 354 | localizer_control_builder.add_theta(0.0); |
Austin Schuh | bb1df6f | 2019-03-17 18:15:43 -0700 | [diff] [blame] | 355 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 356 | if (!builder.Send(localizer_control_builder.Finish())) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 357 | AOS_LOG(ERROR, "Failed to reset localizer.\n"); |
Austin Schuh | bb1df6f | 2019-03-17 18:15:43 -0700 | [diff] [blame] | 358 | } |
| 359 | } |
| 360 | |
| 361 | if (data.PosEdge(kResetLocalizerRightBackwards)) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 362 | auto builder = localizer_control_sender_.MakeBuilder(); |
Austin Schuh | 3632f8d | 2019-03-22 21:14:14 -0700 | [diff] [blame] | 363 | // Start at the right feeder station. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 364 | LocalizerControl::Builder localizer_control_builder = |
| 365 | builder.MakeBuilder<LocalizerControl>(); |
| 366 | localizer_control_builder.add_x(0.4); |
| 367 | localizer_control_builder.add_y(-3.4); |
| 368 | localizer_control_builder.add_theta(M_PI); |
Austin Schuh | bb1df6f | 2019-03-17 18:15:43 -0700 | [diff] [blame] | 369 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 370 | if (!builder.Send(localizer_control_builder.Finish())) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 371 | AOS_LOG(ERROR, "Failed to reset localizer.\n"); |
James Kuszmaul | d8deb68 | 2019-03-10 10:38:42 -0700 | [diff] [blame] | 372 | } |
| 373 | } |
| 374 | |
James Kuszmaul | 1373886 | 2019-04-14 10:48:00 -0700 | [diff] [blame] | 375 | if (data.PosEdge(kRelease) && |
| 376 | monotonic_now > |
| 377 | last_release_button_press_ + ::std::chrono::milliseconds(500)) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 378 | if (superstructure_status_fetcher_->has_piece()) { |
James Kuszmaul | 1373886 | 2019-04-14 10:48:00 -0700 | [diff] [blame] | 379 | release_mode_ = ReleaseButtonMode::kRelease; |
| 380 | } else { |
| 381 | release_mode_ = ReleaseButtonMode::kBallIntake; |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | if (data.IsPressed(kRelease)) { |
| 386 | last_release_button_press_ = monotonic_now; |
| 387 | } |
| 388 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 389 | AOS_LOG(INFO, "has_piece: %d\n", |
| 390 | superstructure_status_fetcher_->has_piece()); |
Austin Schuh | 1a17e13 | 2019-02-17 15:05:06 -0800 | [diff] [blame] | 391 | if (data.IsPressed(kSuctionBall)) { |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame] | 392 | grab_piece_ = true; |
Austin Schuh | 1a17e13 | 2019-02-17 15:05:06 -0800 | [diff] [blame] | 393 | } else if (data.IsPressed(kSuctionHatch)) { |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame] | 394 | grab_piece_ = true; |
James Kuszmaul | 1373886 | 2019-04-14 10:48:00 -0700 | [diff] [blame] | 395 | } else if ((release_mode_ == ReleaseButtonMode::kRelease && |
| 396 | data.IsPressed(kRelease)) || |
Tyler Chatow | 5eeee90 | 2019-04-12 20:47:48 -0700 | [diff] [blame] | 397 | data.IsPressed(kReleaseButtonBoard) || |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 398 | !superstructure_status_fetcher_->has_piece()) { |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame] | 399 | grab_piece_ = false; |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 400 | AOS_LOG(INFO, "releasing due to other thing\n"); |
Austin Schuh | 1a17e13 | 2019-02-17 15:05:06 -0800 | [diff] [blame] | 401 | } |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 402 | |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 403 | if (data.IsPressed(kRocketBackwardUnpressed)) { |
| 404 | elevator_wrist_pos_ = kStowPos; |
| 405 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 406 | mutable_superstructure_goal->mutable_intake()->mutate_unsafe_goal(-1.2); |
| 407 | mutable_superstructure_goal->mutate_roller_voltage(0.0); |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 408 | |
James Kuszmaul | 1373886 | 2019-04-14 10:48:00 -0700 | [diff] [blame] | 409 | const bool kDoBallIntake = |
| 410 | (!climbed_ && release_mode_ == ReleaseButtonMode::kBallIntake && |
| 411 | data.IsPressed(kRelease)) || |
| 412 | data.GetAxis(kBallIntake) > 0.9; |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 413 | const bool kDoBallOutake = data.GetAxis(kBallOutake) > 0.9; |
| 414 | |
| 415 | if (data.IsPressed(kPanelSwitch)) { |
| 416 | switch_ball_ = false; |
| 417 | } else if (data.IsPressed(kCargoSwitch)) { |
| 418 | switch_ball_ = true; |
| 419 | } |
| 420 | |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 421 | if (switch_ball_) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 422 | if (superstructure_status_fetcher_->has_piece()) { |
| 423 | mutable_superstructure_goal->mutable_wrist() |
| 424 | ->mutable_profile_params() |
| 425 | ->mutate_max_acceleration(20); |
Austin Schuh | 1a17e13 | 2019-02-17 15:05:06 -0800 | [diff] [blame] | 426 | } |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 427 | |
| 428 | // Go to intake position and apply vacuum |
| 429 | if (data.IsPressed(kBallHPIntakeForward)) { |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame] | 430 | grab_piece_ = true; |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 431 | elevator_wrist_pos_ = kBallHPIntakeForwardPos; |
| 432 | } else if (data.IsPressed(kBallHPIntakeBackward)) { |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame] | 433 | grab_piece_ = true; |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 434 | elevator_wrist_pos_ = kBallHPIntakeBackwardPos; |
| 435 | } |
| 436 | |
| 437 | // Go to elevator/wrist position. Overrides intake position if pressed so |
| 438 | // we can re-grab the ball. |
| 439 | if (data.IsPressed(kRocketForwardLower)) { |
| 440 | elevator_wrist_pos_ = kBallForwardLowerPos; |
| 441 | } else if (data.IsPressed(kRocketBackwardLower)) { |
| 442 | elevator_wrist_pos_ = kBallBackwardLowerPos; |
| 443 | } else if (data.IsPressed(kRocketForwardMiddle)) { |
| 444 | elevator_wrist_pos_ = kBallForwardMiddlePos; |
| 445 | } else if (data.IsPressed(kRocketBackwardMiddle)) { |
| 446 | elevator_wrist_pos_ = kBallBackwardMiddlePos; |
| 447 | } else if (data.IsPressed(kRocketForwardUpper)) { |
| 448 | elevator_wrist_pos_ = kBallForwardUpperPos; |
| 449 | } else if (data.IsPressed(kRocketBackwardUpper)) { |
| 450 | elevator_wrist_pos_ = kBallBackwardUpperPos; |
| 451 | } else if (data.IsPressed(kCargoForward)) { |
| 452 | elevator_wrist_pos_ = kBallCargoForwardPos; |
| 453 | } else if (data.IsPressed(kCargoBackward)) { |
| 454 | elevator_wrist_pos_ = kBallCargoBackwardPos; |
| 455 | } |
Austin Schuh | 1a17e13 | 2019-02-17 15:05:06 -0800 | [diff] [blame] | 456 | } else { |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 457 | if (data.IsPressed(kPanelHPIntakeForward)) { |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame] | 458 | grab_piece_ = true; |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 459 | elevator_wrist_pos_ = kPanelHPIntakeForwrdPos; |
| 460 | } else if (data.IsPressed(kPanelHPIntakeBackward)) { |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame] | 461 | grab_piece_ = true; |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 462 | elevator_wrist_pos_ = kPanelHPIntakeBackwardPos; |
| 463 | } |
| 464 | |
| 465 | // Go to elevator/wrist position. Overrides intake position if pressed so |
| 466 | // we can re-grab the panel. |
| 467 | if (data.IsPressed(kRocketForwardLower)) { |
| 468 | elevator_wrist_pos_ = kPanelForwardLowerPos; |
| 469 | } else if (data.IsPressed(kRocketBackwardLower)) { |
| 470 | elevator_wrist_pos_ = kPanelBackwardLowerPos; |
| 471 | } else if (data.IsPressed(kRocketForwardMiddle)) { |
| 472 | elevator_wrist_pos_ = kPanelForwardMiddlePos; |
| 473 | } else if (data.IsPressed(kRocketBackwardMiddle)) { |
| 474 | elevator_wrist_pos_ = kPanelBackwardMiddlePos; |
| 475 | } else if (data.IsPressed(kRocketForwardUpper)) { |
| 476 | elevator_wrist_pos_ = kPanelForwardUpperPos; |
| 477 | } else if (data.IsPressed(kRocketBackwardUpper)) { |
| 478 | elevator_wrist_pos_ = kPanelBackwardUpperPos; |
Sabina Davis | e6fe6c5 | 2019-03-03 15:48:51 -0800 | [diff] [blame] | 479 | } else if (data.IsPressed(kCargoForward)) { |
| 480 | elevator_wrist_pos_ = kPanelCargoForwardPos; |
| 481 | } else if (data.IsPressed(kCargoBackward)) { |
| 482 | elevator_wrist_pos_ = kPanelCargoBackwardPos; |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 483 | } |
Austin Schuh | 1a17e13 | 2019-02-17 15:05:06 -0800 | [diff] [blame] | 484 | } |
| 485 | |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 486 | if (switch_ball_) { |
| 487 | if (kDoBallOutake || |
Austin Schuh | aab7e16 | 2019-03-13 22:44:58 -0700 | [diff] [blame] | 488 | (kDoBallIntake && |
Austin Schuh | 20dc050 | 2019-03-30 07:24:07 -0700 | [diff] [blame] | 489 | monotonic_now < last_not_has_piece_ + chrono::milliseconds(200))) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 490 | mutable_superstructure_goal->mutable_intake()->mutate_unsafe_goal(0.83); |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 491 | } |
Austin Schuh | 23a5163 | 2019-02-19 16:50:36 -0800 | [diff] [blame] | 492 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 493 | if (kDoBallIntake && !superstructure_status_fetcher_->has_piece()) { |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 494 | elevator_wrist_pos_ = kBallIntakePos; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 495 | mutable_superstructure_goal->mutate_roller_voltage(9.0); |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame] | 496 | grab_piece_ = true; |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 497 | } else { |
| 498 | if (kDoBallOutake) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 499 | mutable_superstructure_goal->mutate_roller_voltage(-6.0); |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 500 | } else { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 501 | mutable_superstructure_goal->mutate_roller_voltage(0.0); |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 502 | } |
| 503 | } |
Austin Schuh | 23a5163 | 2019-02-19 16:50:36 -0800 | [diff] [blame] | 504 | } |
Austin Schuh | 1a17e13 | 2019-02-17 15:05:06 -0800 | [diff] [blame] | 505 | |
Austin Schuh | 457bde3 | 2019-03-17 18:16:41 -0700 | [diff] [blame] | 506 | constexpr double kDeployStiltPosition = 0.5; |
| 507 | |
Austin Schuh | 457bde3 | 2019-03-17 18:16:41 -0700 | [diff] [blame] | 508 | if (data.IsPressed(kFallOver)) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 509 | mutable_superstructure_goal->mutable_stilts()->mutate_unsafe_goal(0.71); |
| 510 | mutable_superstructure_goal->mutable_stilts() |
| 511 | ->mutable_profile_params() |
| 512 | ->mutate_max_velocity(0.65); |
| 513 | mutable_superstructure_goal->mutable_stilts() |
| 514 | ->mutable_profile_params() |
| 515 | ->mutate_max_acceleration(1.25); |
Austin Schuh | e2f2248 | 2019-04-13 23:05:43 -0700 | [diff] [blame] | 516 | } else if (data.IsPressed(kHalfStilt)) { |
| 517 | was_above_ = false; |
James Kuszmaul | 7077d34 | 2021-06-09 20:23:58 -0700 | [diff] [blame] | 518 | mutable_superstructure_goal->mutable_stilts()->mutate_unsafe_goal(0.345); |
| 519 | mutable_superstructure_goal->mutable_stilts() |
| 520 | ->mutable_profile_params() |
| 521 | ->mutate_max_velocity(0.65); |
Austin Schuh | e2f2248 | 2019-04-13 23:05:43 -0700 | [diff] [blame] | 522 | } else if (data.IsPressed(kDeployStilt) || was_above_) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 523 | mutable_superstructure_goal->mutable_stilts()->mutate_unsafe_goal( |
| 524 | kDeployStiltPosition); |
| 525 | mutable_superstructure_goal->mutable_stilts() |
| 526 | ->mutable_profile_params() |
| 527 | ->mutate_max_velocity(0.65); |
| 528 | mutable_superstructure_goal->mutable_stilts() |
| 529 | ->mutable_profile_params() |
| 530 | ->mutate_max_acceleration(2.0); |
Austin Schuh | 457bde3 | 2019-03-17 18:16:41 -0700 | [diff] [blame] | 531 | } else { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 532 | mutable_superstructure_goal->mutable_stilts()->mutate_unsafe_goal(0.005); |
| 533 | mutable_superstructure_goal->mutable_stilts() |
| 534 | ->mutable_profile_params() |
| 535 | ->mutate_max_velocity(0.65); |
| 536 | mutable_superstructure_goal->mutable_stilts() |
| 537 | ->mutable_profile_params() |
| 538 | ->mutate_max_acceleration(2.0); |
Austin Schuh | 457bde3 | 2019-03-17 18:16:41 -0700 | [diff] [blame] | 539 | } |
| 540 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 541 | if (superstructure_status_fetcher_->stilts()->position() > 0.1) { |
Austin Schuh | 02be8b9 | 2019-03-24 16:04:14 -0700 | [diff] [blame] | 542 | elevator_wrist_pos_ = kClimbPos; |
James Kuszmaul | 1373886 | 2019-04-14 10:48:00 -0700 | [diff] [blame] | 543 | climbed_ = true; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 544 | mutable_superstructure_goal->mutable_wrist() |
| 545 | ->mutable_profile_params() |
| 546 | ->mutate_max_acceleration(10); |
| 547 | mutable_superstructure_goal->mutable_elevator() |
| 548 | ->mutable_profile_params() |
| 549 | ->mutate_max_acceleration(6); |
Austin Schuh | 02be8b9 | 2019-03-24 16:04:14 -0700 | [diff] [blame] | 550 | } |
| 551 | |
Austin Schuh | e2f2248 | 2019-04-13 23:05:43 -0700 | [diff] [blame] | 552 | // If we've been asked to go above deploy and made it up that high, latch |
| 553 | // was_above. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 554 | if (mutable_superstructure_goal->stilts()->unsafe_goal() > |
| 555 | kDeployStiltPosition && |
| 556 | superstructure_status_fetcher_->stilts()->position() >= |
Austin Schuh | 170f495 | 2019-06-29 18:58:30 -0700 | [diff] [blame] | 557 | kDeployStiltPosition) { |
Austin Schuh | e2f2248 | 2019-04-13 23:05:43 -0700 | [diff] [blame] | 558 | was_above_ = true; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 559 | } else if ((superstructure_position_fetcher_->platform_left_detect() && |
| 560 | superstructure_position_fetcher_->platform_right_detect()) && |
Austin Schuh | e2f2248 | 2019-04-13 23:05:43 -0700 | [diff] [blame] | 561 | !data.IsPressed(kDeployStilt) && !data.IsPressed(kFallOver)) { |
Austin Schuh | e2f2248 | 2019-04-13 23:05:43 -0700 | [diff] [blame] | 562 | was_above_ = false; |
| 563 | } |
| 564 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 565 | if (superstructure_status_fetcher_->stilts()->position() > |
Austin Schuh | 170f495 | 2019-06-29 18:58:30 -0700 | [diff] [blame] | 566 | kDeployStiltPosition && |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 567 | mutable_superstructure_goal->stilts()->unsafe_goal() == |
| 568 | kDeployStiltPosition) { |
| 569 | mutable_superstructure_goal->mutable_stilts() |
| 570 | ->mutable_profile_params() |
| 571 | ->mutate_max_velocity(0.30); |
| 572 | mutable_superstructure_goal->mutable_stilts() |
| 573 | ->mutable_profile_params() |
| 574 | ->mutate_max_acceleration(0.75); |
Austin Schuh | 457bde3 | 2019-03-17 18:16:41 -0700 | [diff] [blame] | 575 | } |
| 576 | |
James Kuszmaul | 1373886 | 2019-04-14 10:48:00 -0700 | [diff] [blame] | 577 | if ((release_mode_ == ReleaseButtonMode::kRelease && |
| 578 | data.IsPressed(kRelease)) || |
| 579 | data.IsPressed(kReleaseButtonBoard)) { |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame] | 580 | grab_piece_ = false; |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 581 | AOS_LOG(INFO, "Releasing due to button\n"); |
Austin Schuh | 1a17e13 | 2019-02-17 15:05:06 -0800 | [diff] [blame] | 582 | } |
| 583 | |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame] | 584 | if (switch_ball_) { |
James Kuszmaul | 2457eb7 | 2019-12-15 15:37:18 -0800 | [diff] [blame] | 585 | CHECK_NOTNULL(mutable_superstructure_goal->mutable_suction()) |
| 586 | ->mutate_gamepiece_mode(0); |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame] | 587 | } else { |
James Kuszmaul | 2457eb7 | 2019-12-15 15:37:18 -0800 | [diff] [blame] | 588 | CHECK_NOTNULL(mutable_superstructure_goal->mutable_suction()) |
| 589 | ->mutate_gamepiece_mode(1); |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame] | 590 | } |
| 591 | |
Tyler Chatow | e024145 | 2019-03-08 21:07:50 -0800 | [diff] [blame] | 592 | vision_control_.set_flip_image(elevator_wrist_pos_.wrist < 0); |
| 593 | |
James Kuszmaul | 2457eb7 | 2019-12-15 15:37:18 -0800 | [diff] [blame] | 594 | CHECK_NOTNULL(mutable_superstructure_goal->mutable_suction()) |
| 595 | ->mutate_grab_piece(grab_piece_); |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 596 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 597 | mutable_superstructure_goal->mutable_elevator()->mutate_unsafe_goal( |
| 598 | elevator_wrist_pos_.elevator); |
| 599 | mutable_superstructure_goal->mutable_wrist()->mutate_unsafe_goal( |
| 600 | elevator_wrist_pos_.wrist); |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 601 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 602 | if (!main_superstructure_goal_builder.Send(superstructure_goal_offset)) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 603 | AOS_LOG(ERROR, "Sending superstructure goal failed.\n"); |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 604 | } |
Tyler Chatow | e024145 | 2019-03-08 21:07:50 -0800 | [diff] [blame] | 605 | |
Austin Schuh | aab7e16 | 2019-03-13 22:44:58 -0700 | [diff] [blame] | 606 | if (monotonic_now > |
| 607 | last_vision_control_ + ::std::chrono::milliseconds(50)) { |
Tyler Chatow | 4fedeea | 2019-03-10 15:33:36 -0700 | [diff] [blame] | 608 | video_tx_->Send(vision_control_); |
Austin Schuh | aab7e16 | 2019-03-13 22:44:58 -0700 | [diff] [blame] | 609 | last_vision_control_ = monotonic_now; |
Tyler Chatow | 4fedeea | 2019-03-10 15:33:36 -0700 | [diff] [blame] | 610 | } |
Austin Schuh | 4e2629d | 2019-03-28 14:44:37 -0700 | [diff] [blame] | 611 | |
| 612 | { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 613 | auto builder = camera_log_sender_.MakeBuilder(); |
| 614 | builder.Send(CreateCameraLog(*builder.fbb(), data.IsPressed(kCameraLog))); |
Austin Schuh | 4e2629d | 2019-03-28 14:44:37 -0700 | [diff] [blame] | 615 | } |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | private: |
Austin Schuh | c087b10 | 2019-05-12 15:33:12 -0700 | [diff] [blame] | 619 | ::aos::Sender<::y2019::control_loops::drivetrain::TargetSelectorHint> |
| 620 | target_selector_hint_sender_; |
| 621 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 622 | ::aos::Sender<LocalizerControl> localizer_control_sender_; |
Austin Schuh | eb99d07 | 2019-05-12 21:03:38 -0700 | [diff] [blame] | 623 | |
Austin Schuh | 5671a8c | 2019-05-19 17:01:04 -0700 | [diff] [blame] | 624 | ::aos::Sender<::y2019::CameraLog> camera_log_sender_; |
| 625 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 626 | ::aos::Fetcher<superstructure::Goal> superstructure_goal_fetcher_; |
Austin Schuh | 170f495 | 2019-06-29 18:58:30 -0700 | [diff] [blame] | 627 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 628 | ::aos::Sender<superstructure::Goal> superstructure_goal_sender_; |
Austin Schuh | 170f495 | 2019-06-29 18:58:30 -0700 | [diff] [blame] | 629 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 630 | ::aos::Fetcher<superstructure::Position> superstructure_position_fetcher_; |
| 631 | ::aos::Fetcher<superstructure::Status> superstructure_status_fetcher_; |
Austin Schuh | 170f495 | 2019-06-29 18:58:30 -0700 | [diff] [blame] | 632 | |
Austin Schuh | e2f2248 | 2019-04-13 23:05:43 -0700 | [diff] [blame] | 633 | // Bool to track if we've been above the deploy position. Once this bool is |
| 634 | // set, don't let the stilts retract until we see the platform. |
| 635 | bool was_above_ = false; |
| 636 | |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 637 | // Current goals here. |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 638 | ElevatorWristPosition elevator_wrist_pos_ = kStowPos; |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame] | 639 | bool grab_piece_ = false; |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 640 | |
| 641 | bool switch_ball_ = false; |
Tyler Chatow | e024145 | 2019-03-08 21:07:50 -0800 | [diff] [blame] | 642 | |
James Kuszmaul | 1373886 | 2019-04-14 10:48:00 -0700 | [diff] [blame] | 643 | bool climbed_ = false; |
| 644 | |
| 645 | enum class ReleaseButtonMode { |
| 646 | kBallIntake, |
| 647 | kRelease, |
| 648 | }; |
| 649 | |
| 650 | ReleaseButtonMode release_mode_ = ReleaseButtonMode::kRelease; |
| 651 | aos::monotonic_clock::time_point last_release_button_press_ = |
| 652 | aos::monotonic_clock::min_time; |
| 653 | |
Tyler Chatow | e024145 | 2019-03-08 21:07:50 -0800 | [diff] [blame] | 654 | VisionControl vision_control_; |
| 655 | ::std::unique_ptr<ProtoTXUdpSocket<VisionControl>> video_tx_; |
Tyler Chatow | 4fedeea | 2019-03-10 15:33:36 -0700 | [diff] [blame] | 656 | ::aos::monotonic_clock::time_point last_vision_control_ = |
| 657 | ::aos::monotonic_clock::time_point::min(); |
Austin Schuh | aab7e16 | 2019-03-13 22:44:58 -0700 | [diff] [blame] | 658 | |
| 659 | // Time at which we last did not have a game piece. |
| 660 | ::aos::monotonic_clock::time_point last_not_has_piece_ = |
| 661 | ::aos::monotonic_clock::time_point::min(); |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 662 | }; |
| 663 | |
| 664 | } // namespace joysticks |
| 665 | } // namespace input |
| 666 | } // namespace y2019 |
| 667 | |
Austin Schuh | 094d09b | 2020-11-20 23:26:52 -0800 | [diff] [blame] | 668 | int main(int argc, char **argv) { |
| 669 | ::aos::InitGoogle(&argc, &argv); |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 670 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 671 | aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
| 672 | aos::configuration::ReadConfig("config.json"); |
| 673 | |
| 674 | ::aos::ShmEventLoop event_loop(&config.message()); |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 675 | ::y2019::input::joysticks::Reader reader(&event_loop); |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 676 | |
| 677 | event_loop.Run(); |
| 678 | |
Austin Schuh | ae87e31 | 2020-08-01 16:15:01 -0700 | [diff] [blame] | 679 | return 0; |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 680 | } |