blob: a9358dde315fc47261559987f8d6368ed6beacf3 [file] [log] [blame]
Brian Silverman8fce7482020-01-05 13:18:21 -08001/*----------------------------------------------------------------------------*/
2/* Copyright (c) 2017-2019 FIRST. All Rights Reserved. */
3/* 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/DriverStation.h>
15#include <hal/FRCUsageReporting.h>
16#include <hal/Notifier.h>
17
18#include "frc/Timer.h"
19#include "frc/Utility.h"
20#include "frc/WPIErrors.h"
21
22using namespace frc;
23
24void TimedRobot::StartCompetition() {
25 RobotInit();
26
27 // Tell the DS that the robot is ready to be enabled
28 HAL_ObserveUserProgramStarting();
29
30 m_expirationTime = units::second_t{Timer::GetFPGATimestamp()} + m_period;
31 UpdateAlarm();
32
33 // Loop forever, calling the appropriate mode-dependent function
34 while (true) {
35 int32_t status = 0;
36 uint64_t curTime = HAL_WaitForNotifierAlarm(m_notifier, &status);
37 if (curTime == 0 || status != 0) break;
38
39 m_expirationTime += m_period;
40
41 UpdateAlarm();
42
43 // Call callback
44 LoopFunc();
45 }
46}
47
48void TimedRobot::EndCompetition() {
49 int32_t status = 0;
50 HAL_StopNotifier(m_notifier, &status);
51}
52
53units::second_t TimedRobot::GetPeriod() const {
54 return units::second_t(m_period);
55}
56
57TimedRobot::TimedRobot(double period) : TimedRobot(units::second_t(period)) {}
58
59TimedRobot::TimedRobot(units::second_t period) : IterativeRobotBase(period) {
60 int32_t status = 0;
61 m_notifier = HAL_InitializeNotifier(&status);
62 wpi_setHALError(status);
63 HAL_SetNotifierName(m_notifier, "TimedRobot", &status);
64
65 HAL_Report(HALUsageReporting::kResourceType_Framework,
66 HALUsageReporting::kFramework_Timed);
67}
68
69TimedRobot::~TimedRobot() {
70 int32_t status = 0;
71
72 HAL_StopNotifier(m_notifier, &status);
73 wpi_setHALError(status);
74
75 HAL_CleanNotifier(m_notifier, &status);
76}
77
78void TimedRobot::UpdateAlarm() {
79 int32_t status = 0;
80 HAL_UpdateNotifierAlarm(
81 m_notifier, static_cast<uint64_t>(m_expirationTime * 1e6), &status);
82 wpi_setHALError(status);
83}