blob: 7b1b029bd743b454365c4eac9562bbfa7941f46c [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
Brian Silvermane48dbc12017-02-04 20:06:29 -08008#include "HAL/HAL.h"
Parker Schuhd3b7a8872018-02-19 16:42:27 -08009#include "frc971/wpilib/ahal/DriverStation.h"
Brian Silvermanda45b6c2014-12-28 11:36:50 -080010
11namespace frc971 {
12namespace wpilib {
13
14void JoystickSender::operator()() {
Parker Schuhd3b7a8872018-02-19 16:42:27 -080015 frc::DriverStation *const ds = &frc::DriverStation::GetInstance();
Brian Silvermanbd925f92014-12-31 14:28:06 -080016 ::aos::SetCurrentThreadName("DSReader");
Brian Silvermanda45b6c2014-12-28 11:36:50 -080017 uint16_t team_id = ::aos::network::GetTeamNumber();
18
Brian Silvermanbd925f92014-12-31 14:28:06 -080019 ::aos::SetCurrentThreadRealtimePriority(29);
20
Parker Schuhd3b7a8872018-02-19 16:42:27 -080021 // TODO(Brian): Fix the potential deadlock when stopping here (condition
22 // variable / mutex needs to get exposed all the way out or something).
Brian Silvermanda45b6c2014-12-28 11:36:50 -080023 while (run_) {
Parker Schuhd3b7a8872018-02-19 16:42:27 -080024 ds->RunIteration([&]() {
25 auto new_state = ::aos::joystick_state.MakeMessage();
Brian Silvermanda45b6c2014-12-28 11:36:50 -080026
Parker Schuhd3b7a8872018-02-19 16:42:27 -080027 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 Silvermanda45b6c2014-12-28 11:36:50 -080034 }
Parker Schuhd3b7a8872018-02-19 16:42:27 -080035 HAL_FreeMatchInfo(&match_info);
Brian Silvermanda45b6c2014-12-28 11:36:50 -080036
Parker Schuhd3b7a8872018-02-19 16:42:27 -080037 new_state->test_mode = ds->IsTestMode();
38 new_state->fms_attached = ds->IsFmsAttached();
39 new_state->enabled = ds->IsEnabled();
40 new_state->autonomous = ds->IsAutonomous();
41 new_state->team_id = team_id;
42 new_state->fake = false;
43
Tyler Chatowc92b4872019-02-23 21:42:11 -080044 for (uint8_t i = 0;
45 i < sizeof(new_state->joysticks) / sizeof(::aos::Joystick); ++i) {
Parker Schuhd3b7a8872018-02-19 16:42:27 -080046 new_state->joysticks[i].buttons = ds->GetStickButtons(i);
47 for (int j = 0; j < 6; ++j) {
48 new_state->joysticks[i].axis[j] = ds->GetStickAxis(i, j);
49 }
Tyler Chatowc92b4872019-02-23 21:42:11 -080050 if (ds->GetStickPOVCount(i) > 0) {
51 new_state->joysticks[i].pov = ds->GetStickPOV(i, 0);
52 }
Parker Schuhd3b7a8872018-02-19 16:42:27 -080053 LOG_STRUCT(DEBUG, "joystick_state", *new_state);
Parker Schuhd3b7a8872018-02-19 16:42:27 -080054 }
55 if (!new_state.Send()) {
56 LOG(WARNING, "sending joystick_state failed\n");
57 }
58 });
Brian Silvermanda45b6c2014-12-28 11:36:50 -080059 }
60}
61
62} // namespace wpilib
63} // namespace frc971