ROS
ROS utils for use in Transitive robot capabilities. Supports both ROS 1 (using rosnodejs) and ROS 2 (using rclnodejs). The respective classes ROS1 and ROS2 have a unified interface, so that switching between ROS releases is trivial as can be seen in the example below.
Install
npm i @transitive-sdk/utils-ros
Example
This example requires a running roscore
for ROS 1.
const { ROSs, getForVersion } = require('@transitive-sdk/utils-ros');
const demo = async (version) => {
const ros = getForVersion(version);
await ros.init();
const topic = '/utils_ros/test1';
const type = version == 1 ? 'std_msgs/String' : 'std_msgs/msg/String';
const interval = setInterval(() => {
ros.publish(topic, type, {data: String(Date.now())});
});
const sub = ros.subscribe(topic, type, (msg) => {
console.log('received', msg.data);
});
};
demo(1); // run demo using ROS1
demo(2); // run demo using ROS2
ROS
Small convenient singleton class for interfacing with ROS, including some auxiliary functions that come in handy in capabilities. Based on rosnodejs.
callService
Call the given service of the given type with the given body (not required when type is "std_srvs/Empty").
Parameters
serviceName
type
requestBody
(optional, defaultundefined
)
createHeader
create a std_msgs/Header for the given frame_id and date
Parameters
frame_id
(optional, default''
)date
(optional, defaultnew Date()
)
getAvailableTypes
Get all known message and service types, grouped by package.
getServices
Get available services (list of names).
getServiceType
Get type of a given service. Note that in ROS 1 this requires connecting to the service provider directly, and we don't want to poll all of them. So only use this on-demand.
Parameters
service
getSubscribedTopics
Get topics that have subscribers
Parameters
type
(optional, defaultundefined
)
getTopics
Get all topic of a given type or all topics if no type is specified
Parameters
type
(optional, defaultundefined
)
getTypeTemplate
Given a package, category, and type, e.g., 'std_msgs', 'msg', and 'String', return a plain object representing that type, which can be used as a template for creating messages.
Parameters
pkg
category
type
response
(optional, defaultfalse
)
init
Initialize ROS node. This needs to be called first.
Parameters
suffix
(optional, default''
)
publish
Publish the given message (json) on the names topic of type. Will advertise the topic if not yet advertised.
Parameters
topic
type
message
latching
(optional, defaulttrue
)
subscribe
Subscribe to the named topic of the named type. Each time a new message
is received the provided callback is called. Here options
is an optional
object: { "throttleMs": throttle-in-milliseconds }
.
Parameters
topic
type
onMessage
options
(optional, default{}
)
unsubscribe
Unsubscribe from topic
Parameters
topic
ROS2
Small convenient singleton class for interfacing with ROS2, including some auxiliary functions that come in handy in capabilities. Based on rclnodejs.
callService
Call the given service of the given type with the given body (not required when type is "std_srvs/srv/Empty").
Parameters
serviceName
type
request
(optional, defaultundefined
)
createHeader
create a std_msgs/Header for the given frame_id and date
Parameters
frame_id
(optional, default''
)date
(optional, defaultnew Date()
)
getAvailableTypes
Get all known message, service, and action types, grouped by package.
getServices
Get list of available services (list of names).
getServiceType
Get type of a given service.
Parameters
service
getSubscribedTopics
Get topics that have subscribers
Parameters
type
(optional, defaultundefined
)
getTopics
Get all topic of a given type or all topics ifno type is specified
Parameters
type
(optional, defaultundefined
)
getTypeTemplate
Given a package, category, and type, e.g., 'std_msgs', 'msg', and 'String', return a plain object representing that type, which can be used as a template for creating messages.
Parameters
pkg
category
type
response
(optional, defaultfalse
)
init
Initialize ROS node. This needs to be called first.
Parameters
suffix
(optional, default''
)
publish
Publish the given message (json) on the names topic of type. Will advertise the topic if not yet advertised.
Parameters
topic
type
message
latching
(optional, defaulttrue
)
subscribe
Subscribe to the named topic of the named type. Each time a new message
is received the provided callback is called. For available options see
https://robotwebtools.github.io/rclnodejs/docs/0.22.3/Node.html#createSubscription.
The default options.qos.reliability
is best-effort.
Parameters
topic
type
onMessage
options
(optional, default{}
)
unsubscribe
Unsubscribe from topic
Parameters
topic
getForVersion
Get the correct instance for the given ROS version (1 or 2).
Parameters
version