ehatool 6.0.0.dev0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- ehatool-6.0.0.dev0/LICENSE +21 -0
- ehatool-6.0.0.dev0/MANIFEST.in +7 -0
- ehatool-6.0.0.dev0/PKG-INFO +38 -0
- ehatool-6.0.0.dev0/README.md +16 -0
- ehatool-6.0.0.dev0/ehatool/VERSION +1 -0
- ehatool-6.0.0.dev0/ehatool/__init__.py +4 -0
- ehatool-6.0.0.dev0/ehatool/acceptance.py +92 -0
- ehatool-6.0.0.dev0/ehatool/brt27.py +105 -0
- ehatool-6.0.0.dev0/ehatool/brt27_sim.py +114 -0
- ehatool-6.0.0.dev0/ehatool/calibration.py +111 -0
- ehatool-6.0.0.dev0/ehatool/can_sniff.py +162 -0
- ehatool-6.0.0.dev0/ehatool/cia402.py +31 -0
- ehatool-6.0.0.dev0/ehatool/cli.py +541 -0
- ehatool-6.0.0.dev0/ehatool/console.py +31 -0
- ehatool-6.0.0.dev0/ehatool/device/__init__.py +4 -0
- ehatool-6.0.0.dev0/ehatool/device/can.py +201 -0
- ehatool-6.0.0.dev0/ehatool/device/can_discovery.py +387 -0
- ehatool-6.0.0.dev0/ehatool/device/can_discovery.toml +19 -0
- ehatool-6.0.0.dev0/ehatool/device/io.py +20 -0
- ehatool-6.0.0.dev0/ehatool/devices.py +187 -0
- ehatool-6.0.0.dev0/ehatool/diagnostics.py +58 -0
- ehatool-6.0.0.dev0/ehatool/diagnostics.toml +208 -0
- ehatool-6.0.0.dev0/ehatool/discovery.py +95 -0
- ehatool-6.0.0.dev0/ehatool/display.toml +127 -0
- ehatool-6.0.0.dev0/ehatool/ecat.py +520 -0
- ehatool-6.0.0.dev0/ehatool/ecat.toml +17 -0
- ehatool-6.0.0.dev0/ehatool/ecat_probe.py +401 -0
- ehatool-6.0.0.dev0/ehatool/ecat_probe.toml +14 -0
- ehatool-6.0.0.dev0/ehatool/eha_od.json +5871 -0
- ehatool-6.0.0.dev0/ehatool/experiment.py +576 -0
- ehatool-6.0.0.dev0/ehatool/image.py +70 -0
- ehatool-6.0.0.dev0/ehatool/info.py +121 -0
- ehatool-6.0.0.dev0/ehatool/info.toml +41 -0
- ehatool-6.0.0.dev0/ehatool/install_host.py +243 -0
- ehatool-6.0.0.dev0/ehatool/install_host.toml +27 -0
- ehatool-6.0.0.dev0/ehatool/link.py +811 -0
- ehatool-6.0.0.dev0/ehatool/link.toml +18 -0
- ehatool-6.0.0.dev0/ehatool/live.py +112 -0
- ehatool-6.0.0.dev0/ehatool/motor.py +86 -0
- ehatool-6.0.0.dev0/ehatool/od.py +200 -0
- ehatool-6.0.0.dev0/ehatool/odrive.py +450 -0
- ehatool-6.0.0.dev0/ehatool/output.py +208 -0
- ehatool-6.0.0.dev0/ehatool/ram_load.py +62 -0
- ehatool-6.0.0.dev0/ehatool/scope.py +50 -0
- ehatool-6.0.0.dev0/ehatool/sil.py +260 -0
- ehatool-6.0.0.dev0/ehatool/update.py +320 -0
- ehatool-6.0.0.dev0/ehatool/usb_discovery.py +164 -0
- ehatool-6.0.0.dev0/ehatool/usb_discovery.toml +16 -0
- ehatool-6.0.0.dev0/ehatool/web/__init__.py +28 -0
- ehatool-6.0.0.dev0/ehatool/web/brt_sim.py +130 -0
- ehatool-6.0.0.dev0/ehatool/web/canbus.py +95 -0
- ehatool-6.0.0.dev0/ehatool/web/devices.py +216 -0
- ehatool-6.0.0.dev0/ehatool/web/experiments.py +226 -0
- ehatool-6.0.0.dev0/ehatool/web/job_params.py +208 -0
- ehatool-6.0.0.dev0/ehatool/web/layout.toml +61 -0
- ehatool-6.0.0.dev0/ehatool/web/longrun.py +430 -0
- ehatool-6.0.0.dev0/ehatool/web/polling.py +41 -0
- ehatool-6.0.0.dev0/ehatool/web/recipes/bench.toml +18 -0
- ehatool-6.0.0.dev0/ehatool/web/records.py +333 -0
- ehatool-6.0.0.dev0/ehatool/web/requests.py +254 -0
- ehatool-6.0.0.dev0/ehatool/web/server.py +1051 -0
- ehatool-6.0.0.dev0/ehatool/web/session.py +1552 -0
- ehatool-6.0.0.dev0/ehatool/web/static/THIRD_PARTY_LICENSES.txt +133 -0
- ehatool-6.0.0.dev0/ehatool/web/static/assets/ehapower-logo-Cr84XeX3.png +0 -0
- ehatool-6.0.0.dev0/ehatool/web/static/assets/ehapower-mark-Con0OjPM.png +0 -0
- ehatool-6.0.0.dev0/ehatool/web/static/assets/index-CdLvwKa-.css +1 -0
- ehatool-6.0.0.dev0/ehatool/web/static/assets/index-DiLUQD1i.js +134 -0
- ehatool-6.0.0.dev0/ehatool/web/static/index.html +16 -0
- ehatool-6.0.0.dev0/ehatool/web/streams.py +64 -0
- ehatool-6.0.0.dev0/ehatool/web/workspaces.py +124 -0
- ehatool-6.0.0.dev0/ehatool/workflows/__init__.py +4 -0
- ehatool-6.0.0.dev0/ehatool/workflows/cancheck.py +394 -0
- ehatool-6.0.0.dev0/ehatool/workflows/cancheck.toml +9 -0
- ehatool-6.0.0.dev0/ehatool/workflows/checks.py +43 -0
- ehatool-6.0.0.dev0/ehatool/workflows/usbcheck.py +212 -0
- ehatool-6.0.0.dev0/ehatool.egg-info/PKG-INFO +38 -0
- ehatool-6.0.0.dev0/ehatool.egg-info/SOURCES.txt +81 -0
- ehatool-6.0.0.dev0/ehatool.egg-info/dependency_links.txt +1 -0
- ehatool-6.0.0.dev0/ehatool.egg-info/entry_points.txt +2 -0
- ehatool-6.0.0.dev0/ehatool.egg-info/requires.txt +14 -0
- ehatool-6.0.0.dev0/ehatool.egg-info/top_level.txt +1 -0
- ehatool-6.0.0.dev0/pyproject.toml +54 -0
- ehatool-6.0.0.dev0/setup.cfg +4 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 The eha-firmware Contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: ehatool
|
|
3
|
+
Version: 6.0.0.dev0
|
|
4
|
+
Summary: EHA 驱控一体主机工具:对象字典、USB/CAN 访问、阶跃试验与宿主仿真、示波、CAN 联调检查、固件升级与 EtherCAT 联调
|
|
5
|
+
License: MIT
|
|
6
|
+
Requires-Python: >=3.11
|
|
7
|
+
Description-Content-Type: text/markdown
|
|
8
|
+
License-File: LICENSE
|
|
9
|
+
Requires-Dist: pyserial>=3.5
|
|
10
|
+
Requires-Dist: rich>=13.7
|
|
11
|
+
Requires-Dist: python-can[pcan]>=4.6.1
|
|
12
|
+
Requires-Dist: gs-usb>=0.3.0; sys_platform == "darwin" or sys_platform == "win32"
|
|
13
|
+
Requires-Dist: libusb-package>=1.0.30.1; sys_platform == "darwin" or sys_platform == "win32"
|
|
14
|
+
Requires-Dist: matplotlib>=3.8
|
|
15
|
+
Requires-Dist: pysoem>=1.1
|
|
16
|
+
Requires-Dist: fastapi>=0.115
|
|
17
|
+
Requires-Dist: pydantic>=2.0
|
|
18
|
+
Requires-Dist: uvicorn>=0.32
|
|
19
|
+
Requires-Dist: websockets>=13
|
|
20
|
+
Requires-Dist: odrive==0.5.1.post0
|
|
21
|
+
Dynamic: license-file
|
|
22
|
+
|
|
23
|
+
# ehatool
|
|
24
|
+
|
|
25
|
+
EHA 驱控一体设备的主机工具:经 USB CDC、对外 CAN(CANopen)或 EtherCAT 读写对象字典、保存参数、
|
|
26
|
+
跑试验与标定、升级固件,并提供本机工作台(`ehatool web`)。
|
|
27
|
+
|
|
28
|
+
```sh
|
|
29
|
+
python3 -m venv .venv && .venv/bin/pip install ./ehatool
|
|
30
|
+
.venv/bin/ehatool --help
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
安装包带全部功能依赖(串口、CAN、出图、EtherCAT、Web 服务与 `odrive`),不需要再装额外的 extra。
|
|
34
|
+
|
|
35
|
+
设备侧的协议、对象字典与故障码见仓库中的文档与对象字典参考;命令行各子命令用
|
|
36
|
+
`ehatool <子命令> --help` 查看。升级与首次安装接受发布目录(带 `manifest.toml`)或开发用的构建目录。
|
|
37
|
+
|
|
38
|
+
许可证:MIT。
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# ehatool
|
|
2
|
+
|
|
3
|
+
EHA 驱控一体设备的主机工具:经 USB CDC、对外 CAN(CANopen)或 EtherCAT 读写对象字典、保存参数、
|
|
4
|
+
跑试验与标定、升级固件,并提供本机工作台(`ehatool web`)。
|
|
5
|
+
|
|
6
|
+
```sh
|
|
7
|
+
python3 -m venv .venv && .venv/bin/pip install ./ehatool
|
|
8
|
+
.venv/bin/ehatool --help
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
安装包带全部功能依赖(串口、CAN、出图、EtherCAT、Web 服务与 `odrive`),不需要再装额外的 extra。
|
|
12
|
+
|
|
13
|
+
设备侧的协议、对象字典与故障码见仓库中的文档与对象字典参考;命令行各子命令用
|
|
14
|
+
`ehatool <子命令> --help` 查看。升级与首次安装接受发布目录(带 `manifest.toml`)或开发用的构建目录。
|
|
15
|
+
|
|
16
|
+
许可证:MIT。
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
6.0.0-dev.0
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
# Copyright The eha-firmware Contributors
|
|
2
|
+
|
|
3
|
+
## @file
|
|
4
|
+
## @brief 装配验收(eha-product 技能“装配调试”第 8 步)中由工具执行的检查:上位机失联的反应。期望结果只由读回的
|
|
5
|
+
## 0x6007 与 0x605A 按[危害与机制](../../docs/架构说明.md#危害与机制)推出,不假定默认值。
|
|
6
|
+
|
|
7
|
+
import time
|
|
8
|
+
|
|
9
|
+
from .cia402 import CW_DISABLE_VOLTAGE, CW_ENABLE, CW_FAULT_RESET, CW_QUICK_STOP, CW_SHUTDOWN, state_name
|
|
10
|
+
from .link import DriveAborted, DriveStateUnknown
|
|
11
|
+
|
|
12
|
+
HOST_TIMEOUT = 0x8130
|
|
13
|
+
CSV = 9
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
## @brief 失联后的期望(按读回的 0x6007 与 0x605A):1 故障断电;2 断电并锁存;3 快停后按 0x605A(6 零速保持、2 断电),
|
|
17
|
+
## 并锁存。0 为不处理:检查会让驱动无人看管地运行,不做(ValueError)。对象字典之外的取值也不推断(ValueError)。
|
|
18
|
+
## 返回 {"states": 允许的状态, "error": 期望的 0x603F, "latched": 位 7 是否置位, "text": 说明}。
|
|
19
|
+
def expected_host_loss(abort_option, quick_stop_option):
|
|
20
|
+
if abort_option == 0:
|
|
21
|
+
raise ValueError("0x6007 = 0:失联不处理,检查会让驱动在无人发送时继续运行;不做这项检查")
|
|
22
|
+
if abort_option == 1:
|
|
23
|
+
return {"states": ["Fault"], "error": HOST_TIMEOUT, "latched": False, "text": "0x6007 = 1:故障断电,0x603F = 0x8130"}
|
|
24
|
+
if abort_option == 2:
|
|
25
|
+
return {"states": ["SwitchOnDisabled"], "error": HOST_TIMEOUT, "latched": True,
|
|
26
|
+
"text": "0x6007 = 2:断电,锁存 0x8130"}
|
|
27
|
+
if abort_option == 3 and quick_stop_option == 6:
|
|
28
|
+
return {"states": ["QuickStopActive"], "error": HOST_TIMEOUT, "latched": True,
|
|
29
|
+
"text": "0x6007 = 3、0x605A = 6:快停后零速保持,锁存 0x8130"}
|
|
30
|
+
if abort_option == 3 and quick_stop_option == 2:
|
|
31
|
+
return {"states": ["SwitchOnDisabled"], "error": HOST_TIMEOUT, "latched": True,
|
|
32
|
+
"text": "0x6007 = 3、0x605A = 2:快停后断电,锁存 0x8130"}
|
|
33
|
+
raise ValueError(f"0x6007 = {abort_option}、0x605A = {quick_stop_option} 不在对象字典的取值内")
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def _stream(dev, controlword, seconds, period=0.01):
|
|
37
|
+
fb, end = None, time.monotonic() + seconds
|
|
38
|
+
while time.monotonic() < end:
|
|
39
|
+
fb = dev.command(controlword, CSV, 0.0, velocity_um_s=0) or fb
|
|
40
|
+
time.sleep(period)
|
|
41
|
+
return fb
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
## @brief 以 CSV 零速使能,复核控制权与模式确是本工具的,停发控制数据超过 0x2000,读回状态与 0x603F 与期望比较。
|
|
45
|
+
## 不运动。dev 须已取得控制权(Device.acquire)。任何退出路径都收尾(运行中先快停;本检查造成的失联锁存 0x8130 先确认;
|
|
46
|
+
## 再断电),收尾后经 SDO 确认已断电,否则抛出 DriveStateUnknown。返回 {"expected", "state", "error", "statusword", "ok"}。
|
|
47
|
+
def host_loss(dev, report=print):
|
|
48
|
+
expected = expected_host_loss(dev.read("abort_connection_option"), dev.read("quick_stop_option"))
|
|
49
|
+
timeout_s = dev.read("command_timeout_ms") / 1000
|
|
50
|
+
report(f"期望:{expected['text']}")
|
|
51
|
+
observed = None
|
|
52
|
+
try:
|
|
53
|
+
if getattr(dev, "reset", False): # 取得控制权时用户确认过故障或锁存:先故障复位
|
|
54
|
+
_stream(dev, CW_FAULT_RESET, 0.05)
|
|
55
|
+
_stream(dev, CW_SHUTDOWN, 0.1)
|
|
56
|
+
fb = _stream(dev, CW_ENABLE, 0.3)
|
|
57
|
+
if fb is None or fb["state"] != "OperationEnabled" or fb["modes_of_operation_display"] != CSV:
|
|
58
|
+
raise DriveAborted(f"没有以 CSV 进入运行({fb['state'] if fb else '无反馈'})")
|
|
59
|
+
# 反馈显示运行不说明是本工具使能的:复核属主(另一个主站可能已取得控制权)。
|
|
60
|
+
dev.check_owner()
|
|
61
|
+
report(f"已以 CSV 零速运行;停发控制数据 {timeout_s * 1000:.0f} ms 以上")
|
|
62
|
+
# 失联判出 0x2000 + 1 ms,快停 ≤ 300 ms(危害清单“上位机失联”一行);等待期间可被停止打断。
|
|
63
|
+
cancel = getattr(dev, "cancel", None)
|
|
64
|
+
if cancel is not None:
|
|
65
|
+
cancel.wait(timeout_s + 0.5)
|
|
66
|
+
else:
|
|
67
|
+
time.sleep(timeout_s + 0.5)
|
|
68
|
+
sw, error = dev.read("statusword"), dev.read("error_code")
|
|
69
|
+
observed = {"state": state_name(sw), "error": error, "statusword": sw}
|
|
70
|
+
finally:
|
|
71
|
+
_finish(dev, observed)
|
|
72
|
+
ok = (observed["state"] in expected["states"] and observed["error"] == expected["error"]
|
|
73
|
+
and bool(observed["statusword"] & 0x80) == expected["latched"])
|
|
74
|
+
report(f"{'通过' if ok else '不通过'}:{observed['state']},0x603F = 0x{observed['error']:04X},"
|
|
75
|
+
f"statusword 0x{observed['statusword']:04X}")
|
|
76
|
+
return {"expected": expected["text"], **observed, "ok": ok}
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def _finish(dev, observed):
|
|
80
|
+
# EtherCAT 停发后从站退回 SafeOp,输出被忽略:先重新进入 OP,收尾命令才能到达。
|
|
81
|
+
if hasattr(dev, "leave_op") and getattr(dev, "op", False):
|
|
82
|
+
dev.leave_op()
|
|
83
|
+
dev.enter_op()
|
|
84
|
+
fb = _stream(dev, CW_QUICK_STOP, 0.05)
|
|
85
|
+
if fb is not None and fb["state"] in ("OperationEnabled", "QuickStopActive"):
|
|
86
|
+
fb = _stream(dev, CW_QUICK_STOP, 0.3)
|
|
87
|
+
if observed is not None and observed["error"] == HOST_TIMEOUT and fb is not None and fb["error_code"] == HOST_TIMEOUT:
|
|
88
|
+
_stream(dev, CW_FAULT_RESET, 0.05)
|
|
89
|
+
_stream(dev, CW_DISABLE_VOLTAGE, 0.05)
|
|
90
|
+
state = state_name(dev.read("statusword"))
|
|
91
|
+
if state not in ("SwitchOnDisabled", "ReadyToSwitchOn", "Fault"):
|
|
92
|
+
raise DriveStateUnknown(f"收尾后设备仍报告 {state};用停止按钮“接管并断电”")
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
# Copyright The eha-firmware Contributors
|
|
2
|
+
|
|
3
|
+
## @file
|
|
4
|
+
## @brief BRT27 拉绳位移传感器的装配设置(firmware/app/eha/brt27.h 与 docs/suppliers/BRT27-CAN拉绳位移传感器.md):
|
|
5
|
+
## 经 PC 的 CAN 适配器直接对传感器设置主动回传模式 0xAA、回传周期与缩回端计数,设置掉电保存。帧为经典 CAN 标准数据帧,
|
|
6
|
+
## 仲裁 ID 为传感器节点地址,数据 [LEN][DEVICE_ID][FUNC][DATA...],LEN 含自身。回传周期为小端;设置当前位置 0x0D 为
|
|
7
|
+
## 大端(实板确认,见 brt27.h)。设置期间先切到查询模式,主动回传的位置帧不会淹没应答。
|
|
8
|
+
##
|
|
9
|
+
## 缺省值取自设备描述的 [settings](firmware/devices/displacement/brt27/v2_7.toml),本模块不另存一份。设置项里有
|
|
10
|
+
## 引用对象字典的值(缩回端计数 reference_count 指向 0x2231)时,由调用方从对象字典取实际值传入。
|
|
11
|
+
|
|
12
|
+
import time
|
|
13
|
+
|
|
14
|
+
from .devices import load_device
|
|
15
|
+
|
|
16
|
+
FUNC_POSITION = 0x01
|
|
17
|
+
FUNC_MODE = 0x04
|
|
18
|
+
FUNC_PERIOD = 0x05
|
|
19
|
+
FUNC_SET_POSITION = 0x0D
|
|
20
|
+
MODE_QUERY = 0x00
|
|
21
|
+
MODE_AUTO = 0xAA
|
|
22
|
+
|
|
23
|
+
## @brief 设备描述。[settings] 是装配时写进传感器的值,[roles] 里 node 是出厂节点。
|
|
24
|
+
DEVICE = "firmware/devices/displacement/brt27/v2_7.toml"
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
## @brief 装配用的缺省值:节点取 [roles].node 的下限(出厂节点),回传周期与缩回端计数取 [settings]。
|
|
28
|
+
## 计数引用对象字典时(reference_count 为 {source = 'od:0x...'}),这里给出 None,由调用方从对象字典取。
|
|
29
|
+
def defaults():
|
|
30
|
+
device = load_device(DEVICE)
|
|
31
|
+
settings = device.get("settings", {})
|
|
32
|
+
count = settings.get("reference_count")
|
|
33
|
+
return {"node": device["roles"]["node"]["min"],
|
|
34
|
+
"period_us": int(settings["report_period_us"]),
|
|
35
|
+
"count": None if isinstance(count, dict) else count}
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
## @brief 设置后回读计数与写入值允许的差(计数)。
|
|
39
|
+
COUNT_TOLERANCE = 5
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
## @brief 一条命令帧的数据。
|
|
43
|
+
def command(node, func, data):
|
|
44
|
+
return bytes((3 + len(data), node, func)) + bytes(data)
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
## @brief 设置序列:(说明, 功能码, 帧数据)。缩回端计数 count,回传周期 period_us(50–65535 µs)。
|
|
48
|
+
def setup_frames(node, count=None, period_us=None):
|
|
49
|
+
if not 1 <= node <= 255 or not 50 <= period_us <= 65535 or not 0 <= count <= 0xFFFFFFFF:
|
|
50
|
+
raise ValueError("节点须在 1–255,回传周期 50–65535 µs,计数 0–0xFFFFFFFF")
|
|
51
|
+
return [("切到查询模式", FUNC_MODE, command(node, FUNC_MODE, [MODE_QUERY])),
|
|
52
|
+
(f"回传周期 {period_us} µs", FUNC_PERIOD, command(node, FUNC_PERIOD, period_us.to_bytes(2, "little"))),
|
|
53
|
+
(f"当前位置(缩回端)写为 {count}", FUNC_SET_POSITION, command(node, FUNC_SET_POSITION, count.to_bytes(4, "big"))),
|
|
54
|
+
("切到主动回传 0xAA", FUNC_MODE, command(node, FUNC_MODE, [MODE_AUTO]))]
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
## @brief 在 bus(python-can 总线,速率与传感器一致)上执行设置:每条命令等本节点同一功能码的应答(设置当前位置还要求
|
|
58
|
+
## 状态为 0),没有应答即停止。之后侦听 listen_s 秒的位置帧,返回 {"count": 最后的计数, "frames": 帧数, "rate_hz": 频率}。
|
|
59
|
+
## 杆须已在缩回端。
|
|
60
|
+
def setup(bus, message_type, node, count=None, period_us=None, timeout=0.5, listen_s=0.5, report=print):
|
|
61
|
+
count = defaults()["count"] if count is None else count
|
|
62
|
+
period_us = defaults()["period_us"] if period_us is None else period_us
|
|
63
|
+
if count is None:
|
|
64
|
+
raise ValueError("缩回端计数须给出:设备描述里它引用对象字典(0x2231),从设备读出后传入")
|
|
65
|
+
for label, func, data in setup_frames(node, count, period_us):
|
|
66
|
+
bus.send(message_type(arbitration_id=node, is_extended_id=False, data=data))
|
|
67
|
+
reply = _reply(bus, node, func, timeout)
|
|
68
|
+
if reply is None:
|
|
69
|
+
raise RuntimeError(f"{label}:传感器没有应答(节点 {node}、速率、接线与终端电阻)")
|
|
70
|
+
if func == FUNC_SET_POSITION and len(reply) > 3 and reply[3] != 0:
|
|
71
|
+
raise RuntimeError(f"{label}:传感器返回状态 {reply[3]}")
|
|
72
|
+
report(f"{label}:已应答")
|
|
73
|
+
last, frames, end = None, 0, time.monotonic() + listen_s
|
|
74
|
+
while (remaining := end - time.monotonic()) > 0:
|
|
75
|
+
msg = bus.recv(remaining)
|
|
76
|
+
position = _position(msg, node)
|
|
77
|
+
if position is not None:
|
|
78
|
+
last, frames = position, frames + 1
|
|
79
|
+
if last is None:
|
|
80
|
+
raise RuntimeError("设置后没有收到主动回传的位置帧")
|
|
81
|
+
rate = frames / listen_s
|
|
82
|
+
report(f"主动回传:{rate:.0f} 帧/s,计数 {last}")
|
|
83
|
+
# 核对回读:计数应接近写入值(杆在缩回端不动;字节序错了会差出几个数量级),帧率应接近 1/周期。
|
|
84
|
+
if abs(last - count) > COUNT_TOLERANCE:
|
|
85
|
+
raise RuntimeError(f"回读计数 {last} 与写入的 {count} 不符:设置当前位置的字节序或杆的位置不对,按供应商资料核对")
|
|
86
|
+
if not 0.8 <= rate * period_us / 1e6 <= 1.2:
|
|
87
|
+
raise RuntimeError(f"主动回传 {rate:.0f} 帧/s 与回传周期 {period_us} µs 不符")
|
|
88
|
+
return {"count": last, "frames": frames, "rate_hz": rate}
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def _reply(bus, node, func, timeout):
|
|
92
|
+
end = time.monotonic() + timeout
|
|
93
|
+
while (remaining := end - time.monotonic()) > 0:
|
|
94
|
+
msg = bus.recv(remaining)
|
|
95
|
+
if (msg is not None and not msg.is_extended_id and msg.arbitration_id == node and len(msg.data) >= 3
|
|
96
|
+
and msg.data[1] == node and msg.data[2] == func):
|
|
97
|
+
return bytes(msg.data)
|
|
98
|
+
return None
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
def _position(msg, node):
|
|
102
|
+
if (msg is None or msg.is_extended_id or msg.arbitration_id != node or len(msg.data) != 7
|
|
103
|
+
or msg.data[:3] != bytes((7, node, FUNC_POSITION))):
|
|
104
|
+
return None
|
|
105
|
+
return int.from_bytes(msg.data[3:7], "little")
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
# Copyright The eha-firmware Contributors
|
|
2
|
+
|
|
3
|
+
## @file
|
|
4
|
+
## @brief ehatool brt27-sim:用 PC 的 CAN 适配器冒充 BRT27 拉绳位移传感器。适配器接板上的内部传感器 CAN(J8),按
|
|
5
|
+
## 传感器主动回传(模式 0xAA)的格式周期发送位置帧,没有传感器时用来验证位移的接收、换算与超时。只发位置帧,
|
|
6
|
+
## 不应答设置命令。帧格式见 firmware/app/eha/brt27.h。
|
|
7
|
+
##
|
|
8
|
+
## ehatool brt27-sim slcan:/dev/cu.usbmodem<序号> --count 10000 --swing 2000 --hz 0.5 --seconds 10
|
|
9
|
+
|
|
10
|
+
import argparse
|
|
11
|
+
import math
|
|
12
|
+
import sys
|
|
13
|
+
import threading
|
|
14
|
+
import time
|
|
15
|
+
|
|
16
|
+
from .device.can import CanLink
|
|
17
|
+
|
|
18
|
+
FUNC_POSITION = 0x01
|
|
19
|
+
COUNT_MAX = 0xFFFF_FFFF
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
## @brief 节点 node、计数 count 的位置帧数据:[LEN=7][DEVICE_ID][FUNC=0x01][小端 u32]。
|
|
23
|
+
def position_frame(node, count):
|
|
24
|
+
return bytes((7, node, FUNC_POSITION)) + count.to_bytes(4, "little")
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def register(sub):
|
|
28
|
+
p = sub.add_parser("brt27-sim", help="用 CAN 适配器冒充 BRT27 位移传感器(接 J8),周期发送位置帧")
|
|
29
|
+
p.add_argument("adapter", metavar="接口:通道",
|
|
30
|
+
help="接在内部传感器 CAN(J8)上的适配器,例如 slcan:/dev/cu.usbmodem<序号>、pcan:PCAN_USBBUS1")
|
|
31
|
+
p.add_argument("--rate", type=int, default=1000000,
|
|
32
|
+
help="经典 CAN 速率 bit/s,须与设备内部传感器 CAN 的速率(只读对象 0x2007)一致,型号统一为 1 Mbit/s")
|
|
33
|
+
p.add_argument("--sensor-node", type=int, default=1, help="传感器节点地址 1–255,固件按出厂默认 1 接收")
|
|
34
|
+
p.add_argument("--count", type=int, default=10000, help="中心计数(装配时缩回端写为 10000)")
|
|
35
|
+
p.add_argument("--swing", type=int, default=0, help="正弦摆动幅值(计数),0 为静止")
|
|
36
|
+
p.add_argument("--hz", type=float, default=0.5, help="正弦摆动频率 Hz")
|
|
37
|
+
p.add_argument("--period-us", type=int, default=1000, help="回传周期 µs,50–65535(传感器 0x05 的取值范围)")
|
|
38
|
+
p.add_argument("--seconds", type=float, default=0.0,
|
|
39
|
+
help="发送时长 s,0 为一直发(Ctrl-C 结束);停发后设备应在 0x2235 内报 0xFF30")
|
|
40
|
+
p.add_argument("--stop-on-stdin-close", action="store_true", help=argparse.SUPPRESS)
|
|
41
|
+
p.set_defaults(func=run, device=False)
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
## @brief CLI 与 Web 共用的发送参数检查;在打开适配器前拒绝无效帧、计数和时间。
|
|
45
|
+
def validate(args):
|
|
46
|
+
interface, separator, channel = args.adapter.partition(":")
|
|
47
|
+
if not separator or not interface or not channel:
|
|
48
|
+
raise ValueError("适配器写作 接口:通道,例如 slcan:/dev/cu.usbmodem<序号>")
|
|
49
|
+
if not 1 <= args.sensor_node <= 255:
|
|
50
|
+
raise ValueError("--sensor-node 须在 1–255")
|
|
51
|
+
if not 50 <= args.period_us <= 65535:
|
|
52
|
+
raise ValueError("--period-us 须在 50–65535")
|
|
53
|
+
if args.swing < 0 or args.count - args.swing < 0 or args.count + args.swing > COUNT_MAX:
|
|
54
|
+
raise ValueError("计数须在 0–0xFFFFFFFF 内:count ± swing 越界")
|
|
55
|
+
if args.rate <= 0:
|
|
56
|
+
raise ValueError("CAN 速率须为正数")
|
|
57
|
+
if not math.isfinite(args.hz) or args.hz < 0:
|
|
58
|
+
raise ValueError("摆动频率须为非负有限数")
|
|
59
|
+
if not math.isfinite(args.seconds) or args.seconds < 0:
|
|
60
|
+
raise ValueError("发送时长须为非负有限数,0 表示持续发送")
|
|
61
|
+
return interface, channel
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def run(args):
|
|
65
|
+
try:
|
|
66
|
+
interface, channel = validate(args)
|
|
67
|
+
except ValueError as error:
|
|
68
|
+
raise SystemExit(str(error)) from None
|
|
69
|
+
stopping = threading.Event()
|
|
70
|
+
if getattr(args, "stop_on_stdin_close", False):
|
|
71
|
+
# Web 服务持有管道写端,服务退出或崩溃后 EOF 让模拟器停发,避免后台遗留假位置源。
|
|
72
|
+
def watch_parent():
|
|
73
|
+
sys.stdin.buffer.read(1)
|
|
74
|
+
stopping.set()
|
|
75
|
+
threading.Thread(target=watch_parent, name="eha-brt-parent", daemon=True).start()
|
|
76
|
+
# 借用对外 CAN 通道的适配器打开逻辑(slcan 速率检查、串口独占、gs_usb 后端);节点号对这里没有意义。
|
|
77
|
+
link = CanLink(interface, channel, args.rate, 0, 1)
|
|
78
|
+
period = args.period_us * 1e-6
|
|
79
|
+
sent = failed = 0
|
|
80
|
+
gap_max = 0.0
|
|
81
|
+
message = link.can.Message(arbitration_id=args.sensor_node, is_extended_id=False, dlc=7)
|
|
82
|
+
print(f"BRT27 模拟:节点 {args.sensor_node}(ID 0x{args.sensor_node:03X}),{args.rate} bit/s,"
|
|
83
|
+
f"周期 {args.period_us} µs,计数 {args.count} ± {args.swing}")
|
|
84
|
+
start = last = time.monotonic()
|
|
85
|
+
deadline = start + args.seconds if args.seconds > 0 else None
|
|
86
|
+
next_at = start
|
|
87
|
+
try:
|
|
88
|
+
while not stopping.is_set() and (deadline is None or next_at < deadline):
|
|
89
|
+
now = time.monotonic()
|
|
90
|
+
if next_at > now:
|
|
91
|
+
if stopping.wait(next_at - now):
|
|
92
|
+
break
|
|
93
|
+
now = time.monotonic()
|
|
94
|
+
count = args.count + round(args.swing * math.sin(2 * math.pi * args.hz * (now - start)))
|
|
95
|
+
message.data = position_frame(args.sensor_node, count)
|
|
96
|
+
try:
|
|
97
|
+
link.bus.send(message, timeout=0.01)
|
|
98
|
+
sent += 1
|
|
99
|
+
except link.can.CanError:
|
|
100
|
+
failed += 1
|
|
101
|
+
gap_max = max(gap_max, now - last)
|
|
102
|
+
last = now
|
|
103
|
+
next_at += period
|
|
104
|
+
if next_at < now - period:
|
|
105
|
+
# 落后超过一个周期(系统调度停顿)时不补发,按当前时刻重新排期。
|
|
106
|
+
next_at = now + period
|
|
107
|
+
except KeyboardInterrupt:
|
|
108
|
+
pass
|
|
109
|
+
finally:
|
|
110
|
+
link.close()
|
|
111
|
+
elapsed = last - start
|
|
112
|
+
mean_us = elapsed / (sent + failed - 1) * 1e6 if sent + failed > 1 else 0.0
|
|
113
|
+
print(f"已停发:成功 {sent} 帧,失败 {failed} 帧(无应答或发送队列满),平均间隔 {mean_us:.0f} µs,"
|
|
114
|
+
f"最大间隔 {gap_max * 1e3:.2f} ms(主机侧,含系统调度抖动)")
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
# Copyright The eha-firmware Contributors
|
|
2
|
+
|
|
3
|
+
## @file
|
|
4
|
+
## @brief 装配标定的计算(eha-product 技能“装配调试”第 2–4 步):不依赖设备的纯函数。换算式与固件 eha_meas.c 相同:
|
|
5
|
+
## 位置 mm = (计数 − 0x2231) × 0x2230 × 方向(0x2232 = 1 时方向为 −1);压力 = 低点压力 + 斜率 × (原始值 − 低点原始值)。
|
|
6
|
+
## 输入与输出都用对象字典的单位:位移 µm,压力 MPa,计数与原始值为整数。
|
|
7
|
+
|
|
8
|
+
import math
|
|
9
|
+
import statistics
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
## @brief 一组原始读数的统计:均值、标准差(样本数不足 2 时为 0)与样本数;没有读数时抛出 ValueError。
|
|
13
|
+
def stats(values):
|
|
14
|
+
values = [float(v) for v in values]
|
|
15
|
+
if not values:
|
|
16
|
+
raise ValueError("没有读数")
|
|
17
|
+
return {"mean": statistics.fmean(values), "std": statistics.stdev(values) if len(values) > 1 else 0.0,
|
|
18
|
+
"n": len(values)}
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def _finite(*values):
|
|
22
|
+
if not all(isinstance(v, (int, float)) and math.isfinite(v) for v in values):
|
|
23
|
+
raise ValueError("输入须为有限的数")
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
## @brief 四舍五入到整数,.5 远离零(与固件的 llroundf 一致;Python 的 round 是银行家舍入)。
|
|
27
|
+
def round_half_away(x):
|
|
28
|
+
return int(math.copysign(math.floor(abs(x) + 0.5), x))
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
## @brief 计数换为位置(µm):与固件相同的换算式。
|
|
32
|
+
def position_um(count, um_per_count, reference_count, reversed_):
|
|
33
|
+
return (count - reference_count) * um_per_count * (-1 if reversed_ else 1)
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
## @brief 使位置 x_um 对应计数 count 的零点(0x2231,取整),每计数位移与方向不变。第 2 步的临时零点与第 3 步共用。
|
|
37
|
+
def reference_for(count, x_um, um_per_count, reversed_):
|
|
38
|
+
_finite(count, x_um, um_per_count)
|
|
39
|
+
return round_half_away(count - x_um / um_per_count * (-1 if reversed_ else 1))
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
## @brief 位移两点标定:已知位置 x1、x2(µm)处读得计数 c1、c2。每计数位移 = |x2 − x1| ÷ |c2 − c1|;
|
|
43
|
+
## (c2 − c1)/(x2 − x1) 为负时计数增大对应缩回(0x2232 = 1);零点按第一点求出。两点位置或计数相同时抛出 ValueError。
|
|
44
|
+
## 返回 {"um_per_count", "reversed", "reference_count"},对应 0x2230、0x2232、0x2231。
|
|
45
|
+
def position_two_point(x1_um, c1, x2_um, c2):
|
|
46
|
+
_finite(x1_um, c1, x2_um, c2)
|
|
47
|
+
if x1_um == x2_um:
|
|
48
|
+
raise ValueError("两个已知位置相同,求不出每计数位移")
|
|
49
|
+
if c1 == c2:
|
|
50
|
+
raise ValueError("两点的计数相同:传感器没有随杆移动,或没有收到新的位置帧")
|
|
51
|
+
um_per_count = abs(x2_um - x1_um) / abs(c2 - c1)
|
|
52
|
+
reversed_ = (c2 - c1) / (x2_um - x1_um) < 0
|
|
53
|
+
return {"um_per_count": um_per_count, "reversed": int(reversed_),
|
|
54
|
+
"reference_count": reference_for(c1, x1_um, um_per_count, reversed_)}
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
## @brief 复核点:按标定结果算出计数 c 处的位置,与已知位置 x_um 之差(µm,计算值减已知值)。
|
|
58
|
+
def position_check(cal, c, x_um):
|
|
59
|
+
return position_um(c, cal["um_per_count"], cal["reference_count"], cal["reversed"]) - x_um
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
## @brief 一腔压力两点标定:(原始值, 压力 MPa) 两点,按原始值从低到高排为低点、高点(固件要求高点原始值与压力都更大)。
|
|
63
|
+
## 返回 {"raw_min", "raw_max", "mpa_at_min", "mpa_at_max", "slope_ok"};原始值相同时抛出 ValueError。slope_ok 为假表示
|
|
64
|
+
## 压力随原始值下降,写入后设备的 0x2028 会报斜率不为正,以设备为准。
|
|
65
|
+
def pressure_two_point(raw1, mpa1, raw2, mpa2):
|
|
66
|
+
_finite(raw1, mpa1, raw2, mpa2)
|
|
67
|
+
if raw1 == raw2:
|
|
68
|
+
raise ValueError("两点的原始值相同:压力没有变化,或传感器没有接上")
|
|
69
|
+
(lo_raw, lo_mpa), (hi_raw, hi_mpa) = sorted(((int(raw1), float(mpa1)), (int(raw2), float(mpa2))))
|
|
70
|
+
return {"raw_min": lo_raw, "raw_max": hi_raw, "mpa_at_min": lo_mpa, "mpa_at_max": hi_mpa,
|
|
71
|
+
"slope_ok": hi_mpa > lo_mpa and math.isfinite(hi_mpa - lo_mpa)}
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
## @brief 原始值换为压力(MPa):与固件相同的换算式。
|
|
75
|
+
def pressure_mpa(raw, cal):
|
|
76
|
+
slope = (cal["mpa_at_max"] - cal["mpa_at_min"]) / (cal["raw_max"] - cal["raw_min"])
|
|
77
|
+
return cal["mpa_at_min"] + slope * (raw - cal["raw_min"])
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
## @brief 第 2 步的方向判断:一次低速短时运行(转速 rpm,正为电机正转)前后的变化与用户看到的动作。
|
|
81
|
+
## 位置变化不超过测量误差 position_error_um、压力变化不超过各自误差时为“无法判断”。固件坐标伸出为正;0x2202 = 1 表示
|
|
82
|
+
## 电机正转使活塞缩回;0x2203 为压力升高产生伸出力的一腔(1 A、2 B)。observed 为 "伸出"、"缩回" 或 "没动"。
|
|
83
|
+
## 返回 {"measured": 位置测得的方向, "suggest": {对象名: (建议值, 原因)}, "checked": 实际判断了的对象};只给建议,
|
|
84
|
+
## 由用户决定是否写入。没有看到动作、位置变化不超过误差、或不是恰好一腔压力升高时,相应的对象不在 checked 中。
|
|
85
|
+
def direction_check(rpm, observed, motor_reversed, position_delta_um, position_error_um,
|
|
86
|
+
pressure_a_delta, pressure_b_delta, pressure_a_error, pressure_b_error, positive_channel):
|
|
87
|
+
_finite(rpm, position_delta_um, position_error_um, pressure_a_delta, pressure_b_delta, pressure_a_error, pressure_b_error)
|
|
88
|
+
checked = []
|
|
89
|
+
if abs(position_delta_um) <= position_error_um:
|
|
90
|
+
measured = "无法判断"
|
|
91
|
+
else:
|
|
92
|
+
measured = "伸出" if position_delta_um > 0 else "缩回"
|
|
93
|
+
suggest = {}
|
|
94
|
+
if observed in ("伸出", "缩回") and rpm:
|
|
95
|
+
checked.append("motor_reversed")
|
|
96
|
+
extends = (rpm > 0) != bool(motor_reversed)
|
|
97
|
+
if extends != (observed == "伸出"):
|
|
98
|
+
suggest["motor_reversed"] = (0 if motor_reversed else 1,
|
|
99
|
+
f"电机{'正' if rpm > 0 else '反'}转时活塞{observed},与 0x2202 = {motor_reversed} 不符")
|
|
100
|
+
if measured != "无法判断":
|
|
101
|
+
checked.append("position_reversed")
|
|
102
|
+
if measured != "无法判断" and measured != observed:
|
|
103
|
+
suggest["position_reversed"] = (None, f"位置测得{measured}而实际{observed}:位移方向 0x2232 不对,在第 3 步两点标定时确定")
|
|
104
|
+
rose = [ch for ch, delta, error in ((1, pressure_a_delta, pressure_a_error), (2, pressure_b_delta, pressure_b_error))
|
|
105
|
+
if delta > error]
|
|
106
|
+
if len(rose) == 1:
|
|
107
|
+
checked.append("pressure_positive_channel")
|
|
108
|
+
extend_channel = rose[0] if observed == "伸出" else 3 - rose[0]
|
|
109
|
+
if extend_channel != positive_channel:
|
|
110
|
+
suggest["pressure_positive_channel"] = (extend_channel, f"活塞{observed}时{'AB'[rose[0] - 1]} 腔压力升高")
|
|
111
|
+
return {"measured": measured, "suggest": suggest, "checked": checked}
|