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
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
2018-07-20 00:03:45 -07:00
|
|
|
#include "frc/Timer.h"
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2016-05-26 20:19:23 -07:00
|
|
|
#include <chrono>
|
|
|
|
|
#include <thread>
|
2016-09-25 16:50:13 -07:00
|
|
|
|
2018-07-20 00:03:45 -07:00
|
|
|
#include <hal/HAL.h>
|
2017-08-27 00:11:52 -07:00
|
|
|
|
2018-07-20 00:03:45 -07:00
|
|
|
#include "frc/DriverStation.h"
|
|
|
|
|
#include "frc/RobotController.h"
|
2014-08-04 15:25:41 -04:00
|
|
|
|
2016-11-01 22:33:12 -07:00
|
|
|
namespace frc {
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
void Wait(double seconds) {
|
2016-05-26 20:19:23 -07:00
|
|
|
std::this_thread::sleep_for(std::chrono::duration<double>(seconds));
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
double GetTime() {
|
2017-08-07 17:36:34 -07:00
|
|
|
using std::chrono::duration;
|
|
|
|
|
using std::chrono::duration_cast;
|
|
|
|
|
using std::chrono::system_clock;
|
|
|
|
|
|
2016-05-26 20:19:23 -07:00
|
|
|
return duration_cast<duration<double>>(system_clock::now().time_since_epoch())
|
|
|
|
|
.count();
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2016-11-01 22:33:12 -07:00
|
|
|
} // namespace frc
|
|
|
|
|
|
|
|
|
|
using namespace frc;
|
|
|
|
|
|
2016-05-20 17:30:37 -07:00
|
|
|
// for compatibility with msvc12--see C2864
|
2015-04-26 19:19:57 -04:00
|
|
|
const double Timer::kRolloverTime = (1ll << 32) / 1e6;
|
2018-05-31 20:47:15 -07:00
|
|
|
|
2017-11-16 00:33:51 -08:00
|
|
|
Timer::Timer() { Reset(); }
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2019-07-07 19:15:59 -07:00
|
|
|
Timer::Timer(Timer&& rhs)
|
|
|
|
|
: m_startTime(std::move(rhs.m_startTime)),
|
|
|
|
|
m_accumulatedTime(std::move(rhs.m_accumulatedTime)),
|
|
|
|
|
m_running(std::move(rhs.m_running)) {}
|
|
|
|
|
|
|
|
|
|
Timer& Timer::operator=(Timer&& rhs) {
|
|
|
|
|
std::scoped_lock lock(m_mutex, rhs.m_mutex);
|
|
|
|
|
|
|
|
|
|
m_startTime = std::move(rhs.m_startTime);
|
|
|
|
|
m_accumulatedTime = std::move(rhs.m_accumulatedTime);
|
|
|
|
|
m_running = std::move(rhs.m_running);
|
|
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
double Timer::Get() const {
|
|
|
|
|
double result;
|
|
|
|
|
double currentTime = GetFPGATimestamp();
|
|
|
|
|
|
2019-07-07 19:17:14 -07:00
|
|
|
std::lock_guard lock(m_mutex);
|
2015-06-25 15:07:55 -04:00
|
|
|
if (m_running) {
|
2017-11-16 00:33:51 -08:00
|
|
|
// If the current time is before the start time, then the FPGA clock rolled
|
|
|
|
|
// over. Compensate by adding the ~71 minutes that it takes to roll over to
|
|
|
|
|
// the current time.
|
2015-06-25 15:07:55 -04:00
|
|
|
if (currentTime < m_startTime) {
|
|
|
|
|
currentTime += kRolloverTime;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result = (currentTime - m_startTime) + m_accumulatedTime;
|
|
|
|
|
} else {
|
|
|
|
|
result = m_accumulatedTime;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
void Timer::Reset() {
|
2019-07-07 19:17:14 -07:00
|
|
|
std::lock_guard lock(m_mutex);
|
2015-06-25 15:07:55 -04:00
|
|
|
m_accumulatedTime = 0;
|
|
|
|
|
m_startTime = GetFPGATimestamp();
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
void Timer::Start() {
|
2019-07-07 19:17:14 -07:00
|
|
|
std::lock_guard lock(m_mutex);
|
2015-06-25 15:07:55 -04:00
|
|
|
if (!m_running) {
|
|
|
|
|
m_startTime = GetFPGATimestamp();
|
|
|
|
|
m_running = true;
|
|
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
void Timer::Stop() {
|
|
|
|
|
double temp = Get();
|
|
|
|
|
|
2019-07-07 19:17:14 -07:00
|
|
|
std::lock_guard lock(m_mutex);
|
2015-06-25 15:07:55 -04:00
|
|
|
if (m_running) {
|
|
|
|
|
m_accumulatedTime = temp;
|
|
|
|
|
m_running = false;
|
|
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
bool Timer::HasPeriodPassed(double period) {
|
|
|
|
|
if (Get() > period) {
|
2019-07-07 19:17:14 -07:00
|
|
|
std::lock_guard lock(m_mutex);
|
2015-06-25 15:07:55 -04:00
|
|
|
// Advance the start time by the period.
|
|
|
|
|
m_startTime += period;
|
|
|
|
|
// Don't set it to the current time... we want to avoid drift.
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
double Timer::GetFPGATimestamp() {
|
|
|
|
|
// FPGA returns the timestamp in microseconds
|
2017-12-10 21:52:49 -08:00
|
|
|
return RobotController::GetFPGATime() * 1.0e-6;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
2016-10-25 19:01:18 -07:00
|
|
|
|
|
|
|
|
double Timer::GetMatchTime() {
|
|
|
|
|
return DriverStation::GetInstance().GetMatchTime();
|
|
|
|
|
}
|