blob: 989a1c145d68624a564d1c97f757ba01ace9e2bb [file] [log] [blame]
Comran Morshed41ed7c22015-11-04 21:03:37 +00001#include <stdio.h>
2#include <string.h>
3#include <unistd.h>
4#include <math.h>
5
John Park398c74a2018-10-20 21:17:39 -07006#include "aos/init.h"
Brian Silvermanc2065732015-11-28 22:55:30 +00007#include "aos/input/joystick_input.h"
John Park33858a32018-09-28 23:05:48 -07008#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 Morshed41ed7c22015-11-04 21:03:37 +000012
Austin Schuh0b560152019-01-04 17:02:27 -080013#include "aos/input/drivetrain_input.h"
14#include "frc971/autonomous/auto.q.h"
15#include "frc971/autonomous/base_autonomous_actor.h"
Adam Snaider83eae562016-09-10 16:47:33 -070016#include "frc971/control_loops/drivetrain/drivetrain.q.h"
Austin Schuh0b560152019-01-04 17:02:27 -080017#include "frc971/queues/gyro.q.h"
18#include "y2014_bot3/control_loops/drivetrain/drivetrain_base.h"
Comran Morshed41ed7c22015-11-04 21:03:37 +000019#include "y2014_bot3/control_loops/rollers/rollers.q.h"
20
Comran Morshed41ed7c22015-11-04 21:03:37 +000021using ::aos::input::driver_station::ButtonLocation;
22using ::aos::input::driver_station::POVLocation;
23using ::aos::input::driver_station::JoystickAxis;
24using ::aos::input::driver_station::ControlBit;
Austin Schuh0b560152019-01-04 17:02:27 -080025using ::aos::input::DrivetrainInputReader;
Comran Morshed41ed7c22015-11-04 21:03:37 +000026
27namespace y2014_bot3 {
28namespace input {
29namespace joysticks {
30
31// Joystick & button addresses.
32const JoystickAxis kSteeringWheel(1, 1), kDriveThrottle(2, 2);
Adam Snaider83eae562016-09-10 16:47:33 -070033const ButtonLocation kShiftHigh(2, 3), kShiftHigh2(2, 2), kShiftLow(2, 1);
Comran Morshed41ed7c22015-11-04 21:03:37 +000034const ButtonLocation kQuickTurn(1, 5);
35
Adam Snaider83eae562016-09-10 16:47:33 -070036const ButtonLocation kTurn1(1, 7);
37const ButtonLocation kTurn2(1, 11);
38
Comran Morshed41ed7c22015-11-04 21:03:37 +000039const ButtonLocation kFrontRollersIn(3, 8);
40const ButtonLocation kBackRollersIn(3, 7);
41const ButtonLocation kFrontRollersOut(3, 6);
42const ButtonLocation kBackRollersOut(4, 12);
43const ButtonLocation kHumanPlayer(4, 11);
44
45class Reader : public ::aos::input::JoystickInput {
46 public:
Austin Schuh3e45c752019-02-02 12:19:11 -080047 Reader(::aos::EventLoop *event_loop)
Austin Schuh1bf8a212019-05-26 22:13:14 -070048 : ::aos::input::JoystickInput(event_loop),
Austin Schuh30020d92019-05-27 13:07:02 -070049 rollers_goal_sender_(
50 event_loop
51 ->MakeSender<::y2014_bot3::control_loops::RollersQueue::Goal>(
52 ".y2014_bot3.control_loops.rollers_queue.goal")),
Austin Schuh1bf8a212019-05-26 22:13:14 -070053 autonomous_action_factory_(
54 ::frc971::autonomous::BaseAutonomousActor::MakeFactory(
55 event_loop)) {
Austin Schuh0b560152019-01-04 17:02:27 -080056 drivetrain_input_reader_ = DrivetrainInputReader::Make(
Austin Schuhbd0a40f2019-06-30 14:56:31 -070057 event_loop, DrivetrainInputReader::InputType::kSteeringWheel,
Austin Schuh0b560152019-01-04 17:02:27 -080058 ::y2014_bot3::control_loops::drivetrain::GetDrivetrainConfig());
59 }
Comran Morshed41ed7c22015-11-04 21:03:37 +000060
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 Schuh0b560152019-01-04 17:02:27 -080077
78 action_queue_.Tick();
Comran Morshed41ed7c22015-11-04 21:03:37 +000079 }
80
81 void HandleDrivetrain(const ::aos::input::driver_station::Data &data) {
Austin Schuh0b560152019-01-04 17:02:27 -080082 drivetrain_input_reader_->HandleDrivetrain(data);
Comran Morshed41ed7c22015-11-04 21:03:37 +000083 }
84
85 void HandleTeleop(const ::aos::input::driver_station::Data &data) {
Austin Schuh0b560152019-01-04 17:02:27 -080086 if (!data.GetControlBit(ControlBit::kEnabled)) {
87 action_queue_.CancelAllActions();
88 LOG(DEBUG, "Canceling\n");
89 }
90
Comran Morshed41ed7c22015-11-04 21:03:37 +000091 // Rollers.
Austin Schuh30020d92019-05-27 13:07:02 -070092 auto rollers_goal = rollers_goal_sender_.MakeMessage();
Comran Morshed41ed7c22015-11-04 21:03:37 +000093 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 Schuh0b560152019-01-04 17:02:27 -0800113 ::frc971::autonomous::AutonomousActionParams params;
114 params.mode = 0;
Austin Schuh1bf8a212019-05-26 22:13:14 -0700115 action_queue_.EnqueueAction(autonomous_action_factory_.Make(params));
Comran Morshed41ed7c22015-11-04 21:03:37 +0000116 }
117
118 void StopAuto() {
119 LOG(INFO, "Stopping auto mode\n");
Austin Schuh0b560152019-01-04 17:02:27 -0800120 action_queue_.CancelAllActions();
Comran Morshed41ed7c22015-11-04 21:03:37 +0000121 }
122
123 bool auto_running_ = false;
124
Comran Morshed41ed7c22015-11-04 21:03:37 +0000125 ::aos::util::SimpleLogInterval no_drivetrain_status_ =
Austin Schuh61bdc602016-12-04 19:10:10 -0800126 ::aos::util::SimpleLogInterval(::std::chrono::milliseconds(200), WARNING,
Comran Morshed41ed7c22015-11-04 21:03:37 +0000127 "no drivetrain status");
Austin Schuh0b560152019-01-04 17:02:27 -0800128
129 ::aos::common::actions::ActionQueue action_queue_;
130
131 ::std::unique_ptr<DrivetrainInputReader> drivetrain_input_reader_;
Austin Schuh30020d92019-05-27 13:07:02 -0700132 ::aos::Sender<::y2014_bot3::control_loops::RollersQueue::Goal>
133 rollers_goal_sender_;
Austin Schuh1bf8a212019-05-26 22:13:14 -0700134
135 ::frc971::autonomous::BaseAutonomousActor::Factory autonomous_action_factory_;
Comran Morshed41ed7c22015-11-04 21:03:37 +0000136};
137
138} // namespace joysticks
139} // namespace input
140} // namespace y2014_bot3
141
142int main() {
Brian Silverman5090c432016-01-02 14:44:26 -0800143 ::aos::Init(-1);
Austin Schuh3e45c752019-02-02 12:19:11 -0800144 ::aos::ShmEventLoop event_loop;
145 ::y2014_bot3::input::joysticks::Reader reader(&event_loop);
Comran Morshed41ed7c22015-11-04 21:03:37 +0000146 reader.Run();
147 ::aos::Cleanup();
148}