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); |
Austin Schuh | 79903e8 | 2024-04-07 23:12:00 -0700 | [diff] [blame] | 66 | const ButtonLocation kTurretShuttle(1, 10); |
Filip Kujawa | a7c8b41 | 2024-02-24 18:29:29 -0800 | [diff] [blame] | 67 | const ButtonLocation kExtraButtonTwo(0, 0); |
| 68 | const ButtonLocation kExtraButtonThree(0, 0); |
| 69 | const ButtonLocation kExtraButtonFour(0, 0); |
| 70 | |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 71 | class Reader : public ::frc971::input::ActionJoystickInput { |
| 72 | public: |
Filip Kujawa | a7c8b41 | 2024-02-24 18:29:29 -0800 | [diff] [blame] | 73 | Reader(::aos::EventLoop *event_loop, const y2024::Constants *robot_constants) |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 74 | : ::frc971::input::ActionJoystickInput( |
| 75 | event_loop, |
James Kuszmaul | 2549e75 | 2024-01-20 17:42:51 -0800 | [diff] [blame] | 76 | ::y2024::control_loops::drivetrain::GetDrivetrainConfig(event_loop), |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 77 | ::frc971::input::DrivetrainInputReader::InputType::kPistol, |
| 78 | {.use_redundant_joysticks = true}), |
| 79 | superstructure_goal_sender_( |
Filip Kujawa | a7c8b41 | 2024-02-24 18:29:29 -0800 | [diff] [blame] | 80 | event_loop->MakeSender<control_loops::superstructure::GoalStatic>( |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 81 | "/superstructure")), |
| 82 | superstructure_status_fetcher_( |
| 83 | event_loop->MakeFetcher<control_loops::superstructure::Status>( |
Filip Kujawa | a7c8b41 | 2024-02-24 18:29:29 -0800 | [diff] [blame] | 84 | "/superstructure")), |
Austin Schuh | 6bdcc37 | 2024-06-27 14:49:11 -0700 | [diff] [blame^] | 85 | robot_constants_(robot_constants) { |
| 86 | CHECK(robot_constants_ != nullptr); |
| 87 | } |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 88 | |
| 89 | void AutoEnded() override { AOS_LOG(INFO, "Auto ended.\n"); } |
| 90 | |
| 91 | void HandleTeleop( |
| 92 | const ::frc971::input::driver_station::Data &data) override { |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 93 | superstructure_status_fetcher_.Fetch(); |
| 94 | if (!superstructure_status_fetcher_.get()) { |
| 95 | AOS_LOG(ERROR, "Got no superstructure status message.\n"); |
| 96 | return; |
| 97 | } |
Filip Kujawa | a7c8b41 | 2024-02-24 18:29:29 -0800 | [diff] [blame] | 98 | |
| 99 | aos::Sender<superstructure::GoalStatic>::StaticBuilder |
| 100 | superstructure_goal_builder = |
| 101 | superstructure_goal_sender_.MakeStaticBuilder(); |
| 102 | |
James Kuszmaul | 42a8745 | 2024-04-05 17:30:47 -0700 | [diff] [blame] | 103 | if (data.IsPressed(kIntake) || climbed_count_ > 25) { |
Filip Kujawa | a7c8b41 | 2024-02-24 18:29:29 -0800 | [diff] [blame] | 104 | // Intake is pressed |
James Kuszmaul | 0338ec3 | 2024-03-16 11:40:51 -0700 | [diff] [blame] | 105 | superstructure_goal_builder->set_intake_pivot( |
| 106 | superstructure::IntakePivotGoal::DOWN); |
| 107 | } else { |
| 108 | superstructure_goal_builder->set_intake_pivot( |
| 109 | superstructure::IntakePivotGoal::UP); |
| 110 | } |
| 111 | |
| 112 | if (data.IsPressed(kIntakeRollers)) { |
| 113 | // Intake is pressed |
Filip Kujawa | a7c8b41 | 2024-02-24 18:29:29 -0800 | [diff] [blame] | 114 | superstructure_goal_builder->set_intake_goal( |
| 115 | superstructure::IntakeGoal::INTAKE); |
James Kuszmaul | 0338ec3 | 2024-03-16 11:40:51 -0700 | [diff] [blame] | 116 | } else if (data.IsPressed(kSpitRollers)) { |
| 117 | superstructure_goal_builder->set_intake_goal( |
| 118 | superstructure::IntakeGoal::SPIT); |
James Kuszmaul | 0281e15 | 2024-02-26 22:26:16 -0800 | [diff] [blame] | 119 | } else { |
Filip Kujawa | a7c8b41 | 2024-02-24 18:29:29 -0800 | [diff] [blame] | 120 | superstructure_goal_builder->set_intake_goal( |
James Kuszmaul | 0281e15 | 2024-02-26 22:26:16 -0800 | [diff] [blame] | 121 | superstructure::IntakeGoal::NONE); |
Filip Kujawa | a7c8b41 | 2024-02-24 18:29:29 -0800 | [diff] [blame] | 122 | } |
James Kuszmaul | 0338ec3 | 2024-03-16 11:40:51 -0700 | [diff] [blame] | 123 | |
Maxwell Henderson | 4587850 | 2024-03-15 19:35:25 -0700 | [diff] [blame] | 124 | if (data.IsPressed(kSpitExtend)) { |
| 125 | superstructure_goal_builder->set_spit_extend(true); |
| 126 | } else { |
| 127 | superstructure_goal_builder->set_spit_extend(false); |
| 128 | } |
| 129 | |
James Kuszmaul | 0281e15 | 2024-02-26 22:26:16 -0800 | [diff] [blame] | 130 | if (data.IsPressed(kAmp)) { |
Filip Kujawa | a7c8b41 | 2024-02-24 18:29:29 -0800 | [diff] [blame] | 131 | superstructure_goal_builder->set_note_goal(superstructure::NoteGoal::AMP); |
| 132 | } else if (data.IsPressed(kTrap)) { |
| 133 | superstructure_goal_builder->set_note_goal( |
| 134 | superstructure::NoteGoal::TRAP); |
James Kuszmaul | 0281e15 | 2024-02-26 22:26:16 -0800 | [diff] [blame] | 135 | } else if (data.IsPressed(kCatapultLoad)) { |
| 136 | superstructure_goal_builder->set_note_goal( |
| 137 | superstructure::NoteGoal::CATAPULT); |
| 138 | } else { |
| 139 | superstructure_goal_builder->set_note_goal( |
| 140 | superstructure::NoteGoal::NONE); |
Filip Kujawa | a7c8b41 | 2024-02-24 18:29:29 -0800 | [diff] [blame] | 141 | } |
Filip Kujawa | a7c8b41 | 2024-02-24 18:29:29 -0800 | [diff] [blame] | 142 | auto shooter_goal = superstructure_goal_builder->add_shooter_goal(); |
James Kuszmaul | e3b4569 | 2024-04-07 19:56:14 -0700 | [diff] [blame] | 143 | shooter_goal->set_preloaded(FLAGS_allow_force_preload && |
| 144 | data.IsPressed(kForceLoad)); |
Filip Kujawa | 1286c01 | 2024-03-31 22:53:27 -0700 | [diff] [blame] | 145 | if (data.IsPressed(kAutoAim)) { |
| 146 | shooter_goal->set_auto_aim( |
| 147 | control_loops::superstructure::AutoAimMode::SPEAKER); |
| 148 | } else if (data.IsPressed(kAimShuttle)) { |
| 149 | shooter_goal->set_auto_aim( |
| 150 | control_loops::superstructure::AutoAimMode::SHUTTLE); |
Austin Schuh | 79903e8 | 2024-04-07 23:12:00 -0700 | [diff] [blame] | 151 | } else if (data.IsPressed(kTurretShuttle)) { |
| 152 | shooter_goal->set_auto_aim( |
| 153 | control_loops::superstructure::AutoAimMode::TURRET_SHUTTLE); |
Filip Kujawa | 1286c01 | 2024-03-31 22:53:27 -0700 | [diff] [blame] | 154 | } |
Filip Kujawa | a7c8b41 | 2024-02-24 18:29:29 -0800 | [diff] [blame] | 155 | |
| 156 | // Updating aiming for shooter goal, only one type of aim should be possible |
| 157 | // at a time, auto-aiming is preferred over the setpoints. |
James Kuszmaul | 0281e15 | 2024-02-26 22:26:16 -0800 | [diff] [blame] | 158 | if (data.IsPressed(kAimSpeaker)) { |
Filip Kujawa | a7c8b41 | 2024-02-24 18:29:29 -0800 | [diff] [blame] | 159 | auto catapult_goal = shooter_goal->add_catapult_goal(); |
| 160 | catapult_goal->set_shot_velocity(robot_constants_->common() |
| 161 | ->shooter_speaker_set_point() |
| 162 | ->shot_velocity()); |
James Kuszmaul | 0338ec3 | 2024-03-16 11:40:51 -0700 | [diff] [blame] | 163 | if (FLAGS_speaker_altitude_position_override > 0) { |
| 164 | PopulateStaticZeroingSingleDOFProfiledSubsystemGoal( |
| 165 | shooter_goal->add_altitude_position(), |
| 166 | FLAGS_speaker_altitude_position_override); |
| 167 | } else { |
| 168 | PopulateStaticZeroingSingleDOFProfiledSubsystemGoal( |
| 169 | shooter_goal->add_altitude_position(), |
| 170 | robot_constants_->common() |
| 171 | ->shooter_speaker_set_point() |
| 172 | ->altitude_position()); |
| 173 | } |
Filip Kujawa | a7c8b41 | 2024-02-24 18:29:29 -0800 | [diff] [blame] | 174 | PopulateStaticZeroingSingleDOFProfiledSubsystemGoal( |
| 175 | shooter_goal->add_turret_position(), robot_constants_->common() |
| 176 | ->shooter_speaker_set_point() |
| 177 | ->turret_position()); |
Filip Kujawa | a7c8b41 | 2024-02-24 18:29:29 -0800 | [diff] [blame] | 178 | } |
Austin Schuh | cb70344 | 2024-03-23 14:30:05 -0700 | [diff] [blame] | 179 | superstructure_goal_builder->set_fire(data.IsPressed(kFire) || |
| 180 | data.IsPressed(kDriverFire)); |
Filip Kujawa | a7c8b41 | 2024-02-24 18:29:29 -0800 | [diff] [blame] | 181 | |
Niko Sohmers | 4d93d4c | 2024-03-24 14:48:26 -0700 | [diff] [blame] | 182 | if (data.IsPressed(kRaiseClimber)) { |
James Kuszmaul | 42a8745 | 2024-04-05 17:30:47 -0700 | [diff] [blame] | 183 | ++climbed_count_; |
Niko Sohmers | 4d93d4c | 2024-03-24 14:48:26 -0700 | [diff] [blame] | 184 | superstructure_goal_builder->set_climber_goal_voltage(4.0); |
| 185 | } else if (data.IsPressed(kRaiseFastClimber)) { |
James Kuszmaul | 42a8745 | 2024-04-05 17:30:47 -0700 | [diff] [blame] | 186 | ++climbed_count_; |
| 187 | superstructure_goal_builder->set_climber_goal_voltage(12.0); |
| 188 | } |
| 189 | |
| 190 | if (climbed_count_ > 25) { |
| 191 | PopulateStaticZeroingSingleDOFProfiledSubsystemGoal( |
| 192 | shooter_goal->add_turret_position(), 0.0); |
James Kuszmaul | 0338ec3 | 2024-03-16 11:40:51 -0700 | [diff] [blame] | 193 | } |
| 194 | |
Filip Kujawa | a7c8b41 | 2024-02-24 18:29:29 -0800 | [diff] [blame] | 195 | superstructure_goal_builder.CheckOk(superstructure_goal_builder.Send()); |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | private: |
Filip Kujawa | a7c8b41 | 2024-02-24 18:29:29 -0800 | [diff] [blame] | 199 | ::aos::Sender<control_loops::superstructure::GoalStatic> |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 200 | superstructure_goal_sender_; |
| 201 | ::aos::Fetcher<control_loops::superstructure::Status> |
| 202 | superstructure_status_fetcher_; |
Filip Kujawa | a7c8b41 | 2024-02-24 18:29:29 -0800 | [diff] [blame] | 203 | const y2024::Constants *robot_constants_; |
James Kuszmaul | 42a8745 | 2024-04-05 17:30:47 -0700 | [diff] [blame] | 204 | |
| 205 | int climbed_count_ = 0; |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 206 | }; |
| 207 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 208 | } // namespace y2024::input::joysticks |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 209 | |
| 210 | int main(int argc, char **argv) { |
| 211 | ::aos::InitGoogle(&argc, &argv); |
| 212 | |
| 213 | aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
| 214 | aos::configuration::ReadConfig("aos_config.json"); |
James Kuszmaul | 2549e75 | 2024-01-20 17:42:51 -0800 | [diff] [blame] | 215 | frc971::constants::WaitForConstants<y2024::Constants>(&config.message()); |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 216 | |
Filip Kujawa | a7c8b41 | 2024-02-24 18:29:29 -0800 | [diff] [blame] | 217 | ::aos::ShmEventLoop constant_fetcher_event_loop(&config.message()); |
| 218 | frc971::constants::ConstantsFetcher<y2024::Constants> constants_fetcher( |
| 219 | &constant_fetcher_event_loop); |
| 220 | const y2024::Constants *robot_constants = &constants_fetcher.constants(); |
| 221 | |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 222 | ::aos::ShmEventLoop event_loop(&config.message()); |
Filip Kujawa | a7c8b41 | 2024-02-24 18:29:29 -0800 | [diff] [blame] | 223 | ::y2024::input::joysticks::Reader reader(&event_loop, robot_constants); |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 224 | |
| 225 | event_loop.Run(); |
| 226 | |
| 227 | return 0; |
| 228 | } |