Robotics StackExchange | Archived questions

publish to gazebo topic from c++ node in ROS

Hello everyone, i'm trying command my robot in gazebo by using a C++ node in ROS, but i didn't find how to get the connexion between my urdf robot in gazebo and my C++ node.

i can make the joint of my robot move by using rqt_gui, by i don't know how to publish some random positions using C++ node in ROS

Please help.

Asked by samo on 2018-06-25 10:05:27 UTC

Comments

Answers

  1. add head file

#include <gazebo/transport/TransportTypes.hh>

  1. add definition of node and Publisher

    transport::NodePtr node; transport::PublisherPtr pub_mydata;

  2. Init node

    transport::NodePtr node(new transport::Node()); node->Init("test"); this->pub_mydata = node->Advertise<msgs::Vector3d>("~/MyData");

  3. Publish data

    msgs::Vector3d msg_Vector3d; msgs::Set(&msg_Vector3d, this->data_from_link); this->pub_mydata->Publish(msg_Vector3d);

  4. Visualize data in gazebo9 ctrl+p

If you don't want to control your robot by pulgin but using ros-control . Then you need to Subscribe your cmd ,and then publish the value to ros-controller you used.

If you want to control your robot by a plugin, for example , model plugin.

  1. You should bind a ros::Subscribe to receive your message.

    ros::SubscribeOptions PositionCommandSo = ros::SubscribeOptions::create<your_msgs::PositionCommand>("~/PositionCommand", 1, boost::bind(&YourPlugin::SetPositionCommand, this, _1),ros::VoidPtr(), &this->rosQueue);

```

void YourPlugin::SetPositionCommand(const your_msgs::PositionCommand::ConstPtr  &_msg)
{
    // TODO
    std::cout<<"recive PositionCommand massage"<<std::endl;
    //Update your cmd value tor control
    this->PID_refer_x = _msg->action[0];
}

```

  1. Update your cmd value to a controller, for example PID.

Asked by longwoo on 2018-06-26 01:20:14 UTC

Comments

hello, my publisher is like that : ros::Publisher pub=n.advertise("/MY_pkg/command",1);

i'm trying to publish on gazebo topic so my robot can move

Asked by samo on 2018-06-26 02:48:31 UTC

i can command my robot like i said before by using rqt_gui, right there i'm choosing /My_pkg/joint1_position_controller/command with a sin(i/100)*0.5 command, and my robot move

My question is how to do the same but by using this publisher to communicate with gazebo

Asked by samo on 2018-06-26 02:52:42 UTC

If you want to control your robot. Then you need a plugin, for example , model plugin. 1. You should bind a ros::Subscribe to receive your message. 2. Update your cmd value to a controller, for example PID.

Asked by longwoo on 2018-06-26 06:22:45 UTC

thank you for your answer. I have another question if you didn't mind, can i be able to publish to each link to control its joint angles? without using plugins

Asked by samo on 2018-06-26 08:09:56 UTC

yes, you can do it by using actionlib. Here is the python tutorial
.http://wiki.ros.org/dynamixel_controllers/Tutorials/Creatingdynamixelactionclient . C++ is similar.

Asked by longwoo on 2018-06-26 21:22:33 UTC