blob: 293e57e60bc11d38fe405d1aa16a5dd490ca7546 [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);
Maxwell Henderson45878502024-03-15 19:35:25 -070048const ButtonLocation kSpitExtend(1, 12);
James Kuszmaul0338ec32024-03-16 11:40:51 -070049const ButtonLocation kIntakeRollers(2, 5);
50
51const ButtonLocation kCatapultLoad(2, 1);
52const ButtonLocation kAmp(2, 4);
53const ButtonLocation kFire(2, 8);
Austin Schuhcb703442024-03-23 14:30:05 -070054const ButtonLocation kDriverFire(1, 1);
James Kuszmaul0338ec32024-03-16 11:40:51 -070055const ButtonLocation kTrap(2, 6);
56const ButtonLocation kAutoAim(1, 8);
57const ButtonLocation kAimSpeaker(2, 11);
Filip Kujawaa7c8b412024-02-24 18:29:29 -080058const ButtonLocation kAimPodium(0, 0);
59const ButtonLocation kShoot(0, 0);
James Kuszmaul0281e152024-02-26 22:26:16 -080060const ButtonLocation kRaiseClimber(3, 2);
James Kuszmaul0338ec32024-03-16 11:40:51 -070061const ButtonLocation kSlowClimber(3, 1);
62const ButtonLocation kRetractClimber(2, 3);
Filip Kujawaa7c8b412024-02-24 18:29:29 -080063const ButtonLocation kExtraButtonOne(0, 0);
64const ButtonLocation kExtraButtonTwo(0, 0);
65const ButtonLocation kExtraButtonThree(0, 0);
66const ButtonLocation kExtraButtonFour(0, 0);
67
Niko Sohmers3860f8a2024-01-12 21:05:19 -080068class Reader : public ::frc971::input::ActionJoystickInput {
69 public:
Filip Kujawaa7c8b412024-02-24 18:29:29 -080070 Reader(::aos::EventLoop *event_loop, const y2024::Constants *robot_constants)
Niko Sohmers3860f8a2024-01-12 21:05:19 -080071 : ::frc971::input::ActionJoystickInput(
72 event_loop,
James Kuszmaul2549e752024-01-20 17:42:51 -080073 ::y2024::control_loops::drivetrain::GetDrivetrainConfig(event_loop),
Niko Sohmers3860f8a2024-01-12 21:05:19 -080074 ::frc971::input::DrivetrainInputReader::InputType::kPistol,
75 {.use_redundant_joysticks = true}),
76 superstructure_goal_sender_(
Filip Kujawaa7c8b412024-02-24 18:29:29 -080077 event_loop->MakeSender<control_loops::superstructure::GoalStatic>(
Niko Sohmers3860f8a2024-01-12 21:05:19 -080078 "/superstructure")),
79 superstructure_status_fetcher_(
80 event_loop->MakeFetcher<control_loops::superstructure::Status>(
Filip Kujawaa7c8b412024-02-24 18:29:29 -080081 "/superstructure")),
82 robot_constants_(CHECK_NOTNULL(robot_constants)) {}
Niko Sohmers3860f8a2024-01-12 21:05:19 -080083
84 void AutoEnded() override { AOS_LOG(INFO, "Auto ended.\n"); }
85
86 void HandleTeleop(
87 const ::frc971::input::driver_station::Data &data) override {
Niko Sohmers3860f8a2024-01-12 21:05:19 -080088 superstructure_status_fetcher_.Fetch();
89 if (!superstructure_status_fetcher_.get()) {
90 AOS_LOG(ERROR, "Got no superstructure status message.\n");
91 return;
92 }
Filip Kujawaa7c8b412024-02-24 18:29:29 -080093
94 aos::Sender<superstructure::GoalStatic>::StaticBuilder
95 superstructure_goal_builder =
96 superstructure_goal_sender_.MakeStaticBuilder();
97
98 if (data.IsPressed(kIntake)) {
99 // Intake is pressed
James Kuszmaul0338ec32024-03-16 11:40:51 -0700100 superstructure_goal_builder->set_intake_pivot(
101 superstructure::IntakePivotGoal::DOWN);
102 } else {
103 superstructure_goal_builder->set_intake_pivot(
104 superstructure::IntakePivotGoal::UP);
105 }
106
107 if (data.IsPressed(kIntakeRollers)) {
108 // Intake is pressed
Filip Kujawaa7c8b412024-02-24 18:29:29 -0800109 superstructure_goal_builder->set_intake_goal(
110 superstructure::IntakeGoal::INTAKE);
James Kuszmaul0338ec32024-03-16 11:40:51 -0700111 } else if (data.IsPressed(kSpitRollers)) {
112 superstructure_goal_builder->set_intake_goal(
113 superstructure::IntakeGoal::SPIT);
James Kuszmaul0281e152024-02-26 22:26:16 -0800114 } else {
Filip Kujawaa7c8b412024-02-24 18:29:29 -0800115 superstructure_goal_builder->set_intake_goal(
James Kuszmaul0281e152024-02-26 22:26:16 -0800116 superstructure::IntakeGoal::NONE);
Filip Kujawaa7c8b412024-02-24 18:29:29 -0800117 }
James Kuszmaul0338ec32024-03-16 11:40:51 -0700118
Maxwell Henderson45878502024-03-15 19:35:25 -0700119 if (data.IsPressed(kSpitExtend)) {
120 superstructure_goal_builder->set_spit_extend(true);
121 } else {
122 superstructure_goal_builder->set_spit_extend(false);
123 }
124
James Kuszmaul0281e152024-02-26 22:26:16 -0800125 if (data.IsPressed(kAmp)) {
Filip Kujawaa7c8b412024-02-24 18:29:29 -0800126 superstructure_goal_builder->set_note_goal(superstructure::NoteGoal::AMP);
127 } else if (data.IsPressed(kTrap)) {
128 superstructure_goal_builder->set_note_goal(
129 superstructure::NoteGoal::TRAP);
James Kuszmaul0281e152024-02-26 22:26:16 -0800130 } else if (data.IsPressed(kCatapultLoad)) {
131 superstructure_goal_builder->set_note_goal(
132 superstructure::NoteGoal::CATAPULT);
133 } else {
134 superstructure_goal_builder->set_note_goal(
135 superstructure::NoteGoal::NONE);
Filip Kujawaa7c8b412024-02-24 18:29:29 -0800136 }
Filip Kujawaa7c8b412024-02-24 18:29:29 -0800137 auto shooter_goal = superstructure_goal_builder->add_shooter_goal();
James Kuszmaul0338ec32024-03-16 11:40:51 -0700138 shooter_goal->set_auto_aim(data.IsPressed(kAutoAim));
Filip Kujawaa7c8b412024-02-24 18:29:29 -0800139
140 // Updating aiming for shooter goal, only one type of aim should be possible
141 // at a time, auto-aiming is preferred over the setpoints.
James Kuszmaul0281e152024-02-26 22:26:16 -0800142 if (data.IsPressed(kAimSpeaker)) {
Filip Kujawaa7c8b412024-02-24 18:29:29 -0800143 auto catapult_goal = shooter_goal->add_catapult_goal();
144 catapult_goal->set_shot_velocity(robot_constants_->common()
145 ->shooter_speaker_set_point()
146 ->shot_velocity());
James Kuszmaul0338ec32024-03-16 11:40:51 -0700147 if (FLAGS_speaker_altitude_position_override > 0) {
148 PopulateStaticZeroingSingleDOFProfiledSubsystemGoal(
149 shooter_goal->add_altitude_position(),
150 FLAGS_speaker_altitude_position_override);
151 } else {
152 PopulateStaticZeroingSingleDOFProfiledSubsystemGoal(
153 shooter_goal->add_altitude_position(),
154 robot_constants_->common()
155 ->shooter_speaker_set_point()
156 ->altitude_position());
157 }
Filip Kujawaa7c8b412024-02-24 18:29:29 -0800158 PopulateStaticZeroingSingleDOFProfiledSubsystemGoal(
159 shooter_goal->add_turret_position(), robot_constants_->common()
160 ->shooter_speaker_set_point()
161 ->turret_position());
Filip Kujawaa7c8b412024-02-24 18:29:29 -0800162 }
Austin Schuhcb703442024-03-23 14:30:05 -0700163 superstructure_goal_builder->set_fire(data.IsPressed(kFire) ||
164 data.IsPressed(kDriverFire));
Filip Kujawaa7c8b412024-02-24 18:29:29 -0800165
James Kuszmaul0338ec32024-03-16 11:40:51 -0700166 if (data.IsPressed(kRetractClimber)) {
Filip Kujawaa7c8b412024-02-24 18:29:29 -0800167 superstructure_goal_builder->set_climber_goal(
168 superstructure::ClimberGoal::RETRACT);
James Kuszmaul0338ec32024-03-16 11:40:51 -0700169 } else if (data.IsPressed(kRaiseClimber)) {
170 superstructure_goal_builder->set_climber_goal(
171 superstructure::ClimberGoal::FULL_EXTEND);
James Kuszmaul0281e152024-02-26 22:26:16 -0800172 } else {
173 superstructure_goal_builder->set_climber_goal(
174 superstructure::ClimberGoal::STOWED);
Filip Kujawaa7c8b412024-02-24 18:29:29 -0800175 }
176
James Kuszmaul0338ec32024-03-16 11:40:51 -0700177 if (data.IsPressed(kSlowClimber)) {
178 superstructure_goal_builder->set_slow_climber(true);
179 } else {
180 superstructure_goal_builder->set_slow_climber(false);
181 }
182
Filip Kujawaa7c8b412024-02-24 18:29:29 -0800183 superstructure_goal_builder.CheckOk(superstructure_goal_builder.Send());
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800184 }
185
186 private:
Filip Kujawaa7c8b412024-02-24 18:29:29 -0800187 ::aos::Sender<control_loops::superstructure::GoalStatic>
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800188 superstructure_goal_sender_;
189 ::aos::Fetcher<control_loops::superstructure::Status>
190 superstructure_status_fetcher_;
Filip Kujawaa7c8b412024-02-24 18:29:29 -0800191 const y2024::Constants *robot_constants_;
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800192};
193
Stephan Pleinesf63bde82024-01-13 15:59:33 -0800194} // namespace y2024::input::joysticks
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800195
196int main(int argc, char **argv) {
197 ::aos::InitGoogle(&argc, &argv);
198
199 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
200 aos::configuration::ReadConfig("aos_config.json");
James Kuszmaul2549e752024-01-20 17:42:51 -0800201 frc971::constants::WaitForConstants<y2024::Constants>(&config.message());
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800202
Filip Kujawaa7c8b412024-02-24 18:29:29 -0800203 ::aos::ShmEventLoop constant_fetcher_event_loop(&config.message());
204 frc971::constants::ConstantsFetcher<y2024::Constants> constants_fetcher(
205 &constant_fetcher_event_loop);
206 const y2024::Constants *robot_constants = &constants_fetcher.constants();
207
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800208 ::aos::ShmEventLoop event_loop(&config.message());
Filip Kujawaa7c8b412024-02-24 18:29:29 -0800209 ::y2024::input::joysticks::Reader reader(&event_loop, robot_constants);
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800210
211 event_loop.Run();
212
213 return 0;
214}