Metadata-Version: 2.4
Name: fulai-tactile
Version: 1.0.4
Summary: Fulai tactile glove Python SDK (V3.0 / V3.1 / V3.2)
Author: Fulai
License: Proprietary
Keywords: tactile,glove,sensor,sdk
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Operating System :: Microsoft :: Windows
Classifier: Operating System :: POSIX :: Linux
Requires-Python: >=3.9
Description-Content-Type: text/markdown
Requires-Dist: pyserial>=3.5
Provides-Extra: dev
Requires-Dist: pytest>=7.4; extra == "dev"

# Fulai Tactile Python SDK 1.0.4

默认自动读取设备版本和左右手，旧固件保留手动设置；串口、手套编号和力值标定仍手动指定。详见 [自动识别与手动模式](docs/device-identity.md)。

这是 Fulai Tactile C++ SDK 的 Python 实现，覆盖 V3.0 / V3.1 / V3.2 手套的串口协议、分区布局、去皮、力值标定和剪切估计。协议、布局、去皮、标定和剪切算法与 C++ 版对齐。

未加载标定时读取 ADC。加载匹配的标定并去皮后读取牛顿值。使用力值前检查 `force_valid`。

## 安装

```powershell
pip install fulai-tactile
```

从源码开发安装：

```powershell
pip install -e .
```

## 使用顺序

```text
configure -> open / open_simulated -> load_calibration（可选）
-> tare_all -> read -> close
```

`load_calibration` 必须在设备已连接并识别出版本之后调用，否则会失败。加载标定不会自动去皮。

## Python 接入

```python
from fulai_tactile import Frame, Glove, GloveConfig, Hand, ReadStatus

config = GloveConfig(device_id="G-001", hand=Hand.LEFT)
with Glove() as glove:
    if not glove.configure(config) or not glove.open("COM22"):
        raise SystemExit(glove.last_error())
    if not glove.tare_all():
        raise SystemExit(glove.last_error())
    frame = Frame()
    while True:
        status = glove.read(frame, 200)
        if status == ReadStatus.TIMEOUT:
            continue
        if status != ReadStatus.OK:
            raise SystemExit(glove.last_error())
        zone = frame.zones[0]
        print(
            f"seq={frame.sequence} raw_sum={zone.raw_sum} "
            f"tared_sum={zone.tared_sum} force_valid={zone.force_valid} "
            f"force_N={zone.force_sum}"
        )
```

模拟源走同一套解码、去皮、标定和读取路径，用 `open_simulated` 代替 `open`：

```python
from fulai_tactile import GloveVersion, SimulationConfig

if not glove.open_simulated(SimulationConfig(version=GloveVersion.V32, rate_hz=100)):
    raise SystemExit(glove.last_error())
```

旧参考 SDK 风格的兼容层在 `fulai_tactile.compat`。

## 数据和标定

```text
raw_values           原始 ADC，硬件传输顺序
tared_values         去皮 ADC，紧凑有效点顺序
force_values         牛顿值，已加载并启用标定时存在
zones[i].raw_sum     原始 ADC 总和
zones[i].tared_sum   去皮 ADC 总和
zones[i].force_sum   分区力值，单位 N
zones[i].force_valid 该分区 force_sum 是否有效
frame.force_valid    所有分区的 force_valid 都为 true
```

分区 `force_valid` 为 true 需要：该分区已去皮、已加载并启用标定、该分区有标定数据、力值计算有效。未加载标定、未去皮、分区缺标定或计算结果无效时为 false。整帧 `force_valid` 还要求全部 8 个分区都满足。

SDK 接受两种标定格式。未设置时 `GloveConfig.hand` 默认为右手。新版固件默认通过身份查询返回的 `HAND` 确定手别；手动模式或旧固件无身份回复时使用 `GloveConfig.hand`。`glove.hand()` 返回连接后的有效手别。

旧格式 `glove-calibration/1` 不声明身份，建议命名为 `<device_id>.json`。文件名不会被强制校验：

```python
if not glove.open("COM22"):
    raise SystemExit(glove.last_error())
if not glove.load_calibration("calibrations/G-001.json"):
    raise SystemExit(glove.last_error())
if not glove.tare_all():
    raise SystemExit(glove.last_error())
```

分布式格式 `glove-distribution-calibration/1` 适用于识别到的硬件版本为 V3.1 或 V3.2 的手套。文件自带 `hand` 与 `glove_version`，加载时会与本次连接的有效手别和硬件版本比对。V3.2 使用 615 个传输通道，其中 `[82,90)` 和 `[530,537)` 是保留槽，不进入分区预测。文件名不受限制：

```python
config.hand = Hand.LEFT
if not glove.configure(config) or not glove.open("COM22"):
    raise SystemExit(glove.last_error())
if not glove.load_calibration("models/left.json"):
    raise SystemExit(glove.last_error())
if not glove.tare_all():
    raise SystemExit(glove.last_error())
```

手别或版本不匹配会直接报错，不会静默给出错误力值。分布式模型按 `max(raw - baseline, 0)` 训练，启用后去皮死区自动为 0。V3.1/V3.2 分布模型都需要调用 `tare_all` 采集 30 帧空载均值；采集完成前 `force_valid` 保持为 false。

剪切是相对量，不是牛顿。

## Demo

```text
无硬件模拟：tactile-simulation
真实设备：  tactile-stream COM22 G-001 left
带标定：    tactile-stream COM22 G-001 left calibrations/G-001.json
双手：      tactile-dual COM21 G-L left.json COM22 G-R right.json
分区布局：  tactile-layout
兼容层：    tactile-reference-compatible COM22
```

`tactile-dual` 必须提供左右两份标定文件。`--summary` 仅 `tactile-simulation` 支持。

`--json` 输出 JSON Lines，每行一个完整 `Frame`。枚举输出为数值，非有限浮点值输出为 `null`。

```powershell
tactile-simulation --json
tactile-stream COM22 G-001 left --json
```

模拟设备默认输出 5 帧；需要持续输出时使用：

```powershell
tactile-simulation --json --continuous
```

只查看关键状态，避免解析每帧的大数组：

```powershell
tactile-simulation --summary --continuous
tactile-simulation --summary --json --continuous
```

按 `Ctrl+C` 停止。启动去皮时不要按压手套。流式 Demo 每 20 帧打印一次。Windows 下还可以按 `C/T` 重新去皮，`R` 重置剪切，`Q` 退出。

## 错误处理

```text
OK                    读到一帧
TIMEOUT               继续读取
NOT_OPEN              先 open / open_simulated
NOT_CONFIGURED        先 configure
VERSION_UNKNOWN       检查硬件版本和连接超时
PROTOCOL_ERROR        检查设备、线缆和版本
DEVICE_DISCONNECTED   保存错误后关闭并重连
```

```python
print(glove.last_error())
```

## 测试

```powershell
pip install -e ".[dev]"
pytest
```

文本输出按 6 位小数舍入后，整数直接显示（如 0、2983），有小数部分时显示小数点后 6 位（如 0.730000）；字典和 JSON 导出按 6 位小数舍入（JSON 不补齐末尾零）。SDK 返回的 Frame 及内部计算保留原始精度。
