Brian Silverman | da45b6c | 2014-12-28 11:36:50 -0800 | [diff] [blame] | 1 | #include "frc971/wpilib/joystick_sender.h" |
| 2 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 3 | #include "aos/logging/logging.h" |
Tyler Chatow | c92b487 | 2019-02-23 21:42:11 -0800 | [diff] [blame] | 4 | #include "aos/network/team_number.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 5 | #include "aos/realtime.h" |
James Kuszmaul | 7077d34 | 2021-06-09 20:23:58 -0700 | [diff] [blame] | 6 | #include "frc971/input/driver_station_data.h" |
| 7 | #include "frc971/input/joystick_state_generated.h" |
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 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 14 | using aos::Joystick; |
| 15 | |
James Kuszmaul | 57c2baa | 2020-01-19 14:52:52 -0800 | [diff] [blame] | 16 | JoystickSender::JoystickSender(::aos::ShmEventLoop *event_loop) |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 17 | : event_loop_(event_loop), |
| 18 | joystick_state_sender_( |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 19 | event_loop_->MakeSender<::aos::JoystickState>("/aos")), |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 20 | team_id_(::aos::network::GetTeamNumber()) { |
Austin Schuh | 094d09b | 2020-11-20 23:26:52 -0800 | [diff] [blame] | 21 | event_loop_->SetRuntimeRealtimePriority(28); |
James Kuszmaul | 57c2baa | 2020-01-19 14:52:52 -0800 | [diff] [blame] | 22 | event_loop->set_name("joystick_sender"); |
Brian Silverman | bd925f9 | 2014-12-31 14:28:06 -0800 | [diff] [blame] | 23 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 24 | event_loop_->OnRun([this]() { |
| 25 | frc::DriverStation *const ds = &frc::DriverStation::GetInstance(); |
Brian Silverman | da45b6c | 2014-12-28 11:36:50 -0800 | [diff] [blame] | 26 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 27 | // TODO(Brian): Fix the potential deadlock when stopping here (condition |
| 28 | // variable / mutex needs to get exposed all the way out or something). |
| 29 | while (event_loop_->is_running()) { |
| 30 | ds->RunIteration([&]() { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 31 | auto builder = joystick_state_sender_.MakeBuilder(); |
Brian Silverman | da45b6c | 2014-12-28 11:36:50 -0800 | [diff] [blame] | 32 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 33 | HAL_MatchInfo match_info; |
| 34 | auto status = HAL_GetMatchInfo(&match_info); |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 35 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 36 | std::array<flatbuffers::Offset<Joystick>, |
James Kuszmaul | 7077d34 | 2021-06-09 20:23:58 -0700 | [diff] [blame] | 37 | frc971::input::driver_station::JoystickFeature::kJoysticks> |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 38 | joysticks; |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 39 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 40 | for (size_t i = 0; |
James Kuszmaul | 7077d34 | 2021-06-09 20:23:58 -0700 | [diff] [blame] | 41 | i < frc971::input::driver_station::JoystickFeature::kJoysticks; |
| 42 | ++i) { |
| 43 | std::array<double, frc971::input::driver_station::JoystickAxis::kAxes> |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 44 | axis; |
James Kuszmaul | 7077d34 | 2021-06-09 20:23:58 -0700 | [diff] [blame] | 45 | for (int j = 0; |
| 46 | j < frc971::input::driver_station::JoystickAxis::kAxes; ++j) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 47 | axis[j] = ds->GetStickAxis(i, j); |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 48 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 49 | |
| 50 | flatbuffers::Offset<flatbuffers::Vector<double>> axis_offset = |
| 51 | builder.fbb()->CreateVector(axis.begin(), axis.size()); |
| 52 | |
| 53 | Joystick::Builder joystick_builder = builder.MakeBuilder<Joystick>(); |
| 54 | |
| 55 | joystick_builder.add_buttons(ds->GetStickButtons(i)); |
| 56 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 57 | if (ds->GetStickPOVCount(i) > 0) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 58 | joystick_builder.add_pov(ds->GetStickPOV(i, 0)); |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 59 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 60 | |
| 61 | joystick_builder.add_axis(axis_offset); |
| 62 | |
| 63 | joysticks[i] = joystick_builder.Finish(); |
Tyler Chatow | c92b487 | 2019-02-23 21:42:11 -0800 | [diff] [blame] | 64 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 65 | |
| 66 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Joystick>>> |
| 67 | joysticks_offset = builder.fbb()->CreateVector(joysticks.begin(), |
| 68 | joysticks.size()); |
| 69 | |
James Kuszmaul | a880673 | 2020-02-14 19:45:35 -0800 | [diff] [blame] | 70 | flatbuffers::Offset<flatbuffers::String> game_data_offset; |
| 71 | if (status == 0) { |
| 72 | static_assert(sizeof(match_info.gameSpecificMessage) == 64, |
| 73 | "Check that the match info game specific message size " |
| 74 | "hasn't changed and is still sane."); |
| 75 | CHECK_LE(match_info.gameSpecificMessageSize, |
| 76 | sizeof(match_info.gameSpecificMessage)); |
| 77 | game_data_offset = builder.fbb()->CreateString( |
| 78 | reinterpret_cast<const char *>(match_info.gameSpecificMessage), |
| 79 | match_info.gameSpecificMessageSize); |
| 80 | } |
| 81 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 82 | aos::JoystickState::Builder joystick_state_builder = |
| 83 | builder.MakeBuilder<aos::JoystickState>(); |
| 84 | |
| 85 | joystick_state_builder.add_joysticks(joysticks_offset); |
| 86 | |
| 87 | if (status == 0) { |
| 88 | joystick_state_builder.add_switch_left( |
| 89 | match_info.gameSpecificMessage[0] == 'L' || |
| 90 | match_info.gameSpecificMessage[0] == 'l'); |
| 91 | joystick_state_builder.add_scale_left( |
| 92 | match_info.gameSpecificMessage[1] == 'L' || |
| 93 | match_info.gameSpecificMessage[1] == 'l'); |
James Kuszmaul | a880673 | 2020-02-14 19:45:35 -0800 | [diff] [blame] | 94 | joystick_state_builder.add_game_data(game_data_offset); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | joystick_state_builder.add_test_mode(ds->IsTestMode()); |
| 98 | joystick_state_builder.add_fms_attached(ds->IsFmsAttached()); |
| 99 | joystick_state_builder.add_enabled(ds->IsEnabled()); |
| 100 | joystick_state_builder.add_autonomous(ds->IsAutonomous()); |
James Kuszmaul | a880673 | 2020-02-14 19:45:35 -0800 | [diff] [blame] | 101 | switch (ds->GetAlliance()) { |
| 102 | case frc::DriverStation::kRed: |
| 103 | joystick_state_builder.add_alliance(aos::Alliance::kRed); |
| 104 | break; |
| 105 | case frc::DriverStation::kBlue: |
| 106 | joystick_state_builder.add_alliance(aos::Alliance::kBlue); |
| 107 | break; |
| 108 | case frc::DriverStation::kInvalid: |
| 109 | joystick_state_builder.add_alliance(aos::Alliance::kInvalid); |
| 110 | break; |
| 111 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 112 | joystick_state_builder.add_team_id(team_id_); |
| 113 | |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame^] | 114 | if (builder.Send(joystick_state_builder.Finish()) != |
| 115 | aos::RawSender::Error::kOk) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 116 | AOS_LOG(WARNING, "sending joystick_state failed\n"); |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 117 | } |
| 118 | }); |
| 119 | } |
| 120 | }); |
Brian Silverman | da45b6c | 2014-12-28 11:36:50 -0800 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | } // namespace wpilib |
| 124 | } // namespace frc971 |