Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 1 | #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 Kuszmaul | 2549e75 | 2024-01-20 17:42:51 -0800 | [diff] [blame] | 13 | #include "frc971/constants/constants_sender_lib.h" |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 14 | #include "frc971/control_loops/drivetrain/localizer_generated.h" |
| 15 | #include "frc971/control_loops/profiled_subsystem_generated.h" |
Filip Kujawa | a7c8b41 | 2024-02-24 18:29:29 -0800 | [diff] [blame] | 16 | #include "frc971/control_loops/static_zeroing_single_dof_profiled_subsystem.h" |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 17 | #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 Kuszmaul | 2549e75 | 2024-01-20 17:42:51 -0800 | [diff] [blame] | 23 | #include "y2024/constants/constants_generated.h" |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 24 | #include "y2024/control_loops/drivetrain/drivetrain_base.h" |
Filip Kujawa | a7c8b41 | 2024-02-24 18:29:29 -0800 | [diff] [blame] | 25 | #include "y2024/control_loops/superstructure/superstructure_goal_static.h" |
| 26 | #include "y2024/control_loops/superstructure/superstructure_status_static.h" |
| 27 | |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 28 | using frc971::CreateProfileParameters; |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 29 | using frc971::input::driver_station::ButtonLocation; |
| 30 | using frc971::input::driver_station::ControlBit; |
| 31 | using frc971::input::driver_station::JoystickAxis; |
| 32 | using frc971::input::driver_station::POVLocation; |
| 33 | using Side = frc971::control_loops::drivetrain::RobotSide; |
| 34 | |
James Kuszmaul | 0338ec3 | 2024-03-16 11:40:51 -0700 | [diff] [blame] | 35 | DEFINE_double(speaker_altitude_position_override, -1, |
| 36 | "If set, use this as the altitude angle for the fixed shot."); |
James Kuszmaul | e3b4569 | 2024-04-07 19:56:14 -0700 | [diff] [blame^] | 37 | DEFINE_bool(allow_force_preload, false, |
| 38 | "If set, enable the kForcePreload button."); |
James Kuszmaul | 0338ec3 | 2024-03-16 11:40:51 -0700 | [diff] [blame] | 39 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 40 | namespace y2024::input::joysticks { |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 41 | |
Filip Kujawa | a7c8b41 | 2024-02-24 18:29:29 -0800 | [diff] [blame] | 42 | namespace superstructure = y2024::control_loops::superstructure; |
| 43 | |
James Kuszmaul | 0281e15 | 2024-02-26 22:26:16 -0800 | [diff] [blame] | 44 | // 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 Kuszmaul | 0338ec3 | 2024-03-16 11:40:51 -0700 | [diff] [blame] | 47 | const ButtonLocation kIntake(2, 2); |
| 48 | |
| 49 | const ButtonLocation kSpitRollers(1, 13); |
Maxwell Henderson | 4587850 | 2024-03-15 19:35:25 -0700 | [diff] [blame] | 50 | const ButtonLocation kSpitExtend(1, 12); |
James Kuszmaul | 0338ec3 | 2024-03-16 11:40:51 -0700 | [diff] [blame] | 51 | const ButtonLocation kIntakeRollers(2, 5); |
| 52 | |
| 53 | const ButtonLocation kCatapultLoad(2, 1); |
| 54 | const ButtonLocation kAmp(2, 4); |
| 55 | const ButtonLocation kFire(2, 8); |
James Kuszmaul | e3b4569 | 2024-04-07 19:56:14 -0700 | [diff] [blame^] | 56 | const ButtonLocation kForceLoad(2, 9); |
Austin Schuh | cb70344 | 2024-03-23 14:30:05 -0700 | [diff] [blame] | 57 | const ButtonLocation kDriverFire(1, 1); |
James Kuszmaul | 0338ec3 | 2024-03-16 11:40:51 -0700 | [diff] [blame] | 58 | const ButtonLocation kTrap(2, 6); |
| 59 | const ButtonLocation kAutoAim(1, 8); |
| 60 | const ButtonLocation kAimSpeaker(2, 11); |
Filip Kujawa | a7c8b41 | 2024-02-24 18:29:29 -0800 | [diff] [blame] | 61 | const ButtonLocation kAimPodium(0, 0); |
| 62 | const ButtonLocation kShoot(0, 0); |
James Kuszmaul | 0281e15 | 2024-02-26 22:26:16 -0800 | [diff] [blame] | 63 | const ButtonLocation kRaiseClimber(3, 2); |
Niko Sohmers | 4d93d4c | 2024-03-24 14:48:26 -0700 | [diff] [blame] | 64 | const ButtonLocation kRaiseFastClimber(3, 1); |
Filip Kujawa | 1286c01 | 2024-03-31 22:53:27 -0700 | [diff] [blame] | 65 | const ButtonLocation kAimShuttle(2, 10); |
Filip Kujawa | a7c8b41 | 2024-02-24 18:29:29 -0800 | [diff] [blame] | 66 | const ButtonLocation kExtraButtonTwo(0, 0); |
| 67 | const ButtonLocation kExtraButtonThree(0, 0); |
| 68 | const ButtonLocation kExtraButtonFour(0, 0); |
| 69 | |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 70 | class Reader : public ::frc971::input::ActionJoystickInput { |
| 71 | public: |
Filip Kujawa | a7c8b41 | 2024-02-24 18:29:29 -0800 | [diff] [blame] | 72 | Reader(::aos::EventLoop *event_loop, const y2024::Constants *robot_constants) |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 73 | : ::frc971::input::ActionJoystickInput( |
| 74 | event_loop, |
James Kuszmaul | 2549e75 | 2024-01-20 17:42:51 -0800 | [diff] [blame] | 75 | ::y2024::control_loops::drivetrain::GetDrivetrainConfig(event_loop), |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 76 | ::frc971::input::DrivetrainInputReader::InputType::kPistol, |
| 77 | {.use_redundant_joysticks = true}), |
| 78 | superstructure_goal_sender_( |
Filip Kujawa | a7c8b41 | 2024-02-24 18:29:29 -0800 | [diff] [blame] | 79 | event_loop->MakeSender<control_loops::superstructure::GoalStatic>( |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 80 | "/superstructure")), |
| 81 | superstructure_status_fetcher_( |
| 82 | event_loop->MakeFetcher<control_loops::superstructure::Status>( |
Filip Kujawa | a7c8b41 | 2024-02-24 18:29:29 -0800 | [diff] [blame] | 83 | "/superstructure")), |
| 84 | robot_constants_(CHECK_NOTNULL(robot_constants)) {} |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 85 | |
| 86 | void AutoEnded() override { AOS_LOG(INFO, "Auto ended.\n"); } |
| 87 | |
| 88 | void HandleTeleop( |
| 89 | const ::frc971::input::driver_station::Data &data) override { |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 90 | superstructure_status_fetcher_.Fetch(); |
| 91 | if (!superstructure_status_fetcher_.get()) { |
| 92 | AOS_LOG(ERROR, "Got no superstructure status message.\n"); |
| 93 | return; |
| 94 | } |
Filip Kujawa | a7c8b41 | 2024-02-24 18:29:29 -0800 | [diff] [blame] | 95 | |
| 96 | aos::Sender<superstructure::GoalStatic>::StaticBuilder |
| 97 | superstructure_goal_builder = |
| 98 | superstructure_goal_sender_.MakeStaticBuilder(); |
| 99 | |
James Kuszmaul | 42a8745 | 2024-04-05 17:30:47 -0700 | [diff] [blame] | 100 | if (data.IsPressed(kIntake) || climbed_count_ > 25) { |
Filip Kujawa | a7c8b41 | 2024-02-24 18:29:29 -0800 | [diff] [blame] | 101 | // Intake is pressed |
James Kuszmaul | 0338ec3 | 2024-03-16 11:40:51 -0700 | [diff] [blame] | 102 | 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 Kujawa | a7c8b41 | 2024-02-24 18:29:29 -0800 | [diff] [blame] | 111 | superstructure_goal_builder->set_intake_goal( |
| 112 | superstructure::IntakeGoal::INTAKE); |
James Kuszmaul | 0338ec3 | 2024-03-16 11:40:51 -0700 | [diff] [blame] | 113 | } else if (data.IsPressed(kSpitRollers)) { |
| 114 | superstructure_goal_builder->set_intake_goal( |
| 115 | superstructure::IntakeGoal::SPIT); |
James Kuszmaul | 0281e15 | 2024-02-26 22:26:16 -0800 | [diff] [blame] | 116 | } else { |
Filip Kujawa | a7c8b41 | 2024-02-24 18:29:29 -0800 | [diff] [blame] | 117 | superstructure_goal_builder->set_intake_goal( |
James Kuszmaul | 0281e15 | 2024-02-26 22:26:16 -0800 | [diff] [blame] | 118 | superstructure::IntakeGoal::NONE); |
Filip Kujawa | a7c8b41 | 2024-02-24 18:29:29 -0800 | [diff] [blame] | 119 | } |
James Kuszmaul | 0338ec3 | 2024-03-16 11:40:51 -0700 | [diff] [blame] | 120 | |
Maxwell Henderson | 4587850 | 2024-03-15 19:35:25 -0700 | [diff] [blame] | 121 | 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 Kuszmaul | 0281e15 | 2024-02-26 22:26:16 -0800 | [diff] [blame] | 127 | if (data.IsPressed(kAmp)) { |
Filip Kujawa | a7c8b41 | 2024-02-24 18:29:29 -0800 | [diff] [blame] | 128 | 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 Kuszmaul | 0281e15 | 2024-02-26 22:26:16 -0800 | [diff] [blame] | 132 | } 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 Kujawa | a7c8b41 | 2024-02-24 18:29:29 -0800 | [diff] [blame] | 138 | } |
Filip Kujawa | a7c8b41 | 2024-02-24 18:29:29 -0800 | [diff] [blame] | 139 | auto shooter_goal = superstructure_goal_builder->add_shooter_goal(); |
James Kuszmaul | e3b4569 | 2024-04-07 19:56:14 -0700 | [diff] [blame^] | 140 | shooter_goal->set_preloaded(FLAGS_allow_force_preload && |
| 141 | data.IsPressed(kForceLoad)); |
Filip Kujawa | 1286c01 | 2024-03-31 22:53:27 -0700 | [diff] [blame] | 142 | 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 Kujawa | a7c8b41 | 2024-02-24 18:29:29 -0800 | [diff] [blame] | 149 | |
| 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 Kuszmaul | 0281e15 | 2024-02-26 22:26:16 -0800 | [diff] [blame] | 152 | if (data.IsPressed(kAimSpeaker)) { |
Filip Kujawa | a7c8b41 | 2024-02-24 18:29:29 -0800 | [diff] [blame] | 153 | 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 Kuszmaul | 0338ec3 | 2024-03-16 11:40:51 -0700 | [diff] [blame] | 157 | 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 Kujawa | a7c8b41 | 2024-02-24 18:29:29 -0800 | [diff] [blame] | 168 | PopulateStaticZeroingSingleDOFProfiledSubsystemGoal( |
| 169 | shooter_goal->add_turret_position(), robot_constants_->common() |
| 170 | ->shooter_speaker_set_point() |
| 171 | ->turret_position()); |
Filip Kujawa | a7c8b41 | 2024-02-24 18:29:29 -0800 | [diff] [blame] | 172 | } |
Austin Schuh | cb70344 | 2024-03-23 14:30:05 -0700 | [diff] [blame] | 173 | superstructure_goal_builder->set_fire(data.IsPressed(kFire) || |
| 174 | data.IsPressed(kDriverFire)); |
Filip Kujawa | a7c8b41 | 2024-02-24 18:29:29 -0800 | [diff] [blame] | 175 | |
Niko Sohmers | 4d93d4c | 2024-03-24 14:48:26 -0700 | [diff] [blame] | 176 | if (data.IsPressed(kRaiseClimber)) { |
James Kuszmaul | 42a8745 | 2024-04-05 17:30:47 -0700 | [diff] [blame] | 177 | ++climbed_count_; |
Niko Sohmers | 4d93d4c | 2024-03-24 14:48:26 -0700 | [diff] [blame] | 178 | superstructure_goal_builder->set_climber_goal_voltage(4.0); |
| 179 | } else if (data.IsPressed(kRaiseFastClimber)) { |
James Kuszmaul | 42a8745 | 2024-04-05 17:30:47 -0700 | [diff] [blame] | 180 | ++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 Kuszmaul | 0338ec3 | 2024-03-16 11:40:51 -0700 | [diff] [blame] | 187 | } |
| 188 | |
Filip Kujawa | a7c8b41 | 2024-02-24 18:29:29 -0800 | [diff] [blame] | 189 | superstructure_goal_builder.CheckOk(superstructure_goal_builder.Send()); |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | private: |
Filip Kujawa | a7c8b41 | 2024-02-24 18:29:29 -0800 | [diff] [blame] | 193 | ::aos::Sender<control_loops::superstructure::GoalStatic> |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 194 | superstructure_goal_sender_; |
| 195 | ::aos::Fetcher<control_loops::superstructure::Status> |
| 196 | superstructure_status_fetcher_; |
Filip Kujawa | a7c8b41 | 2024-02-24 18:29:29 -0800 | [diff] [blame] | 197 | const y2024::Constants *robot_constants_; |
James Kuszmaul | 42a8745 | 2024-04-05 17:30:47 -0700 | [diff] [blame] | 198 | |
| 199 | int climbed_count_ = 0; |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 200 | }; |
| 201 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 202 | } // namespace y2024::input::joysticks |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 203 | |
| 204 | int 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 Kuszmaul | 2549e75 | 2024-01-20 17:42:51 -0800 | [diff] [blame] | 209 | frc971::constants::WaitForConstants<y2024::Constants>(&config.message()); |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 210 | |
Filip Kujawa | a7c8b41 | 2024-02-24 18:29:29 -0800 | [diff] [blame] | 211 | ::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 Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 216 | ::aos::ShmEventLoop event_loop(&config.message()); |
Filip Kujawa | a7c8b41 | 2024-02-24 18:29:29 -0800 | [diff] [blame] | 217 | ::y2024::input::joysticks::Reader reader(&event_loop, robot_constants); |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 218 | |
| 219 | event_loop.Run(); |
| 220 | |
| 221 | return 0; |
| 222 | } |