ROS21讲

ROS21讲_第1张图片

话题是管道,消息通过话题传输,订阅和发布是通过管道(话题)
ROS21讲_第2张图片
发出消息后需要得到回应的。一般为配置信息,进行一次配置时候运用服务 。话题是源源不断的进行传输。
ROS21讲_第3张图片

rosnode是看节点的
/rosout是打开roscore之后默认的节点 主要是一些日志信息
rosmsg show + 消息类型 展示你的消息的一些消息格式

1:创建功能空间的流程

mkdir -p ~/catkin_ws/src
cd ~/catkin_ws/src
catkin_init_workspace

编译工作空间

catkin_make

设置环境变量

source devel/setup.bash

检查环境变量

echo $ROS_PACKAGE_PATH

创建带有依赖包的文件

cd~/catkin_ws/src
catkin_create_pkg +名字 +依赖的包
catkin_create_pkg learn roscpp rospy std_msgs geometry_msgs turtlesim

2:订阅和发布

#include
#include

int main(int argc,char **argv)
{
    ros::init(argc,argv,"velocity_publisher"); //节点名字  节点名字不可以重复的
    ros::NodeHandle n;
    //创建一个publisher,发布的话题名字"/turtle1/cmd_vel"(仿真器自带的话题)消息类型为geometry_msgs::Twist
    ros::Publisher turtle_vel_pub=n.advertise<geometry_msgs::Twist>("/turtle1/cmd_vel",10);
    //设置循环频率
    ros::Rate loop_rate(10);
    int cout=0;
    while (ros::ok())
    {
        //初始化geometry_msgs::Twist类型的消息
        geometry_msgs::Twist vel_msg;
        vel_msg.linear.x=0.5;
        vel_msg.angular.z=0.2;
        turtle_vel_pub.publish(vel_msg);
        ROS_INFO("Publsh turtle velocity command [%0.2f m/s,%0.2g rad/s]",vel_msg.linear.x,vel_msg.angular.z);
        loop_rate.sleep();//按照循环频率延时
    }
    return 0;
}

注意CMakeList.txt 需要添加可执行文件和链接文件

add_executable(velocity_publisher src/velocity_publisher.cpp) 
target_link_libraries(velocity_publisher ${catkin_LIBRARIES})

add_executable :将某个程序文件 编译成某个可执行文件的
target_link_libraries :帮助将执行文件链接ROS的一些库进行链接的。

环境配置:
进入文件所在目录:pwd得到目录 然后复制到.bash中。
ROS21讲_第4张图片
ROS21讲_第5张图片

# include
# include"turtlesim/Pose.h"
void poseCallback(const turtlesim::Pose::ConstPtr& msg)
{
    ROS_INFO("Turtle pose: x:%0.6f,y:%0.6f",msg->x,msg->y);
}
int main(int argc,char **argv)
{ 
    //都需要初始化ROS节点  所有ROS进入节点之前都需要节点的初始化
    ros::init(argc,argv,"pose_subscriber");
    ros::NodeHandle n;//创建句柄管理节点的资源
    ros::Subscriber pose_sub=n.subscribe("turtle1/pose",10,poseCallback); //ros::Subscriber这个类创建pose_sub对象 一旦有数据进来,就会订阅信息
    ros::spin(); // 循环等待回调函数
    return 0;
}

3:消息

ROS21讲_第6张图片
ROS21讲_第7张图片

/*
该例程将发布/person_info话题,自定义消息类型learn::Person
*/

#include
#include"learn/Person.h" //devel中的哪个文件

int main(int argc, char **argv)
{
    ros::init(argc, argv, "Person_publisher"); //.cpp节点的名字
    ros::NodeHandle n;
    ros::Publisher person_info_pub=n.advertise<learn::Person>("/person_info",10);
    ros::Rate loop_rate(1);
    int count = 0;
    while (ros::ok())
    {
        learn::Person person_msg;
        person_msg.name ="xiaokui";
        person_msg.age=18;
        person_msg.sex=learn::Person::male;
        person_info_pub.publish(person_msg);//发布消息
        ROS_INFO("Publish Person Info: name:%s age:%d sex:%d",person_msg.name.c_str(),person_msg.age,person_msg.sex);
        loop_rate.sleep();
    }
    return 0;

}
 /*
 订阅/person_info话题  自定义消息类型learn::Person
 */
#include
#include"learn/Person.h"

void personInfoCallback(const learn::Person::ConstPtr& msg)
{
    ROS_INFO("Subcribe Person Info: name:%s age:%d sex:%d",
    msg->name.c_str(),msg->age,msg->sex);
}
int main(int argc , char **argv)
{
    ros::init(argc,argv,"Person_subscriber");
    ros::NodeHandle n;
    ros::Subscriber person_info_sub=n.subscribe("/person_info",10,personInfoCallback);
    ros::spin();
    return 0;
}

4:客户端服务端

ROS21讲_第8张图片

/*
请求/spawn服务 服务数据类型turtlesim::Spawn
*/
#include
#include
int main(int argc,char **argv)
{
    //初始化节点
    ros::init(argc,argv,"turtle_spawn");
    ros::NodeHandle node;
    //发现/spawn服务后,创建一个服务客户端,链接名为/spawn的service
    ros::service::waitForService("/spawn");//等待服务
    ros::ServiceClient add_turtle=node.serviceClient<turtlesim::Spawn>("/spawn");//请求服务
    
    //初始化turtlesim::Spawn的请求数据
    turtlesim::Spawn srv;
    srv.request.x=2.0;
    srv.request.y=2.0;
    srv.request.name="turtle2";


    //请求服务调用
    ROS_INFO("Call service to spwan turtle[x:%0.6f,y:%0.6f,name:%s]",
             srv.request.x,srv.request.y,srv.request.name.c_str());
    add_turtle.call(srv);
    //显示服务调用结果
    ROS_INFO("Spwn turtle successfully [name:%s]",srv.response.name.c_str());
    return 0;
}

ROS21讲_第9张图片

/*
执行/turtle_command服务,服务数据类型为std_srvs/Trigger
*/

#include
#include
#include
ros::Publisher turtle_vel_pub; //全局的publish
bool pubCommand=false;
//service回调函数,输入参数req,输出参数res
bool commandCallback(std_srvs::Trigger::Request  &req,
                     std_srvs::Trigger::Response &res)
{
    pubCommand=!pubCommand;
    //显示请求数据
    ROS_INFO("publish turtle velocity command[%s]",pubCommand==true?"Yes":"No");

    //设置反馈数据
    res.success=true;
    res.message="Change turtle command state!";
    return true;
}
int main(int argc,char **argv)
{
    //ROS节点初始化
    ros::init(argc,argv ,"turtle_command_server");
    ros::NodeHandle n;
    ros::ServiceServer command_service = n.advertiseService("/turtle_command",commandCallback);
    turtle_vel_pub=n.advertise<geometry_msgs::Twist>("/turtle1/cmd_vel",10);
    ROS_INFO("Ready to receive turtle command.");
    ros::Rate loop_rate(10);
    while(ros::ok())
    {
        //查看一次回调函数队列
        ros::spinOnce();
        if(pubCommand)
        {
            geometry_msgs::Twist vel_msg;
            vel_msg.linear.x=0.5;
            vel_msg.angular.z=0.2;
            turtle_vel_pub.publish(vel_msg);
        }
        loop_rate.sleep();
    }
    return 0;
}

你可能感兴趣的:(slam,slam)