blob: 6344127cb2db3d25d92410fc760265af7ed579fc [file] [log] [blame]
Niko Sohmers3860f8a2024-01-12 21:05:19 -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"
James Kuszmaul2549e752024-01-20 17:42:51 -080013#include "frc971/constants/constants_sender_lib.h"
Niko Sohmers3860f8a2024-01-12 21:05:19 -080014#include "frc971/control_loops/drivetrain/localizer_generated.h"
15#include "frc971/control_loops/profiled_subsystem_generated.h"
Filip Kujawaa7c8b412024-02-24 18:29:29 -080016#include "frc971/control_loops/static_zeroing_single_dof_profiled_subsystem.h"
Niko Sohmers3860f8a2024-01-12 21:05:19 -080017#include "frc971/input/action_joystick_input.h"
18#include "frc971/input/driver_station_data.h"
19#include "frc971/input/drivetrain_input.h"
20#include "frc971/input/joystick_input.h"
21#include "frc971/input/redundant_joystick_data.h"
22#include "frc971/zeroing/wrap.h"
James Kuszmaul2549e752024-01-20 17:42:51 -080023#include "y2024/constants/constants_generated.h"
Niko Sohmers3860f8a2024-01-12 21:05:19 -080024#include "y2024/control_loops/drivetrain/drivetrain_base.h"
Filip Kujawaa7c8b412024-02-24 18:29:29 -080025#include "y2024/control_loops/superstructure/superstructure_goal_static.h"
26#include "y2024/control_loops/superstructure/superstructure_status_static.h"
27
Niko Sohmers3860f8a2024-01-12 21:05:19 -080028using frc971::CreateProfileParameters;
Niko Sohmers3860f8a2024-01-12 21:05:19 -080029using 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;
34
Stephan Pleinesf63bde82024-01-13 15:59:33 -080035namespace y2024::input::joysticks {
Niko Sohmers3860f8a2024-01-12 21:05:19 -080036
Filip Kujawaa7c8b412024-02-24 18:29:29 -080037namespace superstructure = y2024::control_loops::superstructure;
38
39// TODO(Xander): add x,y location from physical wiring
40const ButtonLocation kIntake(0, 0);
41const ButtonLocation kSpit(0, 0);
42const ButtonLocation kCatapultLoad(0, 0);
43const ButtonLocation kAmp(0, 0);
44const ButtonLocation kTrap(0, 0);
45const ButtonLocation kAutoAim(0, 0);
46const ButtonLocation kAimSpeaker(0, 0);
47const ButtonLocation kAimPodium(0, 0);
48const ButtonLocation kShoot(0, 0);
49const ButtonLocation kClimb(0, 0);
50const ButtonLocation kExtraButtonOne(0, 0);
51const ButtonLocation kExtraButtonTwo(0, 0);
52const ButtonLocation kExtraButtonThree(0, 0);
53const ButtonLocation kExtraButtonFour(0, 0);
54
Niko Sohmers3860f8a2024-01-12 21:05:19 -080055class Reader : public ::frc971::input::ActionJoystickInput {
56 public:
Filip Kujawaa7c8b412024-02-24 18:29:29 -080057 Reader(::aos::EventLoop *event_loop, const y2024::Constants *robot_constants)
Niko Sohmers3860f8a2024-01-12 21:05:19 -080058 : ::frc971::input::ActionJoystickInput(
59 event_loop,
James Kuszmaul2549e752024-01-20 17:42:51 -080060 ::y2024::control_loops::drivetrain::GetDrivetrainConfig(event_loop),
Niko Sohmers3860f8a2024-01-12 21:05:19 -080061 ::frc971::input::DrivetrainInputReader::InputType::kPistol,
62 {.use_redundant_joysticks = true}),
63 superstructure_goal_sender_(
Filip Kujawaa7c8b412024-02-24 18:29:29 -080064 event_loop->MakeSender<control_loops::superstructure::GoalStatic>(
Niko Sohmers3860f8a2024-01-12 21:05:19 -080065 "/superstructure")),
66 superstructure_status_fetcher_(
67 event_loop->MakeFetcher<control_loops::superstructure::Status>(
Filip Kujawaa7c8b412024-02-24 18:29:29 -080068 "/superstructure")),
69 robot_constants_(CHECK_NOTNULL(robot_constants)) {}
Niko Sohmers3860f8a2024-01-12 21:05:19 -080070
71 void AutoEnded() override { AOS_LOG(INFO, "Auto ended.\n"); }
72
73 void HandleTeleop(
74 const ::frc971::input::driver_station::Data &data) override {
75 (void)data;
76 superstructure_status_fetcher_.Fetch();
77 if (!superstructure_status_fetcher_.get()) {
78 AOS_LOG(ERROR, "Got no superstructure status message.\n");
79 return;
80 }
Filip Kujawaa7c8b412024-02-24 18:29:29 -080081
82 aos::Sender<superstructure::GoalStatic>::StaticBuilder
83 superstructure_goal_builder =
84 superstructure_goal_sender_.MakeStaticBuilder();
85
86 if (data.IsPressed(kIntake)) {
87 // Intake is pressed
88 superstructure_goal_builder->set_intake_goal(
89 superstructure::IntakeGoal::INTAKE);
90 } else if (data.IsPressed(kSpit)) {
91 // If Intake not pressed and spit pressed, spit
92 superstructure_goal_builder->set_intake_goal(
93 superstructure::IntakeGoal::SPIT);
94 }
95
96 // Set note goal for the robot. Loading the catapult will always be
97 // preferred over scoring in the Amp or Trap.
98 if (data.IsPressed(kCatapultLoad)) {
99 superstructure_goal_builder->set_note_goal(
100 superstructure::NoteGoal::CATAPULT);
101 } else if (data.IsPressed(kAmp)) {
102 superstructure_goal_builder->set_note_goal(superstructure::NoteGoal::AMP);
103 } else if (data.IsPressed(kTrap)) {
104 superstructure_goal_builder->set_note_goal(
105 superstructure::NoteGoal::TRAP);
106 }
107
108 // Firing note when requested
109 superstructure_goal_builder->set_fire(data.IsPressed(kShoot));
110
111 // Shooter goal contains all speaker-related goals
112 auto shooter_goal = superstructure_goal_builder->add_shooter_goal();
113
114 shooter_goal->set_auto_aim(data.IsPressed(kAimSpeaker));
115
116 // Updating aiming for shooter goal, only one type of aim should be possible
117 // at a time, auto-aiming is preferred over the setpoints.
118 if (data.IsPressed(kAutoAim)) {
119 shooter_goal->set_auto_aim(true);
120 } else if (data.IsPressed(kAimSpeaker)) {
121 auto catapult_goal = shooter_goal->add_catapult_goal();
122 catapult_goal->set_shot_velocity(robot_constants_->common()
123 ->shooter_speaker_set_point()
124 ->shot_velocity());
125 PopulateStaticZeroingSingleDOFProfiledSubsystemGoal(
126 shooter_goal->add_altitude_position(),
127 robot_constants_->common()
128 ->shooter_speaker_set_point()
129 ->altitude_position());
130 PopulateStaticZeroingSingleDOFProfiledSubsystemGoal(
131 shooter_goal->add_turret_position(), robot_constants_->common()
132 ->shooter_speaker_set_point()
133 ->turret_position());
134 } else if (data.IsPressed(kAimPodium)) {
135 auto catapult_goal = shooter_goal->add_catapult_goal();
136 catapult_goal->set_shot_velocity(robot_constants_->common()
137 ->shooter_podium_set_point()
138 ->shot_velocity());
139 PopulateStaticZeroingSingleDOFProfiledSubsystemGoal(
140 shooter_goal->add_altitude_position(),
141 robot_constants_->common()
142 ->shooter_podium_set_point()
143 ->altitude_position());
144 PopulateStaticZeroingSingleDOFProfiledSubsystemGoal(
145 shooter_goal->add_turret_position(), robot_constants_->common()
146 ->shooter_podium_set_point()
147 ->turret_position());
148 }
149
150 // Extend climbers if pressed, retract otherwise
151 if (data.IsPressed(kClimb)) {
152 superstructure_goal_builder->set_climber_goal(
153 superstructure::ClimberGoal::FULL_EXTEND);
154 } else {
155 superstructure_goal_builder->set_climber_goal(
156 superstructure::ClimberGoal::RETRACT);
157 }
158
159 superstructure_goal_builder.CheckOk(superstructure_goal_builder.Send());
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800160 }
161
162 private:
Filip Kujawaa7c8b412024-02-24 18:29:29 -0800163 ::aos::Sender<control_loops::superstructure::GoalStatic>
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800164 superstructure_goal_sender_;
165 ::aos::Fetcher<control_loops::superstructure::Status>
166 superstructure_status_fetcher_;
Filip Kujawaa7c8b412024-02-24 18:29:29 -0800167 const y2024::Constants *robot_constants_;
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800168};
169
Stephan Pleinesf63bde82024-01-13 15:59:33 -0800170} // namespace y2024::input::joysticks
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800171
172int main(int argc, char **argv) {
173 ::aos::InitGoogle(&argc, &argv);
174
175 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
176 aos::configuration::ReadConfig("aos_config.json");
James Kuszmaul2549e752024-01-20 17:42:51 -0800177 frc971::constants::WaitForConstants<y2024::Constants>(&config.message());
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800178
Filip Kujawaa7c8b412024-02-24 18:29:29 -0800179 ::aos::ShmEventLoop constant_fetcher_event_loop(&config.message());
180 frc971::constants::ConstantsFetcher<y2024::Constants> constants_fetcher(
181 &constant_fetcher_event_loop);
182 const y2024::Constants *robot_constants = &constants_fetcher.constants();
183
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800184 ::aos::ShmEventLoop event_loop(&config.message());
Filip Kujawaa7c8b412024-02-24 18:29:29 -0800185 ::y2024::input::joysticks::Reader reader(&event_loop, robot_constants);
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800186
187 event_loop.Run();
188
189 return 0;
190}