ROS编程示例---完整输出乌龟位姿

运行roscore,turtlesim

//订阅turtle位姿并输出到屏幕
#include 
#include 
#include //使用了turtlesim下的话题pose
#include //使用判断输出语句_COND

//回调函数
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 =("<","<")"<<" *direction ="<else
  {
    //警示的内容包括四个角和四条边
    //设置输出格式
    std::setprecision(2);
    std::fixed;
    //左下角警示
    ROS_WARN_STREAM_COND(x==0&&y==0,"Oh, no! x = "<",y = "<", This is bad");
    //右上角警示
    ROS_WARN_STREAM_COND(x==11&&y==11,"Oh, no! x = "<",y = "<", This is bad");
    //左上角警示
    ROS_WARN_STREAM_COND(x==0&&y==11,"Oh, no! x = "<",y = "<", This is bad");
    //右下角警示
    ROS_WARN_STREAM_COND(x==11&&y==0,"Oh, no! x = "<",y = "<", This is bad");
    //右墙警示
    ROS_WARN_STREAM_COND(x==11&&y!=0&&y!=11,"Oh, no! x = "<", This is bad");
    //左墙警示
    ROS_WARN_STREAM_COND(x==0&&y!=11&&y!=0,"Oh, no! x = "<", This is bad");
    //下墙警示
    ROS_WARN_STREAM_COND(y==0&&x!=11&&x!=0,"Oh, no! y = "<", This is bad");
    //上墙警示
    ROS_WARN_STREAM_COND(y==11&&x!=11&&x!=0,"Oh, no! 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);

  //spin函数用于程序下面没有任何操作,要是有操作的话,最好选择spinOnece();
  ros::spin();
}

你可能感兴趣的:(ROS入门)