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
|
|
|
|
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
|
|
|
|
2018-07-20 00:03:45 -07:00
|
|
|
#include "frc/Timer.h"
|
|
|
|
|
#include "frc/Utility.h"
|
|
|
|
|
#include "frc/WPIErrors.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
|
|
|
|
|
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;
|
2020-10-16 17:56:37 -07:00
|
|
|
HAL_UpdateNotifierAlarm(
|
|
|
|
|
m_notifier, static_cast<uint64_t>(callback.expirationTime * 1e6),
|
|
|
|
|
&status);
|
|
|
|
|
wpi_setHALError(status);
|
|
|
|
|
|
2018-04-30 00:00:09 -07:00
|
|
|
uint64_t curTime = HAL_WaitForNotifierAlarm(m_notifier, &status);
|
|
|
|
|
if (curTime == 0 || status != 0) break;
|
|
|
|
|
|
2020-10-16 17:56:37 -07:00
|
|
|
callback.func();
|
|
|
|
|
|
|
|
|
|
callback.expirationTime += callback.period;
|
|
|
|
|
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
|
|
|
|
|
while (static_cast<uint64_t>(m_callbacks.top().expirationTime * 1e6) <=
|
|
|
|
|
curTime) {
|
|
|
|
|
callback = m_callbacks.pop();
|
2018-04-30 00:00:09 -07:00
|
|
|
|
2020-10-16 17:56:37 -07:00
|
|
|
callback.func();
|
|
|
|
|
|
|
|
|
|
callback.expirationTime += callback.period;
|
|
|
|
|
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
|
|
|
units::second_t TimedRobot::GetPeriod() const {
|
|
|
|
|
return units::second_t(m_period);
|
|
|
|
|
}
|
2018-01-27 01:01:15 -08:00
|
|
|
|
2019-08-24 21:13:29 -07:00
|
|
|
TimedRobot::TimedRobot(double period) : TimedRobot(units::second_t(period)) {}
|
2019-08-17 00:56:48 -04:00
|
|
|
|
|
|
|
|
TimedRobot::TimedRobot(units::second_t period) : IterativeRobotBase(period) {
|
2020-10-16 17:56:37 -07:00
|
|
|
m_startTime = frc2::Timer::GetFPGATimestamp();
|
|
|
|
|
AddPeriodic([=] { LoopFunc(); }, period);
|
|
|
|
|
|
2019-08-17 00:56:48 -04:00
|
|
|
int32_t status = 0;
|
|
|
|
|
m_notifier = HAL_InitializeNotifier(&status);
|
2019-11-08 22:53:20 -08:00
|
|
|
wpi_setHALError(status);
|
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);
|
2019-11-08 22:53:20 -08:00
|
|
|
wpi_setHALError(status);
|
2018-04-30 00:00:09 -07:00
|
|
|
|
|
|
|
|
HAL_CleanNotifier(m_notifier, &status);
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-16 17:56:37 -07:00
|
|
|
void TimedRobot::AddPeriodic(std::function<void()> callback,
|
|
|
|
|
units::second_t period, units::second_t offset) {
|
|
|
|
|
m_callbacks.emplace(callback, m_startTime, period, offset);
|
2018-04-30 00:00:09 -07:00
|
|
|
}
|