2020-12-26 14:12:05 -08:00
|
|
|
// 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
|
|
|
|
2018-07-20 00:03:45 -07:00
|
|
|
#include "frc/TimedRobot.h"
|
2017-07-08 10:50:56 -04:00
|
|
|
|
2018-04-30 00:00:09 -07:00
|
|
|
#include <stdint.h>
|
2017-07-08 10:50:56 -04:00
|
|
|
|
2021-08-05 23:54:50 -07:00
|
|
|
#include <cstdio>
|
2018-09-24 00:08:25 -07:00
|
|
|
#include <utility>
|
|
|
|
|
|
2019-11-08 22:53:20 -08:00
|
|
|
#include <hal/DriverStation.h>
|
|
|
|
|
#include <hal/FRCUsageReporting.h>
|
|
|
|
|
#include <hal/Notifier.h>
|
2017-07-08 10:50:56 -04:00
|
|
|
|
2021-04-18 20:35:29 -07:00
|
|
|
#include "frc/Errors.h"
|
2018-04-30 00:00:09 -07:00
|
|
|
|
2017-07-08 10:50:56 -04:00
|
|
|
using namespace frc;
|
|
|
|
|
|
|
|
|
|
void TimedRobot::StartCompetition() {
|
2017-09-05 23:57:26 -07:00
|
|
|
RobotInit();
|
|
|
|
|
|
2020-10-03 22:26:19 -07:00
|
|
|
if constexpr (IsSimulation()) {
|
2020-02-19 02:05:16 -05:00
|
|
|
SimulationInit();
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-05 23:57:26 -07:00
|
|
|
// Tell the DS that the robot is ready to be enabled
|
2021-08-05 23:54:50 -07:00
|
|
|
std::puts("\n********** Robot program startup complete **********");
|
2017-09-05 23:57:26 -07:00
|
|
|
HAL_ObserveUserProgramStarting();
|
|
|
|
|
|
2018-04-30 00:00:09 -07:00
|
|
|
// Loop forever, calling the appropriate mode-dependent function
|
2017-07-08 10:50:56 -04:00
|
|
|
while (true) {
|
2020-10-16 17:56:37 -07:00
|
|
|
// 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();
|
|
|
|
|
|
2018-04-30 00:00:09 -07:00
|
|
|
int32_t status = 0;
|
2024-07-16 22:56:36 -07:00
|
|
|
HAL_UpdateNotifierAlarm(m_notifier, callback.expirationTime.count(),
|
|
|
|
|
&status);
|
2022-10-19 10:49:27 -07:00
|
|
|
FRC_CheckErrorStatus(status, "UpdateNotifierAlarm");
|
2020-10-16 17:56:37 -07:00
|
|
|
|
2024-07-16 22:56:36 -07:00
|
|
|
std::chrono::microseconds currentTime{
|
|
|
|
|
HAL_WaitForNotifierAlarm(m_notifier, &status)};
|
|
|
|
|
if (currentTime.count() == 0 || status != 0) {
|
2020-12-28 12:58:06 -08:00
|
|
|
break;
|
|
|
|
|
}
|
2018-04-30 00:00:09 -07:00
|
|
|
|
2020-10-16 17:56:37 -07:00
|
|
|
callback.func();
|
|
|
|
|
|
2024-07-16 22:56:36 -07:00
|
|
|
// 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;
|
2020-10-16 17:56:37 -07:00
|
|
|
m_callbacks.push(std::move(callback));
|
2018-04-30 00:00:09 -07:00
|
|
|
|
2020-10-16 17:56:37 -07:00
|
|
|
// Process all other callbacks that are ready to run
|
2024-07-16 22:56:36 -07:00
|
|
|
while (m_callbacks.top().expirationTime <= currentTime) {
|
2020-10-16 17:56:37 -07:00
|
|
|
callback = m_callbacks.pop();
|
2018-04-30 00:00:09 -07:00
|
|
|
|
2020-10-16 17:56:37 -07:00
|
|
|
callback.func();
|
|
|
|
|
|
2024-07-16 22:56:36 -07:00
|
|
|
callback.expirationTime +=
|
|
|
|
|
callback.period + (currentTime - callback.expirationTime) /
|
|
|
|
|
callback.period * callback.period;
|
2020-10-16 17:56:37 -07:00
|
|
|
m_callbacks.push(std::move(callback));
|
|
|
|
|
}
|
2017-07-08 10:50:56 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-05 21:33:09 -08:00
|
|
|
void TimedRobot::EndCompetition() {
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
HAL_StopNotifier(m_notifier, &status);
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-17 00:56:48 -04:00
|
|
|
TimedRobot::TimedRobot(units::second_t period) : IterativeRobotBase(period) {
|
2024-07-16 22:56:36 -07:00
|
|
|
m_startTime = std::chrono::microseconds{RobotController::GetFPGATime()};
|
2022-10-15 16:33:14 -07:00
|
|
|
AddPeriodic([=, this] { LoopFunc(); }, period);
|
2020-10-16 17:56:37 -07:00
|
|
|
|
2019-08-17 00:56:48 -04:00
|
|
|
int32_t status = 0;
|
|
|
|
|
m_notifier = HAL_InitializeNotifier(&status);
|
2022-10-19 10:49:27 -07:00
|
|
|
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,
|
2018-07-25 19:58:05 -07:00
|
|
|
HALUsageReporting::kFramework_Timed);
|
2017-07-08 10:50:56 -04:00
|
|
|
}
|
|
|
|
|
|
2018-04-30 00:00:09 -07:00
|
|
|
TimedRobot::~TimedRobot() {
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
|
|
|
|
|
HAL_StopNotifier(m_notifier, &status);
|
2022-10-19 10:49:27 -07:00
|
|
|
FRC_ReportError(status, "StopNotifier");
|
2018-04-30 00:00:09 -07:00
|
|
|
}
|
|
|
|
|
|
2020-10-16 17:56:37 -07:00
|
|
|
void TimedRobot::AddPeriodic(std::function<void()> callback,
|
|
|
|
|
units::second_t period, units::second_t offset) {
|
2024-07-16 22:56:36 -07:00
|
|
|
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)});
|
2018-04-30 00:00:09 -07:00
|
|
|
}
|