-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathros-function.js
More file actions
45 lines (40 loc) · 1.07 KB
/
Copy pathros-function.js
File metadata and controls
45 lines (40 loc) · 1.07 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
var ROS_IP = 'xxx.xxx.xxx.xxx:8080';
// Connect to ROS.
var ros = new ROSLIB.Ros({
url : 'ws://'+ROS_IP
});
ros.on('connection', function() {
console.log('Connected to ros server.');
});
ros.on('error', function(error) {
console.log('Error connecting to ros server: ', error);
});
ros.on('close', function() {
console.log('Connection to ros server closed.');
});
// Publisher
function ros_pub(topic, msg_type, payload){
//define the topic
var pub_topic = new ROSLIB.Topic({
ros : ros,
name : topic,
messageType : msg_type
});
//publish to the topic
var pub_msg = new ROSLIB.Message(payload);
pub_topic.publish(pub_msg);
}
// Subscriber
function ros_sub(topic, msg_type){
//define the topic
var sub_topic = new ROSLIB.Topic({
ros : ros,
name : topic,
messageType : msg_type
});
//subscribe from the topic
sub_topic.subscribe(function(message) {
console.log("Subscribe to topic " + topic + ", recieved message:" + JSON.stringify(message));
$('#ros_subscriber').append("<p>"+JSON.stringify(message)+"</p>");
});
}