TimedRobot now uses the Notifier HAL API (#942)

Fixes #941.
This commit is contained in:
Tyler Veness
2018-04-30 00:00:09 -07:00
committed by Peter Johnson
parent e7cf6bf7c5
commit 93859eb84f
4 changed files with 94 additions and 29 deletions

View File

@@ -7,10 +7,14 @@
#include "TimedRobot.h"
#include <chrono>
#include <stdint.h>
#include <HAL/HAL.h>
#include "Timer.h"
#include "Utility.h"
#include "WPIErrors.h"
using namespace frc;
/**
@@ -22,11 +26,23 @@ void TimedRobot::StartCompetition() {
// Tell the DS that the robot is ready to be enabled
HAL_ObserveUserProgramStarting();
// Loop forever, calling the appropriate mode-dependent function
m_startLoop = true;
m_loop->StartPeriodic(m_period);
m_expirationTime = Timer::GetFPGATimestamp() + m_period;
UpdateAlarm();
// Loop forever, calling the appropriate mode-dependent function
while (true) {
std::this_thread::sleep_for(std::chrono::hours(24));
int32_t status = 0;
uint64_t curTime = HAL_WaitForNotifierAlarm(m_notifier, &status);
if (curTime == 0 || status != 0) break;
m_expirationTime += m_period;
UpdateAlarm();
// Call callback
LoopFunc();
}
}
@@ -43,7 +59,8 @@ void TimedRobot::SetPeriod(double period) {
m_period = period;
if (m_startLoop) {
m_loop->StartPeriodic(period);
m_expirationTime = Timer::GetFPGATimestamp() + period;
UpdateAlarm();
}
}
@@ -53,7 +70,9 @@ void TimedRobot::SetPeriod(double period) {
double TimedRobot::GetPeriod() const { return m_period; }
TimedRobot::TimedRobot() {
m_loop = std::make_unique<Notifier>(&TimedRobot::LoopFunc, this);
int32_t status = 0;
m_notifier = HAL_InitializeNotifier(&status);
wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
// HAL_Report(HALUsageReporting::kResourceType_Framework,
// HALUsageReporting::kFramework_Periodic);
@@ -61,4 +80,21 @@ TimedRobot::TimedRobot() {
HALUsageReporting::kFramework_Iterative);
}
TimedRobot::~TimedRobot() { m_loop->Stop(); }
TimedRobot::~TimedRobot() {
int32_t status = 0;
HAL_StopNotifier(m_notifier, &status);
wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
HAL_CleanNotifier(m_notifier, &status);
}
/**
* Update the HAL alarm time.
*/
void TimedRobot::UpdateAlarm() {
int32_t status = 0;
HAL_UpdateNotifierAlarm(
m_notifier, static_cast<uint64_t>(m_expirationTime * 1e6), &status);
wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
}