Use rospy with virtualenv in Python3

Introduction

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.

  1. Use Ubuntu
  2. Install ROS
  3. Use the Python2 / library installed on your system

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.

How to use

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

  1. First, start rosmaster.

    . ./venv/bin/activate
    rosmaster --rcore
    
  2. Next, start the talker node.

    . ./venv/bin/activate
    export ROS_MASTER_URI=http://localhost:11311
    python talker.py
    
  3. Finally, start the listener node.

    . ./venv/bin/activate
    export ROS_MASTER_URI=http://localhost:11311
    python listener.py
    
  4. 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.

About the contents

I'm doing something like that.

in conclusion

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

Use rospy with virtualenv in Python3
Use Python in pyenv with NeoVim
Use OpenCV with Python 3 in Window
python in virtualenv
Use config.ini in Python
Use pipdeptree with virtualenv
[Python] Use JSON with Python
Use Valgrind in Python
Use mecab with Python3
Use Python 3.8 with Anaconda
Use python with docker
Use profiler in Python
Use various rabbimq features with pika in python
How to use tkinter with python in pyenv
Work in a virtual environment with Python virtualenv.
Use Python in Anaconda environment with VS Code
Let's use def in python
Use Trello API with python
Use "$ in" operator with mongo-go-driver
Use let expression in Python
Scraping with selenium in Python
Use Measurement Protocol in Python
Working with LibreOffice in Python
Scraping with chromedriver in python
Use callback function in Python
Manage python environment with virtualenv
Use Twitter API with Python
Use parameter store in Python
Debugging with pdb in Python
Use HTTP cache in Python
Working with sounds in Python
Use list-keyed dict in Python
Scraping with Selenium in Python
Use regular expressions in Python
Scraping with Tor in Python
Use Spyder in Python IDE
Tweet with image in Python
Combined with permutations in Python
Use subsonic API with python3
Specify python version with virtualenv
[Python] How to use virtualenv
Use Cursur that closes automatically with sqlite3 in Python
Solution when you want to use cv_bridge with python3 (virtualenv)
Number recognition in images with Python
Python: How to use async with
Use PointGrey camera with Python (PyCapture2)
Testing with random numbers in Python
Use vl53l0x with Raspberry Pi (python)
GOTO in Python with Sublime Text 3
Build python virtual environment with virtualenv
Scraping with Selenium in Python (Basic)
CSS parsing with cssutils in Python
How to use SQLite in Python
[Python] Use Basic/Digest authentication with Flask
Open UTF-8 with BOM in Python
How to use Mysql in python
How to use ChemSpider in Python
How to use FTP with Python
Use Windows 10 speech synthesis with Python
Heatmap with Dendrogram in Python + matplotlib
How to use PubChem in Python