Skip to content

Commit

Permalink
examples fixed and message/service types no longer try and keep track…
Browse files Browse the repository at this point in the history
… of type
  • Loading branch information
rctoris committed Mar 22, 2013
1 parent 57065e3 commit 3aa9ae9
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 74 deletions.
4 changes: 3 additions & 1 deletion examples/fibonacci.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
<script type="text/javascript" type="text/javascript">
// Connecting to ROS
// -----------------
var ros = new ROSLIB.Ros('ws://localhost:9090');
var ros = new ROSLIB.Ros({
url : 'ws://localhost:9090'
});

// If there is an error on the backend, an 'error' emit will be emitted.
ros.on('error', function(error) {
Expand Down
20 changes: 8 additions & 12 deletions src/actionlib/Goal.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,14 @@ ROSLIB.Goal = function(options) {
this.goalID = 'goal_' + Math.random() + '_' + date.getTime();
// Fill in the goal message
this.goalMessage = new ROSLIB.Message({
values : {
goal_id : {
stamp : {
secs : 0,
nsecs : 0
},
id : this.goalID
goal_id : {
stamp : {
secs : 0,
nsecs : 0
},
goal : this.goalMessage
}
id : this.goalID
},
goal : this.goalMessage
});

this.on('status', function(status) {
Expand Down Expand Up @@ -78,9 +76,7 @@ ROSLIB.Goal.prototype.send = function(timeout) {
*/
ROSLIB.Goal.prototype.cancel = function() {
var cancelMessage = new ROSLIB.Message({
values : {
id : this.goalID
}
id : this.goalID
});
this.actionClient.cancelTopic.publish(cancelMessage);
};
18 changes: 6 additions & 12 deletions src/core/Message.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,13 @@
* Message objects are used for publishing and subscribing to and from topics.
*
* @constructor
* @param options - possible keys include:
* * values - object matching the fields defined in the .msg definition file
* * type - the type of message, like 'geometry_msgs/Twist'
* @param values - object matching the fields defined in the .msg definition file
*/
ROSLIB.Message = function(options) {
ROSLIB.Message = function(values) {
var that = this;
var options = options || {};
var values = options.values;
this.type = options.type;
var values = values || {};

if (values) {
Object.keys(values).forEach(function(name) {
that[name] = values[name];
});
}
Object.keys(values).forEach(function(name) {
that[name] = values[name];
});
};
12 changes: 4 additions & 8 deletions src/core/Param.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,8 @@ ROSLIB.Param.prototype.get = function(callback) {
});

var request = new ROSLIB.ServiceRequest({
values : {
name : this.name,
value : JSON.stringify('')
}
name : this.name,
value : JSON.stringify('')
});

paramClient.callService(request, function(result) {
Expand All @@ -55,10 +53,8 @@ ROSLIB.Param.prototype.set = function(value) {
});

var request = new ROSLIB.ServiceRequest({
values : {
name : this.name,
value : JSON.stringify(value)
}
name : this.name,
value : JSON.stringify(value)
});

paramClient.callService(request, function() {
Expand Down
16 changes: 7 additions & 9 deletions src/core/Ros.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ ROSLIB.Ros = function(options) {
var url = options.url;
this.socket = null;


// begin by checking if a URL was given
if (url) {
this.connect(url);
Expand Down Expand Up @@ -93,7 +92,7 @@ ROSLIB.Ros.prototype.connect = function(url) {

// Constructs the JSON.
var jsonData = '';
for (var i = 0; i < imageData.length; i += 4) {
for ( var i = 0; i < imageData.length; i += 4) {
// RGB
jsonData += String.fromCharCode(imageData[i], imageData[i + 1], imageData[i + 2]);
}
Expand Down Expand Up @@ -129,11 +128,11 @@ ROSLIB.Ros.prototype.connect = function(url) {
}
};

that.socket = new WebSocket(url);
that.socket.onopen = onOpen;
that.socket.onclose = onClose;
that.socket.onerror = onError;
that.socket.onmessage = onMessage;
this.socket = new WebSocket(url);
this.socket.onopen = onOpen;
this.socket.onclose = onClose;
this.socket.onerror = onError;
this.socket.onmessage = onMessage;
};

/**
Expand Down Expand Up @@ -180,7 +179,7 @@ ROSLIB.Ros.prototype.callOnConnection = function(message) {
var that = this;
var messageJson = JSON.stringify(message);

if (this.socket.readyState !== WebSocket.OPEN) {
if (!this.socket || this.socket.readyState !== WebSocket.OPEN) {
that.once('connection', function() {
that.socket.send(messageJson);
});
Expand Down Expand Up @@ -247,4 +246,3 @@ ROSLIB.Ros.prototype.getParams = function(callback) {
callback(result.names);
});
};

5 changes: 1 addition & 4 deletions src/core/Service.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,7 @@ ROSLIB.Service.prototype.callService = function(request, callback) {
serviceCallId = 'call_service:' + this.name + ':' + this.ros.idCounter;

this.ros.once(serviceCallId, function(data) {
var response = new ROSLIB.ServiceResponse({
values : data,
type : this.serviceType
});
var response = new ROSLIB.ServiceResponse(data);
callback(response);
});

Expand Down
18 changes: 6 additions & 12 deletions src/core/ServiceRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,13 @@
* A ServiceRequest is passed into the service call.
*
* @constructor
* @param options - possible keys include:
* * values - object matching the values of the request part from the .srv file.
* * type - the type of service, like 'actionlib_tutorials/FibonacciAction'
* @param values - object matching the fields defined in the .srv definition file
*/
ROSLIB.ServiceRequest = function(options) {
ROSLIB.ServiceRequest = function(values) {
var that = this;
var options = options || {};
var values = options.values;
this.type = options.type;
var values = values || {};

if (values) {
Object.keys(values).forEach(function(name) {
that[name] = values[name];
});
}
Object.keys(values).forEach(function(name) {
that[name] = values[name];
});
};
18 changes: 6 additions & 12 deletions src/core/ServiceResponse.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,13 @@
* A ServiceResponse is returned from the service call.
*
* @constructor
* @param options - possible keys include:
* * values - object matching the values of the response part from the .srv file.
* * type - the type of service, like 'actionlib_tutorials/FibonacciAction'
* @param values - object matching the fields defined in the .srv definition file
*/
ROSLIB.ServiceResponse = function(options) {
ROSLIB.ServiceResponse = function(values) {
var that = this;
var options = options || {};
var values = options.values;
this.type = options.type;
var values = values || {};

if (values) {
Object.keys(values).forEach(function(name) {
that[name] = values[name];
});
}
Object.keys(values).forEach(function(name) {
that[name] = values[name];
});
};
5 changes: 1 addition & 4 deletions src/core/Topic.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,7 @@ ROSLIB.Topic.prototype.subscribe = function(callback) {
});

this.ros.on(this.name, function(data) {
var message = new ROSLIB.Message({
values : data,
type : this.messageType
});
var message = new ROSLIB.Message(data);
that.emit('message', message);
});

Expand Down

0 comments on commit 3aa9ae9

Please sign in to comment.