blob: 6736329fd6899f6e839711826066d3080c214691 [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
13#include "frc971/queues/gyro.q.h"
Adam Snaider83eae562016-09-10 16:47:33 -070014#include "frc971/control_loops/drivetrain/drivetrain.q.h"
Comran Morshed41ed7c22015-11-04 21:03:37 +000015#include "y2014_bot3/autonomous/auto.q.h"
Comran Morshed41ed7c22015-11-04 21:03:37 +000016#include "y2014_bot3/control_loops/rollers/rollers.q.h"
17
Adam Snaider83eae562016-09-10 16:47:33 -070018using ::frc971::control_loops::drivetrain_queue;
Comran Morshed41ed7c22015-11-04 21:03:37 +000019using ::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);
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:
47 Reader() : is_high_gear_(false) {}
48
49 virtual void RunIteration(const ::aos::input::driver_station::Data &data) {
50 bool last_auto_running = auto_running_;
51 auto_running_ = data.GetControlBit(ControlBit::kAutonomous) &&
52 data.GetControlBit(ControlBit::kEnabled);
53 if (auto_running_ != last_auto_running) {
54 if (auto_running_) {
55 StartAuto();
56 } else {
57 StopAuto();
58 }
59 }
60
61 if (!data.GetControlBit(ControlBit::kAutonomous)) {
62 HandleDrivetrain(data);
63 HandleTeleop(data);
64 }
65 }
66
67 void HandleDrivetrain(const ::aos::input::driver_station::Data &data) {
Adam Snaider83eae562016-09-10 16:47:33 -070068 bool is_control_loop_driving = false;
Comran Morshed41ed7c22015-11-04 21:03:37 +000069 const double wheel = -data.GetAxis(kSteeringWheel);
70 const double throttle = -data.GetAxis(kDriveThrottle);
71
Adam Snaider83eae562016-09-10 16:47:33 -070072 if (data.PosEdge(kTurn1) || data.PosEdge(kTurn2)) {
73 drivetrain_queue.status.FetchLatest();
74 if (drivetrain_queue.status.get()) {
Adam Snaider418bd822016-11-26 14:49:23 -080075 left_goal_ = drivetrain_queue.status->estimated_left_position;
76 right_goal_ = drivetrain_queue.status->estimated_right_position;
Adam Snaider83eae562016-09-10 16:47:33 -070077 }
78 }
79 if (data.IsPressed(kTurn1) || data.IsPressed(kTurn2)) {
80 is_control_loop_driving = true;
81 }
82
Comran Morshed41ed7c22015-11-04 21:03:37 +000083 if (!drivetrain_queue.goal.MakeWithBuilder()
Austin Schuh2b1fce02018-03-02 20:05:20 -080084 .wheel(wheel)
Comran Morshed41ed7c22015-11-04 21:03:37 +000085 .throttle(throttle)
Comran Morshed41ed7c22015-11-04 21:03:37 +000086 .highgear(is_high_gear_)
Adam Snaider83eae562016-09-10 16:47:33 -070087 .quickturn(data.IsPressed(kQuickTurn))
88 .control_loop_driving(is_control_loop_driving)
Adam Snaider418bd822016-11-26 14:49:23 -080089 .left_goal(left_goal_ - wheel * 0.5 + throttle * 0.3)
90 .right_goal(right_goal_ + wheel * 0.5 + throttle * 0.3)
Adam Snaider83eae562016-09-10 16:47:33 -070091 .left_velocity_goal(0)
92 .right_velocity_goal(0)
Comran Morshed41ed7c22015-11-04 21:03:37 +000093 .Send()) {
94 LOG(WARNING, "sending stick values failed\n");
95 }
96
Campbell Crowley5b27f022016-02-20 16:55:35 -080097 if (data.PosEdge(kShiftLow)) {
Comran Morshed41ed7c22015-11-04 21:03:37 +000098 is_high_gear_ = false;
99 }
100
Adam Snaider83eae562016-09-10 16:47:33 -0700101 if (data.PosEdge(kShiftHigh) || data.PosEdge(kShiftHigh2)) {
Comran Morshed41ed7c22015-11-04 21:03:37 +0000102 is_high_gear_ = true;
103 }
104 }
105
106 void HandleTeleop(const ::aos::input::driver_station::Data &data) {
107 // Rollers.
108 auto rollers_goal = control_loops::rollers_queue.goal.MakeMessage();
109 rollers_goal->Zero();
110 if (data.IsPressed(kFrontRollersIn)) {
111 rollers_goal->intake = 1;
112 } else if (data.IsPressed(kFrontRollersOut)) {
113 rollers_goal->low_spit = 1;
114 } else if (data.IsPressed(kBackRollersIn)) {
115 rollers_goal->intake = -1;
116 } else if (data.IsPressed(kBackRollersOut)) {
117 rollers_goal->low_spit = -1;
118 } else if (data.IsPressed(kHumanPlayer)) {
119 rollers_goal->human_player = true;
120 }
121 if (!rollers_goal.Send()) {
122 LOG(WARNING, "Sending rollers values failed.\n");
123 }
124 }
125
126 private:
127 void StartAuto() {
128 LOG(INFO, "Starting auto mode.\n");
129 ::y2014_bot3::autonomous::autonomous.MakeWithBuilder().run_auto(true).Send();
130 }
131
132 void StopAuto() {
133 LOG(INFO, "Stopping auto mode\n");
134 ::y2014_bot3::autonomous::autonomous.MakeWithBuilder().run_auto(false).Send();
135 }
136
137 bool auto_running_ = false;
138
139 bool is_high_gear_;
Adam Snaidera3271fe2016-10-26 21:03:38 -0700140 // Turning goals.
Adam Snaider418bd822016-11-26 14:49:23 -0800141 double left_goal_;
142 double right_goal_;
Comran Morshed41ed7c22015-11-04 21:03:37 +0000143
144 ::aos::util::SimpleLogInterval no_drivetrain_status_ =
Austin Schuh61bdc602016-12-04 19:10:10 -0800145 ::aos::util::SimpleLogInterval(::std::chrono::milliseconds(200), WARNING,
Comran Morshed41ed7c22015-11-04 21:03:37 +0000146 "no drivetrain status");
147};
148
149} // namespace joysticks
150} // namespace input
151} // namespace y2014_bot3
152
153int main() {
Brian Silverman5090c432016-01-02 14:44:26 -0800154 ::aos::Init(-1);
Comran Morshed41ed7c22015-11-04 21:03:37 +0000155 ::y2014_bot3::input::joysticks::Reader reader;
156 reader.Run();
157 ::aos::Cleanup();
158}