2021-09-26 13:56:33 -07:00
|
|
|
// Copyright (c) FIRST and other WPILib contributors.
|
|
|
|
|
// Open Source Software; you can modify and/or share it under the terms of
|
|
|
|
|
// the WPILib BSD license file in the root directory of this project.
|
|
|
|
|
|
2025-11-07 19:56:21 -05:00
|
|
|
#include "wpi/opmode/TimesliceRobot.hpp"
|
2021-09-26 13:56:33 -07:00
|
|
|
|
2025-11-07 19:56:21 -05:00
|
|
|
#include "wpi/system/Errors.hpp"
|
2021-09-26 13:56:33 -07:00
|
|
|
|
2025-11-07 20:00:05 -05:00
|
|
|
using namespace wpi;
|
2021-09-26 13:56:33 -07:00
|
|
|
|
2025-11-07 20:00:05 -05:00
|
|
|
TimesliceRobot::TimesliceRobot(wpi::units::second_t robotPeriodicAllocation,
|
|
|
|
|
wpi::units::second_t controllerPeriod)
|
2021-09-26 13:56:33 -07:00
|
|
|
: m_nextOffset{robotPeriodicAllocation},
|
|
|
|
|
m_controllerPeriod{controllerPeriod} {}
|
|
|
|
|
|
|
|
|
|
void TimesliceRobot::Schedule(std::function<void()> func,
|
2025-11-07 20:00:05 -05:00
|
|
|
wpi::units::second_t allocation) {
|
2021-09-26 13:56:33 -07:00
|
|
|
if (m_nextOffset + allocation > m_controllerPeriod) {
|
2025-11-07 20:00:43 -05:00
|
|
|
throw WPILIB_MakeError(err::Error,
|
2021-09-26 13:56:33 -07:00
|
|
|
"Function scheduled at offset {} with allocation {} "
|
|
|
|
|
"exceeded controller period of {}\n",
|
|
|
|
|
m_nextOffset, allocation, m_controllerPeriod);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
AddPeriodic(func, m_controllerPeriod, m_nextOffset);
|
|
|
|
|
m_nextOffset += allocation;
|
|
|
|
|
}
|