C++
C++ CMakeクライアントライブラリは、C++プロジェクト用のプロトコルコードを生成するヘッダーファイルとCMakeサポートを提供します。
依存関係
-
CMake
-
ProtobufおよびProtobufコンパイラー
-
gRPCおよびgRPCコンパイラープラグイン
このライブラリは、最新のLTSリリースであるUbuntu 26.04 LTSとDebian 13でビルドおよびテストされています。ディストリビューションのパッケージをインストールします。
$ sudo apt update
$ sudo apt install -y libgrpc++-dev protobuf-compiler-grpc libprotobuf-dev
その他のディストリビューションでは、依存関係をご自身でコンパイルし、システムにインストールしてください。
CMakeプロジェクトにライブラリを追加する
FetchContent
CMake FetchContentモジュールは、ソースコードを取得し、ターゲットをプロジェクトに追加します。
include(FetchContent)
FetchContent_Declare(blickfeld-qb2
GIT_REPOSITORY https://github.com/Blickfeld/blickfeld-qb2.git
GIT_TAG main
)
FetchContent_MakeAvailable(blickfeld-qb2)
これで、CMakeプロジェクトでblickfeld-qb2ターゲットを使用できます。
インストール
リポジトリをクローンし、CMakeで設定およびビルドして、システムにインストールします。
$ git clone https://github.com/Blickfeld/blickfeld-qb2.git
$ mkdir blickfeld-qb2/build
$ cd blickfeld-qb2/build
$ cmake ..
$ make -j
$ sudo make install
これで、CMakeプロジェクトからライブラリが見つかるようになります。
find_package(blickfeld-qb2 REQUIRED)
これで、CMakeプロジェクトでblickfeld-qb2ターゲットを使用できます。
ライブラリを使用する
target_link_librariesで、blickfeld-qb2を実行ファイルまたはライブラリにリンクします。
find_package(blickfeld-qb2 REQUIRED) # or FetchContent
add_executable(example main.cpp)
target_link_libraries(example PUBLIC blickfeld-qb2)
センサーに接続する
blickfeld::hardware::connect_to_deviceは、センサーへの暗号化された接続を開きます。接続は渡したアプリケーションキーで認証されます。
| 引数 | 値 |
|---|---|
|
センサーのホスト名またはIPアドレス。 |
|
センサーのシリアル番号。アプリケーションキーと併せて指定する必要があります。指定しない場合、 |
|
センサー上のアプリケーションキーのシークレット。 |
#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;
}
|
例の アプリケーションキーが無効な場合、チャネル上の最初のAPI呼び出しは gRPCステータスコード14、 |
センサーのすべてのAPIサービスにこのチャネルを使用してください。1つのチャネルで任意の数のリクエストを処理できます。
ポイントクラウドを取得する
サービスの同期スタブは、リクエストと書き込み先のレスポンスを受け取り、grpc::Statusを返します。
#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;
}
ポイントクラウドを処理する
responseには、ポイントクラウドデータを含むframeが格納されています。PointCloud Getを参照してください。
パフォーマンスのため、フレームはポイントをバイナリフィールドで伝送します。 読み取るには、バイナリフィールドをそれぞれの型にキャストしてください。
#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);
}
ポイントクラウドをストリーミングする
ストリーミングの例は、各フレームを上記のprocess_frameに渡します。この関数を同じファイルの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としてビルドしたストリーミング例の出力$ ./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
...
リポジトリのexamplesでは、認証の方法と、ライブラリをプロジェクトに追加する方法を示しています。 gRPC自体については、 gRPC C++のドキュメントを参照してください。