mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-28 02:11:43 +00:00
Now, implicit narrowing conversions are only used with wpi::Now(). This
also fixes clang-tidy warnings about C-style casts. For example:
```
== clang-tidy /__w/allwpilib/allwpilib/wpilibNewCommands/src/main/native/include/frc2/command/SwerveControllerCommand.inc ==
/__w/allwpilib/allwpilib/wpilibNewCommands/src/main/native/include/frc2/command/SwerveControllerCommand.inc:95:18: warning: C-style casts are discouraged; use static_cast/const_cast/reinterpret_cast [google-readability-casting]
auto curTime = units::second_t(m_timer.Get());
^
```
In that case at least, the cast was removed entirely since Get() already
returns a units::second_t.
87 lines
1.8 KiB
C++
87 lines
1.8 KiB
C++
// Copyright (c) FIRST and other WPILib contributors.
|
|
// Open Source Software; you can modify and/or share it under the terms of
|
|
// the WPILib BSD license file in the root directory of this project.
|
|
|
|
#include "frc/Timer.h"
|
|
|
|
#include <chrono>
|
|
#include <thread>
|
|
|
|
#include "frc/DriverStation.h"
|
|
#include "frc/RobotController.h"
|
|
|
|
namespace frc {
|
|
|
|
void Wait(units::second_t seconds) {
|
|
std::this_thread::sleep_for(std::chrono::duration<double>(seconds.value()));
|
|
}
|
|
|
|
units::second_t GetTime() {
|
|
using std::chrono::duration;
|
|
using std::chrono::duration_cast;
|
|
using std::chrono::system_clock;
|
|
|
|
return units::second_t{
|
|
duration_cast<duration<double>>(system_clock::now().time_since_epoch())
|
|
.count()};
|
|
}
|
|
|
|
} // namespace frc
|
|
|
|
using namespace frc;
|
|
|
|
Timer::Timer() {
|
|
Reset();
|
|
}
|
|
|
|
units::second_t Timer::Get() const {
|
|
if (m_running) {
|
|
return (GetFPGATimestamp() - m_startTime) + m_accumulatedTime;
|
|
} else {
|
|
return m_accumulatedTime;
|
|
}
|
|
}
|
|
|
|
void Timer::Reset() {
|
|
m_accumulatedTime = 0_s;
|
|
m_startTime = GetFPGATimestamp();
|
|
}
|
|
|
|
void Timer::Start() {
|
|
if (!m_running) {
|
|
m_startTime = GetFPGATimestamp();
|
|
m_running = true;
|
|
}
|
|
}
|
|
|
|
void Timer::Stop() {
|
|
if (m_running) {
|
|
m_accumulatedTime = Get();
|
|
m_running = false;
|
|
}
|
|
}
|
|
|
|
bool Timer::HasElapsed(units::second_t period) const {
|
|
return Get() >= period;
|
|
}
|
|
|
|
bool Timer::AdvanceIfElapsed(units::second_t period) {
|
|
if (Get() >= period) {
|
|
// 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;
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
units::second_t Timer::GetFPGATimestamp() {
|
|
// FPGA returns the timestamp in microseconds
|
|
return units::second_t{frc::RobotController::GetFPGATime() * 1.0e-6};
|
|
}
|
|
|
|
units::second_t Timer::GetMatchTime() {
|
|
return units::second_t{frc::DriverStation::GetMatchTime()};
|
|
}
|