blob: ac37c2af81bb0a7ce2b608ad21eac85d3a9a43fe [file] [log] [blame]
Sabina Davis91b23602019-01-21 00:06:01 -08001#ifndef AOS_INPUT_ACTION_JOYSTICK_INPUT_H_
2#define AOS_INPUT_ACTION_JOYSTICK_INPUT_H_
3
Alex Perrycb7da4b2019-08-28 19:35:56 -07004#include "frc971/autonomous/auto_generated.h"
Austin Schuhed5b26d2019-12-05 20:51:59 -08005#include "frc971/autonomous/auto_mode_generated.h"
Sabina Davis91b23602019-01-21 00:06:01 -08006#include "frc971/autonomous/base_autonomous_actor.h"
James Kuszmaul7077d342021-06-09 20:23:58 -07007#include "frc971/input/driver_station_data.h"
8#include "frc971/input/drivetrain_input.h"
9#include "frc971/input/joystick_input.h"
Sabina Davis91b23602019-01-21 00:06:01 -080010
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -080011namespace frc971::input {
Sabina Davis91b23602019-01-21 00:06:01 -080012
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 Kuszmaul7077d342021-06-09 20:23:58 -070015class ActionJoystickInput : public ::frc971::input::JoystickInput {
Sabina Davis91b23602019-01-21 00:06:01 -080016 public:
James Kuszmaul7077d342021-06-09 20:23:58 -070017 // Configuration parameters that don't really belong in the DrivetrainConfig.
James Kuszmaulccc368b2019-04-11 20:00:07 -070018 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 Schuhbfb04122019-05-22 21:16:51 -070026 const driver_station::ButtonLocation cancel_auto_button = {-1, -1};
Ravago Jones8c65c432023-03-25 17:35:39 -070027
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 Kuszmaulccc368b2019-04-11 20:00:07 -070032 };
Sabina Davis91b23602019-01-21 00:06:01 -080033 ActionJoystickInput(
34 ::aos::EventLoop *event_loop,
Austin Schuhbfb04122019-05-22 21:16:51 -070035 const ::frc971::control_loops::drivetrain::DrivetrainConfig<double>
36 &dt_config,
37 DrivetrainInputReader::InputType input_type,
James Kuszmaulccc368b2019-04-11 20:00:07 -070038 const InputConfig &input_config)
James Kuszmaul7077d342021-06-09 20:23:58 -070039 : ::frc971::input::JoystickInput(event_loop),
James Kuszmaulccc368b2019-04-11 20:00:07 -070040 input_config_(input_config),
Austin Schuh1bf8a212019-05-26 22:13:14 -070041 drivetrain_input_reader_(
Austin Schuhbd0a40f2019-06-30 14:56:31 -070042 DrivetrainInputReader::Make(event_loop, input_type, dt_config)),
Austin Schuha250b2d2019-05-27 16:14:02 -070043 dt_config_(dt_config),
Austin Schuh1bf8a212019-05-26 22:13:14 -070044 autonomous_action_factory_(
Austin Schuha250b2d2019-05-27 16:14:02 -070045 ::frc971::autonomous::BaseAutonomousActor::MakeFactory(event_loop)),
46 autonomous_mode_fetcher_(
47 event_loop->MakeFetcher<::frc971::autonomous::AutonomousMode>(
Austin Schuhed5b26d2019-12-05 20:51:59 -080048 "/autonomous")) {}
Sabina Davis91b23602019-01-21 00:06:01 -080049
50 virtual ~ActionJoystickInput() {}
51
Austin Schuhbfb04122019-05-22 21:16:51 -070052 protected:
53 bool was_running_action() { return was_running_; }
54
Austin Schuha250b2d2019-05-27 16:14:02 -070055 // Returns true if an action is running.
Austin Schuhbfb04122019-05-22 21:16:51 -070056 bool ActionRunning() { return action_queue_.Running(); }
Austin Schuha250b2d2019-05-27 16:14:02 -070057 // Cancels all actions.
Austin Schuhbfb04122019-05-22 21:16:51 -070058 void CancelAllActions() { action_queue_.CancelAllActions(); }
Austin Schuha250b2d2019-05-27 16:14:02 -070059 // Cancels the current action.
Austin Schuhbfb04122019-05-22 21:16:51 -070060 void CancelCurrentAction() { action_queue_.CancelCurrentAction(); }
61
Austin Schuha250b2d2019-05-27 16:14:02 -070062 // Enqueues an action.
Austin Schuhbfb04122019-05-22 21:16:51 -070063 void EnqueueAction(::std::unique_ptr<::aos::common::actions::Action> action) {
64 action_queue_.EnqueueAction(::std::move(action));
65 }
66
Austin Schuha250b2d2019-05-27 16:14:02 -070067 // Returns the current robot velocity.
68 double robot_velocity() const {
69 return drivetrain_input_reader_->robot_velocity();
70 }
71
Austin Schuh0b00c862021-10-17 17:39:10 -070072 // Returns the current drivetrain status.
73 const control_loops::drivetrain::Status *drivetrain_status() const {
74 return drivetrain_input_reader_->drivetrain_status();
75 }
76
Austin Schuha250b2d2019-05-27 16:14:02 -070077 // 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 Kuszmaul7077d342021-06-09 20:23:58 -070088 ::std::function<bool(const ::frc971::input::driver_station::Data &data)>
Austin Schuha250b2d2019-05-27 16:14:02 -070089 vision_align_fn) {
90 drivetrain_input_reader_->set_vision_align_fn(vision_align_fn);
91 }
92
Sabina Davis91b23602019-01-21 00:06:01 -080093 private:
Austin Schuh59a62e72019-03-13 22:39:03 -070094 // Handles anything that needs to be cleaned up when the auto action exits.
95 virtual void AutoEnded() {}
Sabina Davis91b23602019-01-21 00:06:01 -080096 // Handles any year specific superstructure code.
James Kuszmaul7077d342021-06-09 20:23:58 -070097 virtual void HandleTeleop(
98 const ::frc971::input::driver_station::Data &data) = 0;
Sabina Davis91b23602019-01-21 00:06:01 -080099
James Kuszmaul7077d342021-06-09 20:23:58 -0700100 void RunIteration(const ::frc971::input::driver_station::Data &data) override;
Sabina Davis91b23602019-01-21 00:06:01 -0800101
Ravago Jones8c65c432023-03-25 17:35:39 -0700102 void DoRunIteration(const ::frc971::input::driver_station::Data &data);
103
Sabina Davis91b23602019-01-21 00:06:01 -0800104 void StartAuto();
105 void StopAuto();
106
Austin Schuha9644062019-03-28 14:31:52 -0700107 // Returns the current autonomous mode which has been selected by robot
108 // inputs.
Austin Schuha250b2d2019-05-27 16:14:02 -0700109 virtual uint32_t GetAutonomousMode() {
110 autonomous_mode_fetcher_.Fetch();
111 if (autonomous_mode_fetcher_.get() == nullptr) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700112 AOS_LOG(WARNING, "no auto mode values\n");
Austin Schuha250b2d2019-05-27 16:14:02 -0700113 return 0;
114 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700115 return autonomous_mode_fetcher_->mode();
Austin Schuha250b2d2019-05-27 16:14:02 -0700116 }
Austin Schuha9644062019-03-28 14:31:52 -0700117
Sabina Davis91b23602019-01-21 00:06:01 -0800118 // True if the internal state machine thinks auto is running right now.
119 bool auto_running_ = false;
James Kuszmaulccc368b2019-04-11 20:00:07 -0700120 // True if we think that the auto *action* is running right now.
121 bool auto_action_running_ = false;
Sabina Davis91b23602019-01-21 00:06:01 -0800122 // True if an action was running last cycle.
123 bool was_running_ = false;
124
James Kuszmaulccc368b2019-04-11 20:00:07 -0700125 const InputConfig input_config_;
Austin Schuh30cc40a2019-03-12 21:02:13 -0700126
Austin Schuh59a62e72019-03-13 22:39:03 -0700127 // 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 Davis91b23602019-01-21 00:06:01 -0800130 ::std::unique_ptr<DrivetrainInputReader> drivetrain_input_reader_;
131 ::aos::common::actions::ActionQueue action_queue_;
Austin Schuh1bf8a212019-05-26 22:13:14 -0700132
Austin Schuha250b2d2019-05-27 16:14:02 -0700133 const ::frc971::control_loops::drivetrain::DrivetrainConfig<double>
134 dt_config_;
135
Austin Schuh1bf8a212019-05-26 22:13:14 -0700136 ::frc971::autonomous::BaseAutonomousActor::Factory autonomous_action_factory_;
Austin Schuha250b2d2019-05-27 16:14:02 -0700137
138 ::aos::Fetcher<::frc971::autonomous::AutonomousMode> autonomous_mode_fetcher_;
Sabina Davis91b23602019-01-21 00:06:01 -0800139};
140
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -0800141} // namespace frc971::input
Sabina Davis91b23602019-01-21 00:06:01 -0800142
143#endif // AOS_INPUT_ACTION_JOYSTICK_INPUT_H_