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 | |
| 4 | #include "aos/input/driver_station_data.h" |
| 5 | #include "aos/input/drivetrain_input.h" |
| 6 | #include "aos/input/joystick_input.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 7 | #include "frc971/autonomous/auto_generated.h" |
Austin Schuh | ed5b26d | 2019-12-05 20:51:59 -0800 | [diff] [blame^] | 8 | #include "frc971/autonomous/auto_mode_generated.h" |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 9 | #include "frc971/autonomous/base_autonomous_actor.h" |
| 10 | |
| 11 | namespace aos { |
| 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. |
| 16 | class ActionJoystickInput : public ::aos::input::JoystickInput { |
| 17 | public: |
James Kuszmaul | ccc368b | 2019-04-11 20:00:07 -0700 | [diff] [blame] | 18 | // Configuration parameters that don't really belong in the DrivetrainConfig. |
| 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) |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 35 | : ::aos::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 | |
| 68 | // Returns the drivetrain config. |
| 69 | const ::frc971::control_loops::drivetrain::DrivetrainConfig<double> |
| 70 | dt_config() const { |
| 71 | return dt_config_; |
| 72 | } |
| 73 | |
| 74 | // Sets the vision align function. This function runs before the normal |
| 75 | // drivetrain code runs. If it returns true, we are in vision align mode and |
| 76 | // no drivetain code is run. If it returns false, the vision align function |
| 77 | // is assumed to be disabled and normal drive code is run. |
| 78 | void set_vision_align_fn( |
| 79 | ::std::function<bool(const ::aos::input::driver_station::Data &data)> |
| 80 | vision_align_fn) { |
| 81 | drivetrain_input_reader_->set_vision_align_fn(vision_align_fn); |
| 82 | } |
| 83 | |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 84 | private: |
Austin Schuh | 59a62e7 | 2019-03-13 22:39:03 -0700 | [diff] [blame] | 85 | // Handles anything that needs to be cleaned up when the auto action exits. |
| 86 | virtual void AutoEnded() {} |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 87 | // Handles any year specific superstructure code. |
| 88 | virtual void HandleTeleop(const ::aos::input::driver_station::Data &data) = 0; |
| 89 | |
| 90 | void RunIteration(const ::aos::input::driver_station::Data &data) override; |
| 91 | |
| 92 | void StartAuto(); |
| 93 | void StopAuto(); |
| 94 | |
Austin Schuh | a964406 | 2019-03-28 14:31:52 -0700 | [diff] [blame] | 95 | // Returns the current autonomous mode which has been selected by robot |
| 96 | // inputs. |
Austin Schuh | a250b2d | 2019-05-27 16:14:02 -0700 | [diff] [blame] | 97 | virtual uint32_t GetAutonomousMode() { |
| 98 | autonomous_mode_fetcher_.Fetch(); |
| 99 | if (autonomous_mode_fetcher_.get() == nullptr) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 100 | AOS_LOG(WARNING, "no auto mode values\n"); |
Austin Schuh | a250b2d | 2019-05-27 16:14:02 -0700 | [diff] [blame] | 101 | return 0; |
| 102 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 103 | return autonomous_mode_fetcher_->mode(); |
Austin Schuh | a250b2d | 2019-05-27 16:14:02 -0700 | [diff] [blame] | 104 | } |
Austin Schuh | a964406 | 2019-03-28 14:31:52 -0700 | [diff] [blame] | 105 | |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 106 | // True if the internal state machine thinks auto is running right now. |
| 107 | bool auto_running_ = false; |
James Kuszmaul | ccc368b | 2019-04-11 20:00:07 -0700 | [diff] [blame] | 108 | // True if we think that the auto *action* is running right now. |
| 109 | bool auto_action_running_ = false; |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 110 | // True if an action was running last cycle. |
| 111 | bool was_running_ = false; |
| 112 | |
James Kuszmaul | ccc368b | 2019-04-11 20:00:07 -0700 | [diff] [blame] | 113 | const InputConfig input_config_; |
Austin Schuh | 30cc40a | 2019-03-12 21:02:13 -0700 | [diff] [blame] | 114 | |
Austin Schuh | 59a62e7 | 2019-03-13 22:39:03 -0700 | [diff] [blame] | 115 | // Bool to track if auto was running the last cycle through. This lets us |
| 116 | // call AutoEnded when the auto mode function stops. |
| 117 | bool auto_was_running_ = false; |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 118 | ::std::unique_ptr<DrivetrainInputReader> drivetrain_input_reader_; |
| 119 | ::aos::common::actions::ActionQueue action_queue_; |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 120 | |
Austin Schuh | a250b2d | 2019-05-27 16:14:02 -0700 | [diff] [blame] | 121 | const ::frc971::control_loops::drivetrain::DrivetrainConfig<double> |
| 122 | dt_config_; |
| 123 | |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 124 | ::frc971::autonomous::BaseAutonomousActor::Factory autonomous_action_factory_; |
Austin Schuh | a250b2d | 2019-05-27 16:14:02 -0700 | [diff] [blame] | 125 | |
| 126 | ::aos::Fetcher<::frc971::autonomous::AutonomousMode> autonomous_mode_fetcher_; |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 127 | }; |
| 128 | |
| 129 | } // namespace input |
| 130 | } // namespace aos |
| 131 | |
| 132 | #endif // AOS_INPUT_ACTION_JOYSTICK_INPUT_H_ |