Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 1 | #ifndef AOS_INPUT_ACTION_JOYSTICK_INPUT_H_ |
| 2 | #define AOS_INPUT_ACTION_JOYSTICK_INPUT_H_ |
| 3 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 4 | #include "frc971/autonomous/auto_generated.h" |
Austin Schuh | ed5b26d | 2019-12-05 20:51:59 -0800 | [diff] [blame] | 5 | #include "frc971/autonomous/auto_mode_generated.h" |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 6 | #include "frc971/autonomous/base_autonomous_actor.h" |
James Kuszmaul | 7077d34 | 2021-06-09 20:23:58 -0700 | [diff] [blame] | 7 | #include "frc971/input/driver_station_data.h" |
| 8 | #include "frc971/input/drivetrain_input.h" |
| 9 | #include "frc971/input/joystick_input.h" |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 10 | |
Stephan Pleines | d99b1ee | 2024-02-02 20:56:44 -0800 | [diff] [blame] | 11 | namespace frc971::input { |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 12 | |
| 13 | // Class to abstract out managing actions, autonomous mode, and drivetrains. |
| 14 | // Turns out we do the same thing every year, so let's stop copying it. |
James Kuszmaul | 7077d34 | 2021-06-09 20:23:58 -0700 | [diff] [blame] | 15 | class ActionJoystickInput : public ::frc971::input::JoystickInput { |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 16 | public: |
James Kuszmaul | 7077d34 | 2021-06-09 20:23:58 -0700 | [diff] [blame] | 17 | // Configuration parameters that don't really belong in the DrivetrainConfig. |
James Kuszmaul | ccc368b | 2019-04-11 20:00:07 -0700 | [diff] [blame] | 18 | struct InputConfig { |
| 19 | // If true, we will run teleop during auto mode after auto mode ends. This |
| 20 | // is to support the 2019 sandstorm mode. Auto will run, and then when the |
| 21 | // action ends (either when it's done, or when the driver triggers it to |
| 22 | // finish early), we will run teleop regardless of the mode. |
| 23 | bool run_teleop_in_auto = false; |
| 24 | // A button, for use with the run_teleop_in_auto, that will cancel the auto |
| 25 | // mode, and if run_telop_in_auto is specified, resume teloperation. |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 26 | const driver_station::ButtonLocation cancel_auto_button = {-1, -1}; |
Ravago Jones | 8c65c43 | 2023-03-25 17:35:39 -0700 | [diff] [blame] | 27 | |
| 28 | // Use button 14 and 15 to encode the id of the joystick and remap the |
| 29 | // joysticks so that their ids are independent of their order on the |
| 30 | // driverstation. |
| 31 | bool use_redundant_joysticks = false; |
James Kuszmaul | ccc368b | 2019-04-11 20:00:07 -0700 | [diff] [blame] | 32 | }; |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 33 | ActionJoystickInput( |
| 34 | ::aos::EventLoop *event_loop, |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 35 | const ::frc971::control_loops::drivetrain::DrivetrainConfig<double> |
| 36 | &dt_config, |
| 37 | DrivetrainInputReader::InputType input_type, |
James Kuszmaul | ccc368b | 2019-04-11 20:00:07 -0700 | [diff] [blame] | 38 | const InputConfig &input_config) |
James Kuszmaul | 7077d34 | 2021-06-09 20:23:58 -0700 | [diff] [blame] | 39 | : ::frc971::input::JoystickInput(event_loop), |
James Kuszmaul | ccc368b | 2019-04-11 20:00:07 -0700 | [diff] [blame] | 40 | input_config_(input_config), |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 41 | drivetrain_input_reader_( |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 42 | DrivetrainInputReader::Make(event_loop, input_type, dt_config)), |
Austin Schuh | a250b2d | 2019-05-27 16:14:02 -0700 | [diff] [blame] | 43 | dt_config_(dt_config), |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 44 | autonomous_action_factory_( |
Austin Schuh | a250b2d | 2019-05-27 16:14:02 -0700 | [diff] [blame] | 45 | ::frc971::autonomous::BaseAutonomousActor::MakeFactory(event_loop)), |
| 46 | autonomous_mode_fetcher_( |
| 47 | event_loop->MakeFetcher<::frc971::autonomous::AutonomousMode>( |
Austin Schuh | ed5b26d | 2019-12-05 20:51:59 -0800 | [diff] [blame] | 48 | "/autonomous")) {} |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 49 | |
| 50 | virtual ~ActionJoystickInput() {} |
| 51 | |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 52 | protected: |
| 53 | bool was_running_action() { return was_running_; } |
| 54 | |
Austin Schuh | a250b2d | 2019-05-27 16:14:02 -0700 | [diff] [blame] | 55 | // Returns true if an action is running. |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 56 | bool ActionRunning() { return action_queue_.Running(); } |
Austin Schuh | a250b2d | 2019-05-27 16:14:02 -0700 | [diff] [blame] | 57 | // Cancels all actions. |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 58 | void CancelAllActions() { action_queue_.CancelAllActions(); } |
Austin Schuh | a250b2d | 2019-05-27 16:14:02 -0700 | [diff] [blame] | 59 | // Cancels the current action. |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 60 | void CancelCurrentAction() { action_queue_.CancelCurrentAction(); } |
| 61 | |
Austin Schuh | a250b2d | 2019-05-27 16:14:02 -0700 | [diff] [blame] | 62 | // Enqueues an action. |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 63 | void EnqueueAction(::std::unique_ptr<::aos::common::actions::Action> action) { |
| 64 | action_queue_.EnqueueAction(::std::move(action)); |
| 65 | } |
| 66 | |
Austin Schuh | a250b2d | 2019-05-27 16:14:02 -0700 | [diff] [blame] | 67 | // Returns the current robot velocity. |
| 68 | double robot_velocity() const { |
| 69 | return drivetrain_input_reader_->robot_velocity(); |
| 70 | } |
| 71 | |
Austin Schuh | 0b00c86 | 2021-10-17 17:39:10 -0700 | [diff] [blame] | 72 | // Returns the current drivetrain status. |
| 73 | const control_loops::drivetrain::Status *drivetrain_status() const { |
| 74 | return drivetrain_input_reader_->drivetrain_status(); |
| 75 | } |
| 76 | |
Austin Schuh | a250b2d | 2019-05-27 16:14:02 -0700 | [diff] [blame] | 77 | // Returns the drivetrain config. |
| 78 | const ::frc971::control_loops::drivetrain::DrivetrainConfig<double> |
| 79 | dt_config() const { |
| 80 | return dt_config_; |
| 81 | } |
| 82 | |
| 83 | // Sets the vision align function. This function runs before the normal |
| 84 | // drivetrain code runs. If it returns true, we are in vision align mode and |
| 85 | // no drivetain code is run. If it returns false, the vision align function |
| 86 | // is assumed to be disabled and normal drive code is run. |
| 87 | void set_vision_align_fn( |
James Kuszmaul | 7077d34 | 2021-06-09 20:23:58 -0700 | [diff] [blame] | 88 | ::std::function<bool(const ::frc971::input::driver_station::Data &data)> |
Austin Schuh | a250b2d | 2019-05-27 16:14:02 -0700 | [diff] [blame] | 89 | vision_align_fn) { |
| 90 | drivetrain_input_reader_->set_vision_align_fn(vision_align_fn); |
| 91 | } |
| 92 | |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 93 | private: |
Austin Schuh | 59a62e7 | 2019-03-13 22:39:03 -0700 | [diff] [blame] | 94 | // Handles anything that needs to be cleaned up when the auto action exits. |
| 95 | virtual void AutoEnded() {} |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 96 | // Handles any year specific superstructure code. |
James Kuszmaul | 7077d34 | 2021-06-09 20:23:58 -0700 | [diff] [blame] | 97 | virtual void HandleTeleop( |
| 98 | const ::frc971::input::driver_station::Data &data) = 0; |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 99 | |
James Kuszmaul | 7077d34 | 2021-06-09 20:23:58 -0700 | [diff] [blame] | 100 | void RunIteration(const ::frc971::input::driver_station::Data &data) override; |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 101 | |
Ravago Jones | 8c65c43 | 2023-03-25 17:35:39 -0700 | [diff] [blame] | 102 | void DoRunIteration(const ::frc971::input::driver_station::Data &data); |
| 103 | |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 104 | void StartAuto(); |
| 105 | void StopAuto(); |
| 106 | |
Austin Schuh | a964406 | 2019-03-28 14:31:52 -0700 | [diff] [blame] | 107 | // Returns the current autonomous mode which has been selected by robot |
| 108 | // inputs. |
Austin Schuh | a250b2d | 2019-05-27 16:14:02 -0700 | [diff] [blame] | 109 | virtual uint32_t GetAutonomousMode() { |
| 110 | autonomous_mode_fetcher_.Fetch(); |
| 111 | if (autonomous_mode_fetcher_.get() == nullptr) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 112 | AOS_LOG(WARNING, "no auto mode values\n"); |
Austin Schuh | a250b2d | 2019-05-27 16:14:02 -0700 | [diff] [blame] | 113 | return 0; |
| 114 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 115 | return autonomous_mode_fetcher_->mode(); |
Austin Schuh | a250b2d | 2019-05-27 16:14:02 -0700 | [diff] [blame] | 116 | } |
Austin Schuh | a964406 | 2019-03-28 14:31:52 -0700 | [diff] [blame] | 117 | |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 118 | // True if the internal state machine thinks auto is running right now. |
| 119 | bool auto_running_ = false; |
James Kuszmaul | ccc368b | 2019-04-11 20:00:07 -0700 | [diff] [blame] | 120 | // True if we think that the auto *action* is running right now. |
| 121 | bool auto_action_running_ = false; |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 122 | // True if an action was running last cycle. |
| 123 | bool was_running_ = false; |
| 124 | |
James Kuszmaul | ccc368b | 2019-04-11 20:00:07 -0700 | [diff] [blame] | 125 | const InputConfig input_config_; |
Austin Schuh | 30cc40a | 2019-03-12 21:02:13 -0700 | [diff] [blame] | 126 | |
Austin Schuh | 59a62e7 | 2019-03-13 22:39:03 -0700 | [diff] [blame] | 127 | // Bool to track if auto was running the last cycle through. This lets us |
| 128 | // call AutoEnded when the auto mode function stops. |
| 129 | bool auto_was_running_ = false; |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 130 | ::std::unique_ptr<DrivetrainInputReader> drivetrain_input_reader_; |
| 131 | ::aos::common::actions::ActionQueue action_queue_; |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 132 | |
Austin Schuh | a250b2d | 2019-05-27 16:14:02 -0700 | [diff] [blame] | 133 | const ::frc971::control_loops::drivetrain::DrivetrainConfig<double> |
| 134 | dt_config_; |
| 135 | |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 136 | ::frc971::autonomous::BaseAutonomousActor::Factory autonomous_action_factory_; |
Austin Schuh | a250b2d | 2019-05-27 16:14:02 -0700 | [diff] [blame] | 137 | |
| 138 | ::aos::Fetcher<::frc971::autonomous::AutonomousMode> autonomous_mode_fetcher_; |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 139 | }; |
| 140 | |
Stephan Pleines | d99b1ee | 2024-02-02 20:56:44 -0800 | [diff] [blame] | 141 | } // namespace frc971::input |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 142 | |
| 143 | #endif // AOS_INPUT_ACTION_JOYSTICK_INPUT_H_ |