blob: 01bcbc6a0058e27e61e58602d7c4e02bffb9b1a8 [file] [log] [blame]
Brian Silvermanba3de7e2013-05-08 16:18:15 -07001#include "aos/atom_code/input/joystick_input.h"
2
3#include <string.h>
4
5#include "aos/externals/WPILib/WPILib/NetworkRobot/NetworkRobotValues.h"
6
7#include "aos/common/Configuration.h"
8#include "aos/common/network/ReceiveSocket.h"
9#include "aos/common/messages/RobotState.q.h"
10#include "aos/common/logging/logging.h"
11
12namespace aos {
13namespace input {
14
15void JoystickInput::Run() {
16 ReceiveSocket sock(NetworkPort::kDS);
17
18 NetworkRobotJoysticks joysticks;
19 char buffer[sizeof(joysticks) + ::buffers::kOverhead];
20 driver_station::Data data;
21
22 while (true) {
23 int received = sock.Receive(buffer, sizeof(buffer));
24 if (received == -1) {
25 LOG(WARNING, "socket receive failed with %d: %s\n",
26 errno, strerror(errno));
27 continue;
28 }
29
30 if (!joysticks.DeserializeFrom(buffer, received)) {
31 LOG(WARNING, "deserializing data from %zd bytes failed\n", received);
32 continue;
33 }
34
35 if (!robot_state.MakeWithBuilder()
36 .enabled(joysticks.control.enabled())
37 .autonomous(joysticks.control.autonomous())
38 .team_id(joysticks.team_number)
39 .Send()) {
40 LOG(WARNING, "sending robot_state failed\n");
41 } else {
42 LOG(DEBUG, "sent robot_state{%s, %s, %hu}\n",
43 joysticks.control.enabled() ? "enabled" : "disabled",
44 joysticks.control.autonomous() ? "auto" : "not auto",
45 joysticks.team_number);
46 }
47
48 data.Update(joysticks);
49 // TODO(brians): posedge/negedge logging
50
51 RunIteration(data);
52 }
53}
54
55} // namespace input
56} // namespace aos