blob: f7578f97c1f4d0196176247a05b9ddd0ff25fa29 [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
James Kuszmaul0338ec32024-03-16 11:40:51 -070035DEFINE_double(speaker_altitude_position_override, -1,
36 "If set, use this as the altitude angle for the fixed shot.");
37
Stephan Pleinesf63bde82024-01-13 15:59:33 -080038namespace y2024::input::joysticks {
Niko Sohmers3860f8a2024-01-12 21:05:19 -080039
Filip Kujawaa7c8b412024-02-24 18:29:29 -080040namespace superstructure = y2024::control_loops::superstructure;
41
James Kuszmaul0281e152024-02-26 22:26:16 -080042// TODO(Xander): add button location from physical wiring
43// Note: Due to use_redundant_joysticks, the AOS_LOG statements
44// for the internal joystick code will give offset joystick numbering.
James Kuszmaul0338ec32024-03-16 11:40:51 -070045const ButtonLocation kIntake(2, 2);
46
47const ButtonLocation kSpitRollers(1, 13);
48const ButtonLocation kIntakeRollers(2, 5);
49
50const ButtonLocation kCatapultLoad(2, 1);
51const ButtonLocation kAmp(2, 4);
52const ButtonLocation kFire(2, 8);
53const ButtonLocation kTrap(2, 6);
54const ButtonLocation kAutoAim(1, 8);
55const ButtonLocation kAimSpeaker(2, 11);
Filip Kujawaa7c8b412024-02-24 18:29:29 -080056const ButtonLocation kAimPodium(0, 0);
57const ButtonLocation kShoot(0, 0);
James Kuszmaul0281e152024-02-26 22:26:16 -080058const ButtonLocation kRaiseClimber(3, 2);
James Kuszmaul0338ec32024-03-16 11:40:51 -070059const ButtonLocation kSlowClimber(3, 1);
60const ButtonLocation kRetractClimber(2, 3);
Filip Kujawaa7c8b412024-02-24 18:29:29 -080061const ButtonLocation kExtraButtonOne(0, 0);
62const ButtonLocation kExtraButtonTwo(0, 0);
63const ButtonLocation kExtraButtonThree(0, 0);
64const ButtonLocation kExtraButtonFour(0, 0);
65
Niko Sohmers3860f8a2024-01-12 21:05:19 -080066class Reader : public ::frc971::input::ActionJoystickInput {
67 public:
Filip Kujawaa7c8b412024-02-24 18:29:29 -080068 Reader(::aos::EventLoop *event_loop, const y2024::Constants *robot_constants)
Niko Sohmers3860f8a2024-01-12 21:05:19 -080069 : ::frc971::input::ActionJoystickInput(
70 event_loop,
James Kuszmaul2549e752024-01-20 17:42:51 -080071 ::y2024::control_loops::drivetrain::GetDrivetrainConfig(event_loop),
Niko Sohmers3860f8a2024-01-12 21:05:19 -080072 ::frc971::input::DrivetrainInputReader::InputType::kPistol,
73 {.use_redundant_joysticks = true}),
74 superstructure_goal_sender_(
Filip Kujawaa7c8b412024-02-24 18:29:29 -080075 event_loop->MakeSender<control_loops::superstructure::GoalStatic>(
Niko Sohmers3860f8a2024-01-12 21:05:19 -080076 "/superstructure")),
77 superstructure_status_fetcher_(
78 event_loop->MakeFetcher<control_loops::superstructure::Status>(
Filip Kujawaa7c8b412024-02-24 18:29:29 -080079 "/superstructure")),
80 robot_constants_(CHECK_NOTNULL(robot_constants)) {}
Niko Sohmers3860f8a2024-01-12 21:05:19 -080081
82 void AutoEnded() override { AOS_LOG(INFO, "Auto ended.\n"); }
83
84 void HandleTeleop(
85 const ::frc971::input::driver_station::Data &data) override {
Niko Sohmers3860f8a2024-01-12 21:05:19 -080086 superstructure_status_fetcher_.Fetch();
87 if (!superstructure_status_fetcher_.get()) {
88 AOS_LOG(ERROR, "Got no superstructure status message.\n");
89 return;
90 }
Filip Kujawaa7c8b412024-02-24 18:29:29 -080091
92 aos::Sender<superstructure::GoalStatic>::StaticBuilder
93 superstructure_goal_builder =
94 superstructure_goal_sender_.MakeStaticBuilder();
95
96 if (data.IsPressed(kIntake)) {
97 // Intake is pressed
James Kuszmaul0338ec32024-03-16 11:40:51 -070098 superstructure_goal_builder->set_intake_pivot(
99 superstructure::IntakePivotGoal::DOWN);
100 } else {
101 superstructure_goal_builder->set_intake_pivot(
102 superstructure::IntakePivotGoal::UP);
103 }
104
105 if (data.IsPressed(kIntakeRollers)) {
106 // Intake is pressed
Filip Kujawaa7c8b412024-02-24 18:29:29 -0800107 superstructure_goal_builder->set_intake_goal(
108 superstructure::IntakeGoal::INTAKE);
James Kuszmaul0338ec32024-03-16 11:40:51 -0700109 } else if (data.IsPressed(kSpitRollers)) {
110 superstructure_goal_builder->set_intake_goal(
111 superstructure::IntakeGoal::SPIT);
James Kuszmaul0281e152024-02-26 22:26:16 -0800112 } else {
Filip Kujawaa7c8b412024-02-24 18:29:29 -0800113 superstructure_goal_builder->set_intake_goal(
James Kuszmaul0281e152024-02-26 22:26:16 -0800114 superstructure::IntakeGoal::NONE);
Filip Kujawaa7c8b412024-02-24 18:29:29 -0800115 }
James Kuszmaul0338ec32024-03-16 11:40:51 -0700116
James Kuszmaul0281e152024-02-26 22:26:16 -0800117 if (data.IsPressed(kAmp)) {
Filip Kujawaa7c8b412024-02-24 18:29:29 -0800118 superstructure_goal_builder->set_note_goal(superstructure::NoteGoal::AMP);
119 } else if (data.IsPressed(kTrap)) {
120 superstructure_goal_builder->set_note_goal(
121 superstructure::NoteGoal::TRAP);
James Kuszmaul0281e152024-02-26 22:26:16 -0800122 } else if (data.IsPressed(kCatapultLoad)) {
123 superstructure_goal_builder->set_note_goal(
124 superstructure::NoteGoal::CATAPULT);
125 } else {
126 superstructure_goal_builder->set_note_goal(
127 superstructure::NoteGoal::NONE);
Filip Kujawaa7c8b412024-02-24 18:29:29 -0800128 }
Filip Kujawaa7c8b412024-02-24 18:29:29 -0800129 auto shooter_goal = superstructure_goal_builder->add_shooter_goal();
James Kuszmaul0338ec32024-03-16 11:40:51 -0700130 shooter_goal->set_auto_aim(data.IsPressed(kAutoAim));
Filip Kujawaa7c8b412024-02-24 18:29:29 -0800131
132 // Updating aiming for shooter goal, only one type of aim should be possible
133 // at a time, auto-aiming is preferred over the setpoints.
James Kuszmaul0281e152024-02-26 22:26:16 -0800134 if (data.IsPressed(kAimSpeaker)) {
Filip Kujawaa7c8b412024-02-24 18:29:29 -0800135 auto catapult_goal = shooter_goal->add_catapult_goal();
136 catapult_goal->set_shot_velocity(robot_constants_->common()
137 ->shooter_speaker_set_point()
138 ->shot_velocity());
James Kuszmaul0338ec32024-03-16 11:40:51 -0700139 if (FLAGS_speaker_altitude_position_override > 0) {
140 PopulateStaticZeroingSingleDOFProfiledSubsystemGoal(
141 shooter_goal->add_altitude_position(),
142 FLAGS_speaker_altitude_position_override);
143 } else {
144 PopulateStaticZeroingSingleDOFProfiledSubsystemGoal(
145 shooter_goal->add_altitude_position(),
146 robot_constants_->common()
147 ->shooter_speaker_set_point()
148 ->altitude_position());
149 }
Filip Kujawaa7c8b412024-02-24 18:29:29 -0800150 PopulateStaticZeroingSingleDOFProfiledSubsystemGoal(
151 shooter_goal->add_turret_position(), robot_constants_->common()
152 ->shooter_speaker_set_point()
153 ->turret_position());
Filip Kujawaa7c8b412024-02-24 18:29:29 -0800154 }
James Kuszmaul0281e152024-02-26 22:26:16 -0800155 superstructure_goal_builder->set_fire(data.IsPressed(kFire));
Filip Kujawaa7c8b412024-02-24 18:29:29 -0800156
James Kuszmaul0338ec32024-03-16 11:40:51 -0700157 if (data.IsPressed(kRetractClimber)) {
Filip Kujawaa7c8b412024-02-24 18:29:29 -0800158 superstructure_goal_builder->set_climber_goal(
159 superstructure::ClimberGoal::RETRACT);
James Kuszmaul0338ec32024-03-16 11:40:51 -0700160 } else if (data.IsPressed(kRaiseClimber)) {
161 superstructure_goal_builder->set_climber_goal(
162 superstructure::ClimberGoal::FULL_EXTEND);
James Kuszmaul0281e152024-02-26 22:26:16 -0800163 } else {
164 superstructure_goal_builder->set_climber_goal(
165 superstructure::ClimberGoal::STOWED);
Filip Kujawaa7c8b412024-02-24 18:29:29 -0800166 }
167
James Kuszmaul0338ec32024-03-16 11:40:51 -0700168 if (data.IsPressed(kSlowClimber)) {
169 superstructure_goal_builder->set_slow_climber(true);
170 } else {
171 superstructure_goal_builder->set_slow_climber(false);
172 }
173
Filip Kujawaa7c8b412024-02-24 18:29:29 -0800174 superstructure_goal_builder.CheckOk(superstructure_goal_builder.Send());
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800175 }
176
177 private:
Filip Kujawaa7c8b412024-02-24 18:29:29 -0800178 ::aos::Sender<control_loops::superstructure::GoalStatic>
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800179 superstructure_goal_sender_;
180 ::aos::Fetcher<control_loops::superstructure::Status>
181 superstructure_status_fetcher_;
Filip Kujawaa7c8b412024-02-24 18:29:29 -0800182 const y2024::Constants *robot_constants_;
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800183};
184
Stephan Pleinesf63bde82024-01-13 15:59:33 -0800185} // namespace y2024::input::joysticks
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800186
187int main(int argc, char **argv) {
188 ::aos::InitGoogle(&argc, &argv);
189
190 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
191 aos::configuration::ReadConfig("aos_config.json");
James Kuszmaul2549e752024-01-20 17:42:51 -0800192 frc971::constants::WaitForConstants<y2024::Constants>(&config.message());
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800193
Filip Kujawaa7c8b412024-02-24 18:29:29 -0800194 ::aos::ShmEventLoop constant_fetcher_event_loop(&config.message());
195 frc971::constants::ConstantsFetcher<y2024::Constants> constants_fetcher(
196 &constant_fetcher_event_loop);
197 const y2024::Constants *robot_constants = &constants_fetcher.constants();
198
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800199 ::aos::ShmEventLoop event_loop(&config.message());
Filip Kujawaa7c8b412024-02-24 18:29:29 -0800200 ::y2024::input::joysticks::Reader reader(&event_loop, robot_constants);
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800201
202 event_loop.Run();
203
204 return 0;
205}