修复构建脚本和WASM测试问题
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
|
|
||||||
# build.ps1 - 智能 WASM 构建脚本
|
# build.ps1 - 智能 WASM 构建脚本
|
||||||
param(
|
param(
|
||||||
[switch]$Clean,
|
[switch]$Clean,
|
||||||
@@ -13,16 +13,50 @@ Write-Host "=========================================" -ForegroundColor Cyan
|
|||||||
Write-Host ""
|
Write-Host ""
|
||||||
|
|
||||||
# 1. 设置环境变量
|
# 1. 设置环境变量
|
||||||
$EmsdkPath = "E:\emsdk"
|
$emsdkCandidates = @()
|
||||||
if (-not (Test-Path $EmsdkPath)) {
|
|
||||||
|
if ($env:EMSDK) {
|
||||||
|
$emsdkCandidates += $env:EMSDK
|
||||||
|
}
|
||||||
|
|
||||||
|
$emsdkCandidates += "E:\emsdk"
|
||||||
|
$emsdkCandidates += (Join-Path (Split-Path $PSScriptRoot -Parent) "..\emsdk")
|
||||||
|
$emsdkCandidates += (Join-Path (Split-Path $PSScriptRoot -Parent) "emsdk")
|
||||||
|
|
||||||
|
$emccCommand = Get-Command emcc -ErrorAction SilentlyContinue
|
||||||
|
if ($emccCommand -and $emccCommand.Source) {
|
||||||
|
$emccSourceDir = Split-Path $emccCommand.Source -Parent
|
||||||
|
if (Test-Path (Join-Path $emccSourceDir "emcc.ps1")) {
|
||||||
|
$emsdkCandidates += (Split-Path (Split-Path $emccSourceDir -Parent) -Parent)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$EmsdkPath = $null
|
||||||
|
foreach ($candidate in $emsdkCandidates | Select-Object -Unique) {
|
||||||
|
if (-not [string]::IsNullOrWhiteSpace($candidate)) {
|
||||||
|
try {
|
||||||
|
if (Test-Path $candidate) {
|
||||||
|
$resolvedCandidate = [System.IO.Path]::GetFullPath($candidate)
|
||||||
|
if (Test-Path (Join-Path $resolvedCandidate "upstream\emscripten\emcc.ps1")) {
|
||||||
|
$EmsdkPath = $resolvedCandidate
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (-not $EmsdkPath) {
|
||||||
Write-Host "[错误] 未找到 Emscripten" -ForegroundColor Red
|
Write-Host "[错误] 未找到 Emscripten" -ForegroundColor Red
|
||||||
Write-Host "请确保 Emscripten 安装在: $EmsdkPath" -ForegroundColor Yellow
|
Write-Host "请设置 EMSDK 环境变量,或确认 emsdk 已安装。" -ForegroundColor Yellow
|
||||||
exit 1
|
exit 1
|
||||||
}
|
}
|
||||||
|
|
||||||
# 设置 PATH
|
# 设置 PATH
|
||||||
$env:PATH = "$EmsdkPath\upstream\emscripten;$env:PATH"
|
$env:PATH = "$EmsdkPath\upstream\emscripten;$env:PATH"
|
||||||
Write-Host "[✓] 设置 Emscripten 环境" -ForegroundColor Green
|
Write-Host "[✓] 设置 Emscripten 环境: $EmsdkPath" -ForegroundColor Green
|
||||||
|
|
||||||
# 2. 清理旧构建
|
# 2. 清理旧构建
|
||||||
if ($Clean) {
|
if ($Clean) {
|
||||||
@@ -84,8 +118,8 @@ $compileResult = emcc `
|
|||||||
-lorocos-kdl `
|
-lorocos-kdl `
|
||||||
-std=c++17 `
|
-std=c++17 `
|
||||||
-s WASM=1 `
|
-s WASM=1 `
|
||||||
-s EXPORTED_FUNCTIONS='["_init_func","_func","_smart_process_json","_smart_get_function_list","_smart_get_function_info","_smart_test_match","_smart_get_version","_smart_free_string","_wasm_malloc","_wasm_free","_malloc","_free","_add","_subtract","_multiply","_divide","_fibonacci","_create_buffer","_destroy_buffer","_compute_sum","_get_greeting","_main"]' `
|
-s EXPORTED_FUNCTIONS='["_init_func","_func","_smart_process_json","_smart_get_function_list","_smart_get_function_info","_smart_test_match","_smart_get_version","_smart_free_string","_malloc","_free","_add","_subtract","_multiply","_divide","_fibonacci","_create_buffer","_destroy_buffer","_compute_sum","_get_greeting","_main"]' `
|
||||||
-s EXPORTED_RUNTIME_METHODS='["UTF8ToString","stringToUTF8","lengthBytesUTF8","ccall","cwrap","HEAPU8","HEAP8","HEAPF32","HEAPF64","allocateUTF8"]' `
|
-s EXPORTED_RUNTIME_METHODS='["UTF8ToString","stringToUTF8","lengthBytesUTF8","ccall","cwrap","HEAPU8","HEAP8","HEAPF32","HEAPF64"]' `
|
||||||
-s MODULARIZE=1 `
|
-s MODULARIZE=1 `
|
||||||
-s EXPORT_NAME='createSmartMathModule' `
|
-s EXPORT_NAME='createSmartMathModule' `
|
||||||
-s ALLOW_MEMORY_GROWTH=1 `
|
-s ALLOW_MEMORY_GROWTH=1 `
|
||||||
@@ -125,28 +159,28 @@ $wasmSize = $wasmFile.Length / 1KB
|
|||||||
|
|
||||||
# 6. 显示构建结果
|
# 6. 显示构建结果
|
||||||
Write-Host ""
|
Write-Host ""
|
||||||
Write-Host "🎉 构建成功!" -ForegroundColor Green
|
Write-Host "构建成功" -ForegroundColor Green
|
||||||
Write-Host ("═" * 50) -ForegroundColor DarkGray
|
Write-Host ("=" * 50) -ForegroundColor DarkGray
|
||||||
Write-Host "📦 生成文件:" -ForegroundColor White
|
Write-Host "生成文件:" -ForegroundColor White
|
||||||
Write-Host "├─ smart_math.js ($($jsSize.ToString('F1')) KB)" -ForegroundColor Gray
|
Write-Host " - smart_math.js ($($jsSize.ToString('F1')) KB)" -ForegroundColor Gray
|
||||||
Write-Host "└─ smart_math.wasm ($($wasmSize.ToString('F1')) KB)" -ForegroundColor Gray
|
Write-Host " - smart_math.wasm ($($wasmSize.ToString('F1')) KB)" -ForegroundColor Gray
|
||||||
Write-Host ""
|
Write-Host ""
|
||||||
Write-Host "✨ 功能特性:" -ForegroundColor White
|
Write-Host "功能特性:" -ForegroundColor White
|
||||||
Write-Host "├─ ✅ 机器人正逆解业务接口 (func/init_func)" -ForegroundColor Gray
|
Write-Host " - 机器人正逆解业务接口 (func/init_func)" -ForegroundColor Gray
|
||||||
Write-Host "├─ ✅ SPC / 四足机器人 / 连杆机构业务接口" -ForegroundColor Gray
|
Write-Host " - SPC / 四足机器人 / 连杆机构业务接口" -ForegroundColor Gray
|
||||||
Write-Host "├─ ✅ 智能 JSON 数学测试接口" -ForegroundColor Gray
|
Write-Host " - 智能 JSON 数学测试接口" -ForegroundColor Gray
|
||||||
Write-Host "├─ ✅ Node / Web 双环境加载" -ForegroundColor Gray
|
Write-Host " - Node / Web 双环境加载" -ForegroundColor Gray
|
||||||
Write-Host "├─ ✅ 与 Emscripten 内存管理兼容" -ForegroundColor Gray
|
Write-Host " - 与 Emscripten 内存管理兼容" -ForegroundColor Gray
|
||||||
Write-Host "└─ ✅ 批量回归测试可直接调用" -ForegroundColor Gray
|
Write-Host " - 批量回归测试可直接调用" -ForegroundColor Gray
|
||||||
Write-Host ""
|
Write-Host ""
|
||||||
Write-Host "📁 输出目录: $((Get-Location).Path)\public\wasm" -ForegroundColor Gray
|
Write-Host "输出目录: $((Get-Location).Path)\public\wasm" -ForegroundColor Gray
|
||||||
Write-Host "⏱️ 编译耗时: $($duration.ToString('F2')) 秒" -ForegroundColor Gray
|
Write-Host "编译耗时: $($duration.ToString('F2')) 秒" -ForegroundColor Gray
|
||||||
Write-Host ""
|
Write-Host ""
|
||||||
|
|
||||||
# 7. 生成 JavaScript 包装器(可选)
|
# 7. 生成 JavaScript 包装器(可选)
|
||||||
Write-Host "[ ] 生成 JavaScript 包装器..." -ForegroundColor Cyan
|
Write-Host "[ ] 生成 JavaScript 包装器..." -ForegroundColor Cyan
|
||||||
|
|
||||||
$jsWrapper = @"
|
$jsWrapper = @'
|
||||||
// Smart WASM JSON API JavaScript 包装器
|
// Smart WASM JSON API JavaScript 包装器
|
||||||
class SmartWasmAPI {
|
class SmartWasmAPI {
|
||||||
constructor() {
|
constructor() {
|
||||||
@@ -154,6 +188,13 @@ class SmartWasmAPI {
|
|||||||
this.initialized = false;
|
this.initialized = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
allocString(value) {
|
||||||
|
const size = this.module.lengthBytesUTF8(value) + 1;
|
||||||
|
const ptr = this.module._malloc(size);
|
||||||
|
this.module.stringToUTF8(value, ptr, size);
|
||||||
|
return ptr;
|
||||||
|
}
|
||||||
|
|
||||||
async initialize() {
|
async initialize() {
|
||||||
if (this.initialized) return;
|
if (this.initialized) return;
|
||||||
|
|
||||||
@@ -192,7 +233,7 @@ class SmartWasmAPI {
|
|||||||
let responsePtr = null;
|
let responsePtr = null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
requestPtr = this.module.allocateUTF8(JSON.stringify(jsonRequest));
|
requestPtr = this.allocString(JSON.stringify(jsonRequest));
|
||||||
responsePtr = this.module._func(requestPtr);
|
responsePtr = this.module._func(requestPtr);
|
||||||
const responseStr = this.module.UTF8ToString(responsePtr);
|
const responseStr = this.module.UTF8ToString(responsePtr);
|
||||||
return JSON.parse(responseStr);
|
return JSON.parse(responseStr);
|
||||||
@@ -220,7 +261,7 @@ class SmartWasmAPI {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
// 将 JSON 字符串转换为 WASM 内存中的字符串
|
// 将 JSON 字符串转换为 WASM 内存中的字符串
|
||||||
requestPtr = this.module.allocateUTF8(JSON.stringify(jsonRequest));
|
requestPtr = this.allocString(JSON.stringify(jsonRequest));
|
||||||
|
|
||||||
// 调用 WASM 函数
|
// 调用 WASM 函数
|
||||||
responsePtr = this.module._smart_process_json(requestPtr);
|
responsePtr = this.module._smart_process_json(requestPtr);
|
||||||
@@ -286,7 +327,7 @@ class SmartWasmAPI {
|
|||||||
let responsePtr = null;
|
let responsePtr = null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
requestPtr = this.module.allocateUTF8(JSON.stringify(jsonRequest));
|
requestPtr = this.allocString(JSON.stringify(jsonRequest));
|
||||||
responsePtr = this.module._smart_test_match(requestPtr);
|
responsePtr = this.module._smart_test_match(requestPtr);
|
||||||
const responseStr = this.module.UTF8ToString(responsePtr);
|
const responseStr = this.module.UTF8ToString(responsePtr);
|
||||||
return JSON.parse(responseStr);
|
return JSON.parse(responseStr);
|
||||||
@@ -354,7 +395,7 @@ if (typeof window !== 'undefined') {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export default SmartWasmAPI;
|
export default SmartWasmAPI;
|
||||||
"@
|
'@
|
||||||
|
|
||||||
$jsWrapper | Out-File -FilePath "public/wasm/smart_api_wrapper.js" -Encoding UTF8
|
$jsWrapper | Out-File -FilePath "public/wasm/smart_api_wrapper.js" -Encoding UTF8
|
||||||
Write-Host "[✓] 生成 JavaScript 包装器" -ForegroundColor Green
|
Write-Host "[✓] 生成 JavaScript 包装器" -ForegroundColor Green
|
||||||
@@ -386,9 +427,10 @@ if ($Serve) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Write-Host ""
|
Write-Host ""
|
||||||
Write-Host "🚀 使用提示:" -ForegroundColor White
|
Write-Host "使用提示:" -ForegroundColor White
|
||||||
Write-Host "├─ 在 VS Code 中按 F5 启动调试" -ForegroundColor Gray
|
Write-Host " - 在 VS Code 中按 F5 启动调试" -ForegroundColor Gray
|
||||||
Write-Host "├─ 使用 Ctrl+Shift+B 快速构建" -ForegroundColor Gray
|
Write-Host " - 使用 Ctrl+Shift+B 快速构建" -ForegroundColor Gray
|
||||||
Write-Host "├─ 查看 public/wasm/smart_api_wrapper.js 了解 JavaScript API 使用" -ForegroundColor Gray
|
Write-Host " - 查看 public/wasm/smart_api_wrapper.js 了解 JavaScript API 使用" -ForegroundColor Gray
|
||||||
Write-Host "└─ 运行 \`npm run serve\` 启动开发服务器" -ForegroundColor Gray
|
Write-Host " - 运行 npm run serve 启动开发服务器" -ForegroundColor Gray
|
||||||
Write-Host ""
|
Write-Host ""
|
||||||
|
|
||||||
|
|||||||
@@ -76,9 +76,16 @@ async function loadModule(modulePath) {
|
|||||||
throw new Error(`Unsupported WASM loader type: ${typeof required}`);
|
throw new Error(`Unsupported WASM loader type: ${typeof required}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function allocString(module, value) {
|
||||||
|
const size = module.lengthBytesUTF8(value) + 1;
|
||||||
|
const ptr = module._malloc(size);
|
||||||
|
module.stringToUTF8(value, ptr, size);
|
||||||
|
return ptr;
|
||||||
|
}
|
||||||
|
|
||||||
function callJsonFunction(module, exportName, payload) {
|
function callJsonFunction(module, exportName, payload) {
|
||||||
const requestText = typeof payload === "string" ? payload : JSON.stringify(payload);
|
const requestText = typeof payload === "string" ? payload : JSON.stringify(payload);
|
||||||
const requestPtr = module.allocateUTF8(requestText);
|
const requestPtr = allocString(module, requestText);
|
||||||
let responsePtr = 0;
|
let responsePtr = 0;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -343,7 +350,7 @@ const suite = [
|
|||||||
assertions: [
|
assertions: [
|
||||||
{ path: "success", equals: true },
|
{ path: "success", equals: true },
|
||||||
{ path: "res_data.success", equals: true },
|
{ path: "res_data.success", equals: true },
|
||||||
{ path: "res_data.OPERATION.frames", lengthEquals: 2 },
|
{ path: "res_data.OPERATION.OPERATION.frames", lengthEquals: 2 },
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -435,7 +442,8 @@ async function main() {
|
|||||||
|
|
||||||
ensure(typeof module._init_func === "function", "_init_func is not exported");
|
ensure(typeof module._init_func === "function", "_init_func is not exported");
|
||||||
ensure(typeof module._func === "function", "_func is not exported");
|
ensure(typeof module._func === "function", "_func is not exported");
|
||||||
ensure(typeof module.allocateUTF8 === "function", "allocateUTF8 is not exported");
|
ensure(typeof module.lengthBytesUTF8 === "function", "lengthBytesUTF8 is not exported");
|
||||||
|
ensure(typeof module.stringToUTF8 === "function", "stringToUTF8 is not exported");
|
||||||
ensure(typeof module.UTF8ToString === "function", "UTF8ToString is not exported");
|
ensure(typeof module.UTF8ToString === "function", "UTF8ToString is not exported");
|
||||||
|
|
||||||
const initResponse = callNoArgJsonFunction(module, "_init_func");
|
const initResponse = callNoArgJsonFunction(module, "_init_func");
|
||||||
|
|||||||
@@ -426,9 +426,9 @@ std::string KinematicsWebAPI::func(std::string sanitized_body)
|
|||||||
{"error", "Invalid parameters"},
|
{"error", "Invalid parameters"},
|
||||||
{"validation_errors", validation.Errors},
|
{"validation_errors", validation.Errors},
|
||||||
{"validation_warnings", validation.Warnings}};
|
{"validation_warnings", validation.Warnings}};
|
||||||
return res_data;
|
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
// 执行计算
|
// 执行计算
|
||||||
MechanismState state = mechanism->calculate(angleDeg);
|
MechanismState state = mechanism->calculate(angleDeg);
|
||||||
|
|
||||||
@@ -438,9 +438,9 @@ std::string KinematicsWebAPI::func(std::string sanitized_body)
|
|||||||
res_data = {
|
res_data = {
|
||||||
{"success", false},
|
{"success", false},
|
||||||
{"error", state.ErrorMessage}};
|
{"error", state.ErrorMessage}};
|
||||||
return res_data;
|
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
// 构建响应数据
|
// 构建响应数据
|
||||||
json result;
|
json result;
|
||||||
result["success"] = true;
|
result["success"] = true;
|
||||||
@@ -522,9 +522,13 @@ std::string KinematicsWebAPI::func(std::string sanitized_body)
|
|||||||
|
|
||||||
res_data = result;
|
res_data = result;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
catch (const std::exception &e)
|
catch (const std::exception &e)
|
||||||
{
|
{
|
||||||
res_data = {{"success", false, "error", "Failed to Cmd_FourBar_CrankSlider: " + std::string(e.what())}};
|
res_data = {
|
||||||
|
{"success", false},
|
||||||
|
{"error", "Failed to Cmd_FourBar_CrankSlider: " + std::string(e.what())}};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (req_cmd == "Cmd_QuadrupedRobot_CalculateAllPointsFromMotorAngles")
|
else if (req_cmd == "Cmd_QuadrupedRobot_CalculateAllPointsFromMotorAngles")
|
||||||
|
|||||||
BIN
src/Robot.cpp
BIN
src/Robot.cpp
Binary file not shown.
Reference in New Issue
Block a user