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

import rospy when calling a python script from a c++ executable

asked 2013-08-05 11:57:33 -0500

nickd gravatar image

updated 2014-01-28 17:17:33 -0500

ngrennan gravatar image

I have a Robai Cyton gamma arm that I'm attempting to connect to with ROS by trying to call a Python script from a C++ program.

What I want to do is use C++ and OpenCV to get a video from an Xbox Kinect. From the kinect video I want to be able to click on a point in the image and send the x-y-z position (after converting them to my robot coordinate system) to a Python Script using OpenRave to figure out the inverse kinematics. After the position of my joint angles are known I want to use ROS to send them to the robot.

Currently, I can send the x-y-z locations and find the inverse kinematics no problem and everything works together nicely when I don't have import rospy in my python script(not trying to send the robot my joint angles). I also can move the robot with a different python script I have so I know I can connect with and move it.

Without the ROS commands to try to publish to the robot when I run the c++ exe I get the joint angles for the different links of my robot.

getIndicesFromJointNames [0] ['link1']
[ 1.          0.78545125  0.62655888  0.96602943  1.01751733  1.06180138
 -0.7132951 ]

When I add the command

import rospy

and subsequently try to publish them to the robot

> rospy.init_node('shoulder_roll')   
> ...
> ...
pub = rospy.Publisher('/theta_2/command', Float64)
pub.publish(Float64(sol.item(1)))
pub = rospy.Publisher('/theta_3/command', Float64)
pub.publish(Float64(sol.item(2)*-1)) 
pub = rospy.Publisher('/theta_4/command', Float64)
pub.publish(Float64(sol.item(3)))

I get the error

 Traceback (most recent call last):
  File "/home/nick/fuerte_workspace/Robai/bin/test.py", line 22, in run
    import rospy
  File "/opt/ros/fuerte/lib/python2.7/dist-packages/rospy/__init__.py", line 49, in <module>
    from .client import spin, myargv, init_node, \
  File "/opt/ros/fuerte/lib/python2.7/dist-packages/rospy/client.py", line 53, in <module>
    import rospy.core
  File "/opt/ros/fuerte/lib/python2.7/dist-packages/rospy/core.py", line 66, in <module>
    from rospy.names import *
  File "/opt/ros/fuerte/lib/python2.7/dist-packages/rospy/names.py", line 86, in <module>
    _mappings = load_mappings(sys.argv)
AttributeError: 'module' object has no attribute 'argv'

Any help or suggestions would be appreciated

Thanks, Nick

edit retag flag offensive close merge delete

Comments

Hi Nick, Did you make any progress on this issue? I'm asking, because I'm receiving the same error while importing rospy in a c++ class. I've already looked into the names.py but the load_mappings object has an 'argv' attribute, so the error-message is kind of misguiding me. Kind regards, Tobi

mdtobi gravatar image mdtobi  ( 2013-12-06 22:31:51 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
2

answered 2013-12-06 23:07:47 -0500

Ok I just found the reason for this failure: You need to add PySys_SetArgv(argc, argv); right after Py_Initialize(); to assign the c++-arguments over to the Python interpreter which otherwise would be started without any arguments. After appending this line, sys.argv will be set and the rospy module would be loaded.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2013-08-05 11:57:33 -0500

Seen: 1,301 times

Last updated: Dec 06 '13