Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Programming (http://www.chiefdelphi.com/forums/forumdisplay.php?f=51)
-   -   Timer resets when resumed (http://www.chiefdelphi.com/forums/showthread.php?t=134664)

importsjc 15-02-2015 22:01

Timer resets when resumed
 
Hi everyone!

I have recently run into an interesting issue, it appears that the timer (edu.wpi.first.wpilibj.Timer) resets, when you .stop() and then .start(). Is there a different timer I should be using or a better way to implement the one I have? I am looking for it to resume exactly where it left off, closer to a pause/resume than a start/stop.

Thanks for the help!

legts 15-02-2015 22:20

Re: Timer resets when resumed
 
I don't know if there is a pause sort of thing. You could try setting an integer equal to the value of the timer before you stop it, and setting the value of the timer equal to the variable after you restart it. Not sure if that would work, but it's a thought!

importsjc 16-02-2015 07:49

Re: Timer resets when resumed
 
Thanks for the reply! I had previously considered this, however unless I'm mistaken, there also is no Set() method.

RufflesRidge 16-02-2015 08:02

Re: Timer resets when resumed
 
Quote:

Originally Posted by importsjc (Post 1444745)
Hi everyone!

I have recently run into an interesting issue, it appears that the timer (edu.wpi.first.wpilibj.Timer) resets, when you .stop() and then .start().

Looking at the source for the Timer class, it definitely should continue increasing if you stop() then start() again.

Code:


85            public synchronized double get() {
86                if (m_running) {
87                    return ((double) ((getMsClock() - m_startTime) + m_accumulatedTime)) / 1000.0;
88                } else {
89                    return m_accumulatedTime;
90                }
91            }

107            public synchronized void start() {
108                m_startTime = getMsClock();
109                m_running = true;
110            }

118            public synchronized void stop() {
119                final double temp = get();
120                m_accumulatedTime = temp;
121                m_running = false;
122            }

Can you post your code showing your usage so we can see if anything jumps out as to why you're seeing it reset?

importsjc 16-02-2015 08:07

Re: Timer resets when resumed
 
Interesting! Must be an issue with how I use it...unfortunately I don't have the code in front of me right now, I'll post it later today. Thanks for the help!

importsjc 16-02-2015 21:21

Re: Timer resets when resumed
 
Ok, after doing testing I have narrowed down the timer reset to the line
Code:

timer.start();
here is some test code that is resetting when you start and then stop.

Code:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Custom;
import edu.wpi.first.wpilibj.Timer;

/**
 *
 * @author Robotics
 */
public class TimerTest {
        Timer autoTimer;
    public void robotInit(){
       
    }
     
    public void autonomousInit(){
            autoTimer = new Timer();
        autoTimer.start();
    }
    public void autonomousPeriodic() {
            System.out.println("AutoTimer First: " + autoTimer.get());
            Timer.delay(2);
            System.out.println("AutoTimer Second: " + autoTimer.get()+"\n\n");
            autoTimer.stop();
            Timer.delay(2);
            System.out.println("AutoTimer Third: " + autoTimer.get()+"\n\n");
            Timer.delay(2);
            autoTimer.start();
            System.out.println("AutoTimer Fourth: " + autoTimer.get()+"\n\n");
    }
   
    public void teleopInit(){
    }
   
    public void teleopPeriodic() {
    }
   
    public void testInit(){
    }
   
    public void testPeriodic() {
    }
   
   
    public void disabledInit(){
    }
   
}

you should notice that after the timer is re-started the autoTimer.get(); printout becomes 0.

I have actually solved my problem using an offshoot of what @legts suggested.

Thanks for the help!


All times are GMT -5. The time now is 01:39.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi