@@ -69,7 +69,7 @@ class LifecycleTalker : public rclcpp_lifecycle::LifecycleNode
69
69
* Callback for walltimer. This function gets invoked by the timer
70
70
* and executes the publishing.
71
71
* For this demo, we ask the node for its current state. If the
72
- * lifecycle publisher is not activate , we still invoke publish, but
72
+ * lifecycle publisher is not activated , we still invoke publish, but
73
73
* the communication is blocked so that no messages is actually transferred.
74
74
*/
75
75
void
@@ -88,6 +88,15 @@ class LifecycleTalker : public rclcpp_lifecycle::LifecycleNode
88
88
get_logger (), " Lifecycle publisher is active. Publishing: [%s]" , msg->data .c_str ());
89
89
}
90
90
91
+ // Simulate the node having some error whilst active,
92
+ // necessitating reconfiguration.
93
+ // raise_error() can be called from an inactive or active state.
94
+ if (count == 32 ){
95
+ RCLCPP_ERROR (get_logger ()," An error has arisen in the node and it needs reconfiguration." );
96
+ raise_error ();
97
+ return ;
98
+ }
99
+
91
100
// We independently from the current state call publish on the lifecycle
92
101
// publisher.
93
102
// Only if the publisher is in an active state, the message transfer is
@@ -262,6 +271,40 @@ class LifecycleTalker : public rclcpp_lifecycle::LifecycleNode
262
271
return rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn::SUCCESS;
263
272
}
264
273
274
+ // / Transition callback for state error processing
275
+ /* *
276
+ * on_error callback is being called when the lifecycle node
277
+ * enters the "errorProcessing" state.
278
+ * Depending on the return value of this function, the state machine
279
+ * either invokes a transition to the "unconfigured" state or the
280
+ * finalized state.
281
+ * TRANSITION_CALLBACK_SUCCESS transitions to "unconfigured"
282
+ * TRANSITION_CALLBACK_FAILURE transitions to "finalized"
283
+ * TRANSITION_CALLBACK_ERROR or any uncaught exceptions to "finalized"
284
+ */
285
+ rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn
286
+ on_error (const rclcpp_lifecycle::State & state)
287
+ {
288
+ // We explicitly deactivate the lifecycle publisher.
289
+ // Starting from this point, all messages are no longer
290
+ // sent into the network.
291
+ timer_.reset ();
292
+ pub_.reset ();
293
+
294
+ RCUTILS_LOG_ERROR_NAMED (
295
+ get_name (),
296
+ " on error is called from state %s." ,
297
+ state.label ().c_str ());
298
+
299
+ // We return a success and hence invoke the transition to the next
300
+ // step: "unconfigured".
301
+ // If we returned TRANSITION_CALLBACK_FAILURE instead, the state machine
302
+ // would go the "finalized" state.
303
+ // In case of TRANSITION_CALLBACK_ERROR or any thrown exception within
304
+ // this callback, the state machine also transitions to state "finalized".
305
+ return rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn::SUCCESS;
306
+ }
307
+
265
308
private:
266
309
// We hold an instance of a lifecycle publisher. This lifecycle publisher
267
310
// can be activated or deactivated regarding on which state the lifecycle node
0 commit comments