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
|
|
|
|
2025-11-08 15:08:38 -08:00
|
|
|
#include "wpi/framework/TimedRobot.hpp"
|
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>
|
|
|
|
|
|
2025-11-07 19:56:21 -05:00
|
|
|
#include "wpi/hal/DriverStation.h"
|
2026-01-04 00:41:53 -08:00
|
|
|
#include "wpi/hal/Notifier.hpp"
|
|
|
|
|
#include "wpi/hal/UsageReporting.hpp"
|
2025-11-07 19:56:21 -05:00
|
|
|
#include "wpi/system/Errors.hpp"
|
2018-04-30 00:00:09 -07:00
|
|
|
|
2025-11-07 20:00:05 -05:00
|
|
|
using namespace wpi;
|
2017-07-08 10:50:56 -04:00
|
|
|
|
|
|
|
|
void TimedRobot::StartCompetition() {
|
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;
|
2025-12-09 20:28:15 -07:00
|
|
|
HAL_SetNotifierAlarm(m_notifier, callback.expirationTime.count(), 0, true,
|
|
|
|
|
true, &status);
|
|
|
|
|
WPILIB_CheckErrorStatus(status, "SetNotifierAlarm");
|
2025-11-29 11:00:18 -08:00
|
|
|
|
|
|
|
|
if (WPI_WaitForObject(m_notifier) == 0) {
|
2020-12-28 12:58:06 -08:00
|
|
|
break;
|
|
|
|
|
}
|
2018-04-30 00:00:09 -07:00
|
|
|
|
2024-11-16 10:43:38 -05:00
|
|
|
m_loopStartTimeUs = RobotController::GetFPGATime();
|
2025-11-29 11:00:18 -08:00
|
|
|
std::chrono::microseconds currentTime{m_loopStartTimeUs};
|
2024-11-16 10:43:38 -05: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() {
|
2025-11-29 11:00:18 -08:00
|
|
|
HAL_DestroyNotifier(m_notifier);
|
|
|
|
|
m_notifier = HAL_kInvalidHandle;
|
2019-11-05 21:33:09 -08:00
|
|
|
}
|
|
|
|
|
|
2025-11-07 20:01:58 -05:00
|
|
|
TimedRobot::TimedRobot(wpi::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;
|
2025-11-29 11:00:18 -08:00
|
|
|
m_notifier = HAL_CreateNotifier(&status);
|
2025-11-07 20:00:43 -05:00
|
|
|
WPILIB_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
|
|
|
|
2025-02-07 12:37:23 -08:00
|
|
|
HAL_ReportUsage("Framework", "TimedRobot");
|
2017-07-08 10:50:56 -04:00
|
|
|
}
|
|
|
|
|
|
2025-11-07 20:01:58 -05:00
|
|
|
TimedRobot::TimedRobot(wpi::units::hertz_t frequency)
|
|
|
|
|
: TimedRobot{1 / frequency} {}
|
2025-10-29 03:18:55 +00:00
|
|
|
|
2018-04-30 00:00:09 -07:00
|
|
|
TimedRobot::~TimedRobot() {
|
2024-10-15 22:56:13 -04:00
|
|
|
if (m_notifier != HAL_kInvalidHandle) {
|
2025-11-29 11:00:18 -08:00
|
|
|
HAL_DestroyNotifier(m_notifier);
|
2024-10-15 22:56:13 -04:00
|
|
|
}
|
2018-04-30 00:00:09 -07:00
|
|
|
}
|
|
|
|
|
|
2024-11-16 10:43:38 -05:00
|
|
|
uint64_t TimedRobot::GetLoopStartTime() {
|
|
|
|
|
return m_loopStartTimeUs;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-16 17:56:37 -07:00
|
|
|
void TimedRobot::AddPeriodic(std::function<void()> callback,
|
2025-11-07 20:01:58 -05:00
|
|
|
wpi::units::second_t period,
|
|
|
|
|
wpi::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
|
|
|
}
|