You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: gazebojs_pubsub/tutorial.md
+15-91Lines changed: 15 additions & 91 deletions
Original file line number
Diff line number
Diff line change
@@ -22,121 +22,45 @@ Just press enter to get the default values. This operation generates a package.j
22
22
Now you can create javascript files and execute them by invoking node.
23
23
24
24
25
-
26
-
## Publishers
25
+
## Publishers and subscribers
27
26
28
27
Publishers allow clients and servers to initiate communication, using typed messages. The messages are defined in Gazebo using Protobuf, and they are accessed in javascript via a JSON representation. Publishers can be created in the Gazebo server or the Node client, and messages are sent to unique topics that subscribers can listen to. Messages can be published to existing topics, or new topics can be created for future subscribers.
29
28
30
29
31
-
### Code
32
-
Create publish.js file
33
-
34
-
gedit publish.js
30
+
### Node session
35
31
36
-
And put the following code ([publish.js](https://bitbucket.org/osrf/gazebojs/raw/default/examples/publish.js)):
In a separate terminal, from the gazeboJsPubSub directory, start a node interactive session:
40
37
41
-
### Code explained
38
+
node
42
39
43
-
The first two lines load the Gazebo C++ module into the Node V8 script engine, and an instance of the Gazebo class is created.
40
+
And type in the following two lines to load the Gazebo C++ module into the Node V8 script engine, and crate an instance of the Gazebo class.
44
41
45
42
~~~
46
-
var gazebojs = require("gazebojs");
47
-
var gazebo = new gazebojs.Gazebo();
43
+
var gazebojs = require("gazebojs")
44
+
var gazebo = new gazebojs.Gazebo()
48
45
~~~
49
46
50
-
The command line arguments are then parsed to determine the type of protobuf Gazebo message to send, an acual instance of a message encoded in JSON, and the topic on which to send the message.
51
-
This information is then used to publish a message:
You should see the simulation stop in Gazebo, and the following output:
68
-
69
-
%%%
70
-
type: [gazebo.msgs.WorldControl]
71
-
topic: [~/world_control]
72
-
msg: [{ pause: true }]
73
-
74
-
published!
75
-
bye
76
-
%%%
77
-
53
+
The arguments to publish are the message type, the topic name, and the message (in JSON format) . Once published, the message is going to be received by each subscriber for this topic.
78
54
79
-
## Subscribers
80
55
81
56
Subscribers provide a callback function for a specific type of message on a certain topic. Each time a new message is published by a publisher, the callback is invoked for every subscriber to this topic.
82
57
83
-
### Code
84
-
Create subscribe.js file
85
-
86
-
gedit subscribe.js
87
-
88
-
And put the following code ([subscribe.js](https://bitbucket.org/osrf/gazebojs/raw/default/examples/subscribe.js)):
A subscriber is created by calling subscribe with a message type (the protobuf message name), the topic and a callback function. The call to subscribe is non blocking. The callback has 2 parameters, error and data... following the NodeJs pattern for asynchronous execution.
96
-
In this example, the callback simply converts the JSON to a string and prints it on the console.
58
+
For example, this command displays the world_stats messages by subscribing to the `~/world_stats` topic with the `console.log` message, that prints the messages to the console. You can substitue `console.log` with any function that takes `(error, result)` as arguments.
97
59
98
60
~~~
99
-
// subscribe to the topic with a callback function
100
-
gazebo.subscribe(type, topic, function (err, msg){
It is possible to unsubscribe to a topic. When unsubscribe is called, all subscriptions to that topic are removed. If you need more than one subscriber on the same topic and you don't want to unsubscribe to them at the same time, you need to use multiple instances of gazebojs.Gazebo.
116
-
117
-
### Test your subscriber:
118
-
119
-
120
-
Subscribe for 5 consecutive `WorldStatistics` messages on the world_stats topic:
0 commit comments