Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def median(lst):
- n = len(lst)
- return (sorted(lst)[n//2] if (n % 2 == 1) else sum(sorted(lst)[n//2-1:n//2+1])/2.0)
- def mean(lst): return round(sum(lst)/len(lst), 3)
- def difference(a, b): return (b - a if (a <= b) else (a - b) * 2)
- class Orientation:
- def __init__(self, axis, offset = 0, filter = 'A', sampleSize = 80, stillSampleMultiplier = 2.5, measurementError = 0.2):
- self.samples = []
- self.axis = axis
- self.offset = offset
- self.filter = filter
- self.sampleSize = sampleSize
- self.measurementError = measurementError
- self.stillSampleMultiplier = stillSampleMultiplier
- def getPhoneValue(self):
- if (self.axis == 'Yaw'): return round(-filters.deadband(android[0].googleYaw, deadband), 3)
- elif (self.axis == 'Pitch'): return filters.deadband(android[0].googleRoll, deadband)
- elif (self.axis == 'Roll'): return filters.deadband(android[0].googlePitch, deadband)
- def update(self):
- if (self.axis == 'Yaw'): lastValue = freeTrack.yaw
- elif (self.axis == 'Pitch'): lastValue = freeTrack.pitch
- elif (self.axis == 'Roll'): lastValue = freeTrack.roll
- nowValue = self.getPhoneValue();
- if (difference(lastValue, (nowValue + self.offset)) > 5): # 360° turn - reset sample pool
- self.samples = []
- self.samples.insert(0, nowValue + self.offset) # new sample
- if (len(self.samples) > self.sampleSize and difference(lastValue, (nowValue + self.offset)) > self.measurementError): # larger than measurementError - decrease sample pool size
- self.samples = self.samples[:-int(self.stillSampleMultiplier)]
- elif (len(self.samples) > self.sampleSize * self.stillSampleMultiplier): # remove oldest sample if sample pool size filled
- self.samples.pop()
- if (self.filter == 'A'): filteredValue = mean(self.samples)
- elif (self.filter == 'M'): filteredValue = median(self.samples)
- else: filteredValue = self.samples[0] # no filtering - use last received value
- if (self.axis == 'Yaw'): freeTrack.yaw = filteredValue
- elif (self.axis == 'Pitch'): freeTrack.pitch = filteredValue
- elif (self.axis == 'Roll'): freeTrack.roll = filteredValue
- def update():
- for orientation in orientations: orientation.update()
- diagnostics.watch(freeTrack.yaw)
- diagnostics.watch(len(orientations[0].samples))
- diagnostics.watch(freeTrack.pitch)
- diagnostics.watch(len(orientations[1].samples))
- diagnostics.watch(freeTrack.roll)
- diagnostics.watch(len(orientations[2].samples))
- #Multiply by 10 for SteamVR use - uncomment for psmovebridge or other positional tracking SW
- #freeTrack.x = (freePieIO[0].x * 10)
- #freeTrack.y = (freePieIO[0].y * 10)
- #freeTrack.z = (freePieIO[0].z * 10)
- if starting:
- ##########################
- # MODIFY ONLY VALUES BELOW
- ##########################
- deadband = 0.025
- # 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")
- yaw = Orientation('Yaw', 0, 'A', 200, 3, 0.2)
- pitch = Orientation('Pitch', 1.5, 'A', 80, 1.2, 0.02)
- roll = Orientation('Roll', 0, 'A', 90, 1.5, 0.02)
- ##########################
- # MODIFY ONLY VALUES ABOVE
- ##########################
- orientations = [yaw, pitch, roll]
- freePieIO[0].update += update
Add Comment
Please, Sign In to add comment