blob: ffff2ddcb80eea6d076887d1e7e47c4453590b57 [file] [log] [blame]
Brian Silverman41cdd3e2019-01-19 19:48:58 -08001/*----------------------------------------------------------------------------*/
James Kuszmaul4f3ad3c2019-12-01 16:35:21 -08002/* Copyright (c) 2017-2019 FIRST. All Rights Reserved. */
Brian Silverman41cdd3e2019-01-19 19:48:58 -08003/* Open Source Software - may be modified and shared by FRC teams. The code */
4/* must be accompanied by the FIRST BSD license file in the root directory of */
5/* the project. */
6/*----------------------------------------------------------------------------*/
7
8#include "frc/TimedRobot.h"
9
10#include <stdint.h>
11
12#include <utility>
13
14#include <hal/HAL.h>
15
16#include "frc/Timer.h"
17#include "frc/Utility.h"
18#include "frc/WPIErrors.h"
19
20using namespace frc;
21
22void TimedRobot::StartCompetition() {
23 RobotInit();
24
25 // Tell the DS that the robot is ready to be enabled
26 HAL_ObserveUserProgramStarting();
27
James Kuszmaul4f3ad3c2019-12-01 16:35:21 -080028 m_expirationTime = units::second_t{Timer::GetFPGATimestamp()} + m_period;
Brian Silverman41cdd3e2019-01-19 19:48:58 -080029 UpdateAlarm();
30
31 // Loop forever, calling the appropriate mode-dependent function
32 while (true) {
33 int32_t status = 0;
34 uint64_t curTime = HAL_WaitForNotifierAlarm(m_notifier, &status);
35 if (curTime == 0 || status != 0) break;
36
37 m_expirationTime += m_period;
38
39 UpdateAlarm();
40
41 // Call callback
42 LoopFunc();
43 }
44}
45
James Kuszmaul4f3ad3c2019-12-01 16:35:21 -080046units::second_t TimedRobot::GetPeriod() const {
47 return units::second_t(m_period);
48}
Brian Silverman41cdd3e2019-01-19 19:48:58 -080049
James Kuszmaul4f3ad3c2019-12-01 16:35:21 -080050TimedRobot::TimedRobot(double period) : TimedRobot(units::second_t(period)) {}
51
52TimedRobot::TimedRobot(units::second_t period) : IterativeRobotBase(period) {
Brian Silverman41cdd3e2019-01-19 19:48:58 -080053 int32_t status = 0;
54 m_notifier = HAL_InitializeNotifier(&status);
55 wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
56
57 HAL_Report(HALUsageReporting::kResourceType_Framework,
58 HALUsageReporting::kFramework_Timed);
59}
60
61TimedRobot::~TimedRobot() {
62 int32_t status = 0;
63
64 HAL_StopNotifier(m_notifier, &status);
65 wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
66
67 HAL_CleanNotifier(m_notifier, &status);
68}
69
Brian Silverman41cdd3e2019-01-19 19:48:58 -080070void TimedRobot::UpdateAlarm() {
71 int32_t status = 0;
72 HAL_UpdateNotifierAlarm(
73 m_notifier, static_cast<uint64_t>(m_expirationTime * 1e6), &status);
74 wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
75}