Moving Speed out of GPS data

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
simon.schvartzman
Posts: 638
Joined: Tue Jul 29, 2014 12:52 am
Location: Brazil

Moving Speed out of GPS data

Post by simon.schvartzman » Wed Oct 17, 2018 7:51 pm

Hi nerds (in the good sense), I've seen several old posts regarding the subject but none of them was conclusive and therefore I decided to open a new topic.

I would like to be able to calculate/show the speed I'm traveling on a car by using the GPS info in order to calculate it.

So far I've read the coordinates and the timestamp at fixed intervals then using the 'haversine' formula I can calculate the distance which divided by the timestamps' difference should give me the speed, right? Wrong...

Or may be partially right/wrong. The problem is that the result I get is extremely unstable. I'm driving at let say 30 km/hour the App shows the same result as the car's speedometer and suddenly it goes to 78 and back to 30.

By what I've read in the last several hours it is probably due to the GPS accuracy which takes me to the point of this post, has anybody a working script (willing to share) that would provide a reliable speed reading using the GPS coordinates (both on iPhone and Android)? Or at least a suggestion as how to implement it.

Many thanks
Simon
________________________________________
To ";" or not to ";" that is the question

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7215
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Moving Speed out of GPS data

Post by jacque » Thu Oct 18, 2018 4:59 pm

I don't have a script, only an anecdote. During a recent hike, Google estimated my distance walked as 8.5 miles. I was going over some rugged terrain near the Mississippi River and had only actually walked the equivalent of a few city blocks. Looking at the tracking map later, Google showed that I had "walked" to the center of the river and back.

It looks like my phone lost GPS for a while and Google only picked up an approximate location at some point where there was a break in the foliage around me. Moral of the story, if Google can't get around it, I'm sure I couldn't either. We're at the mercy of the satellites.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

simon.schvartzman
Posts: 638
Joined: Tue Jul 29, 2014 12:52 am
Location: Brazil

Re: Moving Speed out of GPS data

Post by simon.schvartzman » Thu Oct 18, 2018 9:23 pm

Hi @jacque, great story. I though Jesus was the only one able to walk on water but it seems according to Google you are as well. Of course Jesus didn't have a GPS to tell him he couldn't :D (nor did he have a LC forum to share...)

Now I wonder how Waze (or any other GPS running on the cell) deals with it. Comparing Waze speed readings with my car's speedometer I realised that Waze takes a lit bit more time to react to speed changes than the car meter.

I would guess that Waze is making some math (like average of the last readings, or dismissing points clearly out of the curve) before displaying the result. That's the algorithm I'd like to get instead of having to go to a lot of tries to find out it myself.

I have done some web research with no practical results...
Simon
________________________________________
To ";" or not to ";" that is the question

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7215
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Moving Speed out of GPS data

Post by jacque » Fri Oct 19, 2018 8:05 pm

If you use Google Fit, you could work magic too. :) Seriously, anything more than simple arithmetic is beyond me, but we have some brilliant math gurus in our community. There are several on the mailing list who may lurk here as well, and @-hh is no slouch either. Maybe he'll have ideas.

I wouldn't ask Google for help though. ;)
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

capellan
Posts: 654
Joined: Wed Aug 15, 2007 11:09 pm

Re: Moving Speed out of GPS data

Post by capellan » Fri Oct 19, 2018 11:35 pm

Hi Simon,

Does your GPS code works on an Apple or an Android phone?
I am curious to learn how you are calculating it.

Al

simon.schvartzman
Posts: 638
Joined: Tue Jul 29, 2014 12:52 am
Location: Brazil

Re: Moving Speed out of GPS data

Post by simon.schvartzman » Mon Oct 22, 2018 11:02 pm

HI @capellan, I'm running on iPhone but i guess it would be the same on Android.

The math is the following (assuming we are in the second cycle):

1 - at fixed intervals (2 seconds at the moment) I read the position and time stamp using

Code: Select all

 --get actual position and time
   put mobileSensorReading ("location", true) into tDetails 
   put tDetails["latitude"] into tLatitude
   put tDetails["longitude"] into tLongitude
   put (tLatitude && "," && tLongitude) into ActPosition
   put tDetails["timestamp"] into ActTimeStamp
2 - Get the ActualDistance to PreviousPosition using the 'haversine' formula

Code: Select all

function calcDistance ActPosition, PreviousPosition
   
   if (point1 <> point2) then 
      put 6373 into pR  -- earth radius
      -- get latitude and longitude of each point
      put the item 1 of point1 into plat1
      put the item 2 of point1 into plon1
      
      put the item 1 of point2 into plat2
      put the item 2 of point2 into plon2
      
      ## convert to radians
      put plat1 / 180 * pi into lat1
      put plon1 / 180 * pi into lon1
      put plat2 / 180 * pi into lat2
      put plon2 / 180 * pi into lon2
      
      put lat2 - lat1 into dlat
      put lon2 - lon1 into dlon
      
      put (sin(dlat/2))^2 + cos(lat1) * cos(lat2) * (sin(dlon/2))^2 into a
      put 2 * atan2( sqrt(a),sqrt(1-a)) into c
      
      return pR * c
   else
      return 0
   end if
end calcDistance
3 - Calculate ElapsedTime between readings (ActTime - PreviousTime)

4 - Get the speed in km/h as (ActualDistance * 60 * 60 )/ElapsedTime

5 - Save "Actuals" as Previous

Hope it helps, once I'm satisfied with the results I will share my code.
Any suggestions are welcomed.
Simon
________________________________________
To ";" or not to ";" that is the question

mtalluto
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 125
Joined: Tue Apr 11, 2006 7:02 pm
Location: Seattle, WA
Contact:

Re: Moving Speed out of GPS data

Post by mtalluto » Mon Apr 20, 2020 10:14 pm

The speed value that comes from mobileSensorReading("location",true) is accurate for meters per second.

To get miles per hour, multiply the output by 2.237.
To get kilometers per hour, multiply the output by 3.6.

I ran this in my car. I found the output to be spot on. But, there is a bit of lag before the mobile device will match the speedometer. I found 1 to 2 seconds to be enough to catch up. I was relying on the locationChanged message to get my updates. You also run a tighter loop on a custom message that polls the data from mobileSensorReading(). That may give you a faster to true speed result.
Mark Talluto
--
Canela
design - develop - deploy: https://appli.io
Database and Cloud for LiveCode Developers: https://livecloud.io
Company: https://canelasoftware.com

mtalluto
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 125
Joined: Tue Apr 11, 2006 7:02 pm
Location: Seattle, WA
Contact:

Re: Moving Speed out of GPS data

Post by mtalluto » Mon Apr 20, 2020 10:26 pm

This is possible now. I do not know which version of LC getting the speed was released.

Sample code:

on openCard
if the platform is "iphone" then
mobileStartTrackingSensor "location", false
end if
end openCard

command locationChanged
local tSensorDataA, tSpeed

--RETURN A STRING WITH THE CURRENT LATITUDE, LONGITUDE AND ALTITUDE
put mobileSensorReading("location",true) into tSensorDataA
if tSensorDataA is empty then
put "No data" into fld "speed"
exit locationChanged
end if

put tSensorDataA["speed"] into tSpeed --output is in meters per second
if tSpeed is empty or tSpeed = "0.0" then
set numberFormat to "x"
put 0 into tSpeed
put "0" into fld "speed"
exit locationChanged
else
set numberFormat to "x.x"
end if

put tSpeed * 2.237 into tSpeed --convert meters per second to miles per hour
if tSpeed > sMaxSpeed then put tSpeed into sMaxSpeed
put "Max speed:" && max(tSpeed,sMaxSpeed) into fld "max speed"

put tSpeed into fld "speed"
end locationChanged
Mark Talluto
--
Canela
design - develop - deploy: https://appli.io
Database and Cloud for LiveCode Developers: https://livecloud.io
Company: https://canelasoftware.com

Post Reply

Return to “Talking LiveCode”