2017-07-08 10:50:56 -04:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2019-08-17 00:56:48 -04:00
|
|
|
/* Copyright (c) 2017-2019 FIRST. All Rights Reserved. */
|
2017-07-08 10:50:56 -04:00
|
|
|
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
|
|
|
|
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
|
|
|
|
/* the project. */
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
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>
|
|
|
|
|
|
2018-07-20 00:03:45 -07:00
|
|
|
#include <hal/HAL.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();
|
|
|
|
|
|
|
|
|
|
// Tell the DS that the robot is ready to be enabled
|
|
|
|
|
HAL_ObserveUserProgramStarting();
|
|
|
|
|
|
2018-04-30 00:00:09 -07:00
|
|
|
m_expirationTime = Timer::GetFPGATimestamp() + m_period;
|
|
|
|
|
UpdateAlarm();
|
|
|
|
|
|
|
|
|
|
// Loop forever, calling the appropriate mode-dependent function
|
2017-07-08 10:50:56 -04:00
|
|
|
while (true) {
|
2018-04-30 00:00:09 -07:00
|
|
|
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();
|
2017-07-08 10:50:56 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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) {
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
m_notifier = HAL_InitializeNotifier(&status);
|
|
|
|
|
wpi_setErrorWithContext(status, HAL_GetErrorMessage(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);
|
|
|
|
|
wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
|
|
|
|
|
|
|
|
|
|
HAL_CleanNotifier(m_notifier, &status);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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));
|
|
|
|
|
}
|