- 环境配置
参考官方配置教程,我是创建Ubuntu18.04虚拟机并通过shell脚本配置。
- C++基础、Linux基础
可参考我的其他笔记。
- 创建工作空间
创建Catkin工作空间
cd ~/
mkdir --parents catkin_ws/src
cd catkin_ws
初始化Catkin工作空间
catkin init
构建Catkin工作空间
catkin build
ls
使工作空间对ROS可见。 在devel目录中获取配置文件。
source devel/setup.bash
每次开启终端需要重新获取
或运行以下命令,以后开启终端时会自动获取
echo "source ~/catkin_ws/devel/setup.bash" >> ~/.bashrc
- 安装程序包
ROS的许多最酷,最有用的功能已经存在于其社区中。
通常,稳定资源以易于下载的debian软件包形式存在。
或者,某些资源的测试较少,或者更“尖端”,并且尚未达到稳定的发布状态。 您仍然可以通过从资源库(通常位于Github中)下载它们来访问其中的许多资源。 与debian软件包相比,获取这些git软件包需要更多的步骤。
我们将访问两种类型的软件包并将其安装在我们的系统上。
- 要使用的某种消息类型。 稳定的ROS软件包称为:
Calibration_msgs (using apt-get)
sudo apt install ros-melodic-calibration-msgs
roscd calibration_msgs
sudo apt remove ros-melodic-calibration-msgs //以后用不着此程序包-。-!
- 使用AR标签,但出于测试目的,您希望节点发布类似信息:
fake_ar_publisher (from git)
cd ~/catkin_ws/src
git clone https://github.com/jmeyer1292/fake_ar_publisher.git
catkin build
source ~/catkin_ws/devel/setup.bash
rospack find fake_ar_publisher
- 创建包和节点
ROS通信的基础是称为节点(Node)的多个可执行文件正在一个环境中运行,并以各种方式相互通信。 这些节点存在于称为包(Package)的结构中。
创建软件包
所有软件包都应创建在工作空间的/src目录下。
cd ~/catkin_ws/src
catkin create pkg myworkcell_core --catkin-deps roscpp
--catkin-deps,指定新创建的软件包所依赖的软件包
cd myworkcell_core
gedit package.xml
创建节点
1. 在包文件夹中,使用gedit编辑CMakeLists.txt文件。 浏览示例规则,并添加一个可执行文件(add_executable),名为vision_node的节点,名为vision_node.cpp的源文件。 另外,在CMakeLists.txt中,确保将新的vision_node链接(“ target_link_libraries”)链接到catkin库。(在CMakeLists.txt中添加下列代码)
add_compile_options(-std=c++11)
add_executable(vision_node src/vision_node.cpp)
target_link_libraries(vision_node ${catkin_LIBRARIES})
2. 在功能包文件夹下,创建文件src/vision_node.cpp (using gedit).
/**
** Simple ROS Node
**/
#include <ros/ros.h>
int main(int argc, char* argv[])
{
// This must be called before anything else ROS-related
ros::init(argc, argv, "vision_node");
// Create a ROS node handle
ros::NodeHandle nh;
ROS_INFO("Hello, World!");
// Don't exit the program.
ros::spin();
}
ROS_INFO只是众多记录方法的其中一个。
- 它将消息打印到终端输出,并将其发送到/ rosout主题,以供其他节点监视。
- 日志记录分为5个级别:DEBUG,INFO,WARNING,ERROR和FATAL。
- 要使用其他日志记录级别,请用适当的级别替换ROS_INFO或ROS_INFO_STREAM中的INFO.
- 使用ROS_INFO进行printf样式的日志记录,并使用ROS_INFO_STREAM进行cout样式的日志记录.
catkin build
运行节点
roscore
source ~/catkin_ws/devel/setup.bash
rosrun myworkcell_core vision_node
- 主题与讯息
我们将探讨的第一类ROS通信是一种称为消息的单向通信,该消息是通过称为主题的通道发送的。 通常,一个节点发布有关主题的消息,而另一个节点订阅有关同一主题的消息。(topic/message)
创建ROS订阅者
将fake_ar_publisher软件包添加为依赖项
1.Edit your package’s CMakeLists.txt file (~/catkin_ws/src/myworkcell_core/CMakeLists.txt). Make the following changes in the matching sections of the existing template file, by uncommenting and/or editing existing rules.
1.1. Tell cmake to find the fake_ar_publisher package:
## Find catkin macros and libraries
## if COMPONENTS list like find_package(catkin REQUIRED COMPONENTS xyz)
## is used, also find other catkin packages
find_package(catkin REQUIRED COMPONENTS
roscpp
fake_ar_publisher
)
1.2.Add The catkin runtime dependency for publisher.
## The catkin_package macro generates cmake config files for your package
## Declare things to be passed to dependent projects
## LIBRARIES: libraries you create in this project that dependent projects also need
## CATKIN_DEPENDS: catkin_packages dependent projects also need
## DEPENDS: system dependencies of this project that dependent projects also need
catkin_package(
# INCLUDE_DIRS include
# LIBRARIES myworkcell_core
CATKIN_DEPENDS
roscpp
fake_ar_publisher
# DEPENDS system_lib
)
1.3.Uncomment/edit the add_dependencies line below your add_executable rule:
add_dependencies(vision_node ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
2.add dependencies into your package’s package.xml:
<depend>fake_ar_publisher</depend>
- Build & Source
运行Publisher节点
rosrun fake_ar_publisher fake_ar_publisher_node
创建Subscriber节点
编辑vision_node.cpp文件,添加
#include <fake_ar_publisher/ARMarker.h>
class Localizer
{
public:
Localizer(ros::NodeHandle& nh)
{
ar_sub_ = nh.subscribe<fake_ar_publisher::ARMarker>("ar_pose_marker", 1,
&Localizer::visionCallback, this);
}
void visionCallback(const fake_ar_publisher::ARMarkerConstPtr& msg)
{
last_msg_ = msg;
ROS_INFO_STREAM(last_msg_->pose.pose);
}
ros::Subscriber ar_sub_;
fake_ar_publisher::ARMarkerConstPtr last_msg_;
};
int main(int argc, char** argv)
{
...
// The Localizer class provides this node's ROS interfaces
Localizer localizer(nh);
ROS_INFO("Vision node starting");
...
}
Run catkin
build, then rosrun
myworkcell_core
vision_node
运行rqt_graph,可以看到如下节点关系