set_servo_config.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 #
4 # Software License Agreement (BSD License)
5 #
6 # Copyright (c) 2010-2011, Antons Rebguns.
7 # All rights reserved.
8 #
9 # Redistribution and use in source and binary forms, with or without
10 # modification, are permitted provided that the following conditions
11 # are met:
12 #
13 # * Redistributions of source code must retain the above copyright
14 # notice, this list of conditions and the following disclaimer.
15 # * Redistributions in binary form must reproduce the above
16 # copyright notice, this list of conditions and the following
17 # disclaimer in the documentation and/or other materials provided
18 # with the distribution.
19 # * Neither the name of University of Arizona nor the names of its
20 # contributors may be used to endorse or promote products derived
21 # from this software without specific prior written permission.
22 #
23 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 # POSSIBILITY OF SUCH DAMAGE.
35 
36 
37 __author__ = 'Antons Rebguns'
38 __copyright__ = 'Copyright (c) 2010-2011 Antons Rebguns'
39 
40 __license__ = 'BSD'
41 __maintainer__ = 'Antons Rebguns'
42 __email__ = 'anton@email.arizona.edu'
43 
44 
45 import sys
46 from optparse import OptionParser
47 
48 import roslib
49 roslib.load_manifest('dynamixel_driver')
50 
51 from dynamixel_driver import dynamixel_io
52 
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]
57 
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')
75 
76  (options, args) = parser.parse_args(sys.argv)
77  print options
78 
79  if len(args) < 2:
80  parser.print_help()
81  exit(1)
82 
83  port = options.port
84  baudrate = options.baud
85  motor_ids = args[1:]
86 
87  try:
88  dxl_io = dynamixel_io.DynamixelIO(port, baudrate)
89  except dynamixel_io.SerialOpenError, soe:
90  print 'ERROR:', soe
91  else:
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):
96  # check if baud rate needs to be changed
97  if options.baud_rate:
98  valid_rates = (1,3,4,7,9,16,34,103,207,250,251,252)
99 
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)
102 
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
111 
112  dxl_io.set_baud_rate(motor_id, options.baud_rate)
113 
114  # check if return delay time needs to be changed
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)'
118  else:
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)
121 
122  # check if CW angle limit needs to be changed
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)
126 
127  # check if CCW angle limit needs to be changed
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)
131 
132  # check if minimum voltage limit needs to be changed
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)
136 
137  # check if maximum voltage limit needs to be changed
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)
141 
142  print 'done'
143  else:
144  print 'Unable to connect to Dynamixel motor with ID %d' % motor_id


dynamixel_driver
Author(s): Antons Rebguns, Cody Jorgensen
autogenerated on Tue May 12 2020 03:10:57