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