BodyBalanceEvaluation/backend/tests/smitsense_demo.py
2025-08-07 16:22:38 +08:00

194 lines
6.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
SMiTSense足部压力传感器简单演示程序
展示如何使用SMiTSenseUsb-F3.0.dll进行基本的压力数据采集
"""
import sys
import os
import time
from test_smitsense_dll import SMiTSensePressureSensor
def simple_pressure_monitor():
"""
简单的压力监控演示
"""
print("SMiTSense足部压力传感器演示")
print("=" * 40)
# 创建传感器实例
sensor = SMiTSensePressureSensor()
try:
# 初始化
print("正在初始化传感器...")
if not sensor.initialize():
print("❌ 初始化失败")
return
# 查找设备
print("正在查找设备...")
devices = sensor.get_device_list()
if not devices:
print("❌ 未找到SMiTSense设备")
print("请检查:")
print("1. 设备是否正确连接")
print("2. 驱动程序是否已安装")
print("3. 设备是否被其他程序占用")
return
print(f"✅ 找到 {len(devices)} 个设备")
# 连接第一个设备
print("正在连接设备...")
if not sensor.connect(devices[0]):
print("❌ 连接失败")
return
print("✅ 设备连接成功")
# 获取灵敏度
sensitivity = sensor.get_sensitivity()
if sensitivity is not None:
print(f"当前灵敏度: {sensitivity}")
# 开始监控压力数据
print("\n开始压力监控按Ctrl+C停止...")
print("-" * 60)
print("时间\t\t左前\t左后\t右前\t右后\t总压力")
print("-" * 60)
count = 0
while True:
# 读取压力数据
pressure_data = sensor.read_pressure_data()
if pressure_data:
# 转换为足部区域数据
foot_zones = sensor.get_foot_pressure_zones(pressure_data)
if foot_zones:
timestamp = time.strftime("%H:%M:%S")
print(f"{timestamp}\t{foot_zones['left_front']:.1f}\t"
f"{foot_zones['left_rear']:.1f}\t"
f"{foot_zones['right_front']:.1f}\t"
f"{foot_zones['right_rear']:.1f}\t"
f"{foot_zones['total_pressure']:.1f}")
# 简单的平衡分析
if foot_zones['total_pressure'] > 10: # 有压力时才分析
left_ratio = foot_zones['left_total'] / foot_zones['total_pressure']
if left_ratio < 0.4:
balance_status = "右倾"
elif left_ratio > 0.6:
balance_status = "左倾"
else:
balance_status = "平衡"
if count % 10 == 0: # 每10次显示一次平衡状态
print(f"\t\t\t\t\t\t\t平衡状态: {balance_status}")
else:
print(f"{time.strftime('%H:%M:%S')}\t数据处理失败")
else:
print(f"{time.strftime('%H:%M:%S')}\t无数据")
count += 1
time.sleep(0.2) # 5Hz采样率
except KeyboardInterrupt:
print("\n\n用户停止监控")
except Exception as e:
print(f"\n❌ 监控过程中发生错误: {e}")
finally:
# 断开连接
print("正在断开设备连接...")
sensor.disconnect()
print("✅ 设备已断开")
def test_dll_availability():
"""
测试DLL文件是否可用
"""
print("检查DLL文件可用性...")
dll_path = os.path.join(os.path.dirname(__file__), 'SMiTSenseUsb-F3.0.dll')
if not os.path.exists(dll_path):
print(f"❌ DLL文件不存在: {dll_path}")
return False
try:
sensor = SMiTSensePressureSensor(dll_path)
print("✅ DLL文件加载成功")
# 测试基本函数调用
result = sensor.initialize()
print(f"✅ 初始化函数调用成功,返回值: {result}")
return True
except Exception as e:
print(f"❌ DLL测试失败: {e}")
return False
def show_usage():
"""
显示使用说明
"""
print("""
SMiTSense足部压力传感器使用说明
=================================
1. 硬件准备:
- 确保SMiTSense足部压力传感器已正确连接到USB端口
- 安装相应的设备驱动程序
- 确保设备未被其他程序占用
2. 软件准备:
- 确保SMiTSenseUsb-F3.0.dll文件在当前目录中
- 安装Python 3.6+
- 确保ctypes库可用Python标准库
3. 运行程序:
python smitsense_demo.py
4. 功能说明:
- 自动检测并连接SMiTSense设备
- 实时显示足部各区域压力数据
- 简单的平衡状态分析
- 支持Ctrl+C安全退出
5. 数据说明:
- 左前/左后/右前/右后:对应足部四个区域的压力值
- 总压力:所有传感器压力值的总和
- 平衡状态:基于左右脚压力分布的简单分析
6. 故障排除:
- 如果提示"未找到设备",请检查硬件连接和驱动
- 如果提示"DLL加载失败"请检查DLL文件路径
- 如果数据异常,请检查传感器校准状态
7. 集成到现有系统:
- 可以将SMiTSensePressureSensor类集成到device_manager.py中
- 替换现有的MockPressureDevice模拟设备
- 实现真实的压力传感器数据采集
""")
if __name__ == "__main__":
if len(sys.argv) > 1 and sys.argv[1] == "--help":
show_usage()
sys.exit(0)
print("SMiTSense足部压力传感器演示程序")
print("使用 --help 参数查看详细使用说明")
print()
# 首先测试DLL可用性
if not test_dll_availability():
print("\n请检查DLL文件和依赖项后重试")
sys.exit(1)
print()
# 运行压力监控演示
simple_pressure_monitor()