This article works in the following environment.
item | value |
---|---|
CPU | Core i5-8250U |
Ubuntu | 16.04 |
ROS | Kinetic |
Gazebo | 7.0.0 |
python | 2.7.12 |
For installation, refer to ROS Course 02 Installation. Also, the program in this article has been uploaded to github. See ROS Course 11 git repository.
Last time I set up rosbridge_server and introduced an example of accessing from javascript of the browser. This time, use roslibpy
to create a client in python that connects to rosbridge_server.
This time we will use pip to install roslibpy. The trouble with pip (rather than python) is that there are various directories to install, and mixing them can be quite annoying. For example
/usr/local/lib/python2.7/
)
When you get pip with apt ・ When you get it with sudo pip
~ / .local / lib / python2.7 /
)
When entered with pip --user
This will pollute the environment and cause an error in pip when running the following command
I get an error after doing this
sudo apt install python-pip
pip install --upgrade pip
There are many problems with using apt and pip together, so it is better to install without using apt. If you have done the above, you can uninstall all pips with the following command.
Uninstall pip
sudo python -m pip uninstall pip
sudo apt autoremove python-pip
You can install roslibpy by executing the following.
Install roslibpy
curl -kL https://bootstrap.pypa.io/get-pip.py | sudo python
sudo pip install roslibpy
sudo pip install service_identity
web_lecture/scripts/rosbridge_client.py
#!/usr/bin/env python
# -*- coding:utf8 -*-
import time
from roslibpy import Message, Ros, Topic
import time
class rosbridge_client:
def __init__(self):
self.ros_client = Ros('127.0.0.1', 9090)
print("wait for server")
self.publisher = Topic(self.ros_client, '/cmd_vel', 'geometry_msgs/Twist')
self.listener = Topic(self.ros_client, '/odom', 'nav_msgs/Odometry')
self.listener.subscribe(self.callback)
self.ros_client.on_ready(self.start_thread, run_in_thread=True)
self.ros_client.run_forever()
def callback(self, message):
x = message["pose"]["pose"]["position"]["x"]
y = message["pose"]["pose"]["position"]["y"]
print(x, y)
def start_thread(self):
while True:
if self.ros_client.is_connected:
self.publisher.publish(Message({
'linear': {
'x': 0.5,
'y': 0,
'z': 0
},
'angular': {
'x': 0,
'y': 0,
'z': 0.5
}
}))
else:
print("Disconnect")
break
time.sleep(1.0)
if __name__ == '__main__':
rosbridge_client()
self.ros_client = Ros ('127.0.0.1', 9090)
.self.ros_client.is_connected
.self.ros_client.connect ()
even if it is broken.Message ()
and publish it with self.publisher.publish ()
.self.listener.subscribe (self.callback)
. You can pass the received message as an argument of the callback function and access it like x = message ["pose "] [" pose "] ["position "] ["x "]
.self.ros_client.on_ready (self.start_thread, run_in_thread = True)
.Run sim
roslaunch web_lecture web_if.launch
Run roslibpy client
roscd web_lecture/scripts && rospy_client.py`You can do it with.
rosbridge is a tool for accessing ros from a system where ros is not originally installed. However, if you use this loosely coupled system, you can also build a multi-master system as shown in the figure below.
roslibpy example Details of implementation
Link to ROS course table of contents
Recommended Posts