# Calculating distance

I am tracking a bike race via GPS. I get Longitude, Latitude, speed and time stamp in the message. One of the things I have to report is distance traveled since the beginning of the race.

Some of the courses loop, so I am thinking trying to do calculations from Longitude and Latitude might be rather complex.

Instead, I thought I would take the average speed and multiply it by the time elapsed.

Is this reasonable? Perhaps it would be better to take the speed given and multiply it from the time elapsed from the last record and add to the total?

I don’t think anyone will really know if I’m a little off, but I want to be as reasonably accurate as possible. I was thinking the average speed might be self-correcting, but I will be honest and say that this is more of a hunch and not based on hard math (which is why I’m turning to people who have a stronger understanding of math.)

I’m surprised your GPS unit can’t do this for you; most of the ones I’ve seen have this capability built right in. What make and model is it?

It isn’t a unit like you are probably used to. This is one that communicates via TCP/IP and does real-time tracking. It sends back GPRMC sentences like the one below.

\$GPRMC,131332,A,1548.075,S,04750.871,W,032.0,056.2,040300,019.1,W*66

The maker is SANAV. The model is GS 101

Average speed multiplied by time will give you the distance exactly (at least within the precision of the speed and time). It’s much more precise than taking the speed at a given time and using that as the speed for an entire section.

I don’t think there is a definitive answer. Both methods have inaccuracies, and which one is better depends on the accuracy of the GPS receiver, smoothness of the path, smoothness of the speed profile, sampling rate, etc. If I were you I’d try both and see which method is more consistent with an actual distance measurement (i.e. cyclecomputer).

I think he means average speed calculated by him as the average of all the speeds reported. For that to be accurate he would have to weight each speed by the length of the time interval, which would probably work out to be the same as adding all the products of speed and length of time interval.

If the GPS did return an average speed for the course then multiplying it by the time would give the most accurate answer.

From looking at their website, it looks like these units send the data out in TTL serial format which could be converted to standard RS-232 port format. It would then be a fairly simple matter to write up a program to read the data at regular intervals and compute the distance traveled. Might be more trouble than it’s worth to you, but it’s an option.

Forgive me, but I am not following.

I have to receive this data as the race is running. We are tracking the race live on the web - as close to real-time as the unit will allow. I’m not sure how I would read TTL serial format “on the run.” The manual only shows the following ways to interact:
TCP
UDP
HTTP
SMS
email
And does not mention TTL at all.

Oh, you don’t have physical access to the GPS unit; I wasn’t clear on that, sorry. Well, in that case, you could program a small web app to access the internet and get the data and compute the distance like I outlined. How do you actually get the information, with a web browser or some other means?

I’ve written a TCP/IP server. (Well, actually, the TCP doesn’t seem to work for the unit, so I’m using UDP until they can get the TCP to work…)

We then send the unit an SMS message telling it to go into “poll” mode and it sends GPRMC sentences as shown above.

Well, the algorithm to convert lat/long to a Great Circle distance is pretty straightforward, so if you wanted to put something together to do the calculations automatically, feel free to email me.

At what cadence does the unit send you data? Once per second, once per hour, etc? And more importantly, is the distance travelled during that time significant compared to the size of any loops or curves in your course? If, say, the device gives you a data point once per minute, and the biker can make it once around the loop in exactly one minute, then your position data are going to be completely useless, since all of the points will be in the same position. Depending on how the GPS unit gets the speed, though, that might still be perfectly good.

The hope is to eventually get the data in once per second, but I am doubtful that this will happen. Now we get it in about in a little more than 10 seconds and less than 15.

I am pretty sure that the loops are big enough that they won’t loop back on themselves in that time frame.