Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 1 | // Copyright (c) FIRST and other WPILib contributors. |
| 2 | // Open Source Software; you can modify and/or share it under the terms of |
| 3 | // the WPILib BSD license file in the root directory of this project. |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 4 | |
| 5 | #include "frc/Timer.h" |
| 6 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 7 | #include <chrono> |
| 8 | #include <thread> |
| 9 | |
| 10 | #include "frc/DriverStation.h" |
| 11 | #include "frc/RobotController.h" |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 12 | |
| 13 | namespace frc { |
| 14 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 15 | void Wait(units::second_t seconds) { |
| 16 | std::this_thread::sleep_for(std::chrono::duration<double>(seconds.value())); |
| 17 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 18 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 19 | units::second_t GetTime() { |
| 20 | using std::chrono::duration; |
| 21 | using std::chrono::duration_cast; |
| 22 | using std::chrono::system_clock; |
| 23 | |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 24 | return units::second_t{ |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 25 | duration_cast<duration<double>>(system_clock::now().time_since_epoch()) |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 26 | .count()}; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 27 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 28 | |
| 29 | } // namespace frc |
| 30 | |
| 31 | using namespace frc; |
| 32 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 33 | Timer::Timer() { |
| 34 | Reset(); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 35 | } |
| 36 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 37 | units::second_t Timer::Get() const { |
| 38 | if (m_running) { |
| 39 | return (GetFPGATimestamp() - m_startTime) + m_accumulatedTime; |
| 40 | } else { |
| 41 | return m_accumulatedTime; |
| 42 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 43 | } |
| 44 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 45 | void Timer::Reset() { |
| 46 | m_accumulatedTime = 0_s; |
| 47 | m_startTime = GetFPGATimestamp(); |
| 48 | } |
| 49 | |
| 50 | void Timer::Start() { |
| 51 | if (!m_running) { |
| 52 | m_startTime = GetFPGATimestamp(); |
| 53 | m_running = true; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | void Timer::Stop() { |
| 58 | if (m_running) { |
| 59 | m_accumulatedTime = Get(); |
| 60 | m_running = false; |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | bool Timer::HasElapsed(units::second_t period) const { |
| 65 | return Get() >= period; |
| 66 | } |
| 67 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 68 | bool Timer::AdvanceIfElapsed(units::second_t period) { |
| 69 | if (Get() >= period) { |
| 70 | // Advance the start time by the period. |
| 71 | m_startTime += period; |
| 72 | // Don't set it to the current time... we want to avoid drift. |
| 73 | return true; |
| 74 | } else { |
| 75 | return false; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | units::second_t Timer::GetFPGATimestamp() { |
| 80 | // FPGA returns the timestamp in microseconds |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 81 | return units::second_t{frc::RobotController::GetFPGATime() * 1.0e-6}; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | units::second_t Timer::GetMatchTime() { |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 85 | return units::second_t{frc::DriverStation::GetMatchTime()}; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 86 | } |