Skip to content

Instantly share code, notes, and snippets.

@syrnick
Last active April 25, 2016 06:40
Show Gist options
  • Save syrnick/0cd20d838ddef407b564ff03cd88f9fe to your computer and use it in GitHub Desktop.
Save syrnick/0cd20d838ddef407b564ff03cd88f9fe to your computer and use it in GitHub Desktop.
Proof-of-concept of node require hook to parse ROS messages
// Needs to have the messages and /tmp/genjs in NODE_PATH, so here's an sample invocation
// NODE_PATH=$NODE_PATH:/opt/ros/indigo/share/:/tmp/genjs/ node demo_poc.js
// it also assumes that base_deserialize.js base_serialize.js find.js are copied to /tmp/genjs
require('./require_hook');
const stdString = require('std_msgs/msg/String');
console.log(`Loaded message ${stdString} ${stdString.datatype()}`);
const actionlibTutorialAveragingAction = require('actionlib_tutorials/msg/AveragingAction');
console.log(`Loaded message ${actionlibTutorialAveragingAction} ${actionlibTutorialAveragingAction.datatype()}`);
const childProcess = require('child_process');
const genjsLocation = childProcess.execSync(`rospack find genjs`).toString().trim();
const genjsGeneratedPath = '/tmp/genjs/generated';
const loadRosMessage = function(m, filename) {
const filenamePieces = filename.split('/');
const messageName = filenamePieces[filenamePieces.length - 1].split('.')[0];
const packageName = filenamePieces[filenamePieces.length - 3];
const packageLocation = childProcess.execSync(`rospack find ${packageName}`).toString().trim();
console.log(`Package location ${packageLocation}`);
const includeString = `-I${packageName}:${packageLocation}/msg`;
const compileCommand = ['python', `${genjsLocation}/scripts/gen_js.py`,
filename, '-o', `${genjsGeneratedPath}/${packageName}`, '-p', packageName, includeString].join(' ');
childProcess.execSync(compileCommand);
return require.extensions['.js'](m, `${genjsGeneratedPath}/${packageName}/${messageName}.js`);
};
require.extensions['.msg'] = loadRosMessage;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment