Brian Silverman | da45b6c | 2014-12-28 11:36:50 -0800 | [diff] [blame] | 1 | #include "frc971/wpilib/joystick_sender.h" |
| 2 | |
John Park | 398c74a | 2018-10-20 21:17:39 -0700 | [diff] [blame] | 3 | #include "aos/init.h" |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 4 | #include "aos/logging/queue_logging.h" |
Tyler Chatow | c92b487 | 2019-02-23 21:42:11 -0800 | [diff] [blame] | 5 | #include "aos/network/team_number.h" |
| 6 | #include "aos/robot_state/robot_state.q.h" |
Brian Silverman | da45b6c | 2014-12-28 11:36:50 -0800 | [diff] [blame] | 7 | |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 8 | #include "frc971/wpilib/ahal/DriverStation.h" |
Austin Schuh | f6b9463 | 2019-02-02 22:11:27 -0800 | [diff] [blame] | 9 | #include "hal/HAL.h" |
Brian Silverman | da45b6c | 2014-12-28 11:36:50 -0800 | [diff] [blame] | 10 | |
| 11 | namespace frc971 { |
| 12 | namespace wpilib { |
| 13 | |
| 14 | void JoystickSender::operator()() { |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 15 | frc::DriverStation *const ds = &frc::DriverStation::GetInstance(); |
Brian Silverman | bd925f9 | 2014-12-31 14:28:06 -0800 | [diff] [blame] | 16 | ::aos::SetCurrentThreadName("DSReader"); |
Brian Silverman | da45b6c | 2014-12-28 11:36:50 -0800 | [diff] [blame] | 17 | uint16_t team_id = ::aos::network::GetTeamNumber(); |
| 18 | |
Brian Silverman | bd925f9 | 2014-12-31 14:28:06 -0800 | [diff] [blame] | 19 | ::aos::SetCurrentThreadRealtimePriority(29); |
| 20 | |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 21 | // TODO(Brian): Fix the potential deadlock when stopping here (condition |
| 22 | // variable / mutex needs to get exposed all the way out or something). |
Brian Silverman | da45b6c | 2014-12-28 11:36:50 -0800 | [diff] [blame] | 23 | while (run_) { |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 24 | ds->RunIteration([&]() { |
Austin Schuh | df6cbb1 | 2019-02-02 13:46:52 -0800 | [diff] [blame] | 25 | auto new_state = joystick_state_sender_.MakeMessage(); |
Brian Silverman | da45b6c | 2014-12-28 11:36:50 -0800 | [diff] [blame] | 26 | |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 27 | HAL_MatchInfo match_info; |
| 28 | auto status = HAL_GetMatchInfo(&match_info); |
| 29 | if (status == 0) { |
| 30 | new_state->switch_left = match_info.gameSpecificMessage[0] == 'L' || |
| 31 | match_info.gameSpecificMessage[0] == 'l'; |
| 32 | new_state->scale_left = match_info.gameSpecificMessage[1] == 'L' || |
| 33 | match_info.gameSpecificMessage[1] == 'l'; |
Brian Silverman | da45b6c | 2014-12-28 11:36:50 -0800 | [diff] [blame] | 34 | } |
Brian Silverman | da45b6c | 2014-12-28 11:36:50 -0800 | [diff] [blame] | 35 | |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 36 | new_state->test_mode = ds->IsTestMode(); |
| 37 | new_state->fms_attached = ds->IsFmsAttached(); |
| 38 | new_state->enabled = ds->IsEnabled(); |
| 39 | new_state->autonomous = ds->IsAutonomous(); |
| 40 | new_state->team_id = team_id; |
| 41 | new_state->fake = false; |
| 42 | |
Tyler Chatow | c92b487 | 2019-02-23 21:42:11 -0800 | [diff] [blame] | 43 | for (uint8_t i = 0; |
| 44 | i < sizeof(new_state->joysticks) / sizeof(::aos::Joystick); ++i) { |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 45 | new_state->joysticks[i].buttons = ds->GetStickButtons(i); |
| 46 | for (int j = 0; j < 6; ++j) { |
| 47 | new_state->joysticks[i].axis[j] = ds->GetStickAxis(i, j); |
| 48 | } |
Tyler Chatow | c92b487 | 2019-02-23 21:42:11 -0800 | [diff] [blame] | 49 | if (ds->GetStickPOVCount(i) > 0) { |
| 50 | new_state->joysticks[i].pov = ds->GetStickPOV(i, 0); |
| 51 | } |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 52 | LOG_STRUCT(DEBUG, "joystick_state", *new_state); |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 53 | } |
| 54 | if (!new_state.Send()) { |
| 55 | LOG(WARNING, "sending joystick_state failed\n"); |
| 56 | } |
| 57 | }); |
Brian Silverman | da45b6c | 2014-12-28 11:36:50 -0800 | [diff] [blame] | 58 | } |
| 59 | } |
| 60 | |
| 61 | } // namespace wpilib |
| 62 | } // namespace frc971 |