def main(): parser = ArgumentParser(description='Configuration tool for Poppy robots ', formatter_class=ArgumentDefaultsHelpFormatter) parser.add_argument('robot', type=str, choices=installed_poppy_creatures, help='Robot used.') parser.add_argument('motor', type=str, help='Name of the motor to configure.') args = parser.parse_args() RobotCls = installed_poppy_creatures[args.robot] c = RobotCls.default_config if args.motor not in c['motors']: print('"{}" is not a motor of "{}"! ' 'possibilities={}'.format(args.motor, args.robot, sorted(c['motors'].keys()))) print('Exiting now...') sys.exit(1) motor_config = c['motors'][args.motor] args = [ '--id', motor_config['id'], '--type', motor_config['type'], '--port', find_port_for_motor(c, args.motor), '--return-delay-time', 0, '--angle-limit', motor_config['angle_limit'][0], motor_config['angle_limit'][1], '--goto-zero' ] call(['dxl-config'] + map(str, args))
def main(): robots = [c.replace('poppy-', '') for c in installed_poppy_creatures] parser = ArgumentParser(description='Configuration tool for Poppy robots ', formatter_class=ArgumentDefaultsHelpFormatter) parser.add_argument('robot', type=str, choices=robots, help='Robot used.') parser.add_argument('motor', type=str, help='Name of the motor to configure.') args = parser.parse_args() RobotCls = installed_poppy_creatures['poppy-{}'.format(args.robot)] c = RobotCls.default_config if args.motor not in c['motors']: print('"{}" is not a motor of "{}"! ' 'possibilities={}'.format(args.motor, args.robot, sorted(c['motors'].keys()))) print('Exiting now...') sys.exit(1) motor_config = c['motors'][args.motor] args = [ '--id', motor_config['id'], '--type', motor_config['type'], '--port', find_port_for_motor(c, args.motor), '--return-delay-time', 0 ] if 'wheel_mode' in motor_config.keys(): args.extend(('--wheel-mode', motor_config['wheel_mode'])) else: args.extend(('--angle-limit',motor_config['angle_limit'][0],motor_config['angle_limit'][1], '--goto-zero')) call(['dxl-config'] + map(str, args))
def start_poppy_with_services(args): params = poppy_params_from_args(args) for i in range(5): try: print('Attempt {} to start the robot...'.format(i + 1)) return installed_poppy_creatures[args.creature](**params) except Exception as e: # In case of failure, # Give the robot some time to statup, reboot... time.sleep(random.random()) print(e) else: print('Could not start up the robot...') sys.exit(1)
def main(): parser = argparse.ArgumentParser(description=( 'Poppy services launcher. Use it to quickly instantiate a ' + 'poppy creature with Snap!, an http server, or a remote robot.'), epilog=""" Examples: * poppy-services --snap poppy-torso * poppy-services --snap --vrep poppy-humanoid""", formatter_class=RawTextHelpFormatter) parser.add_argument('creature', type=str, help='poppy creature name', action='store', nargs='?', choices=installed_poppy_creatures.keys()) parser.add_argument('--dummy', help='use a simulated dummy robot', action='store_true') parser.add_argument('--vrep', help='use a V-REP simulated Poppy Creature', action='store_true') parser.add_argument( '--poppy-simu', help= 'start a simulated dummy robot and the HTTP API to connect to the viewer on simu.poppy-project.org', action='store_true') parser.add_argument('--snap', help='start a Snap! robot server', action='store_true') parser.add_argument('--snap-port', help='port used by the Snap! server', default=6969, type=int) parser.add_argument('-nb', '--no-browser', help='avoid automatic start of Snap! in web browser', action='store_true') parser.add_argument('--http', help='start a http robot server', action='store_true') parser.add_argument('--http-port', help='port of HttpRobotServer, used for poppy-simu', default=8080, type=int) parser.add_argument('--remote', help='start a remote robot server', action='store_true') parser.add_argument( '--zmq', help='start a remote robot server (but with faster ZMQ)', action='store_true') parser.add_argument('--ws', help='start the websocket server', action='store_true') parser.add_argument('--ws-port', help='port of Websocket Server', default=9009, type=int) parser.add_argument('--disable-camera', help='Start the robot without the camera.', action='store_true') parser.add_argument( '-v', '--verbose', help= 'start services with verbose mode. There is 3 debug levels, add as "v" as debug level you want', action='count') parser.add_argument('-kl', '--with-keylogger', help='start a background key logger for hit detection', action='store_true') parser.add_argument('-f', '--log-file', help='Log filename', action='store') nb_creatures = len(installed_poppy_creatures.keys()) if nb_creatures == 0: print('No installed poppy creature were found!') print('You should first install the python package ' 'corresponding to your robot or check your python environment.') sys.exit(1) args = parser.parse_args() # If no creature are specified and only one is installed # We use it as default. if args.creature is None: if nb_creatures > 1: parser.print_help() sys.exit(1) args.creature = installed_poppy_creatures.keys()[0] print('No creature specified, use {}'.format(args.creature)) if args.log_file: fh = logging.FileHandler(args.log_file) fh.setLevel(logging.DEBUG) formatter = logging.Formatter( '%(asctime)s - %(name)s - %(levelname)s - %(message)s') fh.setFormatter(formatter) logging.getLogger('').addHandler(fh) if args.verbose: args.snap_quiet = False args.http_quiet = False args.ws_quiet = False if args.verbose == 1: lvl = logging.WARNING elif args.verbose == 2: lvl = logging.INFO elif args.verbose > 2: lvl = logging.DEBUG if args.log_file is not None: ch = logging.FileHandler(args.log_file) else: ch = logging.StreamHandler() logging.basicConfig( level=lvl) #FIXME: this is still not working properly. # I.e. logging level isn't set properly across all files, because they are all initiated with # the wrong logger name (non-dependent, just filename). formatter = logging.Formatter( '%(name)-12s: %(levelname)-8s %(message)s') ch.setFormatter(formatter) logging.getLogger('').addHandler(ch) if not any([ args.snap, args.http, args.remote, args.zmq, args.poppy_simu, args.ws, args.dummy ]): print('No service specified! See --help for details.') sys.exit(1) if args.snap and not args.no_browser: snap_url = 'http://snap.berkeley.edu/snapsource/snap.html' block_url = 'http://{}:{}/snap-blocks.xml'.format( find_local_ip(), args.snap_port) url = '{}#open:{}'.format(snap_url, block_url) # Wait for the Snap server to be started before openning the Snap URL time.sleep(3) for browser_name in [ 'chromium-browser', 'chromium', 'google-chrome', 'chrome', 'safari', 'midori', None ]: try: browser = webbrowser.get(browser_name) browser.open(url, new=0, autoraise=True) break except Exception: pass with closing(start_poppy_with_services(args)): print('Robot created and running!') sys.stdout.flush() # Just run4ever (until Ctrl-c...) try: while (True): time.sleep(1000) except KeyboardInterrupt: print("Bye bye!") raise SystemExit
def main(): parser = argparse.ArgumentParser(description=( 'Poppy services launcher. Use it to quickly instantiate a ' + 'poppy creature with Snap!, an http server, or a remote robot.'), epilog=""" Examples: * poppy-services --snap poppy-torso * poppy-services --snap --vrep poppy-humanoid""", formatter_class=RawTextHelpFormatter) parser.add_argument('creature', type=str, help='poppy creature name', action='store', nargs='?', choices=list(installed_poppy_creatures.keys())) parser.add_argument('--dummy', help='use a simulated dummy robot', action='store_true') parser.add_argument('--vrep', help='use a V-REP simulated Poppy Creature', action='store_true') parser.add_argument( '--poppy-simu', help= 'start a simulated dummy robot and the HTTP API to connect to the viewer on simu.poppy-project.org', action='store_true') parser.add_argument('--snap', help='start a Snap! robot server', action='store_true') parser.add_argument('--snap-port', help='port used by the Snap! server', default=6969, type=int) parser.add_argument('-nb', '--no-browser', help='avoid automatic start of Snap! in web browser', action='store_true') parser.add_argument('--http', help='start a http robot server', action='store_true') parser.add_argument('--http-port', help='port of HttpRobotServer, used for poppy-simu', default=8080, type=int) parser.add_argument('--remote', help='start a remote robot server', action='store_true') parser.add_argument('--ws', help='start the websocket server', action='store_true') parser.add_argument('--ws-port', help='port of Websocket Server', default=9009, type=int) parser.add_argument('--disable-camera', help='Start the robot without the camera.', action='store_true') parser.add_argument( '-v', '--verbose', help= 'start services with verbose mode. There is 3 debug levels, add as "v" as debug level you want', action='count') parser.add_argument('-f', '--log-file', help='Log filename', action='store') nb_creatures = len(installed_poppy_creatures.keys()) if nb_creatures == 0: print('No installed poppy creature were found!') print('You should first install the python package ' 'corresponding to your robot or check your python environment.') sys.exit(1) args = parser.parse_args() # If no creature are specified and only one is installed # We use it as default. if args.creature is None: if nb_creatures > 1: parser.print_help() sys.exit(1) args.creature = list(installed_poppy_creatures.keys())[0] print('No creature specified, use {}'.format(args.creature)) if args.log_file: fh = logging.FileHandler(args.log_file) fh.setLevel(logging.DEBUG) formatter = logging.Formatter( '%(asctime)s - %(name)s - %(levelname)s - %(message)s') fh.setFormatter(formatter) logging.getLogger('').addHandler(fh) if args.verbose: args.snap_quiet = False args.http_quiet = False args.ws_quiet = False if args.verbose == 1: lvl = logging.WARNING elif args.verbose == 2: lvl = logging.INFO elif args.verbose > 2: lvl = logging.DEBUG if args.log_file is not None: ch = logging.FileHandler(args.log_file) else: ch = logging.StreamHandler() ch.setLevel(lvl) formatter = logging.Formatter( '%(name)-12s: %(levelname)-8s %(message)s') ch.setFormatter(formatter) logging.getLogger('').addHandler(ch) if not any([ args.snap, args.http, args.remote, args.poppy_simu, args.ws, args.dummy ]): print('No service specified! See --help for details.') sys.exit(1) static_server_started = False if args.snap and not args.no_browser: snap_static_port = 8888 snap_static_server = HTTPServer(("0.0.0.0", snap_static_port), SimpleHTTPRequestHandler) from pypot.vpl.snap import download_snap_interactively static_app = download_snap_interactively() if static_app is None: print( "The static server was not started because the VPL app has not been downloaded" ) else: os.chdir(static_app) snap_static_server_process = Process( target=snap_static_server.serve_forever, args=()) static_server_started = True snap_static_server_process.start() snap_url = 'http://127.0.0.1:{}/snap.html'.format(snap_static_port) block_url = 'http://{}:{}/snap-blocks.xml'.format( find_local_ip(), args.snap_port) url = '{}#open:{}'.format(snap_url, block_url) with closing(start_poppy_with_services(args)): msg = '' if args.dummy or args.poppy_simu: msg += 'Simulated robot created! He is running on: ip={}'.format( find_local_ip()) else: msg += 'Robot instantiated! He is running on: ip={},'.format( find_local_ip()) if args.disable_camera: msg += ' without camera access.' else: msg += ' with camera access.' if args.vrep: msg += ' With V-REP link.' if args.snap or args.ws or args.http or args.poppy_simu: msg += '\nServer started on:' if args.http or args.poppy_simu: msg += ' http_port={},'.format(args.http_port) if args.snap: msg += ' Snap_port={},'.format(args.snap_port) if args.ws: msg += ' ws_port={},'.format(args.ws_port) msg = msg[0:-1] + '.' print(msg) sys.stdout.flush() if static_server_started: for browser_name in [ 'chromium-browser', 'chromium', 'google-chrome', 'chrome', 'safari', 'midori', None ]: try: browser = webbrowser.get(browser_name) browser.open(url, new=0, autoraise=True) break except Exception: pass # Just run4ever (until Ctrl-c...) try: while (True): time.sleep(1000) except KeyboardInterrupt: print("Bye bye!") if static_server_started: snap_static_server_process.terminate() snap_static_server_process.join()
def main(): available_ports = get_available_ports() default_port = available_ports[0] if available_ports else None parser = ArgumentParser(description='Configuration tool for dynamixel motors ' 'WARNING: ONLY ONE MOTOR SHOULD BE ' 'CONNECTED TO THE BUS WHEN CONFIGURING!', formatter_class=ArgumentDefaultsHelpFormatter) parser.add_argument('--id', type=int, required=True, help='Chosen motor id.') parser.add_argument('--type', type=str, required=True, choices=dynamixelModels.values(), help='Type of the motor to configure.') parser.add_argument('--port', type=str, choices=available_ports, default=default_port, help='Serial port connected to the motor.') parser.add_argument('--return-delay-time', type=int, help='Set new return delay time.') parser.add_argument('--wheel-mode', type=bool, default=False, help='Set wheel mode.') parser.add_argument('--angle-limit', type=float, nargs=2, help='Set new angle limit.') parser.add_argument('--goto-zero', action='store_true', help='Go to zero position after configuring the motor') args = parser.parse_args() check(1 <= args.id <= 253, 'Motor id must be in range [1:253]') check(available_ports, 'Could not find an available serial port!') protocol = 2 if args.type in 'XL-320' else 1 DxlIOPort = DxlIO if protocol == 1 else Dxl320IO # Factory Reset print('Factory reset...') if protocol == 1: for br in [57600, 1000000]: with DxlIO(args.port, baudrate=br) as io: io.factory_reset() else: with Dxl320IO(args.port, baudrate=1000000, timeout=0.01) as io: io.factory_reset(ids=range(253)) print('Done!') factory_baudrate = 57600 if args.type.startswith('MX') else 1000000 # Wait for the motor to "reboot..." for _ in range(10): with DxlIOPort(args.port, baudrate=factory_baudrate) as io: if io.ping(1): break time.sleep(.5) else: print('Could not communicate with the motor...') print('Make sure one (and only one) is connected and try again') sys.exit(1) # Switch to 1M bauds if args.type.startswith('MX') or args.type.startswith('SR'): print('Changing to 1M bauds...') with DxlIO(args.port, baudrate=factory_baudrate) as io: io.change_baudrate({1: 1000000}) time.sleep(.5) print('Done!') # Change id print('Changing id to {}...'.format(args.id)) if args.id != 1: with DxlIOPort(args.port) as io: io.change_id({1: args.id}) time.sleep(.5) check(io.ping(args.id), 'Could not change id to {}'.format(args.id)) print('Done!') # Set return delay time if args.return_delay_time is not None: print('Changing return delay time to {}...'.format(args.return_delay_time)) with DxlIOPort(args.port) as io: io.set_return_delay_time({args.id: args.return_delay_time}) time.sleep(.5) check(io.get_return_delay_time([args.id])[0] == args.return_delay_time, 'Could not set return delay time to {}'.format(args.return_delay_time)) print('Done!') # Set wheel Mode if args.wheel_mode == True: print('Set wheel mode') with DxlIOPort(args.port) as io: io.set_control_mode({args.id :'wheel'}) time.sleep(.5) check(io.get_control_mode([args.id])[0] == 'wheel', 'Could not set wheel Mode') print('Done!') # Set Angle Limit if args.angle_limit is not None: print('Changing angle limit to {}...'.format(args.angle_limit)) with DxlIOPort(args.port) as io: io.set_angle_limit({args.id: args.angle_limit}) time.sleep(.5) check(all(map(lambda p1, p2: abs(p1 - p2) < 1., io.get_angle_limit([args.id])[0], args.angle_limit)), 'Could not change angle limit to {}'.format(args.angle_limit)) print('Done!') # GOTO ZERO if args.goto_zero: print('Going to position 0...') with DxlIOPort(args.port) as io: io.set_moving_speed({args.id: 100.0}) io.set_goal_position({args.id: 0.0}) time.sleep(2.0) check(abs(io.get_present_position([args.id])[0]) < 5, 'Could not go to 0 position') print('Done!')
def check(pred, msg): if not pred: print(msg) print('Exiting now...') sys.exit(1)
def main(): parser = argparse.ArgumentParser( description=('Poppy services launcher. Use it to quickly instantiate a ' + 'poppy creature with Snap!, an http server, or a remote robot.'), epilog=""" Examples: * poppy-services --snap poppy-torso * poppy-services --snap --vrep poppy-humanoid""", formatter_class=RawTextHelpFormatter) parser.add_argument('creature', type=str, help='poppy creature name', action='store', nargs='?', choices=installed_poppy_creatures.keys()) parser.add_argument('--dummy', help='use a simulated dummy robot', action='store_true') parser.add_argument('--vrep', help='use a V-REP simulated Poppy Creature', action='store_true') parser.add_argument('--poppy-simu', help='start a simulated dummy robot and the HTTP API to connect to the viewer on simu.poppy-project.org', action='store_true') parser.add_argument('--snap', help='start a Snap! robot server', action='store_true') parser.add_argument('--snap-port', help='port used by the Snap! server', default=6969, type=int) parser.add_argument('-nb', '--no-browser', help='avoid automatic start of Snap! in web browser', action='store_true') parser.add_argument('--http', help='start a http robot server', action='store_true') parser.add_argument('--http-port', help='port of HttpRobotServer, used for poppy-simu', default=8080, type=int) parser.add_argument('--remote', help='start a remote robot server', action='store_true') parser.add_argument('--ws', help='start the websocket server', action='store_true') parser.add_argument('--ws-port', help='port of Websocket Server', default=9009, type=int) parser.add_argument('--disable-camera', help='Start the robot without the camera.', action='store_true') parser.add_argument('-v', '--verbose', help='start services with verbose mode. There is 3 debug levels, add as "v" as debug level you want', action='count') parser.add_argument('-f', '--log-file', help='Log filename', action='store') nb_creatures = len(installed_poppy_creatures.keys()) if nb_creatures == 0: print('No installed poppy creature were found!') print('You should first install the python package ' 'corresponding to your robot or check your python environment.') sys.exit(1) args = parser.parse_args() # If no creature are specified and only one is installed # We use it as default. if args.creature is None: if nb_creatures > 1: parser.print_help() sys.exit(1) args.creature = installed_poppy_creatures.keys()[0] print('No creature specified, use {}'.format(args.creature)) if args.log_file: fh = logging.FileHandler(args.log_file) fh.setLevel(logging.DEBUG) formatter = logging.Formatter( '%(asctime)s - %(name)s - %(levelname)s - %(message)s') fh.setFormatter(formatter) logging.getLogger('').addHandler(fh) if args.verbose: args.snap_quiet = False args.http_quiet = False args.ws_quiet = False if args.verbose == 1: lvl = logging.WARNING elif args.verbose == 2: lvl = logging.INFO elif args.verbose > 2: lvl = logging.DEBUG if args.log_file is not None: ch = logging.FileHandler(args.log_file) else: ch = logging.StreamHandler() ch.setLevel(lvl) formatter = logging.Formatter( '%(name)-12s: %(levelname)-8s %(message)s') ch.setFormatter(formatter) logging.getLogger('').addHandler(ch) if not any([args.snap, args.http, args.remote, args.poppy_simu, args.ws, args.dummy]): print('No service specified! See --help for details.') sys.exit(1) if args.snap and not args.no_browser: snap_url = 'http://snap.berkeley.edu/snapsource/snap.html' block_url = 'http://{}:{}/snap-blocks.xml'.format( find_local_ip(), args.snap_port) url = '{}#open:{}'.format(snap_url, block_url) # Wait for the Snap server to be started before openning the Snap URL time.sleep(3) for browser_name in ['chromium-browser', 'chromium', 'google-chrome', 'chrome', 'safari', 'midori', None]: try: browser = webbrowser.get(browser_name) browser.open(url, new=0, autoraise=True) break except Exception: pass with closing(start_poppy_with_services(args)): print('Robot created and running!') sys.stdout.flush() # Just run4ever (until Ctrl-c...) try: while(True): time.sleep(1000) except KeyboardInterrupt: print("Bye bye!")