Guest User

FreeTrack FreePIE Android python script

a guest
Dec 28th, 2018
493
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.24 KB | None | 0 0
  1. def median(lst):
  2.     n = len(lst)
  3.     return (sorted(lst)[n//2] if (n % 2 == 1) else sum(sorted(lst)[n//2-1:n//2+1])/2.0)
  4.    
  5. def mean(lst): return round(sum(lst)/len(lst), 3)
  6.  
  7. def difference(a, b): return (b - a if (a <= b) else (a - b) * 2)
  8.  
  9. class Orientation:
  10.     def __init__(self, axis, offset = 0, filter = 'A', sampleSize = 80, stillSampleMultiplier = 2.5, measurementError = 0.2):
  11.         self.samples = []
  12.         self.axis = axis
  13.         self.offset = offset
  14.         self.filter = filter
  15.         self.sampleSize = sampleSize
  16.         self.measurementError = measurementError
  17.         self.stillSampleMultiplier = stillSampleMultiplier         
  18.            
  19.     def getPhoneValue(self):
  20.         if (self.axis == 'Yaw'): return round(-filters.deadband(android[0].googleYaw, deadband), 3)
  21.         elif (self.axis == 'Pitch'): return filters.deadband(android[0].googleRoll, deadband)
  22.         elif (self.axis == 'Roll'): return filters.deadband(android[0].googlePitch, deadband)
  23.  
  24.     def update(self):
  25.         if (self.axis == 'Yaw'): lastValue = freeTrack.yaw
  26.         elif (self.axis == 'Pitch'): lastValue = freeTrack.pitch
  27.         elif (self.axis == 'Roll'): lastValue = freeTrack.roll
  28.  
  29.         nowValue = self.getPhoneValue();
  30.                
  31.         if (difference(lastValue, (nowValue + self.offset)) > 5): # 360° turn - reset sample pool
  32.             self.samples = []
  33.  
  34.         self.samples.insert(0, nowValue + self.offset) # new sample
  35.  
  36.         if (len(self.samples) > self.sampleSize and difference(lastValue, (nowValue + self.offset)) > self.measurementError): # larger than measurementError - decrease sample pool size
  37.             self.samples = self.samples[:-int(self.stillSampleMultiplier)]
  38.  
  39.         elif (len(self.samples) > self.sampleSize * self.stillSampleMultiplier): # remove oldest sample if sample pool size filled
  40.             self.samples.pop()
  41.  
  42.         if (self.filter == 'A'): filteredValue = mean(self.samples)
  43.         elif (self.filter == 'M'): filteredValue = median(self.samples)
  44.         else: filteredValue = self.samples[0] # no filtering - use last received value
  45.                  
  46.         if (self.axis == 'Yaw'): freeTrack.yaw = filteredValue
  47.         elif (self.axis == 'Pitch'): freeTrack.pitch  = filteredValue
  48.         elif (self.axis == 'Roll'): freeTrack.roll  = filteredValue
  49.  
  50. def update():
  51.     for orientation in orientations: orientation.update()
  52.     diagnostics.watch(freeTrack.yaw)
  53.     diagnostics.watch(len(orientations[0].samples))
  54.     diagnostics.watch(freeTrack.pitch)
  55.     diagnostics.watch(len(orientations[1].samples))
  56.     diagnostics.watch(freeTrack.roll)
  57.     diagnostics.watch(len(orientations[2].samples))
  58.  
  59.     #Multiply by 10 for SteamVR use - uncomment for psmovebridge or other positional tracking SW
  60.     #freeTrack.x = (freePieIO[0].x * 10)
  61.     #freeTrack.y = (freePieIO[0].y * 10)
  62.     #freeTrack.z = (freePieIO[0].z * 10)
  63.  
  64. if starting:
  65.     ##########################
  66.     # MODIFY ONLY VALUES BELOW
  67.     ##########################
  68.     deadband = 0.025
  69.     # PARAMS Orientation(Axis | Offset | Smooting filter 'A'verage, 'M'edian | Sample pool size | Sample pool size multiplier when not moving | Measurement Error toleration - "not moving")
  70.     yaw = Orientation('Yaw', 0,  'A', 200, 3, 0.2)
  71.     pitch = Orientation('Pitch', 1.5, 'A', 80, 1.2, 0.02)
  72.     roll = Orientation('Roll', 0, 'A', 90, 1.5, 0.02)
  73.     ##########################
  74.     # MODIFY ONLY VALUES ABOVE
  75.     ##########################
  76.     orientations = [yaw, pitch, roll]
  77.     freePieIO[0].update += update
Add Comment
Please, Sign In to add comment