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)
00041 self.iter = 0
00042
00043 def update(self, servos, controllers):
00044 """ Publish diagnostics. """
00045 now = rospy.Time.now()
00046 if now > self.t_next:
00047
00048 if self.iter % 5 == 0:
00049 servos.updateStats()
00050
00051
00052 msg = DiagnosticArray()
00053 msg.header.stamp = now
00054 for servo in servos.values():
00055 msg.status.append(servo.getDiagnostics())
00056 for controller in controllers:
00057 msg.status.append(controller.getDiagnostics())
00058
00059
00060 self.pub.publish(msg)
00061 self.t_next = now + self.t_delta
00062 self.iter += 1
00063
00064
00065 class JointStatePublisher:
00066 """ Class to handle publications of joint_states message. """
00067
00068 def __init__(self):
00069
00070 self.rate = rospy.get_param("~read_rate", 10.0)
00071 self.t_delta = rospy.Duration(1.0/self.rate)
00072 self.t_next = rospy.Time.now() + self.t_delta
00073
00074
00075 self.pub = rospy.Publisher('joint_states', JointState)
00076
00077 def update(self, servos, controllers):
00078 """ publish joint states. """
00079 if rospy.Time.now() > self.t_next:
00080 msg = JointState()
00081 msg.header.stamp = rospy.Time.now()
00082 msg.name = list()
00083 msg.position = list()
00084 msg.velocity = list()
00085 for servo in servos.values():
00086 msg.name.append(servo.name)
00087 msg.position.append(servo.angle)
00088 msg.velocity.append(servo.velocity)
00089 for controller in controllers:
00090 msg.name += controller.joint_names
00091 msg.position += controller.joint_positions
00092 msg.velocity += controller.joint_velocities
00093 self.pub.publish(msg)
00094 self.t_next = rospy.Time.now() + self.t_delta
00095