Files
allwpilib/wpilibc/src/main/native/cpp/TimedRobot.cpp

107 lines
3.4 KiB
C++
Raw Normal View History

// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
2017-07-08 10:50:56 -04:00
#include "frc/TimedRobot.h"
2017-07-08 10:50:56 -04:00
#include <stdint.h>
2017-07-08 10:50:56 -04:00
#include <cstdio>
#include <utility>
#include <hal/DriverStation.h>
#include <hal/FRCUsageReporting.h>
#include <hal/Notifier.h>
2017-07-08 10:50:56 -04:00
#include "frc/Errors.h"
2017-07-08 10:50:56 -04:00
using namespace frc;
void TimedRobot::StartCompetition() {
RobotInit();
if constexpr (IsSimulation()) {
SimulationInit();
}
// Tell the DS that the robot is ready to be enabled
std::puts("\n********** Robot program startup complete **********");
HAL_ObserveUserProgramStarting();
// Loop forever, calling the appropriate mode-dependent function
2017-07-08 10:50:56 -04:00
while (true) {
// We don't have to check there's an element in the queue first because
// there's always at least one (the constructor adds one). It's reenqueued
// at the end of the loop.
auto callback = m_callbacks.pop();
int32_t status = 0;
HAL_UpdateNotifierAlarm(m_notifier, callback.expirationTime.count(),
&status);
FRC_CheckErrorStatus(status, "UpdateNotifierAlarm");
std::chrono::microseconds currentTime{
HAL_WaitForNotifierAlarm(m_notifier, &status)};
if (currentTime.count() == 0 || status != 0) {
break;
}
callback.func();
// Increment the expiration time by the number of full periods it's behind
// plus one to avoid rapid repeat fires from a large loop overrun. We assume
// currentTime ≥ expirationTime rather than checking for it since the
// callback wouldn't be running otherwise.
callback.expirationTime +=
callback.period + (currentTime - callback.expirationTime) /
callback.period * callback.period;
m_callbacks.push(std::move(callback));
// Process all other callbacks that are ready to run
while (m_callbacks.top().expirationTime <= currentTime) {
callback = m_callbacks.pop();
callback.func();
callback.expirationTime +=
callback.period + (currentTime - callback.expirationTime) /
callback.period * callback.period;
m_callbacks.push(std::move(callback));
}
2017-07-08 10:50:56 -04:00
}
}
void TimedRobot::EndCompetition() {
int32_t status = 0;
HAL_StopNotifier(m_notifier, &status);
}
TimedRobot::TimedRobot(units::second_t period) : IterativeRobotBase(period) {
m_startTime = std::chrono::microseconds{RobotController::GetFPGATime()};
AddPeriodic([=, this] { LoopFunc(); }, period);
int32_t status = 0;
m_notifier = HAL_InitializeNotifier(&status);
FRC_CheckErrorStatus(status, "InitializeNotifier");
2019-11-09 11:41:58 -08:00
HAL_SetNotifierName(m_notifier, "TimedRobot", &status);
2017-07-08 10:50:56 -04:00
HAL_Report(HALUsageReporting::kResourceType_Framework,
HALUsageReporting::kFramework_Timed);
2017-07-08 10:50:56 -04:00
}
TimedRobot::~TimedRobot() {
if (m_notifier != HAL_kInvalidHandle) {
int32_t status = 0;
HAL_StopNotifier(m_notifier, &status);
FRC_ReportError(status, "StopNotifier");
}
}
void TimedRobot::AddPeriodic(std::function<void()> callback,
units::second_t period, units::second_t offset) {
m_callbacks.emplace(
callback, m_startTime,
std::chrono::microseconds{static_cast<int64_t>(period.value() * 1e6)},
std::chrono::microseconds{static_cast<int64_t>(offset.value() * 1e6)});
}