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