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