Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 1 | // Copyright (c) FIRST and other WPILib contributors. |
| 2 | // Open Source Software; you can modify and/or share it under the terms of |
| 3 | // the WPILib BSD license file in the root directory of this project. |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 4 | |
| 5 | #include "frc/TimedRobot.h" |
| 6 | |
| 7 | #include <stdint.h> |
| 8 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 9 | #include <cstdio> |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 10 | #include <utility> |
| 11 | |
| 12 | #include <hal/DriverStation.h> |
| 13 | #include <hal/FRCUsageReporting.h> |
| 14 | #include <hal/Notifier.h> |
| 15 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 16 | #include "frc/Errors.h" |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 17 | #include "frc/Timer.h" |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 18 | |
| 19 | using namespace frc; |
| 20 | |
| 21 | void TimedRobot::StartCompetition() { |
| 22 | RobotInit(); |
| 23 | |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 24 | if constexpr (IsSimulation()) { |
| 25 | SimulationInit(); |
| 26 | } |
| 27 | |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 28 | // Tell the DS that the robot is ready to be enabled |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 29 | std::puts("\n********** Robot program startup complete **********"); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 30 | HAL_ObserveUserProgramStarting(); |
| 31 | |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 32 | // Loop forever, calling the appropriate mode-dependent function |
| 33 | while (true) { |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 34 | // We don't have to check there's an element in the queue first because |
| 35 | // there's always at least one (the constructor adds one). It's reenqueued |
| 36 | // at the end of the loop. |
| 37 | auto callback = m_callbacks.pop(); |
| 38 | |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 39 | int32_t status = 0; |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 40 | HAL_UpdateNotifierAlarm( |
| 41 | m_notifier, static_cast<uint64_t>(callback.expirationTime * 1e6), |
| 42 | &status); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 43 | FRC_CheckErrorStatus(status, "{}", "UpdateNotifierAlarm"); |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 44 | |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 45 | uint64_t curTime = HAL_WaitForNotifierAlarm(m_notifier, &status); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 46 | if (curTime == 0 || status != 0) { |
| 47 | break; |
| 48 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 49 | |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 50 | callback.func(); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 51 | |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 52 | callback.expirationTime += callback.period; |
| 53 | m_callbacks.push(std::move(callback)); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 54 | |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 55 | // Process all other callbacks that are ready to run |
| 56 | while (static_cast<uint64_t>(m_callbacks.top().expirationTime * 1e6) <= |
| 57 | curTime) { |
| 58 | callback = m_callbacks.pop(); |
| 59 | |
| 60 | callback.func(); |
| 61 | |
| 62 | callback.expirationTime += callback.period; |
| 63 | m_callbacks.push(std::move(callback)); |
| 64 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 65 | } |
| 66 | } |
| 67 | |
| 68 | void TimedRobot::EndCompetition() { |
| 69 | int32_t status = 0; |
| 70 | HAL_StopNotifier(m_notifier, &status); |
| 71 | } |
| 72 | |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 73 | TimedRobot::TimedRobot(double period) : TimedRobot(units::second_t(period)) {} |
| 74 | |
| 75 | TimedRobot::TimedRobot(units::second_t period) : IterativeRobotBase(period) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 76 | m_startTime = Timer::GetFPGATimestamp(); |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 77 | AddPeriodic([=] { LoopFunc(); }, period); |
| 78 | |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 79 | int32_t status = 0; |
| 80 | m_notifier = HAL_InitializeNotifier(&status); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 81 | FRC_CheckErrorStatus(status, "{}", "InitializeNotifier"); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 82 | HAL_SetNotifierName(m_notifier, "TimedRobot", &status); |
| 83 | |
| 84 | HAL_Report(HALUsageReporting::kResourceType_Framework, |
| 85 | HALUsageReporting::kFramework_Timed); |
| 86 | } |
| 87 | |
| 88 | TimedRobot::~TimedRobot() { |
| 89 | int32_t status = 0; |
| 90 | |
| 91 | HAL_StopNotifier(m_notifier, &status); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 92 | FRC_ReportError(status, "{}", "StopNotifier"); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 93 | |
| 94 | HAL_CleanNotifier(m_notifier, &status); |
| 95 | } |
| 96 | |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 97 | void TimedRobot::AddPeriodic(std::function<void()> callback, |
| 98 | units::second_t period, units::second_t offset) { |
| 99 | m_callbacks.emplace(callback, m_startTime, period, offset); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 100 | } |