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
00030
00031
00032
00033
00034
00035 """ diagnostic_updater for Python.
00036 @author Brice Rebsamen <brice [dot] rebsamen [gmail]>
00037 """
00038
00039 import roslib
00040 roslib.load_manifest('diagnostic_updater')
00041 import rospy
00042 from diagnostic_msgs.msg import DiagnosticStatus, KeyValue
00043
00044 OK = DiagnosticStatus.OK
00045 WARN = DiagnosticStatus.WARN
00046 ERROR = DiagnosticStatus.ERROR
00047
00048 class DiagnosticStatusWrapper(DiagnosticStatus):
00049 """ Wrapper for the diagnostic_msgs::DiagnosticStatus message that makes it
00050 easier to update.
00051
00052 This class handles common string formatting and vector handling issues
00053 for filling the diagnostic_msgs::DiagnosticStatus message. It is a subclass of
00054 diagnostic_msgs::DiagnosticStatus, so it can be passed directly to
00055 diagnostic publish calls.
00056 """
00057
00058 def __init__(self, *args, **kwds):
00059 """
00060 Constructor. Any message fields that are implicitly/explicitly
00061 set to None will be assigned a default value. The recommend
00062 use is keyword arguments as this is more robust to future message
00063 changes. You cannot mix in-order arguments and keyword arguments.
00064
00065 The available fields are:
00066 level,name,message,hardware_id,values
00067
00068 @param args: complete set of field values, in .msg order
00069 @param kwds: use keyword arguments corresponding to message field names
00070 to set specific fields.
00071 """
00072 DiagnosticStatus.__init__(self, *args, **kwds)
00073
00074
00075 def summary(self, *args):
00076 """ Fills out the level and message fields of the DiagnosticStatus.
00077
00078 Usage:
00079 summary(diagnostic_status): Copies the summary from a DiagnosticStatus message
00080 summary(lvl,msg): sets from lvl and messages
00081 """
00082 if len(args)==1:
00083 self.level = args[0].level
00084 self.message = args[0].message
00085 elif len(args)==2:
00086 self.level = args[0]
00087 self.message = str(args[1])
00088
00089
00090 def clearSummary(self):
00091 """ Clears the summary, setting the level to zero and the message to "".
00092 """
00093 self.summary(0, "")
00094
00095
00096 def mergeSummary(self, *args):
00097 """ Merges a level and message with the existing ones.
00098
00099 It is sometimes useful to merge two DiagnosticStatus messages. In that case,
00100 the key value pairs can be unioned, but the level and summary message
00101 have to be merged more intelligently. This function does the merge in
00102 an intelligent manner, combining the summary in *this, with the one
00103 that is passed in.
00104
00105 The combined level is the greater of the two levels to be merged.
00106 If both levels are non-zero (not OK), the messages are combined with a
00107 semicolon separator. If only one level is zero, and the other is
00108 non-zero, the message for the zero level is discarded. If both are
00109 zero, the new message is ignored.
00110
00111 Usage:
00112 mergeSummary(diagnostic_status): merge from a DiagnosticStatus message
00113 mergeSummary(lvl,msg): sets from lvl and msg
00114 """
00115 if len(args)==1:
00116 lvl = args[0].level
00117 msg = args[0].message
00118 elif len(args)==2:
00119 lvl = args[0]
00120 msg = args[1]
00121
00122 if (lvl>0) == (self.level>0):
00123 if len(self.message)>0:
00124 self.message += "; "
00125 self.message += msg
00126 elif lvl > self.level:
00127 self.message = msg
00128
00129 if lvl > self.level:
00130 self.level = lvl
00131
00132
00133 def add(self, key, val):
00134 """ Add a key-value pair.
00135
00136 This method adds a key-value pair. Any type that has a << stream
00137 operator can be passed as the second argument. Formatting is done
00138 using a std::stringstream.
00139
00140 @type key string
00141 @param key Key to be added.
00142 @type value string
00143 @param value Value to be added.
00144 """
00145 self.values.append(KeyValue(key,str(val)))