2017-07-08 10:50:56 -04:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2018-01-02 09:20:21 -08:00
|
|
|
/* Copyright (c) 2017-2018 FIRST. All Rights Reserved. */
|
2017-07-08 10:50:56 -04:00
|
|
|
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
|
|
|
|
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
|
|
|
|
/* the project. */
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
2018-04-30 00:00:09 -07:00
|
|
|
#include <HAL/Notifier.h>
|
2017-07-08 10:50:56 -04:00
|
|
|
|
2018-04-30 00:00:09 -07:00
|
|
|
#include "ErrorBase.h"
|
2017-07-08 10:50:56 -04:00
|
|
|
#include "IterativeRobotBase.h"
|
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
*/
|
2018-04-30 00:00:09 -07:00
|
|
|
class TimedRobot : public IterativeRobotBase, public ErrorBase {
|
2017-07-08 10:50:56 -04:00
|
|
|
public:
|
|
|
|
|
static constexpr double kDefaultPeriod = 0.02;
|
|
|
|
|
|
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;
|
|
|
|
|
|
2018-05-31 20:47:15 -07:00
|
|
|
/**
|
|
|
|
|
* Set time period between calls to Periodic() functions.
|
|
|
|
|
*
|
|
|
|
|
* A timer event is queued for periodic event notification. Each time the
|
|
|
|
|
* interrupt occurs, the event will be immediately requeued for the same time
|
|
|
|
|
* interval.
|
|
|
|
|
*
|
|
|
|
|
* @param period Period in seconds.
|
|
|
|
|
*/
|
2017-07-08 10:50:56 -04:00
|
|
|
void SetPeriod(double seconds);
|
2018-05-31 20:47:15 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get time period between calls to Periodic() functions.
|
|
|
|
|
*/
|
2018-01-27 01:01:15 -08:00
|
|
|
double GetPeriod() const;
|
2017-07-08 10:50:56 -04:00
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
TimedRobot();
|
2018-04-30 00:00:09 -07:00
|
|
|
~TimedRobot() override;
|
2017-07-08 10:50:56 -04:00
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
// Prevents loop from starting if user calls SetPeriod() in RobotInit()
|
|
|
|
|
bool m_startLoop = false;
|
|
|
|
|
|
2018-04-30 00:00:09 -07:00
|
|
|
HAL_NotifierHandle m_notifier{0};
|
|
|
|
|
|
|
|
|
|
// The absolute expiration time
|
|
|
|
|
double m_expirationTime = 0;
|
|
|
|
|
|
|
|
|
|
// The relative time
|
|
|
|
|
double m_period = kDefaultPeriod;
|
|
|
|
|
|
2018-05-31 20:47:15 -07:00
|
|
|
/**
|
|
|
|
|
* Update the HAL alarm time.
|
|
|
|
|
*/
|
2018-04-30 00:00:09 -07:00
|
|
|
void UpdateAlarm();
|
2017-07-08 10:50:56 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace frc
|