在主函数中定义subscribe如下:
ros::NodeHandle nh;
ros::Subscriber model_sub = nh.subscribe("/gazebo/model_states", 1, &modelStatesCallback, this);
Callback 函数定义如下,根据模型的名称,可得对应模型的状态:
void modelStatesCallback(const gazebo_msgs::ModelStatesConstPtr &msg)
{
int modelCount = msg->name.size();
for(int modelInd = 0; modelInd < modelCount; ++modelInd)
{
if(msg->name[modelInd] == "MODEL_NAME")
{
geometry_msgs::Pose pose = msg->pose[modelInd];
break;
}
}
}