ros study notes (updating in real time on 11.14)

0 Preface

Learning about deep learning cannot just focus on one CV field, but also requires a lot of supporting knowledge, such as imaging, image processing, signal processing, image coding, web, application side; the hardware part is ROS, slam, plc, embedded
As a professional master, there is no need to delve too deeply into the subject. It is more necessary to improve the breadth of knowledge, so I will start learning now.
Most of the content is based on the videos and lecture notes of Teacher Zhao Xuzuo: ros Zhao Xuzuo

1. Install and configure the environment

1.1Virtual machine

Some projects in ros and slam are mostly based on Linux systems, so either dual systems or virtual machines are recommended. It is recommended to use Oracle's virtualbox, which is free and open source and is lighter and faster than vm ware. Then configure the ubuntu system. The current mainstream Linux system is ubuntu. There are many ready-made tutorials on the Internet, remember to install virtualbox tools

1.2ROS installation

First you need to update aptapt is a tool used to search, install, upgrade, and uninstall software or operating systems from Internet repositories. Similar to pip and conda

sudo apt update

Then call, which means to install desktop ros according to the officially recommended configuration.

sudo apt install ros-noetic-desktop-full

Remember to change the source of Taika, Alibaba, Douban, Zhongke, Tsinghua, etc.

sudo sh -c ‘. /etc/lsb-release && echo “deb http://mirrors.tuna.tsinghua.edu.cn/ros/ubuntu/ lsb_release -cs main” > /etc/apt/sources.list.d/ros-latest.list’

1.3 Testing

Start three terminals in Linux and enter respectively:
Command line 1 Type: roscore means starting the ros core. No matter what ros program, cpp and py need to enable ros.

Command line 2, type: rosrun turtlesim turtlesim_node (the graphical interface will pop up at this time)
rosrun xx yy is equivalent to python xx, where xx represents the function package name or can be understood as the project name. Here turtlesim means that we use the turtle package, yy Represents the program name or can be understood as a function name or node name (nodes are very important in ros, equivalent to classes, but I feel that ros is process-oriented, and nodes are only equivalent to the triggers of a function). Here turtlesim_node represents summoning a turtle. .

Command line 3: rosrun turtlesim turtle_teleop_key
is the same as above, except that turtle_teleop_key represents keyboard input and can control the turtle to walk.
Although the three commands are entered sequentially, the coupling in the terminal is too strong, so three terminals are required to control them separately. It got better after I learned launch later. Launch is similar to bash.
Insert image description here

2.Communication

Three communication modes: topic communication (publish and subscribe mode), service communication (request response mode), parameter server (parameter sharing mode)

2.1 Topic communication

Insert image description here
The simplest handshake: the publisher publishes the message first, and then the receiver accepts the message. If the receiver does not accept the message, the published message is placed in the queue to wait, and the message in front of the queue is destroyed first.
Published by:

/*
    需求: 实现基本的话题通信,一方发布数据,一方接收数据,
         实现的关键点:
         1.发送方
         2.接收方
         3.数据(此处为普通文本)

         PS: 二者需要设置相同的话题


    消息发布方:
        循环发布信息:HelloWorld 后缀数字编号

    实现流程:
        1.包含头文件 
        2.初始化 ROS 节点:命名(唯一)
        3.实例化 ROS 句柄
        4.实例化 发布者 对象
        5.组织被发布的数据,并编写逻辑发布数据

*/
// 1.包含头文件 
#include "ros/ros.h"
#include "std_msgs/String.h" //普通文本类型的消息
#include <sstream>

int main(int argc, char  *argv[])
{
    
       
    //设置编码
    setlocale(LC_ALL,"");//简中

    //2.初始化 ROS 节点:命名(唯一)
    // 参数1和参数2 后期为节点传值会使用
    // 参数3 是节点名称,是一个标识符,需要保证运行后,在 ROS 网络拓扑中唯一
    ros::init(argc,argv,"talker");
    //3.实例化 ROS 句柄
    ros::NodeHandle nh;//该类封装了 ROS 中的一些常用功能

    //4.实例化 发布者 对象
    //泛型: 发布的消息类型
    //参数1: 要发布到的话题
    //参数2: 队列中最大保存的消息数,超出此阀值时,先进的先销毁(时间早的先销毁)
    ros::Publisher pub = nh.advertise<std_msgs::String>("chatter",10);

    //5.组织被发布的数据,并编写逻辑发布数据
    //数据(动态组织)
    std_msgs::String msg;
    // msg.data = "你好啊!!!";
    std::string msg_front = "Hello 你好!"; //消息前缀
    int count = 0; //消息计数器

    //逻辑(一秒10)
    ros::Rate r(1);

    //节点不死
    while (ros::ok())
    {
    
    
        //使用 stringstream 拼接字符串与编号
        std::stringstream ss;
        ss << msg_front << count;
        msg.data = ss.str();
        //发布消息
        pub.publish(msg);
        //加入调试,打印发送的消息
        ROS_INFO("发送的消息:%s",msg.data.c_str());

        //根据前面制定的发送贫频率自动休眠 休眠时间 = 1/频率;
        r.sleep();
        count++;//循环结束前,让 count 自增
        //暂无应用
        ros::spinOnce();
    }


    return 0;
}

receiver

/*
    需求: 实现基本的话题通信,一方发布数据,一方接收数据,
         实现的关键点:
         1.发送方
         2.接收方
         3.数据(此处为普通文本)


    消息订阅方:
        订阅话题并打印接收到的消息

    实现流程:
        1.包含头文件 
        2.初始化 ROS 节点:命名(唯一)
        3.实例化 ROS 句柄
        4.实例化 订阅者 对象
        5.处理订阅的消息(回调函数)
        6.设置循环调用回调函数

*/
// 1.包含头文件 
#include "ros/ros.h"
#include "std_msgs/String.h"

void doMsg(const std_msgs::String::ConstPtr& msg_p){
    
    
    ROS_INFO("我听见:%s",msg_p->data.c_str());
    // ROS_INFO("我听见:%s",(*msg_p).data.c_str());
}
int main(int argc, char  *argv[])
{
    
    
    setlocale(LC_ALL,"");
    //2.初始化 ROS 节点:命名(唯一)
    ros::init(argc,argv,"listener");
    //3.实例化 ROS 句柄
    ros::NodeHandle nh;

    //4.实例化 订阅者 对象
    ros::Subscriber sub = nh.subscribe<std_msgs::String>("chatter",10,doMsg);
    //5.处理订阅的消息(回调函数)

    //     6.设置循环调用回调函数
    ros::spin();//循环读取接收的数据,并调用回调函数处理

    return 0;
}

When executing a cpp program, you must first write the configuration file, compile it first, and then run it.

2.2msg packaging

Without packaging, you can only use fields of a single data type when using std_msgs.
ros::Subscriber sub = nh.subscribe<std_msgs::String>(“chatter”,10,doMsg);
ros::Publisher pub = nh.advertise<std_msgs ::String>("chatter",10);

Guess you like

Origin blog.csdn.net/qq_41950533/article/details/127641948