ROS2节点生命周期

节点有四个主要状态:

1
2
3
4
Unconfigured
Inactive
Active
Finalized

还存在6个过渡状态
1
2
3
4
5
6
Configuring
CleaningUp
ShuttingDown
Activating
Deactivating
ErrorProcessing

在转换状态中,将执行逻辑以确定转换是否成功。

有7个函数用于监督流程,它们是:

1
2
3
4
5
6
7
create
configure
cleanup
activate
deactivate
shutdown
destroy

ros2 lifecycle 系列命令

  • 获取节点状态

ros2 lifecycle get /my_lifecycle_node. 返回 unconfigured [1]

  • 转换节点的状态

转换为configure状态: ros2 lifecycle set /my_lifecycle_node configure

除了configure,还可以是 cleanup, activate, deactivate, shutdown

  • 获取节点状态转换时的信息

ros2 topic echo /my_lifecycle_node/transition_event

比如上面的状态转换过程,话题transition_event会输出

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
timestamp: 0
transition:
id: 0
label: ''
start_state:
id: 1
label: unconfigured
goal_state:
id: 10
label: configuring
---
timestamp: 0
transition:
id: 0
label: ''
start_state:
id: 10
label: configuring
goal_state:
id: 2
label: inactive
---

也就是状态从unconfigured转换到了configuring,再到inactive

状态转换必须按顺序,比如unconfigured,如果直接set activate,会失败,然后有提示信息。

  • 获取当前状态可转换的目标状态

ros2 lifecycle list my_lifecycle_node

  • 列出当前所有的生命节点

ros2 lifecycle nodes

service 获取当前节点的状态

ros2 service call /my_lifecycle_node/get_state lifecycle_msgs/srv/GetState

/change_state - 调用触发合法转换

/get_available_transitions - 显示合法的转换

/get_available_states - 列出所有状态

/get_transition_graph - 显示完整状态机

有用的函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
const State &   get_current_state ()
Return the current State

// 获得state的名称
std::string State::label() const


std::vector< State > get_available_states ()
Return a list with the available states

std::vector< Transition > get_available_transitions ()
Return a list with the available transitions

const State& trigger_transition (const Transition &transition)
Trigger the specified transition