-
Notifications
You must be signed in to change notification settings - Fork 169
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 joint_state_publisher plugin #27
Open
k-okada
wants to merge
1
commit into
arebgun:master
Choose a base branch
from
k-okada:add_joint_state_publisher
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
116 changes: 116 additions & 0 deletions
116
dynamixel_controllers/src/dynamixel_controllers/joint_state_publisher.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Software License Agreement (BSD License) | ||
# | ||
# Copyright (c) 2015, Kei Okada. | ||
# All rights reserved. | ||
# | ||
# Redistribution and use in source and binary forms, with or without | ||
# modification, are permitted provided that the following conditions | ||
# are met: | ||
# | ||
# * Redistributions of source code must retain the above copyright | ||
# notice, this list of conditions and the following disclaimer. | ||
# * Redistributions in binary form must reproduce the above | ||
# copyright notice, this list of conditions and the following | ||
# disclaimer in the documentation and/or other materials provided | ||
# with the distribution. | ||
# * Neither the name of Kei Okada nor the names of its | ||
# contributors may be used to endorse or promote products derived | ||
# from this software without specific prior written permission. | ||
# | ||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | ||
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | ||
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | ||
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, | ||
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN | ||
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
# POSSIBILITY OF SUCH DAMAGE. | ||
|
||
from threading import Thread | ||
|
||
import rospy | ||
|
||
from sensor_msgs.msg import JointState | ||
|
||
class JointStatePublisher(): | ||
def __init__(self, controller_namespace, controllers): | ||
self.update_rate = 1000 | ||
self.state_update_rate = 50 | ||
self.trajectory = [] | ||
|
||
self.controller_namespace = controller_namespace | ||
self.joint_names = [c.joint_name for c in controllers] | ||
|
||
self.joint_to_controller = {} | ||
for c in controllers: | ||
self.joint_to_controller[c.joint_name] = c | ||
|
||
self.port_to_joints = {} | ||
for c in controllers: | ||
if c.port_namespace not in self.port_to_joints: self.port_to_joints[c.port_namespace] = [] | ||
self.port_to_joints[c.port_namespace].append(c.joint_name) | ||
|
||
self.port_to_io = {} | ||
for c in controllers: | ||
if c.port_namespace in self.port_to_io: continue | ||
self.port_to_io[c.port_namespace] = c.dxl_io | ||
|
||
def initialize(self): | ||
self.msg = JointState() | ||
return True | ||
|
||
|
||
def start(self): | ||
self.running = True | ||
self.state_pub = rospy.Publisher('joint_states', JointState , queue_size=100) | ||
|
||
Thread(target=self.update_state).start() | ||
|
||
|
||
def stop(self): | ||
self.running = False | ||
|
||
def update_state(self): | ||
rate = rospy.Rate(self.state_update_rate) | ||
while self.running and not rospy.is_shutdown(): | ||
self.msg.header.stamp = rospy.Time.now() | ||
self.msg.name = [] | ||
self.msg.position = [] | ||
self.msg.velocity = [] | ||
self.msg.effort = [] | ||
|
||
for port, joints in self.port_to_joints.items(): | ||
vals = [] | ||
for joint in joints: | ||
j = self.joint_names.index(joint) | ||
|
||
motor_id = self.joint_to_controller[joint].motor_id | ||
co = self.joint_to_controller[joint] | ||
io = self.port_to_io[port] | ||
|
||
self.msg.name.append(joint) | ||
po = ve = ef = 0 | ||
try: | ||
ret = io.get_feedback(motor_id) | ||
po = self.raw_to_rad_pos(ret['position'],co) | ||
ve = self.raw_to_rad_spd(ret['speed'],co) | ||
ef = self.raw_to_rad_spd(ret['load'],co) | ||
except Exception as e: | ||
rospy.logerr(e) | ||
self.msg.position.append(po) | ||
self.msg.velocity.append(ve) | ||
self.msg.effort.append(ef) | ||
|
||
self.state_pub.publish(self.msg) | ||
rate.sleep() | ||
|
||
def raw_to_rad_pos(self, raw, c): | ||
return (c.initial_position_raw - raw if c.flipped else raw - c.initial_position_raw) * c.RADIANS_PER_ENCODER_TICK | ||
def raw_to_rad_spd(self, raw, c): | ||
return (- raw if c.flipped else raw ) * c.RADIANS_PER_ENCODER_TICK |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this duplicates some of the logic already contained in the actual underlying controllers.
it might be better to subscribe to the
/.../state
topics of the underlying controllers and republish that data in thesensor_msgs/JointState
formatas a quick hack, I've added
/joint_state
as a 2nd published topic by the meta controller, which publishescontrol_msgs/FollowJointTrajectoryFeedback
wholeactual
field contains exactly all that's needed bysensor_msgs/JointState
. I'll send out a PR with that in case anyone else is interested.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since I'm away from this PR for a year and not remember well what I did. To be clear, this node read data from servo motor via physical serial/usb interface, but you're suggesting we can get same information from
control_msgs/FollowJointTrajectoryFeedback
ofjoint_trajectory_action_controller
. If so, my concern is that can we assume we always run that action server? of course that is the default interface for most of application, but for examplehttp://wiki.ros.org/dynamixel_controllers/Tutorials/CreatingJointPositionController tutorials is not assuming that action server, so I think this cause confusions.by the way, speaking of joint trajectory action, that does publish feedback only when the input trajectory command is interpolated, I think most of other robot publish feedback information all the time, so we need to fix somthing like this ->
def update_state(self):
injoint_trajectory_action_controller.py