Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 import rospy
00030
00031 from pr2_mechanism_msgs.msg import MechanismStatistics
00032 from diagnostic_msgs.msg import DiagnosticArray, DiagnosticStatus, KeyValue
00033
00034
00035 def controller_to_diag(c):
00036 d = DiagnosticStatus()
00037 d.name = 'Controller (' + c.name + ')'
00038
00039 d.level = 0
00040 if (c.running):
00041 d.message = 'Running'
00042 else:
00043 d.message = 'Stopped'
00044
00045 if (not use_sim_time and c.num_control_loop_overruns > 0):
00046 d.message += ' !!! Broke Realtime, used ' + \
00047 str(int(c.max_time.to_sec() * 1e6)) + \
00048 ' micro seconds in update loop'
00049 if c.time_last_control_loop_overrun + rospy.Duration(30.0) > rospy.Time.now():
00050 d.level = 1
00051
00052 d.values.append(
00053 KeyValue('Avg Time in Update Loop (usec)', str(int(c.mean_time.to_sec() * 1e6))))
00054 d.values.append(
00055 KeyValue('Max Time in update Loop (usec)', str(int(c.max_time.to_sec() * 1e6))))
00056 d.values.append(
00057 KeyValue('Variance on Time in Update Loop', str(int(c.variance_time.to_sec() * 1e6))))
00058 d.values.append(
00059 KeyValue('Percent of Cycle Time Used', str(int(c.mean_time.to_sec() / 0.00001))))
00060 d.values.append(
00061 KeyValue('Number of Control Loop Overruns', str(int(c.num_control_loop_overruns))))
00062 d.values.append(
00063 KeyValue('Timestamp of Last Control Loop Overrun (sec)', str(int(c.time_last_control_loop_overrun.to_sec()))))
00064 return d
00065
00066 rospy.init_node('controller_to_diagnostics')
00067 use_sim_time = rospy.get_param('use_sim_time', False)
00068 pub_diag = rospy.Publisher('/diagnostics', DiagnosticArray)
00069
00070 last_publish_time = rospy.Time(0.0)
00071
00072
00073 def state_cb(msg):
00074 global last_publish_time
00075 now = rospy.get_rostime()
00076 if (now - last_publish_time).to_sec() > 1.0:
00077 if len(msg.controller_statistics) == 0:
00078 d = DiagnosticArray()
00079 d.header.stamp = msg.header.stamp
00080 ds = DiagnosticStatus()
00081 ds.name = "Controller: No controllers loaded in controller manager"
00082 d.status = [ds]
00083 pub_diag.publish(d)
00084 else:
00085 for c in msg.controller_statistics:
00086 d = DiagnosticArray()
00087 d.header.stamp = msg.header.stamp
00088 d.status = [controller_to_diag(c)]
00089 pub_diag.publish(d)
00090 last_publish_time = now
00091
00092 rospy.Subscriber('mechanism_statistics', MechanismStatistics, state_cb)
00093 rospy.spin()