Advertisement
Guest User

Pythonista ios dead reckoning script

a guest
Aug 23rd, 2014
347
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. """The MIT License (MIT)
  2.  
  3. Copyright (c) 2014 Andrew Luo luo_andrew a|t yahoo.com
  4.  
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11.  
  12. The above copyright notice and this permission notice shall be included in
  13. all copies or substantial portions of the Software.
  14.  
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE."""
  22.  
  23. def navigate():
  24.     import _motion as motion
  25.     from numpy import zeros, divide
  26.     from math import cos
  27.     from operator import add, sub
  28.     from console import clear
  29.     m = motion.MotionManager()
  30.     m.stop()
  31.     m.start()
  32.     import time
  33.     print "\nStarting motion update, please wait 10 seconds for the values to stabilize. During this time, please avoid touching the phone"
  34.     """First we start updating, we allow a 5 second time out because there is a significant amount of drift initially present"""
  35.     time.sleep(10)
  36.     print "\nStarting calibration in 10 seconds. Please keep the device UTTERLY still in an position that you expect it to be most commonly in during the measurements later."
  37.     time.sleep(6)
  38.     print "\nCalibration starting in 5 seconds."
  39.     time.sleep(5)
  40.     print "\nCalibration starting..."
  41.     caliacc = zeros(3)
  42.     caliangle = zeros(3)
  43.     cali=1
  44.     for u in xrange(300):
  45.         caliacc = caliacc + m.user_acceleration
  46.         caliangle = caliangle + m.rotation_rate
  47.     zeroingacc= divide(caliacc, 300)
  48.     zeroingang=divide(caliangle, 300)
  49.     print "\nCalibration ended, you may start to move in 5 seconds."
  50.     time.sleep(5)
  51.     zeroingacc0=zeroingacc[0]
  52.     zeroingacc1=zeroingacc[1]
  53.     zeroingacc2=zeroingacc[2]
  54.     zeroingang0=zeroingang[0]
  55.     zeroingang1=zeroingang[1]
  56.     zeroingang2=zeroingang[2]
  57.     speed0=0
  58.     speed1=0
  59.     speed2=0
  60.     angle0=0
  61.     angle1=0
  62.     angle2=0
  63.     movement0=0
  64.     movement1=0
  65.     movement2=0
  66.     time2=0.083 #You need to change this number to make it work on your device, just replace the while loop and timeit
  67.     time1=time2*9.83
  68.     """Apparently for small lists, direct assignment is twice as fast as map and lists! Very unexpected!"""
  69.     while 1:
  70.         cos0=cos(angle0)
  71.         cos1=cos(angle1)
  72.         cos2=cos(angle2)
  73.         speed0 += (-zeroingacc0 + m.user_acceleration[0]) * time1*cos0*cos2
  74.         speed1 += (-zeroingacc1 + m.user_acceleration[1]) * time1*cos1*cos2
  75.         speed2 += (-zeroingacc2 + m.user_acceleration[2]) * time1*cos0*cos1
  76.         angle0 += (m.rotation_rate[0]-zeroingang0) * time2
  77.         angle1 += (m.rotation_rate[1]-zeroingang1)* time2
  78.         angle2 += (m.rotation_rate[2]-zeroingang2)* time2
  79.         movement0 = movement0+speed0*time2*cos0*cos2
  80.         movement1 = movement1+speed1*time2*cos1*cos2
  81.         movement2 = movement2+speed2*time2*cos0*cos1
  82.         #speed = map(lambda x, y: time*add(x, y), speed, map(sub, list(m.user_acceleration), zeroingacc))
  83.         #angle = map(add, angle, list(m.rotation_rate))
  84.         #movement = map(lambda x,y: time*add(x,y), movement, map(lambda l, o: l*o, map(cos, angle), speed))
  85.         print movement0, movement1, movement2
  86.         clear()
  87. navigate()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement