Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add scripts to scan IDs and switch servo-on and servo-off #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions dynamixel_driver/scripts/reset_torque.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#!/usr/bin/python

import sys
from optparse import OptionParser

import roslib
roslib.load_manifest('dynamixel_driver')

from dynamixel_driver import dynamixel_io

if __name__ == '__main__':
usage_msg = 'Usage: %prog [options] ID [On|Off]'
desc_msg = 'Turns the torque of specified Dynamixel servo motor on or off.'
epi_msg = 'Example: %s --port=/dev/ttyUSB1 --baud=57600 1 Off' % sys.argv[0]

parser = OptionParser(usage=usage_msg, description=desc_msg, epilog=epi_msg)
parser.add_option('-p', '--port', metavar='PORT', default='/dev/ttyUSB0',
help='motors of specified controllers are connected to PORT [default: %default]')
parser.add_option('-b', '--baud', metavar='BAUD', type="int", default=1000000,
help='connection to serial port will be established at BAUD bps [default: %default]')

(options, args) = parser.parse_args(sys.argv)

if len(args) < 3:
parser.print_help()
exit(1)

port = options.port
baudrate = options.baud
motor_id = int(args[1])
torque_on = args[2]

try:
dxl_io = dynamixel_io.DynamixelIO(port, baudrate)
except dynamixel_io.SerialOpenError, soe:
print 'ERROR:', soe
else:
print 'Turning torque %s for motor %d' % (torque_on, motor_id)
ret = dxl_io.ping(motor_id)
if ret:
from dynamixel_driver import dynamixel_const
if torque_on.lower() == 'off':
torque_on = False
elif torque_on.lower() == 'on':
if not ret[4] & dynamixel_const.DXL_OVERHEATING_ERROR == 0:
dxl_io.set_torque_limit(motor_id, 1023)
print "OVERHEATING -> Reset Torque"
if not ret[4] & dynamixel_const.DXL_OVERLOAD_ERROR == 0:
dxl_io.set_torque_limit(motor_id, 1023)
print "OVERLOAD -> Reset Torque"
torque_on = True
else:
parser.print_help()
exit(1)
ret = dxl_io.set_torque_enabled(motor_id, torque_on)
if not ret[4] & dynamixel_const.DXL_OVERHEATING_ERROR == 0:
print "OVERHEATING"
exit (1)
if not ret[4] & dynamixel_const.DXL_OVERLOAD_ERROR == 0:
print "OVERLOAD"
exit (1)
print "done."
else:
print 'ERROR: motor %d did not respond. Make sure to specify the correct baudrate.' % motor_id
exit (1)

42 changes: 42 additions & 0 deletions dynamixel_driver/scripts/scan_ids.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys
from optparse import OptionParser

import roslib
roslib.load_manifest('dynamixel_driver')

from dynamixel_driver import dynamixel_io

if __name__ == '__main__':
parser = OptionParser(usage='Usage: %prog [options]', description='Changes the unique ID of a Dynamixel servo motor.')
parser.add_option('-p', '--port', metavar='PORT', default='/dev/ttyUSB0',
help='motors of specified controllers are connected to PORT [default: %default]')
parser.add_option('-b', '--baud', metavar='BAUD', type="int", default=1000000,
help='connection to serial port will be established at BAUD bps [default: %default]')
parser.add_option('-f', '--from-id', metavar='FROM_ID', type="int", default=1,
help='from id [default: %default]')
parser.add_option('-t', '--to-id', metavar='TO_ID', type="int", default=7,
help='to id [default: %default]')

(options, args) = parser.parse_args(sys.argv)

if len(args) < 1:
parser.print_help()
exit(1)

port = options.port
baudrate = options.baud

try:
dxl_io = dynamixel_io.DynamixelIO(port, baudrate)
except dynamixel_io.SerialOpenError, soe:
print 'ERROR:', soe
else:
for idx in map(lambda x : x + options.from_id , range(options.to_id - options.from_id + 1)):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just use: for idx in range(options.from_id, options.to_id + 1)?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just use: for idx in range(options.from_id, options.to_id + 1)?
It would be better to use range(options.from_id, options.to_id + 1).
(I did not know the functionality of range. )

print 'Scanning %d...' %(idx),
if dxl_io.ping(idx):
print 'The motor %d respond to a ping' %(idx)
else:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We probably shouldn't call it an error when the motor with certain id is not there, since the purpose of this script is to figure what is connected. I would just keep track of how many motors we found and if there's none display the message that there were no motors found in specified range, otherwise display what motor ids are connected.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, Arebgun.

We probably shouldn't call it an error when the motor with certain id is not there, since the purpose of this script is to figure what is connected.
What kind of error did you mention?

In my case, the function
if dxl_io.ping(idx):
returns false if the motor with certain id is not there and
the program continue to other loop.

When I executed the script by connecting a dynamixel motor which ID is 1,
I obtained these outputs:

$ rosrun dynamixel_driver scan_ids.py -p /dev/ttyUSB0
Scanning 1... The motor 1 respond to a ping
Scanning 2... ERROR: The specified motor did not respond to id 2.
Scanning 3... ERROR: The specified motor did not respond to id 3.
Scanning 4... ERROR: The specified motor did not respond to id 4.
Scanning 5... ERROR: The specified motor did not respond to id 5.
Scanning 6... ERROR: The specified motor did not respond to id 6.
Scanning 7... ERROR: The specified motor did not respond to id 7.

print 'ERROR: The specified motor did not respond to id %d.' % idx