Go to the documentation of this file.00001
00002
00003 """
00004 diagnostics.py - diagnostic output code
00005 Copyright (c) 2011 Vanadium Labs LLC. All right reserved.
00006
00007 Redistribution and use in source and binary forms, with or without
00008 modification, are permitted provided that the following conditions are met:
00009 * Redistributions of source code must retain the above copyright
00010 notice, this list of conditions and the following disclaimer.
00011 * Redistributions in binary form must reproduce the above copyright
00012 notice, this list of conditions and the following disclaimer in the
00013 documentation and/or other materials provided with the distribution.
00014 * Neither the name of Vanadium Labs LLC nor the names of its
00015 contributors may be used to endorse or promote products derived
00016 from this software without specific prior written permission.
00017
00018 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
00019 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00020 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00021 DISCLAIMED. IN NO EVENT SHALL VANADIUM LABS BE LIABLE FOR ANY DIRECT, INDIRECT,
00022 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00023 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
00024 OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00025 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
00026 OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
00027 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00028 """
00029
00030 import rospy
00031 from diagnostic_msgs.msg import DiagnosticArray
00032 from sensor_msgs.msg import JointState
00033
00034 class DiagnosticsPublisher:
00035 """ Class to handle publications of joint_states message. """
00036
00037 def __init__(self):
00038 self.t_delta = rospy.Duration(1.0/rospy.get_param("~diagnostic_rate", 1.0))
00039 self.t_next = rospy.Time.now() + self.t_delta
00040 self.pub = rospy.Publisher('diagnostics', DiagnosticArray, queue_size=5)
00041
00042 def update(self, joints, controllers):
00043 """ Publish diagnostics. """
00044 now = rospy.Time.now()
00045 if now > self.t_next:
00046
00047 msg = DiagnosticArray()
00048 msg.header.stamp = now
00049 for controller in controllers:
00050 d = controller.getDiagnostics()
00051 if d:
00052 msg.status.append(d)
00053 for joint in joints:
00054 d = joint.getDiagnostics()
00055 if d:
00056 msg.status.append(d)
00057
00058 self.pub.publish(msg)
00059 self.t_next = now + self.t_delta
00060
00061
00062 class JointStatePublisher:
00063 """ Class to handle publications of joint_states message. """
00064
00065 def __init__(self):
00066
00067 self.rate = rospy.get_param("~read_rate", 10.0)
00068 self.t_delta = rospy.Duration(1.0/self.rate)
00069 self.t_next = rospy.Time.now() + self.t_delta
00070
00071
00072 self.pub = rospy.Publisher('joint_states', JointState, queue_size=5)
00073
00074 def update(self, joints, controllers):
00075 """ publish joint states. """
00076 if rospy.Time.now() > self.t_next:
00077 msg = JointState()
00078 msg.header.stamp = rospy.Time.now()
00079 msg.name = list()
00080 msg.position = list()
00081 msg.velocity = list()
00082 for joint in joints:
00083 msg.name.append(joint.name)
00084 msg.position.append(joint.position)
00085 msg.velocity.append(joint.velocity)
00086 for controller in controllers:
00087 msg.name += controller.joint_names
00088 msg.position += controller.joint_positions
00089 msg.velocity += controller.joint_velocities
00090 self.pub.publish(msg)
00091 self.t_next = rospy.Time.now() + self.t_delta
00092