-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathlifecycle-node-example.js
More file actions
126 lines (110 loc) · 3.31 KB
/
lifecycle-node-example.js
File metadata and controls
126 lines (110 loc) · 3.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Copyright (c) 2020 Wayne Parrott. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
'use strict';
const rclnodejs = require('../../index.js');
const NODE_NAME = 'test_node';
const TOPIC = 'test';
const COUNTD_DOWN = 5;
/**
* This app demonstrates using a LifecycleNode to
* publish a count down value from 10 - 0. A subscription
* is created to watch for the counter reaching 0 at which
* time it will deactivate and shutdown the node.
*/
class App {
constructor() {
this._node = null;
this._publisher = null;
this._subscriber = null;
this._timer = null;
this._StateInterface = null;
}
async init() {
await rclnodejs.init();
this._count = COUNTD_DOWN;
this._node = rclnodejs.createLifecycleNode(NODE_NAME);
this._node.registerOnConfigure((prevState) => this.onConfigure(prevState));
this._node.registerOnActivate((prevState) => this.onActivate(prevState));
this._node.registerOnDeactivate((prevState) =>
this.onDeactivate(prevState)
);
this._node.registerOnShutdown((prevState) => this.onShutdown(prevState));
this._StateInterface = rclnodejs.createMessage(
'lifecycle_msgs/msg/State'
).constructor;
rclnodejs.spin(this._node);
}
start() {
this._node.configure();
this._node.activate();
}
stop() {
this._node.deactivate();
this._node.shutdown();
rclnodejs.shutdown();
process.exit(0);
}
onConfigure() {
console.log('Lifecycle: CONFIGURE');
this._publisher = this._node.createLifecyclePublisher(
'std_msgs/msg/String',
TOPIC
);
this._subscriber = this._node.createSubscription(
'std_msgs/msg/String',
TOPIC,
(msg) => {
let cnt = parseInt(msg.data, 10);
console.log(`countdown msg: ${cnt}`);
if (cnt < 1) {
this.stop();
}
}
);
return rclnodejs.lifecycle.CallbackReturnCode.SUCCESS;
}
onActivate() {
console.log('Lifecycle: ACTIVATE');
this._publisher.activate();
this._timer = this._node.createTimer(BigInt('1000000000'), () => {
this._publisher.publish(`${this._count--}`);
});
return rclnodejs.lifecycle.CallbackReturnCode.SUCCESS;
}
onDeactivate() {
console.log('Lifecycle: DEACTIVATE');
this._publisher.deactivate();
if (this._timer) {
this._timer.cancel();
this._timer = null;
}
return rclnodejs.lifecycle.CallbackReturnCode.SUCCESS;
}
onShutdown(prevState) {
console.log('Lifecycle: SHUTDOWN');
let result = rclnodejs.lifecycle.CallbackReturnCode.SUCCESS;
if (prevState.id === this._StateInterface.PRIMARY_STATE) {
result = this.onDeactivate();
this._publisher = null;
this._subscriber = null;
}
return result;
}
}
async function main() {
let app = new App();
await app.init();
app.start();
}
main();