James Kuszmaul | 7077d34 | 2021-06-09 20:23:58 -0700 | [diff] [blame] | 1 | #include "frc971/input/action_joystick_input.h" |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 2 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 3 | #include "frc971/autonomous/auto_generated.h" |
Austin Schuh | ed5b26d | 2019-12-05 20:51:59 -0800 | [diff] [blame] | 4 | #include "frc971/autonomous/auto_mode_generated.h" |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 5 | #include "frc971/autonomous/base_autonomous_actor.h" |
James Kuszmaul | 7077d34 | 2021-06-09 20:23:58 -0700 | [diff] [blame] | 6 | #include "frc971/input/driver_station_data.h" |
Ravago Jones | 8c65c43 | 2023-03-25 17:35:39 -0700 | [diff] [blame] | 7 | #include "frc971/input/redundant_joystick_data.h" |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 8 | |
James Kuszmaul | 7077d34 | 2021-06-09 20:23:58 -0700 | [diff] [blame] | 9 | using ::frc971::input::driver_station::ControlBit; |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 10 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 11 | namespace frc971::input { |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 12 | |
| 13 | void ActionJoystickInput::RunIteration( |
Ravago Jones | 8c65c43 | 2023-03-25 17:35:39 -0700 | [diff] [blame] | 14 | const ::frc971::input::driver_station::Data &unsorted_data) { |
| 15 | if (input_config_.use_redundant_joysticks) { |
| 16 | driver_station::RedundantData redundant_data_storage(unsorted_data); |
| 17 | DoRunIteration(redundant_data_storage); |
| 18 | } else { |
| 19 | DoRunIteration(unsorted_data); |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | void ActionJoystickInput::DoRunIteration( |
James Kuszmaul | 7077d34 | 2021-06-09 20:23:58 -0700 | [diff] [blame] | 24 | const ::frc971::input::driver_station::Data &data) { |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 25 | const bool last_auto_running = auto_running_; |
| 26 | auto_running_ = data.GetControlBit(ControlBit::kAutonomous) && |
| 27 | data.GetControlBit(ControlBit::kEnabled); |
| 28 | if (auto_running_ != last_auto_running) { |
| 29 | if (auto_running_) { |
Austin Schuh | 59a62e7 | 2019-03-13 22:39:03 -0700 | [diff] [blame] | 30 | auto_was_running_ = true; |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 31 | StartAuto(); |
| 32 | } else { |
| 33 | StopAuto(); |
| 34 | } |
| 35 | } |
| 36 | |
James Kuszmaul | ccc368b | 2019-04-11 20:00:07 -0700 | [diff] [blame] | 37 | if (!auto_running_ || |
| 38 | (input_config_.run_teleop_in_auto && !action_queue_.Running())) { |
Austin Schuh | 59a62e7 | 2019-03-13 22:39:03 -0700 | [diff] [blame] | 39 | if (auto_was_running_) { |
| 40 | AutoEnded(); |
| 41 | auto_was_running_ = false; |
| 42 | } |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 43 | if (!data.GetControlBit(ControlBit::kEnabled)) { |
| 44 | action_queue_.CancelAllActions(); |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 45 | } |
| 46 | drivetrain_input_reader_->HandleDrivetrain(data); |
| 47 | HandleTeleop(data); |
| 48 | } |
| 49 | |
James Kuszmaul | ccc368b | 2019-04-11 20:00:07 -0700 | [diff] [blame] | 50 | if (auto_action_running_ && |
| 51 | data.IsPressed(input_config_.cancel_auto_button)) { |
| 52 | StopAuto(); |
| 53 | } |
| 54 | |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 55 | // Process pending actions. |
| 56 | action_queue_.Tick(); |
| 57 | was_running_ = action_queue_.Running(); |
| 58 | } |
| 59 | |
| 60 | void ActionJoystickInput::StartAuto() { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 61 | AOS_LOG(INFO, "Starting auto mode\n"); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 62 | frc971::autonomous::AutonomousActionParamsT params; |
| 63 | params.mode = GetAutonomousMode(); |
| 64 | |
| 65 | action_queue_.EnqueueAction(autonomous_action_factory_.Make(params)); |
James Kuszmaul | ccc368b | 2019-04-11 20:00:07 -0700 | [diff] [blame] | 66 | auto_action_running_ = true; |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | void ActionJoystickInput::StopAuto() { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 70 | AOS_LOG(INFO, "Stopping auto mode\n"); |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 71 | action_queue_.CancelAllActions(); |
James Kuszmaul | ccc368b | 2019-04-11 20:00:07 -0700 | [diff] [blame] | 72 | auto_action_running_ = false; |
Austin Schuh | b5b79a5 | 2019-05-08 20:32:07 -0700 | [diff] [blame] | 73 | AutoEnded(); |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 74 | } |
| 75 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 76 | } // namespace frc971::input |