Skip to content

Commit

Permalink
basic throw counter; audio with flite
Browse files Browse the repository at this point in the history
  • Loading branch information
Alon committed Feb 26, 2010
1 parent 799d2bc commit 278f9a9
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 1 deletion.
8 changes: 7 additions & 1 deletion pc/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,17 @@ def accelerations(self):
# (float float float) seen
pass

def main():
def stationary_factory():
""" reads command line parameters and determines the device, returns a Stationary instance """
device_file = '/dev/ttyACM0'
if len(sys.argv) > 1:
device_file = sys.argv[-1]
print "reading accelerations from device %s" % device_file
stationary = Stationary(device_file)
return stationary

def main():
stationary = stationary_factory()
# wait for something
# to make sure this stops at an end of line, so we use s.readline() from now
for acc_X, acc_Y, acc_Z in stationary.accelerations():
Expand Down
1 change: 1 addition & 0 deletions pc/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ def listenToTCPAndSerial(device):
reactor.listenTCP(JUGGLING_SERVER_PORT, teefactory)
return protocol


if __name__ == '__main__':
device = '/dev/ttyACM0' if len(sys.argv) == 1 or not os.path.exists(sys.argv[-1]) else sys.argv[-1]
print "Reading stationary from %s" % device
Expand Down
30 changes: 30 additions & 0 deletions pc/sp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""
signal processing. Determine what is happening to the ball from
a stream of accelerations. The code here is generator based, i.e. event
based.
"""

def sum_squares_gen(v3_gen, verbose=False):
for x, y, z in v3_gen:
sq = x**2 + y**2 + z**2
if verbose:
print sq
yield sq

def throws(accels, initial = 0, throw_max_accel=0.1, land_min_accel=0.5, verbose=False):
""" generator, yields (<throw num>, 1) when thrown, and (<throw num>, 0)
when landed. Throw is detected
"""
i = initial
sq_gen = sum_squares_gen(accels, verbose=verbose)
while True:
for a in sq_gen:
if a < throw_max_accel:
i += 1
yield i, 1
break
for a in sq_gen:
if a > land_min_accel:
yield i, 0
break

16 changes: 16 additions & 0 deletions pc/speaker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/python

import os
from reader import stationary_factory
from sp import throws

def main():
stationary = stationary_factory()
for throw_num, thrown in throws(stationary.accelerations()):
print throw_num, thrown
if thrown:
os.system("flite -t %s &" % throw_num)

if __name__ == '__main__':
main()

0 comments on commit 278f9a9

Please sign in to comment.