GetModelState gives 0.
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;
}