Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame^] | 1 | /*----------------------------------------------------------------------------*/ |
| 2 | /* Copyright (c) 2017-2018 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/HAL.h> |
| 15 | |
| 16 | #include "frc/Timer.h" |
| 17 | #include "frc/Utility.h" |
| 18 | #include "frc/WPIErrors.h" |
| 19 | |
| 20 | using namespace frc; |
| 21 | |
| 22 | void TimedRobot::StartCompetition() { |
| 23 | RobotInit(); |
| 24 | |
| 25 | // Tell the DS that the robot is ready to be enabled |
| 26 | HAL_ObserveUserProgramStarting(); |
| 27 | |
| 28 | m_expirationTime = Timer::GetFPGATimestamp() + m_period; |
| 29 | 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 | |
| 46 | double TimedRobot::GetPeriod() const { return m_period; } |
| 47 | |
| 48 | TimedRobot::TimedRobot(double period) : IterativeRobotBase(period) { |
| 49 | int32_t status = 0; |
| 50 | m_notifier = HAL_InitializeNotifier(&status); |
| 51 | wpi_setErrorWithContext(status, HAL_GetErrorMessage(status)); |
| 52 | |
| 53 | HAL_Report(HALUsageReporting::kResourceType_Framework, |
| 54 | HALUsageReporting::kFramework_Timed); |
| 55 | } |
| 56 | |
| 57 | TimedRobot::~TimedRobot() { |
| 58 | int32_t status = 0; |
| 59 | |
| 60 | HAL_StopNotifier(m_notifier, &status); |
| 61 | wpi_setErrorWithContext(status, HAL_GetErrorMessage(status)); |
| 62 | |
| 63 | HAL_CleanNotifier(m_notifier, &status); |
| 64 | } |
| 65 | |
| 66 | TimedRobot::TimedRobot(TimedRobot&& rhs) |
| 67 | : IterativeRobotBase(std::move(rhs)), |
| 68 | m_expirationTime(std::move(rhs.m_expirationTime)) { |
| 69 | std::swap(m_notifier, rhs.m_notifier); |
| 70 | } |
| 71 | |
| 72 | TimedRobot& TimedRobot::operator=(TimedRobot&& rhs) { |
| 73 | IterativeRobotBase::operator=(std::move(rhs)); |
| 74 | ErrorBase::operator=(std::move(rhs)); |
| 75 | |
| 76 | std::swap(m_notifier, rhs.m_notifier); |
| 77 | m_expirationTime = std::move(rhs.m_expirationTime); |
| 78 | |
| 79 | return *this; |
| 80 | } |
| 81 | |
| 82 | void TimedRobot::UpdateAlarm() { |
| 83 | int32_t status = 0; |
| 84 | HAL_UpdateNotifierAlarm( |
| 85 | m_notifier, static_cast<uint64_t>(m_expirationTime * 1e6), &status); |
| 86 | wpi_setErrorWithContext(status, HAL_GetErrorMessage(status)); |
| 87 | } |