WPILibC++  2020.3.2
Timer.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) 2008-2020 FIRST. All Rights Reserved. */
3 /* Open Source Software - may be modified and shared by FRC teams. The code */
4 /* must be accompanied by the FIRST BSD license file in the root directory of */
5 /* the project. */
6 /*----------------------------------------------------------------------------*/
7 
8 #pragma once
9 
10 #include <units/units.h>
11 #include <wpi/deprecated.h>
12 #include <wpi/mutex.h>
13 
14 #include "frc/Base.h"
15 
16 namespace frc2 {
17 
28 void Wait(units::second_t seconds);
29 
35 units::second_t GetTime();
36 
40 class Timer {
41  public:
48  Timer();
49 
50  virtual ~Timer() = default;
51 
52  Timer(const Timer& rhs);
53  Timer& operator=(const Timer& rhs);
54  Timer(Timer&& rhs);
55  Timer& operator=(Timer&& rhs);
56 
64  units::second_t Get() const;
65 
72  void Reset();
73 
80  void Start();
81 
89  void Stop();
90 
97  bool HasElapsed(units::second_t period);
98 
107  bool HasPeriodPassed(units::second_t period);
108 
117  bool AdvanceIfElapsed(units::second_t period);
118 
127  static units::second_t GetFPGATimestamp();
128 
144  static units::second_t GetMatchTime();
145 
146  // The time, in seconds, at which the 32-bit FPGA timestamp rolls over to 0
147  static const units::second_t kRolloverTime;
148 
149  private:
150  units::second_t m_startTime = 0_s;
151  units::second_t m_accumulatedTime = 0_s;
152  bool m_running = false;
153  mutable wpi::mutex m_mutex;
154 };
155 
156 } // namespace frc2
frc2::Timer::GetFPGATimestamp
static units::second_t GetFPGATimestamp()
Return the FPGA system clock time in seconds.
frc2::Timer::Get
units::second_t Get() const
Get the current time from the timer.
frc2::Timer::Start
void Start()
Start the timer running.
frc2::Timer::Stop
void Stop()
Stop the timer.
frc2::Timer
A wrapper for the frc::Timer class that returns unit-typed values.
Definition: Timer.h:40
frc2::Timer::HasPeriodPassed
bool HasPeriodPassed(units::second_t period)
Check if the period specified has passed and if it has, advance the start time by that period.
frc2::Timer::GetMatchTime
static units::second_t GetMatchTime()
Return the approximate match time.
frc2::Timer::AdvanceIfElapsed
bool AdvanceIfElapsed(units::second_t period)
Check if the period specified has passed and if it has, advance the start time by that period.
frc2::Timer::Reset
void Reset()
Reset the timer by setting the time to 0.
frc::GetTime
double GetTime()
Gives real-time clock system time with nanosecond resolution.
frc2::Timer::HasElapsed
bool HasElapsed(units::second_t period)
Check if the period specified has passed.
frc::Wait
void Wait(double seconds)
Pause the task for a specified time.
frc2::Timer::Timer
Timer()
Create a new timer object.