03. 무선 VerseGrip 인쇄
튜토리얼 02와 동일한 방식이지만, 무선 VerseGrip용으로 제작되었습니다. 스트리밍되는 상태에 버튼(A/B/C)과 배터리 잔량 정보가 추가됩니다.
배울 내용:
- 무선 전용 상태 필드 읽기:
buttons.{a,b,c},battery_level,hall - 사용
probe_orientation독립형 관찰자 키팔라이브로서 - 튜토리얼 02와 동일한 ‘첫 메시지 전용’ 핸드셰이크 방식
작업 흐름
- 다음으로 WebSocket 연결을 열기
ws://localhost:10001그리고 첫 번째 상태 프레임이 나타날 때까지 기다립니다. - 최초의 Wireless VerseGrip을 선택하세요
device_id~에서wireless_verse_grip배열. - 다음과 같이 요청을 생성합니다. 세션 프로필 그리고 기기당
probe_orientationkeepalive. - 요청을 전송한 다음,
session필드 — 일회성 핸드셰이크입니다. - 이후 모든 프레임에서 쿼터니언을 오일러 각도로 변환하고, 버튼 상태 및 배터리 잔량을 포함한 제한된 텔레메트리 데이터를 출력합니다. 매 틱마다 키팔라이브를 재전송합니다.
매개변수
| 이름 | 기본값 | 목적 |
|---|---|---|
URI | ws://localhost:10001 | 시뮬레이션 채널 WebSocket URL |
PRINT_EVERY_MS | 100 | 콘솔 출력 스로틀 |
| 세션 프로필 이름 | co.haply.inverse.tutorials:print-wireless-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.wireless_verse_grip[0].state:
orientation—quaternion(w, x, y, z)hall— 홀 센서 측정값(정수)buttons.a,buttons.b,buttons.c— 부울형battery_level— float (0.0 – 1.0)
보내기 / 받기
모양이 똑같은 튜토리얼 02, 단지 wireless_verse_grip 디바이스 어레이. WebSocket 루프는 상태 프레임을 수신하고 핸드셰이크를 반환하며 + probe_orientation keepalive; 첫 번째 송신 메시지에는 세션 프로필이 포함되며, 그 이후의 모든 프레임에는 keepalive만 포함됩니다.
- 파이썬
- C++ (nlohmann)
- C++ (Glaze)
단일 비동기 루프.
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["wireless_verse_grip"][0]["device_id"]
request_msg = {
"session": {"configure": {"profile": {
"name": "co.haply.inverse.tutorials:print-wireless-verse-grip"}}},
"wireless_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
ws.onmessage = [&](const std::string &msg) {
const json data = json::parse(msg);
if (first_message) {
first_message = false;
device_id = data["wireless_verse_grip"][0].at("device_id").get<std::string>();
request_msg = {
{"session", {{"configure", {{"profile",
{{"name", "co.haply.inverse.tutorials:print-wireless-verse-grip"}}}}}}},
{"wireless_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
명명된 구조체. 참고 button_state 명시적으로 지정된 (아니면 buttons) 그림자가 드리워지지 않도록 buttons 필드에서 wvg_state — Glaze는 필드 이름을 기준으로 매핑하므로, 구조체 유형명은 자유롭게 지정할 수 있습니다.
// Struct models
struct quat { float w{1.0f}, x{}, y{}, z{}; };
struct button_state { bool a{}, b{}, c{}; };
struct wvg_state {
quat orientation{};
uint8_t hall{};
button_state buttons{};
float battery_level{};
};
struct wvg_device { std::string device_id; wvg_state state; };
struct devices_message { std::vector<wvg_device> wireless_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> wireless_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;
request_msg.session = session_cmd{ /* profile = print-wireless-verse-grip */ };
device_commands dc{ .device_id = data.wireless_verse_grip[0].device_id };
dc.commands.probe_orientation = probe_orientation_cmd{};
request_msg.wireless_verse_grip.push_back(std::move(dc));
}
std::string out_json;
(void)glz::write_json(request_msg, out_json);
ws.send(out_json);
request_msg.session.reset(); // one-shot handshake
};
ws.open("ws://localhost:10001");
while (std::cin.get() != '\n') {} // block main thread
관련 기사: 튜토리얼 02 (유선 비디오 게임) · 유형 (쿼터니언) · 제어 명령어 (probe_orientation) · 웹소켓 프로토콜