Skip to content

Instantly share code, notes, and snippets.

@Michcioperz
Last active August 29, 2015 13:55
Show Gist options
  • Save Michcioperz/8767557 to your computer and use it in GitHub Desktop.
Save Michcioperz/8767557 to your computer and use it in GitHub Desktop.
MichiRun - SL4A Python script that will be purely awesome helper for kids who are bad at running on PE at school, haha
# from http://www.johndcook.com/python_longitude_latitude.html
import math
def arc_on_unit_sphere(lat1, long1, lat2, long2):
# Convert latitude and longitude to
# spherical coordinates in radians.
degrees_to_radians = math.pi/180.0
# phi = 90 - latitude
phi1 = (90.0 - lat1)*degrees_to_radians
phi2 = (90.0 - lat2)*degrees_to_radians
# theta = longitude
theta1 = long1*degrees_to_radians
theta2 = long2*degrees_to_radians
# Compute spherical distance from spherical coordinates.
# For two locations in spherical coordinates
# (1, theta, phi) and (1, theta, phi)
# cosine( arc length ) =
# sin phi sin phi' cos(theta-theta') + cos phi cos phi'
# distance = rho * arc length
cos = (math.sin(phi1)*math.sin(phi2)*math.cos(theta1 - theta2) +
math.cos(phi1)*math.cos(phi2))
arc = math.acos( cos )
# Remember to multiply arc by the radius of the earth
# in your favorite set of units to get length.
return arc
def distance_on_earth_sphere(lat1, long1, lat2, long2):
arct = arc_on_unit_sphere(lat1, long1, lat2, long2)
earthradius = 6367.5
return arct*earthradius
# main file
import android, time, arcdist
droid = android.Android()
droid.startLocating(100, 0.25)
dist = 0
while True:
time.sleep(1)
loc = droid.readLocation().result
if loc != {}:
try:
print("%s %s" % (loc['gps']['latitude'], loc['gps']['longitude']))
try:
prevloc
except NameError:
prevloc = loc
newdist = arcdist.distance_on_earth_sphere(loc['gps']['latitude'], loc['gps']['longitude'], prevloc['gps']['latitude'], prevloc['gps']['longitude'])
print("delta %0.5f km = %0.5f meters" % (newdist, newdist*1000))
dist = dist + newdist
print("sum %0.5f km = %0.5f meters" % (dist, dist*1000))
except KeyError:
print("Lack of GPS")
all: arcdist qloc locrot qrot
arcdist:
adb push arcdist.py /storage/sdcard0/sl4a/scripts/
qloc:
adb push qloc.py /storage/sdcard0/sl4a/scripts/
locrot:
adb push locrot.py /storage/sdcard0/sl4a/scripts/
qrot:
adb push qrot.py /storage/sdcard0/sl4a/scripts/
# background script to keep GPS running
import android, time
droid = android.Android()
droid.startLocating(100, 0)
while True:
time.sleep(1)
loc = droid.readLocation().result
import android, time
droid = android.Android()
droid.startSensingTimed(2, 250)
while True:
time.sleep(1)
rot = droid.readAccelerometer().result
print ("%f %f %f" % (rot[1], rot[2], rot[3]))
@mrgame64
Copy link

mrgame64 commented Feb 2, 2014

I'd recommend to make it sleep 10ms instead of 1 in qloc.py, it'll devour battery otherwise D:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment