2020-12-26 14:12:05 -08: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.
|
2017-07-08 10:50:56 -04:00
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
2024-07-16 22:56:36 -07:00
|
|
|
#include <chrono>
|
2020-10-16 17:56:37 -07:00
|
|
|
#include <functional>
|
2020-12-28 10:12:52 -08:00
|
|
|
#include <utility>
|
2020-10-16 17:56:37 -07:00
|
|
|
#include <vector>
|
|
|
|
|
|
2024-09-07 13:58:15 -04:00
|
|
|
#include <hal/Notifier.h>
|
2018-10-29 12:49:17 -07:00
|
|
|
#include <hal/Types.h>
|
2025-10-29 03:18:55 +00:00
|
|
|
#include <units/frequency.h>
|
2020-10-16 17:56:37 -07:00
|
|
|
#include <units/math.h>
|
2020-06-29 22:25:09 -07:00
|
|
|
#include <units/time.h>
|
2020-10-16 17:56:37 -07:00
|
|
|
#include <wpi/priority_queue.h>
|
2017-07-08 10:50:56 -04:00
|
|
|
|
2018-07-20 00:03:45 -07:00
|
|
|
#include "frc/IterativeRobotBase.h"
|
2024-07-16 22:56:36 -07:00
|
|
|
#include "frc/RobotController.h"
|
2017-07-08 10:50:56 -04:00
|
|
|
|
|
|
|
|
namespace frc {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* TimedRobot implements the IterativeRobotBase robot program framework.
|
|
|
|
|
*
|
|
|
|
|
* The TimedRobot class is intended to be subclassed by a user creating a
|
|
|
|
|
* robot program.
|
|
|
|
|
*
|
|
|
|
|
* Periodic() functions from the base class are called on an interval by a
|
|
|
|
|
* Notifier instance.
|
|
|
|
|
*/
|
2021-04-18 20:35:29 -07:00
|
|
|
class TimedRobot : public IterativeRobotBase {
|
2017-07-08 10:50:56 -04:00
|
|
|
public:
|
2024-01-05 07:35:59 -08:00
|
|
|
/// Default loop period.
|
2021-05-28 22:06:59 -07:00
|
|
|
static constexpr auto kDefaultPeriod = 20_ms;
|
2017-07-08 10:50:56 -04:00
|
|
|
|
2018-05-31 20:47:15 -07:00
|
|
|
/**
|
|
|
|
|
* Provide an alternate "main loop" via StartCompetition().
|
|
|
|
|
*/
|
2017-07-08 10:50:56 -04:00
|
|
|
void StartCompetition() override;
|
|
|
|
|
|
2019-11-05 21:33:09 -08:00
|
|
|
/**
|
|
|
|
|
* Ends the main loop in StartCompetition().
|
|
|
|
|
*/
|
|
|
|
|
void EndCompetition() override;
|
|
|
|
|
|
2019-08-17 00:56:48 -04:00
|
|
|
/**
|
|
|
|
|
* Constructor for TimedRobot.
|
|
|
|
|
*
|
2025-10-29 03:18:55 +00:00
|
|
|
* @param period The period of the robot loop function.
|
2019-08-17 00:56:48 -04:00
|
|
|
*/
|
|
|
|
|
explicit TimedRobot(units::second_t period = kDefaultPeriod);
|
2017-07-08 10:50:56 -04:00
|
|
|
|
2025-10-29 03:18:55 +00:00
|
|
|
/**
|
|
|
|
|
* Constructor for TimedRobot.
|
|
|
|
|
*
|
|
|
|
|
* @param frequency The frequency of the robot loop function.
|
|
|
|
|
*/
|
|
|
|
|
explicit TimedRobot(units::hertz_t frequency);
|
|
|
|
|
|
2019-08-25 18:42:00 -07:00
|
|
|
TimedRobot(TimedRobot&&) = default;
|
|
|
|
|
TimedRobot& operator=(TimedRobot&&) = default;
|
2018-09-24 00:08:25 -07:00
|
|
|
|
2024-09-07 13:58:15 -04:00
|
|
|
~TimedRobot() override;
|
|
|
|
|
|
2024-11-16 10:43:38 -05:00
|
|
|
/**
|
|
|
|
|
* Return the system clock time in micrseconds for the start of the current
|
|
|
|
|
* periodic loop. This is in the same time base as Timer.GetFPGATimestamp(),
|
|
|
|
|
* but is stable through a loop. It is updated at the beginning of every
|
|
|
|
|
* periodic callback (including the normal periodic loop).
|
|
|
|
|
*
|
|
|
|
|
* @return Robot running time in microseconds, as of the start of the current
|
|
|
|
|
* periodic function.
|
|
|
|
|
*/
|
|
|
|
|
uint64_t GetLoopStartTime();
|
|
|
|
|
|
2020-10-16 17:56:37 -07:00
|
|
|
/**
|
|
|
|
|
* Add a callback to run at a specific period with a starting time offset.
|
|
|
|
|
*
|
|
|
|
|
* This is scheduled on TimedRobot's Notifier, so TimedRobot and the callback
|
|
|
|
|
* run synchronously. Interactions between them are thread-safe.
|
|
|
|
|
*
|
|
|
|
|
* @param callback The callback to run.
|
|
|
|
|
* @param period The period at which to run the callback.
|
|
|
|
|
* @param offset The offset from the common starting time. This is useful
|
|
|
|
|
* for scheduling a callback in a different timeslot relative
|
|
|
|
|
* to TimedRobot.
|
|
|
|
|
*/
|
|
|
|
|
void AddPeriodic(std::function<void()> callback, units::second_t period,
|
|
|
|
|
units::second_t offset = 0_s);
|
|
|
|
|
|
2017-07-08 10:50:56 -04:00
|
|
|
private:
|
2020-10-16 17:56:37 -07:00
|
|
|
class Callback {
|
|
|
|
|
public:
|
|
|
|
|
std::function<void()> func;
|
2024-07-16 22:56:36 -07:00
|
|
|
std::chrono::microseconds period;
|
|
|
|
|
std::chrono::microseconds expirationTime;
|
2020-10-16 17:56:37 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Construct a callback container.
|
|
|
|
|
*
|
|
|
|
|
* @param func The callback to run.
|
|
|
|
|
* @param startTime The common starting point for all callback scheduling.
|
|
|
|
|
* @param period The period at which to run the callback.
|
|
|
|
|
* @param offset The offset from the common starting time.
|
|
|
|
|
*/
|
2024-07-16 22:56:36 -07:00
|
|
|
Callback(std::function<void()> func, std::chrono::microseconds startTime,
|
|
|
|
|
std::chrono::microseconds period, std::chrono::microseconds offset)
|
2020-12-28 10:12:52 -08:00
|
|
|
: func{std::move(func)},
|
2020-10-16 17:56:37 -07:00
|
|
|
period{period},
|
2024-07-16 22:56:36 -07:00
|
|
|
expirationTime(
|
|
|
|
|
startTime + offset + period +
|
|
|
|
|
(std::chrono::microseconds{frc::RobotController::GetFPGATime()} -
|
|
|
|
|
startTime) /
|
|
|
|
|
period * period) {}
|
2020-10-16 17:56:37 -07:00
|
|
|
|
|
|
|
|
bool operator>(const Callback& rhs) const {
|
|
|
|
|
return expirationTime > rhs.expirationTime;
|
|
|
|
|
}
|
|
|
|
|
};
|
2018-04-30 00:00:09 -07:00
|
|
|
|
2024-09-07 13:58:15 -04:00
|
|
|
hal::Handle<HAL_NotifierHandle, HAL_CleanNotifier> m_notifier;
|
2024-07-16 22:56:36 -07:00
|
|
|
std::chrono::microseconds m_startTime;
|
2024-11-16 10:43:38 -05:00
|
|
|
uint64_t m_loopStartTimeUs = 0;
|
2018-04-30 00:00:09 -07:00
|
|
|
|
2020-10-16 17:56:37 -07:00
|
|
|
wpi::priority_queue<Callback, std::vector<Callback>, std::greater<Callback>>
|
|
|
|
|
m_callbacks;
|
2017-07-08 10:50:56 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace frc
|