blob: a39c130ef5c94eb11d4060ebd3dc3c48dbd1eb67 [file] [log] [blame]
milind-u086d7262022-01-19 20:44:18 -08001#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"
Austin Schuh39f26f62022-02-24 21:34:46 -080013#include "frc971/control_loops/profiled_subsystem_generated.h"
milind-u086d7262022-01-19 20:44:18 -080014#include "frc971/input/action_joystick_input.h"
15#include "frc971/input/driver_station_data.h"
16#include "frc971/input/drivetrain_input.h"
17#include "frc971/input/joystick_input.h"
18#include "y2022/control_loops/drivetrain/drivetrain_base.h"
19#include "y2022/control_loops/superstructure/superstructure_goal_generated.h"
20#include "y2022/control_loops/superstructure/superstructure_status_generated.h"
21
Austin Schuh39f26f62022-02-24 21:34:46 -080022using frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal;
milind-u086d7262022-01-19 20:44:18 -080023using frc971::input::driver_station::ButtonLocation;
24using frc971::input::driver_station::ControlBit;
25using frc971::input::driver_station::JoystickAxis;
26using frc971::input::driver_station::POVLocation;
27
28namespace y2022 {
29namespace input {
30namespace joysticks {
31
Austin Schuh39f26f62022-02-24 21:34:46 -080032const ButtonLocation kCatapultPos(3, 3);
33const ButtonLocation kFire(3, 1);
34
milind-u086d7262022-01-19 20:44:18 -080035namespace superstructure = y2022::control_loops::superstructure;
36
37class Reader : public ::frc971::input::ActionJoystickInput {
38 public:
39 Reader(::aos::EventLoop *event_loop)
40 : ::frc971::input::ActionJoystickInput(
41 event_loop,
42 ::y2022::control_loops::drivetrain::GetDrivetrainConfig(),
43 ::frc971::input::DrivetrainInputReader::InputType::kPistol, {}),
44 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(
Austin Schuh39f26f62022-02-24 21:34:46 -080053 const ::frc971::input::driver_station::Data &data) override {
milind-u086d7262022-01-19 20:44:18 -080054 superstructure_status_fetcher_.Fetch();
55 if (!superstructure_status_fetcher_.get()) {
56 AOS_LOG(ERROR, "Got no superstructure status message.\n");
57 return;
58 }
Austin Schuh39f26f62022-02-24 21:34:46 -080059
60 aos::Sender<superstructure::Goal>::Builder builder =
61 superstructure_goal_sender_.MakeBuilder();
62
63 flatbuffers::Offset<frc971::ProfileParameters> catapult_profile =
64 frc971::CreateProfileParameters(*builder.fbb(), 5.0, 30.0);
65
66 StaticZeroingSingleDOFProfiledSubsystemGoal::Builder
67 catapult_return_builder =
68 builder.MakeBuilder<StaticZeroingSingleDOFProfiledSubsystemGoal>();
69 catapult_return_builder.add_unsafe_goal(
70 data.IsPressed(kCatapultPos) ? 0.3 : -0.85);
71 catapult_return_builder.add_profile_params(catapult_profile);
72 flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal>
73 catapult_return_offset = catapult_return_builder.Finish();
74
75 superstructure::CatapultGoal::Builder catapult_builder =
76 builder.MakeBuilder<superstructure::CatapultGoal>();
77 catapult_builder.add_return_position(catapult_return_offset);
78 catapult_builder.add_fire(data.IsPressed(kFire));
79 catapult_builder.add_shot_position(0.3);
80 catapult_builder.add_shot_velocity(15.0);
81 flatbuffers::Offset<superstructure::CatapultGoal> catapult_offset =
82 catapult_builder.Finish();
83
84 superstructure::Goal::Builder goal_builder =
85 builder.MakeBuilder<superstructure::Goal>();
86 goal_builder.add_catapult(catapult_offset);
87
88 if (builder.Send(goal_builder.Finish()) != aos::RawSender::Error::kOk) {
89 AOS_LOG(ERROR, "Sending superstructure goal failed.\n");
90 }
milind-u086d7262022-01-19 20:44:18 -080091 }
92
93 private:
94 ::aos::Sender<superstructure::Goal> superstructure_goal_sender_;
95
96 ::aos::Fetcher<superstructure::Status> superstructure_status_fetcher_;
97};
98
99} // namespace joysticks
100} // namespace input
101} // namespace y2022
102
103int main(int argc, char **argv) {
104 ::aos::InitGoogle(&argc, &argv);
105
106 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
Austin Schuhc5fa6d92022-02-25 14:36:28 -0800107 aos::configuration::ReadConfig("aos_config.json");
milind-u086d7262022-01-19 20:44:18 -0800108
109 ::aos::ShmEventLoop event_loop(&config.message());
110 ::y2022::input::joysticks::Reader reader(&event_loop);
111
112 event_loop.Run();
113
114 return 0;
115}