Brian Silverman | 14fd0fb | 2014-01-14 21:42:01 -0800 | [diff] [blame] | 1 | #include "aos/prime/input/joystick_input.h" |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 2 | |
| 3 | #include <string.h> |
| 4 | |
| 5 | #include "aos/externals/WPILib/WPILib/NetworkRobot/NetworkRobotValues.h" |
| 6 | |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 7 | #include "aos/common/network_port.h" |
Brian | c9f6455 | 2014-04-02 19:44:09 -0700 | [diff] [blame] | 8 | #include "aos/common/network/receive_socket.h" |
Brian Silverman | 2ac0fbc | 2014-03-20 19:45:13 -0700 | [diff] [blame] | 9 | #include "aos/common/messages/robot_state.q.h" |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 10 | #include "aos/common/logging/logging.h" |
Brian Silverman | e155ebc | 2014-03-20 19:37:57 -0700 | [diff] [blame] | 11 | #include "aos/common/logging/queue_logging.h" |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 12 | |
| 13 | namespace aos { |
| 14 | namespace input { |
| 15 | |
Austin Schuh | 374fd17 | 2014-10-25 17:57:54 -0700 | [diff] [blame^] | 16 | void JoystickProxy::Run() { |
Brian | c9f6455 | 2014-04-02 19:44:09 -0700 | [diff] [blame] | 17 | network::ReceiveSocket sock(NetworkPort::kDS); |
Brian Silverman | e155ebc | 2014-03-20 19:37:57 -0700 | [diff] [blame] | 18 | // 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 Silverman | 48e5fae | 2014-03-26 20:46:50 -0700 | [diff] [blame] | 22 | static const bool kFakeJoysticks = false; |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 23 | |
| 24 | NetworkRobotJoysticks joysticks; |
| 25 | char buffer[sizeof(joysticks) + ::buffers::kOverhead]; |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 26 | |
| 27 | while (true) { |
Brian Silverman | e155ebc | 2014-03-20 19:37:57 -0700 | [diff] [blame] | 28 | 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 Silverman | 01be000 | 2014-05-10 15:44:38 -0700 | [diff] [blame] | 34 | PLOG(WARNING, "socket receive failed") |
Brian Silverman | e155ebc | 2014-03-20 19:37:57 -0700 | [diff] [blame] | 35 | continue; |
| 36 | } |
| 37 | |
| 38 | if (!joysticks.DeserializeFrom(buffer, received)) { |
| 39 | LOG(WARNING, "deserializing data from %d bytes failed\n", received); |
| 40 | continue; |
| 41 | } |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 42 | } |
| 43 | |
Brian Silverman | e155ebc | 2014-03-20 19:37:57 -0700 | [diff] [blame] | 44 | auto new_state = robot_state.MakeMessage(); |
Austin Schuh | 374fd17 | 2014-10-25 17:57:54 -0700 | [diff] [blame^] | 45 | new_state->test_mode = joysticks.control.test_mode(); |
| 46 | new_state->fms_attached = joysticks.control.fms_attached(); |
Brian Silverman | e155ebc | 2014-03-20 19:37:57 -0700 | [diff] [blame] | 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; |
Austin Schuh | 374fd17 | 2014-10-25 17:57:54 -0700 | [diff] [blame^] | 51 | |
| 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 Silverman | e155ebc | 2014-03-20 19:37:57 -0700 | [diff] [blame] | 61 | LOG_STRUCT(DEBUG, "sending", *new_state); |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 62 | |
Brian Silverman | e155ebc | 2014-03-20 19:37:57 -0700 | [diff] [blame] | 63 | if (!new_state.Send()) { |
| 64 | LOG(WARNING, "sending robot_state failed\n"); |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 65 | } |
Austin Schuh | 374fd17 | 2014-10-25 17:57:54 -0700 | [diff] [blame^] | 66 | } |
| 67 | } |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 68 | |
Austin Schuh | 374fd17 | 2014-10-25 17:57:54 -0700 | [diff] [blame^] | 69 | void 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 Silverman | c25bc89 | 2013-05-09 19:09:34 -0700 | [diff] [blame] | 77 | |
| 78 | { |
| 79 | using driver_station::JoystickFeature; |
| 80 | using driver_station::ButtonLocation; |
Brian Silverman | 3d4b897 | 2013-05-15 20:35:33 -0700 | [diff] [blame] | 81 | for (int joystick = 1; joystick <= JoystickFeature::kJoysticks; |
Brian Silverman | c25bc89 | 2013-05-09 19:09:34 -0700 | [diff] [blame] | 82 | ++joystick) { |
Brian Silverman | 3d4b897 | 2013-05-15 20:35:33 -0700 | [diff] [blame] | 83 | for (int button = 1; button <= ButtonLocation::kButtons; ++button) { |
Brian Silverman | c25bc89 | 2013-05-09 19:09:34 -0700 | [diff] [blame] | 84 | 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 Silverman | 6da0427 | 2014-05-18 18:47:48 -0700 | [diff] [blame] | 93 | } |
| 94 | { |
Brian Silverman | a7dec80 | 2013-11-04 20:53:19 -0800 | [diff] [blame] | 95 | 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 Silverman | c25bc89 | 2013-05-09 19:09:34 -0700 | [diff] [blame] | 114 | } |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 115 | |
| 116 | RunIteration(data); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | } // namespace input |
| 121 | } // namespace aos |