Code: Select all
import thinkpad
import sys
import os
import win32com.client, time
from math import *
import serial
import string
import datetime
gps = serial.Serial('COM1', 4800, timeout=1)
##Some default global variables
# Default Latitude in degrees. Between -90 and 90.
#latitude=48.583106
# Default Longitude in degrees. Between -180 and 180.
#longitude=7.751436
# Default altitude in meters
#altitude=10000 # in meters
# looking to the horizon=90, looking to the center of Earth=0
#tilt=0
# looking North=0, East=90, South=180, West=270
#azimuth=370
# speed transition. must be >= 0, above 5.0 the transition is instantaneous
#speed=5
# If notcoun=0 camera will move backward from "range meters along the camera axis
Range=0
#Altitude mode that defines altitude reference origin (1=above ground, 2=absolute)
altMode=2
tiltx=90
tilty=0
count=0
t = datetime.datetime.now()
def ge_connect():
""" Open communication with Google Earth and start GE if necessary"""
global GE
GE = win32com.client.Dispatch("GoogleEarth.ApplicationGE")
while not GE.IsInitialized():
time.sleep(0.5)
print ">>> Waiting for Google Earth to initialize."
print ">>>> Connection established."
def ge_setCamera():
""" Set the camera to a given place, altitude and viewing angles """
GE.SetCameraParams(latitude, longitude, altitude, altMode, Range,tilt, azimuth, speed)
def ge_getCameraInfo():
""" Get some informations about the camera"""
cam=GE.GetCamera(False)
camInfos={}
camInfos["focusPointLatitude"]=cam.FocusPointLatitude
camInfos["focusPointLongitude"]=cam.FocusPointLongitude
camInfos["focusPointAltitude"]=cam.FocusPointAltitude
camInfos["focusPointAltitudeMode"]=cam.FocusPointAltitudeMode
camInfos["range"]=cam.Range
camInfos["tilt"]=cam.Tilt
camInfos["azimuth"]=cam.Azimuth
# compute camera altitude (doesn't work if tilt is very near to 90 degrees)
camInfos["altitude"]=(cam.Range)*cos(cam.Tilt*(2*pi)/360)
#experiment
cam.Azimuth=10
print ">>> Setting camera now"
GE.setCamera(cam,1)
print camInfos
return camInfos
def ge_cam_go_diffazimuth(angle,speed=1):
"""
Change camera azimuth by angle value (degrees)
angle can be postitive or negative
"""
cam=GE.GetCamera(False)
cam.Azimuth+=angle
GE.setCamera(cam,speed)
def ge_cam_go_azimuth(angle,speed=1):
"""
Rotate camera to azimuth angle (degrees)
"""
cam=GE.GetCamera(False)
cam.Azimuth=angle
GE.setCamera(cam,speed)
def ge_cam_go_difftilt(angle,speed=1):
"""
Change camera tilt by angle value (degrees)
angle can be postitive or negative
"""
cam=GE.GetCamera(False)
cam.Tilt+=angle
GE.setCamera(cam,speed)
def ge_cam_go_tilt(angle,speed=1):
"""
Rotate camera to tilt value (degrees)
"""
cam=GE.GetCamera(False)
cam.Tilt=angle
GE.setCamera(cam,speed)
def ge_cam_go_roll(angle,speed=1):
"""
Rotate camera to tilt value (degrees)
"""
cam=GE.GetCamera(False)
cam.roll=angle
GE.setCamera(cam,speed)
def ge_cam_go_diffrange(length,speed=1):
"""
Change camera rangge by angle value (degrees)
angle can be postitive or negative
"""
cam=GE.GetCamera(False)
cam.Range+=length
GE.setCamera(cam,speed)
def ge_cam_go_range(length,speed=1):
"""
Change camera rangge by angle value (degrees)
angle can be postitive or negative
"""
cam=GE.GetCamera(False)
cam.Range=length
GE.setCamera(cam,speed)
def ge_cam_rotate(azimuth=None,tilt=None,diffazimuth=0,difftilt=0):
pass
def ge_cam_diffrotate():
pass
def ge_cam_difflook(difflatitude=0,difflongitude=0,speed=1):
""" Looks like it doesn't work if tilt=0 """
cam=GE.GetCamera(False)
cam.FocusPointLatitude+=difflatitude
cam.FocusPointLongitude+=difflongitude
GE.setCamera(cam,speed)
def ge_getLocation():
loc=GE.GetPointOnTerrainFromScreenCoords(0,-1)
print loc.Latitude
def ge_setAlt(altin, speed):
cam=GE.GetCamera(False)
cam.FocusPointAltitude=2 #1=AGL, 2=MSL
cam.FocusPointAltitude+=altin #meters
GE.setCamera(cam,speed)
def ge_setLatLong(lat1,long1,speed):
cam=GE.GetCamera(False)
cam.FocusPointLatitude=lat1
cam.FocusPointLongitude=long1
GE.setCamera(cam,speed)
def getgps():
line = gps.readline()
datablock = line.split(',')
if line[0:6] == '$GPRMC':
latitude_in = string.atof(datablock[3])
longitude_in = string.atof(datablock[5])
try:
altitude = string.atof(datablock[8])
except ValueError:
# use last good value
altitude = altitude
speed_in = string.atof(datablock[7])
try:
heading_in = string.atof(datablock[8])
except ValueError:
# use last good value
heading_in = heading_in
if datablock[4] == 'S':
latitude_in = -latitude_in
if datablock[6] == 'W':
longitude_in = -longitude_in
latitude_degrees = int(latitude_in/100)
latitude_minutes = latitude_in - latitude_degrees*100
longitude_degrees = int(longitude_in/100)
longitude_minutes = longitude_in - longitude_degrees*100
latitude = latitude_degrees + (latitude_minutes/60)
longitude = longitude_degrees + (longitude_minutes/60)
speed = int(speed_in * 1.852)
altitudegps=altitude
headinggps=heading_in
latitudegps=latitude
longitudegps=longitude
speedgps=speed
#set GE to stuff
cam=GE.GetCamera(False)
cam.FocusPointAltitude=2 #1=AGL, 2=MSL
#cam.FocusPointAltitude=altitudegps #meters
cam.Azimuth=headinggps
cam.Range=0
cam.FocusPointLatitude=latitudegps
cam.FocusPointLongitude=longitudegps
GE.setCamera(cam,2.5)
os.system('CLS')
print altitudegps
print headinggps
print latitudegps
print longitudegps
print speed
if __name__ == '__main__':
ge_connect()
hk = thinkpad.Accelerometer()
oldx=hk.x()
oldy=hk.y()
trip=0
waittime=.35
#ge_setAlt(0,5) #altitude (UNITS!)
count=0
while True:
#time.sleep(2)
x=hk.x()
y=hk.y()
tiltx=(x-373)/1.611
tilty=((y-519)/1.555)*-1
#count=count+1
#os.system('CLS')
#ge_cam_go_tilt(tiltx,5) #tilting forward/back
line = gps.readline()
datablock = line.split(',')
if line[0:6] == '$GPRMC':
latitude_in = string.atof(datablock[3])
longitude_in = string.atof(datablock[5])
try:
altitude = string.atof(datablock[8])
except ValueError:
# use last good value
altitude = altitude
speed_in = string.atof(datablock[7])
try:
heading_in = string.atof(datablock[8])
except ValueError:
# use last good value
heading_in = heading_in
if datablock[4] == 'S':
latitude_in = -latitude_in
if datablock[6] == 'W':
longitude_in = -longitude_in
latitude_degrees = int(latitude_in/100)
latitude_minutes = latitude_in - latitude_degrees*100
longitude_degrees = int(longitude_in/100)
longitude_minutes = longitude_in - longitude_degrees*100
latitude = latitude_degrees + (latitude_minutes/60)
longitude = longitude_degrees + (longitude_minutes/60)
speed = int(speed_in * 1.852)
altitudegps=altitude
headinggps=heading_in
latitudegps=latitude
longitudegps=longitude
speedgps=speed
#set GE to stuff
#cam=GE.GetCamera(False)
#cam.FocusPointAltitude=2 #1=AGL, 2=MSL
#cam.FocusPointAltitude=altitudegps #meters
#cam.Azimuth=headinggps
#cam.Range=0
#cam.FocusPointLatitude=latitudegps
#cam.FocusPointLongitude=longitudegps
#GE.setCamera(cam,2.5)
os.system('CLS')
print altitudegps
print headinggps
print latitudegps
print longitudegps
print tiltx
print tilty
count=count+1
""" Load kml data from a string"""
#Don't add all the kml header => pywintypes errors
data="""
<kml xmlns="http://www.opengis.net/kml/2.2"
xmlns:gx="http://www.google.com/kml/ext/2.2">
<Document>
<Placemark>
<name>%s</name>
<Camera>
<longitude>%s</longitude>
<latitude>%s</latitude>
<altitude>%s</altitude>
<heading>%s</heading>
<tilt>%s</tilt>
<roll>%s</roll>
<altitudeMode>clampToSeaFloor</altitudeMode>
</Camera>
</Placemark>
</Document>
</kml>
"""% (count,longitudegps,latitudegps,altitudegps,headinggps,tiltx,tilty)
#GE.LoadKmlData(data)
f=open("C:\gps.kml", 'w')
f.write(data)
f.close()
GE.OpenKmlFile("C:\gps.kml",True)
#Feature=GE.GetFeatureByName(count)
#GE.SetFeatureView(Feature,5)
ser.close()
Basically what goes on up in the code is it pulls GPS data (Heading, Altitude, Lat/Long) and then accelerometer data from HDAPS in a Thinkpad and uses that to position itself in Google Earth using a temporary KML file (I've tried to avoid writing a file repeatedly but there's no other way).
Not all the code is mine, bits and pieces are mashed up from:
NMEA in Python
KML XML Camera Handling
thinkpad.py is here. Says it doesn't work in Vista but the accelerometer stuff still does (but other stuff doesn't).
GE API COM in Python
But yeah. Unless you know python or anything you probably won't be able to use this I just wanted to share what I've figured out cause I think it's pretty cool
EDIT:
Oh and I have some code that lets you tab around in Firefox (or anything with tabs) by lightly tapping the sides of the laptop if anyone wants it. It's mostly just for novelty though because unless you're on a very stable surface (a table) it just messes with you.



