blob: 4eeed63cd0a8015ae55e9b868a34212054d3937b [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"
Brian Silverman598800f2013-05-09 17:08:42 -07005#include "aos/common/logging/logging.h"
brians343bc112013-02-10 01:53:46 +00006#include "aos/crio/controls/ControlsManager.h"
7#include "aos/common/network/SendSocket.h"
8#include "aos/common/messages/RobotState.q.h"
9
10namespace aos {
11namespace crio {
12
13class JoystickRead {
brians343bc112013-02-10 01:53:46 +000014 public:
briansf6506292013-03-03 22:19:02 +000015 // Represents all of the states that FMS thinks of a robot as being in.
16 enum class FMSState {
17 kDisabled,
18 kAutonomous,
19 kTeleop,
20 kTestMode,
21 };
22
brians343bc112013-02-10 01:53:46 +000023 DriverStation *ds;
briansf6506292013-03-03 22:19:02 +000024
brians343bc112013-02-10 01:53:46 +000025 JoystickRead() {}
briansf6506292013-03-03 22:19:02 +000026
brians343bc112013-02-10 01:53:46 +000027 void Run() {
28 SendSocket sock(NetworkPort::kDS,
29 configuration::GetIPAddress(
30 configuration::NetworkDevice::kAtom));
31 FRCCommonControlData data;
32
33 ds = ControlsManager::GetInstance().GetDS();
34
35 while (true) {
36 // I checked, and this is done intelligently in WPILib.
37 ds->WaitForData();
38
39 robot_state.MakeWithBuilder().enabled(ds->IsEnabled())
40 .autonomous(ds->IsAutonomous()).team_id(ds->GetTeamNumber()).Send();
41 LOG(DEBUG, "sending joystick data\n");
42 data.enabled = ds->IsEnabled();
43 data.autonomous = ds->IsAutonomous();
44 data.fmsAttached = ds->IsFMSAttached();
45 SetStick(data.stick0Axes, 1);
46 SetStick(data.stick1Axes, 2);
47 SetStick(data.stick2Axes, 3);
48 SetStick(data.stick3Axes, 4);
49 data.stick0Buttons = ds->GetStickButtons(1);
50 data.stick1Buttons = ds->GetStickButtons(2);
51 data.stick2Buttons = ds->GetStickButtons(3);
52 data.stick3Buttons = ds->GetStickButtons(4);
53 data.teamID = ds->GetTeamNumber();
54 sock.Send(&data, sizeof(data));
briansf6506292013-03-03 22:19:02 +000055
56 // Echo the state back into the FMS system. It makes the correct lights
57 // turn on so that field personnel don't get distracted when debugging
58 // unrelated problems.
59 FMSState state = GetCurrentState();
60 ds->InDisabled(state == FMSState::kDisabled);
61 ds->InAutonomous(state == FMSState::kAutonomous);
62 ds->InOperatorControl(state == FMSState::kTeleop);
63 ds->InTest(state == FMSState::kTestMode);
brians343bc112013-02-10 01:53:46 +000064 }
65 }
briansf6506292013-03-03 22:19:02 +000066
brians343bc112013-02-10 01:53:46 +000067 void SetStick(int8_t axes[6], uint32_t stick) {
68 for (int i = 0; i < 6; ++i) {
69 double val = ds->GetStickAxis(stick, i + 1);
70 if (val < 0) {
71 axes[i] = (val * 128.0) + 0.5;
72 } else {
73 axes[i] = (val * 127.0) + 0.5;
74 }
75 }
76 }
briansf6506292013-03-03 22:19:02 +000077
78 FMSState GetCurrentState() {
79 if (ds->IsDisabled()) return FMSState::kDisabled;
80 if (ds->IsAutonomous()) return FMSState::kAutonomous;
81 if (ds->IsOperatorControl()) return FMSState::kTeleop;
82 if (ds->IsTest()) return FMSState::kTestMode;
83 LOG(ERROR, "unknown fms state\n");
84 return FMSState::kDisabled;
85 }
brians343bc112013-02-10 01:53:46 +000086};
87
Brian Silverman7429b102013-03-16 13:50:13 -070088// Designed to be called from the console.
89extern "C" int battery_voltage() {
90 printf("battery is currently at %fV\n",
91 ControlsManager::GetInstance().GetDS()->GetBatteryVoltage());
92 return 0;
93}
94
brians343bc112013-02-10 01:53:46 +000095} // namespace crio
96} // namespace aos
97
98AOS_RUN_FORK(aos::crio::JoystickRead, "JSR", 100)