Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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'))
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)
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})
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.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)
os.chdir(os.path.join(os.path.dirname(__file__), "..", "snap"))
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)
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 check(pred, msg):
if not pred:
print(msg)
print('Exiting now...')
sys.exit(1)
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
def check(pred, msg):
if not pred:
print(msg)
print('Exiting now...')
sys.exit(1)