Use third party python library

asked 2020-02-09 16:27:47 -0500

Samuel77 gravatar image

updated 2020-02-10 01:58:35 -0500

mgruhler gravatar image

How to include third party python libraries in the ROS? I wanted to use this library https://github.com/boppreh/keyboard Please let me know the step by step procedure to include any python library in ROS and use it. Here is the code which runs fine without rospy in a python environment but does not works in ROS:

import keyboard
import rospy
from ros_basics.msg import Keyboard

def main():
    rospy.init_node('KeyboardData',anonymous=True)

    rate = rospy.Rate(1)
    while not rospy.is_shutdown():
        data = Movement()
        publisher = rospy.Publisher('data',Keyboard,queue_size = 25)


        up = keyboard.is_pressed('up')
        down =keyboard.is_pressed('down')
        right = keyboard.is_pressed('right')
        left = keyboard.is_pressed('left')
        if(up):
          print("Up key pressed")
          data.up = True
          publisher.publish(data)



        rate.sleep()

if __name__ == "__main__":
    try:
        main()
    except rospy.ROSInterruptException:
    pass
edit retag flag offensive close merge delete

Comments

1

runs fine in a python environment

What do you mean by that? It contains a lot of ROS specific stuff. How does this work without ROS then?

Also, are there any error messages?

Pleas tell us how you installed the library, as there are several options shown in the README... Also, what have you tried so far. What is working and what is not...

mgruhler gravatar image mgruhler  ( 2020-02-10 02:01:34 -0500 )edit

I meant to say that it runs fine without rospy and it's stuffs in a python environment but does not work in ROS. I want to install python library or basically install a third party Non -ROS library(Since i am using JETSON TX2i, which has a python library for it's GPIO) from github and use it in ROS. I installed the library using pip install keyboard since it is a non-ROS library. I wanted to know the way to use/install non-ROS libraries of python available in github with ROS. The error given when using ROS along with the library is import error and unexpected token "(" which by the way works fine when i remove all keyboard code.

Samuel77 gravatar image Samuel77  ( 2020-02-10 08:16:34 -0500 )edit

Can you please edit your question by posting the exact error output via copy and paste? Then we wouldn't have to dig around in the dark that much.

So, installation via pip is fine and usually works fine (assuming, you have installed the Python2.7 version, and not the Python3 one).

So, it might be an issue with the wrong python version.

Can you try to open an interactive python2.7 console and see if the import error is there as well?

Last but not least, from the docs of the library:

To avoid depending on X, the Linux parts reads raw device files (/dev/input/input*) but this requries root.

I'm not sure if this is a dealbreaker for you, but this seems a bit overkill and I wouldn't run a script that requires root privileges simply to read from the keyboard. This is a python ...(more)

mgruhler gravatar image mgruhler  ( 2020-02-11 02:58:17 -0500 )edit