Files
smart_wasm/public/index.html
2026-06-01 17:38:29 +08:00

698 lines
24 KiB
HTML

<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>smart_wasm Local Debug</title>
<style>
:root {
--bg: #f3efe5;
--panel: #fffaf0;
--line: #d2c7b3;
--text: #1f2a30;
--muted: #6e7a80;
--accent: #0d6b5f;
--accent-2: #d7852f;
--error: #b7402a;
}
* {
box-sizing: border-box;
}
body {
margin: 0;
font-family: "Segoe UI", "Microsoft YaHei", sans-serif;
color: var(--text);
background:
radial-gradient(circle at top left, rgba(215, 133, 47, 0.18), transparent 30%),
linear-gradient(135deg, #f7f1e3 0%, #e8efe9 100%);
min-height: 100vh;
}
.shell {
max-width: 1100px;
margin: 0 auto;
padding: 32px 20px 48px;
}
.hero {
display: grid;
gap: 16px;
margin-bottom: 24px;
}
.hero h1 {
margin: 0;
font-size: clamp(28px, 5vw, 48px);
line-height: 1.05;
}
.hero p {
margin: 0;
color: var(--muted);
max-width: 720px;
font-size: 16px;
}
.layout {
display: grid;
grid-template-columns: 1.05fr 0.95fr;
gap: 20px;
}
.card {
background: rgba(255, 250, 240, 0.9);
backdrop-filter: blur(10px);
border: 1px solid var(--line);
border-radius: 20px;
box-shadow: 0 20px 40px rgba(38, 50, 56, 0.08);
overflow: hidden;
}
.card-head {
padding: 18px 20px;
border-bottom: 1px solid var(--line);
display: flex;
justify-content: space-between;
align-items: center;
gap: 12px;
}
.card-head h2 {
margin: 0;
font-size: 18px;
}
.status {
font-size: 13px;
color: var(--muted);
padding: 6px 10px;
border: 1px solid var(--line);
border-radius: 999px;
background: rgba(255, 255, 255, 0.7);
}
.body {
padding: 20px;
}
label {
display: block;
font-size: 13px;
color: var(--muted);
margin-bottom: 8px;
}
textarea {
width: 100%;
min-height: 320px;
resize: vertical;
border: 1px solid var(--line);
border-radius: 14px;
padding: 14px;
font: 13px/1.5 Consolas, "Courier New", monospace;
color: var(--text);
background: #fff;
}
.actions {
display: flex;
flex-wrap: wrap;
gap: 12px;
margin-top: 16px;
}
.sample-bar {
display: grid;
gap: 10px;
margin-top: 16px;
padding: 14px;
border: 1px solid var(--line);
border-radius: 14px;
background: rgba(255, 255, 255, 0.72);
}
.sample-row {
display: flex;
flex-wrap: wrap;
gap: 10px;
align-items: center;
}
select {
width: 100%;
border: 1px solid var(--line);
border-radius: 12px;
padding: 11px 12px;
font-size: 14px;
color: var(--text);
background: #fff;
}
button {
border: 0;
border-radius: 12px;
padding: 12px 16px;
font-size: 14px;
font-weight: 600;
cursor: pointer;
}
.primary {
background: var(--accent);
color: #fff;
}
.secondary {
background: #fff;
color: var(--text);
border: 1px solid var(--line);
}
pre {
margin: 0;
min-height: 420px;
padding: 16px;
overflow: auto;
background: #172126;
color: #e8f2ef;
font: 13px/1.5 Consolas, "Courier New", monospace;
}
.response-shell {
display: grid;
gap: 0;
}
.response-meta {
display: grid;
grid-template-columns: repeat(2, minmax(0, 1fr));
gap: 10px;
padding: 16px 20px;
background: rgba(17, 30, 35, 0.04);
border-bottom: 1px solid var(--line);
}
.response-meta-item {
padding: 10px 12px;
border: 1px solid rgba(210, 199, 179, 0.8);
border-radius: 12px;
background: rgba(255, 255, 255, 0.75);
}
.response-meta-label {
font-size: 12px;
color: var(--muted);
margin-bottom: 4px;
}
.response-meta-value {
font-size: 14px;
font-weight: 600;
color: var(--text);
word-break: break-all;
}
.response-meta-value.is-ok {
color: var(--accent);
}
.response-meta-value.is-error {
color: var(--error);
}
.json-key {
color: #7dd3fc;
}
.json-string {
color: #f9d65c;
}
.json-number {
color: #f59e0b;
}
.json-boolean {
color: #fb7185;
font-weight: 700;
}
.json-null {
color: #c084fc;
font-weight: 700;
}
.tips {
display: grid;
gap: 10px;
margin-top: 18px;
color: var(--muted);
font-size: 13px;
}
.tips code {
color: var(--accent);
}
@media (max-width: 900px) {
.layout {
grid-template-columns: 1fr;
}
textarea,
pre {
min-height: 260px;
}
.response-meta {
grid-template-columns: 1fr;
}
}
</style>
</head>
<body>
<div class="shell">
<section class="hero">
<h1>smart_wasm 本地调试页</h1>
<p>这个页面直接加载 <code>public/wasm/smart_math.js</code>,可以在本地验证 WASM 初始化、命令调用和返回 JSON。</p>
</section>
<section class="layout">
<div class="card">
<div class="card-head">
<h2>请求</h2>
<div class="status" id="module-status">WASM 未初始化</div>
</div>
<div class="body">
<label for="request">JSON Request</label>
<textarea id="request">{
"msg": "list robots after init",
"req_code": "CASE_LIST_001",
"req_from": "local_debug",
"req_cmd": "Cmd_ListRobots",
"req_param": {}
}</textarea>
<div class="sample-bar">
<label for="sample-select">接口测试样例</label>
<div class="sample-row">
<select id="sample-select">
<option value="listRobots">机器人列表 Cmd_ListRobots</option>
<option value="kinematicsForwardZero">运动学正解 Cmd_Kinematics_forward_pose_str</option>
<option value="kinematicsForwardAllJoints">全关节正解 Cmd_Kinematics_forward_all_joints</option>
<option value="spcBasic">SPC 计算 Cmd_Spc</option>
<option value="fourBarValid">四连杆有效样例 Cmd_FourBar_CrankSlider</option>
<option value="fourBarInvalid">四连杆异常样例 Cmd_FourBar_CrankSlider</option>
<option value="quadrupedPoints">四足姿态点位 Cmd_QuadrupedRobot_CalculateAllPointsFromMotorAngles</option>
<option value="quadrupedForwardGait">四足步态正解 Cmd_QuadrupedRobot_PerformForwardKinematics</option>
</select>
</div>
<div class="sample-row">
<button class="secondary" id="sample-btn" type="button">填充选中样例</button>
</div>
</div>
<div class="actions">
<button class="primary" id="init-btn" type="button">初始化 WASM</button>
<button class="primary" id="run-btn" type="button">发送请求</button>
<button class="secondary" id="clear-btn" type="button">清空输出</button>
</div>
<div class="tips">
<div>1. 先点 <code>初始化 WASM</code>,会调用 <code>_init_func()</code></div>
<div>2. 再点 <code>发送请求</code>,会调用 <code>_func()</code></div>
<div>3. 默认地址使用 <code>http://localhost:8080/</code></div>
</div>
</div>
</div>
<div class="card">
<div class="card-head">
<h2>响应</h2>
<div class="status" id="request-status">等待操作</div>
</div>
<div class="response-shell">
<div class="response-meta" id="response-meta">
<div class="response-meta-item">
<div class="response-meta-label">状态</div>
<div class="response-meta-value" id="meta-success">未执行</div>
</div>
<div class="response-meta-item">
<div class="response-meta-label">代码</div>
<div class="response-meta-value" id="meta-code">-</div>
</div>
<div class="response-meta-item">
<div class="response-meta-label">命令</div>
<div class="response-meta-value" id="meta-cmd">-</div>
</div>
<div class="response-meta-item">
<div class="response-meta-label">请求号</div>
<div class="response-meta-value" id="meta-req">-</div>
</div>
</div>
<pre id="output">No output yet.</pre>
</div>
</div>
</section>
</div>
<script src="./wasm/smart_math.js"></script>
<script>
let wasmModule = null;
const moduleStatus = document.getElementById("module-status");
const requestStatus = document.getElementById("request-status");
const requestBox = document.getElementById("request");
const output = document.getElementById("output");
const sampleSelect = document.getElementById("sample-select");
const metaSuccess = document.getElementById("meta-success");
const metaCode = document.getElementById("meta-code");
const metaCmd = document.getElementById("meta-cmd");
const metaReq = document.getElementById("meta-req");
const requestSamples = {
listRobots: {
msg: "list robots after init",
req_code: "CASE_LIST_001",
req_from: "local_debug",
req_cmd: "Cmd_ListRobots",
req_param: {
detail: true
}
},
kinematicsForwardZero: {
msg: "fk zero pose",
req_code: "CASE_FK_001",
req_from: "local_debug",
req_cmd: "Cmd_Kinematics_forward_pose_str",
req_param: {
robot_uuid: "abb_irb120_3_58",
q_init_str: "0,0,0,0,0,0"
}
},
kinematicsForwardAllJoints: {
msg: "fk all joints path",
req_code: "CASE_FK_ALL_001",
req_from: "local_debug",
req_cmd: "Cmd_Kinematics_forward_all_joints",
req_param: {
robot_uuid: "abb_irb120_3_58",
joints_str: "0,0,0,0,0,0;10,20,30,40,50,60"
}
},
spcBasic: {
msg: "spc basic 5x30",
req_code: "CASE_SPC_001",
req_from: "local_debug",
req_cmd: "Cmd_Spc",
req_param: {
n: 5,
k: 30,
usl: 1.7,
lsl: 1.5,
x: [
1.55, 1.58, 1.61, 1.6, 1.6, 1.58, 1.63, 1.63, 1.62, 1.63,
1.62, 1.63, 1.62, 1.59, 1.58, 1.58, 1.6, 1.61, 1.62, 1.63,
1.58, 1.64, 1.63, 1.62, 1.62, 1.62, 1.62, 1.63, 1.61, 1.57,
1.64, 1.62, 1.61, 1.6, 1.58, 1.57, 1.59, 1.61, 1.62, 1.63,
1.58, 1.61, 1.6, 1.62, 1.63, 1.6, 1.61, 1.64, 1.64, 1.63,
1.58, 1.6, 1.62, 1.63, 1.65, 1.62, 1.58, 1.59, 1.57, 1.58,
1.57, 1.57, 1.58, 1.59, 1.64, 1.61, 1.64, 1.62, 1.6, 1.59,
1.65, 1.62, 1.62, 1.6, 1.58, 1.57, 1.59, 1.57, 1.59, 1.62,
1.56, 1.57, 1.57, 1.61, 1.62, 1.56, 1.58, 1.59, 1.6, 1.62,
1.58, 1.6, 1.6, 1.62, 1.63, 1.58, 1.59, 1.6, 1.63, 1.62,
1.58, 1.59, 1.62, 1.63, 1.64, 1.58, 1.59, 1.62, 1.63, 1.61,
1.58, 1.59, 1.6, 1.61, 1.63, 1.57, 1.59, 1.61, 1.61, 1.62,
1.58, 1.58, 1.6, 1.61, 1.63, 1.62, 1.58, 1.58, 1.58, 1.57,
1.63, 1.59, 1.57, 1.58, 1.57, 1.58, 1.62, 1.61, 1.63, 1.61,
1.58, 1.57, 1.59, 1.6, 1.62, 1.62, 1.6, 1.6, 1.57, 1.57
]
}
},
fourBarValid: {
msg: "fourbar valid",
req_code: "CASE_FB_001",
req_from: "local_debug",
req_cmd: "Cmd_FourBar_CrankSlider",
req_param: {
L_AB: 0.5,
L_BS: 2.0,
S_OFS: 0.0,
angleDeg: 45.0
}
},
fourBarInvalid: {
msg: "fourbar invalid",
req_code: "CASE_FB_002",
req_from: "local_debug",
req_cmd: "Cmd_FourBar_CrankSlider",
req_param: {
L_AB: 0.0,
L_BS: 0.2,
S_OFS: 0.5,
angleDeg: 30.0
}
},
quadrupedPoints: {
msg: "quadruped points from motor angles",
req_code: "CASE_QP_001",
req_from: "local_debug",
req_cmd: "Cmd_QuadrupedRobot_CalculateAllPointsFromMotorAngles",
req_param: {
Param: {
LF: {
thigh_angle_deg: -37.4784578338516,
shank_angle_deg: -22.4917532451628,
ankle_angle_deg: 0.0
},
LH: {
thigh_angle_deg: -34.0564623655754,
shank_angle_deg: -22.1353106546916,
ankle_angle_deg: 0.0
},
RF: {
thigh_angle_deg: 34.0564623655754,
shank_angle_deg: 22.1353106546916,
ankle_angle_deg: 0.0
},
RH: {
thigh_angle_deg: 37.4784578338516,
shank_angle_deg: 22.4917532451628,
ankle_angle_deg: 0.0
}
}
}
},
quadrupedForwardGait: {
msg: "quadruped forward gait",
req_code: "CASE_QP_002",
req_from: "local_debug",
req_cmd: "Cmd_QuadrupedRobot_PerformForwardKinematics",
req_param: {
GaitInfo: {
GaitType: "trot",
Period: 0.8,
Frequency: 50.0,
StepTime: 0.02,
SupportTime: 0.4,
SwingTime: 0.4,
SupportRatio: 0.5,
SwingRatio: 0.5,
TotalFrames: 40,
TotalTime: 0.8
},
SystemParameters: {
A_x: 0.0,
A_y: 500.0,
L10: 300.0,
L20: 286.0,
L30: 55.0,
StepLength: 60.0,
StepHeight: 50.0,
X: 30.0,
DeltaX: 0.0,
L21: 70.0,
L22: 292.0,
L23: 80.0,
Beta1: 166.0,
BB2_BC_Angle: 3.3859387489,
C1_B_Length: 107.0,
CC1: 179.0,
L31: 32.0,
C2C3_Length: 32.0,
Lead: 10.0,
D1_L_Offset: 90.0,
D2_L_Offset: 50.0,
C4_D2_Offset: 18.0,
ThighMotorReduction: 1.0,
ShankMotorReduction: 1.0,
AnkleMotorReduction: 1.0
}
}
}
};
function escapeHtml(value) {
return value
.replaceAll("&", "&amp;")
.replaceAll("<", "&lt;")
.replaceAll(">", "&gt;");
}
function resetMeta() {
metaSuccess.textContent = "未执行";
metaSuccess.className = "response-meta-value";
metaCode.textContent = "-";
metaCode.className = "response-meta-value";
metaCmd.textContent = "-";
metaReq.textContent = "-";
}
function setMetaFromJson(data) {
const success = data && typeof data.success === "boolean" ? data.success : null;
metaSuccess.textContent = success === null ? "未知" : (success ? "成功" : "失败");
metaSuccess.className = "response-meta-value " + (success ? "is-ok" : success === false ? "is-error" : "");
metaCode.textContent = data && "code" in data ? String(data.code) : "-";
metaCode.className = "response-meta-value " + (success === false ? "is-error" : "");
metaCmd.textContent = data && data.req_cmd ? data.req_cmd : "-";
metaReq.textContent = data && data.req_code ? data.req_code : "-";
}
function highlightJson(value) {
const escaped = escapeHtml(value);
return escaped.replace(
/("(?:\\u[a-fA-F0-9]{4}|\\[^u]|[^\\"])*")(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d+)?(?:[eE][+\-]?\d+)?/g,
(match, stringToken, keySuffix, literalToken) => {
if (stringToken) {
if (keySuffix) {
return `<span class="json-key">${stringToken}</span>${keySuffix}`;
}
return `<span class="json-string">${stringToken}</span>`;
}
if (literalToken === "true" || literalToken === "false") {
return `<span class="json-boolean">${match}</span>`;
}
if (literalToken === "null") {
return `<span class="json-null">${match}</span>`;
}
return `<span class="json-number">${match}</span>`;
}
);
}
function renderOutput(value) {
if (typeof value !== "string") {
const jsonText = JSON.stringify(value, null, 2);
output.innerHTML = highlightJson(jsonText);
setMetaFromJson(value);
return;
}
try {
const parsed = JSON.parse(value);
renderOutput(parsed);
} catch {
resetMeta();
output.textContent = value;
}
}
function allocString(value) {
const size = wasmModule.lengthBytesUTF8(value) + 1;
const ptr = wasmModule._malloc(size);
wasmModule.stringToUTF8(value, ptr, size);
return ptr;
}
function loadSelectedSample() {
const selectedKey = sampleSelect.value;
const sample = requestSamples[selectedKey];
if (!sample) {
return;
}
requestBox.value = JSON.stringify(sample, null, 2);
}
async function ensureModule() {
if (wasmModule) {
return wasmModule;
}
requestStatus.textContent = "正在加载模块";
wasmModule = await createSmartMathModule();
moduleStatus.textContent = "WASM 已加载";
requestStatus.textContent = "模块加载完成";
return wasmModule;
}
async function initializeBusinessApi() {
const mod = await ensureModule();
let responsePtr = null;
try {
responsePtr = mod._init_func();
const text = mod.UTF8ToString(responsePtr);
renderOutput(text);
moduleStatus.textContent = "业务接口已初始化";
requestStatus.textContent = "初始化成功";
} catch (error) {
moduleStatus.textContent = "初始化失败";
requestStatus.textContent = "初始化失败";
resetMeta();
output.textContent = String(error && error.stack ? error.stack : error);
} finally {
if (responsePtr) {
mod._smart_free_string(responsePtr);
}
}
}
async function runRequest() {
const mod = await ensureModule();
let requestPtr = null;
let responsePtr = null;
try {
const raw = requestBox.value.trim();
JSON.parse(raw);
requestPtr = allocString(raw);
responsePtr = mod._func(requestPtr);
const text = mod.UTF8ToString(responsePtr);
renderOutput(text);
requestStatus.textContent = "请求成功";
} catch (error) {
requestStatus.textContent = "请求失败";
resetMeta();
output.textContent = String(error && error.stack ? error.stack : error);
} finally {
if (requestPtr) {
mod._free(requestPtr);
}
if (responsePtr) {
mod._smart_free_string(responsePtr);
}
}
}
document.getElementById("init-btn").addEventListener("click", initializeBusinessApi);
document.getElementById("run-btn").addEventListener("click", runRequest);
document.getElementById("sample-btn").addEventListener("click", loadSelectedSample);
document.getElementById("clear-btn").addEventListener("click", () => {
requestStatus.textContent = "等待操作";
resetMeta();
output.textContent = "No output yet.";
});
resetMeta();
loadSelectedSample();
</script>
</body>
</html>