Ariv Diggi | 0af59c0 | 2023-10-07 13:15:39 -0700 | [diff] [blame] | 1 | #include <unistd.h> |
| 2 | |
| 3 | #include <cmath> |
| 4 | #include <cstdio> |
| 5 | #include <cstring> |
| 6 | |
| 7 | #include "aos/actions/actions.h" |
| 8 | #include "aos/init.h" |
| 9 | #include "aos/logging/logging.h" |
| 10 | #include "aos/network/team_number.h" |
| 11 | #include "aos/util/log_interval.h" |
| 12 | #include "frc971/autonomous/base_autonomous_actor.h" |
| 13 | #include "frc971/control_loops/drivetrain/localizer_generated.h" |
| 14 | #include "frc971/control_loops/profiled_subsystem_generated.h" |
| 15 | #include "frc971/input/action_joystick_input.h" |
| 16 | #include "frc971/input/driver_station_data.h" |
| 17 | #include "frc971/input/drivetrain_input.h" |
| 18 | #include "frc971/input/joystick_input.h" |
| 19 | #include "frc971/input/redundant_joystick_data.h" |
| 20 | #include "frc971/zeroing/wrap.h" |
| 21 | #include "y2023_bot3/constants.h" |
| 22 | #include "y2023_bot3/control_loops/drivetrain/drivetrain_base.h" |
| 23 | #include "y2023_bot3/control_loops/superstructure/superstructure_goal_generated.h" |
| 24 | #include "y2023_bot3/control_loops/superstructure/superstructure_status_generated.h" |
| 25 | |
| 26 | using frc971::CreateProfileParameters; |
| 27 | using frc971::control_loops::CreateStaticZeroingSingleDOFProfiledSubsystemGoal; |
| 28 | using frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal; |
| 29 | using frc971::input::driver_station::ButtonLocation; |
| 30 | using frc971::input::driver_station::ControlBit; |
| 31 | using frc971::input::driver_station::JoystickAxis; |
| 32 | using frc971::input::driver_station::POVLocation; |
| 33 | using Side = frc971::control_loops::drivetrain::RobotSide; |
Filip Kujawa | ffff93c | 2023-11-14 21:33:26 -0800 | [diff] [blame] | 34 | using y2023_bot3::control_loops::superstructure::PivotGoal; |
| 35 | using y2023_bot3::control_loops::superstructure::RollerGoal; |
Ariv Diggi | 0af59c0 | 2023-10-07 13:15:39 -0700 | [diff] [blame] | 36 | |
| 37 | namespace y2023_bot3 { |
| 38 | namespace input { |
| 39 | namespace joysticks { |
| 40 | |
| 41 | namespace superstructure = y2023_bot3::control_loops::superstructure; |
| 42 | |
| 43 | struct ButtonData { |
| 44 | ButtonLocation button; |
| 45 | }; |
| 46 | |
Filip Kujawa | ffff93c | 2023-11-14 21:33:26 -0800 | [diff] [blame] | 47 | namespace { |
| 48 | // XBox controller |
| 49 | const ButtonLocation kSpit(3, 1); // A |
| 50 | const ButtonLocation kSpitHigh(3, 4); // Y |
| 51 | const ButtonLocation kPickup(3, 8); // M4 |
| 52 | const ButtonLocation kPickupBack(3, 7); // M3 |
| 53 | const ButtonLocation kScore(3, 9); // M1 |
| 54 | const ButtonLocation kScoreBack(3, 10); // M2 |
| 55 | const ButtonLocation kScoreMid(3, 5); // LB |
| 56 | const ButtonLocation kScoreMidBack(3, 6); // RB |
| 57 | } // namespace |
Ariv Diggi | 0af59c0 | 2023-10-07 13:15:39 -0700 | [diff] [blame] | 58 | class Reader : public ::frc971::input::ActionJoystickInput { |
| 59 | public: |
| 60 | Reader(::aos::EventLoop *event_loop) |
| 61 | : ::frc971::input::ActionJoystickInput( |
| 62 | event_loop, |
| 63 | ::y2023_bot3::control_loops::drivetrain::GetDrivetrainConfig(), |
Filip Kujawa | ffff93c | 2023-11-14 21:33:26 -0800 | [diff] [blame] | 64 | ::frc971::input::DrivetrainInputReader::InputType::kPistol, {}), |
Ariv Diggi | 0af59c0 | 2023-10-07 13:15:39 -0700 | [diff] [blame] | 65 | superstructure_goal_sender_( |
| 66 | event_loop->MakeSender<superstructure::Goal>("/superstructure")), |
| 67 | superstructure_status_fetcher_( |
| 68 | event_loop->MakeFetcher<superstructure::Status>( |
| 69 | "/superstructure")) {} |
| 70 | |
| 71 | void AutoEnded() override { AOS_LOG(INFO, "Auto ended.\n"); } |
| 72 | |
| 73 | bool has_scored_ = false; |
| 74 | |
| 75 | void HandleTeleop( |
| 76 | const ::frc971::input::driver_station::Data &data) override { |
| 77 | (void)data; |
| 78 | superstructure_status_fetcher_.Fetch(); |
| 79 | if (!superstructure_status_fetcher_.get()) { |
| 80 | AOS_LOG(ERROR, "Got no superstructure status message.\n"); |
| 81 | return; |
| 82 | } |
| 83 | |
| 84 | std::optional<double> place_index = std::nullopt; |
| 85 | (void)place_index; |
| 86 | |
| 87 | { |
| 88 | auto builder = superstructure_goal_sender_.MakeBuilder(); |
| 89 | |
| 90 | superstructure::Goal::Builder superstructure_goal_builder = |
| 91 | builder.MakeBuilder<superstructure::Goal>(); |
Filip Kujawa | ffff93c | 2023-11-14 21:33:26 -0800 | [diff] [blame] | 92 | |
| 93 | RollerGoal roller_goal = RollerGoal::IDLE; |
| 94 | PivotGoal pivot_goal = PivotGoal::NEUTRAL; |
| 95 | |
| 96 | if (data.IsPressed(kSpit)) { |
| 97 | roller_goal = RollerGoal::SPIT; |
Filip Kujawa | 271c997 | 2023-11-14 22:01:23 -0800 | [diff] [blame^] | 98 | } else if (data.IsPressed(kSpitHigh)) { |
| 99 | roller_goal = RollerGoal::SPIT_HIGH; |
Filip Kujawa | ffff93c | 2023-11-14 21:33:26 -0800 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | if (data.IsPressed(kScore)) { |
| 103 | pivot_goal = PivotGoal::SCORE_LOW_FRONT; |
| 104 | } else if (data.IsPressed(kScoreBack)) { |
| 105 | pivot_goal = PivotGoal::SCORE_LOW_BACK; |
| 106 | } else if (data.IsPressed(kScoreMid)) { |
| 107 | pivot_goal = PivotGoal::SCORE_MID_FRONT; |
| 108 | } else if (data.IsPressed(kScoreMidBack)) { |
| 109 | pivot_goal = PivotGoal::SCORE_MID_BACK; |
| 110 | } else if (data.IsPressed(kPickup)) { |
| 111 | pivot_goal = PivotGoal::PICKUP_FRONT; |
| 112 | roller_goal = RollerGoal::INTAKE_CUBE; |
| 113 | } else if (data.IsPressed(kPickupBack)) { |
| 114 | pivot_goal = PivotGoal::PICKUP_BACK; |
| 115 | roller_goal = RollerGoal::INTAKE_CUBE; |
| 116 | } |
| 117 | |
| 118 | superstructure_goal_builder.add_roller_goal(roller_goal); |
| 119 | superstructure_goal_builder.add_pivot_goal(pivot_goal); |
Ariv Diggi | 0af59c0 | 2023-10-07 13:15:39 -0700 | [diff] [blame] | 120 | if (builder.Send(superstructure_goal_builder.Finish()) != |
| 121 | aos::RawSender::Error::kOk) { |
| 122 | AOS_LOG(ERROR, "Sending superstructure goal failed.\n"); |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | private: |
| 128 | ::aos::Sender<superstructure::Goal> superstructure_goal_sender_; |
| 129 | |
| 130 | ::aos::Fetcher<superstructure::Status> superstructure_status_fetcher_; |
| 131 | }; |
| 132 | |
| 133 | } // namespace joysticks |
| 134 | } // namespace input |
| 135 | } // namespace y2023_bot3 |
| 136 | |
| 137 | int main(int argc, char **argv) { |
| 138 | ::aos::InitGoogle(&argc, &argv); |
| 139 | |
| 140 | aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
| 141 | aos::configuration::ReadConfig("aos_config.json"); |
| 142 | |
| 143 | ::aos::ShmEventLoop event_loop(&config.message()); |
| 144 | ::y2023_bot3::input::joysticks::Reader reader(&event_loop); |
| 145 | |
| 146 | event_loop.Run(); |
| 147 | |
| 148 | return 0; |
Filip Kujawa | ffff93c | 2023-11-14 21:33:26 -0800 | [diff] [blame] | 149 | } |