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. |
| 4 | |
| 5 | #include "frc/TimesliceRobot.h" |
| 6 | |
| 7 | #include "frc/Errors.h" |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 8 | |
| 9 | using namespace frc; |
| 10 | |
| 11 | TimesliceRobot::TimesliceRobot(units::second_t robotPeriodicAllocation, |
| 12 | units::second_t controllerPeriod) |
| 13 | : m_nextOffset{robotPeriodicAllocation}, |
| 14 | m_controllerPeriod{controllerPeriod} {} |
| 15 | |
| 16 | void TimesliceRobot::Schedule(std::function<void()> func, |
| 17 | units::second_t allocation) { |
| 18 | if (m_nextOffset + allocation > m_controllerPeriod) { |
| 19 | throw FRC_MakeError(err::Error, |
| 20 | "Function scheduled at offset {} with allocation {} " |
| 21 | "exceeded controller period of {}\n", |
| 22 | m_nextOffset, allocation, m_controllerPeriod); |
| 23 | } |
| 24 | |
| 25 | AddPeriodic(func, m_controllerPeriod, m_nextOffset); |
| 26 | m_nextOffset += allocation; |
| 27 | } |