blob: 01604683d51e4e21f716fff2f48f74db31d6093b [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
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
10namespace aos {
11namespace 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.
15class ActionJoystickInput : public ::aos::input::JoystickInput {
16 public:
James Kuszmaulccc368b2019-04-11 20:00:07 -070017 // 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 Schuhbfb04122019-05-22 21:16:51 -070026 const driver_station::ButtonLocation cancel_auto_button = {-1, -1};
James Kuszmaulccc368b2019-04-11 20:00:07 -070027 };
Sabina Davis91b23602019-01-21 00:06:01 -080028 ActionJoystickInput(
29 ::aos::EventLoop *event_loop,
Austin Schuhbfb04122019-05-22 21:16:51 -070030 const ::frc971::control_loops::drivetrain::DrivetrainConfig<double>
31 &dt_config,
32 DrivetrainInputReader::InputType input_type,
James Kuszmaulccc368b2019-04-11 20:00:07 -070033 const InputConfig &input_config)
Sabina Davis91b23602019-01-21 00:06:01 -080034 : ::aos::input::JoystickInput(event_loop),
James Kuszmaulccc368b2019-04-11 20:00:07 -070035 input_config_(input_config),
Austin Schuh1bf8a212019-05-26 22:13:14 -070036 drivetrain_input_reader_(
37 DrivetrainInputReader::Make(input_type, dt_config)),
38 autonomous_action_factory_(
39 ::frc971::autonomous::BaseAutonomousActor::MakeFactory(
40 event_loop)) {}
Sabina Davis91b23602019-01-21 00:06:01 -080041
42 virtual ~ActionJoystickInput() {}
43
Austin Schuhbfb04122019-05-22 21:16:51 -070044 protected:
45 bool was_running_action() { return was_running_; }
46
47 bool ActionRunning() { return action_queue_.Running(); }
48 void CancelAllActions() { action_queue_.CancelAllActions(); }
49 void CancelCurrentAction() { action_queue_.CancelCurrentAction(); }
50
51 void EnqueueAction(::std::unique_ptr<::aos::common::actions::Action> action) {
52 action_queue_.EnqueueAction(::std::move(action));
53 }
54
Sabina Davis91b23602019-01-21 00:06:01 -080055 private:
Austin Schuh59a62e72019-03-13 22:39:03 -070056 // Handles anything that needs to be cleaned up when the auto action exits.
57 virtual void AutoEnded() {}
Sabina Davis91b23602019-01-21 00:06:01 -080058 // Handles any year specific superstructure code.
59 virtual void HandleTeleop(const ::aos::input::driver_station::Data &data) = 0;
60
61 void RunIteration(const ::aos::input::driver_station::Data &data) override;
62
63 void StartAuto();
64 void StopAuto();
65
Austin Schuha9644062019-03-28 14:31:52 -070066 // Returns the current autonomous mode which has been selected by robot
67 // inputs.
68 virtual uint32_t GetAutonomousMode() { return 0; }
69
Sabina Davis91b23602019-01-21 00:06:01 -080070 // True if the internal state machine thinks auto is running right now.
71 bool auto_running_ = false;
James Kuszmaulccc368b2019-04-11 20:00:07 -070072 // True if we think that the auto *action* is running right now.
73 bool auto_action_running_ = false;
Sabina Davis91b23602019-01-21 00:06:01 -080074 // True if an action was running last cycle.
75 bool was_running_ = false;
76
James Kuszmaulccc368b2019-04-11 20:00:07 -070077 const InputConfig input_config_;
Austin Schuh30cc40a2019-03-12 21:02:13 -070078
Austin Schuh59a62e72019-03-13 22:39:03 -070079 // Bool to track if auto was running the last cycle through. This lets us
80 // call AutoEnded when the auto mode function stops.
81 bool auto_was_running_ = false;
Sabina Davis91b23602019-01-21 00:06:01 -080082 ::std::unique_ptr<DrivetrainInputReader> drivetrain_input_reader_;
83 ::aos::common::actions::ActionQueue action_queue_;
Austin Schuh1bf8a212019-05-26 22:13:14 -070084
85 ::frc971::autonomous::BaseAutonomousActor::Factory autonomous_action_factory_;
Sabina Davis91b23602019-01-21 00:06:01 -080086};
87
88} // namespace input
89} // namespace aos
90
91#endif // AOS_INPUT_ACTION_JOYSTICK_INPUT_H_