C++

The C++ CMake client library brings the header files and the CMake support that generate the protocol code for your C++ project.

Dependencies

  • CMake

  • Protobuf with Protobuf Compiler

  • gRPC with gRPC Compiler Plugin

The library is built and tested on the latest LTS releases, Ubuntu 26.04 LTS and Debian 13. Install the packages of the distribution:

$ sudo apt update
$ sudo apt install -y libgrpc++-dev protobuf-compiler-grpc libprotobuf-dev

On other distributions, compile the dependencies yourself and install them into the system.

Add the library to a CMake project

FetchContent

The CMake FetchContent module fetches the source code and adds the target to your project.

include(FetchContent)
FetchContent_Declare(blickfeld-qb2
    GIT_REPOSITORY https://github.com/Blickfeld/blickfeld-qb2.git
    GIT_TAG        main
)
FetchContent_MakeAvailable(blickfeld-qb2)

The blickfeld-qb2 target is then available in the CMake project.

Install

Clone the repository, configure and build it with CMake, and install it into the system:

$ git clone https://github.com/Blickfeld/blickfeld-qb2.git
$ mkdir blickfeld-qb2/build
$ cd blickfeld-qb2/build
$ cmake ..
$ make -j
$ sudo make install

Your CMake project then finds the library:

find_package(blickfeld-qb2 REQUIRED)

The blickfeld-qb2 target is then available in the CMake project.

Use the library

Link blickfeld-qb2 to your executable or library with target_link_libraries:

find_package(blickfeld-qb2 REQUIRED) # or FetchContent

add_executable(example main.cpp)
target_link_libraries(example PUBLIC blickfeld-qb2)

Connect to the sensor

blickfeld::hardware::connect_to_device opens an encrypted connection to the sensor, authenticated with the application key you pass.

Argument Value

fqdn_or_ip

The hostname or IP address of the sensor.

serial_number

The serial number of the sensor. Required together with an application key. Without it, connect_to_device first reads the serial number from the sensor over the unencrypted port 50051, which a sensor with disable_insecure_network_ports does not answer.

application_key

The secret of an application key on the sensor.

#include <blickfeld/hardware/client.h>

int main() {
    auto channel = blickfeld::hardware::connect_to_device(
        "qb2-xxxxxxx",
        "serial-number-xxx",
        "application-key-xxxxxxx"
    );
    // Call the services of the sensor here
    return 0;
}

In the example, replace qb2-xxxxxxx with the hostname or IP address, serial-number-xxx with the serial number and application-key-xxxxxxx with the secret of the application key.

If the application key is invalid, the first API call on the channel fails with the gRPC status code 14, UNAVAILABLE.

Use the channel for every API service of the sensor. One channel serves any number of requests.

Fetch a point cloud

The synchronous stub of a service takes the request and a response to fill, and returns a grpc::Status.

Fetch one frame and print its ID
#include <blickfeld/core_processing/services/point_cloud.grpc.pb.h>
#include <blickfeld/hardware/client.h>
#include <iostream>

using namespace blickfeld::core_processing::services;

int main() {
    auto channel = blickfeld::hardware::connect_to_device(
        "qb2-xxxxxxx",
        "serial-number-xxx",
        "application-key-xxxxxxx"
    );

    // Fetch one point cloud frame
    auto service = PointCloud::NewStub(channel);
    grpc::ClientContext context;
    PointCloudGetResponse response;
    auto status = service->Get(&context, PointCloudGetRequest(), &response);
    if (!status.ok()) {
        std::cerr << "Request failed: " << status.error_message() << std::endl;
        return 1;
    }
    std::cout << "Received a frame with the ID " << response.frame().id() << std::endl;

    return 0;
}

Process a point cloud

The response holds the frame with the point cloud data, see PointCloud Get.

For performance, the frame carries its points in binary fields. Cast them to their types to read them.

Cast the binary fields and print one point
#include <blickfeld/core_processing/data/frame.pb.h>
#include <cstdint>
#include <cstdio>

void process_frame(const blickfeld::core_processing::data::Frame& frame) {
    // print frame parameters
    printf("frame size: %u\n", frame.binary().length());

    // access cartesian point data (flat array of floats)
    auto xyz = (const float*) frame.binary().cartesian().data();
    // access photon count / intensity of the points
    auto photon_counts = (const uint16_t*) frame.binary().photon_count().data();
    // access direction_id of the points
    auto direction_ids = (const uint32_t*) frame.binary().direction_id().data();

    // read out x,y,z coordinates, photon count, direction_id of a point in the frame
    int pointIndex = 10;
    float x = xyz[pointIndex * 3 + 0];
    float y = xyz[pointIndex * 3 + 1];
    float z = xyz[pointIndex * 3 + 2];
    uint16_t photon_count = photon_counts[pointIndex];
    uint32_t direction_id = direction_ids[pointIndex];

    printf("point: %d, x: %.3f m, y: %.3f m, z: %.3f m, photon count: %u, direction_id: %u\n",
           pointIndex, x, y, z, photon_count, direction_id);
}

Stream point clouds

The stream example passes each frame to process_frame from above; put that function before main in the same file.

Request a stream and read 10 frames
#include <blickfeld/core_processing/services/point_cloud.grpc.pb.h>
#include <blickfeld/hardware/client.h>
#include <iostream>

using namespace blickfeld::core_processing::services;

int main() {
    auto channel = blickfeld::hardware::connect_to_device(
        "qb2-xxxxxxx",
        "serial-number-xxx",
        "application-key-xxxxxxx"
    );

    // Stream point cloud frames
    auto service = PointCloud::NewStub(channel);
    grpc::ClientContext context;
    PointCloudStreamResponse response;
    auto stream = service->Stream(&context, PointCloudStreamRequest());

    for (int i = 0; i < 10; i++) {
        if (!stream->Read(&response)) {
            auto status = stream->Finish();
            std::cerr << "Stream failed: " << status.error_message() << std::endl;
            return 1;
        }
        std::cout << "Received a frame with the ID " << response.frame().id() << std::endl;
        process_frame(response.frame());
    }

    // Stop stream
    context.TryCancel();

    return 0;
}
Output of the stream example, built as the executable example of the CMake project above
$ ./example
Received a frame with the ID 27
frame size: 144395
point: 10, x: 2.238 m, y: -0.269 m, z: -2.248 m, photon count: 2343, direction_id: 10
Received a frame with the ID 28
frame size: 144398
point: 10, x: 2.242 m, y: -0.269 m, z: -2.227 m, photon count: 2158, direction_id: 10
...

The examples of the repository show the authentication and the ways to add the library to a project. For gRPC itself, see the gRPC C++ documentation.