blob: 00a28be5a2fb0a1c39a3e7c776c9bb80ef4dc8ab [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
29namespace y2022_bot3 {
30namespace input {
31namespace joysticks {
32
33namespace superstructure = y2022_bot3::control_loops::superstructure;
34
Niko Sohmersdfeb19b2022-09-03 16:33:39 -070035// TODO(niko): add a climber button
36const ButtonLocation kIntake(4, 10);
37const ButtonLocation kSpit(4, 9);
38
Henry Speiser354d2782022-07-22 13:56:48 -070039class Reader : public ::frc971::input::ActionJoystickInput {
40 public:
41 Reader(::aos::EventLoop *event_loop)
42 : ::frc971::input::ActionJoystickInput(
43 event_loop,
44 ::y2022_bot3::control_loops::drivetrain::GetDrivetrainConfig(),
Austin Schuhfafdd932023-04-09 17:03:36 -070045 ::frc971::input::DrivetrainInputReader::InputType::kPistol, {}),
Henry Speiser354d2782022-07-22 13:56:48 -070046 superstructure_goal_sender_(
47 event_loop->MakeSender<superstructure::Goal>("/superstructure")),
48 superstructure_status_fetcher_(
49 event_loop->MakeFetcher<superstructure::Status>(
50 "/superstructure")) {}
51
52 void AutoEnded() override { AOS_LOG(INFO, "Auto ended.\n"); }
53
54 void HandleTeleop(
Niko Sohmersdfeb19b2022-09-03 16:33:39 -070055 const ::frc971::input::driver_station::Data &data) override {
Henry Speiser354d2782022-07-22 13:56:48 -070056 superstructure_status_fetcher_.Fetch();
57 if (!superstructure_status_fetcher_.get()) {
58 AOS_LOG(ERROR, "Got no superstructure status message.\n");
59 return;
60 }
Niko Sohmersdfeb19b2022-09-03 16:33:39 -070061 constexpr double kIntakeOutPosition = 0.0;
62 constexpr double kIntakeInPosition = 1.47;
63
64 double roller_speed = 0.0;
65 double intake_pos = kIntakeInPosition;
66
67 if (data.IsPressed(kIntake) || data.IsPressed(kSpit)) {
68 intake_pos = kIntakeOutPosition;
69
70 if (data.IsPressed(kIntake)) {
71 roller_speed = 12.0;
72 } else {
73 roller_speed = -12.0;
74 }
75 }
76
77 {
78 auto builder = superstructure_goal_sender_.MakeBuilder();
79
80 flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal>
81 intake_offset = CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
82 *builder.fbb(), intake_pos,
83 CreateProfileParameters(*builder.fbb(), 8.0, 40.0));
84
85 superstructure::Goal::Builder superstructure_goal_builder =
86 builder.MakeBuilder<superstructure::Goal>();
87
88 superstructure_goal_builder.add_intake(intake_offset);
89 superstructure_goal_builder.add_roller_speed(roller_speed);
90
91 if (builder.Send(superstructure_goal_builder.Finish()) !=
92 aos::RawSender::Error::kOk) {
93 AOS_LOG(ERROR, "Sending superstructure goal failed.\n");
94 }
95 }
Henry Speiser354d2782022-07-22 13:56:48 -070096 }
97
98 private:
99 ::aos::Sender<superstructure::Goal> superstructure_goal_sender_;
100
101 ::aos::Fetcher<superstructure::Status> superstructure_status_fetcher_;
102};
103
104} // namespace joysticks
105} // namespace input
106} // namespace y2022_bot3
107
108int main(int argc, char **argv) {
109 ::aos::InitGoogle(&argc, &argv);
110
111 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
112 aos::configuration::ReadConfig("aos_config.json");
113
114 ::aos::ShmEventLoop event_loop(&config.message());
115 ::y2022_bot3::input::joysticks::Reader reader(&event_loop);
116
117 event_loop.Run();
118
119 return 0;
120}