ROS Advent Calendar 2019 This is the article on the 23rd day.
There are only 8 days left until the EOL of Python2, but are you all done with Python3?
This time, I tried to make it possible to execute rospy
of ROS1 with virtualenv
of Python3, so I would like to introduce it.
In order to use the official ROS1 rospy
, there are the following environmental restrictions.
this is
I want to make ROS1 coexist with an existing Python project (Python3) using pip
.
b. I want to try rospy
on MacOS for a moment
It is inconvenient in such cases.
There is also roslibpy as a solution, but since rosbridge bridges the communication once with websocket, It takes a lot of communication overhead. Therefore, the ROS code related to rospy
is written only in Python, and by preparing PyPI (Python Package Index) that can install rospy
as a pure Python package, even in Python3 I tried to use rospy
.
We have confirmed in the following environment.
Here is a sample of how to use virtualenv
. No ROS installation required.
(Basically the same for pipenv
etc.)
virtualenv -p python3 venv
. ./venv/bin/activate
pip install --extra-index-url https://rospypi.github.io/simple rospy-all
If you want to use TF2 or cv_bridge, add more
pip install --extra-index-url https://rospypi.github.io/simple cv_bridge tf2_ros
You can do it.
Prepare the following sample
talker.py
import rospy
import std_msgs.msg
rospy.init_node("talker")
pub = rospy.Publisher("chat", std_msgs.msg.String, queue_size=1)
rate = rospy.Rate(2)
while not rospy.is_shutdown():
pub.publish("hello")
rate.sleep()
listner.py
import rospy
import std_msgs.msg
def callback(msg):
print(msg.data)
rospy.init_node("listener")
rospy.Subscriber("chat", std_msgs.msg.String, callback)
rospy.spin()
Install additional rosmaster
pip install --extra-index-url https://rospypi.github.io/simple rosmaster defusedxml
From each of the three terminals
First, start rosmaster.
. ./venv/bin/activate
rosmaster --rcore
Next, start the talker node.
. ./venv/bin/activate
export ROS_MASTER_URI=http://localhost:11311
python talker.py
Finally, start the listener node.
. ./venv/bin/activate
export ROS_MASTER_URI=http://localhost:11311
python listener.py
If you can see the Subscirbe topic in the last listener node for the following, it is successful.
$ python listener.py
WARNING: cannot load logging configuration file, logging is disabled
hello
hello
hello
hello
hello
Now you can write ROS nodes only in Python 3 without installing ROS. We have confirmed that the same thing can be done on MacOS.
I'm doing something like that.
The PyPI I prepared this time was made as a hobby by myself, and I don't know how much it can support, but I think it's convenient to use for a while, so please give it a try. https://github.com/rospypi/simple
I want to be able to release it officially ...
Let's enjoy robot programming!
Recommended Posts