I asked this over at ROS forums as well, but I'm in a bit of a rush in this situation. Hi, everyone. I'm currently trying to use gazebo/set_ model_state in a C++ program to set a robot's velocity. My preliminary code is below:
#include "ros/ros.h"
#include <gazebo_msgs/SetModelState.h>
#include <cstdlib>
//I'm going to implement user-specified twist values later.
int main (int argc, char **argv)
{
ros::init(argc,argv,"robot_mover_mark_two");
ros::NodeHandle n;
ros::ServiceClient client = n.serviceClient<gazebo_msgs::SetModelState>("/gazebo/set_model_state");
gazebo_msgs::SetModelState setmodelstate;
gazebo_msgs::ModelState modelstate;
modelstate.model_name = "my_robot";
modelstate.reference_frame = "world";
geometry_msgs::Twist model_twist;
model_twist.linear.x = 1.0;
model_twist.linear.y = 1.0;
model_twist.linear.z = 0.0;
model_twist.angular.x = 0.25;
model_twist.angular.y = 0.0;
model_twist.angular.z = 0.0;
modelstate.twist = model_twist;
setmodelstate.request.model_state = modelstate;
if (client.call(setmodelstate))
{
ROS_INFO("BRILLIANT!!!");
ROS_INFO("%f, %f",modelstate.pose.position.x,modelstate.pose.position.y);
}
else
{
ROS_ERROR("Failed to call service ");
return 1;
}
return 0;
}
I noticed that every time I run the command, the robot's position is reset to the origin in the Gazebo simulation environment. Are the position parameters set to zero by default if I don't include them in the rosservice call? If so, are there any ways I can use this code and have the model stay in the current position (i.e. just change velocity/twist on the fly)?
Originally posted by K. Zeng on Gazebo Answers with karma: 103 on 2014-06-30
Post score: 0
Original comments
Comment by sammiL on 2017-12-04:
you need one more line:
setmodelstate.request.model_state = modelstate;//request line is important
or you will see the error: Updating ModelState: model [] does not exist
check here: https://answers.ros.org/question/12116/calling-gazebo-service-set_model_state-in-c-code/