Skip to content

Commit 740c76d

Browse files
committed
Rosjs initial commit
A minimal addition to the previous initial commit
1 parent 7bbaef0 commit 740c76d

673 files changed

Lines changed: 85015 additions & 1 deletion

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@
186186
same "printed page" as the copyright notice for easier
187187
identification within third-party archives.
188188

189-
Copyright {yyyy} {name of copyright owner}
189+
Copyright 2016 Chris Smith
190190

191191
Licensed under the Apache License, Version 2.0 (the "License");
192192
you may not use this file except in compliance with the License.

index.js

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
/*
2+
* Copyright 2016 Rethink Robotics
3+
*
4+
* Copyright 2016 Chris Smith
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
"use strict";
19+
20+
//------------------------------------------------------------------
21+
22+
const netUtils = require('./utils/network_utils.js');
23+
24+
// these will be modules, they depend on logger which isn't initialized yet
25+
// though so they'll be required later (in initNode)
26+
let logger = null;
27+
let RosNode = null;
28+
let NodeHandle = null;
29+
30+
// will be initialized through call to initNode
31+
let log = null;
32+
let rosNode = null;
33+
let firstCheck = true;
34+
35+
//------------------------------------------------------------------
36+
37+
function _checkMasterHelper(callback, timeout) {
38+
setTimeout(() => {
39+
// also check that the slave api server is set up
40+
if (!rosNode.slaveApiSetupComplete()) {
41+
_checkMasterHelper(callback, 500);
42+
return;
43+
}
44+
// else
45+
rosNode.getMasterUri()
46+
.then((resp) => {
47+
log.info('Connected to master!');
48+
callback(Rosjs.getNodeHandle());
49+
})
50+
.catch((err, resp) => {
51+
if (firstCheck) {
52+
log.warn('Unable to connect to master. ' + err);
53+
firstCheck = false;
54+
}
55+
_checkMasterHelper(callback, 500);
56+
})
57+
}, timeout);
58+
}
59+
60+
/**
61+
* Very basic validation of node name - needs to start with a '/'
62+
* TODO: more
63+
* @return {string} name of node after validation
64+
*/
65+
function _validateNodeName(nodeName) {
66+
if (!nodeName.startsWith('/')) {
67+
nodeName = '/' + nodeName;
68+
}
69+
return nodeName;
70+
}
71+
72+
let Rosjs = {
73+
/**
74+
* Initializes a ros node for this process. Only one ros node can exist per process
75+
* If called a second time with the same nodeName, returns a handle to that node.
76+
* @param nodeName {string} name of the node to initialize
77+
* @param options {object} overrides for this node
78+
* @return {Promise} resolved when connection to master is established
79+
*/
80+
initNode(nodeName, options) {
81+
nodeName = _validateNodeName(nodeName);
82+
83+
if (rosNode !== null) {
84+
if (nodeName === rosNode.getNodeName()) {
85+
return Promise.resolve(this.getNodeHandle());
86+
}
87+
// else
88+
throw new Error('Unable to initialize node [' + nodeName + '] - node [' + rosNode.getNodeName() + '] already exists');
89+
}
90+
91+
// FIXME: validate nodeName -- MUST START WITH '/'
92+
options = options || {};
93+
let rosMasterUri = process.env.ROS_MASTER_URI;
94+
if (options.rosMasterUri) {
95+
rosMasterUri = options.rosMasterUri;
96+
}
97+
98+
if (options.useRosEnvVars) {
99+
netUtils.useRosEnvironmentVariables();
100+
}
101+
102+
if (options.portRange) {
103+
netUtils.setPortRange(options.portRange);
104+
}
105+
106+
// setup logger
107+
logger = require('./utils/logger.js');
108+
logger.init(options.logger);
109+
log = logger.createLogger();
110+
111+
// require other necessary modules...
112+
RosNode = require('./lib/RosNode.js');
113+
NodeHandle = require('./lib/NodeHandle.js');
114+
let message_utils = require('./utils/message_utils.js');
115+
116+
// load all message files
117+
message_utils.loadMessageFiles();
118+
119+
// create the ros node. Return a promise that will
120+
// resolve when connection to master is established
121+
let checkMasterTimeout = 0;
122+
rosNode = new RosNode(nodeName, rosMasterUri);
123+
return new Promise((resolve, reject) => {
124+
_checkMasterHelper(resolve, 0);
125+
});
126+
},
127+
128+
/**
129+
* @return {NodeHandle} for initialized node
130+
*/
131+
getNodeHandle() {
132+
return new NodeHandle(rosNode);
133+
},
134+
135+
get nodeHandle() {
136+
return new NodeHandle(rosNode);
137+
},
138+
139+
get nh() {
140+
return new NodeHandle(rosNode);
141+
}
142+
}
143+
144+
module.exports = Rosjs;

lib/ActionClient.js

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/*
2+
* Copyright 2016 Rethink Robotics
3+
*
4+
* Copyright 2016 Chris Smith
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
'use strict';
19+
20+
const rosjs = require('../index.js');
21+
const timeUtils = require('../utils/time_utils.js');
22+
let EventEmitter = require('events');
23+
24+
class ActionClient extends EventEmitter {
25+
constructor(options) {
26+
this._actionType = options.type;
27+
28+
this._actionServer = options.actionServer;
29+
30+
const nh = rosjs.nh;
31+
32+
// FIXME: support user options for these parameters
33+
this._goalPub = nh.advertise({
34+
topic: this._actionServer + '/goal',
35+
type: this._actionType + 'Goal',
36+
queueSize: 1
37+
});
38+
39+
this._cancelPub = nh.advertise({
40+
topic: this._actionServer + '/goal',
41+
type: 'actionlib_msgs/GoalID',
42+
queueSize: 1
43+
});
44+
45+
this._statusSub = nh.subscribe({
46+
topic: this._actionServer + '/status',
47+
type: 'actionlib_msgs/GoalStatusArray',
48+
queueSize: 1},
49+
(msg) => { this._handleStatus(msg); }
50+
);
51+
52+
this._feedbackSub = nh.subscribe({
53+
topic: this._actionServer + '/feedback',
54+
type: this._actionType + 'Feedback',
55+
queueSize: 1},
56+
(msg) => { this._handleFeedback(msg); }
57+
);
58+
59+
this._statusSub = nh.subscribe({
60+
topic: this._actionServer + '/result',
61+
type: this._actionType + 'Result',
62+
queueSize: 1},
63+
(msg) => { this._handleResult(msg); }
64+
);
65+
66+
this._goals = {};
67+
this._goalCallbacks = {};
68+
69+
this._goalSeqNum = 0;
70+
}
71+
72+
_handleStatus(msg) {
73+
this.emit('status', msg);
74+
}
75+
76+
_handleFeedback(msg) {
77+
const goalId = msg.status.goal_id.id;
78+
if (this._goals.hasOwnProperty(goalId)) {
79+
this.emit('feedback', msg);
80+
}
81+
}
82+
83+
_handleResult(msg) {
84+
const goalId = msg.status.goal_id.id;
85+
if (this._goals.hasOwnProperty(goalId)) {
86+
delete this._goals[goalId];
87+
this.emit('result', msg);
88+
}
89+
}
90+
91+
sendGoal(goal) {
92+
if (!goal.hasOwnProperty('goal_id')) {
93+
goal.goal_id = {
94+
stamp: timeUtils.now(),
95+
id: this._generateGoalId()
96+
};
97+
}
98+
if (!goal.hasOwnProperty(header)) {
99+
goal.header = {
100+
seq: this._goalSeqNum++,
101+
stamp: goal.goal_id.stamp,
102+
frame_id: 'auto-generated'
103+
};
104+
}
105+
const goalId = goal.goal_id.id;
106+
this._goals[goalId] = goal;
107+
108+
this._goalPub.publish(goal);
109+
}
110+
111+
_generateGoalId() {
112+
let id = this._actionType + '.';
113+
id += 'xxxxxxxx'.replace(/[x]/g, function(c) {
114+
return = (Math.random()*16).toString(16);
115+
});
116+
return id;
117+
}
118+
};

0 commit comments

Comments
 (0)