Ask Your Question
0

code error when trying to publish an JointTrajectory message

asked 2017-07-11 13:36:08 -0500

FábioBarbosa gravatar image

updated 2017-07-11 13:38:44 -0500

Hi!

Right now i am working with a custom robot and i am trying to publish a message by cpp. Which it doesn't work for some reason. The points and joint names i send are correct, (tested publishing via terminal) and i run rostopic info /topic_name and it says that my node is associated with the topic via publisher. But i dont understand why it doesnt work.

Can anyone help?

here is the code:

int main(int argc, char **argv) 
{ 
 ros::init(argc, argv, "trajectory_test_node");
 ros::NodeHandle nh;

 ros::Publisher r_arm_comand_publisher = nh.advertise<trajectory_msgs::JointTrajectory>("/signbot/r_arm_controller/command", 1000);

 // Create a JointTrajectory with all positions set to zero, and command the arm.
 if(ros::ok())
 {
  // Create a message to send.
  trajectory_msgs::JointTrajectory msg;

  // Fill the names of the joints to be controlled.
  msg.joint_names.clear();
  msg.joint_names.push_back("r_shoulder_joint");
  msg.joint_names.push_back("r_top_arm_joint");
  msg.joint_names.push_back("r_elbow_joint");
  msg.joint_names.push_back("r_wrist_joint");
  // Create one point in the trajectory.
  msg.points.resize(1);
  // Resize the vector to the same length as the joint names.
  // Values are initialized to 0.
  msg.points[0].positions.resize(msg.joint_names.size(), 1.0);
  // How long to take getting to the point (floating point seconds).
  msg.points[0].time_from_start = ros::Duration(0.001);

  ROS_INFO_STREAM ("Sending command:\n" << msg);
  r_arm_comand_publisher.publish(msg);

  ros::spinOnce();

  ros::Duration(3).sleep();
 }
 ROS_INFO_STREAM("***** SHUTTING DOWN ********\n");
 return 0;
}

here's pictures of rqt_graph when i run my node image description

and here's a picture of rqt_graph when i plushing via terminal image description

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
0

answered 2017-07-19 12:54:53 -0500

FábioBarbosa gravatar image

i found the solution. I dont known why but ros::Duration(3).sleep(); doesnt help, So, i use loop_rate.sleep();. Like:

  ros::Rate loop_rate(0.2);
  ...
  ....
  r_arm_comand_publisher.publish(msg);

  ros::spinOnce();
  loop_rate.sleep();

in this way i get everything working well.

Thanks for all the help

edit flag offensive delete link more

Your Answer

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

Add Answer

Question Tools

2 followers

Stats

Asked: 2017-07-11 13:36:08 -0500

Seen: 336 times

Last updated: Jul 19 '17