blob: 2fb75c2d2c03198e12b56e6db604c8ce08b071d6 [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);
Filip Kujawaa7c8b412024-02-24 18:29:29 -080066const ButtonLocation kExtraButtonTwo(0, 0);
67const ButtonLocation kExtraButtonThree(0, 0);
68const ButtonLocation kExtraButtonFour(0, 0);
69
Niko Sohmers3860f8a2024-01-12 21:05:19 -080070class Reader : public ::frc971::input::ActionJoystickInput {
71 public:
Filip Kujawaa7c8b412024-02-24 18:29:29 -080072 Reader(::aos::EventLoop *event_loop, const y2024::Constants *robot_constants)
Niko Sohmers3860f8a2024-01-12 21:05:19 -080073 : ::frc971::input::ActionJoystickInput(
74 event_loop,
James Kuszmaul2549e752024-01-20 17:42:51 -080075 ::y2024::control_loops::drivetrain::GetDrivetrainConfig(event_loop),
Niko Sohmers3860f8a2024-01-12 21:05:19 -080076 ::frc971::input::DrivetrainInputReader::InputType::kPistol,
77 {.use_redundant_joysticks = true}),
78 superstructure_goal_sender_(
Filip Kujawaa7c8b412024-02-24 18:29:29 -080079 event_loop->MakeSender<control_loops::superstructure::GoalStatic>(
Niko Sohmers3860f8a2024-01-12 21:05:19 -080080 "/superstructure")),
81 superstructure_status_fetcher_(
82 event_loop->MakeFetcher<control_loops::superstructure::Status>(
Filip Kujawaa7c8b412024-02-24 18:29:29 -080083 "/superstructure")),
84 robot_constants_(CHECK_NOTNULL(robot_constants)) {}
Niko Sohmers3860f8a2024-01-12 21:05:19 -080085
86 void AutoEnded() override { AOS_LOG(INFO, "Auto ended.\n"); }
87
88 void HandleTeleop(
89 const ::frc971::input::driver_station::Data &data) override {
Niko Sohmers3860f8a2024-01-12 21:05:19 -080090 superstructure_status_fetcher_.Fetch();
91 if (!superstructure_status_fetcher_.get()) {
92 AOS_LOG(ERROR, "Got no superstructure status message.\n");
93 return;
94 }
Filip Kujawaa7c8b412024-02-24 18:29:29 -080095
96 aos::Sender<superstructure::GoalStatic>::StaticBuilder
97 superstructure_goal_builder =
98 superstructure_goal_sender_.MakeStaticBuilder();
99
James Kuszmaul42a87452024-04-05 17:30:47 -0700100 if (data.IsPressed(kIntake) || climbed_count_ > 25) {
Filip Kujawaa7c8b412024-02-24 18:29:29 -0800101 // Intake is pressed
James Kuszmaul0338ec32024-03-16 11:40:51 -0700102 superstructure_goal_builder->set_intake_pivot(
103 superstructure::IntakePivotGoal::DOWN);
104 } else {
105 superstructure_goal_builder->set_intake_pivot(
106 superstructure::IntakePivotGoal::UP);
107 }
108
109 if (data.IsPressed(kIntakeRollers)) {
110 // Intake is pressed
Filip Kujawaa7c8b412024-02-24 18:29:29 -0800111 superstructure_goal_builder->set_intake_goal(
112 superstructure::IntakeGoal::INTAKE);
James Kuszmaul0338ec32024-03-16 11:40:51 -0700113 } else if (data.IsPressed(kSpitRollers)) {
114 superstructure_goal_builder->set_intake_goal(
115 superstructure::IntakeGoal::SPIT);
James Kuszmaul0281e152024-02-26 22:26:16 -0800116 } else {
Filip Kujawaa7c8b412024-02-24 18:29:29 -0800117 superstructure_goal_builder->set_intake_goal(
James Kuszmaul0281e152024-02-26 22:26:16 -0800118 superstructure::IntakeGoal::NONE);
Filip Kujawaa7c8b412024-02-24 18:29:29 -0800119 }
James Kuszmaul0338ec32024-03-16 11:40:51 -0700120
Maxwell Henderson45878502024-03-15 19:35:25 -0700121 if (data.IsPressed(kSpitExtend)) {
122 superstructure_goal_builder->set_spit_extend(true);
123 } else {
124 superstructure_goal_builder->set_spit_extend(false);
125 }
126
James Kuszmaul0281e152024-02-26 22:26:16 -0800127 if (data.IsPressed(kAmp)) {
Filip Kujawaa7c8b412024-02-24 18:29:29 -0800128 superstructure_goal_builder->set_note_goal(superstructure::NoteGoal::AMP);
129 } else if (data.IsPressed(kTrap)) {
130 superstructure_goal_builder->set_note_goal(
131 superstructure::NoteGoal::TRAP);
James Kuszmaul0281e152024-02-26 22:26:16 -0800132 } else if (data.IsPressed(kCatapultLoad)) {
133 superstructure_goal_builder->set_note_goal(
134 superstructure::NoteGoal::CATAPULT);
135 } else {
136 superstructure_goal_builder->set_note_goal(
137 superstructure::NoteGoal::NONE);
Filip Kujawaa7c8b412024-02-24 18:29:29 -0800138 }
Filip Kujawaa7c8b412024-02-24 18:29:29 -0800139 auto shooter_goal = superstructure_goal_builder->add_shooter_goal();
James Kuszmaule3b45692024-04-07 19:56:14 -0700140 shooter_goal->set_preloaded(FLAGS_allow_force_preload &&
141 data.IsPressed(kForceLoad));
Filip Kujawa1286c012024-03-31 22:53:27 -0700142 if (data.IsPressed(kAutoAim)) {
143 shooter_goal->set_auto_aim(
144 control_loops::superstructure::AutoAimMode::SPEAKER);
145 } else if (data.IsPressed(kAimShuttle)) {
146 shooter_goal->set_auto_aim(
147 control_loops::superstructure::AutoAimMode::SHUTTLE);
148 }
Filip Kujawaa7c8b412024-02-24 18:29:29 -0800149
150 // Updating aiming for shooter goal, only one type of aim should be possible
151 // at a time, auto-aiming is preferred over the setpoints.
James Kuszmaul0281e152024-02-26 22:26:16 -0800152 if (data.IsPressed(kAimSpeaker)) {
Filip Kujawaa7c8b412024-02-24 18:29:29 -0800153 auto catapult_goal = shooter_goal->add_catapult_goal();
154 catapult_goal->set_shot_velocity(robot_constants_->common()
155 ->shooter_speaker_set_point()
156 ->shot_velocity());
James Kuszmaul0338ec32024-03-16 11:40:51 -0700157 if (FLAGS_speaker_altitude_position_override > 0) {
158 PopulateStaticZeroingSingleDOFProfiledSubsystemGoal(
159 shooter_goal->add_altitude_position(),
160 FLAGS_speaker_altitude_position_override);
161 } else {
162 PopulateStaticZeroingSingleDOFProfiledSubsystemGoal(
163 shooter_goal->add_altitude_position(),
164 robot_constants_->common()
165 ->shooter_speaker_set_point()
166 ->altitude_position());
167 }
Filip Kujawaa7c8b412024-02-24 18:29:29 -0800168 PopulateStaticZeroingSingleDOFProfiledSubsystemGoal(
169 shooter_goal->add_turret_position(), robot_constants_->common()
170 ->shooter_speaker_set_point()
171 ->turret_position());
Filip Kujawaa7c8b412024-02-24 18:29:29 -0800172 }
Austin Schuhcb703442024-03-23 14:30:05 -0700173 superstructure_goal_builder->set_fire(data.IsPressed(kFire) ||
174 data.IsPressed(kDriverFire));
Filip Kujawaa7c8b412024-02-24 18:29:29 -0800175
Niko Sohmers4d93d4c2024-03-24 14:48:26 -0700176 if (data.IsPressed(kRaiseClimber)) {
James Kuszmaul42a87452024-04-05 17:30:47 -0700177 ++climbed_count_;
Niko Sohmers4d93d4c2024-03-24 14:48:26 -0700178 superstructure_goal_builder->set_climber_goal_voltage(4.0);
179 } else if (data.IsPressed(kRaiseFastClimber)) {
James Kuszmaul42a87452024-04-05 17:30:47 -0700180 ++climbed_count_;
181 superstructure_goal_builder->set_climber_goal_voltage(12.0);
182 }
183
184 if (climbed_count_ > 25) {
185 PopulateStaticZeroingSingleDOFProfiledSubsystemGoal(
186 shooter_goal->add_turret_position(), 0.0);
James Kuszmaul0338ec32024-03-16 11:40:51 -0700187 }
188
Filip Kujawaa7c8b412024-02-24 18:29:29 -0800189 superstructure_goal_builder.CheckOk(superstructure_goal_builder.Send());
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800190 }
191
192 private:
Filip Kujawaa7c8b412024-02-24 18:29:29 -0800193 ::aos::Sender<control_loops::superstructure::GoalStatic>
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800194 superstructure_goal_sender_;
195 ::aos::Fetcher<control_loops::superstructure::Status>
196 superstructure_status_fetcher_;
Filip Kujawaa7c8b412024-02-24 18:29:29 -0800197 const y2024::Constants *robot_constants_;
James Kuszmaul42a87452024-04-05 17:30:47 -0700198
199 int climbed_count_ = 0;
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800200};
201
Stephan Pleinesf63bde82024-01-13 15:59:33 -0800202} // namespace y2024::input::joysticks
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800203
204int main(int argc, char **argv) {
205 ::aos::InitGoogle(&argc, &argv);
206
207 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
208 aos::configuration::ReadConfig("aos_config.json");
James Kuszmaul2549e752024-01-20 17:42:51 -0800209 frc971::constants::WaitForConstants<y2024::Constants>(&config.message());
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800210
Filip Kujawaa7c8b412024-02-24 18:29:29 -0800211 ::aos::ShmEventLoop constant_fetcher_event_loop(&config.message());
212 frc971::constants::ConstantsFetcher<y2024::Constants> constants_fetcher(
213 &constant_fetcher_event_loop);
214 const y2024::Constants *robot_constants = &constants_fetcher.constants();
215
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800216 ::aos::ShmEventLoop event_loop(&config.message());
Filip Kujawaa7c8b412024-02-24 18:29:29 -0800217 ::y2024::input::joysticks::Reader reader(&event_loop, robot_constants);
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800218
219 event_loop.Run();
220
221 return 0;
222}