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