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/input/driver_station_data.h" |
| 4 | #include "aos/logging/logging.h" |
Tyler Chatow | c92b487 | 2019-02-23 21:42:11 -0800 | [diff] [blame] | 5 | #include "aos/network/team_number.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame^] | 6 | #include "aos/realtime.h" |
| 7 | #include "aos/robot_state/joystick_state_generated.h" |
Brian Silverman | da45b6c | 2014-12-28 11:36:50 -0800 | [diff] [blame] | 8 | |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 9 | #include "frc971/wpilib/ahal/DriverStation.h" |
Austin Schuh | f6b9463 | 2019-02-02 22:11:27 -0800 | [diff] [blame] | 10 | #include "hal/HAL.h" |
Brian Silverman | da45b6c | 2014-12-28 11:36:50 -0800 | [diff] [blame] | 11 | |
| 12 | namespace frc971 { |
| 13 | namespace wpilib { |
| 14 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame^] | 15 | using aos::Joystick; |
| 16 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 17 | JoystickSender::JoystickSender(::aos::EventLoop *event_loop) |
| 18 | : event_loop_(event_loop), |
| 19 | joystick_state_sender_( |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame^] | 20 | event_loop_->MakeSender<::aos::JoystickState>("/aos")), |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 21 | team_id_(::aos::network::GetTeamNumber()) { |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 22 | event_loop_->SetRuntimeRealtimePriority(29); |
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(); |
| 26 | ::aos::SetCurrentThreadName("DSReader"); |
Brian Silverman | da45b6c | 2014-12-28 11:36:50 -0800 | [diff] [blame] | 27 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 28 | // TODO(Brian): Fix the potential deadlock when stopping here (condition |
| 29 | // variable / mutex needs to get exposed all the way out or something). |
| 30 | while (event_loop_->is_running()) { |
| 31 | ds->RunIteration([&]() { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame^] | 32 | auto builder = joystick_state_sender_.MakeBuilder(); |
Brian Silverman | da45b6c | 2014-12-28 11:36:50 -0800 | [diff] [blame] | 33 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 34 | HAL_MatchInfo match_info; |
| 35 | auto status = HAL_GetMatchInfo(&match_info); |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 36 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame^] | 37 | std::array<flatbuffers::Offset<Joystick>, |
| 38 | aos::input::driver_station::JoystickFeature::kJoysticks> |
| 39 | joysticks; |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 40 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame^] | 41 | for (size_t i = 0; |
| 42 | i < aos::input::driver_station::JoystickFeature::kJoysticks; ++i) { |
| 43 | std::array<double, aos::input::driver_station::JoystickAxis::kAxes> |
| 44 | axis; |
| 45 | for (int j = 0; j < aos::input::driver_station::JoystickAxis::kAxes; |
| 46 | ++j) { |
| 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 | |
| 70 | aos::JoystickState::Builder joystick_state_builder = |
| 71 | builder.MakeBuilder<aos::JoystickState>(); |
| 72 | |
| 73 | joystick_state_builder.add_joysticks(joysticks_offset); |
| 74 | |
| 75 | if (status == 0) { |
| 76 | joystick_state_builder.add_switch_left( |
| 77 | match_info.gameSpecificMessage[0] == 'L' || |
| 78 | match_info.gameSpecificMessage[0] == 'l'); |
| 79 | joystick_state_builder.add_scale_left( |
| 80 | match_info.gameSpecificMessage[1] == 'L' || |
| 81 | match_info.gameSpecificMessage[1] == 'l'); |
| 82 | } |
| 83 | |
| 84 | joystick_state_builder.add_test_mode(ds->IsTestMode()); |
| 85 | joystick_state_builder.add_fms_attached(ds->IsFmsAttached()); |
| 86 | joystick_state_builder.add_enabled(ds->IsEnabled()); |
| 87 | joystick_state_builder.add_autonomous(ds->IsAutonomous()); |
| 88 | joystick_state_builder.add_team_id(team_id_); |
| 89 | |
| 90 | if (!builder.Send(joystick_state_builder.Finish())) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 91 | AOS_LOG(WARNING, "sending joystick_state failed\n"); |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 92 | } |
| 93 | }); |
| 94 | } |
| 95 | }); |
Brian Silverman | da45b6c | 2014-12-28 11:36:50 -0800 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | } // namespace wpilib |
| 99 | } // namespace frc971 |