Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions HarpFileFormat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<img src="./assets/HarpLogo.svg" width="200">

# Standardized Harp file format

## Introduction

This document defines a standardized file format for logging data from Harp devices. The file format is based on the [Harp Binary Protocol](./BinaryProtocol-8bit.md) and is designed for efficient data logging and parsing.

One of the main advantages of using a standardized binary communication protocol is that logging data from Harp devices can be largely generalized. Conceptually, because all Harp messages share a common standard structure, we can write all the binary data emitted from a device directly into a single binary file. However, this is not always the most convenient way to log data. For instance, if one is interested in ingesting only a subset of messages (e.g. only the messages from a particular sensor connected to the Harp device), this approach would require a post-processing step to filter out the messages of interest. Furthermore, each address, as per Harp protocol spec, has potentially different data formats (e.g. U8 vs U16) or even different lengths if array registers are involved. This can make it more complex to parse and analyze a binary file offline, since we will have to examine the header of each and every message in the file to determine how to extract its contents.

This processing step could be entirely eliminated if we could ensure that all messages in a single binary file had the same format. Fortunately, for any given Harp device, the payload stored in a specific register address is guaranteed to have a fixed format. This can be leveraged in order to save messages from a specific register into different fixed-format files, by employing a de-multiplexing strategy.

## Harp file format

For each device, we define a "container" file format which is essentially a folder that will store data from a single device, and where the payload from messages coming from each register is saved sequentially to a separate binary file:

```plaintext
📦<Device>
┣ 📜<DeviceName>_0_<suffix>.bin
┣ 📜<DeviceName>_1_<suffix>.bin
┣ ...
┗📜<DeviceName>_<Reg>_<suffix>.bin
```
---

The various components of this convention are detailed below.

- the character `_` is reserved as a separator between fields.
- `<DeviceName>` should match the `device.yml` metadata file that fully defines the device and can be found in the repository of each device ([e.g.](https://raw.githubusercontent.com/harp-tech/device.behavior/main/device.yml)). This file can be seen as the "ground-truth" specification of the device. It is used to automatically generate documentation, interfaces and data ingestion tools. While this is not a strict requirement, it is highly recommended.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- `<DeviceName>` should match the `device.yml` metadata file that fully defines the device and can be found in the repository of each device ([e.g.](https://raw.githubusercontent.com/harp-tech/device.behavior/main/device.yml)). This file can be seen as the "ground-truth" specification of the device. It is used to automatically generate documentation, interfaces and data ingestion tools. While this is not a strict requirement, it is highly recommended.
- `<DeviceName>` should match the name in the `device.yml` metadata file that fully defines the device and can be found in the repository of each device ([e.g.](https://raw.githubusercontent.com/harp-tech/device.behavior/main/device.yml)). This file can be seen as the "ground-truth" specification of the device. It is used to automatically generate documentation, interfaces and data ingestion tools. While this is not a strict requirement, it is highly recommended.

Do we need to require that <DeviceName> matches the device type, or is it enough to match the device folder? In our current standards we tend to have individual files inside container folders be named following the full hierarchy structure leading up to the file.

This is useful in cases where there may be other types of files stored in the experimental device folder which are not harp binary files themselves, but are data stored and synched together with this device (e.g. video frame data).

In this case if we change the convention for non-Harp files the naming convention will be inconsistent. Alternatively, if we do change the convention to name it after the Harp device type, the naming convention may be misleading, e.g. consider the following:

CameraTop
 ┣ 📜Behavior_0.bin
 ┣ 📜Behavior_1.bin
 ┗ 📜CameraTop_Video.avi
CameraSide
 ┣ 📜Behavior_0.bin
 ┣ 📜Behavior_1.bin
 ┗ 📜CameraSide_Video.avi

If we do want to include the device type in the name I guess we could add the container name as a prefix and keep the harp device type in the file:

CameraTop
 ┣ 📜CameraTop_Behavior_0.bin
 ┣ 📜CameraTop_Behavior_1.bin
 ┗ 📜CameraTop_Video.avi
CameraSide
 ┣ 📜CameraSide_Behavior_0.bin
 ┣ 📜CameraSide_Behavior_1.bin
 ┗ 📜CameraSide_Video.avi

There are definitely other possible alternatives such as nested folders, etc, but as above I feel somehow we should avoid being overly prescriptive. It is true that the current harp-python currently adheres to a specific structure, but perhaps we could make it flexible to adhere to a variety of options.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure the standard should encompass multi-file-type containers. In my mind, if you consider the harp container as "indivisible", your use case could instead be modelled as:

CameraTop
┣   CameraTop.harp
   ┣ 📜CameraTop_Behavior_0.bin
   ┣ 📜CameraTop_Behavior_1.bin
┗ 📜CameraTop_Video.avi

What do we gain by doing this? In my mind more predictability of the file structure. At the end of the day, the reason for standardizing the structure are, in my mind:

  1. to know what device we have
    a. If device.yml is present, we are done
    b. if no device.yml is present, it would be nice to at least have a sane way to find where the whoami register is so we can read it and attempt to fetch the device.yml
    2.to have a patterned way to open all files inside the container in a easily automated way.

If we want to allow for multiple file names inside the container, I am worried about running into the following situation:

CameraTop
 ┣ 📜CameraTop_Behavior_0.bin
 ┣ 📜CameraTop_Behavior_1.bin
 ┣ 📜CameraTop_Wheel_0.bin
 ┣ 📜CameraTop_Wheel_1.bin
 ┗ 📜CameraTop_Video.avi

If I now want to ingest this datastream using the harp python library I would need to solve the generate case of Behavior vs Wheel. I am sure this can be handle by a custom regexp, but I would rather consider that as outside the scope of this standard. Otherwise, not really sure we gain much by standardizing anything other than "all harp files are expected to have data from a single register, wherein all messages have the same length and data type".

- `<Device>` is an arbitrary name that identifies the device being used.
- `<Reg>` is the register number that is logged in the binary file.
- `<suffix>` is an optional suffix that can be co-opted by the user to add any additional information to the file name (e.g. a timestamp, a sequence number, etc). If there is no `<suffix>`, the final `_` should be omitted.
- `.harp` is the extension for the container folder.

### The optional `device.yml` file

Including the `device.yml` file that corresponds to the interface used to log the device's data is recommended. To do this, we place a `device.yml` file at the root of the container folder. The folder structure thus becomes:
```plaintext
📦<Device>.harp
┣ 📜<DeviceName>_0_<suffix>.bin
┣ 📜<DeviceName>_1_<suffix>.bin
┣ ...
┣ 📜<DeviceName>_<Reg>_<suffix>.bin
┗ 📜device.yml (Optional) ```
---
```

## Best practices and application notes

### Logging the device's initial configuration

Most registers in a Harp device will not emit periodic events. As such, it is impossible to know their state unless explicitly queried. For configuration registers we do want to know this state, since it will define the behavior of the device at runtime. We also want to include metadata registers such as the device name and versions. Fortunately, the [Device specification](./Device.md) defines a feature for dumping the values of all registers during acquisition. By sending a single message to the `R_OPERATION_CTRL` register with `Bit3` set to 1, we can make the device send a rapid sequence of `READ` type messages with the contents of all registers.

> [!IMPORTANT]
> In your experiments, always validate that your logging routine has fully initialized before requesting a read dump from the device. Failure to do so may result in missing data.


## Release notes

- v0.1
* First draft.