blob: 043716b70fc7b76d55b36883c6bf60696830328e [file] [log] [blame]
brians343bc112013-02-10 01:53:46 +00001#include "WPILib/Task.h"
2#include "WPILib/Timer.h"
3
4#include "aos/aos_core.h"
5#include "aos/crio/controls/ControlsManager.h"
6#include "aos/common/network/SendSocket.h"
7#include "aos/common/messages/RobotState.q.h"
8
9namespace aos {
10namespace crio {
11
12class JoystickRead {
brians343bc112013-02-10 01:53:46 +000013 public:
briansf6506292013-03-03 22:19:02 +000014 // Represents all of the states that FMS thinks of a robot as being in.
15 enum class FMSState {
16 kDisabled,
17 kAutonomous,
18 kTeleop,
19 kTestMode,
20 };
21
brians343bc112013-02-10 01:53:46 +000022 DriverStation *ds;
briansf6506292013-03-03 22:19:02 +000023
brians343bc112013-02-10 01:53:46 +000024 JoystickRead() {}
briansf6506292013-03-03 22:19:02 +000025
brians343bc112013-02-10 01:53:46 +000026 void Run() {
27 SendSocket sock(NetworkPort::kDS,
28 configuration::GetIPAddress(
29 configuration::NetworkDevice::kAtom));
30 FRCCommonControlData data;
31
32 ds = ControlsManager::GetInstance().GetDS();
33
34 while (true) {
35 // I checked, and this is done intelligently in WPILib.
36 ds->WaitForData();
37
38 robot_state.MakeWithBuilder().enabled(ds->IsEnabled())
39 .autonomous(ds->IsAutonomous()).team_id(ds->GetTeamNumber()).Send();
40 LOG(DEBUG, "sending joystick data\n");
41 data.enabled = ds->IsEnabled();
42 data.autonomous = ds->IsAutonomous();
43 data.fmsAttached = ds->IsFMSAttached();
44 SetStick(data.stick0Axes, 1);
45 SetStick(data.stick1Axes, 2);
46 SetStick(data.stick2Axes, 3);
47 SetStick(data.stick3Axes, 4);
48 data.stick0Buttons = ds->GetStickButtons(1);
49 data.stick1Buttons = ds->GetStickButtons(2);
50 data.stick2Buttons = ds->GetStickButtons(3);
51 data.stick3Buttons = ds->GetStickButtons(4);
52 data.teamID = ds->GetTeamNumber();
53 sock.Send(&data, sizeof(data));
briansf6506292013-03-03 22:19:02 +000054
55 // Echo the state back into the FMS system. It makes the correct lights
56 // turn on so that field personnel don't get distracted when debugging
57 // unrelated problems.
58 FMSState state = GetCurrentState();
59 ds->InDisabled(state == FMSState::kDisabled);
60 ds->InAutonomous(state == FMSState::kAutonomous);
61 ds->InOperatorControl(state == FMSState::kTeleop);
62 ds->InTest(state == FMSState::kTestMode);
brians343bc112013-02-10 01:53:46 +000063 }
64 }
briansf6506292013-03-03 22:19:02 +000065
brians343bc112013-02-10 01:53:46 +000066 void SetStick(int8_t axes[6], uint32_t stick) {
67 for (int i = 0; i < 6; ++i) {
68 double val = ds->GetStickAxis(stick, i + 1);
69 if (val < 0) {
70 axes[i] = (val * 128.0) + 0.5;
71 } else {
72 axes[i] = (val * 127.0) + 0.5;
73 }
74 }
75 }
briansf6506292013-03-03 22:19:02 +000076
77 FMSState GetCurrentState() {
78 if (ds->IsDisabled()) return FMSState::kDisabled;
79 if (ds->IsAutonomous()) return FMSState::kAutonomous;
80 if (ds->IsOperatorControl()) return FMSState::kTeleop;
81 if (ds->IsTest()) return FMSState::kTestMode;
82 LOG(ERROR, "unknown fms state\n");
83 return FMSState::kDisabled;
84 }
brians343bc112013-02-10 01:53:46 +000085};
86
87} // namespace crio
88} // namespace aos
89
90AOS_RUN_FORK(aos::crio::JoystickRead, "JSR", 100)