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

rewriting python program in cpp, how to fill in the header field in msg

asked 2011-08-06 01:43:32 -0500

vincent gravatar image

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

ngrennan gravatar image

Hi all,

I am reading the pr2_controllers/Tutorials/Moving the arm through a Cartesian pose trajectory.

At the end of this tutorial, there is a test client program written in python, which send Cartesian trajectories to the service. I got a problem when I was trying to rewrite it in c++.

  13 #execute a Cartesian trajpr2_controllersTutorialsMoving the arm through a Cartesian pose trajectoryectory defined by a root frame, a list of 
  14 #positions (x,y,z), and a list of orientations (quaternions: x,y,z,w)
  15 def call_execute_cartesian_ik_trajectory(frame, positions, orientations):
  16     rospy.wait_for_service("execute_cartesian_ik_trajectory")
  17 
  18     #fill in the header (don't need seq)
  19     header = roslib.msg.Header()
  20     header.frame_id = frame
  21     header.stamp = rospy.get_rostime()

Here the header was filled, I am not sure if I need to do the same thing in c++ and if so, what's the corresponding code that I should be using?

Maybe a very basic question, but I just couldn't find the answer and very confused.
Can someone help me with this? Thank you.

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
5

answered 2011-08-06 07:21:40 -0500

dornhege gravatar image

updated 2011-08-06 07:27:18 -0500

The corresponding code for filling a header would be:

srv.header.frame_id = frame;
srv.header.stamp = ros::Time::now();
edit flag offensive delete link more

Comments

In the above, instead of 'frame' you can fill a string constant as like below:

srv.header.frame_id = "your_frame";
bigbellmercy gravatar image bigbellmercy  ( 2020-05-10 21:58:55 -0500 )edit
0

answered 2011-08-06 12:55:22 -0500

vincent gravatar image

updated 2011-08-06 12:56:42 -0500

Thank you dornhege, but i got another problem here.

This is part of my code for the function "call_execute_cartesian_ik_trajectory"

  bool call_execute_cartesian_ik_trajectory(std::string frame,  double position[6] , double     orientations[8]){
ros::service::waitForService("execute_cartesian_ik_trajectory");
ros::NodeHandle n_;
int i;

ROS_INFO("HERE %f",position[0]);

ros::ServiceClient client=n_.serviceClient<supercontroller_keyboard::ExecuteCartesianIKTrajectory>("execute_cartesian_ik_trajectory");

supercontroller_keyboard::ExecuteCartesianIKTrajectory srv;

srv.request.header.frame_id = frame;
srv.request.header.stamp = ros::Time::now();
srv.request.poses[0].position.x = position[0];

ROS_INFO("the pose[0].position.x is %f",srv.request.poses[0].position.x);

It showed an error when I run it in gdb:

Program received signal SIGSEGV, Segmentation fault.
0x000000000040510c in call_execute_cartesian_ik_trajectory (
frame=<value optimized out>, position=<value optimized out>, 
orientations=<value optimized out>)
at /home/vincent/pr2_newcontroller/supercontroller_keyboard/src/listenertest.cpp:28
28    srv.request.poses[0].position.x = position[0];

ROS_INFO("HERE %f",position[0]); shows that

[ INFO] [1312677933.412527406, 90093.731000000]: HERE 0.760000

There seems to be something wrong when i try to assign a value to srv.request.poses[0].position.x,

the message is defined as:

srv/ExecuteCartesianIKTrajectory.srv:

 Header header
 geometry_msgs/Pose[] poses
 ---
 uint32 success

Do you have any idea where went wrong? BTW, the service program works fine here.

edit flag offensive delete link more

Comments

I think the issue is that poses is a vector, initially with length zero. You're trying to access the first element of the vector which doesn't exist. What you probably want to do instead is create a geometry_msgs::Pose pose and set its field values, then do srv.request.poses.push_back(pose).
Patrick Bouffard gravatar image Patrick Bouffard  ( 2011-08-06 15:59:25 -0500 )edit
Yes it worked, thanks!
vincent gravatar image vincent  ( 2011-08-07 02:30:42 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2011-08-06 01:43:32 -0500

Seen: 3,266 times

Last updated: Aug 08 '11