Skip to content

Commit

Permalink
Roslib working with socketio following firos standard
Browse files Browse the repository at this point in the history
  • Loading branch information
haas85 committed Jan 19, 2015
1 parent a66dadd commit 4f6917d
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 63 deletions.
3 changes: 2 additions & 1 deletion .jshintrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"globals": {
"global": true
"global": true,
"window": true
},
"curly": true,
"eqeqeq": true,
Expand Down
124 changes: 67 additions & 57 deletions build/roslib.js
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,7 @@ Param.prototype.get = function(callback) {
*
* @param value - value to set param to.
*/
Param.prototype.set = function(value) {
Param.prototype.set = function(value, callback) {
var paramClient = new Service({
ros : this.ros,
name : '/rosapi/set_param',
Expand All @@ -586,14 +586,13 @@ Param.prototype.set = function(value) {
value : JSON.stringify(value)
});

paramClient.callService(request, function() {
});
paramClient.callService(request, callback);
};

/**
* Delete this parameter on the ROS server.
*/
Param.prototype.delete = function() {
Param.prototype.delete = function(callback) {
var paramClient = new Service({
ros : this.ros,
name : '/rosapi/delete_param',
Expand All @@ -604,8 +603,7 @@ Param.prototype.delete = function() {
name : this.name
});

paramClient.callService(request, function() {
});
paramClient.callService(request, callback);
};

module.exports = Param;
Expand Down Expand Up @@ -656,6 +654,7 @@ function Ros(options) {

// begin by checking if a URL was given
if (options.url) {
this.socketio = (options.url.indexOf('http') !== -1);
this.connect(options.url);
}
}
Expand All @@ -668,7 +667,17 @@ Ros.prototype.__proto__ = EventEmitter2.prototype;
* @param url - WebSocket URL for Rosbridge
*/
Ros.prototype.connect = function(url) {
this.socket = assign(new WebSocket(url), socketAdapter(this));
if( window.hasOwnProperty('io') && this.socketio){
this.socket = assign(window['io'](url), socketAdapter(this));

this.socket.on('connect', this.socket.onopen);
this.socket.on('data', this.socket.onmessage);
this.socket.on('close', this.socket.onclose);
this.socket.on('error', this.socket.onerror);
}else{
this.socket = assign(new WebSocket(url), socketAdapter(this));
}

};

/**
Expand Down Expand Up @@ -714,13 +723,19 @@ Ros.prototype.authenticate = function(mac, client, dest, rand, t, level, end) {
Ros.prototype.callOnConnection = function(message) {
var that = this;
var messageJson = JSON.stringify(message);
var emitter = null;
if( window.hasOwnProperty('io') && this.socketio ){
emitter = function(msg){that.socket.emit('operation', msg);};
}else{
emitter = function(msg){that.socket.send(msg);};
}

if (!this.isConnected) {
that.once('connection', function() {
that.socket.send(messageJson);
emitter(messageJson);
});
} else {
that.socket.send(messageJson);
emitter(messageJson);
}
};

Expand Down Expand Up @@ -892,7 +907,7 @@ Ros.prototype.decodeTypeDefs = function(defs) {
}
return typeDefDict;
};

return decodeTypeDefsRec(defs[0], defs);
};

Expand Down Expand Up @@ -932,19 +947,19 @@ function Service(options) {
* * error - the error message reported by ROS
*/
Service.prototype.callService = function(request, callback, failedCallback) {
this.ros.idCounter++;
var serviceCallId = 'call_service:' + this.name + ':' + this.ros.idCounter;
var serviceCallId = 'call_service:' + this.name + ':' + (++this.ros.idCounter);

this.ros.once(serviceCallId, function(message) {
if (message.result !== undefined && message.result === false) {
if (typeof failedCallback === 'function') {
failedCallback(message.values);
if (callback || failedCallback) {
this.ros.once(serviceCallId, function(message) {
if (message.result !== undefined && message.result === false) {
if (typeof failedCallback === 'function') {
failedCallback(message.values);
}
} else if (typeof callback === 'function') {
callback(new ServiceResponse(message.values));
}
} else {
var response = new ServiceResponse(message.values);
callback(response);
}
});
});
}

var call = {
op : 'call_service',
Expand Down Expand Up @@ -1191,8 +1206,17 @@ Topic.prototype.subscribe = function(callback) {
/**
* Unregisters as a subscriber for the topic. Unsubscribing will remove
* all subscribe callbacks.
*/
Topic.prototype.unsubscribe = function() {
*
* @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);
Expand Down Expand Up @@ -1544,7 +1568,7 @@ function TFClient(options) {
this.republisherUpdateRequested = false;

// Create an Action client
this.actionclient = this.ros.ActionClien({
this.actionClient = this.ros.ActionClient({
serverName : '/tf2_web_republisher',
actionName : 'tf2_web_republisher/TFSubscriptionAction'
});
Expand All @@ -1565,12 +1589,9 @@ function TFClient(options) {
TFClient.prototype.processTFArray = function(tf) {
var that = this;
tf.transforms.forEach(function(transform) {
var frameID = transform.child_frame_id;
if (frameID[0] === '/') {
frameID = frameID.substring(1);
}
var info = that.frameInfos[frameID];
if (info !== undefined) {
var frameID = transform.child_frame_id.trimLeft('/');
var info = this.frameInfos[frameID];
if (info) {
info.transform = new Transform({
translation : transform.transform.translation,
rotation : transform.transform.rotation
Expand All @@ -1579,7 +1600,7 @@ TFClient.prototype.processTFArray = function(tf) {
cb(info.transform);
});
}
});
}, this);
};

/**
Expand All @@ -1588,17 +1609,13 @@ TFClient.prototype.processTFArray = function(tf) {
*/
TFClient.prototype.updateGoal = function() {
var goalMessage = {
source_frames : [],
source_frames : Object.keys(this.frameInfos),
target_frame : this.fixedFrame,
angular_thres : this.angularThres,
trans_thres : this.transThres,
rate : this.rate
};

for (var frame in this.frameInfos) {
request.source_frames.push(frame);
}

// if we're running in groovy compatibility mode (the default)
// then use the action interface to tf2_web_republisher
if(this.ros.groovyCompatibility) {
Expand Down Expand Up @@ -1655,23 +1672,20 @@ TFClient.prototype.processResponse = function(response) {
*/
TFClient.prototype.subscribe = function(frameID, callback) {
// remove leading slash, if it's there
if (frameID[0] === '/') {
frameID = frameID.substring(1);
}
frameID = frameID.trimLeft('/');
// if there is no callback registered for the given frame, create emtpy callback list
if (this.frameInfos[frameID] === undefined) {
if (!this.frameInfos[frameID]) {
this.frameInfos[frameID] = {
cbs : []
cbs: []
};
if (!this.republisherUpdateRequested) {
setTimeout(this.updateGoal.bind(this), this.updateDelay);
this.republisherUpdateRequested = true;
}
} else {
// if we already have a transform, call back immediately
if (this.frameInfos[frameID].transform !== undefined) {
callback(this.frameInfos[frameID].transform);
}
}
// if we already have a transform, call back immediately
else if (this.frameInfos[frameID].transform) {
callback(this.frameInfos[frameID].transform);
}
this.frameInfos[frameID].cbs.push(callback);
};
Expand All @@ -1684,20 +1698,16 @@ TFClient.prototype.subscribe = function(frameID, callback) {
*/
TFClient.prototype.unsubscribe = function(frameID, callback) {
// remove leading slash, if it's there
if (frameID[0] === '/') {
frameID = frameID.substring(1);
}
frameID = frameID.trimLeft('/');
var info = this.frameInfos[frameID];
if (info !== undefined) {
var cbIndex = info.cbs.indexOf(callback);
if (cbIndex >= 0) {
info.cbs.splice(cbIndex, 1);
if (info.cbs.length === 0) {
delete this.frameInfos[frameID];
}
this.needUpdate = true;
for (var cbs = info && info.cbs || [], idx = cbs.length; idx--;) {
if (cbs[idx] === callback) {
cbs.splice(idx, 1);
}
}
if (!callback || cbs.length === 0) {
delete this.frameInfos[frameID];
}
};

module.exports = TFClient;
Expand Down
2 changes: 1 addition & 1 deletion build/roslib.min.js

Large diffs are not rendered by default.

25 changes: 21 additions & 4 deletions src/core/Ros.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ function Ros(options) {

// begin by checking if a URL was given
if (options.url) {
this.socketio = (options.url.indexOf('http') !== -1);
this.connect(options.url);
}
}
Expand All @@ -56,7 +57,17 @@ Ros.prototype.__proto__ = EventEmitter2.prototype;
* @param url - WebSocket URL for Rosbridge
*/
Ros.prototype.connect = function(url) {
this.socket = assign(new WebSocket(url), socketAdapter(this));
if( window.hasOwnProperty('io') && this.socketio){
this.socket = assign(window['io'](url), socketAdapter(this));

this.socket.on('connect', this.socket.onopen);
this.socket.on('data', this.socket.onmessage);
this.socket.on('close', this.socket.onclose);
this.socket.on('error', this.socket.onerror);
}else{
this.socket = assign(new WebSocket(url), socketAdapter(this));
}

};

/**
Expand Down Expand Up @@ -102,13 +113,19 @@ Ros.prototype.authenticate = function(mac, client, dest, rand, t, level, end) {
Ros.prototype.callOnConnection = function(message) {
var that = this;
var messageJson = JSON.stringify(message);
var emitter = null;
if( window.hasOwnProperty('io') && this.socketio ){
emitter = function(msg){that.socket.emit('operation', msg);};
}else{
emitter = function(msg){that.socket.send(msg);};
}

if (!this.isConnected) {
that.once('connection', function() {
that.socket.send(messageJson);
emitter(messageJson);
});
} else {
that.socket.send(messageJson);
emitter(messageJson);
}
};

Expand Down Expand Up @@ -280,7 +297,7 @@ Ros.prototype.decodeTypeDefs = function(defs) {
}
return typeDefDict;
};

return decodeTypeDefsRec(defs[0], defs);
};

Expand Down
1 change: 1 addition & 0 deletions test/ros.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ describe('ROS', function() {
// of this test proves the 'warn' property will be set with default
// EventEmitter2 settings.
var callCount = 50;
window = {};
var eventEmitter = new EventEmitter2();
for (var i = 0; i < callCount; i++) {
eventEmitter.on('foo', function() { });
Expand Down

0 comments on commit 4f6917d

Please sign in to comment.