blob: 67f8957720cbad6f1c384ed1768b18c53f6b74a0 [file] [log] [blame]
Henry Speiser354d2782022-07-22 13:56:48 -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/input/action_joystick_input.h"
14#include "frc971/input/driver_station_data.h"
15#include "frc971/input/drivetrain_input.h"
16#include "frc971/input/joystick_input.h"
17#include "y2022_bot3/control_loops/drivetrain/drivetrain_base.h"
18#include "y2022_bot3/control_loops/superstructure/superstructure_goal_generated.h"
19#include "y2022_bot3/control_loops/superstructure/superstructure_status_generated.h"
20
Niko Sohmersdfeb19b2022-09-03 16:33:39 -070021using frc971::CreateProfileParameters;
22using frc971::control_loops::CreateStaticZeroingSingleDOFProfiledSubsystemGoal;
23using frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal;
Henry Speiser354d2782022-07-22 13:56:48 -070024using frc971::input::driver_station::ButtonLocation;
25using frc971::input::driver_station::ControlBit;
26using frc971::input::driver_station::JoystickAxis;
27using frc971::input::driver_station::POVLocation;
28
Stephan Pleinesf63bde82024-01-13 15:59:33 -080029namespace y2022_bot3::input::joysticks {
Henry Speiser354d2782022-07-22 13:56:48 -070030
31namespace superstructure = y2022_bot3::control_loops::superstructure;
32
Niko Sohmersdfeb19b2022-09-03 16:33:39 -070033// TODO(niko): add a climber button
34const ButtonLocation kIntake(4, 10);
35const ButtonLocation kSpit(4, 9);
36
Henry Speiser354d2782022-07-22 13:56:48 -070037class Reader : public ::frc971::input::ActionJoystickInput {
38 public:
39 Reader(::aos::EventLoop *event_loop)
40 : ::frc971::input::ActionJoystickInput(
41 event_loop,
42 ::y2022_bot3::control_loops::drivetrain::GetDrivetrainConfig(),
Austin Schuhfafdd932023-04-09 17:03:36 -070043 ::frc971::input::DrivetrainInputReader::InputType::kPistol, {}),
Henry Speiser354d2782022-07-22 13:56:48 -070044 superstructure_goal_sender_(
45 event_loop->MakeSender<superstructure::Goal>("/superstructure")),
46 superstructure_status_fetcher_(
47 event_loop->MakeFetcher<superstructure::Status>(
48 "/superstructure")) {}
49
50 void AutoEnded() override { AOS_LOG(INFO, "Auto ended.\n"); }
51
52 void HandleTeleop(
Niko Sohmersdfeb19b2022-09-03 16:33:39 -070053 const ::frc971::input::driver_station::Data &data) override {
Henry Speiser354d2782022-07-22 13:56:48 -070054 superstructure_status_fetcher_.Fetch();
55 if (!superstructure_status_fetcher_.get()) {
56 AOS_LOG(ERROR, "Got no superstructure status message.\n");
57 return;
58 }
Niko Sohmersdfeb19b2022-09-03 16:33:39 -070059 constexpr double kIntakeOutPosition = 0.0;
60 constexpr double kIntakeInPosition = 1.47;
61
62 double roller_speed = 0.0;
63 double intake_pos = kIntakeInPosition;
64
65 if (data.IsPressed(kIntake) || data.IsPressed(kSpit)) {
66 intake_pos = kIntakeOutPosition;
67
68 if (data.IsPressed(kIntake)) {
69 roller_speed = 12.0;
70 } else {
71 roller_speed = -12.0;
72 }
73 }
74
75 {
76 auto builder = superstructure_goal_sender_.MakeBuilder();
77
78 flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal>
79 intake_offset = CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
80 *builder.fbb(), intake_pos,
81 CreateProfileParameters(*builder.fbb(), 8.0, 40.0));
82
83 superstructure::Goal::Builder superstructure_goal_builder =
84 builder.MakeBuilder<superstructure::Goal>();
85
86 superstructure_goal_builder.add_intake(intake_offset);
87 superstructure_goal_builder.add_roller_speed(roller_speed);
88
89 if (builder.Send(superstructure_goal_builder.Finish()) !=
90 aos::RawSender::Error::kOk) {
91 AOS_LOG(ERROR, "Sending superstructure goal failed.\n");
92 }
93 }
Henry Speiser354d2782022-07-22 13:56:48 -070094 }
95
96 private:
97 ::aos::Sender<superstructure::Goal> superstructure_goal_sender_;
98
99 ::aos::Fetcher<superstructure::Status> superstructure_status_fetcher_;
100};
101
Stephan Pleinesf63bde82024-01-13 15:59:33 -0800102} // namespace y2022_bot3::input::joysticks
Henry Speiser354d2782022-07-22 13:56:48 -0700103
104int main(int argc, char **argv) {
105 ::aos::InitGoogle(&argc, &argv);
106
107 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
108 aos::configuration::ReadConfig("aos_config.json");
109
110 ::aos::ShmEventLoop event_loop(&config.message());
111 ::y2022_bot3::input::joysticks::Reader reader(&event_loop);
112
113 event_loop.Run();
114
115 return 0;
116}