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