|
| 1 | +# Software License Agreement (BSD License) |
| 2 | +# |
| 3 | +# Copyright (c) 2018, PlusOne Robotics, Inc. All rights reserved. |
| 4 | +# |
| 5 | +# Redistribution and use in source and binary forms, with or without |
| 6 | +# modification, are permitted provided that the following conditions |
| 7 | +# are met: |
| 8 | +# |
| 9 | +# * Redistributions of source code must retain the above copyright |
| 10 | +# notice, this list of conditions and the following disclaimer. |
| 11 | +# * Redistributions in binary form must reproduce the above |
| 12 | +# copyright notice, this list of conditions and the following |
| 13 | +# disclaimer in the documentation and/or other materials provided |
| 14 | +# with the distribution. |
| 15 | +# * Neither the name of Plus One Robotics, Inc. nor the names of its |
| 16 | +# contributors may be used to endorse or promote products derived |
| 17 | +# from this software without specific prior written permission. |
| 18 | +# |
| 19 | +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 | +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 | +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 22 | +# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 23 | +# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 24 | +# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 25 | +# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 26 | +# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 27 | +# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 28 | +# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
| 29 | +# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 30 | +# POSSIBILITY OF SUCH DAMAGE. |
| 31 | + |
| 32 | +from __future__ import print_function |
| 33 | +import logging |
| 34 | +import re |
| 35 | +import subprocess |
| 36 | + |
| 37 | +import rospy |
| 38 | +from roswtf.rules import warning_rule, error_rule |
| 39 | + |
| 40 | + |
| 41 | +def _device_notfound_subproc(id_manufacturer, id_product): |
| 42 | + """ |
| 43 | + @rtype: [dict] |
| 44 | + @return: Example: |
| 45 | +
|
| 46 | + [{'device': '/dev/bus/usb/002/004', 'tag': 'Lenovo ', 'id': '17ef:305a'}, |
| 47 | + {'device': '/dev/bus/usb/002/001', 'tag': 'Linux Foundation 3.0 root hub', 'id': '1d6b:0003'}, |
| 48 | + {'device': '/dev/bus/usb/001/006', 'tag': 'Validity Sensors, Inc. ', 'id': '138a:0090'},,,] |
| 49 | +
|
| 50 | + @note: This method depends on Linux command (via subprocess), which makes |
| 51 | + this command platform-dependent. Ubuntu Xenial onward, a Python module |
| 52 | + that encapsulate platform operation becomes available so this method |
| 53 | + can be wiped out. See https://github.com/ros-drivers/openni2_camera/pull/80#discussion_r193295442 |
| 54 | + """ |
| 55 | + device_re = re.compile("Bus\s+(?P<bus>\d+)\s+Device\s+(?P<device>\d+).+ID\s(?P<id>\w+:\w+)\s(?P<tag>.+)$", re.I) |
| 56 | + df = subprocess.check_output("lsusb") |
| 57 | + devices = [] |
| 58 | + for i in df.split('\n'): |
| 59 | + if i: |
| 60 | + info = device_re.match(i) |
| 61 | + if info: |
| 62 | + dinfo = info.groupdict() |
| 63 | + logging.debug("dinfo: {}, dinfo.id: {}".format(dinfo, dinfo["id"])) |
| 64 | + if dinfo["id"] == "{}:{}".format(id_manufacturer, id_product): |
| 65 | + dinfo['device'] = "/dev/bus/usb/{}/{}".format(dinfo.pop('bus'), dinfo.pop('device')) |
| 66 | + devices.append(dinfo) |
| 67 | + logging.info("#devices: {}\ndevices: {}".format(len(devices), devices)) |
| 68 | + return devices |
| 69 | + |
| 70 | + |
| 71 | +def sensor_notfound(ctx): |
| 72 | + """ |
| 73 | + @summary: Check if expected number of sensors are found. |
| 74 | + Expected number of sensors can be set by |
| 75 | + ROS Parameter 'openni2_num_sensors_expected'. |
| 76 | + @note: Technically this can be static check, but because of the |
| 77 | + need for connecting to ROS Param server, this needs |
| 78 | + to be online check. |
| 79 | + """ |
| 80 | + errors = [] |
| 81 | + num_sensors_expected = rospy.get_param("openni2_num_sensors_expected", 1) |
| 82 | + # The set of manufacture id and prod id. Default: Asus Xtion. |
| 83 | + id_manufacturer = rospy.get_param("id_manufacturer", "1d27") |
| 84 | + id_product = rospy.get_param("id_product", "0601") |
| 85 | + devices = _device_notfound_subproc( |
| 86 | + id_manufacturer=id_manufacturer, id_product=id_product) |
| 87 | + num_sensors = len(devices) |
| 88 | + if num_sensors != num_sensors_expected: |
| 89 | + errors.append("{} openni2 sensors found (expected: {}).".format( |
| 90 | + num_sensors, num_sensors_expected)) |
| 91 | + return errors |
| 92 | + |
| 93 | + |
| 94 | +# app_warnings and app_errors declare the rules that we actually check |
| 95 | +app_warnings_online = [ |
| 96 | +] |
| 97 | + |
| 98 | +app_warnings_static = [ |
| 99 | +] |
| 100 | + |
| 101 | +app_errors_online = [ |
| 102 | + (sensor_notfound, "Different number of openni2 sensors found."), |
| 103 | +] |
| 104 | + |
| 105 | +app_errors_static = [ |
| 106 | +] |
| 107 | + |
| 108 | + |
| 109 | +# roswtf entry point for online checks |
| 110 | +def roswtf_plugin_online(ctx): |
| 111 | + for r in app_warnings_online: |
| 112 | + warning_rule(r, r[0](ctx), ctx) |
| 113 | + for r in app_errors_online: |
| 114 | + error_rule(r, r[0](ctx), ctx) |
| 115 | + |
| 116 | + |
| 117 | +def roswtf_plugin_static(ctx): |
| 118 | + for r in app_warnings_static: |
| 119 | + warning_rule(r, r[0](ctx), ctx) |
| 120 | + for r in app_errors_static: |
| 121 | + error_rule(r, r[0](ctx), ctx) |
0 commit comments