本文已参与「新人创作礼」活动,一起开启掘金创作之路。
因为要做一次关于ROS的分享,于是撰写了一下分享内容。 ROS是一个机器人平台,在ROS中包含了各种各样的功能算法,各个功能算法之间通过消息进行传递数据。
- 在ROS中编写一个C++工程 编写一个
hello world!
首先新建一个文件夹catkin_ws
,初始化工作区 catkin_init_workspace
下面就可以在这个工程中编写代码了,此时新建一个src
文件夹用于存放自己的功能包。
在一个catkin_make工程中,一个功能包包含了include、src
文件夹和CMakeLists.txt、package.xml
文件。可以通过命令catkin_create_pkg
创建一个功能包的模板。下面创建一个名为 hello
的功能包,其中需要依赖功能包 roscpp
(ROS中的C++库)。
catkin_create_pkg hello roscpp
在该功能包的src
文件夹中,新建一个hello.cpp
文件,用于编写代码。
#include <ros/ros.h>
int main(int argc, char **argv)
{
ros::init(argc, argv, "hello");
ROS_INFO("hello world!");
return 0;
}
接着修改 CMakeLists.txt
,将 src/hello.cpp
编译成一个名为 hello
的可执行文件。
其中包含了依赖的功能包 roscpp
,然后需要将该功能包链接到我们的 hello
可执行文件上。
cmake_minimum_required(VERSION 3.0.2)
project(hello)
find_package(catkin REQUIRED COMPONENTS
roscpp
)
catkin_package()
include_directories(
# include
${catkin_INCLUDE_DIRS}
)
add_executable(hello src/hello.cpp)
target_link_libraries(hello ${catkin_LIBRARIES})
以上就完成了一个完整的catkin
工程的构建,包含了工作空间catkin_ws
,功能包hello
,可执行文件hello
。
在工作空间下 catkin_make
就可以得到可执行文件 hello
,当包含多个功能包时,也可以使用命令:
catkin_make -DCATKIN_WHITELIST_PACKAGES=hello
完成对指定功能包名的编译。
如果想引入头文件,只需要修改
CMakeLists
中:
include_directories(
include
${catkin_INCLUDE_DIRS}
)
add_executable(hello src/hello.cpp include/hello.h)
- ROS中命令行的使用 以小海龟为例
启动节点管理器(ROS Master)
roscore
运行学习功能包中的小海龟节点
rosrun turtlesim turtlesim_node
运行键盘控制节点
rosrun turtlesim turtle_teleop_key
显示系统计算图的工具
rqt_graph
显示系统所有节点相关信息的指令
列出系统中所有的节点
其中
rosout
采集所有节点的日志信息,提交显示,默认的话题。
列出某个节点的信息
使用rostopic给小海龟发布信息
话题名 + 消息结构(Twist) + 具体数据(Linear 线速度 + angular 角速度)
但pub指令只会发布一次,因此小海龟只会运动一次,
添加频率 -r 10,每秒十次。
查看消息数据结构
再添加一只海龟
rosservice call /spawn "x: 3.0
y: 3.0
theta: 0.0
name: 'turtle2'"
通过 rostopic list 看查看到第二只海龟,也可以控制第二只海龟
记录当前系统中所有话题数据,并在下次复现。
其中 -a 是代表记录所有数据(all),-O是保存成压缩包的形式,后面是名称。默认保存在home文件夹下。
但是在查看.bag文件时,发现只出现.bag.active文件,查找原因可能是电脑在记录信息是没有完成最终录制,虽然可以转换成.bag文件,但是会有部分消息的丢失。
#切换到”xxx.bag.active”文件所在的目录下
rosbag reindex cmd_record.bag.active
rosbag fix cmd_record.bag.active cmd_record.abg
在第二步结束后,除了原来的以.bag.active为后缀的文件之外,还会生成一个以.bag.org.active为后缀的文件,注意该文件只是中间文件,第三部输入的时候不要对该文件进行修复。在第三步结束之后,会生成正常的.bag文件。
- 不同的算法功能包之间的通信(话题与服务)
新建功能包名为 topic_example
,又依赖于 std_msgs
,为通信消息的格式,也可以自定义消息格式。
catkin_create_pkg topic_example roscpp std_msgs
编写 CMakeLists.txt
,这里添加了两个可执行文件,分别为消息的发布和接受。
cmake_minimum_required(VERSION 3.0.2)
project(topic_example)
find_package(catkin REQUIRED COMPONENTS
roscpp
std_msgs
)
catkin_package()
include_directories(
# include
${catkin_INCLUDE_DIRS}
)
add_executable(topic_pub src/publisher.cpp)
target_link_libraries(topic_pub ${catkin_LIBRARIES})
add_executable(topic_sub src/subscriber.cpp)
target_link_libraries(topic_sub ${catkin_LIBRARIES})
#include <ros/ros.h>
#include <std_msgs/Int32.h>
int main(int argc, char **argv)
{
ros::init(argc, argv, "topic_pub");
ros::NodeHandle node_handle;
ros::Publisher pub_number = node_handle.advertise<std_msgs::Int32>("/count",10);// 定义消息发布
ros::Rate rate(1);//发布频率
int number_count = 0;
while(ros::ok()){
std_msgs::Int32 msg; // 定义ROS中消息类型
msg.data = number_count; // 给消息赋值
pub_number.publish(msg); // 发布消息
ros::spinOnce(); // 循环
rate.sleep(); // 指定频率发布
number_count++;
}
return 0;
}
(1)在编写完发布消息的文件后,可以先通过命令行查看是否发送成功。
(2)通过rqt查看是否发送成功
(3)通过回调函数查看消息
#include <ros/ros.h>
#include <std_msgs/Int32.h>
void num_callback(const std_msgs::Int32::ConstPtr& msg){
ROS_INFO("Get Topic Msg:[%d]",msg->data);
}
int main(int argc, char **argv)
{
ros::init(argc, argv, "topic_sub");
ros::NodeHandle node_handle;
ros::Subscriber sub_number = node_handle.subscribe<std_msgs::Int32>("/count",10,num_callback);
ros::spin();
return 0;
}
Topic 发布一个消息后,就直接去执行后面的程序;而Service 调用一个服务,会一直等待结果。因此Topic是异步通信,而Service为同步通信,关于服务通信方式可自行尝试。
- 下载并使用ROS库中的算法功能包,以teb路径规划算法为例。
途径一:从Github中下载源码后编译使用
git clone https://ghproxy.com/https://github.com/rst-tu-dortmund/teb_local_planner.git
安装依赖包:
sudo apt-get install ros-melodic-base-local-planner
sudo apt-get install ros-melodic-costmap-converter
sudo apt-get install ros-melodic-interactive-markers
sudo apt-get install ros-melodic-mbf-costmap-core
sudo apt-get install ros-melodic-mbf-msgs
sudo apt-get install libsuitesparse-dev
sudo apt-get install ros-melodic-libg2o
编译
catkin_make -DCATKIN_WHITELIST_PACKAGES=teb_local_planner
途径二:使用apt-get
安装
sudo apt-get install ros-melodic-teb-local-planner-tutorials
可以发现两种途径安装的位置是不一样的,源码编译安装的会更容易移植和修改。
如果不能使用roscd
需要安装:
sudo apt install ros-melodic-rosbash
安装以下两个算法功能包后就可以运行teb的仿真环境。
sudo apt install ros-melodic-rviz
sudo apt install ros-melodic-global-planner
roslaunch teb_local_planner_tutorials robot_carlike_in_stage.launch