Odometer

timo

Active Member
Licensed User
Longtime User
I've tried doing it this way. But If I change the timer duration it gives me problems. Suggestions? Thanks.

B4X:
'Activity module
Sub Process_Globals
   
   Dim GPS1 As GPS
   Dim Timer1 As Timer
   
End Sub

Sub Globals

   Dim Loc,LocStart,LocStop As Location 
   Dim TickCounter As Int 
   Dim odo,tempOdo As  Float
   Dim lblOdo As Label
        
   
End Sub

Sub Activity_Create(FirstTime As Boolean)
   
   If FirstTime Then
      GPS1.Initialize("GPS")
      Loc.Initialize
      LocStart.Initialize
      LocStop.Initialize
      TickCounter=0 
      
   End If
   
   Activity.LoadLayout("main.bal")'a simple layout with a label 'lblOdo' 
   
   Timer1.Initialize("Timer1",3000)
   Timer1.Enabled = True
   
End Sub

Sub Timer1_Tick

TickCounter=TickCounter+3

If TickCounter= 6 Then 
setStart
End If

If TickCounter= 12 Then 
setStop   
End If


End Sub

Sub setStart

LocStart=Loc

End Sub

Sub setStop

LocStop=Loc

'tempOdo=LocStart.DistanceTo(LocStop) we need a control (see following posts)

'added:

If LocStart.Latitude <> 0.0 Then
   If LocStart.Longitude <> 0.0 Then
      If LocStop.Latitude <> 0.0 Then
         If LocStop.Longitude <> 0.0 Then
            tempOdo=LocStart.DistanceTo(LocStop)
         End If
      End If
   End If
End If

'end added

odo= odo + tempOdo  

lblOdo.text= ("odo: " & Floor(odo) & " m")

TickCounter=0 

End Sub

Sub Activity_Resume
   If GPS1.GPSEnabled = False Then
      ToastMessageShow("Please enable the GPS device before running 'odoometer' ", True)
      Activity.Finish 
   Else
      GPS1.Start(0,0)
      Timer1.Enabled=True
   End If
End Sub

Sub Activity_Pause (UserClosed As Boolean)
   GPS1.Stop
   Timer1.Enabled=False
End Sub

Sub GPS_LocationChanged(Location1 As Location)
   Loc=Location1 
End Sub
 
Last edited:

timo

Active Member
Licensed User
Longtime User
This time it happened even with that timer set to 3000. I think the problem is so elsewhere.
Log:
 

Attachments

  • stamp.jpg
    38.3 KB · Views: 288
Last edited:
Upvote 0

timo

Active Member
Licensed User
Longtime User
I've foud where the problem was. I noticed that the strange distance appears only sometimes, but mostly at the beginning of the Locs acquisition. As you can see on the image, it could happend (here shortly after launching the prg) that one location is kept and the other not (still bad signal). So the DistanceTo calculates exactly between one given location and a 0,0 location.
This situation can also appear if you loose the sats between one acquisition and the other. Because of that it is necessary to add some code to verify that the locations are not 0,0 before passing them to DistanceTo.

Lon 0, Lat 0 is in the middle of the Gulf Of Guinea, so it is better that your odometer remain for terrestrial usage

Hope it could be helpful to someone else.
 

Attachments

  • stamp2.jpg
    66.4 KB · Views: 293
Last edited:
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…