Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Java (http://www.chiefdelphi.com/forums/forumdisplay.php?f=184)
-   -   RPM calculator (http://www.chiefdelphi.com/forums/showthread.php?t=103825)

Brian Selle 02-03-2012 12:43

Re: RPM calculator
 
To calculate the delta time in Java do you use System.currentTimeMillis() before or after getting latest count or is there a better timestamp that is associated with the time the encoder count value was actually taken?

Ether 02-03-2012 13:06

Re: RPM calculator
 
Quote:

Originally Posted by btslaser (Post 1137626)
To calculate the delta time in Java do you use System.currentTimeMillis() before or after getting latest count or is there a better timestamp that is associated with the time the encoder count value was actually taken?

If you are using a 360 count per rev encoder on a wheel spinning at, say, 4000 rpm, and you are sampling at roughly 20ms, then the delta count per sample would be 480. One count out of 480 is 0.2% so you probably don't need to worry about that.

I suppose you could put the code to grab the encoder count and the system timer in a critical section to prevent getting swapped out between the two operations.



Brian Selle 02-03-2012 13:35

Re: RPM calculator
 
I meant to ask if System.currentTimeMillis() is the best time source -or- is there some other timeStamp to use?

Ether 02-03-2012 14:10

Re: RPM calculator
 
Quote:

Originally Posted by btslaser (Post 1137642)
I meant to ask if System.currentTimeMillis() is the best time source -or- is there some other timeStamp to use?

Don't know about Java, but in C++ there appears to be a nanosecond timer in WPILib:

Code:

/**
 * @brief Gives real-time clock system time with nanosecond resolution
 * @return The time, just in case you want the robot to start autonomous at 8pm on Saturday.
*/
double GetTime() 
{
        struct timespec tp;
       
        clock_gettime(CLOCK_REALTIME,&tp);
        double realTime = (double)tp.tv_sec + (double)((double)tp.tv_nsec*1e-9);
       
        return (realTime);
}


Brian Selle 02-03-2012 14:43

Re: RPM calculator
 
Excellent! In the WPILib I just found:

Timer.java

/**
* Return the system clock time in seconds. Return the time from the
* FPGA hardware clock in seconds since the FPGA started.
*
* @return Robot running time in seconds.
*/
public static double getFPGATimestamp() {
return Utility.getFPGATime() / 1000000.0;
}

Utility.java

/**
* Read the microsecond timer from the FPGA.
*
* @return The current time in microseconds according to the FPGA.
*/
public static long getFPGATime() {
return tGlobal.readLocalTime();
}

Thanks!

nickpeq 05-03-2012 01:19

Re: RPM calculator
 
I generally use System.nanoTime() for anything in Java. It's overkill, but I've never found a reason NOT to use it.
You could measure the dT that it takes to get the latest count and see how negligible it is. (or isn't)


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

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