blob: d31d4a5a3f147f1c2f2e849b74012f1e9d7e7077 [file] [log] [blame]
Brian Silvermanda45b6c2014-12-28 11:36:50 -08001#include "frc971/wpilib/joystick_sender.h"
2
Alex Perrycb7da4b2019-08-28 19:35:56 -07003#include "aos/input/driver_station_data.h"
4#include "aos/logging/logging.h"
Tyler Chatowc92b4872019-02-23 21:42:11 -08005#include "aos/network/team_number.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -07006#include "aos/realtime.h"
7#include "aos/robot_state/joystick_state_generated.h"
Brian Silvermanda45b6c2014-12-28 11:36:50 -08008
Parker Schuhd3b7a8872018-02-19 16:42:27 -08009#include "frc971/wpilib/ahal/DriverStation.h"
Austin Schuhf6b94632019-02-02 22:11:27 -080010#include "hal/HAL.h"
Brian Silvermanda45b6c2014-12-28 11:36:50 -080011
12namespace frc971 {
13namespace wpilib {
14
Alex Perrycb7da4b2019-08-28 19:35:56 -070015using aos::Joystick;
16
James Kuszmaul57c2baa2020-01-19 14:52:52 -080017JoystickSender::JoystickSender(::aos::ShmEventLoop *event_loop)
Austin Schuhbd1fe9c2019-06-29 16:35:48 -070018 : event_loop_(event_loop),
19 joystick_state_sender_(
Alex Perrycb7da4b2019-08-28 19:35:56 -070020 event_loop_->MakeSender<::aos::JoystickState>("/aos")),
Austin Schuhbd1fe9c2019-06-29 16:35:48 -070021 team_id_(::aos::network::GetTeamNumber()) {
Austin Schuhbd1fe9c2019-06-29 16:35:48 -070022 event_loop_->SetRuntimeRealtimePriority(29);
James Kuszmaul57c2baa2020-01-19 14:52:52 -080023 event_loop->set_name("joystick_sender");
Brian Silvermanbd925f92014-12-31 14:28:06 -080024
Austin Schuhbd1fe9c2019-06-29 16:35:48 -070025 event_loop_->OnRun([this]() {
26 frc::DriverStation *const ds = &frc::DriverStation::GetInstance();
Brian Silvermanda45b6c2014-12-28 11:36:50 -080027
Austin Schuhbd1fe9c2019-06-29 16:35:48 -070028 // 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 Perrycb7da4b2019-08-28 19:35:56 -070032 auto builder = joystick_state_sender_.MakeBuilder();
Brian Silvermanda45b6c2014-12-28 11:36:50 -080033
Austin Schuhbd1fe9c2019-06-29 16:35:48 -070034 HAL_MatchInfo match_info;
35 auto status = HAL_GetMatchInfo(&match_info);
Austin Schuhbd1fe9c2019-06-29 16:35:48 -070036
Alex Perrycb7da4b2019-08-28 19:35:56 -070037 std::array<flatbuffers::Offset<Joystick>,
38 aos::input::driver_station::JoystickFeature::kJoysticks>
39 joysticks;
Austin Schuhbd1fe9c2019-06-29 16:35:48 -070040
Alex Perrycb7da4b2019-08-28 19:35:56 -070041 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 Schuhbd1fe9c2019-06-29 16:35:48 -070048 }
Alex Perrycb7da4b2019-08-28 19:35:56 -070049
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 Schuhbd1fe9c2019-06-29 16:35:48 -070057 if (ds->GetStickPOVCount(i) > 0) {
Alex Perrycb7da4b2019-08-28 19:35:56 -070058 joystick_builder.add_pov(ds->GetStickPOV(i, 0));
Austin Schuhbd1fe9c2019-06-29 16:35:48 -070059 }
Alex Perrycb7da4b2019-08-28 19:35:56 -070060
61 joystick_builder.add_axis(axis_offset);
62
63 joysticks[i] = joystick_builder.Finish();
Tyler Chatowc92b4872019-02-23 21:42:11 -080064 }
Alex Perrycb7da4b2019-08-28 19:35:56 -070065
66 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Joystick>>>
67 joysticks_offset = builder.fbb()->CreateVector(joysticks.begin(),
68 joysticks.size());
69
James Kuszmaula8806732020-02-14 19:45:35 -080070 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 Perrycb7da4b2019-08-28 19:35:56 -070082 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 Kuszmaula8806732020-02-14 19:45:35 -080094 joystick_state_builder.add_game_data(game_data_offset);
Alex Perrycb7da4b2019-08-28 19:35:56 -070095 }
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 Kuszmaula8806732020-02-14 19:45:35 -0800101 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 Perrycb7da4b2019-08-28 19:35:56 -0700112 joystick_state_builder.add_team_id(team_id_);
113
114 if (!builder.Send(joystick_state_builder.Finish())) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700115 AOS_LOG(WARNING, "sending joystick_state failed\n");
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700116 }
117 });
118 }
119 });
Brian Silvermanda45b6c2014-12-28 11:36:50 -0800120}
121
122} // namespace wpilib
123} // namespace frc971