04. 헬로 플로어
첫 번째 햅틱 효과: 커서가 닿으면 밀어내는 가상 수평 바닥입니다. 이 힘은 단순한 페널티 스프링으로 — stiffness × penetration_depth — Z축을 따라 도포하여 set_cursor_force.
배울 내용:
- 사용
set_cursor_force탄성력을 가하기 위해 - 읽기
cursor_position그리고 실시간으로 힘을 계산하고 - (Python) 설정하기 작업 공간 사전 설정 (
arm_front_centered) 따라서 원점이 작업 공간의 중심에 위치하게 됩니다 - (Python) 키보드를 통해 바닥 높이와 강성을 대화형으로 조정하기
keyboard패키지
작업 흐름
- 다음으로 WebSocket 연결을 열기
ws://localhost:10001그리고 첫 번째 상태 프레임이 나타날 때까지 기다립니다. - 첫 번째 프레임에서: 다음을 등록합니다. 세션 프로필. 이 Python 변종은 추가로 다음을 전송합니다
configure.preset: arm_front_centered따라서 원점은 작업 영역의 중앙에 위치하며, C++ 버전은 기기에서 이미 활성화된 설정을 그대로 사용합니다. - 모든 프레임에서: 읽기
cursor_position.z, 계산force_z = max(0, (floor_pos - z) * stiffness), 그리고 이를set_cursor_force명령어. - 이후의 틱은 힘 명령만 전송합니다. 세션 프로필은 일회성 핸드셰이크 방식입니다.
- (Python) 매 틱마다 키보드 방향키 입력을 확인하고 화면을 업데이트합니다
floor_pos/stiffness라이브.
매개변수
| 이름 | 기본값 | 목적 |
|---|---|---|
floor_pos | 0.10 m | 가상 바닥면의 Z좌표 |
stiffness | 1000 해당 없음 | 탄성 계수 (1 mm 침투 시 → 1 N) |
PRINT_EVERY_MS | 100–200 | 텔레메트리 스로틀 |
| 세션 프로필 이름 | co.haply.inverse.tutorials:hello-floor | Haply Hub에서 이 시뮬레이션을 식별합니다 |
이 Python 변형은 keyboard 패키지 (리눅스에서는 관리자 권한 필요):
↑/↓— 바닥면 높이기 / 낮추기←/→— 강성 감소 / 증가R— 기본값으로 초기화
서비스 틱 단계에서 포스는 합산됩니다. 즉, 장치로 전송되기 전에 모든 소스의 포스가 합쳐집니다. 이와 같은 튜토리얼은 다른 포스 생성기와 함께 작동할 수 있으며, 서로를 차단하지 않습니다.
상태 필드 읽기
발신자 data.inverse3[i].state:
cursor_position.z—vec3, 침투 깊이를 계산하는 데 사용됨current_cursor_force— 원격 측정용으로 보고됨
보내기 / 받기
매 프레임마다: 커서의 Z 좌표를 읽고, 계산한다 force_z = max(0, (floor_pos - z) * stiffness), 그리고 다음을 보내세요 set_cursor_force. 첫 번째 발신 메시지에는 세션 프로필(모든 변형)도 포함되며, Python의 경우, configure.preset: arm_front_centered.
- 파이썬
- C++ (nlohmann)
- C++ (Glaze)
단일 비동기 루프. 첫 번째 프레임의 핸드셰이크에 프로필 정보가 포함됩니다. configure.preset 그래서 floor_pos = 0.1 작업 공간의 중심 좌표계에 맞춰집니다.
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["inverse3"][0]["device_id"]
# Handshake: profile + preset (one-shot)
request_msg = {
"session": {"configure": {"profile": {"name": "co.haply.inverse.tutorials:hello-floor"}}},
"inverse3": [{
"device_id": device_id,
"configure": {"preset": {"preset": "arm_front_centered"}}
}]
}
else:
# Per tick: compute force along Z, send set_cursor_force
z = data["inverse3"][0]["state"]["cursor_position"]["z"]
force_z = 0.0 if z > floor_pos else (floor_pos - z) * stiffness
request_msg = {
"inverse3": [{
"device_id": device_id,
"commands": {"set_cursor_force":
{"vector": {"x": 0.0, "y": 0.0, "z": force_z}}}
}]
}
await websocket.send(json.dumps(request_msg))
libhv 콜백 모델 — onmessage는 WebSocket I/O 스레드에서 실행되며, 메인 스레드는 ENTER에서 차단됩니다. C++ 버전은 최소 핸드셰이크(세션 프로필만 지원, 프리셋 미지원)를 제공합니다.
ws.onmessage = [&](const std::string &msg) {
const json data = json::parse(msg);
// If no Inverse3 yet, ask the service to re-send the full state
if (!data.contains("inverse3") || data["inverse3"].empty()) {
ws.send(R"({"session":{"force_render_full_state":{}}})");
return;
}
json request = {};
if (first_message) {
first_message = false;
request["session"] = {{"configure", {{"profile",
{{"name", "co.haply.inverse.tutorials:hello-floor"}}}}}};
}
request["inverse3"] = json::array();
for (auto &el : data["inverse3"].items()) {
const float z = el.value()["state"]["cursor_position"]["z"].get<float>();
const float force_z = z > floor_pos ? 0.0f : (floor_pos - z) * stiffness;
request["inverse3"].push_back({
{"device_id", el.value()["device_id"]},
{"commands", {{"set_cursor_force",
{{"vector", {{"x", 0.0}, {"y", 0.0}, {"z", force_z}}}}}}},
});
}
ws.send(request.dump());
};
ws.open("ws://localhost:10001");
while (std::cin.get() != '\n') {} // block main thread
동일한 libhv 콜백 모델 — 본문만 변경됩니다. 상태와 명령어 모두에 대해 타입이 지정된 구조체를 사용합니다. std::optional<session_cmd> one-shot 프로필을 유지합니다 — Glaze는 이 속성이 설정되지 않은 경우 시리얼화된 JSON에서 이를 제외합니다.
// Struct models
struct vec3 { float x{}, y{}, z{}; };
struct inverse_state { vec3 cursor_position{}, current_cursor_force{}; };
struct inverse_device { std::string device_id; inverse_state state; };
struct devices_message { std::vector<inverse_device> inverse3; };
struct set_cursor_force_cmd { vec3 vector; };
struct commands_message {
std::optional<session_cmd> session;
std::vector<device_commands> inverse3;
};
// Send / receive
ws.onmessage = [&](const std::string &msg) {
devices_message data{};
if (glz::read<glz_settings>(data, msg)) return;
if (data.inverse3.empty()) {
ws.send(R"({"session":{"force_render_full_state":{}}})");
return;
}
commands_message out_cmds{};
if (first_message) {
first_message = false;
out_cmds.session = session_cmd{ /* profile = hello-floor */ };
}
for (const auto &dev : data.inverse3) {
const float z = dev.state.cursor_position.z;
const float force_z = z > floor_pos ? 0.0f : (floor_pos - z) * stiffness;
device_commands dc{ .device_id = dev.device_id };
dc.commands.set_cursor_force = set_cursor_force_cmd{{0.0f, 0.0f, force_z}};
out_cmds.inverse3.push_back(std::move(dc));
}
std::string out_json;
(void)glz::write_json(out_cmds, out_json);
ws.send(out_json);
};
ws.open("ws://localhost:10001");
while (std::cin.get() != '\n') {} // block main thread
튜토리얼 04도 SDK와 함께 로컬에 설치되어 있습니다. 다음 경로를 확인해 보세요. tutorials/04-haply-inverse-hello-floor/ 서비스 설치 디렉터리 아래에.
관련 기사: 제어 명령어 (set_cursor_force) · 마운트 및 작업 공간 (사전 설정) · 형식 (vec3) · 세션 · 튜토리얼 07 (베이스 및 마운트)