mech_diag_test.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 #
3 # Software License Agreement (BSD License)
4 #
5 # Copyright (c) 2010, Willow Garage, Inc.
6 # All rights reserved.
7 #
8 # Redistribution and use in source and binary forms, with or without
9 # modification, are permitted provided that the following conditions
10 # are met:
11 #
12 # * Redistributions of source code must retain the above copyright
13 # notice, this list of conditions and the following disclaimer.
14 # * Redistributions in binary form must reproduce the above
15 # copyright notice, this list of conditions and the following
16 # disclaimer in the documentation and/or other materials provided
17 # with the distribution.
18 # * Neither the name of the Willow Garage nor the names of its
19 # contributors may be used to endorse or promote products derived
20 # from this software without specific prior written permission.
21 #
22 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 # POSSIBILITY OF SUCH DAMAGE.
34 #
35 
36 ##\author Kevin Watts
37 
38 from __future__ import with_statement
39 PKG = 'pr2_mechanism_diagnostics'
40 import roslib; roslib.load_manifest(PKG)
41 
42 import rospy, rostest, unittest
43 from diagnostic_msgs.msg import DiagnosticArray, DiagnosticStatus
44 
45 import sys
46 import threading
47 from time import sleep
48 
49 WAIT_TIME = 5
50 
51 class TestMechDiag(unittest.TestCase):
52  def __init__(self, *args):
53  super(TestMechDiag, self).__init__(*args)
54 
55  self._mutex = threading.Lock()
56  self._joints = {}
57  self._controllers = {}
58 
59  self._start_time = rospy.get_time()
60 
61  # Calibrated, Nan for joints
62  self._cal = rospy.get_param("mech_diag/cal", True)
63  self._nan = rospy.get_param("mech_diag/nan", False)
64 
65  self._running = rospy.get_param("mech_diag/running", True)
66  self._overrun = rospy.get_param("mech_diag/overrun", False)
67 
68  sub_diag = rospy.Subscriber('/diagnostics', DiagnosticArray, self._diag_cb)
69 
70  def _diag_cb(self, msg):
71  with self._mutex:
72  for stat in msg.status:
73  if stat.name.startswith('Joint'):
74  self._joints[stat.name] = stat
75  if stat.name.startswith('Controller'):
76  # Ignore special "No controllers" status
77  if stat.name.find('No controllers') > 0:
78  continue
79 
80  self._controllers[stat.name] = stat
81 
82  def test_mech_diag(self):
83  while not rospy.is_shutdown() and (rospy.get_time() - self._start_time) < WAIT_TIME:
84  sleep(0.5)
85 
86  self.assert_(not rospy.is_shutdown(), "Rospy shutdown")
87 
88  with self._mutex:
89  if len(self._joints.items()) == 0:
90  self.assert_(False, "No joint data in diagnostics")
91  for key, val in self._joints.iteritems():
92  if self._nan:
93  self.assert_(val.level == 2, "Joint %s was not ERROR, but was NaN. Level %d" % (val.name, val.level))
94  elif self._cal:
95  self.assert_(val.level == 0, "Joint %s was not OK. Level %d" % (val.name, val.level))
96  else:
97  self.assert_(val.level == 1, "Joint %s was not warning, but was uncalibrated. Level %d" % (val.name, val.level))
98 
99  if len(self._controllers.items()) == 0:
100  self.assert_(False, "No controller data in diagnostics")
101 
102  for key, val in self._controllers.iteritems():
103  if self._overrun:
104  self.assert_(val.level == 1, "Controller %s was not WARN, but was overrun. Level %d" % (val.name, val.level))
105  else:
106  self.assert_(val.level == 0, "Controller %s was not OK. Level %d" % (val.name, val.level))
107 
108 if __name__ == '__main__':
109  rospy.init_node('test_mech_diag_nominal')
110  if True:
111  rostest.run(PKG, sys.argv[0], TestMechDiag, sys.argv)
112  else:
113  suite = unittest.TestSuite()
114  suite.addTest(TestMechDiag('test_mech_diag'))
115 
116  unittest.TextTestRunner(verbosity = 2).run(suite)


pr2_mechanism_diagnostics
Author(s): Kevin Watts
autogenerated on Mon Jun 10 2019 14:19:09