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