Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <string.h> |
| 3 | #include <unistd.h> |
| 4 | #include <math.h> |
| 5 | |
John Park | 398c74a | 2018-10-20 21:17:39 -0700 | [diff] [blame] | 6 | #include "aos/init.h" |
Brian Silverman | c206573 | 2015-11-28 22:55:30 +0000 | [diff] [blame] | 7 | #include "aos/input/joystick_input.h" |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 8 | #include "aos/input/driver_station_data.h" |
| 9 | #include "aos/logging/logging.h" |
| 10 | #include "aos/util/log_interval.h" |
| 11 | #include "aos/time/time.h" |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 12 | |
Austin Schuh | 0b56015 | 2019-01-04 17:02:27 -0800 | [diff] [blame] | 13 | #include "aos/input/drivetrain_input.h" |
| 14 | #include "frc971/autonomous/auto.q.h" |
| 15 | #include "frc971/autonomous/base_autonomous_actor.h" |
Adam Snaider | 83eae56 | 2016-09-10 16:47:33 -0700 | [diff] [blame] | 16 | #include "frc971/control_loops/drivetrain/drivetrain.q.h" |
Austin Schuh | 0b56015 | 2019-01-04 17:02:27 -0800 | [diff] [blame] | 17 | #include "frc971/queues/gyro.q.h" |
| 18 | #include "y2014_bot3/control_loops/drivetrain/drivetrain_base.h" |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 19 | #include "y2014_bot3/control_loops/rollers/rollers.q.h" |
| 20 | |
Adam Snaider | 83eae56 | 2016-09-10 16:47:33 -0700 | [diff] [blame] | 21 | using ::frc971::control_loops::drivetrain_queue; |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 22 | using ::y2014_bot3::control_loops::rollers_queue; |
| 23 | using ::frc971::sensors::gyro_reading; |
| 24 | |
| 25 | using ::aos::input::driver_station::ButtonLocation; |
| 26 | using ::aos::input::driver_station::POVLocation; |
| 27 | using ::aos::input::driver_station::JoystickAxis; |
| 28 | using ::aos::input::driver_station::ControlBit; |
Austin Schuh | 0b56015 | 2019-01-04 17:02:27 -0800 | [diff] [blame] | 29 | using ::aos::input::DrivetrainInputReader; |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 30 | |
| 31 | namespace y2014_bot3 { |
| 32 | namespace input { |
| 33 | namespace joysticks { |
| 34 | |
| 35 | // Joystick & button addresses. |
| 36 | const JoystickAxis kSteeringWheel(1, 1), kDriveThrottle(2, 2); |
Adam Snaider | 83eae56 | 2016-09-10 16:47:33 -0700 | [diff] [blame] | 37 | const ButtonLocation kShiftHigh(2, 3), kShiftHigh2(2, 2), kShiftLow(2, 1); |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 38 | const ButtonLocation kQuickTurn(1, 5); |
| 39 | |
Adam Snaider | 83eae56 | 2016-09-10 16:47:33 -0700 | [diff] [blame] | 40 | const ButtonLocation kTurn1(1, 7); |
| 41 | const ButtonLocation kTurn2(1, 11); |
| 42 | |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 43 | const ButtonLocation kFrontRollersIn(3, 8); |
| 44 | const ButtonLocation kBackRollersIn(3, 7); |
| 45 | const ButtonLocation kFrontRollersOut(3, 6); |
| 46 | const ButtonLocation kBackRollersOut(4, 12); |
| 47 | const ButtonLocation kHumanPlayer(4, 11); |
| 48 | |
| 49 | class Reader : public ::aos::input::JoystickInput { |
| 50 | public: |
Austin Schuh | 3e45c75 | 2019-02-02 12:19:11 -0800 | [diff] [blame] | 51 | Reader(::aos::EventLoop *event_loop) |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame^] | 52 | : ::aos::input::JoystickInput(event_loop), |
| 53 | autonomous_action_factory_( |
| 54 | ::frc971::autonomous::BaseAutonomousActor::MakeFactory( |
| 55 | event_loop)) { |
Austin Schuh | 0b56015 | 2019-01-04 17:02:27 -0800 | [diff] [blame] | 56 | drivetrain_input_reader_ = DrivetrainInputReader::Make( |
| 57 | DrivetrainInputReader::InputType::kSteeringWheel, |
| 58 | ::y2014_bot3::control_loops::drivetrain::GetDrivetrainConfig()); |
| 59 | } |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 60 | |
| 61 | virtual void RunIteration(const ::aos::input::driver_station::Data &data) { |
| 62 | bool last_auto_running = auto_running_; |
| 63 | auto_running_ = data.GetControlBit(ControlBit::kAutonomous) && |
| 64 | data.GetControlBit(ControlBit::kEnabled); |
| 65 | if (auto_running_ != last_auto_running) { |
| 66 | if (auto_running_) { |
| 67 | StartAuto(); |
| 68 | } else { |
| 69 | StopAuto(); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | if (!data.GetControlBit(ControlBit::kAutonomous)) { |
| 74 | HandleDrivetrain(data); |
| 75 | HandleTeleop(data); |
| 76 | } |
Austin Schuh | 0b56015 | 2019-01-04 17:02:27 -0800 | [diff] [blame] | 77 | |
| 78 | action_queue_.Tick(); |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | void HandleDrivetrain(const ::aos::input::driver_station::Data &data) { |
Austin Schuh | 0b56015 | 2019-01-04 17:02:27 -0800 | [diff] [blame] | 82 | drivetrain_input_reader_->HandleDrivetrain(data); |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | void HandleTeleop(const ::aos::input::driver_station::Data &data) { |
Austin Schuh | 0b56015 | 2019-01-04 17:02:27 -0800 | [diff] [blame] | 86 | if (!data.GetControlBit(ControlBit::kEnabled)) { |
| 87 | action_queue_.CancelAllActions(); |
| 88 | LOG(DEBUG, "Canceling\n"); |
| 89 | } |
| 90 | |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 91 | // Rollers. |
| 92 | auto rollers_goal = control_loops::rollers_queue.goal.MakeMessage(); |
| 93 | rollers_goal->Zero(); |
| 94 | if (data.IsPressed(kFrontRollersIn)) { |
| 95 | rollers_goal->intake = 1; |
| 96 | } else if (data.IsPressed(kFrontRollersOut)) { |
| 97 | rollers_goal->low_spit = 1; |
| 98 | } else if (data.IsPressed(kBackRollersIn)) { |
| 99 | rollers_goal->intake = -1; |
| 100 | } else if (data.IsPressed(kBackRollersOut)) { |
| 101 | rollers_goal->low_spit = -1; |
| 102 | } else if (data.IsPressed(kHumanPlayer)) { |
| 103 | rollers_goal->human_player = true; |
| 104 | } |
| 105 | if (!rollers_goal.Send()) { |
| 106 | LOG(WARNING, "Sending rollers values failed.\n"); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | private: |
| 111 | void StartAuto() { |
| 112 | LOG(INFO, "Starting auto mode.\n"); |
Austin Schuh | 0b56015 | 2019-01-04 17:02:27 -0800 | [diff] [blame] | 113 | ::frc971::autonomous::AutonomousActionParams params; |
| 114 | params.mode = 0; |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame^] | 115 | action_queue_.EnqueueAction(autonomous_action_factory_.Make(params)); |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | void StopAuto() { |
| 119 | LOG(INFO, "Stopping auto mode\n"); |
Austin Schuh | 0b56015 | 2019-01-04 17:02:27 -0800 | [diff] [blame] | 120 | action_queue_.CancelAllActions(); |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | bool auto_running_ = false; |
| 124 | |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 125 | ::aos::util::SimpleLogInterval no_drivetrain_status_ = |
Austin Schuh | 61bdc60 | 2016-12-04 19:10:10 -0800 | [diff] [blame] | 126 | ::aos::util::SimpleLogInterval(::std::chrono::milliseconds(200), WARNING, |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 127 | "no drivetrain status"); |
Austin Schuh | 0b56015 | 2019-01-04 17:02:27 -0800 | [diff] [blame] | 128 | |
| 129 | ::aos::common::actions::ActionQueue action_queue_; |
| 130 | |
| 131 | ::std::unique_ptr<DrivetrainInputReader> drivetrain_input_reader_; |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame^] | 132 | |
| 133 | ::frc971::autonomous::BaseAutonomousActor::Factory autonomous_action_factory_; |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 134 | }; |
| 135 | |
| 136 | } // namespace joysticks |
| 137 | } // namespace input |
| 138 | } // namespace y2014_bot3 |
| 139 | |
| 140 | int main() { |
Brian Silverman | 5090c43 | 2016-01-02 14:44:26 -0800 | [diff] [blame] | 141 | ::aos::Init(-1); |
Austin Schuh | 3e45c75 | 2019-02-02 12:19:11 -0800 | [diff] [blame] | 142 | ::aos::ShmEventLoop event_loop; |
| 143 | ::y2014_bot3::input::joysticks::Reader reader(&event_loop); |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 144 | reader.Run(); |
| 145 | ::aos::Cleanup(); |
| 146 | } |