HAL Viewer Sample

The sample metavision_hal_viewer.cpp shows how to use Metavision HAL API for visualizing events stream.

Expected Output

The sample visualizes CD events on the screen.

Expected Output from Metavision HAL Viewer Sample

How to start

First, compile the sample as described in this tutorial.

To start the sample based on the live stream from your camera, run:

Linux

./metavision_hal_viewer

Windows

metavision_hal_viewer.exe

To start the sample based on recorded data, provide the full path to a RAW file (here, we use the file spinner.raw from our Sample Recordings):

Linux

./metavision_hal_viewer -i spinner.raw

Windows

metavision_hal_viewer.exe -i spinner.raw

To check for additional options:

Linux

./metavision_hal_viewer -h

Windows

metavision_hal_viewer.exe -h

Code Overview

Visualization of Events

To apply processing on every received CD event, you need to register a callback on the decoder, as shown in the following code snippet:

    // Get the handler of CD events
    Metavision::I_EventDecoder<Metavision::EventCD> *i_cddecoder =
        device->get_facility<Metavision::I_EventDecoder<Metavision::EventCD>>();

    if (i_cddecoder) {
        // Register a lambda function to be called on every CD events
        i_cddecoder->add_event_buffer_callback(
            [&event_analyzer](const Metavision::EventCD *begin, const Metavision::EventCD *end) {
                event_analyzer.process_events(begin, end);
            });
    }

To set up the display frame, the sensor size can be retrieved as follows:

    Metavision::I_Geometry *i_geometry = device->get_facility<Metavision::I_Geometry>();
    if (!i_geometry) {
        std::cerr << "Could not retrieve geometry." << std::endl;
        return 4;
    }

Then, the data needs to be streamed from the device to the decoder:

            // Here we polled data, so we can launch decoding
            long n_bytes;
            uint8_t *raw_data = i_eventsstream->get_latest_raw_data(n_bytes);

            // This will trigger callbacks set on decoders: in our case EventAnalyzer.process_events
            i_decoder->decode(raw_data, raw_data + n_bytes);

Accessing Triggers

You can access triggers using Metavision::I_TriggerIn and Metavision::I_TriggerOut classes. If you are not familiar with the external triggers, check the dedicated page.

Essentially, Trigger Out facility provides an output signal and Trigger In captures an external signal and merges it with the sensor native data stream.

Trigger In can also capture the output signal of Trigger Out with a specific loopback mechanism. To configure the Trigger In facility, a channel parameter is used that depends on the camera. In this sample, we store the different values of the channel in a trigger_in_channels struct that defines the main and the loopback channel for each Prophesee Camera:

    struct TriggerInConfiguration {
        int main_channel;
        int loopback_channel;
    };
    std::map<std::string, TriggerInConfiguration> trigger_in_channels{
        {"hal_plugin_gen31_evk2", {0, 3}}, {"hal_plugin_gen31_evk3", {0, 6}}, {"hal_plugin_gen31_fx3", {0, 6}},
        {"hal_plugin_gen3_fx3", {0, 6}},   {"hal_plugin_gen41_evk2", {1, 3}}, {"hal_plugin_gen4_evk2", {1, 3}},
        {"hal_plugin_gen4_fx3", {1, 6}}};

This code snippet shows how to get the trigger facilities. As you can see,if the Trigger Out facility is available, we can use the loopback mechanism. Otherwise, the Trigger In facility will require an external Signal generator to produce external trigger events:

    // On camera providing Trigger Out, we enable it and duplicate the signal on Trigger In using the loopback channel
    // On the other cameras, we enable Trigger In, but we will need to plug a signal generator to create trigger events
    // and we also set the camera as Master so that we can test the Sync Out signal if needed.
    Metavision::I_TriggerOut *i_trigger_out = device->get_facility<Metavision::I_TriggerOut>();
    Metavision::I_TriggerIn *i_trigger_in   = device->get_facility<Metavision::I_TriggerIn>();
    if (i_trigger_in) {
        if (i_trigger_out) {
            i_trigger_out->set_period(100000);
            i_trigger_out->set_duty_cycle(0.5);
            i_trigger_out->enable();
            i_trigger_in->enable(trigger_in_channels[plugin_name].loopback_channel);
        } else if (i_device_control) {
            std::cout << "Could not get Trigger Out facility" << std::endl;
            i_trigger_in->enable(0);
            i_device_control->set_mode_master();
        }
    }

Recording a RAW File

The Metavision::I_EventsStream class provides a function Metavision::I_EventsStream::log_raw_data() to record all events received from the camera to a RAW file.

The following code snippet shows how to start data recording:

    Metavision::I_EventsStream *i_eventsstream = device->get_facility<Metavision::I_EventsStream>();
    if (i_eventsstream) {
        if (out_raw_file_path != "") {
            i_eventsstream->log_raw_data(out_raw_file_path);
        }
    } else {
        std::cerr << "Could not initialize events stream." << std::endl;
        return 3;
    }

To stop recording, the method Metavision::I_EventsStream::stop_log_raw_data() has to be called.