Add frc2::Timer (#1968)

This is a unit-safe version of frc::Timer.
Undo previous (#1815) deprecation of parts of frc::Timer.
This commit is contained in:
Oblarg
2019-10-26 11:21:40 -04:00
committed by Peter Johnson
parent 36ea865edc
commit 73a30182c3
5 changed files with 293 additions and 122 deletions

View File

@@ -17,123 +17,34 @@
namespace frc {
void Wait(double seconds) {
std::this_thread::sleep_for(std::chrono::duration<double>(seconds));
}
void Wait(double seconds) { frc2::Wait(units::second_t(seconds)); }
double GetTime() {
using std::chrono::duration;
using std::chrono::duration_cast;
using std::chrono::system_clock;
return duration_cast<duration<double>>(system_clock::now().time_since_epoch())
.count();
}
double GetTime() { return frc2::GetTime().to<double>(); }
} // namespace frc
using namespace frc;
// for compatibility with msvc12--see C2864
const double Timer::kRolloverTime = (1ll << 32) / 1e6;
const double Timer::kRolloverTime = frc2::Timer::kRolloverTime.to<double>();
Timer::Timer() { Reset(); }
Timer::Timer(const Timer& rhs)
: m_startTime(rhs.m_startTime),
m_accumulatedTime(rhs.m_accumulatedTime),
m_running(rhs.m_running) {}
double Timer::Get() const { return m_timer.Get().to<double>(); }
Timer& Timer::operator=(const Timer& rhs) {
std::scoped_lock lock(m_mutex, rhs.m_mutex);
void Timer::Reset() { m_timer.Reset(); }
m_startTime = rhs.m_startTime;
m_accumulatedTime = rhs.m_accumulatedTime;
m_running = rhs.m_running;
void Timer::Start() { m_timer.Start(); }
return *this;
}
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;
}
double Timer::Get() const {
double result;
double currentTime = GetFPGATimestamp();
std::scoped_lock lock(m_mutex);
if (m_running) {
// 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.
if (currentTime < m_startTime) {
currentTime += kRolloverTime;
}
result = (currentTime - m_startTime) + m_accumulatedTime;
} else {
result = m_accumulatedTime;
}
return result;
}
void Timer::Reset() {
std::scoped_lock lock(m_mutex);
m_accumulatedTime = 0;
m_startTime = GetFPGATimestamp();
}
void Timer::Start() {
std::scoped_lock lock(m_mutex);
if (!m_running) {
m_startTime = GetFPGATimestamp();
m_running = true;
}
}
void Timer::Stop() {
double temp = Get();
std::scoped_lock lock(m_mutex);
if (m_running) {
m_accumulatedTime = temp;
m_running = false;
}
}
void Timer::Stop() { m_timer.Stop(); }
bool Timer::HasPeriodPassed(double period) {
return HasPeriodPassed(units::second_t(period));
}
bool Timer::HasPeriodPassed(units::second_t period) {
if (Get() > period.to<double>()) {
std::scoped_lock lock(m_mutex);
// Advance the start time by the period.
m_startTime += period.to<double>();
// Don't set it to the current time... we want to avoid drift.
return true;
}
return false;
return m_timer.HasPeriodPassed(units::second_t(period));
}
double Timer::GetFPGATimestamp() {
// FPGA returns the timestamp in microseconds
return RobotController::GetFPGATime() * 1.0e-6;
return frc2::Timer::GetFPGATimestamp().to<double>();
}
double Timer::GetMatchTime() {
return DriverStation::GetInstance().GetMatchTime();
return frc2::Timer::GetMatchTime().to<double>();
}