ROS 学习记录(四)——ROS 自定义头文件/模块,更改日志输出级别

文章目录

自定义 C++ 头文件

当前包名为 libtest,自定义头文件 libtest/include/libtest/hello.h

1#pragma once
2
3#include "ros/ros.h"
4
5namespace Hello
6{
7    void hello();
8}

编辑 .vscode/c_cpp_properties.json 文件,添加头文件搜索路径,确保 IntelliSense 能够识别自定义头文件:

 1{
 2  "configurations": [
 3    {
 4      "browse": {
 5        "databaseFilename": "${default}",
 6        "limitSymbolsToIncludedHeaders": false
 7      },
 8      "includePath": [
 9        "/opt/ros/noetic/include/**",
10        "/home/jackgdn/ros-project/src/**/include/**",  // 将这一项添加到 includePath 中
11        "/usr/include/**",
12        "/home/jackgdn/ros-project/devel/include/**"
13      ],
14      "name": "ROS",
15      "intelliSenseMode": "gcc-x64",
16      "compilerPath": "/usr/bin/gcc",
17      "cStandard": "gnu11",
18      "cppStandard": "c++14"
19    }
20  ],
21  "version": 4
22}

编写 hello_cpp.cpp

 1#include "ros/ros.h"
 2#include "libtest/hello.h"
 3
 4namespace Hello
 5{
 6    void hello()
 7    {
 8        ros::console::set_logger_level(ROSCONSOLE_DEFAULT_NAME, ros::console::levels::Debug); // 设置日志级别
 9        ROS_DEBUG("Hello, World! Hello, C++!");
10    }
11}
12
13int main(int argc, char **argv)
14{
15    ros::init(argc, argv, "hello_cpp");
16    Hello::hello();
17    return 0;
18}

ROS 默认日志级别为 Info,默认情况下 Debug 信息不会显示,因此需要手动设置日志级别使日志能够显示。

随后修改 CMakeLists.txt 文件,添加自定义头文件和模块:

1include_directories(
2include
3  ${catkin_INCLUDE_DIRS}
4)

其余部分与之前相同,编译运行即可。

自定义 Python 模块

scripts 目录下创建 hello.py 作为待导入的模块:

1import rospy
2import logging
3
4
5def hello():
6    logging.getLogger("rosout").setLevel(logging.DEBUG)  # 修改日志级别
7    rospy.logdebug("Hello, world! Hello, Python!")

再创建一个 hello_py.py

 1import os
 2import sys
 3import rospy
 4
 5"""
 6将当前文件所在路径添加到系统路径中
 7以便导入自定义模块
 8"""
 9sys.path.insert(0, os.path.dirname(__file__))
10
11import hello
12
13if __name__ == "__main__":
14    rospy.init_node("hello_py")
15    hello.hello()

为了避免每次创建 Python 脚本都需要手动赋予可执行权限,可以在 .vscode/tasks.json 中添加一个新任务,并将其作为 catkin_make 的依赖:

 1{
 2    // 有关 tasks.json 格式的文档,请参见
 3    // https://go.microsoft.com/fwlink/?LinkId=733558
 4    "version": "2.0.0",
 5    "tasks": [
 6        {
 7            "label": "catkin_make:debug",
 8            "type": "shell",
 9            "command": "catkin_make",
10            "group": {
11                "kind": "build",
12                "isDefault": true
13            },
14            "dependsOn": ["chmod_scripts"],
15            "presentation": {
16                "reveal": "always"
17            },
18            "problemMatcher": "$msCompile"
19        },
20        {
21            "label": "chmod_scripts",
22            "type": "shell",
23            "command": "chmod",
24            "args": [
25                "+x",
26                "/home/jackgdn/ros-project/src/*/scripts/*.py"
27            ],
28            "problemMatcher": [],
29            "presentation": {
30                "reveal": "always",
31                "echo": true
32            }
33        }
34    ]
35}

随后修改 CMakeLists.txt 文件运行即可。


看上去这篇记录内容很少,但是实际上我花了一上午时间查找将 Debug 级别的日志输出到控制台的方法。首先,roscpp 的日志输出通过 log4j 实现,而 rospy 的日志输出通过 logger 实现,两者的实现方式不同。因此可以在配置文件 libtest/config/rosconsole.conf 中写入 log4j.logger.ros=DEBUG,然后在 libtest.launch 中添加标签 <env name="ROSCONSOLE_CONFIG_FILE" value="$(find my_package)/config/custom_rosconsole.conf" /> 来修改 C++ 节点的日志输出级别。

其余有在 launch 文件中配置 param 或者 rosparam 标签来修改日志级别的方法,但是经过我的测试都没有成功。

系列文章