blob: 5b2fc74b525f960c159d016c6e88f9d72062aa19 [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);
Niko Sohmers4d93d4c2024-03-24 14:48:26 -070061const ButtonLocation kRaiseFastClimber(3, 1);
Filip Kujawaa7c8b412024-02-24 18:29:29 -080062const ButtonLocation kExtraButtonOne(0, 0);
63const ButtonLocation kExtraButtonTwo(0, 0);
64const ButtonLocation kExtraButtonThree(0, 0);
65const ButtonLocation kExtraButtonFour(0, 0);
66
Niko Sohmers3860f8a2024-01-12 21:05:19 -080067class Reader : public ::frc971::input::ActionJoystickInput {
68 public:
Filip Kujawaa7c8b412024-02-24 18:29:29 -080069 Reader(::aos::EventLoop *event_loop, const y2024::Constants *robot_constants)
Niko Sohmers3860f8a2024-01-12 21:05:19 -080070 : ::frc971::input::ActionJoystickInput(
71 event_loop,
James Kuszmaul2549e752024-01-20 17:42:51 -080072 ::y2024::control_loops::drivetrain::GetDrivetrainConfig(event_loop),
Niko Sohmers3860f8a2024-01-12 21:05:19 -080073 ::frc971::input::DrivetrainInputReader::InputType::kPistol,
74 {.use_redundant_joysticks = true}),
75 superstructure_goal_sender_(
Filip Kujawaa7c8b412024-02-24 18:29:29 -080076 event_loop->MakeSender<control_loops::superstructure::GoalStatic>(
Niko Sohmers3860f8a2024-01-12 21:05:19 -080077 "/superstructure")),
78 superstructure_status_fetcher_(
79 event_loop->MakeFetcher<control_loops::superstructure::Status>(
Filip Kujawaa7c8b412024-02-24 18:29:29 -080080 "/superstructure")),
81 robot_constants_(CHECK_NOTNULL(robot_constants)) {}
Niko Sohmers3860f8a2024-01-12 21:05:19 -080082
83 void AutoEnded() override { AOS_LOG(INFO, "Auto ended.\n"); }
84
85 void HandleTeleop(
86 const ::frc971::input::driver_station::Data &data) override {
Niko Sohmers3860f8a2024-01-12 21:05:19 -080087 superstructure_status_fetcher_.Fetch();
88 if (!superstructure_status_fetcher_.get()) {
89 AOS_LOG(ERROR, "Got no superstructure status message.\n");
90 return;
91 }
Filip Kujawaa7c8b412024-02-24 18:29:29 -080092
93 aos::Sender<superstructure::GoalStatic>::StaticBuilder
94 superstructure_goal_builder =
95 superstructure_goal_sender_.MakeStaticBuilder();
96
97 if (data.IsPressed(kIntake)) {
98 // Intake is pressed
James Kuszmaul0338ec32024-03-16 11:40:51 -070099 superstructure_goal_builder->set_intake_pivot(
100 superstructure::IntakePivotGoal::DOWN);
101 } else {
102 superstructure_goal_builder->set_intake_pivot(
103 superstructure::IntakePivotGoal::UP);
104 }
105
106 if (data.IsPressed(kIntakeRollers)) {
107 // Intake is pressed
Filip Kujawaa7c8b412024-02-24 18:29:29 -0800108 superstructure_goal_builder->set_intake_goal(
109 superstructure::IntakeGoal::INTAKE);
James Kuszmaul0338ec32024-03-16 11:40:51 -0700110 } else if (data.IsPressed(kSpitRollers)) {
111 superstructure_goal_builder->set_intake_goal(
112 superstructure::IntakeGoal::SPIT);
James Kuszmaul0281e152024-02-26 22:26:16 -0800113 } else {
Filip Kujawaa7c8b412024-02-24 18:29:29 -0800114 superstructure_goal_builder->set_intake_goal(
James Kuszmaul0281e152024-02-26 22:26:16 -0800115 superstructure::IntakeGoal::NONE);
Filip Kujawaa7c8b412024-02-24 18:29:29 -0800116 }
James Kuszmaul0338ec32024-03-16 11:40:51 -0700117
Maxwell Henderson45878502024-03-15 19:35:25 -0700118 if (data.IsPressed(kSpitExtend)) {
119 superstructure_goal_builder->set_spit_extend(true);
120 } else {
121 superstructure_goal_builder->set_spit_extend(false);
122 }
123
James Kuszmaul0281e152024-02-26 22:26:16 -0800124 if (data.IsPressed(kAmp)) {
Filip Kujawaa7c8b412024-02-24 18:29:29 -0800125 superstructure_goal_builder->set_note_goal(superstructure::NoteGoal::AMP);
126 } else if (data.IsPressed(kTrap)) {
127 superstructure_goal_builder->set_note_goal(
128 superstructure::NoteGoal::TRAP);
James Kuszmaul0281e152024-02-26 22:26:16 -0800129 } else if (data.IsPressed(kCatapultLoad)) {
130 superstructure_goal_builder->set_note_goal(
131 superstructure::NoteGoal::CATAPULT);
132 } else {
133 superstructure_goal_builder->set_note_goal(
134 superstructure::NoteGoal::NONE);
Filip Kujawaa7c8b412024-02-24 18:29:29 -0800135 }
Filip Kujawaa7c8b412024-02-24 18:29:29 -0800136 auto shooter_goal = superstructure_goal_builder->add_shooter_goal();
James Kuszmaul0338ec32024-03-16 11:40:51 -0700137 shooter_goal->set_auto_aim(data.IsPressed(kAutoAim));
Filip Kujawaa7c8b412024-02-24 18:29:29 -0800138
139 // Updating aiming for shooter goal, only one type of aim should be possible
140 // at a time, auto-aiming is preferred over the setpoints.
James Kuszmaul0281e152024-02-26 22:26:16 -0800141 if (data.IsPressed(kAimSpeaker)) {
Filip Kujawaa7c8b412024-02-24 18:29:29 -0800142 auto catapult_goal = shooter_goal->add_catapult_goal();
143 catapult_goal->set_shot_velocity(robot_constants_->common()
144 ->shooter_speaker_set_point()
145 ->shot_velocity());
James Kuszmaul0338ec32024-03-16 11:40:51 -0700146 if (FLAGS_speaker_altitude_position_override > 0) {
147 PopulateStaticZeroingSingleDOFProfiledSubsystemGoal(
148 shooter_goal->add_altitude_position(),
149 FLAGS_speaker_altitude_position_override);
150 } else {
151 PopulateStaticZeroingSingleDOFProfiledSubsystemGoal(
152 shooter_goal->add_altitude_position(),
153 robot_constants_->common()
154 ->shooter_speaker_set_point()
155 ->altitude_position());
156 }
Filip Kujawaa7c8b412024-02-24 18:29:29 -0800157 PopulateStaticZeroingSingleDOFProfiledSubsystemGoal(
158 shooter_goal->add_turret_position(), robot_constants_->common()
159 ->shooter_speaker_set_point()
160 ->turret_position());
Filip Kujawaa7c8b412024-02-24 18:29:29 -0800161 }
Austin Schuhcb703442024-03-23 14:30:05 -0700162 superstructure_goal_builder->set_fire(data.IsPressed(kFire) ||
163 data.IsPressed(kDriverFire));
Filip Kujawaa7c8b412024-02-24 18:29:29 -0800164
Niko Sohmers4d93d4c2024-03-24 14:48:26 -0700165 if (data.IsPressed(kRaiseClimber)) {
166 superstructure_goal_builder->set_climber_goal_voltage(4.0);
167 } else if (data.IsPressed(kRaiseFastClimber)) {
168 superstructure_goal_builder->set_climber_goal_voltage(6.0);
James Kuszmaul0338ec32024-03-16 11:40:51 -0700169 }
170
Filip Kujawaa7c8b412024-02-24 18:29:29 -0800171 superstructure_goal_builder.CheckOk(superstructure_goal_builder.Send());
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800172 }
173
174 private:
Filip Kujawaa7c8b412024-02-24 18:29:29 -0800175 ::aos::Sender<control_loops::superstructure::GoalStatic>
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800176 superstructure_goal_sender_;
177 ::aos::Fetcher<control_loops::superstructure::Status>
178 superstructure_status_fetcher_;
Filip Kujawaa7c8b412024-02-24 18:29:29 -0800179 const y2024::Constants *robot_constants_;
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800180};
181
Stephan Pleinesf63bde82024-01-13 15:59:33 -0800182} // namespace y2024::input::joysticks
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800183
184int main(int argc, char **argv) {
185 ::aos::InitGoogle(&argc, &argv);
186
187 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
188 aos::configuration::ReadConfig("aos_config.json");
James Kuszmaul2549e752024-01-20 17:42:51 -0800189 frc971::constants::WaitForConstants<y2024::Constants>(&config.message());
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800190
Filip Kujawaa7c8b412024-02-24 18:29:29 -0800191 ::aos::ShmEventLoop constant_fetcher_event_loop(&config.message());
192 frc971::constants::ConstantsFetcher<y2024::Constants> constants_fetcher(
193 &constant_fetcher_event_loop);
194 const y2024::Constants *robot_constants = &constants_fetcher.constants();
195
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800196 ::aos::ShmEventLoop event_loop(&config.message());
Filip Kujawaa7c8b412024-02-24 18:29:29 -0800197 ::y2024::input::joysticks::Reader reader(&event_loop, robot_constants);
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800198
199 event_loop.Run();
200
201 return 0;
202}