Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 1 | /*----------------------------------------------------------------------------*/ |
James Kuszmaul | 4f3ad3c | 2019-12-01 16:35:21 -0800 | [diff] [blame] | 2 | /* Copyright (c) 2017-2019 FIRST. All Rights Reserved. */ |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 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 | |
James Kuszmaul | 4b81d30 | 2019-12-14 20:53:14 -0800 | [diff] [blame^] | 14 | #include <hal/DriverStation.h> |
| 15 | #include <hal/FRCUsageReporting.h> |
| 16 | #include <hal/Notifier.h> |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 17 | |
| 18 | #include "frc/Timer.h" |
| 19 | #include "frc/Utility.h" |
| 20 | #include "frc/WPIErrors.h" |
| 21 | |
| 22 | using namespace frc; |
| 23 | |
| 24 | void TimedRobot::StartCompetition() { |
| 25 | RobotInit(); |
| 26 | |
| 27 | // Tell the DS that the robot is ready to be enabled |
| 28 | HAL_ObserveUserProgramStarting(); |
| 29 | |
James Kuszmaul | 4f3ad3c | 2019-12-01 16:35:21 -0800 | [diff] [blame] | 30 | m_expirationTime = units::second_t{Timer::GetFPGATimestamp()} + m_period; |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 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 | |
James Kuszmaul | 4b81d30 | 2019-12-14 20:53:14 -0800 | [diff] [blame^] | 48 | void TimedRobot::EndCompetition() { |
| 49 | int32_t status = 0; |
| 50 | HAL_StopNotifier(m_notifier, &status); |
| 51 | } |
| 52 | |
James Kuszmaul | 4f3ad3c | 2019-12-01 16:35:21 -0800 | [diff] [blame] | 53 | units::second_t TimedRobot::GetPeriod() const { |
| 54 | return units::second_t(m_period); |
| 55 | } |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 56 | |
James Kuszmaul | 4f3ad3c | 2019-12-01 16:35:21 -0800 | [diff] [blame] | 57 | TimedRobot::TimedRobot(double period) : TimedRobot(units::second_t(period)) {} |
| 58 | |
| 59 | TimedRobot::TimedRobot(units::second_t period) : IterativeRobotBase(period) { |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 60 | int32_t status = 0; |
| 61 | m_notifier = HAL_InitializeNotifier(&status); |
James Kuszmaul | 4b81d30 | 2019-12-14 20:53:14 -0800 | [diff] [blame^] | 62 | wpi_setHALError(status); |
| 63 | HAL_SetNotifierName(m_notifier, "TimedRobot", &status); |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 64 | |
| 65 | HAL_Report(HALUsageReporting::kResourceType_Framework, |
| 66 | HALUsageReporting::kFramework_Timed); |
| 67 | } |
| 68 | |
| 69 | TimedRobot::~TimedRobot() { |
| 70 | int32_t status = 0; |
| 71 | |
| 72 | HAL_StopNotifier(m_notifier, &status); |
James Kuszmaul | 4b81d30 | 2019-12-14 20:53:14 -0800 | [diff] [blame^] | 73 | wpi_setHALError(status); |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 74 | |
| 75 | HAL_CleanNotifier(m_notifier, &status); |
| 76 | } |
| 77 | |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 78 | void TimedRobot::UpdateAlarm() { |
| 79 | int32_t status = 0; |
| 80 | HAL_UpdateNotifierAlarm( |
| 81 | m_notifier, static_cast<uint64_t>(m_expirationTime * 1e6), &status); |
James Kuszmaul | 4b81d30 | 2019-12-14 20:53:14 -0800 | [diff] [blame^] | 82 | wpi_setHALError(status); |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 83 | } |