Time measuring
H2O2
Posts: 9
Hi!
I have a need to measure the time between two events in an application, ie I am building an GPS based navigator and want to be able to calculate the traversed distance.
I am doing this by a simple numerical integration [noparse][[/noparse]Distance := Distance + speed*DeltaTime]. Therefore I need the time elapsed [noparse][[/noparse]DeltaTime] between the reading the GPSreceiver.
My first approach was to read the System Clock Counter [noparse][[/noparse]cnt] at the beginning and end of reading the NMEA string containing the speed. The difference would then be my DeltaTime. However, after some experimenting I've found that this approach only works when the time between beginning and end is approx 0.25s or less. I am not sure the time between two consecutive readings is less than this. Larger intervals result in negative values.
An alternative solution would be much appreciated!
//G
I have a need to measure the time between two events in an application, ie I am building an GPS based navigator and want to be able to calculate the traversed distance.
I am doing this by a simple numerical integration [noparse][[/noparse]Distance := Distance + speed*DeltaTime]. Therefore I need the time elapsed [noparse][[/noparse]DeltaTime] between the reading the GPSreceiver.
My first approach was to read the System Clock Counter [noparse][[/noparse]cnt] at the beginning and end of reading the NMEA string containing the speed. The difference would then be my DeltaTime. However, after some experimenting I've found that this approach only works when the time between beginning and end is approx 0.25s or less. I am not sure the time between two consecutive readings is less than this. Larger intervals result in negative values.
An alternative solution would be much appreciated!
//G
Comments
If you need time intervals greater than this, I suggest you come up with a real time clock routine that runs in its own cog. It could keep its own one second timer, then use the system clock for the time from the last recorded second. It would also use the system clock (with the WAITCNT statement) to keep the one second interval.
I've seem in NMEA documentation that time is given i milli-secs, but maybe it is some limitation in the Parallax receiver.
//G
attached please find my test program.
CON
_clkmode = xtal1 + pll16x
_xinfreq = 5_000_000
OBJ
Debug : "FullDuplexSerialPlus"
PUB Main | ok
ok := Debug.start(31, 30, 0, 57600)
TestTiming
PUB TestTiming | BaseTime, TimeNow, TimePast, Loop, Delay, ElapsedTime
ElapsedTime := 0
TimeNow := cnt
BaseTime := TimeNow
Delay := clkfreq / 5 ' When this 5 is changed to say 2 it does not work...
Loop := 0
repeat while Loop < 10000
TimePast := TimeNow
waitcnt(BaseTime += Delay)
TimeNow := cnt
ElapsedTime := ElapsedTime + 100*(TimeNow - TimePast)/clkfreq 'In 100th of a sec
Debug.str(String("Elapsed Time: "))
Debug.dec(ElapsedTime)
Debug.str(String(10, 13))
Loop++
//G
Why not calc it against a known value.
Update speed as oftan as you can, or want to.
But only perform this calculation once per second.
The other thing is Im taking a guess that this thing is not insanely fast. Why not just calculate your distance using the GPS data points, it will take float math 32 full, but since it only updates once per second, the added calculation time should not be an issue, even this in full float will be way shorter than 1 second.
Here is the formula for cacluating distance using decimal lat long.
R = earth’s radius (mean radius = 6,371km) ( you have altitude so you can compensate this to increase the accuracy. )
Δlat = lat2− lat1
Δlong = long2− long1
a = sin²(Δlat/2) + cos(lat1)*cos(lat2)*sin²(Δlong/2)
c = 2*atan2(√a, √(1−a))
d = R*c
its not the easiest calculation, but I think it will end up being alot more accurate than dealing with speed. As your speed can fluctuate greatly over your sample times.
TJ
Post Edited (TJHJ) : 12/7/2008 8:52:58 PM GMT