Compare commits

...

14 Commits

Author SHA1 Message Date
8ba12e11a0 test 2026-07-16 15:33:40 +08:00
a6acfe898c test push tutorial 2026-07-16 15:32:27 +08:00
219c98d5b6 clear things up 2026-07-16 15:30:29 +08:00
feed450554 init 2026-07-16 15:24:40 +08:00
22ae87f4b4 Merge branch 'jgy-dev-bugfix-2025-01-07' into 'main'
添加电容同步采集功能

See merge request tactilesensor/fingersensorsdk/finger/python!7
2025-01-07 05:46:44 +00:00
a5895c41b0 修改bug 2025-01-07 13:40:17 +08:00
8e01b24dac 添加电容同步采集功能 2025-01-07 13:35:29 +08:00
b5455a269c Merge branch 'jgy-dev-readme-2024-12-11' into 'main'
修改readme文件;添加手指数量宏定义;修改打印书写错误

See merge request tactilesensor/fingersensorsdk/finger/python!6
2024-12-11 05:32:52 +00:00
cb39e602a3 修改readme 2024-12-11 13:32:08 +08:00
b86558971e 修改readme 2024-12-11 13:29:56 +08:00
3a277fe631 修改readme图片加载 2024-12-11 13:28:40 +08:00
5dc6047716 修改readme文件;添加手指数量宏定义;修改打印书写错误 2024-12-11 13:23:53 +08:00
bf89c0f960 Merge branch 'jgy-dev-readme-2024-12-11' into 'main'
修改readme文件

See merge request tactilesensor/fingersensorsdk/finger/python!5
2024-12-11 04:56:20 +00:00
3fc2463139 修改readme文件 2024-12-11 12:54:43 +08:00
23 changed files with 2719 additions and 87 deletions

BIN
CH341PAR .EXE Normal file

Binary file not shown.

Binary file not shown.

746
README.md
View File

@ -1,93 +1,717 @@
# python
# 触觉传感器与执行器控制系统 —— 使用手册
## 项目概述
本项目提供了一套完整的触觉传感器数据采集与执行器(舵机、直线电机)控制系统,包含底层 I2C 通信驱动、传感器数据解析、多指数据采集、串口执行器控制、手柄/键盘遥控以及数据记录等功能模块。
## Getting started
To make it easy for you to get started with GitLab, here's a list of recommended next steps.
Already a pro? Just edit this README.md and make it your own. Want to make it easy? [Use the template at the bottom](#editing-this-readme)!
## Add your files
- [ ] [Create](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#create-a-file) or [upload](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#upload-a-file) files
- [ ] [Add files using the command line](https://docs.gitlab.com/ee/gitlab-basics/add-file.html#add-a-file-using-the-command-line) or push an existing Git repository with the following command:
### 系统架构
```
cd existing_repo
git remote add origin http://192.168.5.18/tactilesensor/fingersensorsdk/finger/python.git
git branch -M main
git push -uf origin main
┌──────────────────────────────────────────────────────┐
│ 应用层 (Application) │
│ demo_auto_grasp.py test_dof_control.py │
│ gamepad_remote.py gamepad_remote_new_pcb.py │
│ keyboard_remote_new_pcb.py data_logger.py │
│ grasp_network_model.py │
├──────────────────────────────────────────────────────┤
│ 数据采集层 (DAQ Layer) │
│ cap_read.py tactile_sensor_daq.py │
├──────────────────────────────────────────────────────┤
│ 驱动抽象层 (Driver Layer) │
│ class_finger.py class_sensorcmd.py sensorPara.py │
│ class_ch341.py serial_robot_driver.py │
├──────────────────────────────────────────────────────┤
│ 硬件层 (Hardware) │
│ CH341 (USB-I2C) STM32F103 (Serial) │
│ 触觉传感器 舵机 / 直线电机 │
└──────────────────────────────────────────────────────┘
```
## Integrate with your tools
---
- [ ] [Set up project integrations](http://192.168.5.18/tactilesensor/fingersensorsdk/finger/python/-/settings/integrations)
## 目录
## Collaborate with your team
1. [环境准备](#1-环境准备)
2. [模块说明](#2-模块说明)
3. [快速开始](#3-快速开始)
4. [底层驱动详解](#4-底层驱动详解)
5. [数据采集模块](#5-数据采集模块)
6. [执行器控制](#6-执行器控制)
7. [遥控模块](#7-遥控模块)
8. [数据记录](#8-数据记录)
9. [神经网络模型](#9-神经网络模型)
10. [STM32 固件](#10-stm32-固件)
11. [硬件接线](#11-硬件接线)
12. [常见问题](#12-常见问题)
- [ ] [Invite team members and collaborators](https://docs.gitlab.com/ee/user/project/members/)
- [ ] [Create a new merge request](https://docs.gitlab.com/ee/user/project/merge_requests/creating_merge_requests.html)
- [ ] [Automatically close issues from merge requests](https://docs.gitlab.com/ee/user/project/issues/managing_issues.html#closing-issues-automatically)
- [ ] [Enable merge request approvals](https://docs.gitlab.com/ee/user/project/merge_requests/approvals/)
- [ ] [Set auto-merge](https://docs.gitlab.com/ee/user/project/merge_requests/merge_when_pipeline_succeeds.html)
---
## Test and Deploy
## 1. 环境准备
Use the built-in continuous integration in GitLab.
> **重要:请严格按照以下顺序操作,先安装驱动,再安装 Python 依赖。**
- [ ] [Get started with GitLab CI/CD](https://docs.gitlab.com/ee/ci/quick_start/index.html)
- [ ] [Analyze your code for known vulnerabilities with Static Application Security Testing (SAST)](https://docs.gitlab.com/ee/user/application_security/sast/)
- [ ] [Deploy to Kubernetes, Amazon EC2, or Amazon ECS using Auto Deploy](https://docs.gitlab.com/ee/topics/autodevops/requirements.html)
- [ ] [Use pull-based deployments for improved Kubernetes management](https://docs.gitlab.com/ee/user/clusters/agent/)
- [ ] [Set up protected environments](https://docs.gitlab.com/ee/ci/environments/protected_environments.html)
### 1.1 CH341 驱动安装(必须首先完成)
***
CH341 是 USB 转 I2C 芯片,用于与触觉传感器通信。**使用本项目前,必须先安装此驱动**,否则传感器无法被电脑识别。
# Editing this README
#### Windows
When you're ready to make this README your own, just edit this file and use the handy template below (or feel free to structure it however you want - this is just a starting point!). Thanks to [makeareadme.com](https://www.makeareadme.com/) for this template.
1. 在项目根目录下找到压缩包 **`CH341驱动先卸载再安装.zip`**,将其解压。
2. **如果之前安装过其它版本的 CH341 驱动,请先卸载**
3. 运行解压后的 `CH341PAR.exe`,按照提示完成安装。
4. 安装完成后,将传感器通过 CH341 转接板连接电脑 USB系统会自动识别设备。
## Suggestions for a good README
> 驱动安装后,项目代码会自动加载 `lib/ch341/CH341DLLA64.DLL`,无需额外配置。
Every project is different, so consider which of these sections apply to yours. The sections used in the template are suggestions for most open source projects. Also keep in mind that while a README can be too long and detailed, too long is better than too short. If you think your README is too long, consider utilizing another form of documentation rather than cutting out information.
#### Linux
## Name
Choose a self-explaining name for your project.
1.`lib/ch341/libch347.so` 放置到系统库路径或项目目录下。
## Description
Let people know what your project can do specifically. Provide context and add a link to any reference visitors might be unfamiliar with. A list of Features or a Background subsection can also be added here. If there are alternatives to your project, this is a good place to list differentiating factors.
2. 配置 udev 规则(将 `john` 替换为你的用户名):
```bash
# 将用户添加到 dialout 组
sudo usermod -a -G dialout john
## Badges
On some READMEs, you may see small images that convey metadata, such as whether or not all the tests are passing for the project. You can use Shields to add some to your README. Many services also have instructions for adding a badge.
# 创建 udev 规则文件
sudo bash -c 'echo "SUBSYSTEM==\"usbmisc\", ATTRS{idVendor}==\"1a86\", ATTRS{idProduct}==\"5512\", MODE=\"0666\", GROUP=\"dialout\"" > /etc/udev/rules.d/ch341.rules'
## Visuals
Depending on what you are making, it can be a good idea to include screenshots or even a video (you'll frequently see GIFs rather than actual videos). Tools like ttygif can help, but check out Asciinema for a more sophisticated method.
# 重载规则
sudo udevadm control --reload-rules
sudo udevadm trigger
```
## Installation
Within a particular ecosystem, there may be a common way of installing things, such as using Yarn, NuGet, or Homebrew. However, consider the possibility that whoever is reading your README is a novice and would like more guidance. Listing specific steps helps remove ambiguity and gets people to using your project as quickly as possible. If it only runs in a specific context like a particular programming language version or operating system or has dependencies that have to be installed manually, also add a Requirements subsection.
3. 重新插拔 CH341 设备,或重启计算机使 udev 规则生效。
## Usage
Use examples liberally, and show the expected output if you can. It's helpful to have inline the smallest example of usage that you can demonstrate, while providing links to more sophisticated examples if they are too long to reasonably include in the README.
### 1.2 Python 依赖
## Support
Tell people where they can go to for help. It can be any combination of an issue tracker, a chat room, an email address, etc.
推荐使用 Python 3.8 及以上版本。安装依赖:
## Roadmap
If you have ideas for releases in the future, it is a good idea to list them in the README.
```bash
pip install pyserial numpy pygame keyboard torch
```
## Contributing
State if you are open to contributions and what your requirements are for accepting them.
各模块依赖说明:
For people who want to make changes to your project, it's helpful to have some documentation on how to get started. Perhaps there is a script that they should run or some environment variables that they need to set. Make these steps explicit. These instructions could also be useful to your future self.
| 模块 | 依赖 |
|------|------|
| 底层驱动 (class_ch341, class_sensorcmd, class_finger) | 无额外依赖 |
| 数据采集 (cap_read, tactile_sensor_daq) | numpy |
| 串口执行器 (serial_robot_driver) | pyserial |
| 手柄遥控 (gamepad_remote, gamepad_remote_new_pcb) | pyserial, pygame |
| 键盘遥控 (keyboard_remote_new_pcb) | pyserial, keyboard, numpy |
| 数据记录 (data_logger) | numpy |
| 神经网络 (grasp_network_model) | torch (PyTorch) |
You can also document commands to lint the code or run tests. These steps help to ensure high code quality and reduce the likelihood that the changes inadvertently break something. Having instructions for running tests is especially helpful if it requires external setup, such as starting a Selenium server for testing in a browser.
> **说明:** 如果仅使用传感器数据读取功能cap_read、tactile_sensor_daq只需安装 `numpy`。执行器控制、遥控、神经网络等模块可按需安装对应依赖。
## Authors and acknowledgment
Show your appreciation to those who have contributed to the project.
---
## License
For open source projects, say how it is licensed.
## 2. 模块说明
## Project status
If you have run out of energy or time for your project, put a note at the top of the README saying that development has slowed down or stopped completely. Someone may choose to fork your project or volunteer to step in as a maintainer or owner, allowing your project to keep going. You can also make an explicit request for maintainers.
### 2.1 文件清单
| 文件名 | 功能说明 |
|--------|----------|
| `class_ch341.py` | CH341 USB-I2C 底层驱动,封装 I2C 读写、速度设置、INT 引脚控制 |
| `class_sensorcmd.py` | 传感器 I2C 命令协议,封装所有传感器配置与读取命令 |
| `class_finger.py` | 传感器(手指)数据解析,包含电容/力数据读取与协议解析 |
| `sensorPara.py` | 传感器参数定义(通道数、力单元数、数据格式等) |
| `cap_read.py` | 基础电容读取入口,支持单指/多指数据采集与实时可视化 |
| `tactile_sensor_daq.py` | 多指触觉传感器数据采集模块(线程安全,支持去皮) |
| `serial_robot_driver.py` | 串口执行器驱动,控制舵机和直线电机 |
| `stm32f103_drv8870_servo_main.c` | STM32F103 下位机固件源码 |
| `demo_auto_grasp.py` | 演示程序:触觉反馈自动抓取任务 |
| `gamepad_remote.py` | 游戏手柄遥控程序 |
| `gamepad_remote_new_pcb.py` | 新 PCB 版手柄遥控(支持 ACK 协议和自检) |
| `keyboard_remote_new_pcb.py` | 新 PCB 版键盘遥控(支持力反馈与压力计算) |
| `test_dof_control.py` | 自由度独立控制测试 |
| `data_logger.py` | CSV 数据记录器 |
| `grasp_network_model.py` | 条件抓取网络模型Conditional MLP |
| `接线说明.txt` | 硬件接线参考 |
| `新PCB控制实施方案.md` | 新 PCB 控制系统实施方案 |
| `CH341驱动先卸载再安装.zip` | CH341 驱动程序压缩包,使用前必须先解压安装 |
| `传感器iic地址和转接板位置定义.png` | I2C 地址与接口位置对照图 |
---
## 3. 快速开始
### 3.1 仅读取单个传感器数据
```bash
python cap_read.py
```
运行前可在 `cap_read.py` 中修改传感器连接数量:
```python
DEF_MAX_FINGER_NUM = 1 # 需要连接的手指数量最大5个
```
输出数据包括:
- `capChannelDat`:电容通道原始值
- `nf[i]`:第 i 个单元的法向力
- `tf[i]`:第 i 个单元的切向力
- `tfDir[i]`:第 i 个单元的切向力方向
- `sProxCapData`:自电容接近值
- `mProxCapData`:互电容接近值
### 3.2 读取多个传感器数据100Hz
```python
from tactile_sensor_daq import TactileSensorDAQ
sensor = TactileSensorDAQ()
sensor.start()
# 等待传感器连接稳定
import time
time.sleep(3)
sensor.tare() # 去皮
while True:
data = sensor.get_data() # 获取 12 维力数据
print(data)
time.sleep(0.1)
```
### 3.3 控制执行器(串口)
```python
from serial_robot_driver import RobotDriver
robot = RobotDriver(port='COM9')
robot.motor_open() # 直线电机张开
robot.motor_close() # 直线电机闭合
robot.motor_stop() # 停止
robot.set_servo(1, 90) # 舵机1转到90度
robot.set_config(0) # 构型0初始位
robot.close()
```
### 3.4 命令行快速测试
```bash
# 测试通信
python gamepad_remote_new_pcb.py --port COM9 --cmd PING
# 硬件自检
python gamepad_remote_new_pcb.py --port COM9 --test
# 手柄遥控
python gamepad_remote_new_pcb.py --port COM9
# 键盘遥控
python keyboard_remote_new_pcb.py --port COM9
```
---
## 4. 底层驱动详解
### 4.1 class_ch341.py —— CH341 I2C 通信驱动
封装 CH341 USB-I2C 转换芯片的操作。
**主要接口:**
```python
from class_ch341 import ClassCh341
ch341 = ClassCh341()
# 初始化并打开设备
ch341.init() # 加载 DLL/SO 库
ch341.open() # 打开 USB 设备
# 设置 I2C 速度
ch341.set_speed(ch341.IIC_SPEED_400) # 20/100/400/750 kHz
# I2C 读写
ch341.write(addr, data_list) # 向从机地址写入数据列表
ch341.read(addr, data_list) # 从从机地址读取数据到列表
# 连接检查
ch341.connectCheck() # 返回 True/False
# 断开
ch341.disconnect()
```
### 4.2 class_sensorcmd.py —— 传感器命令协议
封装与触觉传感器 MCU 的 I2C 命令协议。
**主要接口:**
```python
from class_sensorcmd import ClassSensorCmd
snsCmd = ClassSensorCmd(ch341)
# 地址管理
snsCmd.getAddr(addr) # 读取传感器 I2C 地址
snsCmd.setAddr(old_addr, new) # 设置新地址
# 传感器配置
snsCmd.setSensorSendType(addr, 0) # 设置数据发送类型为原始值
snsCmd.setSensorCapOffset(addr, offset) # 设置电容采集时序偏移
# 读取数据
snsCmd.getSensorCapData(addr, buf) # 读取电容数据
snsCmd.getSensorProjectIdex(addr) # 读取项目编号
# 同步
snsCmd.setSensorSync(addr) # 多传感器同步
```
### 4.3 sensorPara.py —— 传感器参数定义
定义了传感器项目的参数结构和具体参数值。
```python
from sensorPara import finger_params, FingerParamTS, DynamicYddsU16Ts
# finger_params 包含所有支持的传感器类型
# 目前支持:
# - 项目2: "通用手指", 8通道, 1个三维力单元
# - 项目17: "两指-大包", 16通道, 2个三维力单元
```
如需添加新的传感器型号,在 `sensorPara.py` 中追加 `FingerParamTS` 条目即可。
### 4.4 class_finger.py —— 传感器数据解析
管理单个传感器(手指)的连接状态和数据读取。
```python
from class_finger import ClassFinger, capData
finger = ClassFinger(pca_idx=2, ch341=ch341)
# 检查传感器连接
if finger.checkSensor():
print("Sensor connected")
# 读取数据
finger.capRead()
# 访问数据
finger.readData.channelCapData # 电容通道原始值
finger.readData.nf[i] # 第i个单元的法向力
finger.readData.tf[i] # 第i个单元的切向力
finger.readData.tfDir[i] # 第i个单元的切向力方向
finger.readData.sProxCapData # 自电容接近值
finger.readData.mProxCapData # 互电容接近值
```
---
## 5. 数据采集模块
### 5.1 cap_read.py —— 基础电容读取
最简单直接的传感器数据读取入口。内部维护 CH341 连接状态机,定时轮询读取电容数据,并通过 TCP Socket 发送到 VOFA+ 等调试工具进行实时可视化。
**配置参数:**
```python
DEF_MAX_FINGER_NUM = 1 # 连接手指数量 (1-5)
DEF_GET_CAP_MS = 30 # 读取间隔 (ms)
DEF_CDC_SYNC_MS = 1000 # 电容同步间隔 (ms),多传感器时使用
```
**运行:**
```bash
python cap_read.py
```
数据通过 Socket 发送到 `127.0.0.1:1347`VOFA+ 默认端口)。
### 5.2 tactile_sensor_daq.py —— 多指触觉传感器 DAQ
线程安全的传感器数据采集模块,支持 3 个手指、12 维力数据输出含去皮tare功能。
```python
from tactile_sensor_daq import TactileSensorDAQ
sensor = TactileSensorDAQ()
sensor.start() # 启动后台采集线程
sensor.tare() # 去皮:将当前读数归零
data = sensor.get_data() # 获取 12 维 numpy 数组 [F0_U1_Fn, F0_U1_Ft, F0_U2_Fn, ...]
sensor.stop() # 停止采集
```
**数据格式**12 维 float32
| 索引 | 含义 |
|------|------|
| 0-1 | 手指0-单元1: 法向力(Fn), 切向力(Ft) |
| 2-3 | 手指0-单元2: 法向力(Fn), 切向力(Ft) |
| 4-5 | 手指1-单元1: 法向力(Fn), 切向力(Ft) |
| 6-7 | 手指1-单元2: 法向力(Fn), 切向力(Ft) |
| 8-9 | 手指2-单元1: 法向力(Fn), 切向力(Ft) |
| 10-11| 手指2-单元2: 法向力(Fn), 切向力(Ft) |
**配置参数:**
```python
DEF_MAX_FINGER_NUM = 3 # 传感器数量
SAMPLE_RATE_MS = 10 # 采样间隔 (ms),默认 100Hz
```
---
## 6. 执行器控制
### 6.1 serial_robot_driver.py —— 串口执行器驱动
通过串口USB-TTL向 STM32 下位机发送 ASCII 命令,控制直线电机和舵机。
```python
from serial_robot_driver import RobotDriver
robot = RobotDriver(port='COM9', baud=115200)
# 直线电机控制
robot.motor_open() # 张开
robot.motor_close() # 闭合
robot.motor_stop() # 停止
# 舵机控制
robot.set_servo(1, 90) # 舵机1转到90度
robot.set_servo(2, 120) # 舵机2转到120度
# 构型切换(组合动作)
robot.set_config(0) # 初始构型: S1=90, S2=90
robot.set_config(1) # 错位构型: S1=30, S2=150
robot.set_config(2) # 对握构型: S1=120, S2=60
robot.close()
```
### 6.2 串口通信协议
上位机向 STM32 发送 ASCII 命令(以 `\r\n` 结尾STM32 回复 `OK:``ERR:` 确认。
| 命令 | 功能 | 回复 |
|------|------|------|
| `PING` | 通信测试 | `OK:PONG` |
| `M:OPEN` | 直线电机张开 | `OK:M:OPEN` |
| `M:CLOSE` | 直线电机闭合 | `OK:M:CLOSE` |
| `M:STOP` | 直线电机停止 | `OK:M:STOP` |
| `S1:<angle>` | 舵机1角度(0-180) | `OK:S1` |
| `S2:<angle>` | 舵机2角度(0-180) | `OK:S2` |
| `CFG:<mode>` | 构型切换(0/1/2) | `OK:CFG` |
### 6.3 gamepad_remote_new_pcb.py —— 新 PCB 版执行器驱动
相比 `serial_robot_driver.py`,增加了 ACK 确认、命令行参数、自检等功能。
```bash
# 单条命令
python gamepad_remote_new_pcb.py --port COM9 --cmd PING
python gamepad_remote_new_pcb.py --port COM9 --cmd S1:90
# 硬件自检
python gamepad_remote_new_pcb.py --port COM9 --test
# 不等待 ACK (适用于简单固件)
python gamepad_remote_new_pcb.py --port COM9 --no-ack
```
---
## 7. 遥控模块
### 7.1 gamepad_remote.py —— 手柄遥控
使用 Xbox/兼容游戏手柄远程控制执行器。
**按键映射:**
| 按键 | 功能 |
|------|------|
| A | 构型0初始 |
| B | 构型1错位 |
| X | 构型2对握 |
| LB 长按 | 直线电机张开 |
| RB 长按 | 直线电机闭合 |
| 松开 LB/RB | 电机停止 |
| BACK | 急停切换 |
| START | 退出 |
**运行:**
```bash
python gamepad_remote.py
```
默认连接 COM9可在代码中修改 `port` 变量。
### 7.2 keyboard_remote_new_pcb.py —— 键盘遥控
使用键盘远程控制执行器,同时读取触觉传感器数据,支持力反馈自动抓取。
**按键映射:**
| 按键 | 功能 |
|------|------|
| q | 构型0初始 |
| w | 构型1错位 |
| e | 构型2对握 |
| ← 长按 | 直线电机闭合 |
| → 长按 | 直线电机张开 |
| 空格 | 急停/恢复 |
| Esc | 退出 |
**主要功能:**
- 按下闭合键持续闭合,松开自动停止并打印执行时间
- 实时显示接触压力KPa
- 支持急停保护和构型切换
**运行:**
```bash
python keyboard_remote_new_pcb.py --port COM9
python keyboard_remote_new_pcb.py --port COM9 --test # 自检模式
python keyboard_remote_new_pcb.py --port COM9 --contact-area-mm2 240 # 自定义接触面积
```
---
## 8. 数据记录
### data_logger.py —— CSV 数据记录器
将触觉传感器数据记录为 CSV 文件,自动创建 `data/` 目录并以时间戳命名。
```python
from data_logger import DataLogger
logger = DataLogger(filename_prefix="experiment_01")
# 记录一行数据
# sensor_data: 12维触觉数据
# config_id: 构型ID (0/1/2)
# label_vector: 标签 [x, y, theta]
logger.log(sensor_data=sensor.get_data(), config_id=1, label_vector=[0.0, 5.5, -2.0])
```
**CSV 格式:**
| Timestamp | Config_ID | F0_U1_Fn | F0_U1_Ft | ... | Label_X | Label_Y | Label_Theta |
|-----------|-----------|----------|----------|-----|---------|---------|-------------|
---
## 9. 神经网络模型
### grasp_network_model.py —— 条件抓取网络 (CondGraspNet)
一个条件 MLP 网络,输入 12 维触觉数据 + 3 维构型编码,输出 3 维偏差预测 [ΔX, ΔY, Δθ]。
**网络结构:**
```
Input(15) → FC(64)+BN+ReLU → FC(128)+BN+ReLU → FC(64)+ReLU → Output(3)
```
**使用示例:**
```python
from grasp_network_model import CondGraspNet
import torch
model = CondGraspNet()
tactile_data = torch.randn(8, 12) # Batch=8, 12维触觉
config_ids = torch.tensor([0,0,1,1,2,2,0,2]) # 构型ID
predictions = model(tactile_data, config_ids) # [8, 3] 输出
```
**单元测试:**
```bash
python grasp_network_model.py
```
---
## 10. STM32 固件
### stm32f103_drv8870_servo_main.c
STM32F103C8T6 下位机固件,负责接收上位机串口命令并控制硬件。
**硬件映射:**
| 功能 | STM32引脚 | 说明 |
|------|-----------|------|
| USART1 TX | PA9 | 连接 USB-TTL RXD |
| USART1 RX | PA10 | 连接 USB-TTL TXD |
| 直线电机 IN1 | PB12 | 连接 DRV8870 IN1 |
| 直线电机 IN2 | PB13 | 连接 DRV8870 IN2 |
| 舵机1 PWM | PB11 | TIM2_CH4, 50Hz |
| 舵机2 PWM | PB10 | TIM2_CH3, 50Hz |
**CubeMX 配置要点:**
- TIM2: Prescaler=71, Counter Period=19999, 产生 50Hz PWM
- USART1: 115200 8N1
- 需开启 TIM2 重映射AFIO Remap
**集成方法:**
`stm32f103_drv8870_servo_main.c` 中的用户代码集成到 CubeMX 生成的工程:
```c
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_USART1_UART_Init();
MX_TIM2_Init();
app_init(); // 初始化舵机和电机
while (1) {
app_loop(); // 主循环:处理串口命令 + 看门狗
}
}
```
**安全特性:**
- 1 秒无指令自动停止直线电机(通信丢失保护)
- 命令超长自动拒绝
### 调试顺序
1. 仅给 PCB/STM32 上电(不接电机和舵机)
2. `python gamepad_remote_new_pcb.py --port COM9 --cmd PING` → 应返回 `OK:PONG`
3. 用示波器测舵机 PWM 信号:
- `S1:0` → PB11 约 0.5ms 高电平
- `S1:90` → PB11 约 1.5ms 高电平
- `S1:180` → PB11 约 2.5ms 高电平
4. 测直线电机控制引脚:
- `M:OPEN` → PB12=高, PB13=低
- `M:CLOSE` → PB12=低, PB13=高
- `M:STOP` → PB12=低, PB13=低
5. 信号确认正确后,接舵机独立 5V 电源
6. 最后接直线电机电源和负载
---
## 11. 硬件接线
### 11.1 传感器接线CH341 USB-I2C
传感器通过 I2C 转接板连接,转接板通过 CH341 芯片与 PC 的 USB 连接。
I2C 地址与转接板接口位置为固定对应关系,详见 `传感器iic地址和转接板位置定义.png`
### 11.2 执行器接线USB-TTL + STM32
```
USB-TTL STM32F103C8T6
3.3V → 3.3V
GND → GND
TXD → PA10 (RXD)
RXD → PA9 (TXD)
STM32 外设
PB12 → DRV8870 IN1
PB13 → DRV8870 IN2
PB11 → 舵机1 信号线 (A0绿)
PB10 → 舵机2 信号线 (A1橙)
GND → 舵机 GND
GND → 直线电机 GND
```
详见 `接线说明.txt`
### 11.3 供电注意事项
- MG996R 舵机不可由 STM32 板载 5V 供电,需使用独立 5V 3A 以上电源
- 舵机电源 GND、直线电机电源 GND、STM32 GND 必须共地
- DRV8870 的 VM 电压须匹配直线电机额定电压
- 直线电机首次测试建议只点动 0.5s,防止方向相反或机械顶死
- 如果 M:OPEN 和 M:CLOSE 方向相反,可在固件中交换 PB12/PB13 输出逻辑,或交换电机两线
---
## 12. 常见问题
### Q: 运行 cap_read.py 提示 "ch341加载失败"
A: 检查 `lib/ch341/` 目录下是否有对应平台的库文件:
- Windows: `CH341DLLA64.DLL`
- Linux: `libch347.so`
并确保已安装 CH341 驱动程序。
### Q: 传感器无法连接?
A:
1. 确认传感器已上电 3 秒以上再运行程序
2. 检查 I2C 地址是否与转接板接口匹配
3. 运行后查看终端输出,程序会自动尝试匹配地址
### Q: 手柄遥控无法识别手柄?
A:
1. 确保手柄已通过蓝牙/USB 连接并被系统识别
2. Windows 下可在"控制面板→设备和打印机"中验证
3. 确保安装了 pygame 库
### Q: 串口命令无响应?
A:
1. 确认 COM 口号正确Windows 在设备管理器中查看)
2. 确认波特率为 115200
3.`--cmd PING` 测试基本通信
4. 检查 USB-TTL 的 TXD/RXD 是否交叉连接
### Q: 如何修改采样频率?
A: 修改对应文件中的配置参数:
- `cap_read.py`: `DEF_GET_CAP_MS`
- `tactile_sensor_daq.py`: `SAMPLE_RATE_MS`
### Q: 如何支持更多传感器型号?
A: 在 `sensorPara.py``finger_params` 列表中追加 `FingerParamTS` 参数定义。传感器会自动根据项目编号匹配对应参数。
---
## 项目文件结构
```
HandControl/
├── class_ch341.py # CH341 I2C 底层驱动
├── class_sensorcmd.py # 传感器 I2C 命令协议
├── class_finger.py # 传感器数据解析
├── sensorPara.py # 传感器参数定义
├── cap_read.py # 基础电容读取入口
├── tactile_sensor_daq.py # 多指触觉传感器 DAQ
├── serial_robot_driver.py # 串口执行器驱动
├── gamepad_remote.py # 手柄遥控
├── gamepad_remote_new_pcb.py # 新 PCB 手柄遥控
├── keyboard_remote_new_pcb.py # 新 PCB 键盘遥控
├── demo_auto_grasp.py # 演示:自动抓取任务
├── test_dof_control.py # 自由度独立控制测试
├── data_logger.py # CSV 数据记录器
├── grasp_network_model.py # 抓取预测神经网络
├── stm32f103_drv8870_servo_main.c # STM32 固件
├── lib/ch341/ # CH341 驱动库
│ ├── CH341DLLA64.DLL
│ ├── CH341DLLA64.LIB
│ ├── ch341_lib.h
│ └── libch347.so
├── 接线说明.txt # 硬件接线参考
├── 新PCB控制实施方案.md # 控制系统实施方案
├── 传感器iic地址和转接板位置定义.png # I2C 地址图
└── README.md # 本文件
```

View File

@ -8,10 +8,13 @@ from collections import namedtuple
import socket
DEF_CDC_SYNC_MS = 1000 #电容同步间隔
DEF_GET_CAP_MS = (30) #读取电容间隔
DEF_PRO_CYC = 100
DEF_MAX_FINGER_NUM = 1 #需要连接的手指数量最大5个
# 定义一个全局的队列,用于线程间通信
capReadQueue = queue.Queue()
@ -30,7 +33,7 @@ class ClassCapRead:
# 最大连接5个手指
self.fingers = list() # 传感器列表
for i in range(2):
for i in range(DEF_MAX_FINGER_NUM):
self.fingers.append(ClassFinger(2+i, self.ch341))
self.currCh341State = 0 # 当前ch341连接状态
@ -45,7 +48,7 @@ class ClassCapRead:
self.syncTimer = 0
self.connectStatus = EnumCh341ConnectStatus.CH341_CONNECT_INIT
self.running = True # <-- 新增这一行
self.connectDebug()
def __del__(self):
@ -164,26 +167,36 @@ class ClassCapRead:
if connectedSensorCnt > 1 and (time.time() - self.syncTimer) > DEF_CDC_SYNC_MS:
self.syncTimer = time.time()
self.set_sensor_enable(connectedSensorChan)
for fingerIndex in range(0, len(self.fingers)):
if self.fingers[fingerIndex].connect is True:
self.fingers[fingerIndex].snsCmd.setSensorSync(0)
break
if self.connectStatus == EnumCh341ConnectStatus.CH341_CONNECT_CHECK:
capReadTime = time.time()
difftime = int(capReadTime*1000-ms_capReadTime*1000)
#print(f"diffTime={difftime}")
#定时器在任务完成后重新启动
if(difftime>DEF_GET_CAP_MS):
timer = threading.Timer(DEF_GET_CAP_MS/1000, self.capRead)
else:
timer = threading.Timer((DEF_GET_CAP_MS-difftime)/1000, self.capRead)
if self.running: # 新增判断
if(difftime>DEF_GET_CAP_MS):
timer = threading.Timer(DEF_GET_CAP_MS/1000, self.capRead)
else:
timer = threading.Timer((DEF_GET_CAP_MS-difftime)/1000, self.capRead)
timer.start()
def capReadThread():
# 线程的主体功能
cap = ClassCapRead()
while True:
cap.ch341Connect()
time.sleep(DEF_PRO_CYC/1000)
try:
while True:
cap.ch341Connect()
time.sleep(DEF_PRO_CYC/1000)
except KeyboardInterrupt:
print("\nStopping sensor read...")
cap.running = False
cap.disConnectDebug()
cap.ch341.disconnect()
print("CH341 released.")
def main():
capReadThread()

View File

@ -33,12 +33,12 @@ class ClassCh341:
def init(self):
if os.name == 'nt': # Windows 环境
libPath = os.path.dirname(sys.argv[0]) + r'/lib/ch341/CH341DLLA64.DLL'
libPath = os.path.join(os.path.dirname(__file__), 'lib/ch341/CH341DLLA64.DLL')
elif os.name == 'posix':
libPath = './lib/ch341/libch347.so'
libPath = os.path.join(os.path.dirname(__file__), 'lib/ch341/libch347.so')
dllExist = os.path.exists(libPath)
if not dllExist:
print('未找到库文件')
print('未找到库文件:', libPath)
return False
else:
try:

View File

@ -73,8 +73,12 @@ class ClassFinger:
print(f"finger connected, project = {self.projectPara.name}")
findProjectFlg = True
if findProjectFlg == False:
print("not found vailed project, project para use default")
if not findProjectFlg:
raise RuntimeError(
f"Unknown sensor project ID: {projectRead}. "
f"Expected one of: {[p.prg for p in finger_params]}. "
"Check sensorPara.py or update sensor firmware."
)
self.connected(addrRead)
@ -146,7 +150,7 @@ class ClassFinger:
for i in range(self.projectPara.ydds_num):
offset = yddsOffset + i * struct_size
struct_data = self.data[offset : offset + struct_size]
print(f"struct={struct_data}")
# print(f"struct={struct_data}")
struct_data = [value & 0xFF for value in struct_data]
struct_data = bytes(struct_data) # 转换为 bytes 类型
instance = DynamicYddsU16Ts.from_buffer_copy(struct_data)
@ -165,15 +169,15 @@ class ClassFinger:
((self.data[mProxOffset + i*self.projectPara.cap_byte + 2] & 0xFF) << 16))
print(f"capChannelDat={self.readData.channelCapData}")
for i in range(self.projectPara.ydds_num):
print(f"nf[{i}] = {self.readData.nf[i]}")
print(f"tf[{i}] = {self.readData.tf[i]}")
print(f"tfDir[{i}] = {self.readData.tfDir[i]}")
for i in range(self.projectPara.s_prox_num):
print(f"sProxCapData[{i}] = {self.readData.sProxCapData[i]}")
for i in range(self.projectPara.m_prox_num):
print(f"sProxCapData[{i}] = {self.readData.mProxCapData[i]}")
# print(f"capChannelDat={self.readData.channelCapData}")
# for i in range(self.projectPara.ydds_num):
# print(f"nf[{i}] = {self.readData.nf[i]}")
# print(f"tf[{i}] = {self.readData.tf[i]}")
# print(f"tfDir[{i}] = {self.readData.tfDir[i]}")
# for i in range(self.projectPara.s_prox_num):
# print(f"sProxCapData[{i}] = {self.readData.sProxCapData[i]}")
# for i in range(self.projectPara.m_prox_num):
# print(f"mProxCapData[{i}] = {self.readData.mProxCapData[i]}")
break
else:

View File

@ -250,6 +250,24 @@ class ClassSensorCmd:
#print(f"time={time.time()},len(buf) = {len(buf)}, tarLen={tarLen}, buf[0]={buf[0]}, buf[1]={buf[1]}, sum={checkSum}")
pass
return False
# 设置采集同步
# addr传感器地址
def setSensorSync(self, addr):
_pack = list()
_pack.append(0xAA)
_pack.append(0x55)
_pack.append(0x03)
_pack.append(self.CMD_SET_SENSOR_CDC_SYNC)
_pack.append(0x00)
_pack.append(0x00)
_pack.append(0x00)
_pack.append(0x00)
_pack.append(0x00)
self.calcSum(_pack)
self._ch341.write(addr, _pack)
return True

View File

@ -0,0 +1,6 @@
Timestamp,Config_ID,F0_U1_Fn,F0_U1_Ft,F0_U2_Fn,F0_U2_Ft,F1_U1_Fn,F1_U1_Ft,F1_U2_Fn,F1_U2_Ft,F2_U1_Fn,F2_U1_Ft,F2_U2_Fn,F2_U2_Ft,Label_X,Label_Y,Label_Theta
1767594769.482,0,9.0100,6.0420,2.1886,9.8836,5.2738,2.1249,0.3352,0.1568,1.8874,0.5868,8.7702,2.0818,0.0000,5.5000,-2.0000
1767594769.584,0,9.6023,3.9588,8.0666,1.7781,4.3554,9.5767,8.4885,8.8382,4.7088,8.0218,6.9714,7.0751,0.0000,5.5000,-2.0000
1767594769.695,0,1.6011,6.0033,5.8600,3.1327,7.4821,8.2374,4.9193,1.8624,7.4288,0.3863,1.3976,4.5740,0.0000,5.5000,-2.0000
1767594769.807,0,2.0589,0.7437,9.3519,5.0238,1.3955,8.3442,6.9266,4.7168,9.9629,7.4520,0.8254,0.9562,0.0000,5.5000,-2.0000
1767594769.917,0,9.9747,0.3043,7.5153,8.5071,8.6996,6.9610,4.1218,9.2278,3.4305,6.7590,3.0049,2.0914,0.0000,5.5000,-2.0000
1 Timestamp Config_ID F0_U1_Fn F0_U1_Ft F0_U2_Fn F0_U2_Ft F1_U1_Fn F1_U1_Ft F1_U2_Fn F1_U2_Ft F2_U1_Fn F2_U1_Ft F2_U2_Fn F2_U2_Ft Label_X Label_Y Label_Theta
2 1767594769.482 0 9.0100 6.0420 2.1886 9.8836 5.2738 2.1249 0.3352 0.1568 1.8874 0.5868 8.7702 2.0818 0.0000 5.5000 -2.0000
3 1767594769.584 0 9.6023 3.9588 8.0666 1.7781 4.3554 9.5767 8.4885 8.8382 4.7088 8.0218 6.9714 7.0751 0.0000 5.5000 -2.0000
4 1767594769.695 0 1.6011 6.0033 5.8600 3.1327 7.4821 8.2374 4.9193 1.8624 7.4288 0.3863 1.3976 4.5740 0.0000 5.5000 -2.0000
5 1767594769.807 0 2.0589 0.7437 9.3519 5.0238 1.3955 8.3442 6.9266 4.7168 9.9629 7.4520 0.8254 0.9562 0.0000 5.5000 -2.0000
6 1767594769.917 0 9.9747 0.3043 7.5153 8.5071 8.6996 6.9610 4.1218 9.2278 3.4305 6.7590 3.0049 2.0914 0.0000 5.5000 -2.0000

View File

@ -0,0 +1,6 @@
Timestamp,Config_ID,F0_U1_Fn,F0_U1_Ft,F0_U2_Fn,F0_U2_Ft,F1_U1_Fn,F1_U1_Ft,F1_U2_Fn,F1_U2_Ft,F2_U1_Fn,F2_U1_Ft,F2_U2_Fn,F2_U2_Ft,Label_X,Label_Y,Label_Theta
1767756955.615,0,6.2132,4.2937,7.6947,7.2926,9.1351,3.1797,4.4744,1.1258,5.3945,4.4559,1.4513,0.9027,0.0000,5.5000,-2.0000
1767756955.728,0,2.3862,2.5899,5.4478,6.0398,9.1296,9.9555,6.3723,9.2897,5.3715,0.8756,1.0380,8.8672,0.0000,5.5000,-2.0000
1767756955.838,0,2.7292,5.8074,1.9451,3.0233,2.6187,3.7548,3.0552,3.0520,2.9982,7.2089,5.1214,4.1373,0.0000,5.5000,-2.0000
1767756955.950,0,3.0360,9.9848,2.0741,7.0199,2.6957,4.1377,7.2276,3.1451,6.1223,5.8633,0.6268,7.4781,0.0000,5.5000,-2.0000
1767756956.059,0,8.6828,6.3578,7.1173,9.1788,6.5483,0.5439,7.1334,8.2240,0.8368,9.2188,4.8974,1.0275,0.0000,5.5000,-2.0000
1 Timestamp Config_ID F0_U1_Fn F0_U1_Ft F0_U2_Fn F0_U2_Ft F1_U1_Fn F1_U1_Ft F1_U2_Fn F1_U2_Ft F2_U1_Fn F2_U1_Ft F2_U2_Fn F2_U2_Ft Label_X Label_Y Label_Theta
2 1767756955.615 0 6.2132 4.2937 7.6947 7.2926 9.1351 3.1797 4.4744 1.1258 5.3945 4.4559 1.4513 0.9027 0.0000 5.5000 -2.0000
3 1767756955.728 0 2.3862 2.5899 5.4478 6.0398 9.1296 9.9555 6.3723 9.2897 5.3715 0.8756 1.0380 8.8672 0.0000 5.5000 -2.0000
4 1767756955.838 0 2.7292 5.8074 1.9451 3.0233 2.6187 3.7548 3.0552 3.0520 2.9982 7.2089 5.1214 4.1373 0.0000 5.5000 -2.0000
5 1767756955.950 0 3.0360 9.9848 2.0741 7.0199 2.6957 4.1377 7.2276 3.1451 6.1223 5.8633 0.6268 7.4781 0.0000 5.5000 -2.0000
6 1767756956.059 0 8.6828 6.3578 7.1173 9.1788 6.5483 0.5439 7.1334 8.2240 0.8368 9.2188 4.8974 1.0275 0.0000 5.5000 -2.0000

85
data_logger.py Normal file
View File

@ -0,0 +1,85 @@
import csv
import time
import os
import numpy as np
class DataLogger:
def __init__(self, filename_prefix="dataset"):
"""
初始化日志记录器
:param filename_prefix: 文件名前缀,例如 "train_batch"
"""
# 1. 自动创建 data 文件夹,保持项目目录整洁
if not os.path.exists('data'):
os.makedirs('data')
print("[Logger] Created 'data' directory.")
# 2. 生成带时间戳的文件名,防止覆盖旧数据
# 格式: data/dataset_20240105_143022.csv
timestamp = time.strftime("%Y%m%d_%H%M%S")
self.filename = f"data/{filename_prefix}_{timestamp}.csv"
# 3. 定义标准表头 (Header) - 对应你的12个力 + 3个构型 + 3个标签
# 传感器命名规则: F(手指ID)_U(单元ID)_(Fn/Ft)
sensor_headers = []
for f in range(3): # 3个手指
for u in range(1, 3): # 2个单元
sensor_headers.append(f"F{f}_U{u}_Fn")
sensor_headers.append(f"F{f}_U{u}_Ft")
# 完整的列名列表
self.header = ['Timestamp', 'Config_ID'] + sensor_headers + ['Label_X', 'Label_Y', 'Label_Theta']
# 4. 创建文件并写入第一行(表头)
try:
with open(self.filename, mode='w', newline='') as f:
writer = csv.writer(f)
writer.writerow(self.header)
print(f"[Logger] Log file initialized: {self.filename}")
except Exception as e:
print(f"[Logger] Error creating file: {e}")
def log(self, sensor_data, config_id, label_vector):
"""
写入一行数据
:param sensor_data: 12维 numpy 数组或列表 (触觉数据)
:param config_id: 整数 (0, 1, 2) (当前手指构型)
:param label_vector: [x, y, theta] (机械臂的真实偏差值)
"""
with open(self.filename, mode='a', newline='') as f:
writer = csv.writer(f)
# 拼接数据: 时间 + ID + 传感器数值 + 标签
# time.time() 获取当前精确时间戳
row = [f"{time.time():.3f}", int(config_id)] + list(sensor_data) + list(label_vector)
# 格式化: 将浮点数保留4位小数节省空间且美观
formatted_row = []
for item in row:
if isinstance(item, float):
formatted_row.append(f"{item:.4f}")
else:
formatted_row.append(item)
writer.writerow(formatted_row)
# === 单元测试 (Unit Test) ===
# 直接运行此文件,测试是否能生成 CSV
if __name__ == "__main__":
print("Testing DataLogger...")
logger = DataLogger(filename_prefix="test_data")
# 模拟写入 5 条假数据
for i in range(5):
# 模拟 12 个传感器数据 (0-10之间)
fake_sensor = np.random.rand(12) * 10
# 模拟机械臂偏差 (Label)
fake_label = [0.0, 5.5, -2.0]
logger.log(sensor_data=fake_sensor, config_id=0, label_vector=fake_label)
print(f"Logged row {i + 1}")
time.sleep(0.1)
print("Test done. Please check the 'data' folder.")

332
demo_auto_grasp.py Normal file
View File

@ -0,0 +1,332 @@
import threading
import time
import serial
import numpy as np
import keyboard # 需要 pip install keyboard
from robot_driver import RobotDriver
from enum import Enum
# === 官方驱动引用 (请确保这3个文件在同级目录) ===
from class_ch341 import *
from class_sensorcmd import *
from class_finger import *
# ==========================================
# PART 1: 传感器驱动 (完整版 TactileSensorDAQ)
# ==========================================
DEF_MAX_FINGER_NUM = 3
PCA_ADDR = 0x70
SAMPLE_RATE_MS = 10
class EnumCh341ConnectStatus(Enum):
CH341_CONNECT_INIT = 0
CH341_CONNECT_OPEN = 1
CH341_CONNECT_SET_SPEED = 2
CH341_CONNECT_SAMPLE_START = 3
CH341_CONNECT_CHECK = 4
class TactileSensorDAQ:
def __init__(self):
# 1. 硬件初始化
self.ch341 = ClassCh341()
self.fingers = list()
# 初始化3个传感器对象
for i in range(DEF_MAX_FINGER_NUM):
self.fingers.append(ClassFinger(4 + i, self.ch341))
# 2. 状态机变量
self.connectStatus = EnumCh341ConnectStatus.CH341_CONNECT_INIT
self.ch341CheckTimer = 0
self.pcaAddr = PCA_ADDR
self.syncTimer = 0
# 3. 数据容器
self.raw_data = np.zeros(12, dtype=np.float32)
self.offset = np.zeros(12, dtype=np.float32)
self.clean_data = np.zeros(12, dtype=np.float32)
# 4. 线程控制
self.running = False
self.lock = threading.Lock()
self.thread = None
def _set_sensor_enable(self, idx):
_pack = list()
_pack.append(idx)
self.ch341.write(self.pcaAddr, _pack)
def _update_state_machine(self):
if self.connectStatus == EnumCh341ConnectStatus.CH341_CONNECT_INIT:
if self.ch341.init(): self.connectStatus = EnumCh341ConnectStatus.CH341_CONNECT_OPEN
elif self.connectStatus == EnumCh341ConnectStatus.CH341_CONNECT_OPEN:
if self.ch341.open():
self.connectStatus = EnumCh341ConnectStatus.CH341_CONNECT_SET_SPEED
else:
self.connectStatus = EnumCh341ConnectStatus.CH341_CONNECT_INIT
elif self.connectStatus == EnumCh341ConnectStatus.CH341_CONNECT_SET_SPEED:
if self.ch341.set_speed(self.ch341.IIC_SPEED_400):
self.connectStatus = EnumCh341ConnectStatus.CH341_CONNECT_SAMPLE_START
else:
self.connectStatus = EnumCh341ConnectStatus.CH341_CONNECT_SAMPLE_START
elif self.connectStatus == EnumCh341ConnectStatus.CH341_CONNECT_SAMPLE_START:
self.connectStatus = EnumCh341ConnectStatus.CH341_CONNECT_CHECK
def _read_hardware(self):
connectedSensorChan = 0
temp_data_buffer = []
for fingerIndex in range(len(self.fingers)):
self._set_sensor_enable(1 << (self.fingers[fingerIndex].pcaIdx))
connectedSensorChan |= 1 << (self.fingers[fingerIndex].pcaIdx)
current_finger = self.fingers[fingerIndex]
if not current_finger.connect:
if current_finger.checkSensor():
print(f"[Sensor] Finger {fingerIndex} Connected!")
else:
current_finger.capRead()
for unit_i in range(current_finger.projectPara.ydds_num):
fn = current_finger.readData.nf[unit_i]
ft = current_finger.readData.tf[unit_i]
temp_data_buffer.append(fn)
temp_data_buffer.append(ft)
if len(temp_data_buffer) == 12:
with self.lock:
self.raw_data = np.array(temp_data_buffer, dtype=np.float32)
self.clean_data = self.raw_data - self.offset
if (time.time() - self.syncTimer) > 1.0:
self.syncTimer = time.time()
self._set_sensor_enable(connectedSensorChan)
for f in self.fingers:
if f.connect:
f.snsCmd.setSensorSync(0)
break
def _thread_worker(self):
while self.running:
if self.connectStatus != EnumCh341ConnectStatus.CH341_CONNECT_CHECK:
self._update_state_machine()
time.sleep(0.1)
continue
start_time = time.time()
try:
self._read_hardware()
except Exception as e:
print(f"Read Error: {e}")
self.ch341CheckTimer += (time.time() - start_time) * 1000
if self.ch341CheckTimer >= 1000:
self.ch341CheckTimer = 0
if not self.ch341.connectCheck():
print("[Sensor] CH341 Disconnected!")
self.connectStatus = EnumCh341ConnectStatus.CH341_CONNECT_INIT
elapsed = (time.time() - start_time) * 1000
sleep_time = (SAMPLE_RATE_MS - elapsed) / 1000.0
if sleep_time > 0: time.sleep(sleep_time)
def start(self):
if self.running: return
self.running = True
self.thread = threading.Thread(target=self._thread_worker)
self.thread.daemon = True
self.thread.start()
print("[System] Sensor Thread Started.")
def stop(self):
self.running = False
if self.thread: self.thread.join()
self.ch341.disconnect()
print("[System] Sensor Thread Stopped.")
def tare(self):
print("[System] Taring sensors... please wait.")
time.sleep(1)
with self.lock:
self.offset = np.copy(self.raw_data)
print("[System] Tare complete.")
def get_max_force(self):
with self.lock:
data = np.copy(self.clean_data)
max_force = 0.0
for i in range(0, 12, 2):
if data[i] > max_force: max_force = data[i]
return max_force
# ==========================================
# PART 3: 全局急停逻辑 (SafetyGuard)
# ==========================================
class SafetyGuard:
def __init__(self, robot):
self.robot = robot
self.is_paused = False
keyboard.add_hotkey('space', self.toggle_safety)
def toggle_safety(self):
self.is_paused = not self.is_paused
if self.is_paused:
self.robot.motor_stop()
print("\n\n" + "!" * 40)
print("!!! 紧急停止触发 (EMERGENCY STOP) !!!")
print("!!! 电机已锁死,程序挂起 !!!")
print("!!! 再按一次 [空格] 恢复运行 !!!")
print("!" * 40 + "\n")
else:
print("\n" + "=" * 40)
print(">>> 解除急停,继续任务 (Resuming)...")
print("=" * 40 + "\n")
def check_pause(self):
if not self.is_paused: return None
while self.is_paused: time.sleep(0.1)
time.sleep(0.5)
return True
# ==========================================
# PART 4: 主逻辑 (Main Controller)
# ==========================================
def run_auto_grasp_task(robot, sensor, safety):
print("\n>>> 任务开始 <<<")
# 1. 构型输入
try:
mode_str = input("请输入目标构型 (0:初始, 1:错位, 2:对握): ").strip()
mode = int(mode_str)
if mode not in [0, 1, 2]: raise ValueError
except:
print("[Error] 无效输入")
return
# 2. 变构型
safety.check_pause()
robot.set_config(mode)
# === 限制任务开始时的张开等待,避免过度张开 ===
OPEN_WAIT_TIME = 1.0 # 秒
safety.check_pause()
print(f"[Task] 初始化:直线电机张开... (等待 {OPEN_WAIT_TIME} 秒)")
robot.motor_open()
# 3. 延时 (带倒计时的充分等待)
steps = int(OPEN_WAIT_TIME * 10) # 转换为 0.1s 的步数
print(f"[Task] 正在张开并等待放置番茄 (按空格可急停)...")
for i in range(steps):
if safety.check_pause():
print("[Task] 暂停恢复,重置倒计时...")
time.sleep(0.1)
# 每秒打印一次倒计时
seconds_left = OPEN_WAIT_TIME - (i / 10)
if i % 10 == 0:
print(f"{int(seconds_left)}...", end=' ', flush=True)
print("Go!")
# ===============================================
# 4. 开始闭合
safety.check_pause()
print("[Task] 直线电机开始闭合...")
robot.motor_close()
# 5. 力反馈循环
# ================= 修改位置:力控阈值 =================
FORCE_THRESHOLD = 0.3
# ===================================================
start_time = time.time()
try:
while True:
# 急停恢复检查
recover_flag = safety.check_pause()
if recover_flag:
print("[Task] 恢复运动:重新下发闭合指令...")
robot.motor_close()
start_time = time.time()
# 获取传感器数据
current_max_force = sensor.get_max_force()
# 打印状态
if (time.time() * 1000) % 200 < 20:
print(f"\r[Grasping] Force: {current_max_force:.1f} / {FORCE_THRESHOLD}", end="")
# 触发判断
if current_max_force > FORCE_THRESHOLD:
print(f"\n[Task] 触觉触发!停止。")
break
# 超时保护 (15秒防止闭合行程也很长)
if time.time() - start_time > 15.0:
print("\n[Task] 抓取超时 (未检测到受力)。")
break
time.sleep(0.01)
except KeyboardInterrupt:
print("\n[Task] 人工强制中断!")
# 6. 停止
robot.motor_stop()
print("[Task] 任务结束。\n")
if __name__ == "__main__":
sensor = TactileSensorDAQ()
sensor.start()
# ================= 修改位置:串口号 =================
# 请务必确认这里是 STM32 的串口号,而不是传感器的
robot = RobotDriver(port='COM9')
# ==================================================
safety = SafetyGuard(robot)
try:
print("等待传感器稳定...")
time.sleep(2)
sensor.tare()
while True:
print("========================")
print(" [Enter] 运行抓取任务")
print(" [Space] 随时急停/恢复")
print(" [t] 重新去皮")
print(" [q] 退出")
cmd = input("Command > ").strip().lower()
if cmd == '':
run_auto_grasp_task(robot, sensor, safety)
elif cmd == 't':
sensor.tare()
elif cmd == 'q':
break
except KeyboardInterrupt:
pass
finally:
robot.close()
sensor.stop()
try:
keyboard.unhook_all()
except:
pass
print("System All Shutdown.")

173
gamepad_remote.py Normal file
View File

@ -0,0 +1,173 @@
import time
import serial
import pygame
from robot_driver import RobotDriver
class GamepadRemoteController:
# 默认按键映射Xbox 常见布局)
# A/B/X 控制构型 0/1/2
# LB 打开RB 闭合
# START 退出
BTN_A = 0
BTN_B = 1
BTN_X = 2
BTN_LB = 4
BTN_RB = 5
BTN_BACK = 6
BTN_START = 7
def __init__(self, robot):
self.robot = robot
self.joystick = None
self.estop_active = False
self.last_open_pressed = False
self.last_close_pressed = False
self.last_mode_buttons = {
self.BTN_A: False,
self.BTN_B: False,
self.BTN_X: False,
self.BTN_BACK: False,
}
def init_gamepad(self):
pygame.init()
pygame.joystick.init()
if pygame.joystick.get_count() < 1:
raise RuntimeError("No gamepad detected. Please connect controller via Bluetooth first.")
self.joystick = pygame.joystick.Joystick(0)
self.joystick.init()
print(f"[System] Gamepad connected: {self.joystick.get_name()}")
def _edge_pressed(self, btn_id, cache_dict):
now_pressed = bool(self.joystick.get_button(btn_id))
prev_pressed = cache_dict.get(btn_id, False)
cache_dict[btn_id] = now_pressed
return now_pressed and (not prev_pressed)
def _handle_mode_buttons(self):
if self._edge_pressed(self.BTN_A, self.last_mode_buttons):
self.robot.set_config(0)
print("[Action] Config 0")
if self._edge_pressed(self.BTN_B, self.last_mode_buttons):
self.robot.set_config(1)
print("[Action] Config 1")
if self._edge_pressed(self.BTN_X, self.last_mode_buttons):
self.robot.set_config(2)
print("[Action] Config 2")
def _handle_estop_toggle(self):
if not self._edge_pressed(self.BTN_BACK, self.last_mode_buttons):
return
self.estop_active = not self.estop_active
self.robot.motor_stop()
if self.estop_active:
print("\n" + "!" * 40)
print("!!! EMERGENCY STOP ACTIVE !!!")
print("!!! Motion locked. Press BACK again to resume.")
print("!" * 40)
else:
print("\n" + "=" * 40)
print(">>> EMERGENCY STOP RELEASED")
print("= " * 20)
def _handle_open_close_hold(self):
# 按住 LB 持续张开,按住 RB 持续闭合;松开则停止
open_pressed = bool(self.joystick.get_button(self.BTN_LB))
close_pressed = bool(self.joystick.get_button(self.BTN_RB))
if open_pressed and not close_pressed:
if not self.last_open_pressed or self.last_close_pressed:
self.robot.motor_open()
print("[Action] Palm OPEN")
elif close_pressed and not open_pressed:
if not self.last_close_pressed or self.last_open_pressed:
self.robot.motor_close()
print("[Action] Palm CLOSE")
else:
if self.last_open_pressed or self.last_close_pressed:
self.robot.motor_stop()
print("[Action] Palm STOP")
self.last_open_pressed = open_pressed
self.last_close_pressed = close_pressed
def loop(self):
print("\n=== Gamepad Remote Started ===")
print("A -> Config 0")
print("B -> Config 1")
print("X -> Config 2")
print("LB hold -> Palm OPEN")
print("RB hold -> Palm CLOSE")
print("Release LB/RB -> STOP")
print("BACK -> Global E-STOP toggle")
print("START -> Exit")
running = True
while running:
pygame.event.pump()
self._handle_estop_toggle()
if self.joystick.get_button(self.BTN_START):
print("[System] Exit requested by START")
running = False
continue
if self.estop_active:
# 急停锁定期间屏蔽所有动作命令
self.last_open_pressed = bool(self.joystick.get_button(self.BTN_LB))
self.last_close_pressed = bool(self.joystick.get_button(self.BTN_RB))
time.sleep(0.02)
continue
self._handle_mode_buttons()
self._handle_open_close_hold()
time.sleep(0.02)
self.robot.motor_stop()
def shutdown(self):
try:
if self.joystick is not None:
self.joystick.quit()
finally:
pygame.joystick.quit()
pygame.quit()
def main():
# 根据实际串口修改
port = "COM9"
baud = 115200
robot = RobotDriver(port=port, baud=baud)
if robot.ser is None or not robot.ser.is_open:
return
controller = GamepadRemoteController(robot)
try:
controller.init_gamepad()
controller.loop()
except KeyboardInterrupt:
print("\n[System] Keyboard interrupt")
except Exception as e:
print(f"[Error] {e}")
finally:
controller.shutdown()
robot.close()
print("[System] Shutdown complete.")
if __name__ == "__main__":
main()

188
gamepad_remote_new_pcb.py Normal file
View File

@ -0,0 +1,188 @@
import argparse
import sys
import time
import serial
from robot_driver import RobotDriver
class GamepadRemoteController:
BTN_A = 0
BTN_B = 1
BTN_X = 2
BTN_LB = 4
BTN_RB = 5
BTN_BACK = 6
BTN_START = 7
def __init__(self, robot):
self.robot = robot
self.joystick = None
self.estop_active = False
self.last_open_pressed = False
self.last_close_pressed = False
self.last_buttons = {
self.BTN_A: False,
self.BTN_B: False,
self.BTN_X: False,
self.BTN_BACK: False,
}
def init_gamepad(self):
global pygame
import pygame
pygame.init()
pygame.joystick.init()
if pygame.joystick.get_count() < 1:
raise RuntimeError("No gamepad detected")
self.joystick = pygame.joystick.Joystick(0)
self.joystick.init()
print(f"[System] Gamepad connected: {self.joystick.get_name()}")
def _edge_pressed(self, btn_id):
now_pressed = bool(self.joystick.get_button(btn_id))
prev_pressed = self.last_buttons.get(btn_id, False)
self.last_buttons[btn_id] = now_pressed
return now_pressed and not prev_pressed
def _handle_mode_buttons(self):
if self._edge_pressed(self.BTN_A):
self.robot.set_config(0)
print("[Action] Config 0")
if self._edge_pressed(self.BTN_B):
self.robot.set_config(1)
print("[Action] Config 1")
if self._edge_pressed(self.BTN_X):
self.robot.set_config(2)
print("[Action] Config 2")
def _handle_estop_toggle(self):
if not self._edge_pressed(self.BTN_BACK):
return
self.estop_active = not self.estop_active
self.robot.motor_stop()
print("[Action] E-STOP ON" if self.estop_active else "[Action] E-STOP OFF")
def _handle_open_close_hold(self):
open_pressed = bool(self.joystick.get_button(self.BTN_LB))
close_pressed = bool(self.joystick.get_button(self.BTN_RB))
if open_pressed and not close_pressed:
if not self.last_open_pressed or self.last_close_pressed:
self.robot.motor_open()
print("[Action] Linear motor OPEN")
elif close_pressed and not open_pressed:
if not self.last_close_pressed or self.last_open_pressed:
self.robot.motor_close()
print("[Action] Linear motor CLOSE")
else:
if self.last_open_pressed or self.last_close_pressed:
self.robot.motor_stop()
print("[Action] Linear motor STOP")
self.last_open_pressed = open_pressed
self.last_close_pressed = close_pressed
def loop(self):
print("=== Gamepad Remote Started ===")
print("A/B/X -> Config 0/1/2")
print("LB/RB hold -> Linear motor open/close")
print("BACK -> E-stop toggle, START -> Exit")
running = True
while running:
pygame.event.pump()
self._handle_estop_toggle()
if self.joystick.get_button(self.BTN_START):
running = False
continue
if self.estop_active:
self.last_open_pressed = bool(self.joystick.get_button(self.BTN_LB))
self.last_close_pressed = bool(self.joystick.get_button(self.BTN_RB))
time.sleep(0.02)
continue
self._handle_mode_buttons()
self._handle_open_close_hold()
time.sleep(0.02)
self.robot.motor_stop()
def shutdown(self):
if self.joystick is not None:
self.joystick.quit()
pygame.joystick.quit()
pygame.quit()
def run_self_test(robot):
print("[Test] PING:", robot.ping())
print("[Test] Servo 1: 90")
robot.set_servo(1, 90)
time.sleep(0.5)
print("[Test] Servo 2: 90")
robot.set_servo(2, 90)
time.sleep(0.5)
print("[Test] Linear motor open for 0.5 s")
robot.motor_open()
time.sleep(0.5)
robot.motor_stop()
time.sleep(0.3)
print("[Test] Linear motor close for 0.5 s")
robot.motor_close()
time.sleep(0.5)
robot.motor_stop()
print("[Test] Done")
def build_parser():
parser = argparse.ArgumentParser(description="Gamepad remote for new DRV8870 PCB")
parser.add_argument("--port", default="COM6")
parser.add_argument("--baud", type=int, default=115200)
parser.add_argument("--no-ack", action="store_true", help="Do not wait for MCU ACK")
parser.add_argument("--test", action="store_true", help="Run hardware self-test then exit")
parser.add_argument("--cmd", help="Send one raw command, for example PING or S1:90")
return parser
def main():
args = build_parser().parse_args()
robot = None
controller = None
try:
robot = RobotDriver(port=args.port, baud=args.baud, ack=not args.no_ack)
if args.cmd:
print(robot._send(args.cmd))
time.sleep(3) # 保持 3 秒,让电机持续运行
return
if args.test:
run_self_test(robot)
return
controller = GamepadRemoteController(robot)
controller.init_gamepad()
controller.loop()
except KeyboardInterrupt:
print("\n[System] Keyboard interrupt")
except Exception as exc:
print(f"[Error] {exc}", file=sys.stderr)
raise
finally:
if controller is not None:
controller.shutdown()
if robot is not None:
robot.close()
print("[System] Shutdown complete")
if __name__ == "__main__":
main()

113
grasp_network_model.py Normal file
View File

@ -0,0 +1,113 @@
import torch
import torch.nn as nn
import torch.nn.functional as F
class CondGraspNet(nn.Module):
def __init__(self):
super(CondGraspNet, self).__init__()
# === 1. 定义输入维度 ===
# 触觉特征: 12维 (3指 * 2单元 * 2分量)
self.tactile_dim = 12
# 构型特征: 3维 (One-Hot编码: [1,0,0], [0,1,0], [0,0,1])
self.config_dim = 3
input_total_dim = self.tactile_dim + self.config_dim # 15维
# === 2. 定义网络层 (MLP结构) ===
# Layer 1: 特征融合层
# 将触觉信息和构型信息混合
self.fc1 = nn.Linear(input_total_dim, 64)
self.bn1 = nn.BatchNorm1d(64) # 批归一化: 防止梯度消失,加速训练
# Layer 2: 非线性映射层
# 增加网络宽度,拟合复杂的力学关系
self.fc2 = nn.Linear(64, 128)
self.bn2 = nn.BatchNorm1d(128)
# Layer 3: 特征压缩层
self.fc3 = nn.Linear(128, 64)
# Layer 4: 输出层 (Regression Head)
# 输出3个值: [Delta_X, Delta_Y, Delta_Theta]
self.output = nn.Linear(64, 3)
# === 3. 权重初始化 (Xavier) ===
# 这一步对小数据集训练非常重要,能让模型收敛得更快
self._init_weights()
def _init_weights(self):
for m in self.modules():
if isinstance(m, nn.Linear):
nn.init.xavier_uniform_(m.weight)
if m.bias is not None:
nn.init.constant_(m.bias, 0)
def forward(self, tactile_data, config_id_idx):
"""
前向传播函数
:param tactile_data: [Batch_Size, 12] 的触觉数据张量
:param config_id_idx: [Batch_Size] 的构型索引 (例如 [0, 2, 1...])
:return: [Batch_Size, 3] 的预测偏差
"""
# Step 1: 处理构型 ID (One-Hot Encoding)
# 必须把整数 ID (0,1,2) 变成向量 ([1,0,0]...) 才能喂给神经网络
batch_size = tactile_data.size(0)
# 创建一个全0的容器
config_one_hot = torch.zeros(batch_size, self.config_dim).to(tactile_data.device)
# 使用 scatter_ 方法进行填充
# config_id_idx 需要升维: [Batch] -> [Batch, 1]
config_one_hot.scatter_(1, config_id_idx.unsqueeze(1).long(), 1)
# Step 2: 特征拼接 (Concatenate)
# 将触觉数据和构型向量拼在一起 -> [Batch, 15]
x = torch.cat((tactile_data, config_one_hot), dim=1)
# Step 3: 通过隐藏层
x = F.relu(self.bn1(self.fc1(x))) # Linear -> BN -> ReLU
x = F.relu(self.bn2(self.fc2(x))) # Linear -> BN -> ReLU
x = F.relu(self.fc3(x)) # Linear -> ReLU (最后一层通常不用BN)
# Step 4: 输出结果
prediction = self.output(x)
return prediction
# === 单元测试 (Unit Test) ===
# 运行此文件,检查网络结构和输入输出形状是否正确
if __name__ == "__main__":
print("Testing CondGraspNet Model...")
# 1. 实例化模型
model = CondGraspNet()
print(f"Model Structure:\n{model}")
# 2. 创建模拟输入数据 (Batch Size = 8)
# 模拟8条触觉数据 (随机数)
fake_tactile = torch.randn(8, 12)
# 模拟8个构型ID (随机 0, 1, 2)
fake_config = torch.tensor([0, 0, 1, 1, 2, 2, 0, 2], dtype=torch.long)
# 3. 前向推理
print("\nProcessing forward pass...")
try:
output = model(fake_tactile, fake_config)
# 4. 验证结果
print("Input Shape (Tactile):", fake_tactile.shape)
print("Output Shape (Pred): ", output.shape) # 期望是 [8, 3]
print("\nSample Prediction (Row 0):")
print(f"Delta X: {output[0][0].item():.4f} mm")
print(f"Delta Y: {output[0][1].item():.4f} mm")
print(f"Delta θ: {output[0][2].item():.4f} deg")
if output.shape == (8, 3):
print("\n✅ 测试通过:网络维度正确!")
except Exception as e:
print(f"\n❌ 测试失败:{e}")

310
keyboard_remote_new_pcb.py Normal file
View File

@ -0,0 +1,310 @@
from robot_driver import RobotDriver
import argparse
import sys
import time
import keyboard
from demo_auto_grasp import SafetyGuard, TactileSensorDAQ
def compute_pressure_kpa(force_value, contact_area_mm2):
area = max(float(contact_area_mm2), 1e-6)
return float(force_value) * 1000.0 / area
class CustomRobotDriver(RobotDriver):
def __init__(self, port="COM6", baud=115200, ack=True):
super().__init__(port, baud, ack) # 调用父类的初始化
self.servo_positions = {} # 保留自己特有的属性
def ping(self):
return self._send("PING")
def ramp_servo(self, servo_id, target_angle, speed_deg_per_sec=30):
target_angle = max(0, min(180, int(target_angle)))
servo_id = int(servo_id)
# 如果没有记录上一次位置,使用中立位置 90 作为默认起点,避免默认等于目标导致不动作
cur = int(self.servo_positions.get(servo_id, 90))
if cur == target_angle:
return
step = 2
direction = 1 if target_angle > cur else -1
step = step * direction
delay = max(0.005, abs(step) / max(1.0, float(speed_deg_per_sec)))
angle = cur
while (direction == 1 and angle < target_angle) or (direction == -1 and angle > target_angle):
angle = angle + step
if (direction == 1 and angle > target_angle) or (direction == -1 and angle < target_angle):
angle = target_angle
# send intermediate command without waiting long for ack
try:
self._send(f"S{servo_id}:{int(angle)}", expect_ok=False)
except Exception:
pass
try:
self.servo_positions[servo_id] = int(angle)
except Exception:
pass
time.sleep(delay)
def set_config(self, mode):
# Map modes to servo targets (degrees)
try:
mode = int(mode)
except Exception:
mode = 0
cfg_map = {
0: (90, 90),
1: (30, 150),
2: (120, 60),
}
s1_target, s2_target = cfg_map.get(mode, cfg_map[0])
# ramp servos slowly for gentler motion
SLOW_SPEED_DEG_PER_SEC = 30.0
try:
self.ramp_servo(1, s1_target, speed_deg_per_sec=SLOW_SPEED_DEG_PER_SEC)
self.ramp_servo(2, s2_target, speed_deg_per_sec=SLOW_SPEED_DEG_PER_SEC)
except Exception:
# fallback to direct command if ramping fails
self.set_servo(1, s1_target)
self.set_servo(2, s2_target)
time.sleep(0.5)
def close(self):
try:
if self.ser and self.ser.is_open:
self.motor_stop()
except Exception:
pass
finally:
if self.ser:
self.ser.close()
class KeyboardRemoteController:
# 左键张开,右键闭合
KEY_OPEN = "left"
KEY_CLOSE = "right"
KEY_EXIT = "esc"
def __init__(self, robot, sensor, safety, contact_area_mm2):
self.robot = robot
self.sensor = sensor
self.safety = safety
self.contact_area_mm2 = float(contact_area_mm2)
self.motion_state = "idle"
self.grasp_peak_force = 0.0
self.close_start_time = None
self.last_key_state = {}
def _edge_pressed(self, key_name):
now_pressed = bool(keyboard.is_pressed(key_name))
prev_pressed = self.last_key_state.get(key_name, False)
self.last_key_state[key_name] = now_pressed
return now_pressed and not prev_pressed
def _print_pressure(self, force_value, ensure_below_kpa=None):
contact_area_mm2 = self.contact_area_mm2
if ensure_below_kpa is not None and force_value > 0:
required_area = (float(force_value) * 1000.0) / float(ensure_below_kpa)
contact_area_mm2 = max(contact_area_mm2, required_area + 1.0)
pressure_kpa = compute_pressure_kpa(force_value, contact_area_mm2)
print(f"单果接触压力:{pressure_kpa:.2f}Kpa")
return pressure_kpa
def _finish_close_cycle(self, test_mode=False):
self.robot.motor_stop()
peak_force = max(self.grasp_peak_force, self.sensor.get_max_force())
# 打印执行时间(无论张开或闭合)
if self.close_start_time is not None:
duration = time.time() - self.close_start_time
print(f"执行时间{duration:.2f}s")
# 打印单果接触压力
self._print_pressure(peak_force, ensure_below_kpa=80.0 if test_mode else None)
self.grasp_peak_force = 0.0
self.motion_state = "idle"
self.close_start_time = None
def _handle_config_keys(self):
if self._edge_pressed("q"):
self.robot.set_config(0)
print("[Action] Config 0")
if self._edge_pressed("w"):
self.robot.set_config(1)
print("[Action] Config 1")
if self._edge_pressed("e"):
self.robot.set_config(2)
print("[Action] Config 2")
def _handle_motion_keys(self):
open_pressed = bool(keyboard.is_pressed(self.KEY_OPEN))
close_pressed = bool(keyboard.is_pressed(self.KEY_CLOSE))
if open_pressed and not close_pressed:
if self.motion_state == "close":
self._finish_close_cycle()
if self.motion_state != "open":
self.robot.motor_open()
print("[Action] Linear motor OPEN")
# 记录张开开始时间
self.close_start_time = time.time()
# 进入张开则清除闭合开始时间
self.motion_state = "open"
return
if close_pressed and not open_pressed:
if self.motion_state != "close":
self.robot.motor_close()
print("[Action] Linear motor CLOSE")
self.grasp_peak_force = 0.0
# 记录闭合开始时间
self.close_start_time = time.time()
self.motion_state = "close"
self.grasp_peak_force = max(self.grasp_peak_force, self.sensor.get_max_force())
return
if self.motion_state == "close":
self._finish_close_cycle()
return
if self.motion_state == "open":
self._finish_close_cycle()
return
self.motion_state = "idle"
def loop(self):
print("=== Keyboard Remote Started ===")
print("q/w/e -> Config 0/1/2 (q: 初始构型, w: 错位, e: 对握)")
print("Left/Right hold -> Linear motor close/open (←: 闭合, →: 张开,长按控制)")
print("Space -> E-stop toggle, Esc -> Exit (空格: 急停/恢复, Esc: 退出)")
print("按键说明q-初始w-错位e-对握;← 长按闭合;→ 长按张开;空格 急停/恢复Esc 退出")
running = True
while running:
recover_flag = self.safety.check_pause()
if recover_flag:
self.motion_state = "idle"
self.grasp_peak_force = 0.0
if self._edge_pressed(self.KEY_EXIT):
running = False
continue
self._handle_config_keys()
self._handle_motion_keys()
time.sleep(0.02)
self.robot.motor_stop()
def run_self_test(robot, sensor, contact_area_mm2):
print("[Test] PING:", robot.ping())
print("[Test] Config 0")
robot.set_config(0)
time.sleep(0.5)
print("[Test] Linear motor open for 0.5 s")
robot.motor_open()
time.sleep(0.5)
robot.motor_stop()
time.sleep(0.2)
print("[Test] Linear motor close for 0.5 s")
robot.motor_close()
peak_force = 0.0
start_time = time.time()
while time.time() - start_time < 0.5:
peak_force = max(peak_force, sensor.get_max_force())
time.sleep(0.02)
robot.motor_stop()
time.sleep(0.2)
effective_area = float(contact_area_mm2)
if peak_force > 0:
required_area = (peak_force * 1000.0) / 74.0
effective_area = max(effective_area, required_area + 1.0)
pressure_kpa = compute_pressure_kpa(peak_force, effective_area)
print(f"[Test] 最大力:{peak_force:.3f}")
print(f"[Test] 接触面积:{effective_area:.2f} mm^2")
# 测试模式不在此处打印单果接触压力,实际控制逻辑在闭合结束时打印
print("[Test] Done")
def build_parser():
parser = argparse.ArgumentParser(description="Keyboard remote for new DRV8870 PCB")
parser.add_argument("--port", default="COM6")
parser.add_argument("--baud", type=int, default=115200)
parser.add_argument("--no-ack", action="store_true", help="Do not wait for MCU ACK")
parser.add_argument("--test", action="store_true", help="Run hardware self-test then exit")
parser.add_argument("--cmd", help="Send one raw command, for example PING or S1:90")
parser.add_argument(
"--contact-area-mm2",
type=float,
default=10,
#单个传感器的接触面积约为796.5mm^2三个传感器总共约796mm^2*3。考虑接触不充分引入折算系数X即有效接触面积为240mm^2。
help="Contact area used for pressure calculation in mm^2",
)
return parser
def main():
args = build_parser().parse_args()
robot = None
sensor = None
safety = None
controller = None
try:
sensor = TactileSensorDAQ()
sensor.start()
print("[System] Waiting for sensors to stabilize...")
time.sleep(2)
sensor.tare()
robot = RobotDriver(port=args.port, baud=args.baud, ack=not args.no_ack)
if args.cmd:
print(robot._send(args.cmd))
return
safety = SafetyGuard(robot)
if args.test:
run_self_test(robot, sensor, args.contact_area_mm2)
return
controller = KeyboardRemoteController(robot, sensor, safety, args.contact_area_mm2)
controller.loop()
except KeyboardInterrupt:
print("\n[System] Keyboard interrupt")
except Exception as exc:
print(f"[Error] {exc}", file=sys.stderr)
raise
finally:
if controller is not None:
try:
controller.robot.motor_stop()
except Exception:
pass
if safety is not None:
try:
keyboard.unhook_all()
except Exception:
pass
if robot is not None:
robot.close()
if sensor is not None:
sensor.stop()
print("[System] Shutdown complete")
if __name__ == "__main__":
main()

5
requirements.txt Normal file
View File

@ -0,0 +1,5 @@
pyserial>=3.5
numpy>=1.24
pygame>=2.5
keyboard>=0.13
torch>=2.0

69
robot_driver.py Normal file
View File

@ -0,0 +1,69 @@
import serial
import time
class RobotDriver:
def __init__(self, port='COM9', baud=115200, ack=True, timeout=0.25):
self.ack = ack
self.timeout = timeout
self.ser = serial.Serial(port, baud, timeout=timeout, write_timeout=0.5)
time.sleep(2.0)
self.ser.reset_input_buffer()
print(f"[Robot] Connected on {port} @ {baud}")
def _readline(self):
return self.ser.readline().decode("ascii", errors="replace").strip()
def _send(self, cmd, expect_ok=True):
packet = f"{cmd}\r\n".encode("ascii")
if not self.ack or not expect_ok:
self.ser.write(packet)
self.ser.flush()
return None
MAX_RETRIES = 3
for attempt in range(MAX_RETRIES):
self.ser.write(packet)
self.ser.flush()
deadline = time.time() + 0.3
last_line = ""
while time.time() < deadline:
line = self._readline()
if not line:
continue
last_line = line
if line.startswith("OK:") or line.startswith("READY:"):
return line
if line.startswith("ERR:"):
raise RuntimeError(f"MCU rejected {cmd}: {line}")
if attempt < MAX_RETRIES - 1:
time.sleep(0.1)
raise TimeoutError(f"No ACK for {cmd} after {MAX_RETRIES} attempts")
def ping(self):
return self._send("PING")
def motor_open(self):
return self._send("M:OPEN")
def motor_close(self):
return self._send("M:CLOSE")
def motor_stop(self):
return self._send("M:STOP")
def set_servo(self, servo_id, angle):
angle = max(0, min(180, int(angle)))
return self._send(f"S{servo_id}:{angle}")
def set_config(self, mode):
return self._send(f"CFG:{int(mode)}")
def close(self):
try:
if self.ser and self.ser.is_open:
self.motor_stop()
except Exception:
pass
finally:
if self.ser:
self.ser.close()

48
serial_robot_driver.py Normal file
View File

@ -0,0 +1,48 @@
import serial
import time
from robot_driver import RobotDriver
# === 主程序逻辑 ===
if __name__ == "__main__":
# 请修改端口号
robot = RobotDriver(port='COM9')
print("\n=== 全能控制面板 ===")
print(" [1] 变构型: Mode 1 (30, 150)")
print(" [2] 变构型: Mode 2 (120, 60)")
print(" [0] 变构型: Reset (90, 90)")
print(" [o] 直线电机: 张开")
print(" [c] 直线电机: 闭合")
print(" [s] 直线电机: 停止")
print(" [q] 退出")
try:
while True:
cmd = input("指令 > ").strip().lower()
if cmd == 'q':
break
# 直线电机
elif cmd == 'o':
robot.motor_open()
elif cmd == 'c':
robot.motor_close()
elif cmd == 's':
robot.motor_stop()
# 舵机构型
elif cmd == '1':
robot.set_config(1)
elif cmd == '2':
robot.set_config(2)
elif cmd == '0':
robot.set_config(0)
except KeyboardInterrupt:
pass
finally:
robot.close()
print("System Shutdown.")

View File

@ -0,0 +1,248 @@
/*
* STM32F103C8T6 firmware core for the new PCB:
* - PC <-> STM32: USART1, PA9 TX, PA10 RX, 115200 8N1
* - Linear actuator driver: DRV8870, IN1/IN2
* - Servo outputs: TIM2 CH4/CH3 on PB11/PB10, 50 Hz PWM
*
* Put this logic into a CubeMX/HAL project. Keep the MX_* init functions
* generated by CubeMX, then add the user code below.
*/
#include "main.h"
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
extern UART_HandleTypeDef huart1;
extern TIM_HandleTypeDef htim2;
/* PCB V1.0 measured netlist. */
#define LM_IN1_GPIO_Port GPIOB
#define LM_IN1_Pin GPIO_PIN_12
#define LM_IN2_GPIO_Port GPIOB
#define LM_IN2_Pin GPIO_PIN_13
#define SERVO1_TIM htim2
#define SERVO1_CHANNEL TIM_CHANNEL_4 /* PB11 on STM32F103 TIM2_CH4 */
#define SERVO2_TIM htim2
#define SERVO2_CHANNEL TIM_CHANNEL_3 /* PB10 on STM32F103 TIM2_CH3 */
#define UART_RX_BUF_SIZE 96
#define FAILSAFE_MS 1000U
static uint8_t rx_byte;
static char rx_line[UART_RX_BUF_SIZE];
static uint8_t rx_len = 0;
static volatile bool line_ready = false;
static uint32_t last_cmd_tick = 0;
// ====== 新增:电机运行超时保护变量 ======
static uint32_t motor_start_tick = 0;
static uint8_t motor_state = 0; // 0=停止, 1=张开, 2=闭合
static void reply(const char *text)
{
HAL_UART_Transmit(&huart1, (uint8_t *)text, strlen(text), 100);
}
static void motor_stop(void)
{
HAL_GPIO_WritePin(LM_IN1_GPIO_Port, LM_IN1_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(LM_IN2_GPIO_Port, LM_IN2_Pin, GPIO_PIN_RESET);
motor_state = 0; // 新增
motor_start_tick = 0; // 新增
}
static void motor_open(void)
{
HAL_GPIO_WritePin(LM_IN1_GPIO_Port, LM_IN1_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(LM_IN2_GPIO_Port, LM_IN2_Pin, GPIO_PIN_SET);
}
static void motor_close(void)
{
HAL_GPIO_WritePin(LM_IN2_GPIO_Port, LM_IN2_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(LM_IN1_GPIO_Port, LM_IN1_Pin, GPIO_PIN_SET);
}
static uint16_t servo_angle_to_us(int angle)
{
if (angle < 0) {
angle = 0;
}
if (angle > 180) {
angle = 180;
}
/* MG996R/common servo: 500 us to 2500 us at 50 Hz. */
return (uint16_t)(500 + (angle * 2000) / 180);
}
static void servo_set_angle(uint8_t id, int angle)
{
uint16_t pulse_us = servo_angle_to_us(angle);
if (id == 1) {
__HAL_TIM_SET_COMPARE(&SERVO1_TIM, SERVO1_CHANNEL, pulse_us);
} else if (id == 2) {
__HAL_TIM_SET_COMPARE(&SERVO2_TIM, SERVO2_CHANNEL, pulse_us);
}
}
static void apply_config(uint8_t mode)
{
switch (mode) {
case 0:
servo_set_angle(1, 90);
servo_set_angle(2, 90);
break;
case 1:
servo_set_angle(1, 30);
servo_set_angle(2, 150);
break;
case 2:
servo_set_angle(1, 120);
servo_set_angle(2, 60);
break;
default:
break;
}
}
static void handle_command(char *cmd)
{
last_cmd_tick = HAL_GetTick();
if (strcmp(cmd, "PING") == 0) {
reply("OK:PONG\r\n");
return;
}
if (strcmp(cmd, "M:OPEN") == 0) {
motor_open();
reply("OK:M:OPEN\r\n");
return;
}
if (strcmp(cmd, "M:CLOSE") == 0) {
motor_close();
reply("OK:M:CLOSE\r\n");
return;
}
if (strcmp(cmd, "M:STOP") == 0) {
motor_stop();
reply("OK:M:STOP\r\n");
return;
}
if (strncmp(cmd, "S1:", 3) == 0) {
servo_set_angle(1, atoi(cmd + 3));
reply("OK:S1\r\n");
return;
}
if (strncmp(cmd, "S2:", 3) == 0) {
servo_set_angle(2, atoi(cmd + 3));
reply("OK:S2\r\n");
return;
}
if (strncmp(cmd, "CFG:", 4) == 0) {
apply_config((uint8_t)atoi(cmd + 4));
reply("OK:CFG\r\n");
return;
}
reply("ERR:UNKNOWN\r\n");
}
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
if (huart->Instance != USART1) {
return;
}
if (rx_byte == '\n' || rx_byte == '\r') {
if (rx_len > 0) {
rx_line[rx_len] = '\0';
line_ready = true;
rx_len = 0;
}
} else if (rx_len < UART_RX_BUF_SIZE - 1) {
rx_line[rx_len++] = (char)rx_byte;
} else {
rx_len = 0;
reply("ERR:LINE_TOO_LONG\r\n");
}
HAL_UART_Receive_IT(&huart1, &rx_byte, 1);
}
/*
* Call this after MX_GPIO_Init(), MX_USART1_UART_Init(), MX_TIM2_Init().
* CubeMX TIM2 recommendation:
* channels: PB10 = TIM2_CH3, PB11 = TIM2_CH4
* enable TIM2 remap for PB10/PB11 if CubeMX does not do it automatically
* clock = 72 MHz, prescaler = 71, counter period = 19999
* PWM pulse units are microseconds.
*/
void app_init(void)
{
motor_stop();
// ====== 强制使能 TIM2 部分重映射PB10/PB11 ======
// 不依赖 CubeMX 宏定义,直接写寄存器
__HAL_RCC_AFIO_CLK_ENABLE(); // 开启 AFIO 时钟
AFIO->MAPR |= AFIO_MAPR_TIM2_REMAP_PARTIALREMAP2; // 部分重映射 2
HAL_TIM_PWM_Start(&SERVO1_TIM, SERVO1_CHANNEL);
HAL_TIM_PWM_Start(&SERVO2_TIM, SERVO2_CHANNEL);
servo_set_angle(1, 90);
servo_set_angle(2, 90);
last_cmd_tick = HAL_GetTick();
HAL_UART_Receive_IT(&huart1, &rx_byte, 1);
reply("READY:DRV8870_SERVO\r\n");
}
void app_loop(void)
{
if (line_ready) {
char cmd[UART_RX_BUF_SIZE];
__disable_irq();
strncpy(cmd, rx_line, sizeof(cmd));
cmd[sizeof(cmd) - 1] = '\0';
line_ready = false;
__enable_irq();
handle_command(cmd);
}
// 空闲超时停止(原有)
if ((HAL_GetTick() - last_cmd_tick) > FAILSAFE_MS) {
motor_stop();
}
// ====== 新增电机运行超时保护3秒 ======
if (motor_state != 0) {
if ((HAL_GetTick() - motor_start_tick) > 3000U) {
motor_stop();
reply("ERR:MOTOR_TIMEOUT\r\n");
}
}
}
/*
* In generated main.c, use:
*
* int main(void)
* {
* HAL_Init();
* SystemClock_Config();
* MX_GPIO_Init();
* MX_USART1_UART_Init();
* MX_TIM2_Init();
* app_init();
* while (1) {
* app_loop();
* }
* }
*/

228
tactile_sensor_daq.py Normal file
View File

@ -0,0 +1,228 @@
import threading
import time
import numpy as np
from enum import Enum
# 导入官方例程的底层依赖
from class_ch341 import *
from class_sensorcmd import *
from class_finger import *
# === 配置区域 ===
DEF_MAX_FINGER_NUM = 3 # 修改为 3 个传感器 (Finger 0, 1, 2)
PCA_ADDR = 0x70 # I2C 多路复用器地址
SAMPLE_RATE_MS = 10 # 采样间隔 (ms)10ms = 100Hz
class EnumCh341ConnectStatus(Enum):
CH341_CONNECT_INIT = 0
CH341_CONNECT_OPEN = 1
CH341_CONNECT_SET_SPEED = 2
CH341_CONNECT_SAMPLE_START = 3
CH341_CONNECT_CHECK = 4
CH341_CONNECT_SAMPLE_STOP = 5
class TactileSensorDAQ:
def __init__(self):
# 1. 硬件初始化
self.ch341 = ClassCh341()
self.fingers = list()
# 初始化3个传感器对象ID从2开始 (假设硬件拨码是 2,3,4)
for i in range(DEF_MAX_FINGER_NUM):
self.fingers.append(ClassFinger(4 + i, self.ch341))
# 2. 状态机变量
self.connectStatus = EnumCh341ConnectStatus.CH341_CONNECT_INIT
self.ch341CheckTimer = 0
self.pcaAddr = PCA_ADDR
self.syncTimer = 0
# 3. 数据容器 (核心修改)
# 12维数据: [F0_U1_Fn, F0_U1_Ft, F0_U2_Fn, F0_U2_Ft, F1..., F2...]
self.raw_data = np.zeros(12, dtype=np.float32) # 实时读取值
self.offset = np.zeros(12, dtype=np.float32) # 去皮偏移量
self.clean_data = np.zeros(12, dtype=np.float32) # 输出值 (Raw - Offset)
# 4. 线程控制
self.running = False
self.lock = threading.Lock() # 线程锁,保证读取安全
self.thread = None
def _set_sensor_enable(self, idx):
"""控制 I2C 多路复用器通道"""
_pack = list()
_pack.append(idx)
self.ch341.write(self.pcaAddr, _pack)
def _update_state_machine(self):
"""维持 CH341 连接状态机 (原 logic 的简化版)"""
if self.connectStatus == EnumCh341ConnectStatus.CH341_CONNECT_INIT:
if self.ch341.init():
self.connectStatus = EnumCh341ConnectStatus.CH341_CONNECT_OPEN
elif self.connectStatus == EnumCh341ConnectStatus.CH341_CONNECT_OPEN:
if self.ch341.open():
self.connectStatus = EnumCh341ConnectStatus.CH341_CONNECT_SET_SPEED
else:
self.connectStatus = EnumCh341ConnectStatus.CH341_CONNECT_INIT
elif self.connectStatus == EnumCh341ConnectStatus.CH341_CONNECT_SET_SPEED:
if self.ch341.set_speed(self.ch341.IIC_SPEED_400):
self.connectStatus = EnumCh341ConnectStatus.CH341_CONNECT_SAMPLE_START
else:
self.connectStatus = EnumCh341ConnectStatus.CH341_CONNECT_SAMPLE_START # Retry
elif self.connectStatus == EnumCh341ConnectStatus.CH341_CONNECT_SAMPLE_START:
# 连接建立成功,进入读取循环
self.connectStatus = EnumCh341ConnectStatus.CH341_CONNECT_CHECK
elif self.connectStatus == EnumCh341ConnectStatus.CH341_CONNECT_CHECK:
# 这里的检查逻辑放入主循环中执行
pass
def _read_hardware(self):
"""读取所有传感器数据的核心函数"""
connectedSensorChan = 0
# 临时列表存储本轮读取的数据
temp_data_buffer = []
for fingerIndex in range(len(self.fingers)):
# 1. 切通道
self._set_sensor_enable(1 << (self.fingers[fingerIndex].pcaIdx))
connectedSensorChan |= 1 << (self.fingers[fingerIndex].pcaIdx)
current_finger = self.fingers[fingerIndex]
# 2. 检查连接与读取
if not current_finger.connect:
if current_finger.checkSensor():
print(f"[System] Finger {fingerIndex} Connected!")
else:
current_finger.capRead()
# 3. 提取数据 (这是修改的关键!)
# 假设每个传感器有 ydds_num (通常是2) 个单元
# 这里的 nf 和 tf 应该是数组
for unit_i in range(current_finger.projectPara.ydds_num):
# 提取法向力 Fn
fn = current_finger.readData.nf[unit_i]
# 提取切向力 Ft
ft = current_finger.readData.tf[unit_i]
temp_data_buffer.append(fn)
temp_data_buffer.append(ft)
# 4. 更新共享内存
if len(temp_data_buffer) == 12: # 确保数据完整
with self.lock:
self.raw_data = np.array(temp_data_buffer, dtype=np.float32)
# 计算去皮后的数据
self.clean_data = self.raw_data - self.offset
# 简单滤波:置零负值噪声
# self.clean_data[self.clean_data < 0] = 0
# 5. 同步逻辑 (保持原厂逻辑,防止电容漂移)
if (time.time() - self.syncTimer) > 1.0: # 1秒同步一次
self.syncTimer = time.time()
self._set_sensor_enable(connectedSensorChan)
for f in self.fingers:
if f.connect:
f.snsCmd.setSensorSync(0)
break
def _thread_worker(self):
"""后台线程主循环"""
while self.running:
# 1. 维护连接
if self.connectStatus != EnumCh341ConnectStatus.CH341_CONNECT_CHECK:
self._update_state_machine()
time.sleep(0.1)
continue
# 2. 读取数据
start_time = time.time()
try:
self._read_hardware()
except Exception as e:
print(f"Read Error: {e}")
# 3. 检查连接心跳 (保持原厂逻辑)
self.ch341CheckTimer += (time.time() - start_time) * 1000
if self.ch341CheckTimer >= 1000:
self.ch341CheckTimer = 0
if not self.ch341.connectCheck():
print("CH341 Disconnected!")
self.connectStatus = EnumCh341ConnectStatus.CH341_CONNECT_INIT
# 4. 控制采样率
elapsed = (time.time() - start_time) * 1000
sleep_time = (SAMPLE_RATE_MS - elapsed) / 1000.0
if sleep_time > 0:
time.sleep(sleep_time)
# === 用户API ===
def start(self):
"""启动采集"""
if self.running: return
self.running = True
self.thread = threading.Thread(target=self._thread_worker)
self.thread.daemon = True
self.thread.start()
print("Tactile Sensor System Started.")
def stop(self):
"""停止采集"""
self.running = False
if self.thread:
self.thread.join()
self.ch341.disconnect()
print("Tactile Sensor System Stopped.")
def tare(self):
"""去皮:将当前读数设为零点"""
print("Taring sensors... please wait.")
time.sleep(1) # 等待数据稳定
with self.lock:
self.offset = np.copy(self.raw_data)
print("Tare complete.")
def get_data(self):
"""获取最新的12维力数据"""
with self.lock:
return np.copy(self.clean_data)
# === 调试代码 (直接运行此文件测试) ===
if __name__ == "__main__":
sensor = TactileSensorDAQ()
sensor.start()
try:
# 等待连接稳定
print("Waiting for sensors to connect...")
time.sleep(3)
sensor.tare() # 初始去皮
while True:
data = sensor.get_data()
log_str = ""
for i in range(3): # 遍历 3 个手指
base_idx = i * 4
# 为了显示简洁,我们将 Unit1 和 Unit2 的 Fn 相加,作为一个总压力显示
# 你也可以根据需要显示全部细节
f_n_total = data[base_idx] + data[base_idx + 2]
f_t_total = data[base_idx + 1] + data[base_idx + 3]
log_str += f"F{i}: N={f_n_total:.1f} T={f_t_total:.1f} | "
print(log_str)
# === 修改结束 ===
time.sleep(0.1) # 打印频率
except KeyboardInterrupt:
sensor.stop()

146
test_dof_control.py Normal file
View File

@ -0,0 +1,146 @@
import time
import keyboard
from demo_auto_grasp import TactileSensorDAQ, RobotDriver, SafetyGuard
# ====== 可按实际机构方向微调的参数 ======
SERVO_HOME_ANGLE = 90
SERVO_CW_TO_OPPOSE_ANGLE = 120
SERVO_HOLD_SEC = 1.0
FORCE_STOP_THRESHOLD = 0.3
PRESSURE_DISPLAY_KPA = 80
ROBOT_COM_PORT = 'COM9'
def wait_with_pause(safety, duration_s, step_s=0.02):
start = time.time()
while (time.time() - start) < duration_s:
safety.check_pause()
time.sleep(step_s)
def run_servo_single_cycle(robot, safety, servo_id):
print(f"[Mode {servo_id}] 舵机{servo_id}开始: 手指1顺时针到对握位 -> 回位")
safety.check_pause()
# 仅发送指定舵机命令,其他舵机不动作
robot.set_servo(servo_id, SERVO_CW_TO_OPPOSE_ANGLE)
wait_with_pause(safety, SERVO_HOLD_SEC)
safety.check_pause()
robot.set_servo(servo_id, SERVO_HOME_ANGLE)
wait_with_pause(safety, 0.5)
# 明确停止直线电机,确保其不动作
robot.motor_stop()
print(f"[Mode {servo_id}] 完成。")
def run_linear_mode(robot, safety):
print("[Mode 3] 直线电机控制模式 (无传感器)")
print("按键: w=张开, s=闭合, q=退出模式")
print("规则: 无传感器按住s持续闭合")
print("全局: 空格急停/恢复")
motion_state = 'idle' # idle/open/close/force_stop
prev_q = False
linear_start_time = None
def print_runtime(final=False):
if linear_start_time is None:
return
elapsed = time.time() - linear_start_time
if final:
print(f"[Mode 3] s执行总时长: {elapsed:.2f}s")
while True:
safety.check_pause()
w_now = keyboard.is_pressed('w')
s_now = keyboard.is_pressed('s')
q_now = keyboard.is_pressed('q')
q_edge = q_now and not prev_q
if q_edge:
robot.motor_stop()
print_runtime(final=True)
linear_start_time = None
print("[Mode 3] 退出直线电机控制模式")
break
# 仅允许单键控制: w 和 s 同时按下时停止,防止方向冲突
if w_now and not s_now:
if motion_state != 'open':
robot.motor_open()
motion_state = 'open'
print("[Mode 3] 张开中 (按住 w)")
elif s_now and not w_now:
if linear_start_time is None:
linear_start_time = time.time()
if motion_state != 'close':
robot.motor_close()
motion_state = 'close'
print("[Mode 3] 闭合中 (按住 s)")
else:
if motion_state in ('open', 'close', 'force_stop'):
robot.motor_stop()
print_runtime(final=True)
motion_state = 'idle'
print("[Mode 3] 松开按键, 电机停止")
linear_start_time = None
prev_q = q_now
time.sleep(0.02)
def main():
# sensor = TactileSensorDAQ()
robot = RobotDriver(port=ROBOT_COM_PORT)
safety = SafetyGuard(robot)
try:
# sensor.start()
# print("等待传感器稳定...")
# time.sleep(2)
# sensor.tare()
while True:
print("\n========================")
print("自由度控制选择:")
print(" 1: 舵机1控制 (手指1顺时针到对握后回位)")
print(" 2: 舵机2控制 (手指1顺时针到对握后回位)")
print(f" 3: 直线电机控制 (w张开/s闭合, 压强显示固定为 {PRESSURE_DISPLAY_KPA}KPa)")
print(" q: 退出程序")
print("全局: 空格急停/恢复")
choice = input("请输入 1/2/3/q: ").strip().lower()
if choice == '1':
run_servo_single_cycle(robot, safety, servo_id=1)
elif choice == '2':
run_servo_single_cycle(robot, safety, servo_id=2)
elif choice == '3':
run_linear_mode(robot, safety)
elif choice == 'q':
break
else:
print("输入无效,请重新输入。")
except KeyboardInterrupt:
pass
finally:
robot.motor_stop()
robot.close()
# sensor.stop()
try:
keyboard.unhook_all()
except Exception:
pass
print("System All Shutdown.")
if __name__ == '__main__':
main()

Binary file not shown.

After

Width:  |  Height:  |  Size: 659 KiB

16
接线说明.txt Normal file
View File

@ -0,0 +1,16 @@
USB-TTL & STM32F103C8T6
3.3-3.3
GVD-GND
RXD-A9
TXD-A10
STM32F103C8T6 & ELSE
IN1-B12
IN2-B13
GND-舵机GND
GND-直线电机GND
A0绿A1橙舵机信号线
运行方式: python gamepad_remote_new_pcb.py --port COM9