blob: 7e2f0cd00e5438134d9ad27299021e373da8d5e5 [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"
Brianc9f64552014-04-02 19:44:09 -07008#include "aos/common/network/receive_socket.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() {
Brianc9f64552014-04-02 19:44:09 -070017 network::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.
Brian Silverman48e5fae2014-03-26 20:46:50 -070022 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) {
Brian Silverman01be0002014-05-10 15:44:38 -070035 PLOG(WARNING, "socket receive failed")
Brian Silvermane155ebc2014-03-20 19:37:57 -070036 continue;
37 }
38
39 if (!joysticks.DeserializeFrom(buffer, received)) {
40 LOG(WARNING, "deserializing data from %d bytes failed\n", received);
41 continue;
42 }
Brian Silvermanba3de7e2013-05-08 16:18:15 -070043 }
44
Brian Silvermane155ebc2014-03-20 19:37:57 -070045 auto new_state = robot_state.MakeMessage();
46 new_state->enabled = joysticks.control.enabled();
47 new_state->autonomous = joysticks.control.autonomous();
48 new_state->team_id = joysticks.team_number;
49 new_state->fake = kFakeJoysticks;
50 LOG_STRUCT(DEBUG, "sending", *new_state);
Brian Silvermanba3de7e2013-05-08 16:18:15 -070051
Brian Silvermane155ebc2014-03-20 19:37:57 -070052 if (!new_state.Send()) {
53 LOG(WARNING, "sending robot_state failed\n");
Brian Silvermanba3de7e2013-05-08 16:18:15 -070054 }
55
56 data.Update(joysticks);
Brian Silvermanc25bc892013-05-09 19:09:34 -070057
58 {
59 using driver_station::JoystickFeature;
60 using driver_station::ButtonLocation;
Brian Silverman3d4b8972013-05-15 20:35:33 -070061 for (int joystick = 1; joystick <= JoystickFeature::kJoysticks;
Brian Silvermanc25bc892013-05-09 19:09:34 -070062 ++joystick) {
Brian Silverman3d4b8972013-05-15 20:35:33 -070063 for (int button = 1; button <= ButtonLocation::kButtons; ++button) {
Brian Silvermanc25bc892013-05-09 19:09:34 -070064 ButtonLocation location(joystick, button);
65 if (data.PosEdge(location)) {
66 LOG(INFO, "PosEdge(%d, %d)\n", joystick, button);
67 }
68 if (data.NegEdge(location)) {
69 LOG(INFO, "NegEdge(%d, %d)\n", joystick, button);
70 }
71 }
72 }
Brian Silverman6da04272014-05-18 18:47:48 -070073 }
74 {
Brian Silvermana7dec802013-11-04 20:53:19 -080075 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