C++
Die C++-CMake-Client-Bibliothek liefert die Header-Dateien und die CMake-Unterstützung, die den Protokollcode für Ihr C++-Projekt generieren.
Abhängigkeiten
-
CMake
-
Protobuf mit Protobuf-Compiler
-
gRPC mit gRPC-Compiler-Plugin
Die Bibliothek wird auf den neuesten LTS-Versionen gebaut und getestet, Ubuntu 26.04 LTS und Debian 13. Installieren Sie die Pakete der Distribution:
$ sudo apt update
$ sudo apt install -y libgrpc++-dev protobuf-compiler-grpc libprotobuf-dev
Auf anderen Distributionen kompilieren Sie die Abhängigkeiten selbst und installieren sie im System.
Bibliothek zu einem CMake-Projekt hinzufügen
FetchContent
Das Modul CMake FetchContent ruft den Quellcode ab und fügt das Target zu Ihrem Projekt hinzu.
include(FetchContent)
FetchContent_Declare(blickfeld-qb2
GIT_REPOSITORY https://github.com/Blickfeld/blickfeld-qb2.git
GIT_TAG main
)
FetchContent_MakeAvailable(blickfeld-qb2)
Das Target blickfeld-qb2 steht anschließend im CMake-Projekt zur Verfügung.
Installation
Klonen Sie das Repository, konfigurieren und bauen Sie es mit CMake und installieren Sie es im System:
$ git clone https://github.com/Blickfeld/blickfeld-qb2.git
$ mkdir blickfeld-qb2/build
$ cd blickfeld-qb2/build
$ cmake ..
$ make -j
$ sudo make install
Ihr CMake-Projekt findet die Bibliothek dann:
find_package(blickfeld-qb2 REQUIRED)
Das Target blickfeld-qb2 steht anschließend im CMake-Projekt zur Verfügung.
Bibliothek verwenden
Binden Sie blickfeld-qb2 mit target_link_libraries an Ihre ausführbare Datei oder Bibliothek:
find_package(blickfeld-qb2 REQUIRED) # or FetchContent
add_executable(example main.cpp)
target_link_libraries(example PUBLIC blickfeld-qb2)
Mit dem Sensor verbinden
blickfeld::hardware::connect_to_device öffnet eine verschlüsselte Verbindung zum Sensor, authentifiziert mit dem übergebenen Anwendungsschlüssel.
| Argument | Wert |
|---|---|
|
Der Hostname oder die IP-Adresse des Sensors. |
|
Die Seriennummer des Sensors. Zusammen mit einem Anwendungsschlüssel erforderlich. Ohne sie liest |
|
Das Geheimnis eines Anwendungsschlüssels auf dem 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;
}
|
Ersetzen Sie im Beispiel Ist der Anwendungsschlüssel ungültig, schlägt der erste API-Aufruf auf dem Kanal mit dem gRPC-Statuscode 14, |
Verwenden Sie den Kanal für jeden API-Dienst des Sensors. Ein Kanal bedient beliebig viele Anfragen.
Punktwolke abrufen
Der synchrone Stub eines Dienstes nimmt die Anfrage und eine zu füllende Antwort entgegen und gibt einen grpc::Status zurück.
#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;
}
Punktwolke verarbeiten
Die response enthält den frame mit den Punktwolkendaten, siehe PointCloud Get.
Aus Leistungsgründen überträgt der Frame seine Punkte in binären Feldern. Wandeln Sie sie in ihre Typen um, um sie zu lesen.
#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);
}
Punktwolken streamen
Das Stream-Beispiel übergibt jeden Frame an process_frame von oben; setzen Sie diese Funktion in derselben Datei vor main.
#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;
}
example des CMake-Projekts oben$ ./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
...
Die Beispiele des Repositorys zeigen die Authentifizierung und die Wege, die Bibliothek zu einem Projekt hinzuzufügen. Zu gRPC selbst siehe die gRPC-C++-Dokumentation.