02. VerseGrip 인쇄
첫 번째 유선 VerseGrip의 방향(쿼터니언 + Z-X-Y 오일러 각도), 홀 센서 레벨 및 버튼 상태를 스트리밍합니다.
배울 내용:
- 읽기
quaternion상태 프레임에 대한 방향 - 쿼터니언을 도(°) 단위의 Z-X-Y 오일러 각도로 변환하기 (+X는 오른쪽, +Y는 앞쪽, +Z는 위쪽)
- 사용
probe_orientation독립형 관찰자 키팔라이브로서 - 첫 번째 메시지만 사용하는 핸드셰이크 패턴 ( 튜토리얼 01과 동일)
작업 흐름
- 다음으로 WebSocket 연결을 열기
ws://localhost:10001그리고 첫 번째 상태 프레임이 나타날 때까지 기다립니다. - 최초의 유선 VerseGrip을 선택하세요
device_id~에서verse_grip배열. - 다음과 같이 요청을 생성합니다. 세션 프로필 그리고 기기당
probe_orientationkeepalive (상태 프레임에서 그립 방향을 지속적으로 유지하도록 하는 빈 객체 명령어). - 요청을 전송한 다음,
session필드 — 일회성 핸드셰이크입니다. - 이후 모든 프레임에서 쿼터니언을 오일러 각도로 변환하고, 전송 속도를 조절하여 텔레메트리 데이터를 출력합니다. 매 틱마다 키팔라이브를 재전송합니다.
매개변수
| 이름 | 기본값 | 목적 |
|---|---|---|
URI | ws://localhost:10001 | 시뮬레이션 채널 WebSocket URL |
PRINT_EVERY_MS | 100 | 콘솔 출력 스로틀 |
| 세션 프로필 이름 | co.haply.inverse.tutorials:print-verse-grip | Haply Hub에서 이 시뮬레이션을 식별합니다 |
응용 좌표계에서 변환은 내재적 Z-X-Y(요 → 피치 → 롤) 변환입니다. +X right, +Y forward, +Z up. 하지 마세요 사용 glm::eulerAngles — 이 코드는 다른 규칙을 따르기 때문에 여기서는 올바르게 표시되지 않습니다. 세 가지 언어 변형 모두 동일한 수학적 연산을 구현하고 있습니다. 공식은 소스 코드를 참조하십시오.
probe_orientation 실제로 필요한지probe_orientation 세션이 활성화된 경우에만 유용합니다 ~하지 않는다 Inverse3 명령을 전송하세요. Inverse3 명령 Inverse3 힘, 위치, 토크 등)을 내리는 즉시, 이 서비스는 페어링된 VerseGrip의 방향을 모든 상태 프레임마다 자동으로 스트리밍합니다. 별도의 프로브가 필요하지 않습니다. 사용법 probe_orientation 이 튜토리얼과 같은 독립형 그립 모니터링 도구에만 해당됩니다.
상태 필드 읽기
발신자 data.verse_grip[0].state:
orientation—quaternion(w, x, y, z)hall— 홀 센서 측정값(정수)button— 부울
보내기 / 받기
웹소켓 루프: 상태 프레임 수신, 핸드셰이크 구성 및 전송 + probe_orientation keepalive. 첫 번째 송신 메시지에는 세션 프로필이 포함되며, 그 이후의 모든 프레임에는 keepalive만 포함됩니다.
- 파이썬
- C++ (nlohmann)
- C++ (Glaze)
단일 비동기 루프 — recv() → 빌드 명령어 → send() → 반복.
async with websockets.connect(URI) as websocket:
while True:
msg = await websocket.recv()
data = json.loads(msg)
if first_message:
first_message = False
device_id = data["verse_grip"][0]["device_id"]
request_msg = {
"session": {"configure": {"profile": {
"name": "co.haply.inverse.tutorials:print-verse-grip"}}},
"verse_grip": [{
"device_id": device_id,
"commands": {"probe_orientation": {}} # empty — keepalive
}]
}
await websocket.send(json.dumps(request_msg))
request_msg.pop("session", None) # one-shot handshake
libhv는 I/O 스레드에서 WebSocket을 처리하며, 프레임별 작업은 ws.onmessage. 메인 스레드가 ENTER 키 입력 시 응답을 멈춥니다.
ws.onmessage = [&](const std::string &msg) {
const json data = json::parse(msg);
if (first_message) {
first_message = false;
device_id = data["verse_grip"][0].at("device_id").get<std::string>();
request_msg = {
{"session", {{"configure", {{"profile",
{{"name", "co.haply.inverse.tutorials:print-versegrip"}}}}}}},
{"verse_grip", json::array({
{{"device_id", device_id},
{"commands", {{"probe_orientation", json::object()}}}},
})},
};
}
ws.send(request_msg.dump());
request_msg.erase("session"); // one-shot handshake
};
ws.open("ws://localhost:10001");
while (std::cin.get() != '\n') {} // block main thread
동일한 libhv 콜백 모델입니다. 타입이 지정된 구조체가 대체합니다. nlohmann::json — probe_orientation_cmd 는 빈 구조체입니다 (Glaze는 이를 다음과 같이 작성합니다 {}). std::optional<session_cmd> 일회성 핸드셰이크를 처리합니다: .reset() 첫 번째 전송 후에는 이후의 JSON 출력에서 해당 항목이 제외됩니다.
// Struct models
struct quat { float w{1.0f}, x{}, y{}, z{}; };
struct grip_state { quat orientation{}; bool button{}; uint8_t hall{}; };
struct grip_device { std::string device_id; grip_state state; };
struct devices_message { std::vector<grip_device> verse_grip; };
struct probe_orientation_cmd {}; // empty object on the wire
struct commands_message {
std::optional<session_cmd> session; // one-shot — omitted when unset
std::vector<device_commands> verse_grip;
};
// Send / receive
ws.onmessage = [&](const std::string &msg) {
devices_message data{};
if (glz::read<glz_settings>(data, msg)) return;
if (first_message) {
first_message = false;
out_cmds.session = session_cmd{ /* profile = print-versegrip */ };
device_commands dc{ .device_id = data.verse_grip[0].device_id };
dc.commands.probe_orientation = probe_orientation_cmd{};
out_cmds.verse_grip.push_back(std::move(dc));
}
std::string out_json;
(void)glz::write_json(out_cmds, out_json);
ws.send(out_json);
out_cmds.session.reset(); // one-shot handshake
};
ws.open("ws://localhost:10001");
while (std::cin.get() != '\n') {} // block main thread
관련 기사: 유형 (쿼터니언) · 제어 명령어 (probe_orientation) · 웹소켓 프로토콜 · 튜토리얼 03 (무선 VG)