Skip to content

Commit 59e7d82

Browse files
author
Hugo Boyer
committed
made gazebojs tutorials interactive
1 parent 7b11814 commit 59e7d82

File tree

1 file changed

+15
-91
lines changed

1 file changed

+15
-91
lines changed

gazebojs_pubsub/tutorial.md

Lines changed: 15 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -22,121 +22,45 @@ Just press enter to get the default values. This operation generates a package.j
2222
Now you can create javascript files and execute them by invoking node.
2323

2424

25-
26-
## Publishers
25+
## Publishers and subscribers
2726

2827
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.
2928

3029

31-
### Code
32-
Create publish.js file
33-
34-
gedit publish.js
30+
### Node session
3531

36-
And put the following code ([publish.js](https://bitbucket.org/osrf/gazebojs/raw/default/examples/publish.js)):
32+
Start gazebo
3733

38-
<include src='https://bitbucket.org/osrf/gazebojs/raw/default/examples/publish.js' />
34+
gazebo
3935

36+
In a separate terminal, from the gazeboJsPubSub directory, start a node interactive session:
4037

41-
### Code explained
38+
node
4239

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.
4441

4542
~~~
46-
var gazebojs = require("gazebojs");
47-
var gazebo = new gazebojs.Gazebo();
43+
var gazebojs = require("gazebojs")
44+
var gazebo = new gazebojs.Gazebo()
4845
~~~
4946

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:
47+
Type in the following to publish a message:
5248

5349
~~~
54-
gazebo.publish(type, topic , msg);
50+
gazebo.publish('gazebo.msgs.WorldControl', '~/world_control', { pause: true});
5551
~~~
5652

57-
Once published, the message is going to be received by each subscriber for this topic.
58-
59-
60-
### Test your publisher:
61-
62-
63-
Publish `WorldControl` message on the world_control topic to pause the simulation:
64-
65-
node publish.js 'gazebo.msgs.WorldControl' '~/world_control' '{"pause": true}'
66-
67-
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.
7854

79-
## Subscribers
8055

8156
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.
8257

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)):
89-
90-
91-
<include src='https://bitbucket.org/osrf/gazebojs/raw/default/examples/subscribe.js' />
92-
93-
### Code explained
94-
95-
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.
9759

9860
~~~
99-
// subscribe to the topic with a callback function
100-
gazebo.subscribe(type, topic, function (err, msg){
101-
try {
102-
if (err) throw(err);
103-
console.log('-- [' + count + '] --');
104-
count += -1;
105-
// convert the Json msg to a string
106-
var s= JSON.stringify(msg);
107-
console.log(s);
108-
} catch(err) {
109-
console.log('error: ' + err);
110-
console.log(msg);
111-
}
112-
});
61+
gazebo.subscribe('gazebo.msgs.WorldStatistics', '~/world_stats', console.log)
11362
~~~
11463

115-
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:
121-
122-
node subscribe.js 'gazebo.msgs.WorldStatistics' '~/world_stats' 5
123-
124-
You should see the following output:
125-
126-
%%%
127-
subscribing to topic [~/world_stats] of type [gazebo.msgs.WorldStatistics]
128-
keep the process alive...
129-
-- [5] --
130-
{"sim_time":{"sec":13081,"nsec":864000000},"iterations":13081864,"paused":false,"pause_time":{"sec":69,"nsec":653000000},"real_time":{"sec":13115,"nsec":231224650}}
131-
-- [4] --
132-
{"sim_time":{"sec":13082,"nsec":64000000},"iterations":13082064,"paused":false,"pause_time":{"sec":69,"nsec":653000000},"real_time":{"sec":13115,"nsec":431904634}}
133-
-- [3] --
134-
{"sim_time":{"sec":13082,"nsec":264000000},"iterations":13082264,"paused":false,"pause_time":{"sec":69,"nsec":653000000},"real_time":{"sec":13115,"nsec":632504383}}
135-
-- [2] --
136-
{"sim_time":{"sec":13082,"nsec":464000000},"iterations":13082464,"paused":false,"pause_time":{"sec":69,"nsec":653000000},"real_time":{"sec":13115,"nsec":833144611}}
137-
-- [1] --
138-
{"sim_time":{"sec":13082,"nsec":664000000},"iterations":13082664,"paused":false,"pause_time":{"sec":69,"nsec":653000000},"real_time":{"sec":13116,"nsec":33668964}}
139-
GZPubSub::Unsubscribe() topic = [~/world_stats]
140-
%%%
64+
It is possible to unsubscribe to a topic. When `unsubscribe` is called, all subscriptions to that topic are removed.
14165

14266

0 commit comments

Comments
 (0)