Location Sensor (Speed)

With App Inventor I tried to create a speedometer for my bicycle using the speed of the position sensor. When you move, the speedometer works well respecting the speed, but the problem is that when you stop, the speed doesn't always go to zero. This is a big problem because it increases the distance even if you stay still. What can be done to avoid it? Some idea?

Set LocationSensor.Enabled = false when you stop might be what you should do Stef. That will disable the LocationSensor. When you start peddling again, set LocationSensor.Enabled=true.

You could also note the location at the beginning of your trip and then again at the end. Calculate the distance between the two geocoordinates and divide by the duration (the time of your bike ride) to get mph or whatever. That is your average trip speed.

The LocationSensor ā€˜calculatesā€™ speed by noting a previous location; noting the present location and calculating the duration by noting the time between LocationChanged events.

What is happening to you at the moment, is the LocationChanged event keeps firing; even while stopped. When that happens, the GPS provides a ā€˜newā€™ location that is slightly different from the location where you actually stopped your bike. This is complicated because the GPS does not know your location precisely. Where you actually are located may 3 or for or 50 meters away from your ā€˜realā€™ location. If the satellite fix you get while you are stopped is 50 meters away while you are stopped (without turning off the ability of the LocationSensor to record an event), it could ā€˜addā€™ 50 meters to your distance traveled.

Sorry, this is the way speed works. Just disable the sensor when you stop might work for you.

Regards,
Steve

2 Likes

I thank you for your answer. But how can I tell the Location Sensor that I stopped? I donā€™t want to put a button every time I stop and start again.

Changing the LocationSensor.Enabled state with a Button is unacceptable? You donā€™t ā€œwant to put a button every time I stop and start again.ā€ Are you afraid of wearing out your finger? There are some alternatives to consider:

ā€“ unfortunately a Button event handler used like a toggle switch is your best solution to enable and un-enable the LocationChanged event. You could use a Switch component instead but you still would have to engage it.

ā€“ talk to it; reason with the thing. Use a SpeechRecognizer to coax the LocationSensor to stop working and plead to start again using a voice command

ā€“use an Accellerometer component that will respond when you shake your Android vigorously. Perhaps one shake to stop; two strong shakes to start.

-connect your app to a BlueTooth device mounted on your handle bars that communicates with your app. Use the device as a switch to elicit a Procedure to set the LocationSensor enabled state. That would be pretty neat.

ā€“realistically, an Event Handler type component must be used as an on/off switch.

Sorryā€¦ a Button on; Button off is easiest to code.

What would you like to do to turn it on/off instead?

Do you understand how a GPS receiver works and how the LocationSensor can determine speed? To understand why you see what happens, a reading of this tutorial Using the Location Sensor will help you understand.

A calculation of speed when using the speed property requires that the LocationSensor capture two separate satellite fixes and two LocationChanged events. The sensor cannot calculate instantaneous velocity. It has to have a history of at least two points.

LocationChanged(number latitude, number longitude, number altitude, number speed)
Indicates that a new location has been detected.
Called when the Android device reports a new location.

speed units - the LocationSensor reports speed in m/sec units), convert to km/hr if you want speed in kilometers (1000 meters) and also be aware m/sec must be addressed to convert kilometers/ hour . m/sec * km/1000m * 60 sec/min * 60 min/hour = km/hour .

Speed has a second component, the TimeInterval ā€¦ how frequently you update the LocationSensor to determine a satellite fix. The initial satellite fix usually takes about 30-40 seconds; subsequent fixes dependent on factors discussed in the tutorial takes at least 10 to 20 seconds and how good a GPS receiver is in your Android, so it makes little sense to set the TimeInteral to less than about 20000 ms (20 seconds). This means, the speed will update at the best, every 20 seconds if even that.

What happens when your sensor is enabled. Here is a plot of where an Android having a high end GPS receiver is sitting at rest on a desktop. The box is about 50 m wide and shows the locations where the GPS indicates the Android is located over a period of about 10 to 15 minutes. Remember, the GPS receiver does NOT move, it just reports that it is moving in the LocationChanged event.

MarkersAndroidAtRest

Do you need more information? The following documents the parameters a developer can control using the LocationSensor LocationSensor. Notice, there is only one way to enable/disable itā€™s activity

Enabled
Indicates whether the user has specified that the sensor should listen for location changes and raise the corresponding events.

If you do not want to ā€˜listenā€™ for location changes, you must set LocationSensor.Enabled to false; then re-enable it by setting to true.

Does this information help?

1 Like

In my app created with app inventor I had a button that reset my speed to zero. It works, but I got tired every time I stopped with the bicycle, push the button every time. I thought about the distance between the latitude and the longitude of departure and arrival, but when I go out with the bicycle I start from a point and always return to the same point. I tried at least 30 apps on the play store, but more or less the same flaw. Only one proved to be very good with a zero on the speed. Thanks for the info. Hello

This should solve your problemā€¦
Basically compares latitude with previous_latitude & longitude with previous_longitude, if either is not the same then your moving & update the speed, else youā€™re not moving so set speed to zeroā€¦

Hi Dave,

This is a ā€˜creativeā€™ solution. How did you test it? On a desktop? Riding in a vehicle?

Comparing a previous lat/lon to the current lat/lon is what the LocationSensor does to obtain itā€™s estimate of the Androidā€™s average velocity (speed) between location changed events.

The LocationSensor.LocationChanged even handler ā€˜firesā€™ when the gps receiver
achieves a new satellite fix and either latitude or longitude changes from the previous values of the prior satellite fix.

The LocationChanged fires when the gps ā€˜thinksā€™ the Android has moved. This happens when the gps receiver receives an updated latitude or longitude from an updated satellite fix. It may occur at any time and not necessarily when the device moves. The Accuracy of your GPS readings is continually in a state of flux depending on, among other things, the number of satellites it uses to calculate the new coordinates.

Theā€™solutionā€™ you believe is determining whether the Android is actually moving has multiple issues. The statement you make with regards to yourr method "compares latitude with previous_latitude & longitude with previous_longitude, if either is not the same then your moving & update the speed, else youā€™re not moving " is not necessarily true. The latitude or longitude reported will vary on subsequent satellite fixes whether the device physically moves or not.

Have you done the tutorial LocationSensor . The section discussing the Property called Accuracy is relevant. You should build the tutorial example or load the aia. The code you shared will indicate a device sitting at rest on a desk is not moving (although the LocationSensor erroneously determines you are moving); however the algorithm also determines you are not moving, even when you are moving.

There are several issues with your reasoning.

The gps in most phones cannot, even in the best of circumstances resolve movement when it is less than about 2 meters.

The gps receiver, because of how it works does not know precisely where it is, especially when the Accuracy is greater than 2 meters or so. Accuracy is usually the much larger than several meters.

Unfortunately, the Block that indicates 0 mph always displays.regardless of stationary activity or movement.

A good try Dave. Unfortunately the claim it can determine that the Android is not moving is false. My tests show that upon reproducing the code will/may indicate 0 mph whether the device is moving or not. In fact, it always indicates 0 mph.

I tested this by walking at about 2.5-2.7 mph. All the while, the algorithm indicated 0 mph but the displayed speed (using code I supplied) indicated over 2mph. Have you tried your code in a vehicle? Perhaps I missed something in transcribing the code. Could you post your aia?

Regards,
Steve

Very interesting post...

Also i need a speedomter in my app... if it is available the aia file will be great

Hi Steve, thanks for your comments. TBH it wasnā€™t tested on a device and the text iā€™d written was basically sudo code. Having tried it on a device and gone through the blocks again, yes, there was a few logic errors and a few assumptions iā€™d made. Iā€™ve gone back to the drawing board and after a bit or research and head scratching i think might just have cracked itā€¦ As requested here is the .aia fileā€¦ Let me know your thoughts (I mean that genuinely, not being sarcastic :smiley: )

Speedo_v2(1).aia (37.4 KB)

EDIT: Iā€™ve included some extra bits (in red) for diagnostics, obvs if you donā€™t want it, remove itā€¦ Same with the heading calculation & smoothingā€¦

PS Have a play with the timing of the ā€˜SpeedTimerā€™ā€¦

PPS I havenā€™t tested it for accuracy, itā€™s more of a proof of concept, donā€™t shoot me if iā€™ve made an error in the math!

1 Like

@carto_lover
Try this simple as it gets code. Run the app while bicycling or riding in a vehicle. Standing still, it will indicate a speed because each successive satellite fix provides new but slightly different latitude and longitude calculations even though the gps receiver is not moving. The LocationSensor algorithm calculates the speed in meters per second. The app searches for a satellite fix every 20 seconds. This means you need to wait about a minute before the app will determine your average velocity between the two satellite fix geocoordinates. Convert the resulting mps as shown to mph (conversion factor of 2.23694) or convert to kph (conversion factor of 3.6) instead by modifying the Blocks.

Yes, this is that simple.

so... is there any way to develop a gps speedometer and work real time as all apps in play store? i just want to add it to my app and refreshes to slow... or i have to develop it with android studio?

hello, can you provide your project to study it? i try but i cant do it work in real time...

@JohnnieK Use the code shown in the #6 discussion.

No app using the GPS can work instantaneously using the constellation used by the LocationSensor. The gps using the LocationSensor takes 30 to 60 seconds to acquire it's first satellite fix and subsequent fixes might be possible each successive fix at up to 10000 ms intervals. To record speed, it takes the LocationSensor to report at least two successive satellite fixes. Read about this in LocationSensor .

You can try to set the TimeInterval to 1000 ms, with unpreditable results. The TimeInterval determines the minimum time interval, in milliseconds, that the sensor will try to use for sending out location updates. However, location updates will only be received when the location of the phone actually changes, and use of the specified time interval is not guaranteed. For example, if 30000 is used as the time interval, location updates will never be fired sooner than 30000ms, but they may be fired anytime after.

Values smaller than 30000ms (30 seconds) are not practical for most devices. Small values may drain battery and overwork the GPS.

How are you trying? Are you testing on a bicycle or in a car?

1 Like

i dont understand where is #6.... i am new to site :stuck_out_tongue: how other apps in play store get speed in real time?

motorcycle, i get a speed in a few seconds but when i increase speed i dont get the speed, only after a lot seconds... i saw with android studio a project and was "real time" results. to your code i see...19km........ after a lot of sec..... 45km....... after a while 70km

Change the IntervalTime to 1000 ms and see what happens. If you do not get an improvement, you are getting the best possible using the LocationSensor that is part of App Inventor.

Did you test it or is this a description of what they claim? The newest phones have the ability to use information from several satellite constellations. The MIT App Inventor 2 control does not. If you see an improvement using another app; do so. Instead of using LocationSensor, you might have a different result using one of the extensions that are 'replacement' controls (search here App Inventor Extensions | Pura Vida Apps . Will an extension provide a better result, I do not know.

The conversation for May 6 Johnnie has the code.

i see... the app from android studio was real time, like all speedometer apps in play store, i just dont know to use Studio, it was very complicated for me and the app i made is in MIT app... so i dont know how to join them :stuck_out_tongue: i ll try to minimize the ms and lets hope it works :slight_smile: thanks for your time

OK. Good luck. Let us know what you discover.

Can I know How did you make your speedometer? I tried the simplest one but it doesn't work!