forked from RobotWebTools/roslibjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore_Topic.js.html
259 lines (223 loc) · 8.85 KB
/
core_Topic.js.html
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Source: core/Topic.js</title>
<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<div id="main">
<h1 class="page-title">Source: core/Topic.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>/**
* @fileoverview
* @author Brandon Alexander - [email protected]
*/
var EventEmitter2 = require('eventemitter2').EventEmitter2;
var Message = require('./Message');
/**
* Publish and/or subscribe to a topic in ROS.
*
* Emits the following events:
* * 'warning' - if there are any warning during the Topic creation
* * 'message' - the message data from rosbridge
*
* @constructor
* @param options - object with following keys:
* * ros - the ROSLIB.Ros connection handle
* * name - the topic name, like /cmd_vel
* * messageType - the message type, like 'std_msgs/String'
* * compression - the type of compression to use, like 'png', 'cbor', or 'cbor-raw'
* * throttle_rate - the rate (in ms in between messages) at which to throttle the topics
* * queue_size - the queue created at bridge side for re-publishing webtopics (defaults to 100)
* * latch - latch the topic when publishing
* * queue_length - the queue length at bridge side used when subscribing (defaults to 0, no queueing).
* * reconnect_on_close - the flag to enable resubscription and readvertisement on close event(defaults to true).
*/
function Topic(options) {
options = options || {};
this.ros = options.ros;
this.name = options.name;
this.messageType = options.messageType;
this.isAdvertised = false;
this.compression = options.compression || 'none';
this.throttle_rate = options.throttle_rate || 0;
this.latch = options.latch || false;
this.queue_size = options.queue_size || 100;
this.queue_length = options.queue_length || 0;
this.reconnect_on_close = options.reconnect_on_close !== undefined ? options.reconnect_on_close : true;
// Check for valid compression types
if (this.compression && this.compression !== 'png' &&
this.compression !== 'cbor' && this.compression !== 'cbor-raw' &&
this.compression !== 'none') {
this.emit('warning', this.compression +
' compression is not supported. No compression will be used.');
this.compression = 'none';
}
// Check if throttle rate is negative
if (this.throttle_rate < 0) {
this.emit('warning', this.throttle_rate + ' is not allowed. Set to 0');
this.throttle_rate = 0;
}
var that = this;
if (this.reconnect_on_close) {
this.callForSubscribeAndAdvertise = function(message) {
that.ros.callOnConnection(message);
that.waitForReconnect = false;
that.reconnectFunc = function() {
if(!that.waitForReconnect) {
that.waitForReconnect = true;
that.ros.callOnConnection(message);
that.ros.once('connection', function() {
that.waitForReconnect = false;
});
}
};
that.ros.on('close', that.reconnectFunc);
};
}
else {
this.callForSubscribeAndAdvertise = this.ros.callOnConnection;
}
this._messageCallback = function(data) {
that.emit('message', new Message(data));
};
}
Topic.prototype.__proto__ = EventEmitter2.prototype;
/**
* Every time a message is published for the given topic, the callback
* will be called with the message object.
*
* @param callback - function with the following params:
* * message - the published message
*/
Topic.prototype.subscribe = function(callback) {
if (typeof callback === 'function') {
this.on('message', callback);
}
if (this.subscribeId) { return; }
this.ros.on(this.name, this._messageCallback);
this.subscribeId = 'subscribe:' + this.name + ':' + (++this.ros.idCounter);
this.callForSubscribeAndAdvertise({
op: 'subscribe',
id: this.subscribeId,
type: this.messageType,
topic: this.name,
compression: this.compression,
throttle_rate: this.throttle_rate,
queue_length: this.queue_length
});
};
/**
* Unregisters as a subscriber for the topic. Unsubscribing stop remove
* all subscribe callbacks. To remove a call back, you must explicitly
* pass the callback function in.
*
* @param callback - the optional callback to unregister, if
* * provided and other listeners are registered the topic won't
* * unsubscribe, just stop emitting to the passed listener
*/
Topic.prototype.unsubscribe = function(callback) {
if (callback) {
this.off('message', callback);
// If there is any other callbacks still subscribed don't unsubscribe
if (this.listeners('message').length) { return; }
}
if (!this.subscribeId) { return; }
// Note: Don't call this.removeAllListeners, allow client to handle that themselves
this.ros.off(this.name, this._messageCallback);
if(this.reconnect_on_close) {
this.ros.off('close', this.reconnectFunc);
}
this.emit('unsubscribe');
this.ros.callOnConnection({
op: 'unsubscribe',
id: this.subscribeId,
topic: this.name
});
this.subscribeId = null;
};
/**
* Registers as a publisher for the topic.
*/
Topic.prototype.advertise = function() {
if (this.isAdvertised) {
return;
}
this.advertiseId = 'advertise:' + this.name + ':' + (++this.ros.idCounter);
this.callForSubscribeAndAdvertise({
op: 'advertise',
id: this.advertiseId,
type: this.messageType,
topic: this.name,
latch: this.latch,
queue_size: this.queue_size
});
this.isAdvertised = true;
if(!this.reconnect_on_close) {
var that = this;
this.ros.on('close', function() {
that.isAdvertised = false;
});
}
};
/**
* Unregisters as a publisher for the topic.
*/
Topic.prototype.unadvertise = function() {
if (!this.isAdvertised) {
return;
}
if(this.reconnect_on_close) {
this.ros.off('close', this.reconnectFunc);
}
this.emit('unadvertise');
this.ros.callOnConnection({
op: 'unadvertise',
id: this.advertiseId,
topic: this.name
});
this.isAdvertised = false;
};
/**
* Publish the message.
*
* @param message - A ROSLIB.Message object.
*/
Topic.prototype.publish = function(message) {
if (!this.isAdvertised) {
this.advertise();
}
this.ros.idCounter++;
var call = {
op: 'publish',
id: 'publish:' + this.name + ':' + this.ros.idCounter,
topic: this.name,
msg: message,
latch: this.latch
};
this.ros.callOnConnection(call);
};
module.exports = Topic;
</code></pre>
</article>
</section>
</div>
<nav>
<h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="ActionClient.html">ActionClient</a></li><li><a href="ActionListener.html">ActionListener</a></li><li><a href="Goal.html">Goal</a></li><li><a href="Message.html">Message</a></li><li><a href="Param.html">Param</a></li><li><a href="Pose.html">Pose</a></li><li><a href="Quaternion.html">Quaternion</a></li><li><a href="Ros.html">Ros</a></li><li><a href="Service.html">Service</a></li><li><a href="ServiceRequest.html">ServiceRequest</a></li><li><a href="ServiceResponse.html">ServiceResponse</a></li><li><a href="SimpleActionServer.html">SimpleActionServer</a></li><li><a href="TFClient.html">TFClient</a></li><li><a href="Topic.html">Topic</a></li><li><a href="Transform.html">Transform</a></li><li><a href="UrdfBox.html">UrdfBox</a></li><li><a href="UrdfColor.html">UrdfColor</a></li><li><a href="UrdfCylinder.html">UrdfCylinder</a></li><li><a href="UrdfJoint.html">UrdfJoint</a></li><li><a href="UrdfLink.html">UrdfLink</a></li><li><a href="UrdfMaterial.html">UrdfMaterial</a></li><li><a href="UrdfMesh.html">UrdfMesh</a></li><li><a href="UrdfModel.html">UrdfModel</a></li><li><a href="UrdfSphere.html">UrdfSphere</a></li><li><a href="UrdfVisual.html">UrdfVisual</a></li><li><a href="Vector3.html">Vector3</a></li></ul><h3>Global</h3><ul><li><a href="global.html#cborTypedArrayTagger">cborTypedArrayTagger</a></li><li><a href="global.html#conversionArrayTypes">conversionArrayTypes</a></li><li><a href="global.html#decodeInt64LE">decodeInt64LE</a></li><li><a href="global.html#decodeNativeArray">decodeNativeArray</a></li><li><a href="global.html#decodeUint64LE">decodeUint64LE</a></li><li><a href="global.html#nativeArrayTypes">nativeArrayTypes</a></li><li><a href="global.html#ROSLIB">ROSLIB</a></li></ul>
</nav>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.7</a> on Mon Jan 10 2022 09:16:37 GMT+0100 (Central European Standard Time)
</footer>
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>