blob: 0046069148a1af67dae14a74c757bfce78db0a3f [file] [log] [blame]
Brian Silvermanc71537c2016-01-01 13:43:14 -08001#include <stdio.h>
2#include <string.h>
3#include <unistd.h>
4#include <math.h>
5
6#include "aos/linux_code/init.h"
7#include "aos/input/joystick_input.h"
8#include "aos/common/input/driver_station_data.h"
9#include "aos/common/logging/logging.h"
10#include "aos/common/time.h"
11#include "aos/common/actions/actions.h"
12
13#include "y2012/control_loops/drivetrain/drivetrain.q.h"
14#include "y2012/control_loops/accessories/accessories.q.h"
15
16using ::y2012::control_loops::drivetrain_queue;
17using ::y2012::control_loops::accessories_queue;
18
19using ::aos::input::driver_station::ButtonLocation;
20using ::aos::input::driver_station::JoystickAxis;
21using ::aos::input::driver_station::ControlBit;
22
23#define OLD_DS 0
24
25namespace y2012 {
26namespace input {
27namespace joysticks {
28
29const JoystickAxis kSteeringWheel(1, 1), kDriveThrottle(2, 2);
30const ButtonLocation kShiftHigh(2, 1), kShiftLow(2, 3);
31const ButtonLocation kQuickTurn(1, 5);
32
33const ButtonLocation kCatch(3, 10);
34
35#if OLD_DS
36const ButtonLocation kFire(3, 11);
37const ButtonLocation kUnload(1, 4);
38const ButtonLocation kReload(1, 2);
39
40const ButtonLocation kRollersOut(3, 12);
41const ButtonLocation kRollersIn(3, 7);
42
43const ButtonLocation kTuck(3, 9);
44const ButtonLocation kIntakePosition(3, 8);
45const ButtonLocation kIntakeOpenPosition(3, 10);
46const ButtonLocation kVerticalTuck(3, 1);
47const JoystickAxis kFlipRobot(3, 3);
48
49const ButtonLocation kLongShot(3, 5);
50const ButtonLocation kCloseShot(3, 2);
51const ButtonLocation kFenderShot(3, 3);
52const ButtonLocation kTrussShot(2, 11);
53const ButtonLocation kHumanPlayerShot(3, 2);
54#else
55const ButtonLocation kFire(3, 9);
56const ButtonLocation kUnload(1, 4);
57const ButtonLocation kReload(1, 2);
58
59const ButtonLocation kRollersOut(3, 8);
60const ButtonLocation kRollersIn(3, 3);
61
62const ButtonLocation kTuck(3, 4);
63const ButtonLocation kIntakePosition(3, 5);
64const ButtonLocation kIntakeOpenPosition(3, 11);
65const ButtonLocation kVerticalTuck(2, 6);
66const JoystickAxis kFlipRobot(3, 3);
67
68const ButtonLocation kLongShot(3, 7);
69const ButtonLocation kCloseShot(3, 6);
70const ButtonLocation kFenderShot(3, 2);
71const ButtonLocation kTrussShot(2, 11);
72const ButtonLocation kHumanPlayerShot(3, 1);
73#endif
74
75const ButtonLocation kUserLeft(2, 7);
76const ButtonLocation kUserRight(2, 10);
77
78const JoystickAxis kAdjustClawGoal(3, 2);
79const JoystickAxis kAdjustClawSeparation(3, 1);
80
81class Reader : public ::aos::input::JoystickInput {
82 public:
83 Reader()
84 : is_high_gear_(false) {}
85
86 void RunIteration(const ::aos::input::driver_station::Data &data) override {
87 if (!data.GetControlBit(ControlBit::kAutonomous)) {
88 HandleDrivetrain(data);
89 HandleTeleop(data);
90 }
91 }
92
93 void HandleDrivetrain(const ::aos::input::driver_station::Data &data) {
94 const double wheel = -data.GetAxis(kSteeringWheel);
95 const double throttle = -data.GetAxis(kDriveThrottle);
96 if (data.PosEdge(kShiftHigh)) {
97 is_high_gear_ = false;
98 }
99 if (data.PosEdge(kShiftLow)) {
100 is_high_gear_ = true;
101 }
102 if (!drivetrain_queue.goal.MakeWithBuilder()
103 .steering(wheel)
104 .throttle(throttle)
105 .highgear(is_high_gear_)
106 .quickturn(data.IsPressed(kQuickTurn))
107 .left_velocity_goal(0)
108 .right_velocity_goal(0)
109 .Send()) {
110 LOG(WARNING, "sending stick values failed\n");
111 }
112 }
113
114 void HandleTeleop(const ::aos::input::driver_station::Data &data) {
115 if (!data.GetControlBit(ControlBit::kEnabled)) {
116 action_queue_.CancelAllActions();
117 LOG(DEBUG, "Canceling\n");
118 }
119
120 {
121 auto accessories_message = accessories_queue.goal.MakeMessage();
122 accessories_message->solenoids[0] = data.IsPressed(kLongShot);
123 accessories_message->solenoids[1] = data.IsPressed(kCloseShot);
124 accessories_message->solenoids[2] = data.IsPressed(kFenderShot);
125 accessories_message->sticks[0] = data.GetAxis(kAdjustClawGoal);
126 accessories_message->sticks[1] = data.GetAxis(kAdjustClawSeparation);
127 if (!accessories_message.Send()) {
128 LOG(WARNING, "sending accessories goal failed\n");
129 }
130 }
131
132 action_queue_.Tick();
133 }
134
135 private:
136 bool is_high_gear_;
137
138 ::aos::common::actions::ActionQueue action_queue_;
139};
140
141} // namespace joysticks
142} // namespace input
143} // namespace y2012
144
145int main() {
146 ::aos::Init();
147 ::y2012::input::joysticks::Reader reader;
148 reader.Run();
149 ::aos::Cleanup();
150}