blob: 847adf79b8a212b93fd653e50d6b9090beb64bb8 [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
Adam Snaider83eae562016-09-10 16:47:33 -070021using ::frc971::control_loops::drivetrain_queue;
Comran Morshed41ed7c22015-11-04 21:03:37 +000022using ::frc971::sensors::gyro_reading;
23
24using ::aos::input::driver_station::ButtonLocation;
25using ::aos::input::driver_station::POVLocation;
26using ::aos::input::driver_station::JoystickAxis;
27using ::aos::input::driver_station::ControlBit;
Austin Schuh0b560152019-01-04 17:02:27 -080028using ::aos::input::DrivetrainInputReader;
Comran Morshed41ed7c22015-11-04 21:03:37 +000029
30namespace y2014_bot3 {
31namespace input {
32namespace joysticks {
33
34// Joystick & button addresses.
35const JoystickAxis kSteeringWheel(1, 1), kDriveThrottle(2, 2);
Adam Snaider83eae562016-09-10 16:47:33 -070036const ButtonLocation kShiftHigh(2, 3), kShiftHigh2(2, 2), kShiftLow(2, 1);
Comran Morshed41ed7c22015-11-04 21:03:37 +000037const ButtonLocation kQuickTurn(1, 5);
38
Adam Snaider83eae562016-09-10 16:47:33 -070039const ButtonLocation kTurn1(1, 7);
40const ButtonLocation kTurn2(1, 11);
41
Comran Morshed41ed7c22015-11-04 21:03:37 +000042const ButtonLocation kFrontRollersIn(3, 8);
43const ButtonLocation kBackRollersIn(3, 7);
44const ButtonLocation kFrontRollersOut(3, 6);
45const ButtonLocation kBackRollersOut(4, 12);
46const ButtonLocation kHumanPlayer(4, 11);
47
48class Reader : public ::aos::input::JoystickInput {
49 public:
Austin Schuh3e45c752019-02-02 12:19:11 -080050 Reader(::aos::EventLoop *event_loop)
Austin Schuh1bf8a212019-05-26 22:13:14 -070051 : ::aos::input::JoystickInput(event_loop),
Austin Schuh30020d92019-05-27 13:07:02 -070052 rollers_goal_sender_(
53 event_loop
54 ->MakeSender<::y2014_bot3::control_loops::RollersQueue::Goal>(
55 ".y2014_bot3.control_loops.rollers_queue.goal")),
Austin Schuh1bf8a212019-05-26 22:13:14 -070056 autonomous_action_factory_(
57 ::frc971::autonomous::BaseAutonomousActor::MakeFactory(
58 event_loop)) {
Austin Schuh0b560152019-01-04 17:02:27 -080059 drivetrain_input_reader_ = DrivetrainInputReader::Make(
60 DrivetrainInputReader::InputType::kSteeringWheel,
61 ::y2014_bot3::control_loops::drivetrain::GetDrivetrainConfig());
62 }
Comran Morshed41ed7c22015-11-04 21:03:37 +000063
64 virtual void RunIteration(const ::aos::input::driver_station::Data &data) {
65 bool last_auto_running = auto_running_;
66 auto_running_ = data.GetControlBit(ControlBit::kAutonomous) &&
67 data.GetControlBit(ControlBit::kEnabled);
68 if (auto_running_ != last_auto_running) {
69 if (auto_running_) {
70 StartAuto();
71 } else {
72 StopAuto();
73 }
74 }
75
76 if (!data.GetControlBit(ControlBit::kAutonomous)) {
77 HandleDrivetrain(data);
78 HandleTeleop(data);
79 }
Austin Schuh0b560152019-01-04 17:02:27 -080080
81 action_queue_.Tick();
Comran Morshed41ed7c22015-11-04 21:03:37 +000082 }
83
84 void HandleDrivetrain(const ::aos::input::driver_station::Data &data) {
Austin Schuh0b560152019-01-04 17:02:27 -080085 drivetrain_input_reader_->HandleDrivetrain(data);
Comran Morshed41ed7c22015-11-04 21:03:37 +000086 }
87
88 void HandleTeleop(const ::aos::input::driver_station::Data &data) {
Austin Schuh0b560152019-01-04 17:02:27 -080089 if (!data.GetControlBit(ControlBit::kEnabled)) {
90 action_queue_.CancelAllActions();
91 LOG(DEBUG, "Canceling\n");
92 }
93
Comran Morshed41ed7c22015-11-04 21:03:37 +000094 // Rollers.
Austin Schuh30020d92019-05-27 13:07:02 -070095 auto rollers_goal = rollers_goal_sender_.MakeMessage();
Comran Morshed41ed7c22015-11-04 21:03:37 +000096 rollers_goal->Zero();
97 if (data.IsPressed(kFrontRollersIn)) {
98 rollers_goal->intake = 1;
99 } else if (data.IsPressed(kFrontRollersOut)) {
100 rollers_goal->low_spit = 1;
101 } else if (data.IsPressed(kBackRollersIn)) {
102 rollers_goal->intake = -1;
103 } else if (data.IsPressed(kBackRollersOut)) {
104 rollers_goal->low_spit = -1;
105 } else if (data.IsPressed(kHumanPlayer)) {
106 rollers_goal->human_player = true;
107 }
108 if (!rollers_goal.Send()) {
109 LOG(WARNING, "Sending rollers values failed.\n");
110 }
111 }
112
113 private:
114 void StartAuto() {
115 LOG(INFO, "Starting auto mode.\n");
Austin Schuh0b560152019-01-04 17:02:27 -0800116 ::frc971::autonomous::AutonomousActionParams params;
117 params.mode = 0;
Austin Schuh1bf8a212019-05-26 22:13:14 -0700118 action_queue_.EnqueueAction(autonomous_action_factory_.Make(params));
Comran Morshed41ed7c22015-11-04 21:03:37 +0000119 }
120
121 void StopAuto() {
122 LOG(INFO, "Stopping auto mode\n");
Austin Schuh0b560152019-01-04 17:02:27 -0800123 action_queue_.CancelAllActions();
Comran Morshed41ed7c22015-11-04 21:03:37 +0000124 }
125
126 bool auto_running_ = false;
127
Comran Morshed41ed7c22015-11-04 21:03:37 +0000128 ::aos::util::SimpleLogInterval no_drivetrain_status_ =
Austin Schuh61bdc602016-12-04 19:10:10 -0800129 ::aos::util::SimpleLogInterval(::std::chrono::milliseconds(200), WARNING,
Comran Morshed41ed7c22015-11-04 21:03:37 +0000130 "no drivetrain status");
Austin Schuh0b560152019-01-04 17:02:27 -0800131
132 ::aos::common::actions::ActionQueue action_queue_;
133
134 ::std::unique_ptr<DrivetrainInputReader> drivetrain_input_reader_;
Austin Schuh30020d92019-05-27 13:07:02 -0700135 ::aos::Sender<::y2014_bot3::control_loops::RollersQueue::Goal>
136 rollers_goal_sender_;
Austin Schuh1bf8a212019-05-26 22:13:14 -0700137
138 ::frc971::autonomous::BaseAutonomousActor::Factory autonomous_action_factory_;
Comran Morshed41ed7c22015-11-04 21:03:37 +0000139};
140
141} // namespace joysticks
142} // namespace input
143} // namespace y2014_bot3
144
145int main() {
Brian Silverman5090c432016-01-02 14:44:26 -0800146 ::aos::Init(-1);
Austin Schuh3e45c752019-02-02 12:19:11 -0800147 ::aos::ShmEventLoop event_loop;
148 ::y2014_bot3::input::joysticks::Reader reader(&event_loop);
Comran Morshed41ed7c22015-11-04 21:03:37 +0000149 reader.Run();
150 ::aos::Cleanup();
151}