blob: c9b890a79b1a9a64db395916972dc1d1efb5db84 [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
Austin Schuh374fd172014-10-25 17:57:54 -070016void JoystickProxy::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];
Brian Silvermanba3de7e2013-05-08 16:18:15 -070026
27 while (true) {
Brian Silvermane155ebc2014-03-20 19:37:57 -070028 if (kFakeJoysticks) {
29 ::aos::time::SleepFor(::aos::time::Time::InSeconds(0.02));
30 memset(&joysticks, 0, sizeof(joysticks));
31 } else {
32 int received = sock.Receive(buffer, sizeof(buffer));
33 if (received == -1) {
Brian Silverman01be0002014-05-10 15:44:38 -070034 PLOG(WARNING, "socket receive failed")
Brian Silvermane155ebc2014-03-20 19:37:57 -070035 continue;
36 }
37
38 if (!joysticks.DeserializeFrom(buffer, received)) {
39 LOG(WARNING, "deserializing data from %d bytes failed\n", received);
40 continue;
41 }
Brian Silvermanba3de7e2013-05-08 16:18:15 -070042 }
43
Brian Silvermane155ebc2014-03-20 19:37:57 -070044 auto new_state = robot_state.MakeMessage();
Austin Schuh374fd172014-10-25 17:57:54 -070045 new_state->test_mode = joysticks.control.test_mode();
46 new_state->fms_attached = joysticks.control.fms_attached();
Brian Silvermane155ebc2014-03-20 19:37:57 -070047 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;
Austin Schuh374fd172014-10-25 17:57:54 -070051
52 for (int i = 0; i < 4; ++i) {
53 new_state->joysticks[i].buttons = joysticks.joysticks[i].buttons;
54 for (int j = 0; j < 4; ++j) {
55 // TODO(brians): check this math against what our joysticks report as
56 // their logical minimums and maximums
57 new_state->joysticks[i].axis[j] = joysticks.joysticks[i].axes[j] / 127.0;
58 }
59 }
60
Brian Silvermane155ebc2014-03-20 19:37:57 -070061 LOG_STRUCT(DEBUG, "sending", *new_state);
Brian Silvermanba3de7e2013-05-08 16:18:15 -070062
Brian Silvermane155ebc2014-03-20 19:37:57 -070063 if (!new_state.Send()) {
64 LOG(WARNING, "sending robot_state failed\n");
Brian Silvermanba3de7e2013-05-08 16:18:15 -070065 }
Austin Schuh374fd172014-10-25 17:57:54 -070066 }
67}
Brian Silvermanba3de7e2013-05-08 16:18:15 -070068
Austin Schuh374fd172014-10-25 17:57:54 -070069void JoystickInput::Run() {
70 driver_station::Data data;
71 while (true) {
72 robot_state.FetchAnother();
73
74 LOG_STRUCT(DEBUG, "sending", *robot_state);
75
76 data.Update(*robot_state);
Brian Silvermanc25bc892013-05-09 19:09:34 -070077
78 {
79 using driver_station::JoystickFeature;
80 using driver_station::ButtonLocation;
Brian Silverman3d4b8972013-05-15 20:35:33 -070081 for (int joystick = 1; joystick <= JoystickFeature::kJoysticks;
Brian Silvermanc25bc892013-05-09 19:09:34 -070082 ++joystick) {
Brian Silverman3d4b8972013-05-15 20:35:33 -070083 for (int button = 1; button <= ButtonLocation::kButtons; ++button) {
Brian Silvermanc25bc892013-05-09 19:09:34 -070084 ButtonLocation location(joystick, button);
85 if (data.PosEdge(location)) {
86 LOG(INFO, "PosEdge(%d, %d)\n", joystick, button);
87 }
88 if (data.NegEdge(location)) {
89 LOG(INFO, "NegEdge(%d, %d)\n", joystick, button);
90 }
91 }
92 }
Brian Silverman6da04272014-05-18 18:47:48 -070093 }
94 {
Brian Silvermana7dec802013-11-04 20:53:19 -080095 using driver_station::ControlBit;
96 if (data.PosEdge(ControlBit::kFmsAttached)) {
97 LOG(INFO, "PosEdge(kFmsAttached)\n");
98 }
99 if (data.NegEdge(ControlBit::kFmsAttached)) {
100 LOG(INFO, "NegEdge(kFmsAttached)\n");
101 }
102 if (data.PosEdge(ControlBit::kAutonomous)) {
103 LOG(INFO, "PosEdge(kAutonomous)\n");
104 }
105 if (data.NegEdge(ControlBit::kAutonomous)) {
106 LOG(INFO, "NegEdge(kAutonomous)\n");
107 }
108 if (data.PosEdge(ControlBit::kEnabled)) {
109 LOG(INFO, "PosEdge(kEnabled)\n");
110 }
111 if (data.NegEdge(ControlBit::kEnabled)) {
112 LOG(INFO, "NegEdge(kEnabled)\n");
113 }
Brian Silvermanc25bc892013-05-09 19:09:34 -0700114 }
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700115
116 RunIteration(data);
117 }
118}
119
120} // namespace input
121} // namespace aos