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