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