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" |
| 5 | #include "aos/crio/controls/ControlsManager.h" |
| 6 | #include "aos/common/network/SendSocket.h" |
| 7 | #include "aos/common/messages/RobotState.q.h" |
| 8 | |
| 9 | namespace aos { |
| 10 | namespace crio { |
| 11 | |
| 12 | class JoystickRead { |
| 13 | /*virtual void Disabled () { |
| 14 | int i = 0; |
| 15 | while (IsDisabled()) { |
| 16 | printf("Disabled! %d\n", i); |
| 17 | Wait(0.1); |
| 18 | i++; |
| 19 | } |
| 20 | printf("Done with disabled. %d\n", i); |
| 21 | } |
| 22 | virtual void Autonomous () { |
| 23 | int j = 0; |
| 24 | while (IsAutonomous()) { |
| 25 | printf("Autonomous! %d\n", j); |
| 26 | Wait(0.1); |
| 27 | //if (j > 5) { |
| 28 | //i(0); |
| 29 | //} |
| 30 | j ++; |
| 31 | } |
| 32 | printf("Done with autonomous. %d\n", j); |
| 33 | } |
| 34 | virtual void OperatorControl () { |
| 35 | int i = 0; |
| 36 | while (IsOperatorControl()) { |
| 37 | printf("Operator Control! %d\n", i); |
| 38 | Wait(0.1); |
| 39 | i ++; |
| 40 | } |
| 41 | printf("Done with operator control. %d\n", i); |
| 42 | }*/ |
| 43 | public: |
| 44 | DriverStation *ds; |
| 45 | JoystickRead() {} |
| 46 | void Run() { |
| 47 | SendSocket sock(NetworkPort::kDS, |
| 48 | configuration::GetIPAddress( |
| 49 | configuration::NetworkDevice::kAtom)); |
| 50 | FRCCommonControlData data; |
| 51 | |
| 52 | ds = ControlsManager::GetInstance().GetDS(); |
| 53 | |
| 54 | while (true) { |
| 55 | // I checked, and this is done intelligently in WPILib. |
| 56 | ds->WaitForData(); |
| 57 | |
| 58 | robot_state.MakeWithBuilder().enabled(ds->IsEnabled()) |
| 59 | .autonomous(ds->IsAutonomous()).team_id(ds->GetTeamNumber()).Send(); |
| 60 | LOG(DEBUG, "sending joystick data\n"); |
| 61 | data.enabled = ds->IsEnabled(); |
| 62 | data.autonomous = ds->IsAutonomous(); |
| 63 | data.fmsAttached = ds->IsFMSAttached(); |
| 64 | SetStick(data.stick0Axes, 1); |
| 65 | SetStick(data.stick1Axes, 2); |
| 66 | SetStick(data.stick2Axes, 3); |
| 67 | SetStick(data.stick3Axes, 4); |
| 68 | data.stick0Buttons = ds->GetStickButtons(1); |
| 69 | data.stick1Buttons = ds->GetStickButtons(2); |
| 70 | data.stick2Buttons = ds->GetStickButtons(3); |
| 71 | data.stick3Buttons = ds->GetStickButtons(4); |
| 72 | data.teamID = ds->GetTeamNumber(); |
| 73 | sock.Send(&data, sizeof(data)); |
| 74 | } |
| 75 | } |
| 76 | void SetStick(int8_t axes[6], uint32_t stick) { |
| 77 | for (int i = 0; i < 6; ++i) { |
| 78 | double val = ds->GetStickAxis(stick, i + 1); |
| 79 | if (val < 0) { |
| 80 | axes[i] = (val * 128.0) + 0.5; |
| 81 | } else { |
| 82 | axes[i] = (val * 127.0) + 0.5; |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | }; |
| 87 | |
| 88 | } // namespace crio |
| 89 | } // namespace aos |
| 90 | |
| 91 | AOS_RUN_FORK(aos::crio::JoystickRead, "JSR", 100) |