Gazebo | Ignition | Community
Ask Your Question

Call gazebo/set_model_state without resetting position

asked 2014-06-30 15:36:46 -0500

K. Zeng gravatar image

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::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 (
    ROS_INFO("%f, %f",modelstate.pose.position.x,modelstate.pose.position.y);
    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)?

edit retag flag offensive close merge delete


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:

sammiL gravatar imagesammiL ( 2017-12-04 19:41:33 -0500 )edit

2 Answers

Sort by ยป oldest newest most voted

answered 2014-07-01 17:56:43 -0500

nkoenig gravatar image

I believe the position is set to zero by default.

You can try to get the model state first:

edit flag offensive delete link more


Thank you. I'm going to try that right now.

K. Zeng gravatar imageK. Zeng ( 2014-07-02 10:41:34 -0500 )edit

answered 2021-08-31 00:28:53 -0500

Is this problem solved? I ran into the same problem

edit flag offensive delete link more
Login/Signup to Answer

Question Tools


Asked: 2014-06-30 15:36:46 -0500

Seen: 6,599 times

Last updated: Aug 31