blob: 460d850b72ab830263406cac63446ba2ef4209e2 [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
Austin Schuh3e45c752019-02-02 12:19:11 -08006#include "aos/actions/actions.h"
7#include "aos/events/shm-event-loop.h"
John Park398c74a2018-10-20 21:17:39 -07008#include "aos/init.h"
John Park33858a32018-09-28 23:05:48 -07009#include "aos/input/driver_station_data.h"
Austin Schuh3e45c752019-02-02 12:19:11 -080010#include "aos/input/joystick_input.h"
John Park33858a32018-09-28 23:05:48 -070011#include "aos/logging/logging.h"
12#include "aos/time/time.h"
Brian Silvermanc71537c2016-01-01 13:43:14 -080013
Austin Schuh2a671df2016-11-26 15:00:06 -080014#include "frc971/control_loops/drivetrain/drivetrain.q.h"
Brian Silvermanc71537c2016-01-01 13:43:14 -080015#include "y2012/control_loops/accessories/accessories.q.h"
16
Brian Silvermanc71537c2016-01-01 13:43:14 -080017using ::aos::input::driver_station::ButtonLocation;
18using ::aos::input::driver_station::JoystickAxis;
19using ::aos::input::driver_station::ControlBit;
20
21#define OLD_DS 0
22
23namespace y2012 {
24namespace input {
25namespace joysticks {
26
27const JoystickAxis kSteeringWheel(1, 1), kDriveThrottle(2, 2);
Campbell Crowley5b27f022016-02-20 16:55:35 -080028const ButtonLocation kShiftHigh(2, 3), kShiftLow(2, 1);
Brian Silvermanc71537c2016-01-01 13:43:14 -080029const ButtonLocation kQuickTurn(1, 5);
30
31const ButtonLocation kCatch(3, 10);
32
33#if OLD_DS
34const ButtonLocation kFire(3, 11);
35const ButtonLocation kUnload(1, 4);
36const ButtonLocation kReload(1, 2);
37
38const ButtonLocation kRollersOut(3, 12);
39const ButtonLocation kRollersIn(3, 7);
40
41const ButtonLocation kTuck(3, 9);
42const ButtonLocation kIntakePosition(3, 8);
43const ButtonLocation kIntakeOpenPosition(3, 10);
44const ButtonLocation kVerticalTuck(3, 1);
45const JoystickAxis kFlipRobot(3, 3);
46
47const ButtonLocation kLongShot(3, 5);
48const ButtonLocation kCloseShot(3, 2);
49const ButtonLocation kFenderShot(3, 3);
50const ButtonLocation kTrussShot(2, 11);
51const ButtonLocation kHumanPlayerShot(3, 2);
52#else
53const ButtonLocation kFire(3, 9);
54const ButtonLocation kUnload(1, 4);
55const ButtonLocation kReload(1, 2);
56
57const ButtonLocation kRollersOut(3, 8);
58const ButtonLocation kRollersIn(3, 3);
59
60const ButtonLocation kTuck(3, 4);
61const ButtonLocation kIntakePosition(3, 5);
62const ButtonLocation kIntakeOpenPosition(3, 11);
63const ButtonLocation kVerticalTuck(2, 6);
64const JoystickAxis kFlipRobot(3, 3);
65
66const ButtonLocation kLongShot(3, 7);
67const ButtonLocation kCloseShot(3, 6);
68const ButtonLocation kFenderShot(3, 2);
69const ButtonLocation kTrussShot(2, 11);
70const ButtonLocation kHumanPlayerShot(3, 1);
71#endif
72
73const ButtonLocation kUserLeft(2, 7);
74const ButtonLocation kUserRight(2, 10);
75
76const JoystickAxis kAdjustClawGoal(3, 2);
77const JoystickAxis kAdjustClawSeparation(3, 1);
78
79class Reader : public ::aos::input::JoystickInput {
80 public:
Austin Schuh3e45c752019-02-02 12:19:11 -080081 Reader(::aos::EventLoop *event_loop)
Austin Schuhe4526232019-06-29 16:51:33 -070082 : ::aos::input::JoystickInput(event_loop),
Austin Schuhbd0a40f2019-06-30 14:56:31 -070083 drivetrain_goal_sender_(
84 event_loop
85 ->MakeSender<::frc971::control_loops::DrivetrainQueue::Goal>(
86 ".frc971.control_loops.drivetrain_queue.goal")),
Austin Schuhe4526232019-06-29 16:51:33 -070087 accessories_goal_sender_(
88 event_loop
89 ->MakeSender<::y2012::control_loops::AccessoriesQueue::Message>(
90 ".y2012.control_loops.accessories_queue.goal")),
91 is_high_gear_(false) {}
Brian Silvermanc71537c2016-01-01 13:43:14 -080092
93 void RunIteration(const ::aos::input::driver_station::Data &data) override {
94 if (!data.GetControlBit(ControlBit::kAutonomous)) {
95 HandleDrivetrain(data);
96 HandleTeleop(data);
97 }
98 }
99
100 void HandleDrivetrain(const ::aos::input::driver_station::Data &data) {
101 const double wheel = -data.GetAxis(kSteeringWheel);
102 const double throttle = -data.GetAxis(kDriveThrottle);
Campbell Crowley5b27f022016-02-20 16:55:35 -0800103 if (data.PosEdge(kShiftLow)) {
Brian Silvermanc71537c2016-01-01 13:43:14 -0800104 is_high_gear_ = false;
105 }
Campbell Crowley5b27f022016-02-20 16:55:35 -0800106 if (data.PosEdge(kShiftHigh)) {
Brian Silvermanc71537c2016-01-01 13:43:14 -0800107 is_high_gear_ = true;
108 }
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700109 auto drivetrain_message = drivetrain_goal_sender_.MakeMessage();
110 drivetrain_message->wheel = wheel;
111 drivetrain_message->throttle = throttle;
112 drivetrain_message->highgear = is_high_gear_;
113 drivetrain_message->quickturn = data.IsPressed(kQuickTurn);
114
115 if (!drivetrain_message.Send()) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700116 AOS_LOG(WARNING, "sending stick values failed\n");
Brian Silvermanc71537c2016-01-01 13:43:14 -0800117 }
118 }
119
120 void HandleTeleop(const ::aos::input::driver_station::Data &data) {
Austin Schuhe4526232019-06-29 16:51:33 -0700121 auto accessories_message = accessories_goal_sender_.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()) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700128 AOS_LOG(WARNING, "sending accessories goal failed\n");
Brian Silvermanc71537c2016-01-01 13:43:14 -0800129 }
Brian Silvermanc71537c2016-01-01 13:43:14 -0800130 }
131
132 private:
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700133 ::aos::Sender<::frc971::control_loops::DrivetrainQueue::Goal>
134 drivetrain_goal_sender_;
Austin Schuhe4526232019-06-29 16:51:33 -0700135 ::aos::Sender<::y2012::control_loops::AccessoriesQueue::Message>
136 accessories_goal_sender_;
Brian Silvermanc71537c2016-01-01 13:43:14 -0800137
Austin Schuhe4526232019-06-29 16:51:33 -0700138 bool is_high_gear_;
Brian Silvermanc71537c2016-01-01 13:43:14 -0800139};
140
141} // namespace joysticks
142} // namespace input
143} // namespace y2012
144
145int main() {
Austin Schuh9fe68f72019-08-10 19:32:03 -0700146 ::aos::InitNRT(true);
147
Austin Schuh3e45c752019-02-02 12:19:11 -0800148 ::aos::ShmEventLoop event_loop;
149 ::y2012::input::joysticks::Reader reader(&event_loop);
Austin Schuh9fe68f72019-08-10 19:32:03 -0700150
151 event_loop.Run();
152
Brian Silvermanc71537c2016-01-01 13:43:14 -0800153 ::aos::Cleanup();
154}