blob: a9358dde315fc47261559987f8d6368ed6beacf3 [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
James Kuszmaul4b81d302019-12-14 20:53:14 -080014#include <hal/DriverStation.h>
15#include <hal/FRCUsageReporting.h>
16#include <hal/Notifier.h>
Brian Silverman41cdd3e2019-01-19 19:48:58 -080017
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
James Kuszmaul4f3ad3c2019-12-01 16:35:21 -080030 m_expirationTime = units::second_t{Timer::GetFPGATimestamp()} + m_period;
Brian Silverman41cdd3e2019-01-19 19:48:58 -080031 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
James Kuszmaul4b81d302019-12-14 20:53:14 -080048void TimedRobot::EndCompetition() {
49 int32_t status = 0;
50 HAL_StopNotifier(m_notifier, &status);
51}
52
James Kuszmaul4f3ad3c2019-12-01 16:35:21 -080053units::second_t TimedRobot::GetPeriod() const {
54 return units::second_t(m_period);
55}
Brian Silverman41cdd3e2019-01-19 19:48:58 -080056
James Kuszmaul4f3ad3c2019-12-01 16:35:21 -080057TimedRobot::TimedRobot(double period) : TimedRobot(units::second_t(period)) {}
58
59TimedRobot::TimedRobot(units::second_t period) : IterativeRobotBase(period) {
Brian Silverman41cdd3e2019-01-19 19:48:58 -080060 int32_t status = 0;
61 m_notifier = HAL_InitializeNotifier(&status);
James Kuszmaul4b81d302019-12-14 20:53:14 -080062 wpi_setHALError(status);
63 HAL_SetNotifierName(m_notifier, "TimedRobot", &status);
Brian Silverman41cdd3e2019-01-19 19:48:58 -080064
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);
James Kuszmaul4b81d302019-12-14 20:53:14 -080073 wpi_setHALError(status);
Brian Silverman41cdd3e2019-01-19 19:48:58 -080074
75 HAL_CleanNotifier(m_notifier, &status);
76}
77
Brian Silverman41cdd3e2019-01-19 19:48:58 -080078void TimedRobot::UpdateAlarm() {
79 int32_t status = 0;
80 HAL_UpdateNotifierAlarm(
81 m_notifier, static_cast<uint64_t>(m_expirationTime * 1e6), &status);
James Kuszmaul4b81d302019-12-14 20:53:14 -080082 wpi_setHALError(status);
Brian Silverman41cdd3e2019-01-19 19:48:58 -080083}