blob: 4b0bec9b89c694222d847ea40956c59b225b3587 [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
Austin Schuh99f7c6a2024-06-25 22:07:44 -07007#include "absl/flags/flag.h"
8
Niko Sohmers3860f8a2024-01-12 21:05:19 -08009#include "aos/actions/actions.h"
10#include "aos/init.h"
11#include "aos/logging/logging.h"
12#include "aos/network/team_number.h"
13#include "aos/util/log_interval.h"
14#include "frc971/autonomous/base_autonomous_actor.h"
James Kuszmaul2549e752024-01-20 17:42:51 -080015#include "frc971/constants/constants_sender_lib.h"
Niko Sohmers3860f8a2024-01-12 21:05:19 -080016#include "frc971/control_loops/drivetrain/localizer_generated.h"
17#include "frc971/control_loops/profiled_subsystem_generated.h"
Filip Kujawaa7c8b412024-02-24 18:29:29 -080018#include "frc971/control_loops/static_zeroing_single_dof_profiled_subsystem.h"
Niko Sohmers3860f8a2024-01-12 21:05:19 -080019#include "frc971/input/action_joystick_input.h"
20#include "frc971/input/driver_station_data.h"
21#include "frc971/input/drivetrain_input.h"
22#include "frc971/input/joystick_input.h"
23#include "frc971/input/redundant_joystick_data.h"
24#include "frc971/zeroing/wrap.h"
James Kuszmaul2549e752024-01-20 17:42:51 -080025#include "y2024/constants/constants_generated.h"
Niko Sohmers3860f8a2024-01-12 21:05:19 -080026#include "y2024/control_loops/drivetrain/drivetrain_base.h"
Filip Kujawaa7c8b412024-02-24 18:29:29 -080027#include "y2024/control_loops/superstructure/superstructure_goal_static.h"
28#include "y2024/control_loops/superstructure/superstructure_status_static.h"
29
Niko Sohmers3860f8a2024-01-12 21:05:19 -080030using frc971::CreateProfileParameters;
Niko Sohmers3860f8a2024-01-12 21:05:19 -080031using frc971::input::driver_station::ButtonLocation;
32using frc971::input::driver_station::ControlBit;
33using frc971::input::driver_station::JoystickAxis;
34using frc971::input::driver_station::POVLocation;
35using Side = frc971::control_loops::drivetrain::RobotSide;
36
Austin Schuh99f7c6a2024-06-25 22:07:44 -070037ABSL_FLAG(double, speaker_altitude_position_override, -1,
38 "If set, use this as the altitude angle for the fixed shot.");
39ABSL_FLAG(bool, allow_force_preload, false,
40 "If set, enable the kForcePreload button.");
James Kuszmaul0338ec32024-03-16 11:40:51 -070041
Stephan Pleinesf63bde82024-01-13 15:59:33 -080042namespace y2024::input::joysticks {
Niko Sohmers3860f8a2024-01-12 21:05:19 -080043
Filip Kujawaa7c8b412024-02-24 18:29:29 -080044namespace superstructure = y2024::control_loops::superstructure;
45
James Kuszmaul0281e152024-02-26 22:26:16 -080046// TODO(Xander): add button location from physical wiring
47// Note: Due to use_redundant_joysticks, the AOS_LOG statements
48// for the internal joystick code will give offset joystick numbering.
James Kuszmaul0338ec32024-03-16 11:40:51 -070049const ButtonLocation kIntake(2, 2);
50
51const ButtonLocation kSpitRollers(1, 13);
Maxwell Henderson45878502024-03-15 19:35:25 -070052const ButtonLocation kSpitExtend(1, 12);
James Kuszmaul0338ec32024-03-16 11:40:51 -070053const ButtonLocation kIntakeRollers(2, 5);
54
55const ButtonLocation kCatapultLoad(2, 1);
56const ButtonLocation kAmp(2, 4);
57const ButtonLocation kFire(2, 8);
James Kuszmaule3b45692024-04-07 19:56:14 -070058const ButtonLocation kForceLoad(2, 9);
Austin Schuhcb703442024-03-23 14:30:05 -070059const ButtonLocation kDriverFire(1, 1);
James Kuszmaul0338ec32024-03-16 11:40:51 -070060const ButtonLocation kTrap(2, 6);
61const ButtonLocation kAutoAim(1, 8);
62const ButtonLocation kAimSpeaker(2, 11);
Filip Kujawaa7c8b412024-02-24 18:29:29 -080063const ButtonLocation kAimPodium(0, 0);
64const ButtonLocation kShoot(0, 0);
James Kuszmaul0281e152024-02-26 22:26:16 -080065const ButtonLocation kRaiseClimber(3, 2);
Niko Sohmers4d93d4c2024-03-24 14:48:26 -070066const ButtonLocation kRaiseFastClimber(3, 1);
Filip Kujawa1286c012024-03-31 22:53:27 -070067const ButtonLocation kAimShuttle(2, 10);
Austin Schuh79903e82024-04-07 23:12:00 -070068const ButtonLocation kTurretShuttle(1, 10);
Filip Kujawaa7c8b412024-02-24 18:29:29 -080069const ButtonLocation kExtraButtonTwo(0, 0);
70const ButtonLocation kExtraButtonThree(0, 0);
71const ButtonLocation kExtraButtonFour(0, 0);
72
Niko Sohmers3860f8a2024-01-12 21:05:19 -080073class Reader : public ::frc971::input::ActionJoystickInput {
74 public:
Filip Kujawaa7c8b412024-02-24 18:29:29 -080075 Reader(::aos::EventLoop *event_loop, const y2024::Constants *robot_constants)
Niko Sohmers3860f8a2024-01-12 21:05:19 -080076 : ::frc971::input::ActionJoystickInput(
77 event_loop,
James Kuszmaul2549e752024-01-20 17:42:51 -080078 ::y2024::control_loops::drivetrain::GetDrivetrainConfig(event_loop),
Niko Sohmers3860f8a2024-01-12 21:05:19 -080079 ::frc971::input::DrivetrainInputReader::InputType::kPistol,
80 {.use_redundant_joysticks = true}),
81 superstructure_goal_sender_(
Filip Kujawaa7c8b412024-02-24 18:29:29 -080082 event_loop->MakeSender<control_loops::superstructure::GoalStatic>(
Niko Sohmers3860f8a2024-01-12 21:05:19 -080083 "/superstructure")),
84 superstructure_status_fetcher_(
85 event_loop->MakeFetcher<control_loops::superstructure::Status>(
Filip Kujawaa7c8b412024-02-24 18:29:29 -080086 "/superstructure")),
Austin Schuh6bdcc372024-06-27 14:49:11 -070087 robot_constants_(robot_constants) {
88 CHECK(robot_constants_ != nullptr);
89 }
Niko Sohmers3860f8a2024-01-12 21:05:19 -080090
91 void AutoEnded() override { AOS_LOG(INFO, "Auto ended.\n"); }
92
93 void HandleTeleop(
94 const ::frc971::input::driver_station::Data &data) override {
Niko Sohmers3860f8a2024-01-12 21:05:19 -080095 superstructure_status_fetcher_.Fetch();
96 if (!superstructure_status_fetcher_.get()) {
97 AOS_LOG(ERROR, "Got no superstructure status message.\n");
98 return;
99 }
Filip Kujawaa7c8b412024-02-24 18:29:29 -0800100
101 aos::Sender<superstructure::GoalStatic>::StaticBuilder
102 superstructure_goal_builder =
103 superstructure_goal_sender_.MakeStaticBuilder();
104
James Kuszmaul42a87452024-04-05 17:30:47 -0700105 if (data.IsPressed(kIntake) || climbed_count_ > 25) {
Filip Kujawaa7c8b412024-02-24 18:29:29 -0800106 // Intake is pressed
James Kuszmaul0338ec32024-03-16 11:40:51 -0700107 superstructure_goal_builder->set_intake_pivot(
108 superstructure::IntakePivotGoal::DOWN);
109 } else {
110 superstructure_goal_builder->set_intake_pivot(
111 superstructure::IntakePivotGoal::UP);
112 }
113
114 if (data.IsPressed(kIntakeRollers)) {
115 // Intake is pressed
Filip Kujawaa7c8b412024-02-24 18:29:29 -0800116 superstructure_goal_builder->set_intake_goal(
117 superstructure::IntakeGoal::INTAKE);
James Kuszmaul0338ec32024-03-16 11:40:51 -0700118 } else if (data.IsPressed(kSpitRollers)) {
119 superstructure_goal_builder->set_intake_goal(
120 superstructure::IntakeGoal::SPIT);
James Kuszmaul0281e152024-02-26 22:26:16 -0800121 } else {
Filip Kujawaa7c8b412024-02-24 18:29:29 -0800122 superstructure_goal_builder->set_intake_goal(
James Kuszmaul0281e152024-02-26 22:26:16 -0800123 superstructure::IntakeGoal::NONE);
Filip Kujawaa7c8b412024-02-24 18:29:29 -0800124 }
James Kuszmaul0338ec32024-03-16 11:40:51 -0700125
Maxwell Henderson45878502024-03-15 19:35:25 -0700126 if (data.IsPressed(kSpitExtend)) {
127 superstructure_goal_builder->set_spit_extend(true);
128 } else {
129 superstructure_goal_builder->set_spit_extend(false);
130 }
131
James Kuszmaul0281e152024-02-26 22:26:16 -0800132 if (data.IsPressed(kAmp)) {
Filip Kujawaa7c8b412024-02-24 18:29:29 -0800133 superstructure_goal_builder->set_note_goal(superstructure::NoteGoal::AMP);
134 } else if (data.IsPressed(kTrap)) {
135 superstructure_goal_builder->set_note_goal(
136 superstructure::NoteGoal::TRAP);
James Kuszmaul0281e152024-02-26 22:26:16 -0800137 } else if (data.IsPressed(kCatapultLoad)) {
138 superstructure_goal_builder->set_note_goal(
139 superstructure::NoteGoal::CATAPULT);
140 } else {
141 superstructure_goal_builder->set_note_goal(
142 superstructure::NoteGoal::NONE);
Filip Kujawaa7c8b412024-02-24 18:29:29 -0800143 }
Filip Kujawaa7c8b412024-02-24 18:29:29 -0800144 auto shooter_goal = superstructure_goal_builder->add_shooter_goal();
Austin Schuh99f7c6a2024-06-25 22:07:44 -0700145 shooter_goal->set_preloaded(absl::GetFlag(FLAGS_allow_force_preload) &&
James Kuszmaule3b45692024-04-07 19:56:14 -0700146 data.IsPressed(kForceLoad));
Filip Kujawa1286c012024-03-31 22:53:27 -0700147 if (data.IsPressed(kAutoAim)) {
148 shooter_goal->set_auto_aim(
149 control_loops::superstructure::AutoAimMode::SPEAKER);
150 } else if (data.IsPressed(kAimShuttle)) {
151 shooter_goal->set_auto_aim(
152 control_loops::superstructure::AutoAimMode::SHUTTLE);
Austin Schuh79903e82024-04-07 23:12:00 -0700153 } else if (data.IsPressed(kTurretShuttle)) {
154 shooter_goal->set_auto_aim(
155 control_loops::superstructure::AutoAimMode::TURRET_SHUTTLE);
Filip Kujawa1286c012024-03-31 22:53:27 -0700156 }
Filip Kujawaa7c8b412024-02-24 18:29:29 -0800157
158 // Updating aiming for shooter goal, only one type of aim should be possible
159 // at a time, auto-aiming is preferred over the setpoints.
James Kuszmaul0281e152024-02-26 22:26:16 -0800160 if (data.IsPressed(kAimSpeaker)) {
Filip Kujawaa7c8b412024-02-24 18:29:29 -0800161 auto catapult_goal = shooter_goal->add_catapult_goal();
162 catapult_goal->set_shot_velocity(robot_constants_->common()
163 ->shooter_speaker_set_point()
164 ->shot_velocity());
Austin Schuh99f7c6a2024-06-25 22:07:44 -0700165 if (absl::GetFlag(FLAGS_speaker_altitude_position_override) > 0) {
James Kuszmaul0338ec32024-03-16 11:40:51 -0700166 PopulateStaticZeroingSingleDOFProfiledSubsystemGoal(
167 shooter_goal->add_altitude_position(),
Austin Schuh99f7c6a2024-06-25 22:07:44 -0700168 absl::GetFlag(FLAGS_speaker_altitude_position_override));
James Kuszmaul0338ec32024-03-16 11:40:51 -0700169 } else {
170 PopulateStaticZeroingSingleDOFProfiledSubsystemGoal(
171 shooter_goal->add_altitude_position(),
172 robot_constants_->common()
173 ->shooter_speaker_set_point()
174 ->altitude_position());
175 }
Filip Kujawaa7c8b412024-02-24 18:29:29 -0800176 PopulateStaticZeroingSingleDOFProfiledSubsystemGoal(
177 shooter_goal->add_turret_position(), robot_constants_->common()
178 ->shooter_speaker_set_point()
179 ->turret_position());
Filip Kujawaa7c8b412024-02-24 18:29:29 -0800180 }
Austin Schuhcb703442024-03-23 14:30:05 -0700181 superstructure_goal_builder->set_fire(data.IsPressed(kFire) ||
182 data.IsPressed(kDriverFire));
Filip Kujawaa7c8b412024-02-24 18:29:29 -0800183
Niko Sohmers4d93d4c2024-03-24 14:48:26 -0700184 if (data.IsPressed(kRaiseClimber)) {
James Kuszmaul42a87452024-04-05 17:30:47 -0700185 ++climbed_count_;
Niko Sohmers4d93d4c2024-03-24 14:48:26 -0700186 superstructure_goal_builder->set_climber_goal_voltage(4.0);
187 } else if (data.IsPressed(kRaiseFastClimber)) {
James Kuszmaul42a87452024-04-05 17:30:47 -0700188 ++climbed_count_;
189 superstructure_goal_builder->set_climber_goal_voltage(12.0);
190 }
191
192 if (climbed_count_ > 25) {
193 PopulateStaticZeroingSingleDOFProfiledSubsystemGoal(
194 shooter_goal->add_turret_position(), 0.0);
James Kuszmaul0338ec32024-03-16 11:40:51 -0700195 }
196
Filip Kujawaa7c8b412024-02-24 18:29:29 -0800197 superstructure_goal_builder.CheckOk(superstructure_goal_builder.Send());
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800198 }
199
200 private:
Filip Kujawaa7c8b412024-02-24 18:29:29 -0800201 ::aos::Sender<control_loops::superstructure::GoalStatic>
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800202 superstructure_goal_sender_;
203 ::aos::Fetcher<control_loops::superstructure::Status>
204 superstructure_status_fetcher_;
Filip Kujawaa7c8b412024-02-24 18:29:29 -0800205 const y2024::Constants *robot_constants_;
James Kuszmaul42a87452024-04-05 17:30:47 -0700206
207 int climbed_count_ = 0;
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800208};
209
Stephan Pleinesf63bde82024-01-13 15:59:33 -0800210} // namespace y2024::input::joysticks
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800211
212int main(int argc, char **argv) {
213 ::aos::InitGoogle(&argc, &argv);
214
215 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
216 aos::configuration::ReadConfig("aos_config.json");
James Kuszmaul2549e752024-01-20 17:42:51 -0800217 frc971::constants::WaitForConstants<y2024::Constants>(&config.message());
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800218
Filip Kujawaa7c8b412024-02-24 18:29:29 -0800219 ::aos::ShmEventLoop constant_fetcher_event_loop(&config.message());
220 frc971::constants::ConstantsFetcher<y2024::Constants> constants_fetcher(
221 &constant_fetcher_event_loop);
222 const y2024::Constants *robot_constants = &constants_fetcher.constants();
223
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800224 ::aos::ShmEventLoop event_loop(&config.message());
Filip Kujawaa7c8b412024-02-24 18:29:29 -0800225 ::y2024::input::joysticks::Reader reader(&event_loop, robot_constants);
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800226
227 event_loop.Run();
228
229 return 0;
230}