Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const path = require('path');
const QoS = require('./lib/qos.js');
const rclnodejs = require('bindings')('rclnodejs');
const validator = require('./lib/validator.js');
const util = require('./lib/utility.js');

function inherits(target, source) {
let properties = Object.getOwnPropertyNames(source.prototype);
Expand Down Expand Up @@ -224,7 +225,9 @@ let rcl = {
*/
expandTopicName(topicName, nodeName, nodeNamespace) {
return rclnodejs.expandTopicName(topicName, nodeName, nodeNamespace);
}
},

util: util,
};

module.exports = rcl;
109 changes: 109 additions & 0 deletions lib/message_translator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// Copyright (c) 2017 Intel Corporation. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

'use strict';

const debug = require('debug')('rclnodejs:message_translator');

/* eslint-disable max-depth */
function copyMsgObject(msg, obj) {
if (typeof obj === 'object') {
for (let i in obj) {
if (typeof msg[i] !== 'undefined') {
const type = typeof obj[i];
if (type === 'string' || type === 'number' || type === 'boolean') {
// A primitive-type value
msg[i] = obj[i];
} else if (Array.isArray(obj[i])) { // TODO(Kenny): deal with TypedArray
// It's an array
if (typeof obj[i][0] === 'object') {
// It's an array of objects: converting to ROS message objects

// 1. Extract the element-type first
const t = new MessageTranslator(msg[i].classType.elementType);
// 2. Build the array by translate every elements
let msgArray = [];
obj[i].forEach((o) => {
msgArray.push(t.toROSMessage(o));
});
// 3. Assign
msg[i].fill(msgArray);
} else {
// It's an array of primitive-type elements
msg[i] = obj[i];
}
} else {
// Proceed further of this object
copyMsgObject(msg[i], obj[i]); // TODO(Kenny): deal with TypedArray
}
} else {
// Extra fields in obj (but not in msg)
}
} // for
}
}
/* eslint-enable max-depth */

class MessageTranslator {
constructor(typeClass) {
this._typeClass = typeClass;
}

static toPlainObject(message) {
// TODO(Kenny): deal with primitive-type array (convert to TypedArray)

if (message.isROSArray) {
// It's a ROS message array
// Note: there won't be any JavaScript array in message
let array = [];
const data = message.data;
data.forEach((e) => {
// Translate every elements
array.push(MessageTranslator.toPlainObject(e));
});
return array;
// eslint-disable-next-line no-else-return
} else {
// It's a ROS message
const def = message.classType.ROSMessageDef;
let obj = {};
for (let i in def.fields) {
const name = def.fields[i].name;
if (def.fields[i].type.isPrimitiveType) {
// Direct assignment
obj[name] = message[name];
} else {
// Proceed further
obj[name] = MessageTranslator.toPlainObject(message[name]);
}
}
return obj;
}
}

toROSMessage(obj) {
let msg = new this._typeClass();
const type = typeof obj;
if (type === 'string' || type === 'number' || type === 'boolean') {
msg.data = obj;
} else if (type === 'object') {
copyMsgObject(msg, obj);
}
return msg;
}
}

module.exports = {
MessageTranslator: MessageTranslator
};
17 changes: 11 additions & 6 deletions lib/publisher.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
const rclnodejs = require('bindings')('rclnodejs');
const debug = require('debug')('rclnodejs:publisher');
const Entity = require('./entity.js');
const {MessageTranslator} = require('./message_translator.js');

/**
* @class - Class representing a Publisher in ROS
Expand All @@ -27,6 +28,7 @@ class Publisher extends Entity {
constructor(handle, nodeHandle, typeClass, topic, qos) {
super(handle, typeClass, qos);
this._topic = topic;
this.translator = new MessageTranslator(typeClass);
}

/**
Expand All @@ -42,12 +44,15 @@ class Publisher extends Entity {
* @return {undefined}
*/
publish(message) {
// TODO(minggang): Support to convert a plain JavaScript value/object to a ROS message,
// thus we can invoke this function like: publisher.publish('hello world').
let rclMessage = message;
if (!(message instanceof this._typeClass)) {
rclMessage = new this._typeClass();
rclMessage.data = message;
let rclMessage;
if (message instanceof this._typeClass) {
rclMessage = message;
} else {
// Use translator to enable call by plain object/number/string argument
// e.g. publisher.publish(3.14);
// publisher.publish('The quick brown fox...');
// publisher.publish({linear: {x: 0, y: 1, z: 2}, ...});
rclMessage = this.translator.toROSMessage(message);
}

let rawRosMessage = rclMessage.serialize();
Expand Down
62 changes: 62 additions & 0 deletions lib/utility.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright (c) 2017 Intel Corporation. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

'use strict';

/* eslint-disable max-depth */
function deepEqual(msg, obj) {
if (typeof obj !== 'object') {
return msg === obj;
}

for (let i in obj) {
const m = msg[i];
const o = obj[i];

if (typeof m === 'undefined') {
return false;
}

const t = typeof o;
if (t === 'string' || t === 'number' || t === 'boolean') {
// A primitive value
if (m !== o) {
return false;
}
} else if (m.isROSArray) {
// An array of message objects
if (!Array.isArray(o)) return false;

const data = m.data;
for (let j in data) {
if (!deepEqual(data[j], o[j])) {
return false;
}
}
} else {
// A regular message object
const equal = deepEqual(m, o);
if (!equal) {
return false;
}
}
}

return true;
}
/* eslint-enable max-depth */

module.exports = {
deepEqual: deepEqual,
};
21 changes: 21 additions & 0 deletions rosidl_gen/templates/message.dot
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ let arrayWrapper = it.spec.msgName + 'ArrayWrapper';
let refObjectType = it.spec.msgName + 'RefStruct';
let refObjectArrayType = it.spec.msgName + 'RefStructArray';
let refArrayType = it.spec.msgName + 'RefArray';
const compactMsgDefJSON = JSON.stringify(JSON.parse(it.json));

if (it.spec.fields.length === 0) {
/* Following rosidl_generator_c style, put a bool member named '_dummy' */
Expand Down Expand Up @@ -326,6 +327,14 @@ class {{=objectWrapper}} {
{{?}}
{{~}}
}

get classType() {
return {{=objectWrapper}};
}

static get ROSMessageDef() {
return {{=compactMsgDefJSON}};
}
}

// Define the wrapper of array class.
Expand Down Expand Up @@ -445,6 +454,18 @@ class {{=arrayWrapper}} {
{{=objectWrapper}}.freeStruct(refObjectArray[index]);
}
}

static get elementType() {
return {{=objectWrapper}};
}

get isROSArray() {
return true;
}

get classType() {
return {{=arrayWrapper}};
}
}

{{? it.spec.constants != undefined && it.spec.constants.length}}
Expand Down
2 changes: 1 addition & 1 deletion test/publisher_array_setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ rclnodejs.init().then(function() {
node.destroy();
rclnodejs.shutdown();
process.exit(0);
});
});
}).catch(function(err) {
console.log(err);
});
Loading