2013-12-15 18:30:16 -05:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2019-06-10 22:03:15 -07:00
|
|
|
/* Copyright (c) 2008-2019 FIRST. All Rights Reserved. */
|
2013-12-15 18:30:16 -05:00
|
|
|
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
2016-01-02 03:02:34 -08:00
|
|
|
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
|
|
|
|
/* the project. */
|
2013-12-15 18:30:16 -05:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2016-01-02 03:02:34 -08:00
|
|
|
|
2014-05-02 17:54:01 -04:00
|
|
|
#pragma once
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2019-08-17 00:56:48 -04:00
|
|
|
#include <units/units.h>
|
2018-04-29 23:33:19 -07:00
|
|
|
#include <wpi/deprecated.h>
|
|
|
|
|
#include <wpi/mutex.h>
|
2017-08-27 00:11:52 -07:00
|
|
|
|
2018-07-20 00:03:45 -07:00
|
|
|
#include "frc/Base.h"
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2016-11-01 22:33:12 -07:00
|
|
|
namespace frc {
|
|
|
|
|
|
2018-09-26 00:09:25 -07:00
|
|
|
using TimerInterruptHandler = void (*)(void* param);
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2018-05-31 20:47:15 -07:00
|
|
|
/**
|
|
|
|
|
* Pause the task for a specified time.
|
|
|
|
|
*
|
|
|
|
|
* Pause the execution of the program for a specified period of time given in
|
|
|
|
|
* seconds. Motors will continue to run at their last assigned values, and
|
|
|
|
|
* sensors will continue to update. Only the task containing the wait will pause
|
|
|
|
|
* until the wait time is expired.
|
|
|
|
|
*
|
|
|
|
|
* @param seconds Length of time to pause, in seconds.
|
|
|
|
|
*/
|
2013-12-15 18:30:16 -05:00
|
|
|
void Wait(double seconds);
|
2018-05-31 20:47:15 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Gives real-time clock system time with nanosecond resolution
|
|
|
|
|
* @return The time, just in case you want the robot to start autonomous at 8pm
|
|
|
|
|
* on Saturday.
|
|
|
|
|
*/
|
2013-12-15 18:30:16 -05:00
|
|
|
double GetTime();
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Timer objects measure accumulated time in seconds.
|
2017-11-16 00:33:51 -08:00
|
|
|
*
|
2015-06-25 15:07:55 -04:00
|
|
|
* The timer object functions like a stopwatch. It can be started, stopped, and
|
2016-05-20 17:30:37 -07:00
|
|
|
* cleared. When the timer is running its value counts up in seconds. When
|
|
|
|
|
* stopped, the timer holds the current value. The implementation simply records
|
|
|
|
|
* the time when started and subtracts the current time whenever the value is
|
|
|
|
|
* requested.
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
class Timer {
|
|
|
|
|
public:
|
2018-05-31 20:47:15 -07:00
|
|
|
/**
|
|
|
|
|
* Create a new timer object.
|
|
|
|
|
*
|
|
|
|
|
* Create a new timer object and reset the time to zero. The timer is
|
|
|
|
|
* initially not running and must be started.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
Timer();
|
2018-05-31 20:47:15 -07:00
|
|
|
|
2015-06-25 01:54:20 -07:00
|
|
|
virtual ~Timer() = default;
|
2015-07-21 01:23:34 -07:00
|
|
|
|
2019-07-07 19:15:59 -07:00
|
|
|
Timer(Timer&& rhs);
|
|
|
|
|
Timer& operator=(Timer&& rhs);
|
2015-07-21 01:23:34 -07:00
|
|
|
|
2018-05-31 20:47:15 -07:00
|
|
|
/**
|
|
|
|
|
* Get the current time from the timer. If the clock is running it is derived
|
|
|
|
|
* from the current system clock the start time stored in the timer class. If
|
|
|
|
|
* the clock is not running, then return the time when it was last stopped.
|
|
|
|
|
*
|
|
|
|
|
* @return Current time value for this timer in seconds
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
double Get() const;
|
2018-05-31 20:47:15 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Reset the timer by setting the time to 0.
|
|
|
|
|
*
|
|
|
|
|
* Make the timer startTime the current time so new requests will be relative
|
|
|
|
|
* to now.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void Reset();
|
2018-05-31 20:47:15 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Start the timer running.
|
|
|
|
|
*
|
|
|
|
|
* Just set the running flag to true indicating that all time requests should
|
|
|
|
|
* be relative to the system clock.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void Start();
|
2018-05-31 20:47:15 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Stop the timer.
|
|
|
|
|
*
|
|
|
|
|
* This computes the time as of now and clears the running flag, causing all
|
|
|
|
|
* subsequent time requests to be read from the accumulated time rather than
|
|
|
|
|
* looking at the system clock.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void Stop();
|
2018-05-31 20:47:15 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Check if the period specified has passed and if it has, advance the start
|
|
|
|
|
* time by that period. This is useful to decide if it's time to do periodic
|
|
|
|
|
* work without drifting later by the time it took to get around to checking.
|
|
|
|
|
*
|
|
|
|
|
* @param period The period to check for (in seconds).
|
|
|
|
|
* @return True if the period has passed.
|
|
|
|
|
*/
|
2019-08-17 00:56:48 -04:00
|
|
|
WPI_DEPRECATED("Use unit-safe HasPeriodPassed method instead.")
|
2015-06-25 15:07:55 -04:00
|
|
|
bool HasPeriodPassed(double period);
|
|
|
|
|
|
2019-08-17 00:56:48 -04:00
|
|
|
/**
|
|
|
|
|
* Check if the period specified has passed and if it has, advance the start
|
|
|
|
|
* time by that period. This is useful to decide if it's time to do periodic
|
|
|
|
|
* work without drifting later by the time it took to get around to checking.
|
|
|
|
|
*
|
|
|
|
|
* @param period The period to check for.
|
|
|
|
|
* @return True if the period has passed.
|
|
|
|
|
*/
|
|
|
|
|
bool HasPeriodPassed(units::second_t period);
|
|
|
|
|
|
2018-05-31 20:47:15 -07:00
|
|
|
/**
|
|
|
|
|
* Return the FPGA system clock time in seconds.
|
|
|
|
|
*
|
|
|
|
|
* Return the time from the FPGA hardware clock in seconds since the FPGA
|
|
|
|
|
* started. Rolls over after 71 minutes.
|
|
|
|
|
*
|
|
|
|
|
* @returns Robot running time in seconds.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
static double GetFPGATimestamp();
|
2018-05-31 20:47:15 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return the approximate match time.
|
|
|
|
|
*
|
|
|
|
|
* The FMS does not send an official match time to the robots, but does send
|
|
|
|
|
* an approximate match time. The value will count down the time remaining in
|
|
|
|
|
* the current period (auto or teleop).
|
|
|
|
|
*
|
|
|
|
|
* Warning: This is not an official time (so it cannot be used to dispute ref
|
|
|
|
|
* calls or guarantee that a function will trigger before the match ends).
|
|
|
|
|
*
|
|
|
|
|
* The Practice Match function of the DS approximates the behavior seen on the
|
|
|
|
|
* field.
|
|
|
|
|
*
|
|
|
|
|
* @return Time remaining in current match period (auto or teleop)
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
static double GetMatchTime();
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
// The time, in seconds, at which the 32-bit FPGA timestamp rolls over to 0
|
2015-04-26 19:19:57 -04:00
|
|
|
static const double kRolloverTime;
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
private:
|
2015-06-24 01:06:29 -07:00
|
|
|
double m_startTime = 0.0;
|
|
|
|
|
double m_accumulatedTime = 0.0;
|
|
|
|
|
bool m_running = false;
|
2017-11-13 09:51:48 -08:00
|
|
|
mutable wpi::mutex m_mutex;
|
2013-12-15 18:30:16 -05:00
|
|
|
};
|
2016-11-01 22:33:12 -07:00
|
|
|
|
|
|
|
|
} // namespace frc
|