def find_any(path=default_search_path, serial_number=None, search_cancellation_token=None, channel_termination_token=None, timeout=None, logger=fibre.Logger(verbose=False)): """ Blocks until the first matching ODrive object is connected and then returns that object """ result = [] done_signal = fibre.Event(search_cancellation_token) channel_termination_token = fibre.Event(channel_termination_token) async def discovered_object(obj): if not (serial_number is None) and ( (await get_serial_number_str(obj)) != serial_number): return # ignore this device obj._on_lost.add_done_callback( lambda x: channel_termination_token.set()) result.append(obj) done_signal.set() def domain_thread(): with fibre.Domain(path) as domain: discovery = domain.run_discovery(discovered_object) channel_termination_token.wait() discovery.stop() threading.Thread(target=domain_thread).start() try: done_signal.wait(timeout=timeout) except: channel_termination_token.set() raise return result[0] if len(result) > 0 else None
def _start_discovery(path): _domain_termination_token = fibre.Event() async def discovered_object(obj): def lost_object(_): idx = [i for i, (o, _) in enumerate(_objects) if o == obj][0] _objects.pop(idx) _objects.append((obj, await get_serial_number_str(obj))) obj._on_lost.add_done_callback(lost_object) with _discovery_signal: _discovery_signal.notify_all() def domain_thread(): with fibre.Domain(path) as domain: discovery = domain.run_discovery(discovered_object) _domain_termination_token.wait() discovery.stop() threading.Thread(target=domain_thread, daemon=True).start()
print( "You must either provide a --test-rig-yaml argument or set the environment variable ODRIVE_TEST_RIG_NAME." ) sys.exit(1) path = os.path.join( os.path.dirname( os.path.dirname(os.path.dirname(os.path.realpath(__file__)))), test_rig_name + '.yaml') args.test_rig_yaml = open(path, 'r') # Load objects test_rig_yaml = yaml.load(args.test_rig_yaml, Loader=yaml.BaseLoader) logger = Logger() testrig = TestRig(test_rig_yaml, logger) shutdown_token = fibre.Event() if args.setup_host: for gpio in testrig.get_components(LinuxGpioComponent): num = gpio.num logger.debug('exporting GPIO ' + str(num) + ' to user space...') if not os.path.isdir("/sys/class/gpio/gpio{}".format(num)): with open("/sys/class/gpio/export", "w") as fp: fp.write(str(num)) os.chmod("/sys/class/gpio/gpio{}/value".format(num), stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO) os.chmod("/sys/class/gpio/gpio{}/direction".format(num), stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO) for port in testrig.get_components(SerialPortComponent): logger.debug('changing permissions on ' + port.yaml['port'] + '...')
print("fcn call failed") if __name__ == "__main__": print("args from python: " + str(sys.argv[1:0])) #print(sys.argv[1:]) # try to import based on command line arguments or config file for optPath in sys.argv[1:]: print("adding " + str(optPath.rstrip()) + " to import path for odrive_server.py") sys.path.insert(0, optPath.rstrip()) import odrive import odrive.utils # for dump_errors() import fibre # global for holding references to all connected odrives globals()['odrives'] = {} # global dict {'odriveX': True/False} where True/False reflects status of connection # on handle_disconnect, set it to False. On connection, set it to True globals()['odrives_status'] = {} globals()['discovered_devices'] = [] # spinlock globals()['inUse'] = False log = fibre.Logger(verbose=False) shutdown = fibre.Event() socketio.run(app, host='0.0.0.0', port=5000)
def main(): app_shutdown_token = fibre.Event() odrv = odrive.find_any() print("serial: {}".format(odrv.serial_number)) config_axis(odrv, odrv.axis1 )