Files
Robot-Essentials/src/main/java/frc4388/utility/RobotTime.java
T

81 lines
2.2 KiB
Java
Raw Normal View History

2020-03-27 12:20:22 -06:00
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2018-2019 FIRST. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
/*----------------------------------------------------------------------------*/
package frc4388.utility;
/**
* <p>Keeps track of Robot times like time passed, delta time, etc
* <p>All times are in milliseconds
*/
2020-03-28 21:42:08 -06:00
public class RobotTime {
private long m_currTime = System.currentTimeMillis();
public long m_deltaTime = 0;
2020-03-27 12:20:22 -06:00
2020-03-28 21:42:08 -06:00
private long m_startRobotTime = m_currTime;
public long m_robotTime = 0;
public long m_lastRobotTime = 0;
2020-03-27 12:20:22 -06:00
2020-03-28 21:42:08 -06:00
private long m_startMatchTime = 0;
public long m_matchTime = 0;
public long m_lastMatchTime = 0;
2020-03-27 12:20:22 -06:00
2020-03-28 21:42:08 -06:00
public long m_frameNumber = 0;
2020-03-27 12:20:22 -06:00
/**
2020-03-28 21:42:08 -06:00
* Private constructor prevents other classes from instantiating
2020-03-27 12:20:22 -06:00
*/
private RobotTime(){}
2020-03-28 21:42:08 -06:00
private static RobotTime instance = null;
/**
* Gets the instance of Robot Time. If there is no instance running one will be created.
* @return instance of Robot Time
*/
public static RobotTime getInstance() {
if (instance == null) {
instance = new RobotTime();
}
return instance;
}
2020-03-27 12:20:22 -06:00
/**
* Call this once per periodic loop.
*/
2020-03-28 21:42:08 -06:00
public void updateTimes() {
m_lastRobotTime = m_robotTime;
m_lastMatchTime = m_matchTime;
2020-03-27 12:20:22 -06:00
m_currTime = System.currentTimeMillis();
m_robotTime = m_currTime - m_startRobotTime;
m_deltaTime = m_robotTime - m_lastRobotTime;
m_frameNumber++;
2020-03-27 12:20:22 -06:00
if (m_matchTime != 0) {
m_matchTime = m_currTime - m_startMatchTime;
2020-03-27 12:20:22 -06:00
}
}
/**
* Call this in both the auto and periodic inits
*/
2020-03-28 21:42:08 -06:00
public void startMatchTime() {
if (m_matchTime == 0) {
m_startMatchTime = m_currTime;
m_matchTime = 1;
2020-03-27 12:20:22 -06:00
}
}
/**
* Call this in disabled init
*/
2020-03-28 21:42:08 -06:00
public void endMatchTime() {
m_startMatchTime = 0;
m_matchTime = 0;
2020-03-27 12:20:22 -06:00
}
}