blob: 3e6e3e2c9bf57d4cfdb3a81865019a8f39dd5543 [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
6#include "aos/linux_code/init.h"
Brian Silvermanc2065732015-11-28 22:55:30 +00007#include "aos/input/joystick_input.h"
Comran Morshed41ed7c22015-11-04 21:03:37 +00008#include "aos/common/input/driver_station_data.h"
9#include "aos/common/logging/logging.h"
10#include "aos/common/util/log_interval.h"
11#include "aos/common/time.h"
12
13#include "frc971/queues/gyro.q.h"
14#include "y2014_bot3/autonomous/auto.q.h"
15#include "y2014_bot3/control_loops/drivetrain/drivetrain.q.h"
16#include "y2014_bot3/control_loops/rollers/rollers.q.h"
17
18using ::y2014_bot3::control_loops::drivetrain_queue;
19using ::y2014_bot3::control_loops::rollers_queue;
20using ::frc971::sensors::gyro_reading;
21
22using ::aos::input::driver_station::ButtonLocation;
23using ::aos::input::driver_station::POVLocation;
24using ::aos::input::driver_station::JoystickAxis;
25using ::aos::input::driver_station::ControlBit;
26
27namespace y2014_bot3 {
28namespace input {
29namespace joysticks {
30
31// Joystick & button addresses.
32const JoystickAxis kSteeringWheel(1, 1), kDriveThrottle(2, 2);
33const ButtonLocation kShiftHigh(2, 1), kShiftLow(2, 3);
34const ButtonLocation kQuickTurn(1, 5);
35
36const ButtonLocation kFrontRollersIn(3, 8);
37const ButtonLocation kBackRollersIn(3, 7);
38const ButtonLocation kFrontRollersOut(3, 6);
39const ButtonLocation kBackRollersOut(4, 12);
40const ButtonLocation kHumanPlayer(4, 11);
41
42class Reader : public ::aos::input::JoystickInput {
43 public:
44 Reader() : is_high_gear_(false) {}
45
46 virtual void RunIteration(const ::aos::input::driver_station::Data &data) {
47 bool last_auto_running = auto_running_;
48 auto_running_ = data.GetControlBit(ControlBit::kAutonomous) &&
49 data.GetControlBit(ControlBit::kEnabled);
50 if (auto_running_ != last_auto_running) {
51 if (auto_running_) {
52 StartAuto();
53 } else {
54 StopAuto();
55 }
56 }
57
58 if (!data.GetControlBit(ControlBit::kAutonomous)) {
59 HandleDrivetrain(data);
60 HandleTeleop(data);
61 }
62 }
63
64 void HandleDrivetrain(const ::aos::input::driver_station::Data &data) {
65 const double wheel = -data.GetAxis(kSteeringWheel);
66 const double throttle = -data.GetAxis(kDriveThrottle);
67
68 if (!drivetrain_queue.goal.MakeWithBuilder()
69 .steering(wheel)
70 .throttle(throttle)
71 .quickturn(data.IsPressed(kQuickTurn))
72 .control_loop_driving(false)
73 .highgear(is_high_gear_)
74 .Send()) {
75 LOG(WARNING, "sending stick values failed\n");
76 }
77
78 if (data.PosEdge(kShiftHigh)) {
79 is_high_gear_ = false;
80 }
81
82 if (data.PosEdge(kShiftLow)) {
83 is_high_gear_ = true;
84 }
85 }
86
87 void HandleTeleop(const ::aos::input::driver_station::Data &data) {
88 // Rollers.
89 auto rollers_goal = control_loops::rollers_queue.goal.MakeMessage();
90 rollers_goal->Zero();
91 if (data.IsPressed(kFrontRollersIn)) {
92 rollers_goal->intake = 1;
93 } else if (data.IsPressed(kFrontRollersOut)) {
94 rollers_goal->low_spit = 1;
95 } else if (data.IsPressed(kBackRollersIn)) {
96 rollers_goal->intake = -1;
97 } else if (data.IsPressed(kBackRollersOut)) {
98 rollers_goal->low_spit = -1;
99 } else if (data.IsPressed(kHumanPlayer)) {
100 rollers_goal->human_player = true;
101 }
102 if (!rollers_goal.Send()) {
103 LOG(WARNING, "Sending rollers values failed.\n");
104 }
105 }
106
107 private:
108 void StartAuto() {
109 LOG(INFO, "Starting auto mode.\n");
110 ::y2014_bot3::autonomous::autonomous.MakeWithBuilder().run_auto(true).Send();
111 }
112
113 void StopAuto() {
114 LOG(INFO, "Stopping auto mode\n");
115 ::y2014_bot3::autonomous::autonomous.MakeWithBuilder().run_auto(false).Send();
116 }
117
118 bool auto_running_ = false;
119
120 bool is_high_gear_;
121
122 ::aos::util::SimpleLogInterval no_drivetrain_status_ =
123 ::aos::util::SimpleLogInterval(::aos::time::Time::InSeconds(0.2), WARNING,
124 "no drivetrain status");
125};
126
127} // namespace joysticks
128} // namespace input
129} // namespace y2014_bot3
130
131int main() {
Brian Silverman5090c432016-01-02 14:44:26 -0800132 ::aos::Init(-1);
Comran Morshed41ed7c22015-11-04 21:03:37 +0000133 ::y2014_bot3::input::joysticks::Reader reader;
134 reader.Run();
135 ::aos::Cleanup();
136}