Convert all year's robots to proper event loops
Each robot has a couple of event loops, one per thread. Each of these
threads corresponds to the threads from before the change. y2016 has
been tested on real hardware.
Change-Id: I99f726a8bc0498204c1a3b99f15508119eed9ad3
diff --git a/frc971/wpilib/joystick_sender.cc b/frc971/wpilib/joystick_sender.cc
index a71ab9c..a073066 100644
--- a/frc971/wpilib/joystick_sender.cc
+++ b/frc971/wpilib/joystick_sender.cc
@@ -11,51 +11,57 @@
namespace frc971 {
namespace wpilib {
-void JoystickSender::operator()() {
- frc::DriverStation *const ds = &frc::DriverStation::GetInstance();
- ::aos::SetCurrentThreadName("DSReader");
- uint16_t team_id = ::aos::network::GetTeamNumber();
+JoystickSender::JoystickSender(::aos::EventLoop *event_loop)
+ : event_loop_(event_loop),
+ joystick_state_sender_(
+ event_loop_->MakeSender<::aos::JoystickState>(".aos.joystick_state")),
+ team_id_(::aos::network::GetTeamNumber()) {
- ::aos::SetCurrentThreadRealtimePriority(29);
+ event_loop_->SetRuntimeRealtimePriority(29);
- // TODO(Brian): Fix the potential deadlock when stopping here (condition
- // variable / mutex needs to get exposed all the way out or something).
- while (run_) {
- ds->RunIteration([&]() {
- auto new_state = joystick_state_sender_.MakeMessage();
+ event_loop_->OnRun([this]() {
+ frc::DriverStation *const ds = &frc::DriverStation::GetInstance();
+ ::aos::SetCurrentThreadName("DSReader");
- HAL_MatchInfo match_info;
- auto status = HAL_GetMatchInfo(&match_info);
- if (status == 0) {
- new_state->switch_left = match_info.gameSpecificMessage[0] == 'L' ||
- match_info.gameSpecificMessage[0] == 'l';
- new_state->scale_left = match_info.gameSpecificMessage[1] == 'L' ||
- match_info.gameSpecificMessage[1] == 'l';
- }
+ // TODO(Brian): Fix the potential deadlock when stopping here (condition
+ // variable / mutex needs to get exposed all the way out or something).
+ while (event_loop_->is_running()) {
+ ds->RunIteration([&]() {
+ auto new_state = joystick_state_sender_.MakeMessage();
- new_state->test_mode = ds->IsTestMode();
- new_state->fms_attached = ds->IsFmsAttached();
- new_state->enabled = ds->IsEnabled();
- new_state->autonomous = ds->IsAutonomous();
- new_state->team_id = team_id;
- new_state->fake = false;
-
- for (uint8_t i = 0;
- i < sizeof(new_state->joysticks) / sizeof(::aos::Joystick); ++i) {
- new_state->joysticks[i].buttons = ds->GetStickButtons(i);
- for (int j = 0; j < 6; ++j) {
- new_state->joysticks[i].axis[j] = ds->GetStickAxis(i, j);
+ HAL_MatchInfo match_info;
+ auto status = HAL_GetMatchInfo(&match_info);
+ if (status == 0) {
+ new_state->switch_left = match_info.gameSpecificMessage[0] == 'L' ||
+ match_info.gameSpecificMessage[0] == 'l';
+ new_state->scale_left = match_info.gameSpecificMessage[1] == 'L' ||
+ match_info.gameSpecificMessage[1] == 'l';
}
- if (ds->GetStickPOVCount(i) > 0) {
- new_state->joysticks[i].pov = ds->GetStickPOV(i, 0);
+
+ new_state->test_mode = ds->IsTestMode();
+ new_state->fms_attached = ds->IsFmsAttached();
+ new_state->enabled = ds->IsEnabled();
+ new_state->autonomous = ds->IsAutonomous();
+ new_state->team_id = team_id_;
+ new_state->fake = false;
+
+ for (uint8_t i = 0;
+ i < sizeof(new_state->joysticks) / sizeof(::aos::Joystick); ++i) {
+ new_state->joysticks[i].buttons = ds->GetStickButtons(i);
+ for (int j = 0; j < 6; ++j) {
+ new_state->joysticks[i].axis[j] = ds->GetStickAxis(i, j);
+ }
+ if (ds->GetStickPOVCount(i) > 0) {
+ new_state->joysticks[i].pov = ds->GetStickPOV(i, 0);
+ }
+ LOG_STRUCT(DEBUG, "joystick_state", *new_state);
}
- LOG_STRUCT(DEBUG, "joystick_state", *new_state);
- }
- if (!new_state.Send()) {
- LOG(WARNING, "sending joystick_state failed\n");
- }
- });
- }
+ if (!new_state.Send()) {
+ LOG(WARNING, "sending joystick_state failed\n");
+ }
+ });
+ }
+ });
}
} // namespace wpilib