ROS2 常用命令

339 阅读2分钟

命令

通用

ros2 -h
ros2 <command> -h
ros2 <command1> <cammand2> -h
......

ros2 pkg

包管理指令

# 创建功能包
ros2 pkg create [-h] [--package-format {2,3}]
                       [--description DESCRIPTION] [--license LICENSE]
                       [--destination-directory DESTINATION_DIRECTORY]
                       [--build-type {cmake,ament_cmake,ament_python}]
                       [--dependencies DEPENDENCIES [DEPENDENCIES ...]]
                       [--maintainer-email MAINTAINER_EMAIL]
                       [--maintainer-name MAINTAINER_NAME]
                       [--node-name NODE_NAME] [--library-name LIBRARY_NAME]
                       package_name

ros2 pkg list # 列出所有pkg
ros2 pkg executables [pkg_name] # 列出可运行指令
ros2 pkg prefix [pkg_name] # 输出某个包的全路径
ros2 pkg xml [pkg_name] # 输出某个包的package.xml

ros2 run

# 运行某包下的某个可执行程序
ros2 run [pkg_name] [node_name]

ros2 node (运行中的节点)

ros2 node list  # 列出节点
ros2 node info /node_name  # 查询节点信息

ros2 interface

ros2 interface list # 列出系统内所有的消息、服务以及action
ros2 interface show [interface_name] # 查看某接口具体信息
ros2 interface package [package_name] # 查看一个包下的接口

ros2 topic

ros2 topic list
ros2 topic info [topic_name] # topic的信息
ros2 topic type [topic_name] # topic的类型
ros2 topic hz [topic_name] # topic的发送频率
ros2 topic echo [topic_name] # 在终端打印主题消息, 相当于创建一个订阅者
ros2 topic pub topic_name message_type values  # 发布一条消息, 详见 ros2 topic pub -h
# ros2 topic pub turtle1/cmd_vel geometry_msgs/msg/Twist "{linear: {x: 0.5, y: 0.0, z:0.0}, angular: {x: 0.0, y: 0.0, z: 0.2}}"

ros2 service

ros2 service list
ros2 service type [name] # Output a service's type
ros2 service find [type] # Output a list of available services of a given type
ros2 service call service_name service_type [values] # 请求某服务
# ros2 service call /spawn turtlesim/srv/Spawn "{x: 2, y: 2, theta: 0.2, name: ''}"

ros2 action

ros2 action list
ros2 action info [name]
ros2 action send_goal [-h] [-f] action_name action_type goal

positional arguments:
action_name Name of the ROS action (e.g. '/fibonacci')
action_type Type of the ROS action (e.g. 'example_interfaces/action/Fibonacci')
goal Goal request values in YAML format (e.g. '{order: 10}')

otional arguments:
-h, --help show this help message and exit
-f, --feedback Echo feedback messages for the goal

ros2 param

ros2 param list
ros2 param get node_name param_name
ros2 param set node_name param_name value

时间相关API

Rate

除了定时器之外,ROS2 中还提供了 Rate 类,通过该类对象也可以控制程序的运行频率; rclpy 中的 Rate 对象可以通过节点创建,Rate 对象的 sleep() 函数需要在子线程中执行,否则会阻塞程序。

import rclpy
import threading
from rclpy.timer import Rate

rate = None
node = None

def do_some():
    global rate
    global node

    while rclpy.ok():
        node.get_logger().info("hello ---------")
        # 休眠
        rate.sleep()

def main():
    global rate
    global node

    rclpy.init()
    node = rclpy.create_node("rate_demo")

    # 创建 Rate 对象
    rate = node.create_rate(1.0)

    # 创建子线程
    thread = threading.Thread(target=do_some)
    thread.start()
    rclpy.shutdown()

if __name__ == "__main__":
    main()

Time

import rclpy
from rclpy.time import Time

def main():
    rclpy.init()
    node = rclpy.create_node("time_demo")
    # 创建 Time 对象
    right_now = node.get_clock().now()
    t1 = Time(seconds=10,nanoseconds=500000000)
    node.get_logger().info("s = %.2f, ns = %d" % (right_now.seconds_nanoseconds()[0], right_now.seconds_nanoseconds()[1]))
    node.get_logger().info("s = %.2f, ns = %d" % (t1.seconds_nanoseconds()[0], t1.seconds_nanoseconds()[1]))
    node.get_logger().info("ns = %d" % right_now.nanoseconds)
    node.get_logger().info("ns = %d" % t1.nanoseconds)
    rclpy.shutdown()

if __name__ == "__main__":
    main()

Duration

import rclpy
from rclpy.duration import Duration

def main():
    rclpy.init()
    node = rclpy.create_node("duration_demo")
    du1 = Duration(seconds = 2,nanoseconds = 500000000)
    node.get_logger().info("ns = %d" % du1.nanoseconds)
    rclpy.shutdown()

if __name__ == "__main__":
    main()