04. ハロー・フロア
最初の触覚エフェクト:仮想の水平な地面で、カーソルがそこに押し込まれると反発力が発生します。この力は、単純なペナルティばねモデルです――stiffness × penetration_depth——通過 set_cursor_force Z軸に沿って加える。
学習内容:
- 使用
set_cursor_forceバネ力を加える - 読み込み
cursor_position力をリアルタイムで計算し - (Python)設定 ワークスペースのプリセット(
arm_front_centered),原点を作業空間の中心に位置づける - (Python)による
keyboard地面の高さと剛性をインタラクティブに調整する
ワークフロー
- 開く
ws://localhost:10001WebSocket を確立し、最初のステータスフレームを待ちます。 - 最初のフレームで:登録 会話プロファイル。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 パッケージ(Linux では権限の昇格が必要):
↑/↓——地面の高さを上げる/下げる←/→——剛性を下げる/上げるR——デフォルト値に戻す
サービスサイクルにおいて、力は加算可能です。つまり、デバイスに送信される前に、各発生源からの力が集計されます。このチュートリアルのような例は、他の力発生源と共存することができ、互いに干渉することはありません。
読み込まれたステータスフィールド
出典: data.inverse3[i].state:
cursor_position.z—vec3,貫通深度の計算に用いるcurrent_cursor_force— テレメトリ報告用
送信 / 受信
各サイクル:カーソルのZ座標を読み取り、計算する force_z = max(0, (floor_pos - z) * stiffness),そして1通のメッセージを送信し、 set_cursor_force コマンド。第1条で送信されるメッセージには、セッション設定ファイル(全バージョン)が添付され、Python版の場合はさらに configure.preset: arm_front_centered。
- Python
- 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> 一時的な設定ファイルを保存する――設定されていない場合、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(ベースとマウント)