ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Importing python3 library

asked 2020-10-30 08:11:51 -0500

Ariel gravatar image


I want to use some libraries that are python3 based to run on a pynq board with pynq overlay which is python3 based. The goal is to read the sensors from a 10 DOF IMU.

One one side, I have the following python script:

import time
from pynq.overlays.base import BaseOverlay
from pynq.lib.pmod import Grove_IMU
from pynq.lib.pmod import PMOD_GROVE_G3

base = BaseOverlay("base.bit")
mb_info = {'ip_name':'iop_pmoda/mb_bram_ctrl' ,'rst_name':'mb_iop_pmoda_reset'}
gr_pin = PMOD_GROVE_G3  # SDA on 3, SCL on 7 (PMOD A):
imu = Grove_IMU(mb_info, gr_pin)


while True:
    accl = imu.get_accl()
    print("Accelerator: ", accl)
    mag = imu.get_compass()
    print("Magnetometer: ", mag)
    gyro = imu.get_gyro()
    print("Gyroscope: ", gyro, "\n")

To launch this python script, I have to do it with $ sudo python3.6 There it is the first problem, that it needs sudo.

From the ROS side, I have the following custom messages (I know I could have used directly sensor_msgs/Point32) and python script:


float32 x
float32 y
float32 z


imu_sensors/sensor accelerator
imu_sensors/sensor gyroscope
imu_sensors/sensor magnetometer

#!/usr/bin/env python
import rospy
from imu_sensors.msg import sensor
from imu_sensors.msg import imu_data

def imu_loop():
    pub = rospy.Publisher('IMU_output', imu_data, queue_size=10)
    rospy.init_node('talker', anonymous=True)
    rate = rospy.Rate(2) # 10hz
    imu = imu_data()
    num = 0
    while not rospy.is_shutdown():
        #hello_str = "hello world %s" % rospy.get_time()
        num = num + 1
        if num>251:
        #for i in range(5):
        #    vec.array[i] = num

        imu.accelerator.x = 1

if __name__ == '__main__':
    except rospy.ROSInterruptException:

Now I have a problem to incorporate the first python script into ROS. First of all, I am following this tutorial to compile ROS with python3 support. It basically does the following:

## Prerequisits:
### 1. Install basic Python3 packages and some ROS dependencies (there might be more depending on each ROS package)
$ sudo apt-get install python-catkin-tools python3-dev python3-numpy
$ pip install pyaml
$ pip install rospkg
$ pip install numpy
$ pip install empy
$ pip install cffi

### 2. Install Python virtualenv
$ sudo pip install virtualenv

## Procedure
### 1. Create a virtual environment (workspace) and activate it
$ mkdir -p ~/python3_ws/src
$ cd ~/python3_ws/src
$ virtualenv py3venv --python=python3
$ source ~/python3_ws/py3venv/bin/activate

### 2. Install ROS packages required by your ROS code
This is needed when you need some off-the-shelf packages which have to be re-compiled for python3

### 3. Create your ROS package and compile with the specific flag
catkin_make -DPYTHON_EXECUTABLE:FILEPATH=/home/xilinx/python3_ws/py3venv/bin/python

Like this, I am able to compile the ROS python script and I can see that it publishes on the expected topic. The pynq libraries are in the /home/user folder so I added this path to my PYTHONPATH. I only added from pynq.overlays.base import BaseOverlay, recompiled and run it with rosrun imu_sensors However, as expected, it ... (more)

edit retag flag offensive close merge delete


Doing everything under $ sudo su solved the issue. I had to source the virtual environment for python3 but then the topic runs.

Ariel gravatar image Ariel  ( 2020-10-30 09:15:07 -0500 )edit

Which version of ROS and Ubuntu are you using? If python3 compatibility continues to be an issue, you could upgrade to Noetic, which supports python3.

roboav8r gravatar image roboav8r  ( 2020-10-30 09:47:56 -0500 )edit

I am using melodic with ubuntu 18.04. The python3 compatibility does not seem to be an issue with that virtual environment....the need for sudo is the main thing....which I solved with sudo su. I don't think that is recommended though....

Ariel gravatar image Ariel  ( 2020-11-02 03:30:59 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2020-11-04 10:14:43 -0500

duck-development gravatar image

You can run it with Sudo - E to get the users environment.

edit flag offensive delete link more

Question Tools

1 follower


Asked: 2020-10-30 08:11:51 -0500

Seen: 301 times

Last updated: Nov 04 '20