blob: a0730665c791231ae99bd0874bd6553b7c96de82 [file] [log] [blame]
Brian Silvermanda45b6c2014-12-28 11:36:50 -08001#include "frc971/wpilib/joystick_sender.h"
2
John Park398c74a2018-10-20 21:17:39 -07003#include "aos/init.h"
John Park33858a32018-09-28 23:05:48 -07004#include "aos/logging/queue_logging.h"
Tyler Chatowc92b4872019-02-23 21:42:11 -08005#include "aos/network/team_number.h"
6#include "aos/robot_state/robot_state.q.h"
Brian Silvermanda45b6c2014-12-28 11:36:50 -08007
Parker Schuhd3b7a8872018-02-19 16:42:27 -08008#include "frc971/wpilib/ahal/DriverStation.h"
Austin Schuhf6b94632019-02-02 22:11:27 -08009#include "hal/HAL.h"
Brian Silvermanda45b6c2014-12-28 11:36:50 -080010
11namespace frc971 {
12namespace wpilib {
13
Austin Schuhbd1fe9c2019-06-29 16:35:48 -070014JoystickSender::JoystickSender(::aos::EventLoop *event_loop)
15 : event_loop_(event_loop),
16 joystick_state_sender_(
17 event_loop_->MakeSender<::aos::JoystickState>(".aos.joystick_state")),
18 team_id_(::aos::network::GetTeamNumber()) {
Brian Silvermanda45b6c2014-12-28 11:36:50 -080019
Austin Schuhbd1fe9c2019-06-29 16:35:48 -070020 event_loop_->SetRuntimeRealtimePriority(29);
Brian Silvermanbd925f92014-12-31 14:28:06 -080021
Austin Schuhbd1fe9c2019-06-29 16:35:48 -070022 event_loop_->OnRun([this]() {
23 frc::DriverStation *const ds = &frc::DriverStation::GetInstance();
24 ::aos::SetCurrentThreadName("DSReader");
Brian Silvermanda45b6c2014-12-28 11:36:50 -080025
Austin Schuhbd1fe9c2019-06-29 16:35:48 -070026 // TODO(Brian): Fix the potential deadlock when stopping here (condition
27 // variable / mutex needs to get exposed all the way out or something).
28 while (event_loop_->is_running()) {
29 ds->RunIteration([&]() {
30 auto new_state = joystick_state_sender_.MakeMessage();
Brian Silvermanda45b6c2014-12-28 11:36:50 -080031
Austin Schuhbd1fe9c2019-06-29 16:35:48 -070032 HAL_MatchInfo match_info;
33 auto status = HAL_GetMatchInfo(&match_info);
34 if (status == 0) {
35 new_state->switch_left = match_info.gameSpecificMessage[0] == 'L' ||
36 match_info.gameSpecificMessage[0] == 'l';
37 new_state->scale_left = match_info.gameSpecificMessage[1] == 'L' ||
38 match_info.gameSpecificMessage[1] == 'l';
Parker Schuhd3b7a8872018-02-19 16:42:27 -080039 }
Austin Schuhbd1fe9c2019-06-29 16:35:48 -070040
41 new_state->test_mode = ds->IsTestMode();
42 new_state->fms_attached = ds->IsFmsAttached();
43 new_state->enabled = ds->IsEnabled();
44 new_state->autonomous = ds->IsAutonomous();
45 new_state->team_id = team_id_;
46 new_state->fake = false;
47
48 for (uint8_t i = 0;
49 i < sizeof(new_state->joysticks) / sizeof(::aos::Joystick); ++i) {
50 new_state->joysticks[i].buttons = ds->GetStickButtons(i);
51 for (int j = 0; j < 6; ++j) {
52 new_state->joysticks[i].axis[j] = ds->GetStickAxis(i, j);
53 }
54 if (ds->GetStickPOVCount(i) > 0) {
55 new_state->joysticks[i].pov = ds->GetStickPOV(i, 0);
56 }
57 LOG_STRUCT(DEBUG, "joystick_state", *new_state);
Tyler Chatowc92b4872019-02-23 21:42:11 -080058 }
Austin Schuhbd1fe9c2019-06-29 16:35:48 -070059 if (!new_state.Send()) {
60 LOG(WARNING, "sending joystick_state failed\n");
61 }
62 });
63 }
64 });
Brian Silvermanda45b6c2014-12-28 11:36:50 -080065}
66
67} // namespace wpilib
68} // namespace frc971