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 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 35 | namespace y2024::input::joysticks { |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 36 | |
Filip Kujawa | a7c8b41 | 2024-02-24 18:29:29 -0800 | [diff] [blame] | 37 | namespace superstructure = y2024::control_loops::superstructure; |
| 38 | |
James Kuszmaul | 0281e15 | 2024-02-26 22:26:16 -0800 | [diff] [blame^] | 39 | // TODO(Xander): add button location from physical wiring |
| 40 | // Note: Due to use_redundant_joysticks, the AOS_LOG statements |
| 41 | // for the internal joystick code will give offset joystick numbering. |
| 42 | const ButtonLocation kIntake(2, 8); |
Filip Kujawa | a7c8b41 | 2024-02-24 18:29:29 -0800 | [diff] [blame] | 43 | const ButtonLocation kSpit(0, 0); |
James Kuszmaul | 0281e15 | 2024-02-26 22:26:16 -0800 | [diff] [blame^] | 44 | const ButtonLocation kCatapultLoad(1, 7); |
| 45 | const ButtonLocation kAmp(2, 7); |
| 46 | const ButtonLocation kFire(2, 6); |
| 47 | const ButtonLocation kTrap(2, 5); |
Filip Kujawa | a7c8b41 | 2024-02-24 18:29:29 -0800 | [diff] [blame] | 48 | const ButtonLocation kAutoAim(0, 0); |
James Kuszmaul | 0281e15 | 2024-02-26 22:26:16 -0800 | [diff] [blame^] | 49 | const ButtonLocation kAimSpeaker(1, 6); |
Filip Kujawa | a7c8b41 | 2024-02-24 18:29:29 -0800 | [diff] [blame] | 50 | const ButtonLocation kAimPodium(0, 0); |
| 51 | const ButtonLocation kShoot(0, 0); |
James Kuszmaul | 0281e15 | 2024-02-26 22:26:16 -0800 | [diff] [blame^] | 52 | const ButtonLocation kRaiseClimber(3, 2); |
| 53 | const ButtonLocation kRetractClimber(2, 4); |
Filip Kujawa | a7c8b41 | 2024-02-24 18:29:29 -0800 | [diff] [blame] | 54 | const ButtonLocation kExtraButtonOne(0, 0); |
| 55 | const ButtonLocation kExtraButtonTwo(0, 0); |
| 56 | const ButtonLocation kExtraButtonThree(0, 0); |
| 57 | const ButtonLocation kExtraButtonFour(0, 0); |
| 58 | |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 59 | class Reader : public ::frc971::input::ActionJoystickInput { |
| 60 | public: |
Filip Kujawa | a7c8b41 | 2024-02-24 18:29:29 -0800 | [diff] [blame] | 61 | Reader(::aos::EventLoop *event_loop, const y2024::Constants *robot_constants) |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 62 | : ::frc971::input::ActionJoystickInput( |
| 63 | event_loop, |
James Kuszmaul | 2549e75 | 2024-01-20 17:42:51 -0800 | [diff] [blame] | 64 | ::y2024::control_loops::drivetrain::GetDrivetrainConfig(event_loop), |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 65 | ::frc971::input::DrivetrainInputReader::InputType::kPistol, |
| 66 | {.use_redundant_joysticks = true}), |
| 67 | superstructure_goal_sender_( |
Filip Kujawa | a7c8b41 | 2024-02-24 18:29:29 -0800 | [diff] [blame] | 68 | event_loop->MakeSender<control_loops::superstructure::GoalStatic>( |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 69 | "/superstructure")), |
| 70 | superstructure_status_fetcher_( |
| 71 | event_loop->MakeFetcher<control_loops::superstructure::Status>( |
Filip Kujawa | a7c8b41 | 2024-02-24 18:29:29 -0800 | [diff] [blame] | 72 | "/superstructure")), |
| 73 | robot_constants_(CHECK_NOTNULL(robot_constants)) {} |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 74 | |
| 75 | void AutoEnded() override { AOS_LOG(INFO, "Auto ended.\n"); } |
| 76 | |
| 77 | void HandleTeleop( |
| 78 | const ::frc971::input::driver_station::Data &data) override { |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 79 | superstructure_status_fetcher_.Fetch(); |
| 80 | if (!superstructure_status_fetcher_.get()) { |
| 81 | AOS_LOG(ERROR, "Got no superstructure status message.\n"); |
| 82 | return; |
| 83 | } |
Filip Kujawa | a7c8b41 | 2024-02-24 18:29:29 -0800 | [diff] [blame] | 84 | |
| 85 | aos::Sender<superstructure::GoalStatic>::StaticBuilder |
| 86 | superstructure_goal_builder = |
| 87 | superstructure_goal_sender_.MakeStaticBuilder(); |
| 88 | |
| 89 | if (data.IsPressed(kIntake)) { |
| 90 | // Intake is pressed |
| 91 | superstructure_goal_builder->set_intake_goal( |
| 92 | superstructure::IntakeGoal::INTAKE); |
James Kuszmaul | 0281e15 | 2024-02-26 22:26:16 -0800 | [diff] [blame^] | 93 | } else { |
Filip Kujawa | a7c8b41 | 2024-02-24 18:29:29 -0800 | [diff] [blame] | 94 | superstructure_goal_builder->set_intake_goal( |
James Kuszmaul | 0281e15 | 2024-02-26 22:26:16 -0800 | [diff] [blame^] | 95 | superstructure::IntakeGoal::NONE); |
Filip Kujawa | a7c8b41 | 2024-02-24 18:29:29 -0800 | [diff] [blame] | 96 | } |
James Kuszmaul | 0281e15 | 2024-02-26 22:26:16 -0800 | [diff] [blame^] | 97 | if (data.IsPressed(kAmp)) { |
Filip Kujawa | a7c8b41 | 2024-02-24 18:29:29 -0800 | [diff] [blame] | 98 | superstructure_goal_builder->set_note_goal(superstructure::NoteGoal::AMP); |
| 99 | } else if (data.IsPressed(kTrap)) { |
| 100 | superstructure_goal_builder->set_note_goal( |
| 101 | superstructure::NoteGoal::TRAP); |
James Kuszmaul | 0281e15 | 2024-02-26 22:26:16 -0800 | [diff] [blame^] | 102 | } else if (data.IsPressed(kCatapultLoad)) { |
| 103 | superstructure_goal_builder->set_note_goal( |
| 104 | superstructure::NoteGoal::CATAPULT); |
| 105 | } else { |
| 106 | superstructure_goal_builder->set_note_goal( |
| 107 | superstructure::NoteGoal::NONE); |
Filip Kujawa | a7c8b41 | 2024-02-24 18:29:29 -0800 | [diff] [blame] | 108 | } |
Filip Kujawa | a7c8b41 | 2024-02-24 18:29:29 -0800 | [diff] [blame] | 109 | auto shooter_goal = superstructure_goal_builder->add_shooter_goal(); |
James Kuszmaul | 0281e15 | 2024-02-26 22:26:16 -0800 | [diff] [blame^] | 110 | shooter_goal->set_auto_aim(false); |
Filip Kujawa | a7c8b41 | 2024-02-24 18:29:29 -0800 | [diff] [blame] | 111 | |
| 112 | // Updating aiming for shooter goal, only one type of aim should be possible |
| 113 | // at a time, auto-aiming is preferred over the setpoints. |
James Kuszmaul | 0281e15 | 2024-02-26 22:26:16 -0800 | [diff] [blame^] | 114 | if (data.IsPressed(kAimSpeaker)) { |
Filip Kujawa | a7c8b41 | 2024-02-24 18:29:29 -0800 | [diff] [blame] | 115 | auto catapult_goal = shooter_goal->add_catapult_goal(); |
| 116 | catapult_goal->set_shot_velocity(robot_constants_->common() |
| 117 | ->shooter_speaker_set_point() |
| 118 | ->shot_velocity()); |
| 119 | PopulateStaticZeroingSingleDOFProfiledSubsystemGoal( |
| 120 | shooter_goal->add_altitude_position(), |
| 121 | robot_constants_->common() |
| 122 | ->shooter_speaker_set_point() |
| 123 | ->altitude_position()); |
| 124 | PopulateStaticZeroingSingleDOFProfiledSubsystemGoal( |
| 125 | shooter_goal->add_turret_position(), robot_constants_->common() |
| 126 | ->shooter_speaker_set_point() |
| 127 | ->turret_position()); |
Filip Kujawa | a7c8b41 | 2024-02-24 18:29:29 -0800 | [diff] [blame] | 128 | } |
James Kuszmaul | 0281e15 | 2024-02-26 22:26:16 -0800 | [diff] [blame^] | 129 | superstructure_goal_builder->set_fire(data.IsPressed(kFire)); |
Filip Kujawa | a7c8b41 | 2024-02-24 18:29:29 -0800 | [diff] [blame] | 130 | |
James Kuszmaul | 0281e15 | 2024-02-26 22:26:16 -0800 | [diff] [blame^] | 131 | if (data.IsPressed(kRaiseClimber)) { |
Filip Kujawa | a7c8b41 | 2024-02-24 18:29:29 -0800 | [diff] [blame] | 132 | superstructure_goal_builder->set_climber_goal( |
| 133 | superstructure::ClimberGoal::FULL_EXTEND); |
James Kuszmaul | 0281e15 | 2024-02-26 22:26:16 -0800 | [diff] [blame^] | 134 | } else if (data.IsPressed(kRetractClimber)) { |
Filip Kujawa | a7c8b41 | 2024-02-24 18:29:29 -0800 | [diff] [blame] | 135 | superstructure_goal_builder->set_climber_goal( |
| 136 | superstructure::ClimberGoal::RETRACT); |
James Kuszmaul | 0281e15 | 2024-02-26 22:26:16 -0800 | [diff] [blame^] | 137 | } else { |
| 138 | superstructure_goal_builder->set_climber_goal( |
| 139 | superstructure::ClimberGoal::STOWED); |
Filip Kujawa | a7c8b41 | 2024-02-24 18:29:29 -0800 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | superstructure_goal_builder.CheckOk(superstructure_goal_builder.Send()); |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | private: |
Filip Kujawa | a7c8b41 | 2024-02-24 18:29:29 -0800 | [diff] [blame] | 146 | ::aos::Sender<control_loops::superstructure::GoalStatic> |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 147 | superstructure_goal_sender_; |
| 148 | ::aos::Fetcher<control_loops::superstructure::Status> |
| 149 | superstructure_status_fetcher_; |
Filip Kujawa | a7c8b41 | 2024-02-24 18:29:29 -0800 | [diff] [blame] | 150 | const y2024::Constants *robot_constants_; |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 151 | }; |
| 152 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 153 | } // namespace y2024::input::joysticks |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 154 | |
| 155 | int main(int argc, char **argv) { |
| 156 | ::aos::InitGoogle(&argc, &argv); |
| 157 | |
| 158 | aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
| 159 | aos::configuration::ReadConfig("aos_config.json"); |
James Kuszmaul | 2549e75 | 2024-01-20 17:42:51 -0800 | [diff] [blame] | 160 | frc971::constants::WaitForConstants<y2024::Constants>(&config.message()); |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 161 | |
Filip Kujawa | a7c8b41 | 2024-02-24 18:29:29 -0800 | [diff] [blame] | 162 | ::aos::ShmEventLoop constant_fetcher_event_loop(&config.message()); |
| 163 | frc971::constants::ConstantsFetcher<y2024::Constants> constants_fetcher( |
| 164 | &constant_fetcher_event_loop); |
| 165 | const y2024::Constants *robot_constants = &constants_fetcher.constants(); |
| 166 | |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 167 | ::aos::ShmEventLoop event_loop(&config.message()); |
Filip Kujawa | a7c8b41 | 2024-02-24 18:29:29 -0800 | [diff] [blame] | 168 | ::y2024::input::joysticks::Reader reader(&event_loop, robot_constants); |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 169 | |
| 170 | event_loop.Run(); |
| 171 | |
| 172 | return 0; |
| 173 | } |