blob: f9f03b75295bf851c56cea2eb0bdc8fc9eb46017 [file] [log] [blame]
Ariv Diggi0af59c02023-10-07 13:15:39 -07001#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
26using frc971::CreateProfileParameters;
27using frc971::control_loops::CreateStaticZeroingSingleDOFProfiledSubsystemGoal;
28using frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal;
29using frc971::input::driver_station::ButtonLocation;
30using frc971::input::driver_station::ControlBit;
31using frc971::input::driver_station::JoystickAxis;
32using frc971::input::driver_station::POVLocation;
33using Side = frc971::control_loops::drivetrain::RobotSide;
Filip Kujawaffff93c2023-11-14 21:33:26 -080034using y2023_bot3::control_loops::superstructure::PivotGoal;
35using y2023_bot3::control_loops::superstructure::RollerGoal;
Ariv Diggi0af59c02023-10-07 13:15:39 -070036
37namespace y2023_bot3 {
38namespace input {
39namespace joysticks {
40
41namespace superstructure = y2023_bot3::control_loops::superstructure;
42
43struct ButtonData {
44 ButtonLocation button;
45};
46
Filip Kujawaffff93c2023-11-14 21:33:26 -080047namespace {
48// XBox controller
49const ButtonLocation kSpit(3, 1); // A
50const ButtonLocation kSpitHigh(3, 4); // Y
51const ButtonLocation kPickup(3, 8); // M4
52const ButtonLocation kPickupBack(3, 7); // M3
53const ButtonLocation kScore(3, 9); // M1
54const ButtonLocation kScoreBack(3, 10); // M2
55const ButtonLocation kScoreMid(3, 5); // LB
56const ButtonLocation kScoreMidBack(3, 6); // RB
57} // namespace
Ariv Diggi0af59c02023-10-07 13:15:39 -070058class 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 Kujawaffff93c2023-11-14 21:33:26 -080064 ::frc971::input::DrivetrainInputReader::InputType::kPistol, {}),
Ariv Diggi0af59c02023-10-07 13:15:39 -070065 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 Kujawaffff93c2023-11-14 21:33:26 -080092
93 RollerGoal roller_goal = RollerGoal::IDLE;
94 PivotGoal pivot_goal = PivotGoal::NEUTRAL;
95
96 if (data.IsPressed(kSpit)) {
97 roller_goal = RollerGoal::SPIT;
98 }
99
100 if (data.IsPressed(kScore)) {
101 pivot_goal = PivotGoal::SCORE_LOW_FRONT;
102 } else if (data.IsPressed(kScoreBack)) {
103 pivot_goal = PivotGoal::SCORE_LOW_BACK;
104 } else if (data.IsPressed(kScoreMid)) {
105 pivot_goal = PivotGoal::SCORE_MID_FRONT;
106 } else if (data.IsPressed(kScoreMidBack)) {
107 pivot_goal = PivotGoal::SCORE_MID_BACK;
108 } else if (data.IsPressed(kPickup)) {
109 pivot_goal = PivotGoal::PICKUP_FRONT;
110 roller_goal = RollerGoal::INTAKE_CUBE;
111 } else if (data.IsPressed(kPickupBack)) {
112 pivot_goal = PivotGoal::PICKUP_BACK;
113 roller_goal = RollerGoal::INTAKE_CUBE;
114 }
115
116 superstructure_goal_builder.add_roller_goal(roller_goal);
117 superstructure_goal_builder.add_pivot_goal(pivot_goal);
Ariv Diggi0af59c02023-10-07 13:15:39 -0700118 if (builder.Send(superstructure_goal_builder.Finish()) !=
119 aos::RawSender::Error::kOk) {
120 AOS_LOG(ERROR, "Sending superstructure goal failed.\n");
121 }
122 }
123 }
124
125 private:
126 ::aos::Sender<superstructure::Goal> superstructure_goal_sender_;
127
128 ::aos::Fetcher<superstructure::Status> superstructure_status_fetcher_;
129};
130
131} // namespace joysticks
132} // namespace input
133} // namespace y2023_bot3
134
135int main(int argc, char **argv) {
136 ::aos::InitGoogle(&argc, &argv);
137
138 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
139 aos::configuration::ReadConfig("aos_config.json");
140
141 ::aos::ShmEventLoop event_loop(&config.message());
142 ::y2023_bot3::input::joysticks::Reader reader(&event_loop);
143
144 event_loop.Run();
145
146 return 0;
Filip Kujawaffff93c2023-11-14 21:33:26 -0800147}