Liking cljdoc? Tell your friends :D

Java Bag Reader Build Status

This is a Java library intended for reading information from and deserializing ROS Bag Files.

It is capable of reading Version 2.0 bag files and does not require ROS or any other non-Java library to be installed.

It does not support writing to or playing back bag files; I would have no objections to adding that functionality, but I haven't needed it for my purposes.

Requirements

Java 1.8 or higher is required. Compilation requires Maven 3.0.4 or higher.

Obtaining

Add the following dependency to your Maven pom.xml:

<dependency>
    <groupId>com.github.swri-robotics</groupId>
    <artifactId>bag-reader-java</artifactId>
    <version>1.10.3</version>
</dependency>

Usage

The general pattern you'll follow is:

  1. Use the static BagReader.read() methods to obtain a BagFile.
  2. Call forMessagesOfType() or forMessagesOnTopic() on your BagFile to iterate through all of the messages on a particular type or topic.

Read over the Javadocs for the BagFile class; it has many other methods you can use to extract data from the bag file in various ways, such as looking for only the first message of a type or on a topic, or directly looking through individual connections.

Type Conversions

The field types in ROS messages do not all have exact equivalents in Java, so they are converted into similar types that are capable of representing them. Keep in mind that because Java does not have any unsigned types, uint types must be stored in the next-larger type in order to preserve their entire possible range.

ROS TypeJava type
boolBoolean
byteByte
int8Byte
charShort
uint8Short
int16Short
uint16Integer
int32Integer
uint32Long
int64Long
uint64java.math.BigInteger
float32Float
float64Double
stringjava.lang.String
timejava.sql.Timestamp
durationDouble

Examples

Print info for all of the topics in a bag

public class ExampleClass {
    public static void main(String[] args) throws BagReaderException {
        BagFile file = BagReader.readFile("file.bag");

        System.out.println("Topics:");
        for (TopicInfo topic : file.getTopics()) {
            System.out.println(topic.getName() + " \t\t" + topic.getMessageCount() +
                          " msgs \t: " + topic.getMessageType() + " \t" +
                          (topic.getConnectionCount() > 1 ? ("(" + topic.getConnectionCount() + " connections)") : ""));
        }
    }
}

Print all of the std_msg/String values in a bag

public class ExampleClass {
    public static void main(String[] args) throws BagReaderException {
        BagFile file = BagReader.readFile("file.bag");

        file.forMessagesOfType("std_msgs/String", new MessageHandler() {
            @Override
            public boolean process(MessageType message) {
                try {
                    System.out.println(message.<StringType>getField("data").getValue());
                }
                catch (UninitializedFieldException e) {
                    System.err.println("Field was not initialized.");
                }
                return true;
            }
        });
    }
}

Can you improve this documentation? These fine people already did:
P. J. Reed & Jack Morrison
Edit on GitHub

cljdoc is a website building & hosting documentation for Clojure/Script libraries

× close