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