ros2笔记

Ros2

hello world c++

bash 复制代码
mkdir -p {your workspace name}/src
cd {your workspace name} #进入工作空间
colcon build #编译

create package

in {your workspace name}/src

bash 复制代码
ros2 pkg create {your package name} --build-type ament-cmake --dependencies rclcpp	 --node-name helloworld

helloworld.cpp

cpp 复制代码
#include "rclcpp/rclcpp.hpp"

int main(int argc,char ** argv)
{
  rclcpp::init(argc,argv);

  auto node = rclcpp::Node::make_shared("helloworld_node");

  RCLCPP_INFO(node->get_logger(),"hello world!");

  rclcpp::shutdown();
  return 0; 
}

if you want to add some dependencies you can like this

this is CMakeLists 's explain

c 复制代码
# 引入外部依赖包
find_package(rclcpp REQUIRED)
# 映射源文件与可执行文件
add_executable(helloworld src/helloworld.cpp)

# 设置目标依赖库
ament_target_dependencies(
  helloworld
  "rclcpp"
)
# 定义安装规则
install(TARGETS helloworld
  DESTINATION lib/${PROJECT_NAME})

build

in your workspace

bash 复制代码
colcon build

if you meet this situation
you must use pip install catkin_pkg

pip swapping source

pip config set global.index-url --site https://pypi.tuna.tsinghua.edu.cn/simple

run

you must in your workspace

bash 复制代码
source ./install/setup.bash 
ros2 run {your package name} {your node name} 

hello world python

in {your workspace name}/src

bash 复制代码
ros2 pkg create {your package name} --build-type ament_python --dependencies rclpy --node-name {your node name}

helloworld.py

python 复制代码
import rclpy

def main():
    rclpy.init();

    node = rclpy.create_node("hello")

    node.get_logger().info("hello python")

    rclpy.shutdown()

if __name__ == '__main___':

    main()

setup.py

run

bash 复制代码
colcon build
. install/setup.bash 
ros2 run {your paceage name} {your node name} 

operational optimization

bash 复制代码
echo "source /{your work space path}/install/setup.bash" >> ~/.bashrc

file explain

WorkSpace --- 自定义的工作空间。
    |--- build:存储中间文件的目录,该目录下会为每一个功能包创建一个单独子目录。
    |--- install:安装目录,该目录下会为每一个功能包创建一个单独子目录。
    |--- log:日志目录,用于存储日志文件。
    |--- src:用于存储功能包源码的目录。
        |-- C++功能包
            |-- package.xml:包信息,比如:包名、版本、作者、依赖项。
            |-- CMakeLists.txt:配置编译规则,比如源文件、依赖项、目标文件。
            |-- src:C++源文件目录。
            |-- include:头文件目录。
            |-- msg:消息接口文件目录。
            |-- srv:服务接口文件目录。
            |-- action:动作接口文件目录。
        |-- Python功能包
            |-- package.xml:包信息,比如:包名、版本、作者、依赖项。
            |-- setup.py:与C++功能包的CMakeLists.txt类似。
            |-- setup.cfg:功能包基本配置文件。
            |-- resource:资源目录。
            |-- test:存储测试相关文件。
            |-- 功能包同名目录:Python源文件目录。
		|-- C++或Python功能包
		    |-- launch:存储launch文件。
		    |-- rviz:存储rviz2配置相关文件。
		    |-- urdf:存储机器人建模文件。
		    |-- params:存储参数文件。
		    |-- world:存储仿真环境相关文件。
		    |-- map:存储导航所需地图文件。
		    |-- ......

c++ template

c++ 复制代码
#include "rclcpp/rclcpp.hpp"

class MyNode: public rclcpp::Node{
public:
    MyNode():Node("node_name"){
        RCLCPP_INFO(this->get_logger(),"hello world!");
    }

};

int main(int argc, char *argv[])
{
    rclcpp::init(argc,argv);
    auto node = std::make_shared<MyNode>();
    rclcpp::shutdown();
    return 0;
}

python template

python 复制代码
import rclpy
from rclpy.node import Node

class MyNode(Node):
    def __init__(self):
        super().__init__("node_name_py")
        self.get_logger().info("hello world!")
def main():

    rclpy.init()
    node = MyNode() 
    rclpy.shutdown()

package.xml

1.根标签
<package>:该标签为整个xml文件的根标签,format属性用来声明文件的格式版本。

2.元信息标签
<name>:包名;

<version>:包的版本号;

<description>:包的描述信息;

<maintainer>:维护者信息;

<license>:软件协议;

<url>:包的介绍网址;

<author>:包的作者信息。

3.依赖项
<buildtool_depend>:声明编译工具依赖;

<build_depend>:声明编译依赖;

<build_export_depend>:声明根据此包构建库所需依赖;

<exec_depend>:声明执行时依赖;

<depend>:相当于<build_depend>、<build_export_depend>、<exec_depend>三者的集成;

<test_depend>:声明测试依赖;

<doc_depend>:声明构建文档依赖。

CMakeLists.txt

c 复制代码
# 引入外部依赖包
find_package(rclcpp REQUIRED)

# 映射源文件与可执行文件
add_executable(helloworld src/helloworld.cpp)
# 设置目标依赖库
ament_target_dependencies(
  helloworld
  "rclcpp"
)
# 定义安装规则
install(TARGETS helloworld
  DESTINATION lib/${PROJECT_NAME})

setup.py

python 复制代码
from setuptools import setup

package_name = 'pkg02_helloworld_py'

setup(
    name=package_name, # 包名
    version='0.0.0',   # 版本
    packages=[package_name], # 功能包列表
    data_files=[ #需要被安装的文件以及安装路径
        ('share/ament_index/resource_index/packages',
            ['resource/' + package_name]),
        ('share/' + package_name, ['package.xml']),
    ],
    install_requires=['setuptools'], # 安装依赖
    zip_safe=True,
    maintainer='ros2', # 维护者
    maintainer_email='ros2@todo.todo', # 维护者 email
    description='TODO: Package description', # 包描述
    license='TODO: License declaration', # 软件协议
    tests_require=['pytest'], # 测试依赖
    entry_points={
        'console_scripts': [
            # 映射源文件与可执行文件
            'helloworld = pkg02_helloworld_py.helloworld:main'
        ],
    },
)

ros2 cmd

create

python 复制代码
ros2 pkg create 包名 --build-type 构建类型 --dependencies 依赖列表 --node-name 可执行程序名称

格式解释:

--build-type:是指功能包的构建类型,有cmake、ament_cmake、ament_python三种类型可选;

--dependencies:所依赖的功能包列表;

--node-name:可执行程序的名称,会自动生成对应的源文件并生成配置文件。

build

build all

bash 复制代码
colcon build

build select

bash 复制代码
colcon build --packages-select 功能包列表

find

bash 复制代码
ros2 pkg executables [包名] # 输出所有功能包或指定功能包下的可执行程序。
ros2 pkg list # 列出所有功能包
ros2 pkg prefix 包名 # 列出功能包路径
ros2 pkg xml # 输出功能包的package.xml内容

run

ros2 run 功能包 可执行程序 参数

install

sudo apt install ros-ROS2版本代号-功能包名称

interfaces

msg file

int64 num1
int64 num2

srv file

int64 num1
int64 num2
---
int64 sum

upper part is used to declare request and the lower part is response

action file

int64 num
---
int64 sum
---
float64 progress

request response and feedback

Topic

bash 复制代码
ros2 pkg create cpp01_topic --build-type ament_cmake --dependencies rclcpp std_msgs base_interfaces_demo
ros2 pkg create py01_topic --build-type ament_python --dependencies rclpy std_msgs base_interfaces_demo
相关推荐
ct102703852710 小时前
ROS2使用C++开发动作通信
c++·ros2
LSG_Dawn15 小时前
ROS2 分布式 及 ssh远程控制 和 上传下载文件或文件夹
ros2·dds
LSG_Dawn3 天前
1.4 ROS2集成开发环境搭建
ros2
Niu战士5 天前
Ros2学习中的话题通信-自定义接口消息:在VScode中不能补全的问题
vscode·嵌入式硬件·ros2
Mr.Winter`7 天前
ROS2从入门到精通2-2:详解机器人3D可视化工具Rviz2与案例分析
3d·机器人·自动驾驶·ros·ros2
笨小古13 天前
ROS2学习资源
机器人·ros2
押波张飞1 个月前
【ROS2大白话】四、ROS2非常简单的传参方式
机器人·ros2
liu-yonggang1 个月前
ros常用环境变量
ros2
彩云的笔记1 个月前
PX4 ROS2 真机
px4·ros2