rosbag
rosbag is a node.js & browser compatible module for reading rosbag binary data files.
Installation
npm install rosbag
or
yarn add rosbag
Quick start
The most common way to interact with a rosbag is to read data records for a specific set of topics. The rosbag format encodes type information for topics, and rosbag reads this type information and parses the data records into JavaScript objects and arrays.
Here is an example of reading messages from a rosbag in node.js:
const { open } = require('rosbag');
const bag = await open('../path/to/ros.bag');
await bag.readMessages({ topics: ['/foo', '/bar'] }, (result) => {
console.log(result.topic);
console.log(result.message);
});
API
Opening a new rosbag reader
function open(fileOrPath: File | string) => Promise<Bag>
Opening a new rosbag reader is done with the open function. In the browser the function takes a File instance which you will generally get from a file input element. In node.js the function takes a string which should be the full path to a rosbag file. Node.js will read the file off of the disk. The promise will reject if there is an issue opening the file or if the file format is invalid, otherwise it will resolve with an instance of a Bag.
Bag instance
class Bag {
startTime: Time,
endTime: Time,
connections: { [number]: Connection },
chunkInfos: Array<ChunkInfo>,
readMessages(options: BagOptions, cb: (result: ReadResult) => void) => Promise<void>
}
Consuming messages from the bag instance
bag.readMessages method returns a Promise<void> which resolves when the read operation is completed or rejects in the event of a read error. During the read operation individual ReadResult objects are passed to the callback supplied to the open function. The callback may be called multiple times on the same tick as multiple data records can be encoded within a single binary chunk read within the bag reader.
BagOptions
const bagOptions = {
topics?: Array<string>,
startTime?: Time,
endTime? Time,
decompress?: {|
bz2?: (buffer: Buffer) => Buffer,
lz4?: (buffer: Buffer) => Buffer,
|}
noParse?: boolean
}
All options are optional and used to filter down from the sometimes enormous and varied data records in a rosbag. One could omit all options & filter the messages in memory within the readMessages callback; however, due to the rosbag format optimizations can be made during reading & parsing which will yield significant performance and memory gains if you specify topics and/or date ranges ahead of time.
ReadResult
const readResult {
topic: string,
message: { [string]: any },
timestamp: Time
data: Array<int8>,
chunkOffset: number,
totalChunks: number,
}
Connection
class Connection {
conn: number,
topic: string,
md5sum: string,
messageDefinition: string,
}
Time
The ROS format represents time to the nanosecond granularity. In JavaScript it is stored as an instance of the Time class. The time class has conversion helper methods to go to and from JavaScript dates.
class Time {
sec: number,
nsec: number,
constructor(sec: number, nsec: number),
toDate(): Date,
static fromDate(Date): Time,
static compare(left: Time, right: Time): number,
static isLessThan(left: Time, right: Time): boolean,
static isGreaterThan(left: Time, right: Time): boolean,
static areSame(left: Time, right: Time): boolean,
static add(left: Time, right: Time): Time,
}
You can import the Time module like this: const { Time } = require('rosbag')
Supported platforms
Currently rosbag is used & heavily tested in node@8.x as well as google chrome (via webpack). It should also work under all modern browsers which have the FileReader and typed array APIs available. If you run into issues with Firefox, Edge, or Safari please feel free to open an issue or submit a pull request with a fix.
LICENSE
This software is licensed under the Apache License, version 2 ("ALv2"), quoted below.
Copyright 2017-2018 GM Cruise
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.