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