How can I program the app to compare the measured speed with a set speed limit?

36 views
Skip to first unread message

Deborah

unread,
Mar 16, 2019, 3:12:50 AM3/16/19
to MIT App Inventor Forum
Currently, I am able to detect my current location, coordinates, and speed based on the Location Sensor while I am constantly moving. These sets of coordinates retrieved by Location Sensor are now stored in a .csv file. 

Now I would like to program the app such that as I move along a certain area, I would like to compare if the speed I am going at currently, complies with the speed limit at that particular area. My idea is to input a coordinate at that spot and use an if statement to compare if the speed measured by the location sensor is above the speed limit I set. If yes, send a warning message. If no, send a notification that I am within the speed limit. 

Currently, I am not able to yield any results. May I ask on how I can program this?

 Also, another worry is if the app is able to do the speed comparison since I am constantly moving. Based on earlier experiments, it seems like the app is not able to do real-time calculations. I manage to build a program that calculates the distance by manually inputting the coordinates. However, when I use that same set of program and change it to the location sensor's coordinates and the remembered coordinate I saved earlier, it is unable to compute the distance then. 

Any help and feedback would be very much appreciated. thank you. 




SteveJG

unread,
Mar 16, 2019, 9:09:07 AM3/16/19
to MIT App Inventor Forum
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.

This event is the most important part of the control when using a GPS receiver. Latitude, longitude and altitude are all local variables. Hover your mouse over one of them to capture either a get latitude or set latitude to block or a get longitude or set longitude to block or a get altitude or set altitude to block as required. Be aware, the altitude value is notoriously inaccurate as compared to the latitude / longitude. This value can easily be off a lot. Why? A lot of geometry information is being processed, the earth is not a sphere (it is a geoid) which makes this determination difficult. The values appear order of magnitude reasonable for values greater than a hundred meters.

Best practice is to always verify that a sensor exists on a device before you attempt to acquire data from it. Do not assume a sensor exists simply because it is a frequently-used sensor. Device manufacturers are not required to provide any particular sensors in their devices.

See also TimeInterval.


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.

You said  when you " calculates the distance by manually inputting the coordinates. However, when I use that same set of program and change it to the location sensor's coordinates and the remembered coordinate I saved earlier, it is unable to compute the distance then"  ... show your blocks, you certainly can use two points to calculate the average velocity per time elapsed.  Comparing that to speed may not always be precise because you are probably not comparing the same instances in time as a guess. 

Regards to your speed limits and comparing speed to the limits.  That can be done several ways.  A simple way is to use a ringed fence .  A discussion showing how to do that is here 
  • Chapter 18. Programming Your App to Make Decisions  .   Be aware the logic only works if you are in the Western Hemisphere Debora, if you are located in the Eastern Hemisphere, you will have to adopt the shown Blocks.  Another factor comes into play.  
  • Once you set up your ring fence or multiple ring fences, use conditional statements to calculate when the Android is located (at least 'thinks' it is) within the ringed fence or not, if it is within the area compare the speed to the speed you associate with the particular ringed fence.   If the speed is greater than your posted speed, set off a siren. Be aware, a degree of latitude is as described here https://groups.google.com/forum/#!msg/mitappinventortest/61NSDQNEJC8/xVPTwt_hAwAJ;context-place=categories/mitappinventortest  .   The moral of that discussion is do NOT make the ringed fence too small, Accuracy is going to eat your lunch.
  • Your GPS receiver really does not always know where it is at any time.  The property called Accuracy always lets you know how reliable the satellite fix is.  Smaller values are better (read the tutorial)  The tutorial does not explain the entire story
  • Accuracy  

    Indicates Android device accuracy level, in meters.

    AI2 has no control to tell you how many satellites are being used in a fix or how strong the received signals are at the GPS. Instead, AI2 use other information from the satellite data stream to establish a reliability circle, a distance circle with a radius of the indicated accuracy.  It only reports the radius of that circle as described below. Your device may be in the center of the circle, you may be on the edge of the reliability circle or in the center. Accuracy demonstrates the quality of the satellite fix by reporting plus/minus x meters accuracy at any moment. Lower values are better.

    The Android Developer Reference defines accuracy as “the radius of 68% confidence."  If you draw a circle centered at this location's latitude and longitude, and with a radius equal to the accuracy, then there is a 68% probability that the true location is inside the circle. In statistical terms, it is assumed that location errors are random with a normal distribution, so the 68% confidence circle represents one standard deviation. In practice, location errors do not always follow such a simple distribution. This accuracy estimation is only for horizontal accuracy, and does not indicate the accuracy of altitude if those are reported.  If this location does not have an accuracy, then 0.0 is returned.

  • So, when you do a calculation of speed, it will be accurate or not depending on the Accuracy at the time of sampling.

Does this help Debora?  What you want to do is fairly complex; is doable and might be a lot of fun.   Post your Blocks here in the Forum when you are done and let us see how it works!

Regards,
Steve

Reply all
Reply to author
Forward
0 new messages