brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 1 | #include "WPILib/Task.h" |
| 2 | #include "WPILib/Timer.h" |
| 3 | |
| 4 | #include "aos/aos_core.h" |
Brian Silverman | 598800f | 2013-05-09 17:08:42 -0700 | [diff] [blame^] | 5 | #include "aos/common/logging/logging.h" |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 6 | #include "aos/crio/controls/ControlsManager.h" |
| 7 | #include "aos/common/network/SendSocket.h" |
| 8 | #include "aos/common/messages/RobotState.q.h" |
| 9 | |
| 10 | namespace aos { |
| 11 | namespace crio { |
| 12 | |
| 13 | class JoystickRead { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 14 | public: |
brians | f650629 | 2013-03-03 22:19:02 +0000 | [diff] [blame] | 15 | // 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 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 23 | DriverStation *ds; |
brians | f650629 | 2013-03-03 22:19:02 +0000 | [diff] [blame] | 24 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 25 | JoystickRead() {} |
brians | f650629 | 2013-03-03 22:19:02 +0000 | [diff] [blame] | 26 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 27 | 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)); |
brians | f650629 | 2013-03-03 22:19:02 +0000 | [diff] [blame] | 55 | |
| 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); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 64 | } |
| 65 | } |
brians | f650629 | 2013-03-03 22:19:02 +0000 | [diff] [blame] | 66 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 67 | 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 | } |
brians | f650629 | 2013-03-03 22:19:02 +0000 | [diff] [blame] | 77 | |
| 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 | } |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 86 | }; |
| 87 | |
Brian Silverman | 7429b10 | 2013-03-16 13:50:13 -0700 | [diff] [blame] | 88 | // Designed to be called from the console. |
| 89 | extern "C" int battery_voltage() { |
| 90 | printf("battery is currently at %fV\n", |
| 91 | ControlsManager::GetInstance().GetDS()->GetBatteryVoltage()); |
| 92 | return 0; |
| 93 | } |
| 94 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 95 | } // namespace crio |
| 96 | } // namespace aos |
| 97 | |
| 98 | AOS_RUN_FORK(aos::crio::JoystickRead, "JSR", 100) |