blob: a2ba21b48059c43501f6231c1e6af6d294aa10a5 [file] [log] [blame]
Brian Silverman14fd0fb2014-01-14 21:42:01 -08001#include "aos/prime/input/joystick_input.h"
Brian Silvermanba3de7e2013-05-08 16:18:15 -07002
3#include <string.h>
4
5#include "aos/externals/WPILib/WPILib/NetworkRobot/NetworkRobotValues.h"
6
Brian Silverman66f079a2013-08-26 16:24:30 -07007#include "aos/common/network_port.h"
Brian Silvermanba3de7e2013-05-08 16:18:15 -07008#include "aos/common/network/ReceiveSocket.h"
Brian Silverman2ac0fbc2014-03-20 19:45:13 -07009#include "aos/common/messages/robot_state.q.h"
Brian Silvermanba3de7e2013-05-08 16:18:15 -070010#include "aos/common/logging/logging.h"
Brian Silvermane155ebc2014-03-20 19:37:57 -070011#include "aos/common/logging/queue_logging.h"
Brian Silvermanba3de7e2013-05-08 16:18:15 -070012
13namespace aos {
14namespace input {
15
16void JoystickInput::Run() {
17 ReceiveSocket sock(NetworkPort::kDS);
Brian Silvermane155ebc2014-03-20 19:37:57 -070018 // If true, this code won't try to read anything from the network and instead
19 // feed all 0s to the joystick code.
20 // The RobotState messages will be marked as fake so anything that outputs
21 // values won't while this is enabled.
22 static const bool kFakeJoysticks = false;
Brian Silvermanba3de7e2013-05-08 16:18:15 -070023
24 NetworkRobotJoysticks joysticks;
25 char buffer[sizeof(joysticks) + ::buffers::kOverhead];
26 driver_station::Data data;
27
28 while (true) {
Brian Silvermane155ebc2014-03-20 19:37:57 -070029 if (kFakeJoysticks) {
30 ::aos::time::SleepFor(::aos::time::Time::InSeconds(0.02));
31 memset(&joysticks, 0, sizeof(joysticks));
32 } else {
33 int received = sock.Receive(buffer, sizeof(buffer));
34 if (received == -1) {
35 LOG(WARNING, "socket receive failed with %d: %s\n",
36 errno, strerror(errno));
37 continue;
38 }
39
40 if (!joysticks.DeserializeFrom(buffer, received)) {
41 LOG(WARNING, "deserializing data from %d bytes failed\n", received);
42 continue;
43 }
Brian Silvermanba3de7e2013-05-08 16:18:15 -070044 }
45
Brian Silvermane155ebc2014-03-20 19:37:57 -070046 auto new_state = robot_state.MakeMessage();
47 new_state->enabled = joysticks.control.enabled();
48 new_state->autonomous = joysticks.control.autonomous();
49 new_state->team_id = joysticks.team_number;
50 new_state->fake = kFakeJoysticks;
51 LOG_STRUCT(DEBUG, "sending", *new_state);
Brian Silvermanba3de7e2013-05-08 16:18:15 -070052
Brian Silvermane155ebc2014-03-20 19:37:57 -070053 if (!new_state.Send()) {
54 LOG(WARNING, "sending robot_state failed\n");
Brian Silvermanba3de7e2013-05-08 16:18:15 -070055 }
56
57 data.Update(joysticks);
Brian Silvermanc25bc892013-05-09 19:09:34 -070058
59 {
60 using driver_station::JoystickFeature;
61 using driver_station::ButtonLocation;
Brian Silverman3d4b8972013-05-15 20:35:33 -070062 for (int joystick = 1; joystick <= JoystickFeature::kJoysticks;
Brian Silvermanc25bc892013-05-09 19:09:34 -070063 ++joystick) {
Brian Silverman3d4b8972013-05-15 20:35:33 -070064 for (int button = 1; button <= ButtonLocation::kButtons; ++button) {
Brian Silvermanc25bc892013-05-09 19:09:34 -070065 ButtonLocation location(joystick, button);
66 if (data.PosEdge(location)) {
67 LOG(INFO, "PosEdge(%d, %d)\n", joystick, button);
68 }
69 if (data.NegEdge(location)) {
70 LOG(INFO, "NegEdge(%d, %d)\n", joystick, button);
71 }
72 }
73 }
Brian Silvermana7dec802013-11-04 20:53:19 -080074
75 using driver_station::ControlBit;
76 if (data.PosEdge(ControlBit::kFmsAttached)) {
77 LOG(INFO, "PosEdge(kFmsAttached)\n");
78 }
79 if (data.NegEdge(ControlBit::kFmsAttached)) {
80 LOG(INFO, "NegEdge(kFmsAttached)\n");
81 }
82 if (data.PosEdge(ControlBit::kAutonomous)) {
83 LOG(INFO, "PosEdge(kAutonomous)\n");
84 }
85 if (data.NegEdge(ControlBit::kAutonomous)) {
86 LOG(INFO, "NegEdge(kAutonomous)\n");
87 }
88 if (data.PosEdge(ControlBit::kEnabled)) {
89 LOG(INFO, "PosEdge(kEnabled)\n");
90 }
91 if (data.NegEdge(ControlBit::kEnabled)) {
92 LOG(INFO, "NegEdge(kEnabled)\n");
93 }
Brian Silvermanc25bc892013-05-09 19:09:34 -070094 }
Brian Silvermanba3de7e2013-05-08 16:18:15 -070095
96 RunIteration(data);
97 }
98}
99
100} // namespace input
101} // namespace aos