blob: 711f9d2af2c14d1dc1733adfe52c2d09124e4289 [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.");
James Kuszmaule3b45692024-04-07 19:56:14 -070037DEFINE_bool(allow_force_preload, false,
38 "If set, enable the kForcePreload button.");
James Kuszmaul0338ec32024-03-16 11:40:51 -070039
Stephan Pleinesf63bde82024-01-13 15:59:33 -080040namespace y2024::input::joysticks {
Niko Sohmers3860f8a2024-01-12 21:05:19 -080041
Filip Kujawaa7c8b412024-02-24 18:29:29 -080042namespace superstructure = y2024::control_loops::superstructure;
43
James Kuszmaul0281e152024-02-26 22:26:16 -080044// TODO(Xander): add button location from physical wiring
45// Note: Due to use_redundant_joysticks, the AOS_LOG statements
46// for the internal joystick code will give offset joystick numbering.
James Kuszmaul0338ec32024-03-16 11:40:51 -070047const ButtonLocation kIntake(2, 2);
48
49const ButtonLocation kSpitRollers(1, 13);
Maxwell Henderson45878502024-03-15 19:35:25 -070050const ButtonLocation kSpitExtend(1, 12);
James Kuszmaul0338ec32024-03-16 11:40:51 -070051const ButtonLocation kIntakeRollers(2, 5);
52
53const ButtonLocation kCatapultLoad(2, 1);
54const ButtonLocation kAmp(2, 4);
55const ButtonLocation kFire(2, 8);
James Kuszmaule3b45692024-04-07 19:56:14 -070056const ButtonLocation kForceLoad(2, 9);
Austin Schuhcb703442024-03-23 14:30:05 -070057const ButtonLocation kDriverFire(1, 1);
James Kuszmaul0338ec32024-03-16 11:40:51 -070058const ButtonLocation kTrap(2, 6);
59const ButtonLocation kAutoAim(1, 8);
60const ButtonLocation kAimSpeaker(2, 11);
Filip Kujawaa7c8b412024-02-24 18:29:29 -080061const ButtonLocation kAimPodium(0, 0);
62const ButtonLocation kShoot(0, 0);
James Kuszmaul0281e152024-02-26 22:26:16 -080063const ButtonLocation kRaiseClimber(3, 2);
Niko Sohmers4d93d4c2024-03-24 14:48:26 -070064const ButtonLocation kRaiseFastClimber(3, 1);
Filip Kujawa1286c012024-03-31 22:53:27 -070065const ButtonLocation kAimShuttle(2, 10);
Austin Schuh79903e82024-04-07 23:12:00 -070066const ButtonLocation kTurretShuttle(1, 10);
Filip Kujawaa7c8b412024-02-24 18:29:29 -080067const ButtonLocation kExtraButtonTwo(0, 0);
68const ButtonLocation kExtraButtonThree(0, 0);
69const ButtonLocation kExtraButtonFour(0, 0);
70
Niko Sohmers3860f8a2024-01-12 21:05:19 -080071class Reader : public ::frc971::input::ActionJoystickInput {
72 public:
Filip Kujawaa7c8b412024-02-24 18:29:29 -080073 Reader(::aos::EventLoop *event_loop, const y2024::Constants *robot_constants)
Niko Sohmers3860f8a2024-01-12 21:05:19 -080074 : ::frc971::input::ActionJoystickInput(
75 event_loop,
James Kuszmaul2549e752024-01-20 17:42:51 -080076 ::y2024::control_loops::drivetrain::GetDrivetrainConfig(event_loop),
Niko Sohmers3860f8a2024-01-12 21:05:19 -080077 ::frc971::input::DrivetrainInputReader::InputType::kPistol,
78 {.use_redundant_joysticks = true}),
79 superstructure_goal_sender_(
Filip Kujawaa7c8b412024-02-24 18:29:29 -080080 event_loop->MakeSender<control_loops::superstructure::GoalStatic>(
Niko Sohmers3860f8a2024-01-12 21:05:19 -080081 "/superstructure")),
82 superstructure_status_fetcher_(
83 event_loop->MakeFetcher<control_loops::superstructure::Status>(
Filip Kujawaa7c8b412024-02-24 18:29:29 -080084 "/superstructure")),
85 robot_constants_(CHECK_NOTNULL(robot_constants)) {}
Niko Sohmers3860f8a2024-01-12 21:05:19 -080086
87 void AutoEnded() override { AOS_LOG(INFO, "Auto ended.\n"); }
88
89 void HandleTeleop(
90 const ::frc971::input::driver_station::Data &data) override {
Niko Sohmers3860f8a2024-01-12 21:05:19 -080091 superstructure_status_fetcher_.Fetch();
92 if (!superstructure_status_fetcher_.get()) {
93 AOS_LOG(ERROR, "Got no superstructure status message.\n");
94 return;
95 }
Filip Kujawaa7c8b412024-02-24 18:29:29 -080096
97 aos::Sender<superstructure::GoalStatic>::StaticBuilder
98 superstructure_goal_builder =
99 superstructure_goal_sender_.MakeStaticBuilder();
100
James Kuszmaul42a87452024-04-05 17:30:47 -0700101 if (data.IsPressed(kIntake) || climbed_count_ > 25) {
Filip Kujawaa7c8b412024-02-24 18:29:29 -0800102 // Intake is pressed
James Kuszmaul0338ec32024-03-16 11:40:51 -0700103 superstructure_goal_builder->set_intake_pivot(
104 superstructure::IntakePivotGoal::DOWN);
105 } else {
106 superstructure_goal_builder->set_intake_pivot(
107 superstructure::IntakePivotGoal::UP);
108 }
109
110 if (data.IsPressed(kIntakeRollers)) {
111 // Intake is pressed
Filip Kujawaa7c8b412024-02-24 18:29:29 -0800112 superstructure_goal_builder->set_intake_goal(
113 superstructure::IntakeGoal::INTAKE);
James Kuszmaul0338ec32024-03-16 11:40:51 -0700114 } else if (data.IsPressed(kSpitRollers)) {
115 superstructure_goal_builder->set_intake_goal(
116 superstructure::IntakeGoal::SPIT);
James Kuszmaul0281e152024-02-26 22:26:16 -0800117 } else {
Filip Kujawaa7c8b412024-02-24 18:29:29 -0800118 superstructure_goal_builder->set_intake_goal(
James Kuszmaul0281e152024-02-26 22:26:16 -0800119 superstructure::IntakeGoal::NONE);
Filip Kujawaa7c8b412024-02-24 18:29:29 -0800120 }
James Kuszmaul0338ec32024-03-16 11:40:51 -0700121
Maxwell Henderson45878502024-03-15 19:35:25 -0700122 if (data.IsPressed(kSpitExtend)) {
123 superstructure_goal_builder->set_spit_extend(true);
124 } else {
125 superstructure_goal_builder->set_spit_extend(false);
126 }
127
James Kuszmaul0281e152024-02-26 22:26:16 -0800128 if (data.IsPressed(kAmp)) {
Filip Kujawaa7c8b412024-02-24 18:29:29 -0800129 superstructure_goal_builder->set_note_goal(superstructure::NoteGoal::AMP);
130 } else if (data.IsPressed(kTrap)) {
131 superstructure_goal_builder->set_note_goal(
132 superstructure::NoteGoal::TRAP);
James Kuszmaul0281e152024-02-26 22:26:16 -0800133 } else if (data.IsPressed(kCatapultLoad)) {
134 superstructure_goal_builder->set_note_goal(
135 superstructure::NoteGoal::CATAPULT);
136 } else {
137 superstructure_goal_builder->set_note_goal(
138 superstructure::NoteGoal::NONE);
Filip Kujawaa7c8b412024-02-24 18:29:29 -0800139 }
Filip Kujawaa7c8b412024-02-24 18:29:29 -0800140 auto shooter_goal = superstructure_goal_builder->add_shooter_goal();
James Kuszmaule3b45692024-04-07 19:56:14 -0700141 shooter_goal->set_preloaded(FLAGS_allow_force_preload &&
142 data.IsPressed(kForceLoad));
Filip Kujawa1286c012024-03-31 22:53:27 -0700143 if (data.IsPressed(kAutoAim)) {
144 shooter_goal->set_auto_aim(
145 control_loops::superstructure::AutoAimMode::SPEAKER);
146 } else if (data.IsPressed(kAimShuttle)) {
147 shooter_goal->set_auto_aim(
148 control_loops::superstructure::AutoAimMode::SHUTTLE);
Austin Schuh79903e82024-04-07 23:12:00 -0700149 } else if (data.IsPressed(kTurretShuttle)) {
150 shooter_goal->set_auto_aim(
151 control_loops::superstructure::AutoAimMode::TURRET_SHUTTLE);
Filip Kujawa1286c012024-03-31 22:53:27 -0700152 }
Filip Kujawaa7c8b412024-02-24 18:29:29 -0800153
154 // Updating aiming for shooter goal, only one type of aim should be possible
155 // at a time, auto-aiming is preferred over the setpoints.
James Kuszmaul0281e152024-02-26 22:26:16 -0800156 if (data.IsPressed(kAimSpeaker)) {
Filip Kujawaa7c8b412024-02-24 18:29:29 -0800157 auto catapult_goal = shooter_goal->add_catapult_goal();
158 catapult_goal->set_shot_velocity(robot_constants_->common()
159 ->shooter_speaker_set_point()
160 ->shot_velocity());
James Kuszmaul0338ec32024-03-16 11:40:51 -0700161 if (FLAGS_speaker_altitude_position_override > 0) {
162 PopulateStaticZeroingSingleDOFProfiledSubsystemGoal(
163 shooter_goal->add_altitude_position(),
164 FLAGS_speaker_altitude_position_override);
165 } else {
166 PopulateStaticZeroingSingleDOFProfiledSubsystemGoal(
167 shooter_goal->add_altitude_position(),
168 robot_constants_->common()
169 ->shooter_speaker_set_point()
170 ->altitude_position());
171 }
Filip Kujawaa7c8b412024-02-24 18:29:29 -0800172 PopulateStaticZeroingSingleDOFProfiledSubsystemGoal(
173 shooter_goal->add_turret_position(), robot_constants_->common()
174 ->shooter_speaker_set_point()
175 ->turret_position());
Filip Kujawaa7c8b412024-02-24 18:29:29 -0800176 }
Austin Schuhcb703442024-03-23 14:30:05 -0700177 superstructure_goal_builder->set_fire(data.IsPressed(kFire) ||
178 data.IsPressed(kDriverFire));
Filip Kujawaa7c8b412024-02-24 18:29:29 -0800179
Niko Sohmers4d93d4c2024-03-24 14:48:26 -0700180 if (data.IsPressed(kRaiseClimber)) {
James Kuszmaul42a87452024-04-05 17:30:47 -0700181 ++climbed_count_;
Niko Sohmers4d93d4c2024-03-24 14:48:26 -0700182 superstructure_goal_builder->set_climber_goal_voltage(4.0);
183 } else if (data.IsPressed(kRaiseFastClimber)) {
James Kuszmaul42a87452024-04-05 17:30:47 -0700184 ++climbed_count_;
185 superstructure_goal_builder->set_climber_goal_voltage(12.0);
186 }
187
188 if (climbed_count_ > 25) {
189 PopulateStaticZeroingSingleDOFProfiledSubsystemGoal(
190 shooter_goal->add_turret_position(), 0.0);
James Kuszmaul0338ec32024-03-16 11:40:51 -0700191 }
192
Filip Kujawaa7c8b412024-02-24 18:29:29 -0800193 superstructure_goal_builder.CheckOk(superstructure_goal_builder.Send());
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800194 }
195
196 private:
Filip Kujawaa7c8b412024-02-24 18:29:29 -0800197 ::aos::Sender<control_loops::superstructure::GoalStatic>
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800198 superstructure_goal_sender_;
199 ::aos::Fetcher<control_loops::superstructure::Status>
200 superstructure_status_fetcher_;
Filip Kujawaa7c8b412024-02-24 18:29:29 -0800201 const y2024::Constants *robot_constants_;
James Kuszmaul42a87452024-04-05 17:30:47 -0700202
203 int climbed_count_ = 0;
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800204};
205
Stephan Pleinesf63bde82024-01-13 15:59:33 -0800206} // namespace y2024::input::joysticks
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800207
208int main(int argc, char **argv) {
209 ::aos::InitGoogle(&argc, &argv);
210
211 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
212 aos::configuration::ReadConfig("aos_config.json");
James Kuszmaul2549e752024-01-20 17:42:51 -0800213 frc971::constants::WaitForConstants<y2024::Constants>(&config.message());
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800214
Filip Kujawaa7c8b412024-02-24 18:29:29 -0800215 ::aos::ShmEventLoop constant_fetcher_event_loop(&config.message());
216 frc971::constants::ConstantsFetcher<y2024::Constants> constants_fetcher(
217 &constant_fetcher_event_loop);
218 const y2024::Constants *robot_constants = &constants_fetcher.constants();
219
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800220 ::aos::ShmEventLoop event_loop(&config.message());
Filip Kujawaa7c8b412024-02-24 18:29:29 -0800221 ::y2024::input::joysticks::Reader reader(&event_loop, robot_constants);
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800222
223 event_loop.Run();
224
225 return 0;
226}