Home | Tutorials | Wiki | Issues
Ask Your Question
0

publish to gazebo topic from c++ node in ROS

asked 2018-06-25 10:05:27 -0600

samo gravatar image

updated 2018-06-25 11:13:47 -0600

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.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2018-06-26 01:20:14 -0600

longwoo gravatar image

updated 2018-06-26 06:42:28 -0600

  1. add head file

    #include <gazebo/transport/TransportTypes.hh>

  2. add definition of node and Publisher

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

  3. Init node

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

  4. Publish data

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

  5. 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.
edit flag offensive delete link more

Comments

hello, my publisher is like that : ros::Publisher pub=n.advertise<trajectory_msgs::jointtrajectory>("/MY_pkg/command",1); i'm trying to publish on gazebo topic so my robot can move

samo gravatar imagesamo ( 2018-06-26 02:48:31 -0600 )edit

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

samo gravatar imagesamo ( 2018-06-26 02:52:42 -0600 )edit

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.

longwoo gravatar imagelongwoo ( 2018-06-26 06:22:45 -0600 )edit

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

samo gravatar imagesamo ( 2018-06-26 08:09:56 -0600 )edit

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

longwoo gravatar imagelongwoo ( 2018-06-26 21:22:33 -0600 )edit
Login/Signup to Answer

Question Tools

2 followers

Stats

Asked: 2018-06-25 10:05:27 -0600

Seen: 295 times

Last updated: Jun 26 '18