configure_devices.py
Go to the documentation of this file.
00001 #! /usr/bin/env python
00002 
00003 import os
00004 import sys
00005 import fcntl
00006 import select
00007 import time
00008 from glob import glob
00009 import collections
00010 import yaml
00011 
00012 import roslib
00013 
00014 class DeviceNotDetected(Exception): pass
00015 
00016 def open_nb(name, mode = 'r'):
00017     f = open(name, mode, 0)
00018     fcntl.fcntl(f, fcntl.F_SETFL, os.O_NONBLOCK)
00019     return f
00020 
00021 def get_hidraw():
00022     return glob('/dev/hidraw*')
00023 def get_js():
00024     return glob('/dev/input/js*')
00025 
00026 
00027 def what_moved(paths, timeout = 1.0):
00028     fs = [open_nb(p, 'rb') for p in paths]
00029     started = time.time()
00030     moved = collections.defaultdict(int)
00031     while started + timeout >= time.time():
00032         ready, _, _ = select.select(fs, [], [], 0.25)
00033         for f in ready:
00034             buf = f.read(10000)
00035             moved[f.name] += len(buf)
00036         time.sleep(0.001)
00037     #print "## Moved:", moved
00038     sys.stdout.flush()
00039     return dict(moved)
00040 
00041 
00042 def find_device(description, candidates, timeout = -1):
00043     print "Getting a baseline..."
00044     sys.stdout.flush()
00045     baseline = what_moved(candidates)
00046     print "Ok\n"
00047     print "Start pushing the %s" % description
00048     sys.stdout.flush()
00049     started = time.time()
00050     while timeout < 0 or time.time() < started + timeout:
00051         moved = what_moved(candidates, 0.2)
00052 
00053         began = []
00054         increased = []
00055         for m in moved:
00056             if m not in baseline:
00057                 began.append(m)
00058             elif moved[m] > 3 * baseline[m]:
00059                 increased.append(m)
00060                 
00061         #print "## Began: %s" % began
00062         #print "## Increased: %s" % increased
00063         
00064         if len(began) > 1:
00065             raise DeviceNotDetected("Devices noticed were: %s" % ', '.join(began))
00066         elif len(began) == 1:
00067             print "Found: %s" % began[0]
00068             time.sleep(1.0)
00069             return began[0]
00070         elif len(increased) == 1:
00071             print "Found: %s" % increased[0]
00072             time.sleep(1.0)
00073             return increased[0]
00074     raise DeviceNotDetected("No devices noticed")
00075 
00076 def find_device_robust(description, candidates):
00077     while True:
00078         try:
00079             return find_device(description, candidates, 5.0)
00080         except DeviceNotDetected, ex:
00081             print "Failed:", ex
00082             print "."
00083             time.sleep(1)
00084             print "Trying again"
00085 
00086 os.system("sudo chmod a+rwx /dev/hidraw*")
00087 os.system("sudo chmod a+rwx /dev/input/js*")
00088 os.system("sudo chmod a+rwx -R /dev/bus/usb/*")
00089 
00090 y = {
00091     'l_pedals': {'device': find_device_robust('left transcription pedals', get_hidraw())},
00092     'r_pedals': {'device': find_device_robust('right transcription pedals', get_hidraw())},
00093     'l_rudder_pedals': {'dev': find_device_robust('left rudder pedals', get_js())},
00094     'r_rudder_pedals': {'dev': find_device_robust('right rudder pedals', get_js())},
00095 }
00096 
00097 
00098 filename = os.path.join(roslib.packages.get_pkg_dir('teleop_microscribe'), 'devices.yaml')
00099 with open(filename, 'w') as fout:
00100     yaml.dump(y, fout)
00101     print "Written to: %s" % filename


teleop_microscribe
Author(s): Stuart Glaser
autogenerated on Sat Dec 28 2013 17:24:30