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

GetModelState gives 0.

asked 2021-09-22 13:55:02 -0600

Roshan gravatar image

Hello, I'm trying to get the position of my turtlebot3. The turtlebot is driving around, not at origo. I'm getting:

 [ INFO] [1632336073.528190391]: x = 0.000000 :

As output when i use:

 ROS_INFO("x = %f :",getModelState.response.pose.position.x);

I have also tried getting the info directly from the command line by using:

rosservice call /gazebo/get_model_state tb3_0 world

and then it is working fine, so I don't get why my code isn't giving the right answer. Here's the rest of the code

#include "ros/ros.h"
#include "gazebo_ros_link_attacher/Attach.h" //For custom meldinger 
#include "gazebo_msgs/SpawnModel.h"
#include "gazebo_msgs/GetModelState.h"
#include "gazebo_msgs/ModelState.h"
#include "geometry_msgs/Quaternion.h" 

using namespace std;

int main(int argc,char **argv){
    ros::init(argc,argv,"spawn_marker");/*Initialiserer node med navn*/
    ros::NodeHandle n;
    geometry_msgs::Pose pose;


    ros::ServiceClient spawn_sdf_model_client = n.serviceClient<gazebo_msgs::SpawnModel>("/gazebo/spawn_sdf_model");
    ros::ServiceClient get_model = n.serviceClient<gazebo_msgs::GetModelState>("/gazebo/get_model_state");
    ros::ServiceClient attach_client = n.serviceClient<gazebo_ros_link_attacher::Attach>("link_attacher/attach");

    gazebo_msgs::GetModelState getModelState;
    gazebo_msgs::ModelState modelstate;

    string modelName = "tb3_1" ;
    string relativeEntityName = "world" ;

    getModelState.request.model_name = modelName;
    getModelState.request.relative_entity_name = relativeEntityName;


    //if (get_model.call(getModelState)){
      //  ROS_INFO("X= %lf ;",getModelState.response.pose.position.x);
    //}

    //ROS_INFO("x = %s :",state.response.model_name);

    //pose = 

    //ObjectAvoidance object;

    ros::Rate loop_rate(1); /*Bestemmer loop rate hastighet */

    while (ros::ok()){
      //  object.control();
        ROS_INFO("x = %f :",getModelState.response.pose.position.x);
        loop_rate.sleep();
        ros::spinOnce();
    }

    return 0;
}
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2021-09-23 02:33:43 -0600

tfoote gravatar image

You appear to have the function which is actually going to call the service commented out. aka the line which invokes get_model.call(... Without that you're just printing the initialized value of the struct.

edit flag offensive delete link more

Question Tools

Stats

Asked: 2021-09-22 13:54:13 -0600

Seen: 163 times

Last updated: Sep 23 '21