diagnostics.py
Go to the documentation of this file.
00001 # Software License Agreement (BSD License)
00002 #
00003 # Copyright (c) 2011, Willow Garage, Inc.
00004 # All rights reserved.
00005 #
00006 # Redistribution and use in source and binary forms, with or without
00007 # modification, are permitted provided that the following conditions
00008 # are met:
00009 #
00010 #  * Redistributions of source code must retain the above copyright
00011 #    notice, this list of conditions and the following disclaimer.
00012 #  * Redistributions in binary form must reproduce the above
00013 #    copyright notice, this list of conditions and the following
00014 #    disclaimer in the documentation and/or other materials provided
00015 #    with the distribution.
00016 #  * Neither the name of the Willow Garage nor the names of its
00017 #    contributors may be used to endorse or promote products derived
00018 #    from this software without specific prior written permission.
00019 #
00020 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00021 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00022 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00023 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00024 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00025 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00026 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00027 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00028 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00029 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00030 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00031 # POSSIBILITY OF SUCH DAMAGE.
00032 
00033 # Johannes Mayr joh.mayr@jku.at
00034 
00035 import rospy
00036 
00037 from diagnostic_msgs.msg import DiagnosticArray, DiagnosticStatus, KeyValue
00038 
00039 class WheeledRobinDiagnostics():
00040     def __init__(self):
00041         self.digital_outputs = {0:"OFF",
00042                                 1:"ON"}
00043         self.mode = {1:"Passive",
00044                         2:"Safe",
00045                         3:"Full"}
00046         self.diag_pub = rospy.Publisher('/diagnostics', DiagnosticArray)
00047         self.last_diagnostics_time = rospy.get_rostime()        
00048 
00049     def node_status(self, msg, status):
00050         curr_time = rospy.Time.now()
00051         diag = DiagnosticArray()
00052         diag.header.stamp = curr_time
00053         stat = DiagnosticStatus()
00054         if status == "error":
00055             stat = DiagnosticStatus(name="WheeledRobin Node", level=DiagnosticStatus.ERROR, message=msg)
00056         if status == "warn":
00057             stat = DiagnosticStatus(name="WheeledRobin Node", level=DiagnosticStatus.WARN, message=msg)
00058         diag.status.append(stat)
00059         self.diag_pub.publish(diag)
00060 
00061     def publish(self, sensor_state, gyro):
00062         curr_time = sensor_state.header.stamp
00063         # limit to 5hz
00064         if (curr_time - self.last_diagnostics_time).to_sec() < 0.2:
00065             return
00066         self.last_diagnostics_time = curr_time
00067 
00068         diag = DiagnosticArray()
00069         diag.header.stamp = curr_time
00070         stat = DiagnosticStatus()
00071 
00072         #node status
00073         stat = DiagnosticStatus(name="WheeledRobin Node", level=DiagnosticStatus.OK, message="RUNNING")
00074         diag.status.append(stat)
00075 
00076         #mode info
00077         """
00078         stat = DiagnosticStatus(name="Operating Mode", level=DiagnosticStatus.OK)
00079         try:
00080             stat.message = self.mode[sensor_state.mode]
00081         except KeyError as ex:
00082             stat.level=DiagnosticStatus.ERROR
00083             stat.message = "Invalid Mode Reported %s"%ex
00084             rospy.logwarn(stat.message)
00085         diag.status.append(stat)
00086         """
00087         
00088         #cliff sensors
00089         """ 
00090         stat = DiagnosticStatus(name="Cliff Sensor", level=DiagnosticStatus.OK, message="OK")
00091         if sensor_state.cliff_left or sensor_state.cliff_front_left or sensor_state.cliff_right or sensor_state.cliff_front_right:
00092             stat.level = DiagnosticStatus.WARN
00093             if (sensor_state.current/1000.0)>0:
00094                 stat.message = "Near Cliff: While the irobot create is charging, the create thinks it's near a cliff."
00095                 stat.level = DiagnosticStatus.OK # We're OK here
00096             else:
00097                 stat.message = "Near Cliff"
00098         stat.values = [KeyValue("Left", str(sensor_state.cliff_left)),
00099                        KeyValue("Left Signal", str(sensor_state.cliff_left_signal)),
00100                        KeyValue("Front Left", str(sensor_state.cliff_front_left)),
00101                        KeyValue("Front Left Signal", str(sensor_state.cliff_front_left_signal)),
00102                        KeyValue("Front Right", str(sensor_state.cliff_right)),
00103                        KeyValue("Front Right Signal", str(sensor_state.cliff_right_signal)),
00104                        KeyValue("Right", str(sensor_state.cliff_front_right)),
00105                        KeyValue("Right Signal", str(sensor_state.cliff_front_right_signal))]
00106         diag.status.append(stat)
00107 
00108         #Wall sensors
00109         stat = DiagnosticStatus(name="Wall Sensor", level=DiagnosticStatus.OK, message="OK")
00110         #wall always seems to be false??? 
00111         if sensor_state.wall:
00112             stat.level = DiagnosticStatus.ERROR
00113             stat.message = "Near Wall"
00114         stat.values = [KeyValue("Wall", str(sensor_state.wall)),
00115                        KeyValue("Wall Signal", str(sensor_state.wall_signal)),
00116                        KeyValue("Virtual Wall", str(sensor_state.virtual_wall))]
00117         diag.status.append(stat)
00118 
00119         #Gyro
00120         stat = DiagnosticStatus(name="Gyro Sensor", level = DiagnosticStatus.OK, message = "OK")
00121         if gyro is None:
00122             stat.level = DiagnosticStatus.WARN
00123             stat.message = "Gyro Not Enabled: To enable the gyro set the has_gyro param in the wheeled_robin_node."   
00124         elif gyro.cal_offset < 100:
00125             stat.level = DiagnosticStatus.ERROR
00126             stat.message = "Gyro Power Problem: For more information visit http://answers.ros.org/question/2091/turtlebot-bad-gyro-calibration-also."
00127         elif gyro.cal_offset > 575.0 or gyro.cal_offset < 425.0:
00128             stat.level = DiagnosticStatus.ERROR
00129             stat.message = "Bad Gyro Calibration Offset: The gyro average is outside the standard deviation."
00130 
00131         if gyro is not None:    
00132             stat.values = [KeyValue("Gyro Enabled", str(gyro is not None)),
00133                            KeyValue("Raw Gyro Rate", str(sensor_state.user_analog_input)),
00134                            KeyValue("Calibration Offset", str(gyro.cal_offset)),
00135                            KeyValue("Calibration Buffer", str(gyro.cal_buffer))]
00136         diag.status.append(stat)
00137         """
00138         #Digital Outputs
00139         stat = DiagnosticStatus(name="Digital Outputs", level = DiagnosticStatus.OK, message = "OK")
00140         out_byte = sensor_state.digital_outputs
00141         stat.values = [KeyValue("Raw Byte", str(out_byte)),
00142                         KeyValue("Digital Out 0", self.digital_outputs[(out_byte >>0)%2]),
00143                         KeyValue("Digital Out 1", self.digital_outputs[(out_byte >>1)%2]),
00144                         KeyValue("Digital Out 2", self.digital_outputs[(out_byte >>2)%2]),
00145                         KeyValue("Digital Out 3", self.digital_outputs[(out_byte >>3)%2]),
00146                         KeyValue("Digital Out 4", self.digital_outputs[(out_byte >>4)%2]),
00147                         KeyValue("Digital Out 5", self.digital_outputs[(out_byte >>5)%2]),
00148                         KeyValue("Digital Out 6", self.digital_outputs[(out_byte >>6)%2]),
00149                         KeyValue("Digital Out 7", self.digital_outputs[(out_byte >>7)%2])]
00150         diag.status.append(stat)
00151 
00152         #Digital Inputs
00153         stat = DiagnosticStatus(name="Digital Inputs", level = DiagnosticStatus.OK, message = "OK")
00154         in_byte = sensor_state.digital_inputs
00155         stat.values = [KeyValue("Raw Byte", str(in_byte)),
00156                         KeyValue("Digital Input 0", self.digital_outputs[(in_byte >>0)%2]),
00157                         KeyValue("Digital Input 1", self.digital_outputs[(in_byte >>1)%2]),
00158                         KeyValue("Digital Input 2", self.digital_outputs[(in_byte >>2)%2]),
00159                         KeyValue("Digital Input 3", self.digital_outputs[(in_byte >>3)%2]),
00160                         KeyValue("Digital Input 4", self.digital_outputs[(in_byte >>4)%2]),
00161                         KeyValue("Digital Input 5", self.digital_outputs[(in_byte >>5)%2]),
00162                         KeyValue("Digital Input 6", self.digital_outputs[(in_byte >>6)%2]),
00163                         KeyValue("Digital Input 7", self.digital_outputs[(in_byte >>7)%2])]
00164         diag.status.append(stat)
00165 
00166         #publish
00167         self.diag_pub.publish(diag)
00168 


wheeled_robin_node
Author(s): Johannes Mayr , Klemens Springer
autogenerated on Fri Aug 28 2015 13:39:00