How calculate delta time?

i tried to run

1.start one timer in initialize and the second in execute and subtract the last one from the first but the values ​​are very small
2. run both timers in initialize and find delta in execute

my code commands

public Timer st_Timer = new Timer();
public Timer cur_Timer = new Timer();
public FollowTrajCommand() {

// each subsystem used by the command must be passed into the
// addRequirements() method (which takes a vararg of Subsystem)
addRequirements(subsystem);

}

@Override
public void initialize() {
cur_Timer.start(); // cur timer
st_Timer.start(); // start timer
}

@Override
public void execute() {

double dT = cur_Timer.get() - st_Timer.get();

SmartDashboard.putNumber(“start time”, st_Timer.get());
SmartDashboard.putNumber(“cyr time”, cur_Timer.get());
SmartDashboard.putNumber(“deltaTime”, dT);

}

To get the delta time you could do something like:

private double lastTime;

...

@Override
public void initalize() {
    lastTime = Timer.getFPGATimestamp();
}

@Override
public void execute() {
    double currentTime = Timer.getFPGATimestamp();

    // In seconds
    double deltaTime = currentTime - lastTime;
    lastTime = currentTime;
}

...
2 Likes

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.