From 1d99dc2b5c4f36431e2404f5b478c11102edccdd Mon Sep 17 00:00:00 2001 From: Naoya Yamaguchi <708yamaguchi@gmail.com> Date: Fri, 8 Oct 2021 21:31:24 +0900 Subject: [PATCH 1/6] Add script to bind rfcomm device to remote Bluetooth device --- .../jsk_robot_startup/scripts/rfcomm_bind.py | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100755 jsk_robot_common/jsk_robot_startup/scripts/rfcomm_bind.py diff --git a/jsk_robot_common/jsk_robot_startup/scripts/rfcomm_bind.py b/jsk_robot_common/jsk_robot_startup/scripts/rfcomm_bind.py new file mode 100755 index 0000000000..f28041f4b7 --- /dev/null +++ b/jsk_robot_common/jsk_robot_startup/scripts/rfcomm_bind.py @@ -0,0 +1,60 @@ +#!/usr/bin/env python + +import os +import rospy +import subprocess +from subprocess import PIPE +import yaml + + +class RfcommBind(object): + """ + This node binds the RFCOMM device to a remote Bluetooth device. + MAC address of bound devices must be stored in '~rfcomm_devices' file. + + '~rfcomm_devices' yaml file is like the following. + name is device name for human to distinguish. + address is device's bluetooth MAC address + - name: device1 + address: XX:XX:XX:XX:XX:XX + - name: device2 + address: YY:YY:YY:YY:YY:YY + + Note that this node is called with sudo. + + For detail of rfcomm, run this command: + $ man rfcomm + """ + + def __init__(self): + self.rfcomm_devices = {} + yaml_path = rospy.get_param( + '~rfcomm_devices', '/var/lib/robot/rfcomm_devices.yaml') + if os.path.exists(yaml_path): + with open(yaml_path) as yaml_f: + self.rfcomm_devices = yaml.load(yaml_f) + rospy.loginfo('{} is loaded.'.format(yaml_path)) + else: + rospy.logerr('Cannot find {}'.format(yaml_path)) + + def bind_devices(self): + dev_num = 0 + for dev in self.rfcomm_devices: + while 0 == subprocess.call( + ['rfcomm', 'show', '{}'.format( + dev_num)], stdout=PIPE, stderr=PIPE): + rospy.loginfo('/dev/rfcomm{} is already used.'.format(dev_num)) + dev_num += 1 + subprocess.call( + ['sudo', 'rfcomm', 'bind', str(dev_num), dev["address"]], + stdout=PIPE, stderr=PIPE) + rospy.loginfo( + 'Device {}({}) is bound to /dev/rfcomm{}'.format( + dev["name"], dev["address"], dev_num)) + dev_num += 1 + + +if __name__ == "__main__": + rospy.init_node('rfcomm_bind') + app = RfcommBind() + app.bind_devices() From 8a8322dc4f4fbf582a80c4a9ceeb382fca68fa9d Mon Sep 17 00:00:00 2001 From: Naoya Yamaguchi <708yamaguchi@gmail.com> Date: Fri, 8 Oct 2021 21:56:53 +0900 Subject: [PATCH 2/6] Add launch file to call rfcomm_bind.py --- .../jsk_robot_startup/launch/rfcomm_bind.launch | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 jsk_robot_common/jsk_robot_startup/launch/rfcomm_bind.launch diff --git a/jsk_robot_common/jsk_robot_startup/launch/rfcomm_bind.launch b/jsk_robot_common/jsk_robot_startup/launch/rfcomm_bind.launch new file mode 100644 index 0000000000..84cfce1386 --- /dev/null +++ b/jsk_robot_common/jsk_robot_startup/launch/rfcomm_bind.launch @@ -0,0 +1,13 @@ + + + + + + + + + rfcomm_devices: $(arg rfcomm_devices) + + + From 1fb5db59f898976f6a5e1cacfcfa3f9e8d8a2208 Mon Sep 17 00:00:00 2001 From: Naoya Yamaguchi <708yamaguchi@gmail.com> Date: Fri, 8 Oct 2021 21:57:38 +0900 Subject: [PATCH 3/6] Add launch for pr2 to call rfcomm_bind.py --- .../jsk_pr2_sensors/rfcomm_bind_pr2.launch | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 jsk_pr2_robot/jsk_pr2_startup/jsk_pr2_sensors/rfcomm_bind_pr2.launch diff --git a/jsk_pr2_robot/jsk_pr2_startup/jsk_pr2_sensors/rfcomm_bind_pr2.launch b/jsk_pr2_robot/jsk_pr2_startup/jsk_pr2_sensors/rfcomm_bind_pr2.launch new file mode 100644 index 0000000000..d4ad4c56cf --- /dev/null +++ b/jsk_pr2_robot/jsk_pr2_startup/jsk_pr2_sensors/rfcomm_bind_pr2.launch @@ -0,0 +1,11 @@ + + + + + + + + + + + From ffb17816924c7a9290f1dc6d1dde633613eefd33 Mon Sep 17 00:00:00 2001 From: Naoya Yamaguchi <708yamaguchi@gmail.com> Date: Fri, 8 Oct 2021 22:13:06 +0900 Subject: [PATCH 4/6] Change launch name --- .../{rfcomm_bind_pr2.launch => pr2_rfcomm_bind.launch} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename jsk_pr2_robot/jsk_pr2_startup/jsk_pr2_sensors/{rfcomm_bind_pr2.launch => pr2_rfcomm_bind.launch} (100%) diff --git a/jsk_pr2_robot/jsk_pr2_startup/jsk_pr2_sensors/rfcomm_bind_pr2.launch b/jsk_pr2_robot/jsk_pr2_startup/jsk_pr2_sensors/pr2_rfcomm_bind.launch similarity index 100% rename from jsk_pr2_robot/jsk_pr2_startup/jsk_pr2_sensors/rfcomm_bind_pr2.launch rename to jsk_pr2_robot/jsk_pr2_startup/jsk_pr2_sensors/pr2_rfcomm_bind.launch From 144a116e3dea4d92a114e14038ae5fbab9385213 Mon Sep 17 00:00:00 2001 From: Naoya Yamaguchi <708yamaguchi@gmail.com> Date: Sat, 20 Nov 2021 01:52:06 +0900 Subject: [PATCH 5/6] Add usage and management about rfcomm bind --- jsk_pr2_robot/jsk_pr2_startup/README.md | 40 ++++++++++++++++++++ jsk_robot_common/jsk_robot_startup/README.md | 26 +++++++++++++ 2 files changed, 66 insertions(+) diff --git a/jsk_pr2_robot/jsk_pr2_startup/README.md b/jsk_pr2_robot/jsk_pr2_startup/README.md index 3a1bd33d66..bfd7e51926 100644 --- a/jsk_pr2_robot/jsk_pr2_startup/README.md +++ b/jsk_pr2_robot/jsk_pr2_startup/README.md @@ -57,3 +57,43 @@ $ sudo setcap cap_fowner+ep /usr/bin/mongod - hark jsk installation: https://github.com/jsk-ros-pkg/jsk_3rdparty/blob/master/hark_jsk_plugins/INSTALL - Microcone: http://www.hark.jp/wiki.cgi?page=SupportedHardware#p10 + +### Bind rfcomm device + +By binding rfcomm device, we can connect bluetooth device via device file (e.g. `/dev/rfcomm1`). For example, rosserial with [this PR](https://github.com/ros-drivers/rosserial/pull/569) can be used over bluetooth connection. + +For detail, please see https://github.com/jsk-ros-pkg/jsk_robot/blob/master/jsk_robot_common/jsk_robot_startup/README.md#launchrfcomm_bindlaunch + +#### usage + +Save the bluetooth device MAC address to file like `/var/lib/robot/rfcomm_devices.yaml` in PR2. + +``` +- name: device1 + address: XX:XX:XX:XX:XX:XX +- name: device2 + address: YY:YY:YY:YY:YY:YY +``` + +Then, bind rfcomm devices. + +``` +# login as root user in pr2 +ssh pr2 +su +# Assume the bluetooth dongle is plugged into c2 +roslaunch jsk_pr2_startup pr2_rfcomm_bind.launch machine:=c2 +``` + +To check how many devices are bound to rfcomm, use rfcomm command. +``` +ssh pr2 +ssh c2 +rfcomm +``` + +#### management + +Currently in PR2, `pr2_rfcomm_bind.launch` is started automatically by upstart. + +The upstart config file is in `/etc/upstart/jsk-rfcomm-bind.conf` in PR2. diff --git a/jsk_robot_common/jsk_robot_startup/README.md b/jsk_robot_common/jsk_robot_startup/README.md index 815db0ffb5..ce294a10a8 100644 --- a/jsk_robot_common/jsk_robot_startup/README.md +++ b/jsk_robot_common/jsk_robot_startup/README.md @@ -144,3 +144,29 @@ This node publish the luminance calculated from input image and room light statu This launch file provides a set of nodes for safe teleoperation common to mobile robots. Robot-specific nodes such as `/joy`, `/teleop` or `/cable_warning` must be included in the teleop launch file for each robot, such as [safe_teleop.xml for PR2](https://github.com/jsk-ros-pkg/jsk_robot/blob/master/jsk_pr2_robot/jsk_pr2_startup/jsk_pr2_move_base/safe_teleop.xml) or [safe_teleop.xml for fetch](https://github.com/jsk-ros-pkg/jsk_robot/blob/master/jsk_fetch_robot/jsk_fetch_startup/launch/fetch_teleop.xml). ![JSK teleop_base system](images/jsk_safe_teleop_system.png) + +## launch/rfcomm_bind.launch + +This script binds rfcomm device to remote bluetooth device. By binding rfcomm device, we can connect bluetooth device via device file (e.g. `/dev/rfcomm1`). For example, rosserial with [this PR](https://github.com/ros-drivers/rosserial/pull/569) can be used over bluetooth connection. + +### Usage + +Save the bluetooth device MAC address to file like `/var/lib/robot/rfcomm_devices.yaml`. + +``` +- name: device1 + address: XX:XX:XX:XX:XX:XX +- name: device2 + address: YY:YY:YY:YY:YY:YY +``` + +Then, bind rfcomm devices. + +``` +roslaunch jsk_robot_startup rfcomm_bind.launch +``` + +To check how many devices are bound to rfcomm, use rfcomm command. +``` +rfcomm +``` From e1e8836f0658203030110cb6ec7175a56d495a9d Mon Sep 17 00:00:00 2001 From: Naoya Yamaguchi <708yamaguchi@gmail.com> Date: Fri, 1 Jul 2022 02:37:41 +0900 Subject: [PATCH 6/6] [jsk_robot_startup] Clear rfcomm devices before binding --- jsk_robot_common/jsk_robot_startup/scripts/rfcomm_bind.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/jsk_robot_common/jsk_robot_startup/scripts/rfcomm_bind.py b/jsk_robot_common/jsk_robot_startup/scripts/rfcomm_bind.py index f28041f4b7..0ad26599a3 100755 --- a/jsk_robot_common/jsk_robot_startup/scripts/rfcomm_bind.py +++ b/jsk_robot_common/jsk_robot_startup/scripts/rfcomm_bind.py @@ -37,6 +37,11 @@ def __init__(self): else: rospy.logerr('Cannot find {}'.format(yaml_path)) + def release_devices(self): + subprocess.call( + ['sudo', 'rfcomm', 'release', 'all'], + stdout=PIPE, stderr=PIPE) + def bind_devices(self): dev_num = 0 for dev in self.rfcomm_devices: @@ -57,4 +62,5 @@ def bind_devices(self): if __name__ == "__main__": rospy.init_node('rfcomm_bind') app = RfcommBind() + app.release_devices() app.bind_devices()