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

Convert rostopic pub into c++ code

asked 2020-06-22 18:42:13 -0500

Calvin gravatar image

How do i convert a rostopic pub command and run it in a C++ script? I would like to pub the gripper angle when i state the position of the robot arm but i cant seam to control the gripper.

rostopic pub gripper_angle std_msgs/UInt16 0-180

Where the 0-180 is the angles and you edit the angle of the gripper you want it to be. Normally the rostopic pub is sent from the terminal. The c++ script will be run from the terminal which will be run using rosrun.

I hope yoou guys can help me. Thanks.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2020-06-22 19:19:35 -0500

Geoff gravatar image

The answer to the question you asked is below. However I think there is a hidden question here. What have you tried so far to make a publisher in C++ and what happened when it didn't work?


Take a look at the tutorial for writing a publisher in C++.

You will need to make the following changes:

  1. Change the name of the node on line 47 to something more appropriate for what you are doing, e.g. "gripper_controller"
  2. Change the topic name on line 73 to be the topic you want to publish to, in this case "gripper_angle".
  3. Change the header file on line 28 to the data type you want to publish, which is "std_msgs/UInt16.h"
  4. Change the type of the Publisher on line 73 to be UInt16.
  5. Remove the while loop and have it just publish data once, with the value you want to publish. If you are trying to make a utility you can call from the command line with an argument, then you should get that value from a command line argument. There are plenty of tutorials on the web about how to parse command line arguments in C++.
edit flag offensive delete link more

Comments

i have coded this but there is not echo from the function clodeGripper().

  ros::NodeHandle node_handle;
  ros::Publisher gripper_angle = node_handle.advertise<std_msgs::UInt16>("angle", 10);
  std_msgs::UInt16 msg;
  msg.data = 180;
  gripper_angle.publish(msg);
Calvin gravatar image Calvin  ( 2020-06-23 04:20:33 -0500 )edit

Please post your complete code, and if there is an error output when compiling it or running it, post that as well. Use a GitHub Gist if it's long.

Geoff gravatar image Geoff  ( 2020-06-23 18:34:29 -0500 )edit

Maybe I can redirect you here, https://answers.ros.org/question/3556...

Calvin gravatar image Calvin  ( 2020-06-23 19:59:40 -0500 )edit
  • That question duplicates this one, with different wording and a little different information. Please do not do that.
  • That question still doesn't include your entire source.
Geoff gravatar image Geoff  ( 2020-06-23 20:26:45 -0500 )edit

I am sorry for the mistake that I have made. I am new to posting questions online and asking for help. this is the link to both the files of the publisher and the subscriber for the question above.

https://github.com/MojoZuikie/eti_mov...

Calvin gravatar image Calvin  ( 2020-06-23 21:25:18 -0500 )edit

The problem is that you are creating a publisher in the closeGripper() and openGripper() functions, using that to publish a message, then exiting the function. It is likely that the publisher is being cleaned up by going out of scope before it even has a chance to publish the message, and even if it publishes the message it probably does so before any subscribers listening to that topic get connected to the publisher. You need to create a single publisher for the topic once in your node's set up, then use that throughout.

Geoff gravatar image Geoff  ( 2020-06-24 19:47:16 -0500 )edit

I found out a way to control the gripper using the code above. Thanks Very much.

Calvin gravatar image Calvin  ( 2020-06-25 00:03:34 -0500 )edit

Question Tools

2 followers

Stats

Asked: 2020-06-22 18:42:13 -0500

Seen: 679 times

Last updated: Jun 22 '20