運行roscore,turtlesim
#include <ros/ros.h>
#include <iomanip>
#include <turtlesim/Pose.h>
#include <ros/console.h>
void poseMessageReceived(const turtlesim::Pose& msg)
{
bool Safe;
int x=(int)msg.x;
int y=(int)msg.y;
if(x > 0 && y > 0 && x < 11 && y < 11)Safe=true;
else Safe=false;
if(Safe)
{
ROS_INFO_STREAM(std::setprecision(2)<<std::fixed<<"position =("<<msg.x<<","<<msg.y<<")"<<" *direction ="<<msg.theta);
}
else
{
std::setprecision(2);
std::fixed;
ROS_WARN_STREAM_COND(x==0&&y==0,"Oh, no! x = "<<msg.x<<",y = "<<msg.y<<", This is bad");
ROS_WARN_STREAM_COND(x==11&&y==11,"Oh, no! x = "<<msg.x<<",y = "<<msg.y<<", This is bad");
ROS_WARN_STREAM_COND(x==0&&y==11,"Oh, no! x = "<<msg.x<<",y = "<<msg.y<<", This is bad");
ROS_WARN_STREAM_COND(x==11&&y==0,"Oh, no! x = "<<msg.x<<",y = "<<msg.y<<", This is bad");
ROS_WARN_STREAM_COND(x==11&&y!=0&&y!=11,"Oh, no! x = "<<msg.x<<", This is bad");
ROS_WARN_STREAM_COND(x==0&&y!=11&&y!=0,"Oh, no! x = "<<msg.x<<", This is bad");
ROS_WARN_STREAM_COND(y==0&&x!=11&&x!=0,"Oh, no! y = "<<msg.y<<", This is bad");
ROS_WARN_STREAM_COND(y==11&&x!=11&&x!=0,"Oh, no! y = "<<msg.y<<", This is bad");
}
}
int main(int argc,char **argv)
{
ros::init(argc,argv,"subscribe_to_pose");
ros::NodeHandle n;
ros::Subscriber sub = n.subscribe("turtle1/pose",1000,&poseMessageReceived);
ros::spin();
}