SDKC++ SDK

Quick start

Five runnable scenarios: raw pose, Skeleton, and the three dexterous hand mappings.

Current version · 2.0.0

Quick start

See Example index for all examples and their main APIs.

Complete Installation & requirements and connect the glove. Choose a scenario, save its code as main.cpp, and build the my_qnbot_app CMake target as shown below. Rebuild after switching scenarios. Stop continuously running scenarios with Ctrl+C; the many-to-many scenario exits on its own.

The four later scenarios also use example_support.hpp; save this file next to main.cpp first.

Read raw pose data

Pose comes straight from the device, without SDK processing and without an algorithm package:

Save the following code as main.cpp, then configure and build the project below.

Zero-argument configuration that auto-discovers the single supported glove with the default GloveConfig and receives pose frames through a callback:

#include <qnbot/glove.hpp>

#include <cstdlib>
#include <exception>
#include <iostream>
#include <utility>

int main() {
    try {
        qnbot::SdkConfig config;
        config.devices = {qnbot::GloveConfig{}};

        qnbot::Sdk sdk(std::move(config));
        auto glove = sdk.glove();

        auto device = glove.device();
        const auto subscription = device.pose().subscribe(
            [](const qnbot::Sample<qnbot::GlovePose>& sample) {
                std::cout << "pose sequence=" << sample.sequence
                          << " fingertips="
                          << (sample.value.payload.hand
                                  ? sample.value.payload.hand->fingertips.size()
                                  : 0)
                          << '\n';
            });

        try {
            glove.start();
            std::cout << "running; press Ctrl+C to stop" << std::endl;
            glove.run_forever();
        } catch (...) {
            glove.close();
            throw;
        }
        static_cast<void>(subscription);
        glove.close();
        return EXIT_SUCCESS;
    } catch (const std::exception& error) {
        std::cerr << "qnbot quick start failed: " << error.what() << '\n';
        return EXIT_FAILURE;
    }
}

Prepare the project once

Save this CMakeLists.txt beside main.cpp, then configure from that directory using the installation method already set up in Installation.

cmake_minimum_required(VERSION 3.16)
project(my_qnbot_app LANGUAGES CXX)
find_package(qnbot_glove CONFIG REQUIRED)
add_executable(my_qnbot_app main.cpp)
target_link_libraries(my_qnbot_app PRIVATE qnbot::glove)

Conan — generate the dependency files, then configure for your platform:

conan install --requires="qnbot-sdk-glove/2.0.0" -r=qnbot --build=never --update -s build_type=Release -g CMakeDeps -g CMakeToolchain --output-folder=build/conan

macOS / Linux

cmake -S . -B build/conan -DCMAKE_TOOLCHAIN_FILE=conan_toolchain.cmake -DCMAKE_BUILD_TYPE=Release

Windows (PowerShell)

cmake -S . -B build/conan -G "Visual Studio 17 2022" -A x64 -DCMAKE_TOOLCHAIN_FILE=conan_toolchain.cmake

vcpkg — use the same installed triplet and VCPKG_ROOT; the following examples use macOS ARM64 or Windows x64:

macOS ARM64

cmake -S . -B build/vcpkg -DCMAKE_TOOLCHAIN_FILE="$VCPKG_ROOT/scripts/buildsystems/vcpkg.cmake" -DVCPKG_TARGET_TRIPLET=arm64-osx -DVCPKG_MANIFEST_MODE=OFF -DCMAKE_BUILD_TYPE=Release

On Linux, replace arm64-osx with the installed x64-linux or arm64-linux triplet.

Windows (PowerShell)

cmake -S . -B build/vcpkg -G "Visual Studio 17 2022" -A x64 -DCMAKE_TOOLCHAIN_FILE="$env:VCPKG_ROOT/scripts/buildsystems/vcpkg.cmake" -DVCPKG_TARGET_TRIPLET=x64-windows -DVCPKG_MANIFEST_MODE=OFF

Build and run every scenario

The commands below use Conan. After switching scenarios, save the code as main.cpp and rebuild. In a new shell, load Conan’s run script before launching. For vcpkg, replace build/conan with build/vcpkg and omit the Conan script; run ./build/vcpkg/my_qnbot_app on macOS/Linux or .\build\vcpkg\Release\my_qnbot_app.exe in PowerShell, with the same scenario arguments. Windows vcpkg builds copy dependent DLLs beside the executable by default. For later Conan scenarios in PowerShell, use cmd /c "build\conan\conanrun.bat && build\conan\Release\my_qnbot_app.exe <same arguments>".

Run:

cmake --build build/conan --config Release
. ./build/conan/conanrun.sh
./build/conan/my_qnbot_app

Windows (PowerShell):

cmake --build build/conan --config Release
cmd /c "build\conan\conanrun.bat && build\conan\Release\my_qnbot_app.exe"

Expected output (shown for received hand samples, with SDK initialization and connection logs omitted; the sequence number increases per frame and the fingertip count is 5):

running; press Ctrl+C to stop
pose sequence=1 fingertips=5
pose sequence=2 fingertips=5

The process keeps running and prints continuously. If multiple gloves are found, use SerialConnection to select one, see Device discovery & connection.

Skeleton

Save this scenario as main.cpp.

Adds SDK-processed data on top of pose reading: subscribes to the dynamic OpenXR hand skeleton and reads joint angles and bone poses. Skeleton is provided by the SDK and needs no algorithm package; the first run needs one capture calibration, which later runs reuse. See Reading retargeting data for the definition.

Keep the SDK scheduling advancing as described in Runtime modes.

On macOS, the first device.skeleton() call can take time for one-time preparation. Make this call before starting the device, as shown below, and before entering a latency-sensitive loop. Device selection and raw pose reading do not require requesting Skeleton.

#include <qnbot/glove.hpp>

#include "example_support.hpp"

#include <cstdlib>
#include <iostream>
#include <optional>
#include <string>

int main(int argc, char** argv) {
    try {
        std::string port;
        std::optional<qnbot::Side> side;
        for (int index = 1; index < argc; ++index) {
            const std::string argument = argv[index];
            if (argument == "--port") {
                port = example::require_value(argc, argv, index, argument);
            } else if (argument == "--side") {
                side = example::parse_side(
                    example::require_value(argc, argv, index, argument));
            } else {
                throw std::invalid_argument("unknown argument: " + argument);
            }
        }
        if (port.empty()) throw std::invalid_argument("--port is required");
        if (!side) throw std::invalid_argument("--side is required");

        qnbot::Sdk sdk(example::serial_config(port, *side));
        auto glove = sdk.glove();

        auto device = glove.device();
        auto skeleton = device.skeleton();
        const auto joint_angles_subscription =
            skeleton.joint_angles().subscribe(
                [](const qnbot::Sample<qnbot::HandJointCommand>& sample) {
                    std::cout << "skeleton sequence=" << sample.sequence
                              << " target=" << sample.value.target
                              << " joints=" << sample.value.joints.size()
                              << '\n';
                });
        try {
            glove.start();
            std::cout << "running; press Ctrl+C to stop" << std::endl;
            glove.run_forever();
        } catch (...) {
            glove.close();
            throw;
        }
        static_cast<void>(joint_angles_subscription);
        glove.close();
        return EXIT_SUCCESS;
    } catch (const std::exception& error) {
        return example::report_error(error);
    }
}

Run:

./build/conan/my_qnbot_app --port <serial-port> --side right

Expected output:

running; press Ctrl+C to stop
skeleton sequence=1 target=hand.skeleton.right joints=25

Each line shows the sample sequence, target name and joint count (25).

Dexterous hand output

The three mappings differ only in how gloves map to dexterous hands:

ScenarioMappingExample
One-to-oneOne glove drives one named targetmain.cpp
One-to-manyOne glove drives several dexterous handsmain.cpp
Many-to-manySeveral gloves drive several dexterous handsmain.cpp

All three mappings need an algorithm package installed first, see Reading retargeting data — these values are the retargeting result the package computes for each target. On the first run, the SDK guides capture through its fixed eight hand gestures, then computes calibration for each target. Compatible capture and calibration results are reused on later runs.

One-to-one: one glove drives one named target

The program runs continuously in the current thread and subscribes to the target output.

#include "example_support.hpp"

#include <cstdlib>
#include <iostream>

int main(int argc, char** argv) {
    try {
        const auto options = example::parse_serial_options(
            argc, argv, example::SerialExample::foreground);
        const auto side = *options.side;
        qnbot::SdkConfig config;
        qnbot::SerialConnection connection;
        connection.port = options.port;
        config.devices.push_back(qnbot::GloveConfig{side, connection});
        qnbot::TargetConfig target;
        target.name = example::default_target_name;
        target.side = side;
        target.source = qnbot::DeviceSelector{"glove", std::nullopt, side};
        target.algorithms = {qnbot::TargetAlgorithm{options.package_id}};
        config.targets.push_back(target);
        qnbot::Sdk sdk(config);
        auto glove = sdk.glove();

        auto device = glove.device();
        const auto subscription = device.output(example::default_target_name)
                                      .subscribe(example::print_output);
        glove.start();
        try {
            std::cout << "running; press Ctrl+C to stop" << std::endl;
            glove.run_forever();
        } catch (...) {
            glove.close();
            throw;
        }
        static_cast<void>(subscription);
        glove.close();
        return EXIT_SUCCESS;
    } catch (const std::exception& error) {
        return example::report_error(error);
    }
}

Run:

./build/conan/my_qnbot_app --port <serial-port> --side right --package-id <package-id>

Expected output (target defaults to hand.openxr_hand.<side>; joint values are elided):

running; press Ctrl+C to stop
retargeting sequence=1 target=hand.openxr_hand.right joints={…}

If no output arrives, check the algorithm package ID, the target configuration and the calibration status, see Connected but no data.

One-to-many: one glove drives several dexterous hands

Configures several named targets for the same source and reads each output by name; the glove connection and data stream stay single.

#include "example_support.hpp"

#include <cstdlib>
#include <iostream>
#include <optional>
#include <string>

namespace {

struct Options {
    std::string port;
    std::optional<qnbot::Side> side;
    std::string package_id;
};

Options parse_options(int argc, char** argv) {
    Options options;
    for (int index = 1; index < argc; ++index) {
        const std::string argument = argv[index];
        if (argument == "--port") {
            options.port = example::require_value(argc, argv, index, argument);
        } else if (argument == "--side") {
            options.side = example::parse_side(
                example::require_value(argc, argv, index, argument));
        } else if (argument == "--package-id") {
            options.package_id =
                example::require_value(argc, argv, index, argument);
        } else {
            throw std::invalid_argument("unknown argument: " + argument);
        }
    }
    if (options.port.empty()) throw std::invalid_argument("--port is required");
    if (!options.side) throw std::invalid_argument("--side is required");
    if (options.package_id.empty()) {
        throw std::invalid_argument("--package-id is required");
    }
    return options;
}

qnbot::SdkConfig make_config(const Options& options) {
    auto config = example::serial_config(options.port, *options.side);
    qnbot::TargetConfig target;
    target.name = "primary";
    target.side = options.side;
    target.source = qnbot::DeviceSelector{"glove", std::nullopt, options.side};
    target.algorithms = {qnbot::TargetAlgorithm{options.package_id}};
    config.targets.push_back(target);
    target.name = "backup";
    config.targets.push_back(std::move(target));
    return config;
}

} // namespace

int main(int argc, char** argv) {
    try {
        const auto options = parse_options(argc, argv);
        qnbot::Sdk sdk(make_config(options));
        auto glove = sdk.glove();

        auto device = glove.device();
        const auto primary_subscription = device.output("primary").subscribe(
            [](const qnbot::Sample<qnbot::HandJointCommand>& sample) {
                std::cout << "primary ";
                example::print_output(sample);
            });
        const auto backup_subscription = device.output("backup").subscribe(
            [](const qnbot::Sample<qnbot::HandJointCommand>& sample) {
                std::cout << "backup ";
                example::print_output(sample);
            });

        try {
            glove.start();
            std::cout << "running; press Ctrl+C to stop" << std::endl;
            glove.run_forever();
        } catch (...) {
            glove.close();
            throw;
        }
        static_cast<void>(primary_subscription);
        static_cast<void>(backup_subscription);
        glove.close();
        return EXIT_SUCCESS;
    } catch (const std::exception& error) {
        return example::report_error(error);
    }
}

Run:

./build/conan/my_qnbot_app --port <serial-port> --side right --package-id <package-id>

Expected output (each target's output carries its own name and channel sequence; joint values elided):

running; press Ctrl+C to stop
primary retargeting sequence=1 target=hand.primary.right joints={…}
backup retargeting sequence=1 target=hand.backup.right joints={…}
primary retargeting sequence=2 target=hand.primary.right joints={…}

The two callbacks can run on different threads, and one output line is assembled from several std::cout writes, so real output can interleave inside a line (observed: backup retargeting sequence=primary retargeting sequence=1 target=…); synchronise yourself when you need whole lines. For the full target configuration, see Reading retargeting data.

Many-to-many: several gloves drive several dexterous hands

Each glove gets its own target; the program selects each device and reads its own output. The script first waits for both outputs, stops the right device to confirm the left keeps advancing, then restarts it.

#include "example_support.hpp"

#include <cstdlib>
#include <chrono>
#include <iostream>
#include <optional>
#include <string>
#include <thread>
#include <unordered_set>

namespace {

struct Options {
    std::string left_port;
    std::string right_port;
    std::uint64_t updates{10};
    std::string package_id;
};

Options parse_options(int argc, char** argv) {
    Options options;
    for (int index = 1; index < argc; ++index) {
        const std::string argument = argv[index];
        if (argument == "--left-port") {
            options.left_port =
                example::require_value(argc, argv, index, argument);
        } else if (argument == "--right-port") {
            options.right_port =
                example::require_value(argc, argv, index, argument);
        } else if (argument == "--updates") {
            options.updates = example::parse_positive_count(
                example::require_value(argc, argv, index, argument), argument);
        } else if (argument == "--package-id") {
            options.package_id =
                example::require_value(argc, argv, index, argument);
        } else {
            throw std::invalid_argument("unknown argument: " + argument);
        }
    }
    if (options.left_port.empty()) {
        throw std::invalid_argument("--left-port is required");
    }
    if (options.right_port.empty()) {
        throw std::invalid_argument("--right-port is required");
    }
    if (options.left_port == options.right_port) {
        throw std::invalid_argument(
            "--left-port and --right-port must be different");
    }
    if (options.package_id.empty()) {
        throw std::invalid_argument("--package-id is required");
    }
    return options;
}

qnbot::SdkConfig make_config(const Options& options) {
    qnbot::SerialConnection left_connection;
    left_connection.port = options.left_port;
    qnbot::GloveConfig left{qnbot::Side::left, left_connection};
    left.name = "left";

    qnbot::SerialConnection right_connection;
    right_connection.port = options.right_port;
    qnbot::GloveConfig right{qnbot::Side::right, right_connection};
    right.name = "right";

    qnbot::TargetConfig left_target;
    left_target.name = example::default_target_name;
    left_target.side = qnbot::Side::left;
    left_target.source =
        qnbot::DeviceSelector{"glove", std::string("left"), std::nullopt};
    left_target.algorithms = {qnbot::TargetAlgorithm{options.package_id}};

    qnbot::TargetConfig right_target;
    right_target.name = example::default_target_name;
    right_target.side = qnbot::Side::right;
    right_target.source =
        qnbot::DeviceSelector{"glove", std::string("right"), std::nullopt};
    right_target.algorithms = {qnbot::TargetAlgorithm{options.package_id}};

    qnbot::SdkConfig config;
    config.devices = {left, right};
    config.targets = {left_target, right_target};
    config.algorithms.capture.interaction =
        qnbot::CaptureInteractionMode::external;
    return config;
}

void print_output(const char* device_name,
                  const qnbot::Sample<qnbot::HandJointCommand>& sample) {
    std::cout << device_name << ' ';
    example::print_output(sample);
}

void handle_capture_prompt(
    const qnbot::ReadChannel<qnbot::CaptureProgress>& capture_progress,
    const qnbot::CaptureControl& capture_control,
    const qnbot::ReadChannel<qnbot::CalibrationProgress>& calibration_progress,
    std::unordered_set<std::string>& handled_request_ids) {
    if (const auto capture = capture_progress.latest()) {
        if (capture->value.session_state ==
            qnbot::CaptureSessionState::failed) {
            const auto message = capture->value.failure
                                     ? capture->value.failure->message
                                     : "unknown error";
            throw std::runtime_error("capture failed: " + message);
        }
        const auto& stage = capture->value.stage;
        if (stage.state == qnbot::CaptureStageState::awaiting_confirmation &&
            stage.request_id &&
            handled_request_ids.count(*stage.request_id) == 0) {
            std::cout << (stage.prompt_en ? *stage.prompt_en : stage.prompt)
                      << " [Y/n]: ";
            std::string answer;
            std::getline(std::cin, answer);
            if (answer.empty() || answer == "y" || answer == "Y" ||
                answer == "yes" || answer == "YES") {
                capture_control.confirm(*stage.request_id);
            } else {
                capture_control.cancel(*stage.request_id);
                throw std::runtime_error("capture was cancelled");
            }
            handled_request_ids.insert(*stage.request_id);
        }
    }
    if (const auto calibration = calibration_progress.latest();
        calibration &&
        calibration->value.job.state == qnbot::CalibrationJobState::failed) {
        const auto message = calibration->value.job.failure
                                 ? calibration->value.job.failure->message
                                 : "unknown error";
        throw std::runtime_error("calibration failed: " + message);
    }
}

} // namespace

int main(int argc, char** argv) {
    try {
        const auto options = parse_options(argc, argv);
        qnbot::Sdk sdk(make_config(options));
        auto glove = sdk.glove();

        auto left = glove.device("left");
        auto right = glove.device(qnbot::Side::right);
        auto left_output = left.output(example::default_target_name);
        auto right_output = right.output(example::default_target_name);
        auto left_capture_progress = left.capture_progress();
        auto right_capture_progress = right.capture_progress();
        auto left_calibration_progress =
            left.calibration_progress(example::default_target_name);
        auto right_calibration_progress =
            right.calibration_progress(example::default_target_name);
        auto left_capture_control = left.capture_control();
        auto right_capture_control = right.capture_control();
        left.start();
        right.start();

        const auto wait_for_output = [&](const auto& output,
                                         const auto& capture_progress,
                                         const auto& capture_control,
                                         const auto& calibration_progress) {
            std::unordered_set<std::string> handled_request_ids;
            const auto deadline =
                std::chrono::steady_clock::now() + std::chrono::seconds(300);
            while (!output.latest() &&
                   std::chrono::steady_clock::now() < deadline) {
                const auto update = glove.update();
                handle_capture_prompt(capture_progress, capture_control,
                                      calibration_progress,
                                      handled_request_ids);
                if (update.sleep() == qnbot::SleepResult::no_tasks)
                    std::this_thread::sleep_for(std::chrono::milliseconds(10));
            }
            if (!output.latest()) {
                throw std::runtime_error(
                    "retargeting output was not ready before the timeout");
            }
        };
        wait_for_output(left_output, left_capture_progress,
                        left_capture_control, left_calibration_progress);
        wait_for_output(right_output, right_capture_progress,
                        right_capture_control, right_calibration_progress);

        for (std::uint64_t index = 0; index < options.updates; ++index) {
            const auto update = glove.update();
            if (update.sleep() == qnbot::SleepResult::no_tasks)
                std::this_thread::sleep_for(std::chrono::milliseconds(10));
            if (const auto sample = left_output.latest()) {
                print_output("left", *sample);
            }
            if (const auto sample = right_output.latest()) {
                print_output("right", *sample);
            }
        }

        const auto left_before_stop = left_output.latest();
        if (!left_before_stop) {
            throw std::runtime_error(
                "both gloves must produce output before lifecycle control");
        }

        right.stop();
        std::cout << "right device stopped; left remains active\n";
        const auto right_stopped_baseline = right_output.latest();
        if (!right_stopped_baseline) {
            throw std::runtime_error("right output was unavailable after stop");
        }
        std::optional<qnbot::Sample<qnbot::HandJointCommand>> left_sample;
        for (std::uint64_t index = 0; index < options.updates; ++index) {
            const auto update = glove.update();
            if (update.sleep() == qnbot::SleepResult::no_tasks)
                std::this_thread::sleep_for(std::chrono::milliseconds(10));
            const auto right_candidate = right_output.latest();
            if (right_candidate &&
                right_candidate->sequence > right_stopped_baseline->sequence) {
                throw std::runtime_error(
                    "right output advanced while the device was stopped");
            }
            const auto candidate = left_output.latest();
            if (candidate && candidate->sequence > left_before_stop->sequence) {
                left_sample = candidate;
                break;
            }
        }
        if (!left_sample) {
            throw std::runtime_error(
                "left output did not advance while right was stopped");
        }
        std::cout << "left output while right stopped\n";
        print_output("left", *left_sample);

        const auto right_before_restart = right_output.latest();
        if (!right_before_restart) {
            throw std::runtime_error(
                "right output was unavailable before restart");
        }
        right.start();
        std::cout << "right device restarted\n";
        std::optional<qnbot::Sample<qnbot::HandJointCommand>> right_sample;
        for (std::uint64_t index = 0; index < options.updates; ++index) {
            const auto update = glove.update();
            if (update.sleep() == qnbot::SleepResult::no_tasks)
                std::this_thread::sleep_for(std::chrono::milliseconds(10));
            const auto candidate = right_output.latest();
            if (candidate &&
                candidate->sequence > right_before_restart->sequence) {
                right_sample = candidate;
                break;
            }
        }
        if (!right_sample) {
            throw std::runtime_error(
                "right output did not advance after restart");
        }
        std::cout << "right output after restart\n";
        print_output("right", *right_sample);
        glove.close();
        return EXIT_SUCCESS;
    } catch (const std::exception& error) {
        return example::report_error(error);
    }
}

Run (both a left and a right glove have to be connected):

./build/conan/my_qnbot_app \
  --left-port <left-serial-port> --right-port <right-serial-port> --package-id <package-id>

Expected output: the terminal prints the left and right hand samples and step messages such as right device stopped; left remains active, left output while right stopped, right device restarted and right output after restart, then exits (joint values elided):

left retargeting sequence=… target=hand.openxr_hand.left joints={…}
right retargeting sequence=… target=hand.openxr_hand.right joints={…}
right device stopped; left remains active
left output while right stopped
left retargeting sequence=… target=hand.openxr_hand.left joints={…}
right device restarted
right output after restart
right retargeting sequence=… target=hand.openxr_hand.right joints={…}

With only one glove attached, or with the same glove port passed for both roles, startup fails outright:

qnbot example failed: configured left-hand glove does not match reported right-hand DeviceInfo

For the full mapping configuration, see Reading retargeting data.