ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | Q&A
Ask Your Question

Load a urdf/xml file as rosparm from a c++ ros node

asked 2018-02-01 10:12:44 -0600

vacky11 gravatar image

updated 2018-02-01 12:47:07 -0600

I have followed the rosparam tutorial. But it dosent show how to load a xml/urdf file as a rosparam. Specifically, I want to set "robot_description" parameter from a c++ ros node not from a launch file.

edit retag flag offensive close merge delete


How about using a launch file?

jayess gravatar image jayess  ( 2018-02-01 13:08:03 -0600 )edit

I have mentioned it clearly in the question that I am not looking to set the parameter from a launch file. It is not suitable for my purpose.

vacky11 gravatar image vacky11  ( 2018-02-01 13:11:16 -0600 )edit

Yeah, guess I missed that. Why not?

jayess gravatar image jayess  ( 2018-02-01 13:18:04 -0600 )edit

The reason is that I get the 'robot_name' from a rostopic inside the ros node and then I need to load the appropriate urdf file as parameter:"robot_description", depending on the robot_name

vacky11 gravatar image vacky11  ( 2018-02-01 13:30:05 -0600 )edit

2 Answers

Sort by ยป oldest newest most voted

answered 2018-02-01 14:11:32 -0600

vacky11 gravatar image

updated 2018-06-04 12:52:13 -0600


Finally, this is how im doing it. You just need to read urdf file as a text file and store its content in a string and then set the string as a parameter

// Parse the urdf/xml file
std::string urdf_file_name = "/home/.../urdf_file_name.urdf"
std::ifstream f(urdf_file_name); 
std::stringstream ss;
ss << f.rdbuf();

// Set param
nh.setParam("robot_description", ss.str());
edit flag offensive delete link more


If you're not actually using the parsed urdf, why use tinyxml? A urdf is just a text file.

gvdhoorn gravatar image gvdhoorn  ( 2018-02-02 04:28:54 -0600 )edit

@gvdhoorn I didnt realize this. I'll try to read urdf file as a simple text file and will update my answer if that works

vacky11 gravatar image vacky11  ( 2018-02-05 10:53:32 -0600 )edit

@gvdhoorn you were absolutely correct. Thanks for your input.

vacky11 gravatar image vacky11  ( 2018-02-12 10:17:55 -0600 )edit

@vacky11 Could you please mention what is asset_urdf? Thanks.

NiranjanDeshpande gravatar image NiranjanDeshpande  ( 2018-04-01 10:12:53 -0600 )edit

@NiranjanDeshpande it is the urdf file name that needs to be set as a ros parameter. I will update my answer so that its intuitive

vacky11 gravatar image vacky11  ( 2018-05-22 14:07:02 -0600 )edit

Can you please update your full code. I code it like you, but there comes an error that 'youbot_arm' was not declared in this scope. youbot_arm is the name of my urdf file.

JonasG gravatar image JonasG  ( 2018-06-04 10:43:28 -0600 )edit

If I code it this way:

std::ifstream u ("/home/.../youbot_arm.urdf");

It cannot open the file.

JonasG gravatar image JonasG  ( 2018-06-04 11:16:04 -0600 )edit

@JonasG I have updated my answer. Hope that helps!

vacky11 gravatar image vacky11  ( 2018-06-04 12:52:50 -0600 )edit

answered 2018-02-01 12:43:16 -0600

updated 2018-02-01 13:25:41 -0600

rospy parameter API:

roscpp parameter API:

rospy example (note, I have not tried running this code):

import rospy


# Open .urdf and save contents as str
urdf_filename = "robot.urdf"
urdf_string = None
with open(urdf_filename) as urdf_file:  
    urdf_string =  

# Load urdf_string onto parameter server
if urdf_string is not None:
    rospy.set_param('robot_description', urdf_filename)

# Check
if not rospy.has_param('robot_description'):
    rospy.login("Cannot find 'robot_description' on parameter server")
    urdf_string_param = rospy.get_param('robot_description')
    rospy.login(urdf_string_param )
edit flag offensive delete link more


@josephcoombe Thanks for your answer but Im looking for c++ implementation. Can you please give me a example for the same in c++?

vacky11 gravatar image vacky11  ( 2018-02-01 12:49:09 -0600 )edit

@vacky11 I've updated my answer with a link to the roscpp equivalent.

josephcoombe gravatar image josephcoombe  ( 2018-02-01 13:26:27 -0600 )edit

Hey. I am looking for that c++ implementation too. I don't know how to make a string out of my urdf.file. Can you please help me out? The rest of the code is clear.


JonasG gravatar image JonasG  ( 2018-05-22 13:05:05 -0600 )edit
josephcoombe gravatar image josephcoombe  ( 2018-05-22 13:33:16 -0600 )edit

But I don't know what is meant with asset_urdf. I tried this but it doesn't work.

JonasG gravatar image JonasG  ( 2018-05-22 13:52:40 -0600 )edit

@JonasG it is urdf file name.

vacky11 gravatar image vacky11  ( 2018-05-22 14:09:10 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2018-02-01 10:12:44 -0600

Seen: 1,813 times

Last updated: Jun 04 '18