blob: e005475c6e8d467f111b1ec8df62d3d06ecea3f8 [file] [log] [blame]
Brian Silvermanda45b6c2014-12-28 11:36:50 -08001#include "frc971/wpilib/joystick_sender.h"
2
John Park33858a32018-09-28 23:05:48 -07003#include "aos/robot_state/robot_state.q.h"
John Park398c74a2018-10-20 21:17:39 -07004#include "aos/init.h"
John Park33858a32018-09-28 23:05:48 -07005#include "aos/network/team_number.h"
6#include "aos/logging/queue_logging.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
44 for (int i = 0; i < 4; ++i) {
45 new_state->joysticks[i].buttons = ds->GetStickButtons(i);
46 for (int j = 0; j < 6; ++j) {
47 new_state->joysticks[i].axis[j] = ds->GetStickAxis(i, j);
48 }
49 LOG_STRUCT(DEBUG, "joystick_state", *new_state);
50
51 }
52 if (!new_state.Send()) {
53 LOG(WARNING, "sending joystick_state failed\n");
54 }
55 });
Brian Silvermanda45b6c2014-12-28 11:36:50 -080056 }
57}
58
59} // namespace wpilib
60} // namespace frc971