### Install rosbag.js Source: https://github.com/cruise-automation/rosbag.js/blob/main/README.md Install the rosbag.js module using npm or yarn for Node.js and browser projects. ```bash npm install rosbag ``` ```bash yarn add rosbag ``` -------------------------------- ### Read Messages from ROS Bag Source: https://github.com/cruise-automation/rosbag.js/blob/main/README.md Example of opening a rosbag file and reading messages from specified topics using the rosbag.js library in a Node.js environment. ```javascript const { open } = require('rosbag'); async function logMessagesFromFooBar() { // open a new bag at a given file location: const bag = await open('../path/to/ros.bag'); // read all messages from both the '/foo' and '/bar' topics: await bag.readMessages({ topics: ['/foo', '/bar'] }, (result) => { // topic is the topic the data record was in // in this case it will be either '/foo' or '/bar' console.log(result.topic); // message is the parsed payload // this payload will likely differ based on the topic console.log(result.message); }); } logMessagesFromFooBar(); ``` -------------------------------- ### Include rosbag.js in Browser Source: https://github.com/cruise-automation/rosbag.js/blob/main/README.md Include the rosbag.js script directly in an HTML page for browser usage, typically after installation via a package manager. ```html ``` -------------------------------- ### rosbag.js API Documentation Source: https://github.com/cruise-automation/rosbag.js/blob/main/README.md Documentation for the core rosbag.js API, including opening bag files and the structure of the Bag instance and its methods. ```APIDOC open(fileOrPath: File | string) => Promise - Opens a new rosbag reader. - In the browser, accepts a File instance (e.g., from an input element). - In Node.js, accepts a string representing the full path to the rosbag file. - Resolves with a Bag instance upon successful opening, or rejects if the file cannot be opened or is invalid. Bag instance: - startTime: Time - The time of the earliest message in the bag. - endTime: Time - The time of the last message in the bag. - connections: { [number]: Connection } - A hash of connection records by their ID. - chunkInfos: Array - An array of ChunkInfos describing the chunks within the bag. - readMessages(options: BagOptions, cb: (result: ReadResult) => void) => Promise - Consumes messages from the bag. - Options can specify topics to read (e.g., { topics: ['/topic1', '/topic2'] }). - The callback function `cb` is invoked for each message read, receiving a `ReadResult` object containing the topic and parsed message. - Returns a Promise that resolves when the read operation is completed or rejects on error. - Multiple `ReadResult` objects can be passed to the callback within the same tick. ``` -------------------------------- ### Releasing a New Version of rosbag.js Source: https://github.com/cruise-automation/rosbag.js/blob/main/CONTRIBUTING.md This section details the process for releasing a new version of the rosbag.js project. It involves updating the changelog, bumping the version using yarn, creating a pull request, and finally publishing the new version and creating a GitHub release. ```shell 1. Update [HISTORY.md](./HISTORY.md) with the changes since the last release. 2. Run `yarn version` to pick the new version. 3. Create a new pull request with these changes. 4. After the pull request has been merged to `main` run `yarn publish`. Pick the same version as above. 5. Create a [Github release](https://github.com/cruise-automation/rosbag.js/releases/new) for the version. ``` -------------------------------- ### BagOptions Configuration Source: https://github.com/cruise-automation/rosbag.js/blob/main/README.md Defines the optional configuration parameters for reading ROS bag files. These options allow filtering data by topics and time ranges, specifying decompression callbacks for compressed bags, and controlling message parsing behavior. ```js const bagOptions = { // an optional array of topics used to filter down // which data records will be read // the default is all records on all topics topics?: Array, // an optional Time instance used to filter data records // to only those which start on or after the given start time // the default is undefined which will apply no filter startTime?: Time, // an optional Time instance used to filter data records // to only those which end on or before the given end time // the default is undefined which will apply no filter endTime? Time, // decompression callbacks: // if your bag is compressed you can supply a callback to decompress it // based on the compression type. The callback should accept a buffer of compressed bytes // and return a buffer of uncompressed bytes. For examples on how to decompress lz4 and bz2 compressed bags // please see the tests here: https://github.com/cruise-automation/rosbag.js/blob/545529344c8c2a0b3a3126646d065043c2d67d84/src/bag.test.js#L167-L192 // The decompression callback is also passed the uncompressedByteLength which is stored in the bag. // This byte length can be used with some decompression libraries to increase decompression efficiency. decompress?: {| bz2?: (buffer: Buffer, uncompressedByteLength: number) => Buffer, lz4?: (buffer: Buffer, uncompressedByteLength: number) => Buffer, |}, // by default the individual parsed binary messages will be parsed based on their [ROS message definition](http://wiki.ros.org/msg) // if you set noParse to true the read operation will skip the message parsing step noParse?: boolean, // Whether the resulting messages should be deeply frozen using Object.freeze(). (default: false) // Useful to make sure your code or libraries doesn't accidentally mutate bag messages. freeze?: boolean, } ``` -------------------------------- ### TimeUtil API Reference Source: https://github.com/cruise-automation/rosbag.js/blob/main/README.md Provides utility methods for manipulating ROS Time objects in JavaScript, including conversion to/from Date objects, comparison, arithmetic, and sorting. ```APIDOC TimeUtil: toDate(Time): Date Converts a ROS Time object (with sec and nsec fields) to a JavaScript Date object. Note: This is a lossy conversion as JavaScript dates do not store nanoseconds. Parameters: Time: An object with 'sec' and 'nsec' fields representing ROS time. Returns: A JavaScript Date object. fromDate(Date): Time Builds a ROS Time instance from a JavaScript Date object. Parameters: Date: A JavaScript Date object. Returns: A ROS Time object with 'sec' and 'nsec' fields. compare(left: Time, right: Time): number Compares two ROS Time objects. Returns: - A positive number if left is greater than right. - A negative number if right is greater than left. - 0 if the times are the same. Usage: Useful for sorting arrays of times, e.g., times.sort(Time.compare). isLessThan(left: Time, right: Time): boolean Checks if the left ROS Time is less than the right ROS Time. Parameters: left: The first Time object. right: The second Time object. Returns: true if left < right, false otherwise. isGreaterThan(left: Time, right: Time): boolean Checks if the left ROS Time is greater than the right ROS Time. Parameters: left: The first Time object. right: The second Time object. Returns: true if left > right, false otherwise. areSame(left: Time, right: Time): boolean Checks if two ROS Time objects represent the same point in time. Parameters: left: The first Time object. right: The second Time object. Returns: true if the times are the same, false otherwise. add(left: Time, right: Time): Time Computes the sum of two ROS Time objects and returns a new Time object. Parameters: left: The first Time object. right: The second Time object. Returns: A new Time object representing the sum. ``` -------------------------------- ### Connection Information Structure Source: https://github.com/cruise-automation/rosbag.js/blob/main/README.md Provides details about a specific connection within a ROS bag, including its unique ID, associated topic, message definition's MD5 hash, and the raw message definition string. ```js class Connection { // the id of the connection conn: number, // the topic for the connection topic: string, // the md5 hash for the connection message definition md5sum: string, // the rosbag formatted message definition for records on this connection's topic messageDefinition: string, } ``` -------------------------------- ### JSON Parsing Directive Source: https://github.com/cruise-automation/rosbag.js/blob/main/README.md A special pragma directive used in ROS message definitions. When placed before a string field, it instructs rosbag.js to parse the content of that string field as JSON. ```cpp #pragma rosbag_parse_json string data ``` -------------------------------- ### ReadResult Message Structure Source: https://github.com/cruise-automation/rosbag.js/blob/main/README.md Represents the structure of a single message record read from a ROS bag. It includes the topic, parsed message content, timestamp, raw data buffer, and progress information about the chunk being read. ```js class ReadResult { // the topic from which the current record was read topic: string, // the parsed message contents as a JavaScript object // this can contain nested complex types // and arrays of complex & simple types // this will be undefined if you supply { noParse: true } to `bag.readMessages` message: { [string]: any }, // a Time instance - the receive time of the message timestamp: Time // the raw buffer data from the data record // a node.js buffer in node & an array buffer in the browser data: Array, // the offset of the chunk being read // starts at 0 and eventually increments to totalChunks // useful for computing read progress as a percentage chunkOffset: number, // the total chunks to eventually be consumed // during the current read operation totalChunks: number, } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.