Robotics StackExchange | Archived questions

Importing python3 library

Hello,

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): https://pynq.readthedocs.io/en/v2.0/pynq_libraries/grove.html
imu = Grove_IMU(mb_info, gr_pin)
imu.reset()

SampleFreq=30
delay=1/SampleFreq

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

To launch this python script, I have to do it with $ sudo python3.6 imu_reader.py. 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:

sensor.msg:

float32 x
float32 y
float32 z

imu_data.msg:

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

imu.py

#!/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()
        #rospy.loginfo(hello_str)
        #pub.publish(hello_str)
        num = num + 1
        if num>251:
            num=0
        #for i in range(5):
        #    vec.array[i] = num

        imu.accelerator.x = 1
        pub.publish(imu)
        rate.sleep()

if __name__ == '__main__':
    try:
        imu_loop()
    except rospy.ROSInterruptException:
        pass

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 imu_pynq.py. However, as expected, it threw the error RuntimeError: Root permission needed by the library. So, would it even be possible to directly incorporate these libraries into ROS?

Thanks for the help.

Asked by Ariel on 2020-10-30 08:11:51 UTC

Comments

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

Asked by Ariel on 2020-10-30 09:15:07 UTC

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.

Asked by roboav8r on 2020-10-30 09:47:56 UTC

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....

Asked by Ariel on 2020-11-02 04:30:59 UTC

Answers

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

Asked by duck-development on 2020-11-04 11:14:43 UTC

Comments