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