Android Question System Time and GPS Time Accuracy

nemiroG1

Member
Licensed User
Longtime User
I am working on an app that requires excellent time accuracy. I have an external GPS feeding the system at up to 10Hz, and I need to get the time of each update. It has to be accurate to within 10ms, or else I may end up with two records of the same time number. Well, that is exactly what it happening.

Using Erel's code from the GPS Tutorial, https://www.b4x.com/android/forum/threads/gps-tutorial.6592/#content I added a simple line to observe GPS time (attached as a .zip, too).

B4X:
#Region Module Attributes
    #FullScreen: False
    #IncludeTitle: True
    #ApplicationLabel: GPS
    #VersionCode: 1
    #VersionName: 
    #SupportedOrientations: unspecified
#End Region

Sub Process_Globals
    Dim GPS1 As GPS
End Sub

Sub Globals
    Dim lblLon As Label
    Dim lblLat As Label
    Dim lblElev As Label
    Dim lblSpeed As Label
    Dim lblTime As Label
    Dim lblSatellites As Label
    Private lblNMEA As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)
    If FirstTime Then
        GPS1.Initialize("GPS")
    End If
    Activity.LoadLayout("1")
End Sub

Sub Activity_Resume
    If GPS1.GPSEnabled = False Then
        ToastMessageShow("Please enable the GPS device.", True)
        StartActivity(GPS1.LocationSettingsIntent) 'Will open the relevant settings screen.
    Else
        GPS1.Start(0, 0) 'Listen to GPS with no filters.
    End If
End Sub

Sub Activity_Pause (UserClosed As Boolean)
    GPS1.Stop
End Sub

Sub GPS_LocationChanged (Location1 As Location)
    lblLat.Text = "Lat = " & Location1.ConvertToMinutes(Location1.Latitude)
    lblLon.Text = "Lon = " & Location1.ConvertToMinutes(Location1.Longitude)
    lblElev.Text = "Elevation = " & Location1.Altitude
    lblSpeed.Text = "Speed = " & Location1.Speed
    lblTime.Text = "Time = " & Location1.Time
End Sub

Sub GPS_UserEnabled (Enabled As Boolean)
    ToastMessageShow("GPS device enabled = " & Enabled, True)
End Sub

Sub GPS_GpsStatus (Satellites As List)
    lblSatellites.Text = "Satellites:" & CRLF
    For i = 0 To Satellites.Size - 1
        Dim Satellite As GPSSatellite
        Satellite = Satellites.Get(i)
        lblSatellites.Text = lblSatellites.Text & CRLF & Satellite.Prn & _
            " " & Satellite.Snr & " " & Satellite.UsedInFix & " " & Satellite.Azimuth _ 
            & " " & Satellite.Elevation
    Next
End Sub

I am finding that the internal GPS will ALWAYS get a reading at the even full second, and that the external GPS will ALWAYS read to the 1/10th of a second. Well, after logging this data, I will see four or five entries, all with the last two digits of the GPS time reading 00. This means that the routine is seeing a GPS location change in that time, and it does indeed log the different GPS positions, but the time does not indicate a change for these updates. I know that this is not correct. My question is, how do I get resolution on those last two digits?

BTW, this same thing happens if you try to read DateTime.Now, as well.
 

Attachments

  • GPS_with_Time.zip
    12 KB · Views: 381

nemiroG1

Member
Licensed User
Longtime User
Why doesn't the system read down to the millisecond, though? Even trying to read the system ticks doesn't seem to work. It is still only reading to 100ms.
 
Last edited:
Upvote 0

Troberg

Well-Known Member
Licensed User
Longtime User
My suspicion is that it's the device that's not giving more precision. Another device could probably do it, as GPS time is about the most accurate time you can get as a consumer. Then again, another device might even give you less precision.

Maybe you need to rethink a bit. Instead of just a time stamp, also add a counter. You'll still have two records with the same time, but you'll still have a uniques ID and know the correct order of them, which, unless you need to know the exact time of each record down to less than 0.1 s, should be sufficient.

Also, remember that things sometimes bunch up, so even millisecond accuracy might give you duplicates, so some kind of handling might be needed anyway. Never rely on things happening at a certain speed in a non-RT-OS.
 
Upvote 0

nemiroG1

Member
Licensed User
Longtime User
Very true, thank you. I will try to come up with a counter, as that does make sense. Will also try some other devices. Right now I am on Samsung Galaxy S3 and a S5 Active, just for reference. Thought perhaps I was not initializing something right, since the last few digits were always "0".
 
Upvote 0
Top