37 __author__ =
'Antons Rebguns' 38 __copyright__ =
'Copyright (c) 2010-2011 Antons Rebguns' 41 __maintainer__ =
'Antons Rebguns' 42 __email__ =
'anton@email.arizona.edu' 46 from optparse
import OptionParser
49 roslib.load_manifest(
'dynamixel_driver')
51 from dynamixel_driver
import dynamixel_io
53 if __name__ ==
'__main__':
54 usage_msg =
'Usage: %prog [options] MOTOR_IDs' 55 desc_msg =
'Sets various configuration options of specified Dynamixel servo motor.' 56 epi_msg =
'Example: %s --port=/dev/ttyUSB1 --baud=57600 --baud-rate=1 --return-delay=1 5 9 23' % sys.argv[0]
58 parser = OptionParser(usage=usage_msg, description=desc_msg, epilog=epi_msg)
59 parser.add_option(
'-p',
'--port', metavar=
'PORT', default=
'/dev/ttyUSB0',
60 help=
'motors of specified controllers are connected to PORT [default: %default]')
61 parser.add_option(
'-b',
'--baud', metavar=
'BAUD', type=
'int', default=1000000,
62 help=
'connection to serial port will be established at BAUD bps [default: %default]')
63 parser.add_option(
'-r',
'--baud-rate', type=
'int', metavar=
'RATE', dest=
'baud_rate',
64 help=
'set servo motor communication speed')
65 parser.add_option(
'-d',
'--return-delay', type=
'int', metavar=
'DELAY', dest=
'return_delay',
66 help=
'set servo motor return packet delay time')
67 parser.add_option(
'--cw-angle-limit', type=
'int', metavar=
'CW_ANGLE', dest=
'cw_angle_limit',
68 help=
'set servo motor CW angle limit')
69 parser.add_option(
'--ccw-angle-limit', type=
'int', metavar=
'CCW_ANGLE', dest=
'ccw_angle_limit',
70 help=
'set servo motor CCW angle limit')
71 parser.add_option(
'--min-voltage-limit', type=
'int', metavar=
'MIN_VOLTAGE', dest=
'min_voltage_limit',
72 help=
'set servo motor minimum voltage limit')
73 parser.add_option(
'--max-voltage-limit', type=
'int', metavar=
'MAX_VOLTAGE', dest=
'max_voltage_limit',
74 help=
'set servo motor maximum voltage limit')
76 (options, args) = parser.parse_args(sys.argv)
84 baudrate = options.baud
88 dxl_io = dynamixel_io.DynamixelIO(port, baudrate)
89 except dynamixel_io.SerialOpenError, soe:
92 for motor_id
in motor_ids:
93 motor_id = int(motor_id)
94 print 'Configuring Dynamixel motor with ID %d' % motor_id
95 if dxl_io.ping(motor_id):
98 valid_rates = (1,3,4,7,9,16,34,103,207,250,251,252)
100 if options.baud_rate
not in valid_rates:
101 print 'Requested baud rate is invalid, please use one of the following: %s' % str(valid_rates)
103 if options.baud_rate <= 207:
104 print 'Setting baud rate to %d bps' % int(2000000.0/(options.baud_rate + 1))
105 elif options.baud_rate == 250:
106 print 'Setting baud rate to %d bps' % 2250000
107 elif options.baud_rate == 251:
108 print 'Setting baud rate to %d bps' % 2500000
109 elif options.baud_rate == 252:
110 print 'Setting baud rate to %d bps' % 3000000
112 dxl_io.set_baud_rate(motor_id, options.baud_rate)
115 if options.return_delay
is not None:
116 if options.return_delay < 0
or options.return_delay > 254:
117 print 'Requested return delay time is out of valid range (0 - 254)' 119 print 'Setting return delay time to %d us' % (options.return_delay * 2)
120 dxl_io.set_return_delay_time(motor_id, options.return_delay)
123 if options.cw_angle_limit
is not None:
124 print 'Setting CW angle limit to %d' % options.cw_angle_limit
125 dxl_io.set_angle_limit_cw(motor_id, options.cw_angle_limit)
128 if options.ccw_angle_limit
is not None:
129 print 'Setting CCW angle limit to %d' % options.ccw_angle_limit
130 dxl_io.set_angle_limit_ccw(motor_id, options.ccw_angle_limit)
133 if options.min_voltage_limit:
134 print 'Setting minimum voltage limit to %d' % options.min_voltage_limit
135 dxl_io.set_voltage_limit_min(motor_id, options.min_voltage_limit)
138 if options.max_voltage_limit:
139 print 'Setting maximum voltage limit to %d' % options.max_voltage_limit
140 dxl_io.set_voltage_limit_max(motor_id, options.max_voltage_limit)
144 print 'Unable to connect to Dynamixel motor with ID %d' % motor_id