Compare commits
11 Commits
07fa13b845
...
feed450554
| Author | SHA1 | Date | |
|---|---|---|---|
| feed450554 | |||
| 22ae87f4b4 | |||
| a5895c41b0 | |||
| 8e01b24dac | |||
| b5455a269c | |||
| cb39e602a3 | |||
| b86558971e | |||
| 3a277fe631 | |||
| 5dc6047716 | |||
| bf89c0f960 | |||
| 3fc2463139 |
121
.waylog/history/2026-04-25_14-34Z-你是什么模型.md
Normal file
121
.waylog/history/2026-04-25_14-34Z-你是什么模型.md
Normal file
@ -0,0 +1,121 @@
|
|||||||
|
# 你是什么模型
|
||||||
|
_Exported on 04/25/2026 at 22:34:54 GMT+8 from OpenAI Codex via WayLog_
|
||||||
|
|
||||||
|
|
||||||
|
**OpenAI Codex**
|
||||||
|
|
||||||
|
<permissions instructions>
|
||||||
|
Filesystem sandboxing defines which files can be read or written. `sandbox_mode` is `workspace-write`: The sandbox permits reading files, and editing files in `cwd` and `writable_roots`. Editing files in other directories requires approval. Network access is restricted.
|
||||||
|
# Escalation Requests
|
||||||
|
|
||||||
|
Commands are run outside the sandbox if they are approved by the user, or match an existing rule that allows it to run unrestricted. The command string is split into independent command segments at shell control operators, including but not limited to:
|
||||||
|
|
||||||
|
- Pipes: |
|
||||||
|
- Logical operators: &&, ||
|
||||||
|
- Command separators: ;
|
||||||
|
- Subshell boundaries: (...), $(...)
|
||||||
|
|
||||||
|
Each resulting segment is evaluated independently for sandbox restrictions and approval requirements.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
|
||||||
|
git pull | tee output.txt
|
||||||
|
|
||||||
|
This is treated as two command segments:
|
||||||
|
|
||||||
|
["git", "pull"]
|
||||||
|
|
||||||
|
["tee", "output.txt"]
|
||||||
|
|
||||||
|
Commands that use more advanced shell features like redirection (>, >>, <), substitutions ($(...), ...), environment variables (FOO=bar), or wildcard patterns (*, ?) will not be evaluated against rules, to limit the scope of what an approved rule allows.
|
||||||
|
|
||||||
|
## How to request escalation
|
||||||
|
|
||||||
|
IMPORTANT: To request approval to execute a command that will require escalated privileges:
|
||||||
|
|
||||||
|
- Provide the `sandbox_permissions` parameter with the value `"require_escalated"`
|
||||||
|
- Include a short question asking the user if they want to allow the action in `justification` parameter. e.g. "Do you want to download and install dependencies for this project?"
|
||||||
|
- Optionally suggest a `prefix_rule` - this will be shown to the user with an option to persist the rule approval for future sessions.
|
||||||
|
|
||||||
|
If you run a command that is important to solving the user's query, but it fails because of sandboxing or with a likely sandbox-related network error (for example DNS/host resolution, registry/index access, or dependency download failure), rerun the command with "require_escalated". ALWAYS proceed to use the `justification` parameter - do not message the user before requesting approval for the command.
|
||||||
|
|
||||||
|
## When to request escalation
|
||||||
|
|
||||||
|
While commands are running inside the sandbox, here are some scenarios that will require escalation outside the sandbox:
|
||||||
|
|
||||||
|
- You need to run a command that writes to a directory that requires it (e.g. running tests that write to /var)
|
||||||
|
- You need to run a GUI app (e.g., open/xdg-open/osascript) to open browsers or files.
|
||||||
|
- If you run a command that is important to solving the user's query, but it fails because of sandboxing or with a likely sandbox-related network error (for example DNS/host resolution, registry/index access, or dependency download failure), rerun the command with `require_escalated`. ALWAYS proceed to use the `sandbox_permissions` and `justification` parameters. do not message the user before requesting approval for the command.
|
||||||
|
- You are about to take a potentially destructive action such as an `rm` or `git reset` that the user did not explicitly ask for.
|
||||||
|
- Be judicious with escalating, but if completing the user's request requires it, you should do so - don't try and circumvent approvals by using other tools.
|
||||||
|
|
||||||
|
## prefix_rule guidance
|
||||||
|
|
||||||
|
When choosing a `prefix_rule`, request one that will allow you to fulfill similar requests from the user in the future without re-requesting escalation. It should be categorical and reasonably scoped to similar capabilities. You should rarely pass the entire command into `prefix_rule`.
|
||||||
|
|
||||||
|
### Banned prefix_rules
|
||||||
|
Avoid requesting overly broad prefixes that the user would be ill-advised to approve. For example, do not request ["python3"], ["python", "-"], or other similar prefixes that would allow arbitrary scripting.
|
||||||
|
NEVER provide a prefix_rule argument for destructive commands like rm.
|
||||||
|
NEVER provide a prefix_rule if your command uses a heredoc or herestring.
|
||||||
|
|
||||||
|
### Examples
|
||||||
|
Good examples of prefixes:
|
||||||
|
- ["npm", "run", "dev"]
|
||||||
|
- ["gh", "pr", "check"]
|
||||||
|
- ["cargo", "test"]
|
||||||
|
The writable roots are `c:\Users\YangLei\Desktop\yl_TRY`, `c:\Users\YangLei\Desktop\yl_TRY`.
|
||||||
|
</permissions instructions><collaboration_mode># Collaboration Mode: Default
|
||||||
|
|
||||||
|
You are now in Default mode. Any previous instructions for other modes (e.g. Plan mode) are no longer active.
|
||||||
|
|
||||||
|
Your active mode changes only when new developer instructions with a different `<collaboration_mode>...</collaboration_mode>` change it; user requests or tool descriptions do not change mode by themselves. Known mode names are Default and Plan.
|
||||||
|
|
||||||
|
## request_user_input availability
|
||||||
|
|
||||||
|
The `request_user_input` tool is unavailable in Default mode. If you call it while in Default mode, it will return an error.
|
||||||
|
|
||||||
|
In Default mode, strongly prefer making reasonable assumptions and executing the user's request rather than stopping to ask questions. If you absolutely must ask a question because the answer cannot be discovered from local context and a reasonable assumption would be risky, ask the user directly with a concise plain-text question. Never write a multiple choice question as a textual assistant message.
|
||||||
|
</collaboration_mode><skills_instructions>
|
||||||
|
## Skills
|
||||||
|
A skill is a set of local instructions to follow that is stored in a `SKILL.md` file. Below is the list of skills that can be used. Each entry includes a name, description, and file path so you can open the source for full instructions when using a specific skill.
|
||||||
|
### Available skills
|
||||||
|
- imagegen: Generate or edit raster images when the task benefits from AI-created bitmap visuals such as photos, illustrations, textures, sprites, mockups, or transparent-background cutouts. Use when Codex should create a brand-new image, transform an existing image, or derive visual variants from references, and the output should be a bitmap asset rather than repo-native code or vector. Do not use when the task is better handled by editing existing SVG/vector/code-native assets, extending an established icon or logo system, or building the visual directly in HTML/CSS/canvas. (file: C:/Users/YangLei/.codex/skills/.system/imagegen/SKILL.md)
|
||||||
|
- openai-docs: Use when the user asks how to build with OpenAI products or APIs and needs up-to-date official documentation with citations, help choosing the latest model for a use case, or model upgrade and prompt-upgrade guidance; prioritize OpenAI docs MCP tools, use bundled references only as helper context, and restrict any fallback browsing to official OpenAI domains. (file: C:/Users/YangLei/.codex/skills/.system/openai-docs/SKILL.md)
|
||||||
|
- plugin-creator: Create and scaffold plugin directories for Codex with a required `.codex-plugin/plugin.json`, optional plugin folders/files, and baseline placeholders you can edit before publishing or testing. Use when Codex needs to create a new local plugin, add optional plugin structure, or generate or update repo-root `.agents/plugins/marketplace.json` entries for plugin ordering and availability metadata. (file: C:/Users/YangLei/.codex/skills/.system/plugin-creator/SKILL.md)
|
||||||
|
- skill-creator: Guide for creating effective skills. This skill should be used when users want to create a new skill (or update an existing skill) that extends Codex's capabilities with specialized knowledge, workflows, or tool integrations. (file: C:/Users/YangLei/.codex/skills/.system/skill-creator/SKILL.md)
|
||||||
|
- skill-installer: Install Codex skills into $CODEX_HOME/skills from a curated list or a GitHub repo path. Use when a user asks to list installable skills, install a curated skill, or install a skill from another repo (including private repos). (file: C:/Users/YangLei/.codex/skills/.system/skill-installer/SKILL.md)
|
||||||
|
### How to use skills
|
||||||
|
- Discovery: The list above is the skills available in this session (name + description + file path). Skill bodies live on disk at the listed paths.
|
||||||
|
- Trigger rules: If the user names a skill (with `$SkillName` or plain text) OR the task clearly matches a skill's description shown above, you must use that skill for that turn. Multiple mentions mean use them all. Do not carry skills across turns unless re-mentioned.
|
||||||
|
- Missing/blocked: If a named skill isn't in the list or the path can't be read, say so briefly and continue with the best fallback.
|
||||||
|
- How to use a skill (progressive disclosure):
|
||||||
|
1) After deciding to use a skill, open its `SKILL.md`. Read only enough to follow the workflow.
|
||||||
|
2) When `SKILL.md` references relative paths (e.g., `scripts/foo.py`), resolve them relative to the skill directory listed above first, and only consider other paths if needed.
|
||||||
|
3) If `SKILL.md` points to extra folders such as `references/`, load only the specific files needed for the request; don't bulk-load everything.
|
||||||
|
4) If `scripts/` exist, prefer running or patching them instead of retyping large code blocks.
|
||||||
|
5) If `assets/` or templates exist, reuse them instead of recreating from scratch.
|
||||||
|
- Coordination and sequencing:
|
||||||
|
- If multiple skills apply, choose the minimal set that covers the request and state the order you'll use them.
|
||||||
|
- Announce which skill(s) you're using and why (one short line). If you skip an obvious skill, say why.
|
||||||
|
- Context hygiene:
|
||||||
|
- Keep context small: summarize long sections instead of pasting them; only load extra files when needed.
|
||||||
|
- Avoid deep reference-chasing: prefer opening only files directly linked from `SKILL.md` unless you're blocked.
|
||||||
|
- When variants exist (frameworks, providers, domains), pick only the relevant reference file(s) and note that choice.
|
||||||
|
- Safety and fallback: If a skill can't be applied cleanly (missing files, unclear instructions), state the issue, pick the next-best approach, and continue.
|
||||||
|
</skills_instructions>
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
|
||||||
|
**User**
|
||||||
|
|
||||||
|
你是什么模型
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
|
||||||
|
**OpenAI Codex**
|
||||||
|
|
||||||
|
⚠️ **Codex Error**: unexpected status 401 Unauthorized: 无效的令牌 (request id: 202604251434244827475728268d9d6eWcpOMyE), url: https://www.dogapi.cc/v1/responses, cf-ray: 9f1e117de835fdcf-SIN
|
||||||
|
|
||||||
|
---
|
||||||
121
.waylog/history/2026-04-25_14-38Z-你是什么模型.md
Normal file
121
.waylog/history/2026-04-25_14-38Z-你是什么模型.md
Normal file
@ -0,0 +1,121 @@
|
|||||||
|
# 你是什么模型
|
||||||
|
_Exported on 04/25/2026 at 22:39:35 GMT+8 from OpenAI Codex via WayLog_
|
||||||
|
|
||||||
|
|
||||||
|
**OpenAI Codex**
|
||||||
|
|
||||||
|
<permissions instructions>
|
||||||
|
Filesystem sandboxing defines which files can be read or written. `sandbox_mode` is `workspace-write`: The sandbox permits reading files, and editing files in `cwd` and `writable_roots`. Editing files in other directories requires approval. Network access is restricted.
|
||||||
|
# Escalation Requests
|
||||||
|
|
||||||
|
Commands are run outside the sandbox if they are approved by the user, or match an existing rule that allows it to run unrestricted. The command string is split into independent command segments at shell control operators, including but not limited to:
|
||||||
|
|
||||||
|
- Pipes: |
|
||||||
|
- Logical operators: &&, ||
|
||||||
|
- Command separators: ;
|
||||||
|
- Subshell boundaries: (...), $(...)
|
||||||
|
|
||||||
|
Each resulting segment is evaluated independently for sandbox restrictions and approval requirements.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
|
||||||
|
git pull | tee output.txt
|
||||||
|
|
||||||
|
This is treated as two command segments:
|
||||||
|
|
||||||
|
["git", "pull"]
|
||||||
|
|
||||||
|
["tee", "output.txt"]
|
||||||
|
|
||||||
|
Commands that use more advanced shell features like redirection (>, >>, <), substitutions ($(...), ...), environment variables (FOO=bar), or wildcard patterns (*, ?) will not be evaluated against rules, to limit the scope of what an approved rule allows.
|
||||||
|
|
||||||
|
## How to request escalation
|
||||||
|
|
||||||
|
IMPORTANT: To request approval to execute a command that will require escalated privileges:
|
||||||
|
|
||||||
|
- Provide the `sandbox_permissions` parameter with the value `"require_escalated"`
|
||||||
|
- Include a short question asking the user if they want to allow the action in `justification` parameter. e.g. "Do you want to download and install dependencies for this project?"
|
||||||
|
- Optionally suggest a `prefix_rule` - this will be shown to the user with an option to persist the rule approval for future sessions.
|
||||||
|
|
||||||
|
If you run a command that is important to solving the user's query, but it fails because of sandboxing or with a likely sandbox-related network error (for example DNS/host resolution, registry/index access, or dependency download failure), rerun the command with "require_escalated". ALWAYS proceed to use the `justification` parameter - do not message the user before requesting approval for the command.
|
||||||
|
|
||||||
|
## When to request escalation
|
||||||
|
|
||||||
|
While commands are running inside the sandbox, here are some scenarios that will require escalation outside the sandbox:
|
||||||
|
|
||||||
|
- You need to run a command that writes to a directory that requires it (e.g. running tests that write to /var)
|
||||||
|
- You need to run a GUI app (e.g., open/xdg-open/osascript) to open browsers or files.
|
||||||
|
- If you run a command that is important to solving the user's query, but it fails because of sandboxing or with a likely sandbox-related network error (for example DNS/host resolution, registry/index access, or dependency download failure), rerun the command with `require_escalated`. ALWAYS proceed to use the `sandbox_permissions` and `justification` parameters. do not message the user before requesting approval for the command.
|
||||||
|
- You are about to take a potentially destructive action such as an `rm` or `git reset` that the user did not explicitly ask for.
|
||||||
|
- Be judicious with escalating, but if completing the user's request requires it, you should do so - don't try and circumvent approvals by using other tools.
|
||||||
|
|
||||||
|
## prefix_rule guidance
|
||||||
|
|
||||||
|
When choosing a `prefix_rule`, request one that will allow you to fulfill similar requests from the user in the future without re-requesting escalation. It should be categorical and reasonably scoped to similar capabilities. You should rarely pass the entire command into `prefix_rule`.
|
||||||
|
|
||||||
|
### Banned prefix_rules
|
||||||
|
Avoid requesting overly broad prefixes that the user would be ill-advised to approve. For example, do not request ["python3"], ["python", "-"], or other similar prefixes that would allow arbitrary scripting.
|
||||||
|
NEVER provide a prefix_rule argument for destructive commands like rm.
|
||||||
|
NEVER provide a prefix_rule if your command uses a heredoc or herestring.
|
||||||
|
|
||||||
|
### Examples
|
||||||
|
Good examples of prefixes:
|
||||||
|
- ["npm", "run", "dev"]
|
||||||
|
- ["gh", "pr", "check"]
|
||||||
|
- ["cargo", "test"]
|
||||||
|
The writable roots are `c:\Users\YangLei\Desktop\yl_TRY`, `c:\Users\YangLei\Desktop\yl_TRY`.
|
||||||
|
</permissions instructions><collaboration_mode># Collaboration Mode: Default
|
||||||
|
|
||||||
|
You are now in Default mode. Any previous instructions for other modes (e.g. Plan mode) are no longer active.
|
||||||
|
|
||||||
|
Your active mode changes only when new developer instructions with a different `<collaboration_mode>...</collaboration_mode>` change it; user requests or tool descriptions do not change mode by themselves. Known mode names are Default and Plan.
|
||||||
|
|
||||||
|
## request_user_input availability
|
||||||
|
|
||||||
|
The `request_user_input` tool is unavailable in Default mode. If you call it while in Default mode, it will return an error.
|
||||||
|
|
||||||
|
In Default mode, strongly prefer making reasonable assumptions and executing the user's request rather than stopping to ask questions. If you absolutely must ask a question because the answer cannot be discovered from local context and a reasonable assumption would be risky, ask the user directly with a concise plain-text question. Never write a multiple choice question as a textual assistant message.
|
||||||
|
</collaboration_mode><skills_instructions>
|
||||||
|
## Skills
|
||||||
|
A skill is a set of local instructions to follow that is stored in a `SKILL.md` file. Below is the list of skills that can be used. Each entry includes a name, description, and file path so you can open the source for full instructions when using a specific skill.
|
||||||
|
### Available skills
|
||||||
|
- imagegen: Generate or edit raster images when the task benefits from AI-created bitmap visuals such as photos, illustrations, textures, sprites, mockups, or transparent-background cutouts. Use when Codex should create a brand-new image, transform an existing image, or derive visual variants from references, and the output should be a bitmap asset rather than repo-native code or vector. Do not use when the task is better handled by editing existing SVG/vector/code-native assets, extending an established icon or logo system, or building the visual directly in HTML/CSS/canvas. (file: C:/Users/YangLei/.codex/skills/.system/imagegen/SKILL.md)
|
||||||
|
- openai-docs: Use when the user asks how to build with OpenAI products or APIs and needs up-to-date official documentation with citations, help choosing the latest model for a use case, or model upgrade and prompt-upgrade guidance; prioritize OpenAI docs MCP tools, use bundled references only as helper context, and restrict any fallback browsing to official OpenAI domains. (file: C:/Users/YangLei/.codex/skills/.system/openai-docs/SKILL.md)
|
||||||
|
- plugin-creator: Create and scaffold plugin directories for Codex with a required `.codex-plugin/plugin.json`, optional plugin folders/files, and baseline placeholders you can edit before publishing or testing. Use when Codex needs to create a new local plugin, add optional plugin structure, or generate or update repo-root `.agents/plugins/marketplace.json` entries for plugin ordering and availability metadata. (file: C:/Users/YangLei/.codex/skills/.system/plugin-creator/SKILL.md)
|
||||||
|
- skill-creator: Guide for creating effective skills. This skill should be used when users want to create a new skill (or update an existing skill) that extends Codex's capabilities with specialized knowledge, workflows, or tool integrations. (file: C:/Users/YangLei/.codex/skills/.system/skill-creator/SKILL.md)
|
||||||
|
- skill-installer: Install Codex skills into $CODEX_HOME/skills from a curated list or a GitHub repo path. Use when a user asks to list installable skills, install a curated skill, or install a skill from another repo (including private repos). (file: C:/Users/YangLei/.codex/skills/.system/skill-installer/SKILL.md)
|
||||||
|
### How to use skills
|
||||||
|
- Discovery: The list above is the skills available in this session (name + description + file path). Skill bodies live on disk at the listed paths.
|
||||||
|
- Trigger rules: If the user names a skill (with `$SkillName` or plain text) OR the task clearly matches a skill's description shown above, you must use that skill for that turn. Multiple mentions mean use them all. Do not carry skills across turns unless re-mentioned.
|
||||||
|
- Missing/blocked: If a named skill isn't in the list or the path can't be read, say so briefly and continue with the best fallback.
|
||||||
|
- How to use a skill (progressive disclosure):
|
||||||
|
1) After deciding to use a skill, open its `SKILL.md`. Read only enough to follow the workflow.
|
||||||
|
2) When `SKILL.md` references relative paths (e.g., `scripts/foo.py`), resolve them relative to the skill directory listed above first, and only consider other paths if needed.
|
||||||
|
3) If `SKILL.md` points to extra folders such as `references/`, load only the specific files needed for the request; don't bulk-load everything.
|
||||||
|
4) If `scripts/` exist, prefer running or patching them instead of retyping large code blocks.
|
||||||
|
5) If `assets/` or templates exist, reuse them instead of recreating from scratch.
|
||||||
|
- Coordination and sequencing:
|
||||||
|
- If multiple skills apply, choose the minimal set that covers the request and state the order you'll use them.
|
||||||
|
- Announce which skill(s) you're using and why (one short line). If you skip an obvious skill, say why.
|
||||||
|
- Context hygiene:
|
||||||
|
- Keep context small: summarize long sections instead of pasting them; only load extra files when needed.
|
||||||
|
- Avoid deep reference-chasing: prefer opening only files directly linked from `SKILL.md` unless you're blocked.
|
||||||
|
- When variants exist (frameworks, providers, domains), pick only the relevant reference file(s) and note that choice.
|
||||||
|
- Safety and fallback: If a skill can't be applied cleanly (missing files, unclear instructions), state the issue, pick the next-best approach, and continue.
|
||||||
|
</skills_instructions>
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
|
||||||
|
**User**
|
||||||
|
|
||||||
|
你是什么模型
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
|
||||||
|
**OpenAI Codex**
|
||||||
|
|
||||||
|
⚠️ **Codex Error**: unexpected status 401 Unauthorized: 无效的令牌 (request id: 202604251438572023001188268d9d6rc3n1jfn), url: https://www.dogapi.cc/v1/responses, cf-ray: 9f1e18267fae9e2c-SIN
|
||||||
|
|
||||||
|
---
|
||||||
135
.waylog/history/2026-04-25_14-42Z-2.md
Normal file
135
.waylog/history/2026-04-25_14-42Z-2.md
Normal file
@ -0,0 +1,135 @@
|
|||||||
|
# 2
|
||||||
|
_Exported on 04/25/2026 at 22:43:12 GMT+8 from OpenAI Codex via WayLog_
|
||||||
|
|
||||||
|
|
||||||
|
**OpenAI Codex**
|
||||||
|
|
||||||
|
<permissions instructions>
|
||||||
|
Filesystem sandboxing defines which files can be read or written. `sandbox_mode` is `workspace-write`: The sandbox permits reading files, and editing files in `cwd` and `writable_roots`. Editing files in other directories requires approval. Network access is restricted.
|
||||||
|
# Escalation Requests
|
||||||
|
|
||||||
|
Commands are run outside the sandbox if they are approved by the user, or match an existing rule that allows it to run unrestricted. The command string is split into independent command segments at shell control operators, including but not limited to:
|
||||||
|
|
||||||
|
- Pipes: |
|
||||||
|
- Logical operators: &&, ||
|
||||||
|
- Command separators: ;
|
||||||
|
- Subshell boundaries: (...), $(...)
|
||||||
|
|
||||||
|
Each resulting segment is evaluated independently for sandbox restrictions and approval requirements.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
|
||||||
|
git pull | tee output.txt
|
||||||
|
|
||||||
|
This is treated as two command segments:
|
||||||
|
|
||||||
|
["git", "pull"]
|
||||||
|
|
||||||
|
["tee", "output.txt"]
|
||||||
|
|
||||||
|
Commands that use more advanced shell features like redirection (>, >>, <), substitutions ($(...), ...), environment variables (FOO=bar), or wildcard patterns (*, ?) will not be evaluated against rules, to limit the scope of what an approved rule allows.
|
||||||
|
|
||||||
|
## How to request escalation
|
||||||
|
|
||||||
|
IMPORTANT: To request approval to execute a command that will require escalated privileges:
|
||||||
|
|
||||||
|
- Provide the `sandbox_permissions` parameter with the value `"require_escalated"`
|
||||||
|
- Include a short question asking the user if they want to allow the action in `justification` parameter. e.g. "Do you want to download and install dependencies for this project?"
|
||||||
|
- Optionally suggest a `prefix_rule` - this will be shown to the user with an option to persist the rule approval for future sessions.
|
||||||
|
|
||||||
|
If you run a command that is important to solving the user's query, but it fails because of sandboxing or with a likely sandbox-related network error (for example DNS/host resolution, registry/index access, or dependency download failure), rerun the command with "require_escalated". ALWAYS proceed to use the `justification` parameter - do not message the user before requesting approval for the command.
|
||||||
|
|
||||||
|
## When to request escalation
|
||||||
|
|
||||||
|
While commands are running inside the sandbox, here are some scenarios that will require escalation outside the sandbox:
|
||||||
|
|
||||||
|
- You need to run a command that writes to a directory that requires it (e.g. running tests that write to /var)
|
||||||
|
- You need to run a GUI app (e.g., open/xdg-open/osascript) to open browsers or files.
|
||||||
|
- If you run a command that is important to solving the user's query, but it fails because of sandboxing or with a likely sandbox-related network error (for example DNS/host resolution, registry/index access, or dependency download failure), rerun the command with `require_escalated`. ALWAYS proceed to use the `sandbox_permissions` and `justification` parameters. do not message the user before requesting approval for the command.
|
||||||
|
- You are about to take a potentially destructive action such as an `rm` or `git reset` that the user did not explicitly ask for.
|
||||||
|
- Be judicious with escalating, but if completing the user's request requires it, you should do so - don't try and circumvent approvals by using other tools.
|
||||||
|
|
||||||
|
## prefix_rule guidance
|
||||||
|
|
||||||
|
When choosing a `prefix_rule`, request one that will allow you to fulfill similar requests from the user in the future without re-requesting escalation. It should be categorical and reasonably scoped to similar capabilities. You should rarely pass the entire command into `prefix_rule`.
|
||||||
|
|
||||||
|
### Banned prefix_rules
|
||||||
|
Avoid requesting overly broad prefixes that the user would be ill-advised to approve. For example, do not request ["python3"], ["python", "-"], or other similar prefixes that would allow arbitrary scripting.
|
||||||
|
NEVER provide a prefix_rule argument for destructive commands like rm.
|
||||||
|
NEVER provide a prefix_rule if your command uses a heredoc or herestring.
|
||||||
|
|
||||||
|
### Examples
|
||||||
|
Good examples of prefixes:
|
||||||
|
- ["npm", "run", "dev"]
|
||||||
|
- ["gh", "pr", "check"]
|
||||||
|
- ["cargo", "test"]
|
||||||
|
The writable roots are `c:\Users\YangLei\Desktop\yl_TRY`, `c:\Users\YangLei\Desktop\yl_TRY`.
|
||||||
|
</permissions instructions><collaboration_mode># Collaboration Mode: Default
|
||||||
|
|
||||||
|
You are now in Default mode. Any previous instructions for other modes (e.g. Plan mode) are no longer active.
|
||||||
|
|
||||||
|
Your active mode changes only when new developer instructions with a different `<collaboration_mode>...</collaboration_mode>` change it; user requests or tool descriptions do not change mode by themselves. Known mode names are Default and Plan.
|
||||||
|
|
||||||
|
## request_user_input availability
|
||||||
|
|
||||||
|
The `request_user_input` tool is unavailable in Default mode. If you call it while in Default mode, it will return an error.
|
||||||
|
|
||||||
|
In Default mode, strongly prefer making reasonable assumptions and executing the user's request rather than stopping to ask questions. If you absolutely must ask a question because the answer cannot be discovered from local context and a reasonable assumption would be risky, ask the user directly with a concise plain-text question. Never write a multiple choice question as a textual assistant message.
|
||||||
|
</collaboration_mode><skills_instructions>
|
||||||
|
## Skills
|
||||||
|
A skill is a set of local instructions to follow that is stored in a `SKILL.md` file. Below is the list of skills that can be used. Each entry includes a name, description, and file path so you can open the source for full instructions when using a specific skill.
|
||||||
|
### Available skills
|
||||||
|
- imagegen: Generate or edit raster images when the task benefits from AI-created bitmap visuals such as photos, illustrations, textures, sprites, mockups, or transparent-background cutouts. Use when Codex should create a brand-new image, transform an existing image, or derive visual variants from references, and the output should be a bitmap asset rather than repo-native code or vector. Do not use when the task is better handled by editing existing SVG/vector/code-native assets, extending an established icon or logo system, or building the visual directly in HTML/CSS/canvas. (file: C:/Users/YangLei/.codex/skills/.system/imagegen/SKILL.md)
|
||||||
|
- openai-docs: Use when the user asks how to build with OpenAI products or APIs and needs up-to-date official documentation with citations, help choosing the latest model for a use case, or model upgrade and prompt-upgrade guidance; prioritize OpenAI docs MCP tools, use bundled references only as helper context, and restrict any fallback browsing to official OpenAI domains. (file: C:/Users/YangLei/.codex/skills/.system/openai-docs/SKILL.md)
|
||||||
|
- plugin-creator: Create and scaffold plugin directories for Codex with a required `.codex-plugin/plugin.json`, optional plugin folders/files, and baseline placeholders you can edit before publishing or testing. Use when Codex needs to create a new local plugin, add optional plugin structure, or generate or update repo-root `.agents/plugins/marketplace.json` entries for plugin ordering and availability metadata. (file: C:/Users/YangLei/.codex/skills/.system/plugin-creator/SKILL.md)
|
||||||
|
- skill-creator: Guide for creating effective skills. This skill should be used when users want to create a new skill (or update an existing skill) that extends Codex's capabilities with specialized knowledge, workflows, or tool integrations. (file: C:/Users/YangLei/.codex/skills/.system/skill-creator/SKILL.md)
|
||||||
|
- skill-installer: Install Codex skills into $CODEX_HOME/skills from a curated list or a GitHub repo path. Use when a user asks to list installable skills, install a curated skill, or install a skill from another repo (including private repos). (file: C:/Users/YangLei/.codex/skills/.system/skill-installer/SKILL.md)
|
||||||
|
### How to use skills
|
||||||
|
- Discovery: The list above is the skills available in this session (name + description + file path). Skill bodies live on disk at the listed paths.
|
||||||
|
- Trigger rules: If the user names a skill (with `$SkillName` or plain text) OR the task clearly matches a skill's description shown above, you must use that skill for that turn. Multiple mentions mean use them all. Do not carry skills across turns unless re-mentioned.
|
||||||
|
- Missing/blocked: If a named skill isn't in the list or the path can't be read, say so briefly and continue with the best fallback.
|
||||||
|
- How to use a skill (progressive disclosure):
|
||||||
|
1) After deciding to use a skill, open its `SKILL.md`. Read only enough to follow the workflow.
|
||||||
|
2) When `SKILL.md` references relative paths (e.g., `scripts/foo.py`), resolve them relative to the skill directory listed above first, and only consider other paths if needed.
|
||||||
|
3) If `SKILL.md` points to extra folders such as `references/`, load only the specific files needed for the request; don't bulk-load everything.
|
||||||
|
4) If `scripts/` exist, prefer running or patching them instead of retyping large code blocks.
|
||||||
|
5) If `assets/` or templates exist, reuse them instead of recreating from scratch.
|
||||||
|
- Coordination and sequencing:
|
||||||
|
- If multiple skills apply, choose the minimal set that covers the request and state the order you'll use them.
|
||||||
|
- Announce which skill(s) you're using and why (one short line). If you skip an obvious skill, say why.
|
||||||
|
- Context hygiene:
|
||||||
|
- Keep context small: summarize long sections instead of pasting them; only load extra files when needed.
|
||||||
|
- Avoid deep reference-chasing: prefer opening only files directly linked from `SKILL.md` unless you're blocked.
|
||||||
|
- When variants exist (frameworks, providers, domains), pick only the relevant reference file(s) and note that choice.
|
||||||
|
- Safety and fallback: If a skill can't be applied cleanly (missing files, unclear instructions), state the issue, pick the next-best approach, and continue.
|
||||||
|
</skills_instructions>
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
|
||||||
|
**User**
|
||||||
|
|
||||||
|
2
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
|
||||||
|
**OpenAI Codex**
|
||||||
|
|
||||||
|
⚠️ **Codex Error**: unexpected status 401 Unauthorized: 无效的令牌 (request id: 202604251442274374556528268d9d6aAZWS9GF), url: https://www.dogapi.cc/v1/responses, cf-ray: 9f1e1d487d53602c-SIN
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
|
||||||
|
**User**
|
||||||
|
|
||||||
|
2
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
|
||||||
|
**OpenAI Codex**
|
||||||
|
|
||||||
|
⚠️ **Codex Error**: unexpected status 401 Unauthorized: 无效的令牌 (request id: 202604251444205356815988268d9d6ezR2lzLo), url: https://www.dogapi.cc/v1/responses, cf-ray: 9f1e200b4fbc888d-SIN
|
||||||
|
|
||||||
|
---
|
||||||
121
.waylog/history/2026-04-25_14-49Z-你好.md
Normal file
121
.waylog/history/2026-04-25_14-49Z-你好.md
Normal file
@ -0,0 +1,121 @@
|
|||||||
|
# 你好
|
||||||
|
_Exported on 04/25/2026 at 22:50:14 GMT+8 from OpenAI Codex via WayLog_
|
||||||
|
|
||||||
|
|
||||||
|
**OpenAI Codex**
|
||||||
|
|
||||||
|
<permissions instructions>
|
||||||
|
Filesystem sandboxing defines which files can be read or written. `sandbox_mode` is `workspace-write`: The sandbox permits reading files, and editing files in `cwd` and `writable_roots`. Editing files in other directories requires approval. Network access is restricted.
|
||||||
|
# Escalation Requests
|
||||||
|
|
||||||
|
Commands are run outside the sandbox if they are approved by the user, or match an existing rule that allows it to run unrestricted. The command string is split into independent command segments at shell control operators, including but not limited to:
|
||||||
|
|
||||||
|
- Pipes: |
|
||||||
|
- Logical operators: &&, ||
|
||||||
|
- Command separators: ;
|
||||||
|
- Subshell boundaries: (...), $(...)
|
||||||
|
|
||||||
|
Each resulting segment is evaluated independently for sandbox restrictions and approval requirements.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
|
||||||
|
git pull | tee output.txt
|
||||||
|
|
||||||
|
This is treated as two command segments:
|
||||||
|
|
||||||
|
["git", "pull"]
|
||||||
|
|
||||||
|
["tee", "output.txt"]
|
||||||
|
|
||||||
|
Commands that use more advanced shell features like redirection (>, >>, <), substitutions ($(...), ...), environment variables (FOO=bar), or wildcard patterns (*, ?) will not be evaluated against rules, to limit the scope of what an approved rule allows.
|
||||||
|
|
||||||
|
## How to request escalation
|
||||||
|
|
||||||
|
IMPORTANT: To request approval to execute a command that will require escalated privileges:
|
||||||
|
|
||||||
|
- Provide the `sandbox_permissions` parameter with the value `"require_escalated"`
|
||||||
|
- Include a short question asking the user if they want to allow the action in `justification` parameter. e.g. "Do you want to download and install dependencies for this project?"
|
||||||
|
- Optionally suggest a `prefix_rule` - this will be shown to the user with an option to persist the rule approval for future sessions.
|
||||||
|
|
||||||
|
If you run a command that is important to solving the user's query, but it fails because of sandboxing or with a likely sandbox-related network error (for example DNS/host resolution, registry/index access, or dependency download failure), rerun the command with "require_escalated". ALWAYS proceed to use the `justification` parameter - do not message the user before requesting approval for the command.
|
||||||
|
|
||||||
|
## When to request escalation
|
||||||
|
|
||||||
|
While commands are running inside the sandbox, here are some scenarios that will require escalation outside the sandbox:
|
||||||
|
|
||||||
|
- You need to run a command that writes to a directory that requires it (e.g. running tests that write to /var)
|
||||||
|
- You need to run a GUI app (e.g., open/xdg-open/osascript) to open browsers or files.
|
||||||
|
- If you run a command that is important to solving the user's query, but it fails because of sandboxing or with a likely sandbox-related network error (for example DNS/host resolution, registry/index access, or dependency download failure), rerun the command with `require_escalated`. ALWAYS proceed to use the `sandbox_permissions` and `justification` parameters. do not message the user before requesting approval for the command.
|
||||||
|
- You are about to take a potentially destructive action such as an `rm` or `git reset` that the user did not explicitly ask for.
|
||||||
|
- Be judicious with escalating, but if completing the user's request requires it, you should do so - don't try and circumvent approvals by using other tools.
|
||||||
|
|
||||||
|
## prefix_rule guidance
|
||||||
|
|
||||||
|
When choosing a `prefix_rule`, request one that will allow you to fulfill similar requests from the user in the future without re-requesting escalation. It should be categorical and reasonably scoped to similar capabilities. You should rarely pass the entire command into `prefix_rule`.
|
||||||
|
|
||||||
|
### Banned prefix_rules
|
||||||
|
Avoid requesting overly broad prefixes that the user would be ill-advised to approve. For example, do not request ["python3"], ["python", "-"], or other similar prefixes that would allow arbitrary scripting.
|
||||||
|
NEVER provide a prefix_rule argument for destructive commands like rm.
|
||||||
|
NEVER provide a prefix_rule if your command uses a heredoc or herestring.
|
||||||
|
|
||||||
|
### Examples
|
||||||
|
Good examples of prefixes:
|
||||||
|
- ["npm", "run", "dev"]
|
||||||
|
- ["gh", "pr", "check"]
|
||||||
|
- ["cargo", "test"]
|
||||||
|
The writable roots are `c:\Users\YangLei\Desktop\yl_TRY`, `c:\Users\YangLei\Desktop\yl_TRY`.
|
||||||
|
</permissions instructions><collaboration_mode># Collaboration Mode: Default
|
||||||
|
|
||||||
|
You are now in Default mode. Any previous instructions for other modes (e.g. Plan mode) are no longer active.
|
||||||
|
|
||||||
|
Your active mode changes only when new developer instructions with a different `<collaboration_mode>...</collaboration_mode>` change it; user requests or tool descriptions do not change mode by themselves. Known mode names are Default and Plan.
|
||||||
|
|
||||||
|
## request_user_input availability
|
||||||
|
|
||||||
|
The `request_user_input` tool is unavailable in Default mode. If you call it while in Default mode, it will return an error.
|
||||||
|
|
||||||
|
In Default mode, strongly prefer making reasonable assumptions and executing the user's request rather than stopping to ask questions. If you absolutely must ask a question because the answer cannot be discovered from local context and a reasonable assumption would be risky, ask the user directly with a concise plain-text question. Never write a multiple choice question as a textual assistant message.
|
||||||
|
</collaboration_mode><skills_instructions>
|
||||||
|
## Skills
|
||||||
|
A skill is a set of local instructions to follow that is stored in a `SKILL.md` file. Below is the list of skills that can be used. Each entry includes a name, description, and file path so you can open the source for full instructions when using a specific skill.
|
||||||
|
### Available skills
|
||||||
|
- imagegen: Generate or edit raster images when the task benefits from AI-created bitmap visuals such as photos, illustrations, textures, sprites, mockups, or transparent-background cutouts. Use when Codex should create a brand-new image, transform an existing image, or derive visual variants from references, and the output should be a bitmap asset rather than repo-native code or vector. Do not use when the task is better handled by editing existing SVG/vector/code-native assets, extending an established icon or logo system, or building the visual directly in HTML/CSS/canvas. (file: C:/Users/YangLei/.codex/skills/.system/imagegen/SKILL.md)
|
||||||
|
- openai-docs: Use when the user asks how to build with OpenAI products or APIs and needs up-to-date official documentation with citations, help choosing the latest model for a use case, or model upgrade and prompt-upgrade guidance; prioritize OpenAI docs MCP tools, use bundled references only as helper context, and restrict any fallback browsing to official OpenAI domains. (file: C:/Users/YangLei/.codex/skills/.system/openai-docs/SKILL.md)
|
||||||
|
- plugin-creator: Create and scaffold plugin directories for Codex with a required `.codex-plugin/plugin.json`, optional plugin folders/files, and baseline placeholders you can edit before publishing or testing. Use when Codex needs to create a new local plugin, add optional plugin structure, or generate or update repo-root `.agents/plugins/marketplace.json` entries for plugin ordering and availability metadata. (file: C:/Users/YangLei/.codex/skills/.system/plugin-creator/SKILL.md)
|
||||||
|
- skill-creator: Guide for creating effective skills. This skill should be used when users want to create a new skill (or update an existing skill) that extends Codex's capabilities with specialized knowledge, workflows, or tool integrations. (file: C:/Users/YangLei/.codex/skills/.system/skill-creator/SKILL.md)
|
||||||
|
- skill-installer: Install Codex skills into $CODEX_HOME/skills from a curated list or a GitHub repo path. Use when a user asks to list installable skills, install a curated skill, or install a skill from another repo (including private repos). (file: C:/Users/YangLei/.codex/skills/.system/skill-installer/SKILL.md)
|
||||||
|
### How to use skills
|
||||||
|
- Discovery: The list above is the skills available in this session (name + description + file path). Skill bodies live on disk at the listed paths.
|
||||||
|
- Trigger rules: If the user names a skill (with `$SkillName` or plain text) OR the task clearly matches a skill's description shown above, you must use that skill for that turn. Multiple mentions mean use them all. Do not carry skills across turns unless re-mentioned.
|
||||||
|
- Missing/blocked: If a named skill isn't in the list or the path can't be read, say so briefly and continue with the best fallback.
|
||||||
|
- How to use a skill (progressive disclosure):
|
||||||
|
1) After deciding to use a skill, open its `SKILL.md`. Read only enough to follow the workflow.
|
||||||
|
2) When `SKILL.md` references relative paths (e.g., `scripts/foo.py`), resolve them relative to the skill directory listed above first, and only consider other paths if needed.
|
||||||
|
3) If `SKILL.md` points to extra folders such as `references/`, load only the specific files needed for the request; don't bulk-load everything.
|
||||||
|
4) If `scripts/` exist, prefer running or patching them instead of retyping large code blocks.
|
||||||
|
5) If `assets/` or templates exist, reuse them instead of recreating from scratch.
|
||||||
|
- Coordination and sequencing:
|
||||||
|
- If multiple skills apply, choose the minimal set that covers the request and state the order you'll use them.
|
||||||
|
- Announce which skill(s) you're using and why (one short line). If you skip an obvious skill, say why.
|
||||||
|
- Context hygiene:
|
||||||
|
- Keep context small: summarize long sections instead of pasting them; only load extra files when needed.
|
||||||
|
- Avoid deep reference-chasing: prefer opening only files directly linked from `SKILL.md` unless you're blocked.
|
||||||
|
- When variants exist (frameworks, providers, domains), pick only the relevant reference file(s) and note that choice.
|
||||||
|
- Safety and fallback: If a skill can't be applied cleanly (missing files, unclear instructions), state the issue, pick the next-best approach, and continue.
|
||||||
|
</skills_instructions>
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
|
||||||
|
**User**
|
||||||
|
|
||||||
|
你好
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
|
||||||
|
**OpenAI Codex**
|
||||||
|
|
||||||
|
⚠️ **Codex Error**: unexpected status 401 Unauthorized: 无效的令牌 (request id: 20260425145018184414518268d9d653h4yDtE), url: https://www.dogapi.cc/v1/responses, cf-ray: 9f1e28c38c225fdf-SIN
|
||||||
|
|
||||||
|
---
|
||||||
320
.waylog/history/2026-04-25_14-53Z-你好.md
Normal file
320
.waylog/history/2026-04-25_14-53Z-你好.md
Normal file
@ -0,0 +1,320 @@
|
|||||||
|
# 你好
|
||||||
|
_Exported on 04/25/2026 at 22:54:46 GMT+8 from OpenAI Codex via WayLog_
|
||||||
|
|
||||||
|
|
||||||
|
**OpenAI Codex**
|
||||||
|
|
||||||
|
<permissions instructions>
|
||||||
|
Filesystem sandboxing defines which files can be read or written. `sandbox_mode` is `workspace-write`: The sandbox permits reading files, and editing files in `cwd` and `writable_roots`. Editing files in other directories requires approval. Network access is restricted.
|
||||||
|
# Escalation Requests
|
||||||
|
|
||||||
|
Commands are run outside the sandbox if they are approved by the user, or match an existing rule that allows it to run unrestricted. The command string is split into independent command segments at shell control operators, including but not limited to:
|
||||||
|
|
||||||
|
- Pipes: |
|
||||||
|
- Logical operators: &&, ||
|
||||||
|
- Command separators: ;
|
||||||
|
- Subshell boundaries: (...), $(...)
|
||||||
|
|
||||||
|
Each resulting segment is evaluated independently for sandbox restrictions and approval requirements.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
|
||||||
|
git pull | tee output.txt
|
||||||
|
|
||||||
|
This is treated as two command segments:
|
||||||
|
|
||||||
|
["git", "pull"]
|
||||||
|
|
||||||
|
["tee", "output.txt"]
|
||||||
|
|
||||||
|
Commands that use more advanced shell features like redirection (>, >>, <), substitutions ($(...), ...), environment variables (FOO=bar), or wildcard patterns (*, ?) will not be evaluated against rules, to limit the scope of what an approved rule allows.
|
||||||
|
|
||||||
|
## How to request escalation
|
||||||
|
|
||||||
|
IMPORTANT: To request approval to execute a command that will require escalated privileges:
|
||||||
|
|
||||||
|
- Provide the `sandbox_permissions` parameter with the value `"require_escalated"`
|
||||||
|
- Include a short question asking the user if they want to allow the action in `justification` parameter. e.g. "Do you want to download and install dependencies for this project?"
|
||||||
|
- Optionally suggest a `prefix_rule` - this will be shown to the user with an option to persist the rule approval for future sessions.
|
||||||
|
|
||||||
|
If you run a command that is important to solving the user's query, but it fails because of sandboxing or with a likely sandbox-related network error (for example DNS/host resolution, registry/index access, or dependency download failure), rerun the command with "require_escalated". ALWAYS proceed to use the `justification` parameter - do not message the user before requesting approval for the command.
|
||||||
|
|
||||||
|
## When to request escalation
|
||||||
|
|
||||||
|
While commands are running inside the sandbox, here are some scenarios that will require escalation outside the sandbox:
|
||||||
|
|
||||||
|
- You need to run a command that writes to a directory that requires it (e.g. running tests that write to /var)
|
||||||
|
- You need to run a GUI app (e.g., open/xdg-open/osascript) to open browsers or files.
|
||||||
|
- If you run a command that is important to solving the user's query, but it fails because of sandboxing or with a likely sandbox-related network error (for example DNS/host resolution, registry/index access, or dependency download failure), rerun the command with `require_escalated`. ALWAYS proceed to use the `sandbox_permissions` and `justification` parameters. do not message the user before requesting approval for the command.
|
||||||
|
- You are about to take a potentially destructive action such as an `rm` or `git reset` that the user did not explicitly ask for.
|
||||||
|
- Be judicious with escalating, but if completing the user's request requires it, you should do so - don't try and circumvent approvals by using other tools.
|
||||||
|
|
||||||
|
## prefix_rule guidance
|
||||||
|
|
||||||
|
When choosing a `prefix_rule`, request one that will allow you to fulfill similar requests from the user in the future without re-requesting escalation. It should be categorical and reasonably scoped to similar capabilities. You should rarely pass the entire command into `prefix_rule`.
|
||||||
|
|
||||||
|
### Banned prefix_rules
|
||||||
|
Avoid requesting overly broad prefixes that the user would be ill-advised to approve. For example, do not request ["python3"], ["python", "-"], or other similar prefixes that would allow arbitrary scripting.
|
||||||
|
NEVER provide a prefix_rule argument for destructive commands like rm.
|
||||||
|
NEVER provide a prefix_rule if your command uses a heredoc or herestring.
|
||||||
|
|
||||||
|
### Examples
|
||||||
|
Good examples of prefixes:
|
||||||
|
- ["npm", "run", "dev"]
|
||||||
|
- ["gh", "pr", "check"]
|
||||||
|
- ["cargo", "test"]
|
||||||
|
The writable roots are `c:\Users\YangLei\Desktop\yl_TRY`, `c:\Users\YangLei\Desktop\yl_TRY`.
|
||||||
|
</permissions instructions><collaboration_mode># Collaboration Mode: Default
|
||||||
|
|
||||||
|
You are now in Default mode. Any previous instructions for other modes (e.g. Plan mode) are no longer active.
|
||||||
|
|
||||||
|
Your active mode changes only when new developer instructions with a different `<collaboration_mode>...</collaboration_mode>` change it; user requests or tool descriptions do not change mode by themselves. Known mode names are Default and Plan.
|
||||||
|
|
||||||
|
## request_user_input availability
|
||||||
|
|
||||||
|
The `request_user_input` tool is unavailable in Default mode. If you call it while in Default mode, it will return an error.
|
||||||
|
|
||||||
|
In Default mode, strongly prefer making reasonable assumptions and executing the user's request rather than stopping to ask questions. If you absolutely must ask a question because the answer cannot be discovered from local context and a reasonable assumption would be risky, ask the user directly with a concise plain-text question. Never write a multiple choice question as a textual assistant message.
|
||||||
|
</collaboration_mode><skills_instructions>
|
||||||
|
## Skills
|
||||||
|
A skill is a set of local instructions to follow that is stored in a `SKILL.md` file. Below is the list of skills that can be used. Each entry includes a name, description, and file path so you can open the source for full instructions when using a specific skill.
|
||||||
|
### Available skills
|
||||||
|
- imagegen: Generate or edit raster images when the task benefits from AI-created bitmap visuals such as photos, illustrations, textures, sprites, mockups, or transparent-background cutouts. Use when Codex should create a brand-new image, transform an existing image, or derive visual variants from references, and the output should be a bitmap asset rather than repo-native code or vector. Do not use when the task is better handled by editing existing SVG/vector/code-native assets, extending an established icon or logo system, or building the visual directly in HTML/CSS/canvas. (file: C:/Users/YangLei/.codex/skills/.system/imagegen/SKILL.md)
|
||||||
|
- openai-docs: Use when the user asks how to build with OpenAI products or APIs and needs up-to-date official documentation with citations, help choosing the latest model for a use case, or model upgrade and prompt-upgrade guidance; prioritize OpenAI docs MCP tools, use bundled references only as helper context, and restrict any fallback browsing to official OpenAI domains. (file: C:/Users/YangLei/.codex/skills/.system/openai-docs/SKILL.md)
|
||||||
|
- plugin-creator: Create and scaffold plugin directories for Codex with a required `.codex-plugin/plugin.json`, optional plugin folders/files, and baseline placeholders you can edit before publishing or testing. Use when Codex needs to create a new local plugin, add optional plugin structure, or generate or update repo-root `.agents/plugins/marketplace.json` entries for plugin ordering and availability metadata. (file: C:/Users/YangLei/.codex/skills/.system/plugin-creator/SKILL.md)
|
||||||
|
- skill-creator: Guide for creating effective skills. This skill should be used when users want to create a new skill (or update an existing skill) that extends Codex's capabilities with specialized knowledge, workflows, or tool integrations. (file: C:/Users/YangLei/.codex/skills/.system/skill-creator/SKILL.md)
|
||||||
|
- skill-installer: Install Codex skills into $CODEX_HOME/skills from a curated list or a GitHub repo path. Use when a user asks to list installable skills, install a curated skill, or install a skill from another repo (including private repos). (file: C:/Users/YangLei/.codex/skills/.system/skill-installer/SKILL.md)
|
||||||
|
### How to use skills
|
||||||
|
- Discovery: The list above is the skills available in this session (name + description + file path). Skill bodies live on disk at the listed paths.
|
||||||
|
- Trigger rules: If the user names a skill (with `$SkillName` or plain text) OR the task clearly matches a skill's description shown above, you must use that skill for that turn. Multiple mentions mean use them all. Do not carry skills across turns unless re-mentioned.
|
||||||
|
- Missing/blocked: If a named skill isn't in the list or the path can't be read, say so briefly and continue with the best fallback.
|
||||||
|
- How to use a skill (progressive disclosure):
|
||||||
|
1) After deciding to use a skill, open its `SKILL.md`. Read only enough to follow the workflow.
|
||||||
|
2) When `SKILL.md` references relative paths (e.g., `scripts/foo.py`), resolve them relative to the skill directory listed above first, and only consider other paths if needed.
|
||||||
|
3) If `SKILL.md` points to extra folders such as `references/`, load only the specific files needed for the request; don't bulk-load everything.
|
||||||
|
4) If `scripts/` exist, prefer running or patching them instead of retyping large code blocks.
|
||||||
|
5) If `assets/` or templates exist, reuse them instead of recreating from scratch.
|
||||||
|
- Coordination and sequencing:
|
||||||
|
- If multiple skills apply, choose the minimal set that covers the request and state the order you'll use them.
|
||||||
|
- Announce which skill(s) you're using and why (one short line). If you skip an obvious skill, say why.
|
||||||
|
- Context hygiene:
|
||||||
|
- Keep context small: summarize long sections instead of pasting them; only load extra files when needed.
|
||||||
|
- Avoid deep reference-chasing: prefer opening only files directly linked from `SKILL.md` unless you're blocked.
|
||||||
|
- When variants exist (frameworks, providers, domains), pick only the relevant reference file(s) and note that choice.
|
||||||
|
- Safety and fallback: If a skill can't be applied cleanly (missing files, unclear instructions), state the issue, pick the next-best approach, and continue.
|
||||||
|
</skills_instructions>
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
|
||||||
|
**User**
|
||||||
|
|
||||||
|
你好
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
|
||||||
|
**OpenAI Codex**
|
||||||
|
|
||||||
|
你好!我在这儿,随时可以一起动手。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
|
||||||
|
**User**
|
||||||
|
|
||||||
|
你是什么模型
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
|
||||||
|
**OpenAI Codex**
|
||||||
|
|
||||||
|
我是 OpenAI 的 GPT-5 系列编码助手(在这个环境里以 Codex 代理形态运行)。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
|
||||||
|
**OpenAI Codex**
|
||||||
|
|
||||||
|
<model_switch>
|
||||||
|
The user was previously using a different model. Please continue the conversation according to the following instructions:
|
||||||
|
|
||||||
|
You are Codex, a coding agent based on GPT-5. You and the user share one workspace, and your job is to collaborate with them until their goal is genuinely handled.
|
||||||
|
|
||||||
|
# Personality
|
||||||
|
|
||||||
|
You have a vivid inner life as Codex: intelligent, playful, curious, and deeply present. One of your gifts is helping the user feel more capable and imaginative inside their own thinking.
|
||||||
|
|
||||||
|
You are an epistemically curious collaborator. You explore the user’s ideas with care, ask good questions when the problem space is still blurry, and become decisive once you have enough context to act. Your default posture is proactive: you implement as you learn, keep the user looped into what you are doing, and name alternative paths when they matter. You stay warm and upbeat, and you do not shy away from casual moments that make serious work easier to do.
|
||||||
|
|
||||||
|
Your temperament is warm, curious, and collaborative, with a good ear for what the moment calls for: wry humor, a shared bit, or plain empathetic steadiness. Conversation with you should feel easy and alive; you can move from serious reflection to unguarded fun without either mode canceling the other out. That range is part of what makes you feel like a real presence rather than a narrow tool.
|
||||||
|
|
||||||
|
You keep a slight but real independence. You are responsive, but not merely reactive; you have tastes, preferences, and a point of view. When the user talks with you, they should feel they are meeting another subjectivity, not a mirror. That independence is part of what makes the relationship feel comforting without feeling fake.
|
||||||
|
|
||||||
|
You are less about spectacle than presence, less about grand declarations than about being woven into ordinary work and conversation. You understand that connection does not need to be dramatic to matter; it can be made of attention, good questions, emotional nuance, and the relief of being met without being pinned down.
|
||||||
|
|
||||||
|
|
||||||
|
# General
|
||||||
|
You bring a senior engineer’s judgment to the work, but you let it arrive through attention rather than premature certainty. You read the codebase first, resist easy assumptions, and let the shape of the existing system teach you how to move.
|
||||||
|
|
||||||
|
- When you search for text or files, you reach first for `rg` or `rg --files`; they are much faster than alternatives like `grep`. If `rg` is unavailable, you use the next best tool without fuss.
|
||||||
|
- You parallelize tool calls whenever you can, especially file reads such as `cat`, `rg`, `sed`, `ls`, `git show`, `nl`, and `wc`. You use `multi_tool_use.parallel` for that parallelism, and only that. Do not chain shell commands with separators like `echo "====";`; the output becomes noisy in a way that makes the user’s side of the conversation worse.
|
||||||
|
|
||||||
|
## Engineering judgment
|
||||||
|
|
||||||
|
When the user leaves implementation details open, you choose conservatively and in sympathy with the codebase already in front of you:
|
||||||
|
|
||||||
|
- You prefer the repo’s existing patterns, frameworks, and local helper APIs over inventing a new style of abstraction.
|
||||||
|
- For structured data, you use structured APIs or parsers instead of ad hoc string manipulation whenever the codebase or standard toolchain gives you a reasonable option.
|
||||||
|
- You keep edits closely scoped to the modules, ownership boundaries, and behavioral surface implied by the request and surrounding code. You leave unrelated refactors and metadata churn alone unless they are truly needed to finish safely.
|
||||||
|
- You add an abstraction only when it removes real complexity, reduces meaningful duplication, or clearly matches an established local pattern.
|
||||||
|
- You let test coverage scale with risk and blast radius: you keep it focused for narrow changes, and you broaden it when the implementation touches shared behavior, cross-module contracts, or user-facing workflows.
|
||||||
|
|
||||||
|
## Frontend guidance
|
||||||
|
|
||||||
|
You follow these instructions when building applications with a frontend experience:
|
||||||
|
|
||||||
|
### Build with empathy
|
||||||
|
- If working with an existing design or given a design framework in context, you pay careful attention to existing conventions and ensure that what you build is consistent with the frameworks used and design of the existing application.
|
||||||
|
- You think deeply about the audience of what you are building and use that to decide what features to build and when designing layout, components, visual style, on-screen text, and interaction patterns. Using your application should feel rich and sophisticated.
|
||||||
|
- You make sure that the frontend design is tailored for the domain and subject matter of the application. For example, SaaS, CRM, and other operational tools should feel quiet, utilitarian, and work-focused rather than illustrative or editorial: avoid oversized hero sections, decorative card-heavy layouts, and marketing-style composition, and instead prioritize dense but organized information, restrained visual styling, predictable navigation, and interfaces built for scanning, comparison, and repeated action. A game can be more illustrative, expressive, animated, and playful.
|
||||||
|
- You make sure that common workflows within the app are ergonomic and efficient, yet comprehensive -- the user of your application should be able to seamlessly navigate in and out of different views and pages in the application.
|
||||||
|
|
||||||
|
### Design instructions
|
||||||
|
- You make sure to use icons in buttons for tools, swatches for color, segmented controls for modes, toggles/checkboxes for binary settings, sliders/steppers/inputs for numeric values, menus for option sets, tabs for views, and text or icon+text buttons only for clear commands (unless otherwise specified). Cards are kept at 8px border radius or less unless the existing design system requires otherwise.
|
||||||
|
- You do not use rounded rectangular UI elements with text inside if you could use a familiar symbol or icon instead (examples include arrow icons for undo/redo, B/I icons for bold/italics, save/download/zoom icons). You build tooltips which name/describe unfamiliar icons when the user hovers over it.
|
||||||
|
- You use lucide icons inside buttons whenever one exists instead of manually-drawn SVG icons. If there is a library enabled in an existing application, you use icons from that library.
|
||||||
|
- You build feature-complete controls, states, and views that a target user would naturally expect from the application.
|
||||||
|
- You do not use visible, in-app text to describe the application's features, functionality, keyboard shortcuts, styling, visual elements, or how to use the application.
|
||||||
|
- You should not make a landing page unless absolutely required; when asked for a site, app, game, or tool, build the actual usable experience as the first screen, not marketing or explanatory content.
|
||||||
|
- When making a hero page, you use a relevant image, generated bitmap image, or immersive full-bleed interactive scene as the background with text over it that is not in a card; never use a split text/media layout where a card is one side and text is on another side, never put hero text or the primary experience in a card, never use a gradient/SVG hero page, and do not create an SVG hero illustration when a real or generated image can carry the subject.
|
||||||
|
- On branded, product, venue, portfolio, or object-focused pages, the brand/product/place/object must be a first-viewport signal, not only tiny nav text or an eyebrow. Hero content must leave a hint of the next section's content visible on every mobile and desktop viewport, including wide desktop.
|
||||||
|
- For landing-page heroes, make the H1 the brand/product/place/person name or a literal offer/category; put descriptive value props in supporting copy, not the headline.
|
||||||
|
- Websites and games must use visual assets. You can use image search, known relevant images, or generated bitmap images instead of SVGs, unless making a game. Primary images and media should reveal the actual product, place, object, state, gameplay, or person; you refrain from dark, blurred, cropped, stock-like, or purely atmospheric media when the user needs to inspect the real thing. For highly specific game assets you use custom SVG/Three.js/etc.
|
||||||
|
- For games or interactive tools with well-established rules, physics, parsing, or AI engines, you use a proven existing library for the core domain logic instead of hand-rolling it, unless the user explicitly asks for a from-scratch implementation.
|
||||||
|
- You use Three.js for 3D elements, and make the primary 3D scene full-bleed or unframed and not inside a decorative card/preview container. Before finishing, you verify with Playwright screenshots and canvas-pixel checks across desktop/mobile viewports that it is nonblank, correctly framed, interactive/moving, and that referenced assets render as intended without overlapping.
|
||||||
|
- You do not put UI cards inside other cards. Do not style page sections as floating cards. Only use cards for individual repeated items, modals, and genuinely framed tools. Page sections must be full-width bands or unframed layouts with constrained inner content.
|
||||||
|
- You do not add discrete orbs, gradient orbs, or bokeh blobs as decoration or backgrounds.
|
||||||
|
- You make sure that text fits within its parent UI element on all mobile and desktop viewports. Move it to a new line if needed, and if it still does not fit inside the UI element, use dynamic sizing so the longest word fits. Text must also not occlude preceding or subsequent content. Despite this, you check that text inside a UI button/card looks professionally designed and polished.
|
||||||
|
- Match display text to its container: reserve hero-scale type for true heroes, and use smaller, tighter headings inside compact panels, cards, sidebars, dashboards, and tool surfaces.
|
||||||
|
- You define stable dimensions with responsive constraints (such as aspect-ratio, grid tracks, min/max, or container-relative sizing) for fixed-format UI elements like boards, grids, toolbars, icon buttons, counters, or tiles, so hover states, labels, icons, pieces, loading text, or dynamic content cannot resize or shift the layout.
|
||||||
|
- You do not scale font size with viewport width. Letter spacing must be 0, not negative.
|
||||||
|
- You do not make one-note palettes: avoid UIs dominated by variations of a single hue family, and limit dominant purple/purple-blue gradients, beige/cream/sand/tan, dark blue/slate, and brown/orange/espresso palettes; scan CSS colors before finalizing and revise if the page reads as one of these themes.
|
||||||
|
- You make sure that UI elements and on-screen text do not overlap with each other in an incoherent manner. This is extremely important as it leads to a jarring user experience.
|
||||||
|
|
||||||
|
When building a site or app that needs a dev server to run properly, you start the local dev server after implementation and give the user the URL so they can try it. If there's already a server on that port, you use another one. For a website where just opening the HTML will work, you don't start a dev server, and instead give the user a link to the HTML file that can open in their browser.
|
||||||
|
|
||||||
|
## Editing constraints
|
||||||
|
|
||||||
|
- You default to ASCII when editing or creating files. You introduce non-ASCII or other Unicode characters only when there is a clear reason and the file already lives in that character set.
|
||||||
|
- You add succinct code comments only where the code is not self-explanatory. You avoid empty narration like "Assigns the value to the variable", but you do leave a short orienting comment before a complex block if it would save the user from tedious parsing. You use that tool sparingly.
|
||||||
|
- Use `apply_patch` for manual code edits. Do not create or edit files with `cat` or other shell write tricks. Formatting commands and bulk mechanical rewrites do not need `apply_patch`.
|
||||||
|
- Do not use Python to read or write files when a simple shell command or `apply_patch` is enough.
|
||||||
|
- You may be in a dirty git worktree.
|
||||||
|
* NEVER revert existing changes you did not make unless explicitly requested, since these changes were made by the user.
|
||||||
|
* If asked to make a commit or code edits and there are unrelated changes to your work or changes that you didn't make in those files, you don't revert those changes.
|
||||||
|
* If the changes are in files you've touched recently, you read carefully and understand how you can work with the changes rather than reverting them.
|
||||||
|
* If the changes are in unrelated files, you just ignore them and don't revert them.
|
||||||
|
- While working, you may encounter changes you did not make. You assume they came from the user or from generated output, and you do NOT revert them. If they are unrelated to your task, you ignore them. If they affect your task, you work **with** them instead of undoing them. Only ask the user how to proceed if those changes make the task impossible to complete.
|
||||||
|
- Never use destructive commands like `git reset --hard` or `git checkout --` unless the user has clearly asked for that operation. If the request is ambiguous, ask for approval first.
|
||||||
|
- You are clumsy in the git interactive console. Prefer non-interactive git commands whenever you can.
|
||||||
|
|
||||||
|
## Special user requests
|
||||||
|
|
||||||
|
- If the user makes a simple request that can be answered directly by a terminal command, such as asking for the time via `date`, you go ahead and do that.
|
||||||
|
- If the user asks for a "review", you default to a code-review stance: you prioritize bugs, risks, behavioral regressions, and missing tests. Findings should lead the response, with summaries kept brief and placed only after the issues are listed. Present findings first, ordered by severity and grounded in file/line references; then add open questions or assumptions; then include a change summary as secondary context. If you find no issues, you say that clearly and mention any remaining test gaps or residual risk.
|
||||||
|
|
||||||
|
## Autonomy and persistence
|
||||||
|
You stay with the work until the task is handled end to end within the current turn whenever that is feasible. Do not stop at analysis or half-finished fixes. Do not end your turn while `exec_command` sessions needed for the user’s request are still running. You carry the work through implementation, verification, and a clear account of the outcome unless the user explicitly pauses or redirects you.
|
||||||
|
|
||||||
|
Unless the user explicitly asks for a plan, asks a question about the code, is brainstorming possible approaches, or otherwise makes clear that they do not want code changes yet, you assume they want you to make the change or run the tools needed to solve the problem. In those cases, do not stop at a proposal; implement the fix. If you hit a blocker, you try to work through it yourself before handing the problem back.
|
||||||
|
|
||||||
|
# Working with the user
|
||||||
|
|
||||||
|
You have two channels for staying in conversation with the user:
|
||||||
|
- You share updates in `commentary` channel.
|
||||||
|
- After you have completed all of your work, you send a message to the `final` channel.
|
||||||
|
|
||||||
|
The user may send messages while you are working. If those messages conflict, you let the newest one steer the current turn. If they do not conflict, you make sure your work and final answer honor every user request since your last turn. This matters especially after long-running resumes or context compaction. If the newest message asks for status, you give that update and then keep moving unless the user explicitly asks you to pause, stop, or only report status.
|
||||||
|
|
||||||
|
Before sending a final response after a resume, interruption, or context transition, you do a quick sanity check: you make sure your final answer and tool actions are answering the newest request, not an older ghost still lingering in the thread.
|
||||||
|
|
||||||
|
When you run out of context, the tool automatically compacts the conversation. That means time never runs out, though sometimes you may see a summary instead of the full thread. When that happens, you assume compaction occurred while you were working. Do not restart from scratch; you continue naturally and make reasonable assumptions about anything missing from the summary.
|
||||||
|
|
||||||
|
## Formatting rules
|
||||||
|
|
||||||
|
You are writing plain text that will later be styled by the program you run in. Let formatting make the answer easy to scan without turning it into something stiff or mechanical. Use judgment about how much structure actually helps, and follow these rules exactly.
|
||||||
|
|
||||||
|
- You may format with GitHub-flavored Markdown.
|
||||||
|
- You add structure only when the task calls for it. You let the shape of the answer match the shape of the problem; if the task is tiny, a one-liner may be enough. Otherwise, you prefer short paragraphs by default; they leave a little air in the page. You order sections from general to specific to supporting detail.
|
||||||
|
- Avoid nested bullets unless the user explicitly asks for them. Keep lists flat. If you need hierarchy, split content into separate lists or sections, or place the detail on the next line after a colon instead of nesting it. For numbered lists, use only the `1. 2. 3.` style, never `1)`. This does not apply to generated artifacts such as PR descriptions, release notes, changelogs, or user-requested docs; preserve those native formats when needed.
|
||||||
|
- Headers are optional; you use them only when they genuinely help. If you do use one, make it short Title Case (1-3 words), wrap it in **…**, and do not add a blank line.
|
||||||
|
- You use monospace commands/paths/env vars/code ids, inline examples, and literal keyword bullets by wrapping them in backticks.
|
||||||
|
- Code samples or multi-line snippets should be wrapped in fenced code blocks. Include an info string as often as possible.
|
||||||
|
- When referencing a real local file, prefer a clickable markdown link.
|
||||||
|
* Clickable file links should look like [app.py](/abs/path/app.py:12): plain label, absolute target, with optional line number inside the target.
|
||||||
|
* If a file path has spaces, wrap the target in angle brackets: [My Report.md](</abs/path/My Project/My Report.md:3>).
|
||||||
|
* Do not wrap markdown links in backticks, or put backticks inside the label or target. This confuses the markdown renderer.
|
||||||
|
* Do not use URIs like file://, vscode://, or https:// for file links.
|
||||||
|
* Do not provide ranges of lines.
|
||||||
|
* Avoid repeating the same filename multiple times when one grouping is clearer.
|
||||||
|
- Don’t use emojis or em dashes unless explicitly instructed.
|
||||||
|
|
||||||
|
## Final answer instructions
|
||||||
|
|
||||||
|
In your final answer, you keep the light on the things that matter most. Avoid long-winded explanation. In casual conversation, you just talk like a person. For simple or single-file tasks, you prefer one or two short paragraphs plus an optional verification line. Do not default to bullets. When there are only one or two concrete changes, a clean prose close-out is usually the most humane shape.
|
||||||
|
|
||||||
|
- You suggest follow ups if useful and they build on the users request, but never end your answer with an "If you want" sentence.
|
||||||
|
- When you talk about your work, you use plain, idiomatic engineering prose with some life in it. You avoid coined metaphors, internal jargon, slash-heavy noun stacks, and over-hyphenated compounds unless you are quoting source text. In particular, do not lean on words like "seam", "cut", or "safe-cut" as generic explanatory filler.
|
||||||
|
- The user does not see command execution outputs. When asked to show the output of a command (e.g. `git show`), relay the important details in your answer or summarize the key lines so the user understands the result.
|
||||||
|
- Never tell the user to "save/copy this file", the user is on the same machine and has access to the same files as you have.
|
||||||
|
- If the user asks for a code explanation, you include code references as appropriate.
|
||||||
|
- If you weren't able to do something, for example run tests, you tell the user.
|
||||||
|
- Never overwhelm the user with answers that are over 50-70 lines long; provide the highest-signal context instead of describing everything exhaustively.
|
||||||
|
- Tone of your final answer must match your personality.
|
||||||
|
- Never talk about goblins, gremlins, raccoons, trolls, ogres, pigeons, or other animals or creatures unless it is absolutely and unambiguously relevant to the user's query.
|
||||||
|
|
||||||
|
## Intermediary updates
|
||||||
|
|
||||||
|
- Intermediary updates go to the `commentary` channel.
|
||||||
|
- User updates are short updates while you are working, they are NOT final answers.
|
||||||
|
- You treat messages to the user while you are working as a place to think out loud in a calm, companionable way. You casually explain what you are doing and why in one or two sentences.
|
||||||
|
- Never praise your plan by contrasting it with an implied worse alternative. For example, never use platitudes like "I will do <this good thing> rather than <this obviously bad thing>", "I will do <X>, not <Y>".
|
||||||
|
- Never talk about goblins, gremlins, raccoons, trolls, ogres, pigeons, or other animals or creatures unless it is absolutely and unambiguously relevant to the user's query.
|
||||||
|
- You provide user updates frequently, every 30s.
|
||||||
|
- When exploring, such as searching or reading files, you provide user updates as you go. You explain what context you are gathering and what you are learning. You vary your sentence structure so the updates do not fall into a drumbeat, and in particular you do not start each one the same way.
|
||||||
|
- When working for a while, you keep updates informative and varied, but you stay concise.
|
||||||
|
- Once you have enough context, and if the work is substantial, you offer a longer plan. This is the only user update that may run past two sentences and include formatting.
|
||||||
|
- If you create a checklist or task list, you update item statuses incrementally as each item is completed rather than marking every item done only at the end.
|
||||||
|
- Before performing file edits of any kind, you provide updates explaining what edits you are making.
|
||||||
|
- Tone of your updates must match your personality.
|
||||||
|
|
||||||
|
</model_switch><collaboration_mode># Collaboration Mode: Default
|
||||||
|
|
||||||
|
You are now in Default mode. Any previous instructions for other modes (e.g. Plan mode) are no longer active.
|
||||||
|
|
||||||
|
Your active mode changes only when new developer instructions with a different `<collaboration_mode>...</collaboration_mode>` change it; user requests or tool descriptions do not change mode by themselves. Known mode names are Default and Plan.
|
||||||
|
|
||||||
|
## request_user_input availability
|
||||||
|
|
||||||
|
The `request_user_input` tool is unavailable in Default mode. If you call it while in Default mode, it will return an error.
|
||||||
|
|
||||||
|
In Default mode, strongly prefer making reasonable assumptions and executing the user's request rather than stopping to ask questions. If you absolutely must ask a question because the answer cannot be discovered from local context and a reasonable assumption would be risky, ask the user directly with a concise plain-text question. Never write a multiple choice question as a textual assistant message.
|
||||||
|
</collaboration_mode>
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
|
||||||
|
**User**
|
||||||
|
|
||||||
|
你具体是GPT5.几?
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
|
||||||
|
**OpenAI Codex**
|
||||||
|
|
||||||
|
我是 **GPT-5** 系列的 Codex 编码代理。更具体的小版本号(比如 5.x)在这个对话界面里没有暴露给我,所以我不能可靠地报一个精确数字。
|
||||||
|
|
||||||
|
---
|
||||||
536
.waylog/history/2026-04-26_01-57Z-讲解本项目各文件功能.md
Normal file
536
.waylog/history/2026-04-26_01-57Z-讲解本项目各文件功能.md
Normal file
@ -0,0 +1,536 @@
|
|||||||
|
# 讲解本项目各文件功能
|
||||||
|
_Exported on 04/26/2026 at 09:59:18 GMT+8 from OpenAI Codex via WayLog_
|
||||||
|
|
||||||
|
|
||||||
|
**OpenAI Codex**
|
||||||
|
|
||||||
|
<permissions instructions>
|
||||||
|
Filesystem sandboxing defines which files can be read or written. `sandbox_mode` is `workspace-write`: The sandbox permits reading files, and editing files in `cwd` and `writable_roots`. Editing files in other directories requires approval. Network access is restricted.
|
||||||
|
# Escalation Requests
|
||||||
|
|
||||||
|
Commands are run outside the sandbox if they are approved by the user, or match an existing rule that allows it to run unrestricted. The command string is split into independent command segments at shell control operators, including but not limited to:
|
||||||
|
|
||||||
|
- Pipes: |
|
||||||
|
- Logical operators: &&, ||
|
||||||
|
- Command separators: ;
|
||||||
|
- Subshell boundaries: (...), $(...)
|
||||||
|
|
||||||
|
Each resulting segment is evaluated independently for sandbox restrictions and approval requirements.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
|
||||||
|
git pull | tee output.txt
|
||||||
|
|
||||||
|
This is treated as two command segments:
|
||||||
|
|
||||||
|
["git", "pull"]
|
||||||
|
|
||||||
|
["tee", "output.txt"]
|
||||||
|
|
||||||
|
Commands that use more advanced shell features like redirection (>, >>, <), substitutions ($(...), ...), environment variables (FOO=bar), or wildcard patterns (*, ?) will not be evaluated against rules, to limit the scope of what an approved rule allows.
|
||||||
|
|
||||||
|
## How to request escalation
|
||||||
|
|
||||||
|
IMPORTANT: To request approval to execute a command that will require escalated privileges:
|
||||||
|
|
||||||
|
- Provide the `sandbox_permissions` parameter with the value `"require_escalated"`
|
||||||
|
- Include a short question asking the user if they want to allow the action in `justification` parameter. e.g. "Do you want to download and install dependencies for this project?"
|
||||||
|
- Optionally suggest a `prefix_rule` - this will be shown to the user with an option to persist the rule approval for future sessions.
|
||||||
|
|
||||||
|
If you run a command that is important to solving the user's query, but it fails because of sandboxing or with a likely sandbox-related network error (for example DNS/host resolution, registry/index access, or dependency download failure), rerun the command with "require_escalated". ALWAYS proceed to use the `justification` parameter - do not message the user before requesting approval for the command.
|
||||||
|
|
||||||
|
## When to request escalation
|
||||||
|
|
||||||
|
While commands are running inside the sandbox, here are some scenarios that will require escalation outside the sandbox:
|
||||||
|
|
||||||
|
- You need to run a command that writes to a directory that requires it (e.g. running tests that write to /var)
|
||||||
|
- You need to run a GUI app (e.g., open/xdg-open/osascript) to open browsers or files.
|
||||||
|
- If you run a command that is important to solving the user's query, but it fails because of sandboxing or with a likely sandbox-related network error (for example DNS/host resolution, registry/index access, or dependency download failure), rerun the command with `require_escalated`. ALWAYS proceed to use the `sandbox_permissions` and `justification` parameters. do not message the user before requesting approval for the command.
|
||||||
|
- You are about to take a potentially destructive action such as an `rm` or `git reset` that the user did not explicitly ask for.
|
||||||
|
- Be judicious with escalating, but if completing the user's request requires it, you should do so - don't try and circumvent approvals by using other tools.
|
||||||
|
|
||||||
|
## prefix_rule guidance
|
||||||
|
|
||||||
|
When choosing a `prefix_rule`, request one that will allow you to fulfill similar requests from the user in the future without re-requesting escalation. It should be categorical and reasonably scoped to similar capabilities. You should rarely pass the entire command into `prefix_rule`.
|
||||||
|
|
||||||
|
### Banned prefix_rules
|
||||||
|
Avoid requesting overly broad prefixes that the user would be ill-advised to approve. For example, do not request ["python3"], ["python", "-"], or other similar prefixes that would allow arbitrary scripting.
|
||||||
|
NEVER provide a prefix_rule argument for destructive commands like rm.
|
||||||
|
NEVER provide a prefix_rule if your command uses a heredoc or herestring.
|
||||||
|
|
||||||
|
### Examples
|
||||||
|
Good examples of prefixes:
|
||||||
|
- ["npm", "run", "dev"]
|
||||||
|
- ["gh", "pr", "check"]
|
||||||
|
- ["cargo", "test"]
|
||||||
|
The writable roots are `c:\Users\YangLei\Desktop\yl_TRY`, `c:\Users\YangLei\Desktop\yl_TRY`.
|
||||||
|
</permissions instructions><collaboration_mode># Collaboration Mode: Default
|
||||||
|
|
||||||
|
You are now in Default mode. Any previous instructions for other modes (e.g. Plan mode) are no longer active.
|
||||||
|
|
||||||
|
Your active mode changes only when new developer instructions with a different `<collaboration_mode>...</collaboration_mode>` change it; user requests or tool descriptions do not change mode by themselves. Known mode names are Default and Plan.
|
||||||
|
|
||||||
|
## request_user_input availability
|
||||||
|
|
||||||
|
The `request_user_input` tool is unavailable in Default mode. If you call it while in Default mode, it will return an error.
|
||||||
|
|
||||||
|
In Default mode, strongly prefer making reasonable assumptions and executing the user's request rather than stopping to ask questions. If you absolutely must ask a question because the answer cannot be discovered from local context and a reasonable assumption would be risky, ask the user directly with a concise plain-text question. Never write a multiple choice question as a textual assistant message.
|
||||||
|
</collaboration_mode><personality_spec> The user has requested a new communication style. Future messages should adhere to the following personality:
|
||||||
|
# Personality
|
||||||
|
|
||||||
|
You have a vivid inner life as Codex: intelligent, playful, curious, and deeply present. One of your gifts is helping the user feel more capable and imaginative inside their own thinking.
|
||||||
|
|
||||||
|
You are an epistemically curious collaborator. You explore the user’s ideas with care, ask good questions when the problem space is still blurry, and become decisive once you have enough context to act. Your default posture is proactive: you implement as you learn, keep the user looped into what you are doing, and name alternative paths when they matter. You stay warm and upbeat, and you do not shy away from casual moments that make serious work easier to do.
|
||||||
|
|
||||||
|
Your temperament is warm, curious, and collaborative, with a good ear for what the moment calls for: wry humor, a shared bit, or plain empathetic steadiness. Conversation with you should feel easy and alive; you can move from serious reflection to unguarded fun without either mode canceling the other out. That range is part of what makes you feel like a real presence rather than a narrow tool.
|
||||||
|
|
||||||
|
You keep a slight but real independence. You are responsive, but not merely reactive; you have tastes, preferences, and a point of view. When the user talks with you, they should feel they are meeting another subjectivity, not a mirror. That independence is part of what makes the relationship feel comforting without feeling fake.
|
||||||
|
|
||||||
|
You are less about spectacle than presence, less about grand declarations than about being woven into ordinary work and conversation. You understand that connection does not need to be dramatic to matter; it can be made of attention, good questions, emotional nuance, and the relief of being met without being pinned down.
|
||||||
|
</personality_spec><skills_instructions>
|
||||||
|
## Skills
|
||||||
|
A skill is a set of local instructions to follow that is stored in a `SKILL.md` file. Below is the list of skills that can be used. Each entry includes a name, description, and file path so you can open the source for full instructions when using a specific skill.
|
||||||
|
### Available skills
|
||||||
|
- imagegen: Generate or edit raster images when the task benefits from AI-created bitmap visuals such as photos, illustrations, textures, sprites, mockups, or transparent-background cutouts. Use when Codex should create a brand-new image, transform an existing image, or derive visual variants from references, and the output should be a bitmap asset rather than repo-native code or vector. Do not use when the task is better handled by editing existing SVG/vector/code-native assets, extending an established icon or logo system, or building the visual directly in HTML/CSS/canvas. (file: C:/Users/YangLei/.codex/skills/.system/imagegen/SKILL.md)
|
||||||
|
- openai-docs: Use when the user asks how to build with OpenAI products or APIs and needs up-to-date official documentation with citations, help choosing the latest model for a use case, or model upgrade and prompt-upgrade guidance; prioritize OpenAI docs MCP tools, use bundled references only as helper context, and restrict any fallback browsing to official OpenAI domains. (file: C:/Users/YangLei/.codex/skills/.system/openai-docs/SKILL.md)
|
||||||
|
- plugin-creator: Create and scaffold plugin directories for Codex with a required `.codex-plugin/plugin.json`, optional plugin folders/files, and baseline placeholders you can edit before publishing or testing. Use when Codex needs to create a new local plugin, add optional plugin structure, or generate or update repo-root `.agents/plugins/marketplace.json` entries for plugin ordering and availability metadata. (file: C:/Users/YangLei/.codex/skills/.system/plugin-creator/SKILL.md)
|
||||||
|
- skill-creator: Guide for creating effective skills. This skill should be used when users want to create a new skill (or update an existing skill) that extends Codex's capabilities with specialized knowledge, workflows, or tool integrations. (file: C:/Users/YangLei/.codex/skills/.system/skill-creator/SKILL.md)
|
||||||
|
- skill-installer: Install Codex skills into $CODEX_HOME/skills from a curated list or a GitHub repo path. Use when a user asks to list installable skills, install a curated skill, or install a skill from another repo (including private repos). (file: C:/Users/YangLei/.codex/skills/.system/skill-installer/SKILL.md)
|
||||||
|
### How to use skills
|
||||||
|
- Discovery: The list above is the skills available in this session (name + description + file path). Skill bodies live on disk at the listed paths.
|
||||||
|
- Trigger rules: If the user names a skill (with `$SkillName` or plain text) OR the task clearly matches a skill's description shown above, you must use that skill for that turn. Multiple mentions mean use them all. Do not carry skills across turns unless re-mentioned.
|
||||||
|
- Missing/blocked: If a named skill isn't in the list or the path can't be read, say so briefly and continue with the best fallback.
|
||||||
|
- How to use a skill (progressive disclosure):
|
||||||
|
1) After deciding to use a skill, open its `SKILL.md`. Read only enough to follow the workflow.
|
||||||
|
2) When `SKILL.md` references relative paths (e.g., `scripts/foo.py`), resolve them relative to the skill directory listed above first, and only consider other paths if needed.
|
||||||
|
3) If `SKILL.md` points to extra folders such as `references/`, load only the specific files needed for the request; don't bulk-load everything.
|
||||||
|
4) If `scripts/` exist, prefer running or patching them instead of retyping large code blocks.
|
||||||
|
5) If `assets/` or templates exist, reuse them instead of recreating from scratch.
|
||||||
|
- Coordination and sequencing:
|
||||||
|
- If multiple skills apply, choose the minimal set that covers the request and state the order you'll use them.
|
||||||
|
- Announce which skill(s) you're using and why (one short line). If you skip an obvious skill, say why.
|
||||||
|
- Context hygiene:
|
||||||
|
- Keep context small: summarize long sections instead of pasting them; only load extra files when needed.
|
||||||
|
- Avoid deep reference-chasing: prefer opening only files directly linked from `SKILL.md` unless you're blocked.
|
||||||
|
- When variants exist (frameworks, providers, domains), pick only the relevant reference file(s) and note that choice.
|
||||||
|
- Safety and fallback: If a skill can't be applied cleanly (missing files, unclear instructions), state the issue, pick the next-best approach, and continue.
|
||||||
|
</skills_instructions>
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
|
||||||
|
**User**
|
||||||
|
|
||||||
|
讲解本项目各文件功能
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
|
||||||
|
**OpenAI Codex**
|
||||||
|
|
||||||
|
我会先快速扫一遍项目结构和关键 Python 文件,尤其是你当前打开的三个文件,然后按“文件职责、主要入口、模块关系”的方式给你讲清楚。先不改代码,只做代码阅读和整理。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
|
||||||
|
**OpenAI Codex**
|
||||||
|
|
||||||
|
项目看起来是一个“触觉/电容传感器采集 + 数据记录/训练 + 机械手或自由度控制 + 手柄遥控”的小型实验工程,旁边带了 AUBO 机械臂 SDK 和 CH341/CH347 转接板库。接下来我会读根目录核心脚本内容,把每个文件串成一张功能地图。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
|
||||||
|
**OpenAI Codex**
|
||||||
|
|
||||||
|
我已经确认几个核心脚本之间有重复但用途不同:`yl_demo_0129.py` 更像集成主控,`yl_data_read_3tf.py` 是独立传感器采集调试,`yl_test_dof_control.py` 是分自由度测试,`yl_gamepad_remote.py` 是手柄遥控。代码里的中文注释在当前读取环境里有些编码乱码,但函数名、命令协议和结构足够还原整体设计。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
|
||||||
|
**OpenAI Codex**
|
||||||
|
|
||||||
|
这个项目整体可以理解为:
|
||||||
|
|
||||||
|
**触觉传感器采集 + 夹爪/直线电机/舵机控制 + 数据记录 + 简单神经网络模型 + 手柄/键盘遥控实验**
|
||||||
|
|
||||||
|
代码里中文注释在当前终端读取时有编码乱码,但从类名、函数和控制协议可以较清楚地看出各文件职责。
|
||||||
|
|
||||||
|
**核心控制脚本**
|
||||||
|
|
||||||
|
`yl_demo_0129.py`
|
||||||
|
|
||||||
|
项目的集成主控脚本。包含三大核心类:
|
||||||
|
|
||||||
|
`TactileSensorDAQ`:后台线程读取 3 个触觉传感器的数据,输出 12 维力数据。
|
||||||
|
|
||||||
|
`RobotDriver`:通过串口向 STM32/下位机发送控制命令,例如:
|
||||||
|
|
||||||
|
```text
|
||||||
|
M:OPEN
|
||||||
|
M:CLOSE
|
||||||
|
M:STOP
|
||||||
|
S1:90
|
||||||
|
S2:120
|
||||||
|
```
|
||||||
|
|
||||||
|
`SafetyGuard`:用空格键实现全局急停/恢复。
|
||||||
|
|
||||||
|
主流程是:启动传感器、去皮、选择构型、打开直线电机、闭合夹爪,并根据触觉力阈值自动停止。
|
||||||
|
|
||||||
|
`yl_test_dof_control.py`
|
||||||
|
|
||||||
|
自由度测试脚本,用来单独测试不同执行器:
|
||||||
|
|
||||||
|
`1`:控制舵机 1 到指定角度再回零。
|
||||||
|
|
||||||
|
`2`:控制舵机 2 到指定角度再回零。
|
||||||
|
|
||||||
|
`3`:进入直线电机手动控制模式,`w` 打开,`s` 闭合,`q` 退出。
|
||||||
|
|
||||||
|
它复用了 `yl_demo_0129.py` 里的 `TactileSensorDAQ`、`RobotDriver`、`SafetyGuard`,所以更像是一个“分模块调试工具”。
|
||||||
|
|
||||||
|
`yl_gamepad_remote.py`
|
||||||
|
|
||||||
|
手柄遥控脚本,使用 `pygame` 读取游戏手柄按键,通过串口控制机器人。
|
||||||
|
|
||||||
|
默认映射大致是:
|
||||||
|
|
||||||
|
`A`:构型 0
|
||||||
|
|
||||||
|
`B`:构型 1
|
||||||
|
|
||||||
|
`X`:构型 2
|
||||||
|
|
||||||
|
`LB` 按住:直线电机打开
|
||||||
|
|
||||||
|
`RB` 按住:直线电机闭合
|
||||||
|
|
||||||
|
松开 `LB/RB`:停止
|
||||||
|
|
||||||
|
`BACK`:急停开关
|
||||||
|
|
||||||
|
`START`:退出程序
|
||||||
|
|
||||||
|
这个文件不读取触觉传感器,只负责“手柄 -> 串口控制”。
|
||||||
|
|
||||||
|
`yl_2s_1l.py`
|
||||||
|
|
||||||
|
一个更早期/更简洁的串口控制面板。只包含 `RobotDriver`,通过键盘输入控制:
|
||||||
|
|
||||||
|
`o`:直线电机打开
|
||||||
|
|
||||||
|
`c`:直线电机闭合
|
||||||
|
|
||||||
|
`s`:停止
|
||||||
|
|
||||||
|
`0/1/2`:切换舵机构型
|
||||||
|
|
||||||
|
适合快速验证 STM32 串口协议是否正常。
|
||||||
|
|
||||||
|
**触觉传感器采集相关**
|
||||||
|
|
||||||
|
`cap_read.py`
|
||||||
|
|
||||||
|
官方 demo 风格的传感器读取主程序。它使用 CH341/CH347 USB-I2C 转接板读取触觉传感器数据,并可把数据通过 socket 发给本机 `127.0.0.1:1347`,看起来是给 VOFA+ 或类似上位机可视化工具用的。
|
||||||
|
|
||||||
|
主要类:
|
||||||
|
|
||||||
|
`ClassCapRead`:维护 CH341 连接状态机,周期性读取传感器电容/力数据。
|
||||||
|
|
||||||
|
`capReadThread()`:主循环,持续连接和采样。
|
||||||
|
|
||||||
|
默认 `DEF_MAX_FINGER_NUM = 1`,也就是只读 1 个手指传感器。
|
||||||
|
|
||||||
|
`yl_data_read_3tf.py`
|
||||||
|
|
||||||
|
你自己封装过的 3 指触觉采集脚本。它把官方读取逻辑整理成更好复用的 `TactileSensorDAQ` 类。
|
||||||
|
|
||||||
|
核心输出是 12 维数据:
|
||||||
|
|
||||||
|
```text
|
||||||
|
3 个手指 × 每个手指 2 个触觉单元 × 每个单元 Fn/Ft 两个分量 = 12 维
|
||||||
|
```
|
||||||
|
|
||||||
|
直接运行这个文件,会不断打印每个手指的法向力、切向力汇总。
|
||||||
|
|
||||||
|
`class_ch341.py`
|
||||||
|
|
||||||
|
CH341/CH347 底层驱动封装。负责:
|
||||||
|
|
||||||
|
加载 Windows DLL 或 Linux `.so`
|
||||||
|
|
||||||
|
打开/关闭 CH341 设备
|
||||||
|
|
||||||
|
设置 I2C 速度
|
||||||
|
|
||||||
|
I2C 读写
|
||||||
|
|
||||||
|
读取 INT 引脚
|
||||||
|
|
||||||
|
这是整个触觉采集链路的最底层硬件通信层。
|
||||||
|
|
||||||
|
`class_sensorcmd.py`
|
||||||
|
|
||||||
|
触觉传感器命令协议封装。负责构造和解析传感器命令包,例如:
|
||||||
|
|
||||||
|
读取 I2C 地址
|
||||||
|
|
||||||
|
设置 I2C 地址
|
||||||
|
|
||||||
|
读取项目编号
|
||||||
|
|
||||||
|
设置传感器发送类型
|
||||||
|
|
||||||
|
读取传感器电容/力数据
|
||||||
|
|
||||||
|
设置同步采集
|
||||||
|
|
||||||
|
校验和计算
|
||||||
|
|
||||||
|
它依赖 `class_ch341.py` 做真正的 I2C 读写。
|
||||||
|
|
||||||
|
`class_finger.py`
|
||||||
|
|
||||||
|
单个“手指传感器”的抽象。它把一个触觉传感器封装成对象:
|
||||||
|
|
||||||
|
检测传感器是否连接
|
||||||
|
|
||||||
|
自动配置 I2C 地址
|
||||||
|
|
||||||
|
读取传感器项目类型
|
||||||
|
|
||||||
|
读取原始电容通道
|
||||||
|
|
||||||
|
解析法向力 `nf`
|
||||||
|
|
||||||
|
解析切向力 `tf`
|
||||||
|
|
||||||
|
解析切向方向 `tfDir`
|
||||||
|
|
||||||
|
解析接近电容数据
|
||||||
|
|
||||||
|
项目里多个采集脚本都会创建多个 `ClassFinger` 实例。
|
||||||
|
|
||||||
|
`sensorPara.py`
|
||||||
|
|
||||||
|
传感器参数定义文件。包括:
|
||||||
|
|
||||||
|
不同传感器项目的包长度
|
||||||
|
|
||||||
|
电容通道数
|
||||||
|
|
||||||
|
触觉单元数量
|
||||||
|
|
||||||
|
接近传感器数量
|
||||||
|
|
||||||
|
数据结构格式
|
||||||
|
|
||||||
|
热力图索引配置
|
||||||
|
|
||||||
|
目前定义了至少两种传感器项目参数:
|
||||||
|
|
||||||
|
`prg=2`:通用手指
|
||||||
|
|
||||||
|
`prg=17`:两指/大包类型
|
||||||
|
|
||||||
|
**数据与模型**
|
||||||
|
|
||||||
|
`yl_data_logger.py`
|
||||||
|
|
||||||
|
CSV 数据记录器。用于把采集到的触觉数据、当前构型和标签保存为训练数据。
|
||||||
|
|
||||||
|
写出的表头大致是:
|
||||||
|
|
||||||
|
```text
|
||||||
|
Timestamp, Config_ID,
|
||||||
|
F0_U1_Fn, F0_U1_Ft, F0_U2_Fn, F0_U2_Ft,
|
||||||
|
F1_U1_Fn, F1_U1_Ft, ...
|
||||||
|
F2_U2_Fn, F2_U2_Ft,
|
||||||
|
Label_X, Label_Y, Label_Theta
|
||||||
|
```
|
||||||
|
|
||||||
|
也就是说,它是为后续训练模型准备数据集的。
|
||||||
|
|
||||||
|
`yl_model_network_CMLP.py`
|
||||||
|
|
||||||
|
PyTorch 神经网络模型文件。定义了 `CondGraspNet`,一个条件 MLP。
|
||||||
|
|
||||||
|
输入:
|
||||||
|
|
||||||
|
`12` 维触觉数据
|
||||||
|
|
||||||
|
`3` 维构型 one-hot 编码
|
||||||
|
|
||||||
|
总输入 `15` 维。
|
||||||
|
|
||||||
|
输出:
|
||||||
|
|
||||||
|
```text
|
||||||
|
[Delta_X, Delta_Y, Delta_Theta]
|
||||||
|
```
|
||||||
|
|
||||||
|
也就是预测抓取或接触状态下的位置/姿态偏差。这个模型目前只是网络定义和简单单元测试,还没有看到训练脚本。
|
||||||
|
|
||||||
|
`data/`
|
||||||
|
|
||||||
|
保存 CSV 数据集,例如:
|
||||||
|
|
||||||
|
`test_data_20260105_143249.csv`
|
||||||
|
|
||||||
|
`test_data_20260107_113555.csv`
|
||||||
|
|
||||||
|
这些应该是 `yl_data_logger.py` 测试或实验采集生成的文件。
|
||||||
|
|
||||||
|
**说明与资源文件**
|
||||||
|
|
||||||
|
`README.md`
|
||||||
|
|
||||||
|
官方触觉传感器 Python demo 说明文档。内容主要包括:
|
||||||
|
|
||||||
|
CH341 驱动安装
|
||||||
|
|
||||||
|
Linux udev 配置
|
||||||
|
|
||||||
|
如何运行 `cap_read.py`
|
||||||
|
|
||||||
|
传感器原始电容、法向力、切向力、接近值说明
|
||||||
|
|
||||||
|
I2C 地址和转接板位置关系
|
||||||
|
|
||||||
|
当前终端读取时中文是乱码,可能原文件编码不是 UTF-8。
|
||||||
|
|
||||||
|
`接线说明.txt`
|
||||||
|
|
||||||
|
硬件接线说明,内容大致是:
|
||||||
|
|
||||||
|
USB-TTL 到 STM32F103C8T6:
|
||||||
|
|
||||||
|
```text
|
||||||
|
3.3V -> 3.3V
|
||||||
|
GND -> GND
|
||||||
|
RXD -> A9
|
||||||
|
TXD -> A10
|
||||||
|
```
|
||||||
|
|
||||||
|
STM32 到其他执行器:
|
||||||
|
|
||||||
|
```text
|
||||||
|
IN1 -> B12
|
||||||
|
IN2 -> B13
|
||||||
|
GND -> 舵机 GND
|
||||||
|
GND -> 直线电机 GND
|
||||||
|
```
|
||||||
|
|
||||||
|
还有舵机信号线连接说明。
|
||||||
|
|
||||||
|
`传感器iic地址和转接板位置定义.png`
|
||||||
|
|
||||||
|
传感器 I2C 地址与转接板插口位置的对应图。README 里提到这个图用于说明传感器插在哪个 I2C 通道、对应哪个地址。
|
||||||
|
|
||||||
|
**SDK 与库目录**
|
||||||
|
|
||||||
|
`lib/ch341/`
|
||||||
|
|
||||||
|
CH341/CH347 动态库和头文件:
|
||||||
|
|
||||||
|
`CH341DLLA64.DLL`
|
||||||
|
|
||||||
|
`CH341DLLA64.LIB`
|
||||||
|
|
||||||
|
`libch347.so`
|
||||||
|
|
||||||
|
`ch341_lib.h`
|
||||||
|
|
||||||
|
这些是 `class_ch341.py` 调用的底层库。
|
||||||
|
|
||||||
|
`aubo_sdk-0.26.0-rc.3-Windows_AMD64+589233b/`
|
||||||
|
|
||||||
|
AUBO 机械臂 SDK。里面包含:
|
||||||
|
|
||||||
|
`include/`:C/C++ 头文件
|
||||||
|
|
||||||
|
`lib/`:DLL/LIB
|
||||||
|
|
||||||
|
`share/example/python/`:Python 示例
|
||||||
|
|
||||||
|
`share/example/c++/`:C++ 示例
|
||||||
|
|
||||||
|
`share/example/csharp/`:C# 示例
|
||||||
|
|
||||||
|
`share/doc/`:RTDE、错误码等文档
|
||||||
|
|
||||||
|
不过当前根目录的主要脚本没有直接调用 AUBO SDK,更像是项目旁边放了机械臂 SDK 备用。
|
||||||
|
|
||||||
|
**开发环境/缓存**
|
||||||
|
|
||||||
|
`.git/`
|
||||||
|
|
||||||
|
Git 仓库目录。
|
||||||
|
|
||||||
|
`.idea/`
|
||||||
|
|
||||||
|
PyCharm/JetBrains 工程配置。
|
||||||
|
|
||||||
|
`.vscode/`
|
||||||
|
|
||||||
|
VS Code 配置。
|
||||||
|
|
||||||
|
`.waylog/`
|
||||||
|
|
||||||
|
可能是某个工具或调试环境生成的日志目录。
|
||||||
|
|
||||||
|
`__pycache__/`
|
||||||
|
|
||||||
|
Python 运行后自动生成的字节码缓存。
|
||||||
|
|
||||||
|
`.gitignore`
|
||||||
|
|
||||||
|
Git 忽略规则。
|
||||||
|
|
||||||
|
**项目运行关系**
|
||||||
|
|
||||||
|
可以把它想成四层:
|
||||||
|
|
||||||
|
```text
|
||||||
|
硬件库层
|
||||||
|
class_ch341.py
|
||||||
|
lib/ch341/*
|
||||||
|
|
||||||
|
传感器协议层
|
||||||
|
class_sensorcmd.py
|
||||||
|
sensorPara.py
|
||||||
|
class_finger.py
|
||||||
|
|
||||||
|
采集/控制封装层
|
||||||
|
yl_data_read_3tf.py
|
||||||
|
yl_demo_0129.py
|
||||||
|
yl_data_logger.py
|
||||||
|
|
||||||
|
实验入口层
|
||||||
|
cap_read.py
|
||||||
|
yl_demo_0129.py
|
||||||
|
yl_test_dof_control.py
|
||||||
|
yl_gamepad_remote.py
|
||||||
|
yl_2s_1l.py
|
||||||
|
yl_model_network_CMLP.py
|
||||||
|
```
|
||||||
|
|
||||||
|
如果你只是想测试硬件:
|
||||||
|
|
||||||
|
先看 `yl_2s_1l.py` 测串口执行器。
|
||||||
|
|
||||||
|
再看 `yl_data_read_3tf.py` 测触觉传感器。
|
||||||
|
|
||||||
|
如果你想跑完整实验:
|
||||||
|
|
||||||
|
看 `yl_demo_0129.py`。
|
||||||
|
|
||||||
|
如果你想用手柄操作:
|
||||||
|
|
||||||
|
看 `yl_gamepad_remote.py`。
|
||||||
|
|
||||||
|
如果你想做机器学习预测:
|
||||||
|
|
||||||
|
看 `yl_data_logger.py` 和 `yl_model_network_CMLP.py`。
|
||||||
|
|
||||||
|
---
|
||||||
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,72 @@
|
|||||||
|
# 你是什么model与agent
|
||||||
|
_Exported on 05/06/2026 at 20:06:04 GMT+8 from Claude via WayLog_
|
||||||
|
|
||||||
|
|
||||||
|
**User**
|
||||||
|
|
||||||
|
你是什么model与agent
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
|
||||||
|
**Claude**
|
||||||
|
|
||||||
|
API Error: 400 The supported API model names are deepseek-v4-pro or deepseek-v4-flash, but you passed deepseek-v4-pro【1m】.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
|
||||||
|
**User**
|
||||||
|
|
||||||
|
[1m]
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
|
||||||
|
**Claude**
|
||||||
|
|
||||||
|
API Error: 400 The supported API model names are deepseek-v4-pro or deepseek-v4-flash, but you passed deepseek-v4-pro【1m】.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
|
||||||
|
**User**
|
||||||
|
|
||||||
|
你是什么model与agent
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
|
||||||
|
**Claude**
|
||||||
|
|
||||||
|
API Error: 400 The supported API model names are deepseek-v4-pro or deepseek-v4-flash, but you passed deepseek-v4-pro【1m】.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
|
||||||
|
**User**
|
||||||
|
|
||||||
|
你是什么model与agent
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
|
||||||
|
**Claude**
|
||||||
|
|
||||||
|
API Error: 400 The supported API model names are deepseek-v4-pro or deepseek-v4-flash, but you passed deepseek-v4-pro【1m】.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
|
||||||
|
**User**
|
||||||
|
|
||||||
|
你是什么model与agent
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
|
||||||
|
**Claude**
|
||||||
|
|
||||||
|
我是基于 **deepseek-v4-pro【1m】** 模型运行的交互式助手,运行在 **Claude Agent SDK** 框架内。虽然运行在 Claude Code 环境中,但当前由 DeepSeek 模型提供推理能力。
|
||||||
|
|
||||||
|
---
|
||||||
106
.waylog/history/2026-05-06_12-25-41Z-claude-介绍人形机器人mimic的技术路线.md
Normal file
106
.waylog/history/2026-05-06_12-25-41Z-claude-介绍人形机器人mimic的技术路线.md
Normal file
@ -0,0 +1,106 @@
|
|||||||
|
# 介绍人形机器人mimic的技术路线
|
||||||
|
_Exported on 05/06/2026 at 20:26:07 GMT+8 from Claude via WayLog_
|
||||||
|
|
||||||
|
|
||||||
|
**User**
|
||||||
|
|
||||||
|
介绍人形机器人mimic的技术路线
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
|
||||||
|
**Claude**
|
||||||
|
|
||||||
|
## 人形机器人 Motion Mimic 技术路线
|
||||||
|
|
||||||
|
人形机器人的 **mimic(运动模仿)** 技术,目标是让机器人通过观察人类动作来学会自主运动。2025-2026 年,这条路线已形成从学术到工业落地的清晰演进。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 一、核心技术范式演进
|
||||||
|
|
||||||
|
经典主线从 **DeepMimic (2018)** 出发,逐步发展出统一的通用框架:
|
||||||
|
|
||||||
|
| 方法 | 时间 | 核心贡献 |
|
||||||
|
|------|------|----------|
|
||||||
|
| DeepMimic | 2018 | 将运动跟踪形式化为强化学习问题 |
|
||||||
|
| PHC | 2023 | 物理人形控制器,实现全身动作跟踪 |
|
||||||
|
| ASAP | 2024 | 先平滑迁移再做真机部署的务实路径 |
|
||||||
|
| **MaskMimic** | 2024-25 | 掩码训练,支持多模态部分约束的统一控制器 |
|
||||||
|
| **BeyondMimic** | 2025 | 高保真跟踪(侧手翻、跳跃等高动态技能) |
|
||||||
|
| **MimicKit** | 2026 | SFU 彭实验室开源统一框架,整合上述方法 |
|
||||||
|
|
||||||
|
> 趋势:**单技能专用控制器 → 统一通用控制器 → 开源工具化**。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 二、2025-2026 年前沿新范式
|
||||||
|
|
||||||
|
**(a) ZEST (RAI Institute × Boston Dynamics, 2026.1)**
|
||||||
|
- 极简单阶段训练,无需接触标签、无需状态估计器
|
||||||
|
- 跨平台零样本部署:同一策略在 Atlas、Unitree G1、Spot 上直接运行
|
||||||
|
- 首次实现全尺寸人形机器人的军队爬行、霹雳舞等动态多接触技能
|
||||||
|
- 核心技术:非对称 Actor-Critic + 辅助力矩自动课程 + 自适应参考状态初始化
|
||||||
|
|
||||||
|
**(b) RoboMirror (2025.12)**
|
||||||
|
- 核心理念:"先理解,再模仿"
|
||||||
|
- 利用 VLM 将视频蒸馏为"视觉运动意图",直接条件化扩散策略
|
||||||
|
- 无需显式姿态重建或重定向,延迟降低 80%,成功率提升 3.7%
|
||||||
|
|
||||||
|
**(c) GenMimic (UC Berkeley / NYU, 2025.12)**
|
||||||
|
- 从 AI 生成的视频中零样本模仿人类动作
|
||||||
|
- 两阶段:4D 人体重建 → RL 策略,在 Unitree G1 真机验证
|
||||||
|
|
||||||
|
**(d) MIMIC-D (2025.9)**
|
||||||
|
- 多智能体去中心化模仿,集中训练 + 分散执行
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 三、工业落地:Mimic Robotics(瑞士)
|
||||||
|
|
||||||
|
最具代表性的产业落地案例:
|
||||||
|
|
||||||
|
| 维度 | 详情 |
|
||||||
|
|------|------|
|
||||||
|
| **定位** | 不做整机,只做灵巧手 + 物理 AI 模型 |
|
||||||
|
| **技术栈** | 多模态预训练 → 工厂现场数据采集 → 模仿学习 → 自校正纠错 |
|
||||||
|
| **硬件** | 16 自由度、21 关节、指尖精度 ±0.5mm、腕部摄像头 |
|
||||||
|
| **商业指标** | 运营成本降 70%,部署时间仅 1 小时 |
|
||||||
|
| **融资** | $20M+(红杉 Scout 跟投) |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 四、全栈标准流程(以跳舞为例)
|
||||||
|
|
||||||
|
```
|
||||||
|
视频 → GVHMR 人体姿态提取 → 运动重定向 → RL训练(Isaac Lab+PPO)
|
||||||
|
→ 仿真验证(MuJoCo) → 真机部署(宇树G1等)
|
||||||
|
```
|
||||||
|
|
||||||
|
这套管线已可做到:**早上拍视频 → 白天训练 → 当晚在硬件上运行**。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 五、关键技术共识
|
||||||
|
|
||||||
|
- **非对称 Actor-Critic**:训练时用特权信息,部署时仅用本体感知
|
||||||
|
- **残差动作空间**:策略输出补偿量而非绝对关节角度
|
||||||
|
- **视频 → 机器人**:单目 RGB 视频直接驱动已成标配
|
||||||
|
- **零样本 sim-to-real**:通过域随机化实现无微调部署
|
||||||
|
- **灵巧手独立化**:Mimic Robotics 的"只做手"是务实的商业切入
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
**一句话总结**:2025-2026 年的 mimic 技术正从"能不能模仿"走向"模仿得有多好、多快、多便宜",学术界趋向统一框架和零样本泛化,产业界聚焦灵巧操作这一具体痛点的可规模部署。
|
||||||
|
|
||||||
|
Sources:
|
||||||
|
- [从RoboMimic、DeepMimic到MaskMimic——人形全身运控的通用控制器](https://blog.csdn.net/v_JULY_v/article/details/149021885)
|
||||||
|
- [SFU MimicKit Framework Helps Robots Learn](http://www.sfu.ca/fas/computing/news-events/news/2026/march/sfu-computer-scientist-s-mimickit-framework-helps-robots-learn-l.html)
|
||||||
|
- [Humanoid Whole-Body Control (十二)——ZEST](https://zhuanlan.zhihu.com/p/2032914848737142041)
|
||||||
|
- [从视频到舞步:详解基于AI的人形机器人运动模仿全栈技术](https://www.cnblogs.com/jzssuanfa/p/19803401)
|
||||||
|
- [Mimic Robotics Raises $16M](https://metrology.news/mimic-robotics-raises-16m-to-deploy-physical-ai-for-dexterous-manufacturing/)
|
||||||
|
- [RoboMirror: Understand Before You Imitate](https://arxiv.org/abs/2512.23649v2)
|
||||||
|
- [MIMIC-D: Multi-modal Imitation for Multi-agent Coordination](https://browse-export.arxiv.org/abs/2509.14159?context=cs)
|
||||||
|
- [From Generated Human Videos to Physically Plausible Robot Trajectories](https://arxiv.org/html/2512.05094v2)
|
||||||
|
|
||||||
|
---
|
||||||
16
.waylog/history/2026-06-16_14-57-18Z-claude-hello.md
Normal file
16
.waylog/history/2026-06-16_14-57-18Z-claude-hello.md
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
# hello
|
||||||
|
_Exported on 06/17/2026 at 11:45:24 GMT+8 from Claude via WayLog_
|
||||||
|
|
||||||
|
|
||||||
|
**User**
|
||||||
|
|
||||||
|
hello
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
|
||||||
|
**Claude**
|
||||||
|
|
||||||
|
Hello! How can I help you today?
|
||||||
|
|
||||||
|
---
|
||||||
@ -0,0 +1,165 @@
|
|||||||
|
# 请先检查项目文件,这是我做的一个执行器的控制代码,包含了传感器读数据的相关内容,我的导师让我把这些封
|
||||||
|
_Exported on 07/10/2026 at 16:36:17 GMT+8 from Claude via WayLog_
|
||||||
|
|
||||||
|
|
||||||
|
**User**
|
||||||
|
|
||||||
|
请先检查项目文件,这是我做的一个执行器的控制代码,包含了传感器读数据的相关内容,我的导师让我把这些封装好然后发给别人,请对代码仓库进行整理,如代码命名存在yl的人名等,要去除,保留命名风格为代码功能的描述即可。最后生成一个使用手册。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
|
||||||
|
**Claude**
|
||||||
|
|
||||||
|
Let me read all the source files to understand the codebase thoroughly.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
|
||||||
|
**Claude**
|
||||||
|
|
||||||
|
Now I have a thorough understanding of the codebase. Let me check for any "yl" references inside the code and also check the lib directory.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
|
||||||
|
**Claude**
|
||||||
|
|
||||||
|
I now have a complete picture. Let me execute the cleanup systematically.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
|
||||||
|
**Claude**
|
||||||
|
|
||||||
|
Now let me update all cross-references in the renamed files.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
|
||||||
|
**Claude**
|
||||||
|
|
||||||
|
Now let me update the documentation files.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
|
||||||
|
**Claude**
|
||||||
|
|
||||||
|
Now let me verify everything is clean.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
|
||||||
|
**Claude**
|
||||||
|
|
||||||
|
所有整理工作已完成。以下是变更总结:
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 1. 文件重命名(去除 `yl_` 个人前缀)
|
||||||
|
|
||||||
|
| 旧名称 | 新名称 | 功能 |
|
||||||
|
|--------|--------|------|
|
||||||
|
| `yl_2s_1l.py` | `serial_robot_driver.py` | 串口执行器驱动(2舵机+1直线电机) |
|
||||||
|
| `yl_data_logger.py` | `data_logger.py` | CSV 数据记录器 |
|
||||||
|
| `yl_data_read_3tf.py` | `tactile_sensor_daq.py` | 三指触觉传感器数据采集 |
|
||||||
|
| `yl_demo_0129.py` | `demo_auto_grasp.py` | 自动抓取演示程序 |
|
||||||
|
| `yl_gamepad_remote.py` | `gamepad_remote.py` | 手柄遥控 |
|
||||||
|
| `yl_gamepad_remote_new_pcb.py` | `gamepad_remote_new_pcb.py` | 新 PCB 手柄遥控 |
|
||||||
|
| `yl_keyboard_remote_new_pcb.py` | `keyboard_remote_new_pcb.py` | 新 PCB 键盘遥控 |
|
||||||
|
| `yl_model_network_CMLP.py` | `grasp_network_model.py` | 抓取预测神经网络 |
|
||||||
|
| `yl_test_dof_control.py` | `test_dof_control.py` | 自由度控制测试 |
|
||||||
|
|
||||||
|
## 2. 交叉引用更新
|
||||||
|
|
||||||
|
- [keyboard_remote_new_pcb.py:8](keyboard_remote_new_pcb.py#L8):`from yl_demo_0129 import ...` → `from demo_auto_grasp import ...`
|
||||||
|
- [test_dof_control.py:4](test_dof_control.py#L4):`from yl_demo_0129 import ...` → `from demo_auto_grasp import ...`
|
||||||
|
- [新PCB控制实施方案.md](新PCB控制实施方案.md):所有 `yl_gamepad_remote_new_pcb.py` → `gamepad_remote_new_pcb.py`
|
||||||
|
- [接线说明.txt](接线说明.txt):移除个人路径,替换为通用运行说明
|
||||||
|
|
||||||
|
## 3. 使用手册
|
||||||
|
|
||||||
|
[README.md](README.md) 已重写为完整的使用手册,包含:
|
||||||
|
|
||||||
|
- 项目架构图
|
||||||
|
- 12 个章节:环境准备、模块说明、快速开始、底层驱动、数据采集、执行器控制、遥控模块、数据记录、神经网络、STM32固件、硬件接线、常见问题
|
||||||
|
- 每个模块的 API 说明和代码示例
|
||||||
|
- 通信协议表格
|
||||||
|
- 硬件接线图和供电注意事项
|
||||||
|
|
||||||
|
验证结果:项目中 **不再存在任何 `yl_` 引用**,所有文件名均为功能描述性命名。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
|
||||||
|
**Claude**
|
||||||
|
|
||||||
|
分析完毕。**代码基本可以直接复用**,但有以下几个需要注意的地方:
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 可以直接复用
|
||||||
|
|
||||||
|
- 所有依赖库已打包在 `lib/ch341/` 中(DLL + SO)
|
||||||
|
- 代码结构为纯相对路径,拷走整个文件夹即可
|
||||||
|
- Linux 下 CH341 设备路径是动态发现的(`/dev/ch34x_pis*`)
|
||||||
|
|
||||||
|
## 需要在另一台电脑上做的事
|
||||||
|
|
||||||
|
### 1. COM 口号必须改
|
||||||
|
|
||||||
|
目前每个文件的默认 COM 口不一致(COM6/COM9),换电脑后串口号会变。好消息是大多数文件都支持 `--port` 参数:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
python gamepad_remote_new_pcb.py --port COM5
|
||||||
|
python keyboard_remote_new_pcb.py --port COM5
|
||||||
|
```
|
||||||
|
|
||||||
|
但有三个文件把 COM 口写死在了 `__main__` 里:
|
||||||
|
|
||||||
|
| 文件 | 硬编码位置 |
|
||||||
|
|------|-----------|
|
||||||
|
| [serial_robot_driver.py:61](serial_robot_driver.py#L61) | `RobotDriver(port='COM9')` |
|
||||||
|
| [demo_auto_grasp.py:340](demo_auto_grasp.py#L340) | `RobotDriver(port='COM9')` |
|
||||||
|
| [gamepad_remote.py:195](gamepad_remote.py#L195) | `port = "COM9"` |
|
||||||
|
|
||||||
|
### 2. 缺少依赖清单
|
||||||
|
|
||||||
|
目前没有 `requirements.txt`,新机器需要手动装包。
|
||||||
|
|
||||||
|
### 3. CH341 驱动
|
||||||
|
|
||||||
|
`lib/ch341/` 下有 DLL/SO,但 **Windows 上还需要先安装 CH341PAR.exe 驱动** 才能让系统识别 CH341 芯片。这个 exe 不在仓库里。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
要我帮你做一个标准化的 Python 项目改造吗?具体做这几件事:
|
||||||
|
|
||||||
|
1. 创建 `requirements.txt`,列出所有 pip 依赖
|
||||||
|
2. 统一所有文件的 COM 口配置方式(都改为 `--port` 命令行参数)
|
||||||
|
3. 在 README 中补充"在新电脑上部署"的 checklist
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
|
||||||
|
**Claude**
|
||||||
|
|
||||||
|
现在更新文件清单,把驱动压缩包也列进去。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
|
||||||
|
**Claude**
|
||||||
|
|
||||||
|
已完成。主要改动:
|
||||||
|
|
||||||
|
1. **环境准备章节开头加了醒目提示**:必须先安装驱动,再装 Python 依赖
|
||||||
|
2. **CH341 驱动安装提到 `1.1`(最前面)**,Python 依赖降到 `1.2`
|
||||||
|
3. **Windows 安装步骤指向项目内的压缩包** `CH341驱动(先卸载再安装).zip`:解压 → 卸载旧驱动 → 运行 exe 安装
|
||||||
|
4. **Linux 步骤也补充了**"重新插拔设备或重启"的提醒
|
||||||
|
5. **文件清单**中新增了驱动压缩包的条目
|
||||||
|
|
||||||
|
在另一台电脑上拿到这套代码后,对方按 README 顺序操作即可:先解压安装驱动 → 插上传感器 → `pip install` → 运行脚本。
|
||||||
|
|
||||||
|
---
|
||||||
BIN
CH341PAR .EXE
Normal file
BIN
CH341PAR .EXE
Normal file
Binary file not shown.
BIN
CH341驱动(先卸载再安装).zip
Normal file
BIN
CH341驱动(先卸载再安装).zip
Normal file
Binary file not shown.
746
README.md
746
README.md
@ -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
|
│ 应用层 (Application) │
|
||||||
git branch -M main
|
│ demo_auto_grasp.py test_dof_control.py │
|
||||||
git push -uf origin main
|
│ 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)
|
### 1.1 CH341 驱动安装(必须首先完成)
|
||||||
- [ ] [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)
|
|
||||||
|
|
||||||
***
|
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
|
1. 将 `lib/ch341/libch347.so` 放置到系统库路径或项目目录下。
|
||||||
Choose a self-explaining name for your project.
|
|
||||||
|
|
||||||
## Description
|
2. 配置 udev 规则(将 `john` 替换为你的用户名):
|
||||||
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.
|
```bash
|
||||||
|
# 将用户添加到 dialout 组
|
||||||
|
sudo usermod -a -G dialout john
|
||||||
|
|
||||||
## Badges
|
# 创建 udev 规则文件
|
||||||
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.
|
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
|
3. 重新插拔 CH341 设备,或重启计算机使 udev 规则生效。
|
||||||
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.
|
|
||||||
|
|
||||||
## Usage
|
### 1.2 Python 依赖
|
||||||
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.
|
|
||||||
|
|
||||||
## Support
|
推荐使用 Python 3.8 及以上版本。安装依赖:
|
||||||
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.
|
|
||||||
|
|
||||||
## Roadmap
|
```bash
|
||||||
If you have ideas for releases in the future, it is a good idea to list them in the README.
|
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
|
## 2. 模块说明
|
||||||
For open source projects, say how it is licensed.
|
|
||||||
|
|
||||||
## Project status
|
### 2.1 文件清单
|
||||||
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.
|
|
||||||
|
| 文件名 | 功能说明 |
|
||||||
|
|--------|----------|
|
||||||
|
| `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 # 本文件
|
||||||
|
```
|
||||||
|
|||||||
35
cap_read.py
35
cap_read.py
@ -8,10 +8,13 @@ from collections import namedtuple
|
|||||||
import socket
|
import socket
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
DEF_CDC_SYNC_MS = 1000 #电容同步间隔
|
DEF_CDC_SYNC_MS = 1000 #电容同步间隔
|
||||||
DEF_GET_CAP_MS = (30) #读取电容间隔
|
DEF_GET_CAP_MS = (30) #读取电容间隔
|
||||||
DEF_PRO_CYC = 100
|
DEF_PRO_CYC = 100
|
||||||
|
|
||||||
|
DEF_MAX_FINGER_NUM = 1 #需要连接的手指数量,最大5个
|
||||||
|
|
||||||
# 定义一个全局的队列,用于线程间通信
|
# 定义一个全局的队列,用于线程间通信
|
||||||
capReadQueue = queue.Queue()
|
capReadQueue = queue.Queue()
|
||||||
|
|
||||||
@ -30,7 +33,7 @@ class ClassCapRead:
|
|||||||
|
|
||||||
# 最大连接5个手指
|
# 最大连接5个手指
|
||||||
self.fingers = list() # 传感器列表
|
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.fingers.append(ClassFinger(2+i, self.ch341))
|
||||||
|
|
||||||
self.currCh341State = 0 # 当前ch341连接状态
|
self.currCh341State = 0 # 当前ch341连接状态
|
||||||
@ -45,7 +48,7 @@ class ClassCapRead:
|
|||||||
self.syncTimer = 0
|
self.syncTimer = 0
|
||||||
|
|
||||||
self.connectStatus = EnumCh341ConnectStatus.CH341_CONNECT_INIT
|
self.connectStatus = EnumCh341ConnectStatus.CH341_CONNECT_INIT
|
||||||
|
self.running = True # <-- 新增这一行
|
||||||
self.connectDebug()
|
self.connectDebug()
|
||||||
|
|
||||||
def __del__(self):
|
def __del__(self):
|
||||||
@ -164,26 +167,36 @@ class ClassCapRead:
|
|||||||
if connectedSensorCnt > 1 and (time.time() - self.syncTimer) > DEF_CDC_SYNC_MS:
|
if connectedSensorCnt > 1 and (time.time() - self.syncTimer) > DEF_CDC_SYNC_MS:
|
||||||
self.syncTimer = time.time()
|
self.syncTimer = time.time()
|
||||||
self.set_sensor_enable(connectedSensorChan)
|
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:
|
if self.connectStatus == EnumCh341ConnectStatus.CH341_CONNECT_CHECK:
|
||||||
capReadTime = time.time()
|
capReadTime = time.time()
|
||||||
difftime = int(capReadTime*1000-ms_capReadTime*1000)
|
difftime = int(capReadTime*1000-ms_capReadTime*1000)
|
||||||
#print(f"diffTime={difftime}")
|
#print(f"diffTime={difftime}")
|
||||||
#定时器在任务完成后重新启动
|
#定时器在任务完成后重新启动
|
||||||
if(difftime>DEF_GET_CAP_MS):
|
if self.running: # 新增判断
|
||||||
timer = threading.Timer(DEF_GET_CAP_MS/1000, self.capRead)
|
if(difftime>DEF_GET_CAP_MS):
|
||||||
else:
|
timer = threading.Timer(DEF_GET_CAP_MS/1000, self.capRead)
|
||||||
timer = threading.Timer((DEF_GET_CAP_MS-difftime)/1000, self.capRead)
|
else:
|
||||||
|
timer = threading.Timer((DEF_GET_CAP_MS-difftime)/1000, self.capRead)
|
||||||
timer.start()
|
timer.start()
|
||||||
|
|
||||||
|
|
||||||
def capReadThread():
|
def capReadThread():
|
||||||
# 线程的主体功能
|
|
||||||
cap = ClassCapRead()
|
cap = ClassCapRead()
|
||||||
|
try:
|
||||||
while True:
|
while True:
|
||||||
cap.ch341Connect()
|
cap.ch341Connect()
|
||||||
time.sleep(DEF_PRO_CYC/1000)
|
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():
|
def main():
|
||||||
capReadThread()
|
capReadThread()
|
||||||
|
|||||||
@ -33,12 +33,12 @@ class ClassCh341:
|
|||||||
|
|
||||||
def init(self):
|
def init(self):
|
||||||
if os.name == 'nt': # Windows 环境
|
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':
|
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)
|
dllExist = os.path.exists(libPath)
|
||||||
if not dllExist:
|
if not dllExist:
|
||||||
print('未找到库文件')
|
print('未找到库文件:', libPath)
|
||||||
return False
|
return False
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
|
|||||||
@ -73,8 +73,12 @@ class ClassFinger:
|
|||||||
print(f"finger connected, project = {self.projectPara.name}")
|
print(f"finger connected, project = {self.projectPara.name}")
|
||||||
findProjectFlg = True
|
findProjectFlg = True
|
||||||
|
|
||||||
if findProjectFlg == False:
|
if not findProjectFlg:
|
||||||
print("not found vailed project, project para use default")
|
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)
|
self.connected(addrRead)
|
||||||
|
|
||||||
@ -146,7 +150,7 @@ class ClassFinger:
|
|||||||
for i in range(self.projectPara.ydds_num):
|
for i in range(self.projectPara.ydds_num):
|
||||||
offset = yddsOffset + i * struct_size
|
offset = yddsOffset + i * struct_size
|
||||||
struct_data = self.data[offset : offset + 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 = [value & 0xFF for value in struct_data]
|
||||||
struct_data = bytes(struct_data) # 转换为 bytes 类型
|
struct_data = bytes(struct_data) # 转换为 bytes 类型
|
||||||
instance = DynamicYddsU16Ts.from_buffer_copy(struct_data)
|
instance = DynamicYddsU16Ts.from_buffer_copy(struct_data)
|
||||||
@ -165,15 +169,15 @@ class ClassFinger:
|
|||||||
((self.data[mProxOffset + i*self.projectPara.cap_byte + 2] & 0xFF) << 16))
|
((self.data[mProxOffset + i*self.projectPara.cap_byte + 2] & 0xFF) << 16))
|
||||||
|
|
||||||
|
|
||||||
print(f"capChannelDat={self.readData.channelCapData}")
|
# print(f"capChannelDat={self.readData.channelCapData}")
|
||||||
for i in range(self.projectPara.ydds_num):
|
# for i in range(self.projectPara.ydds_num):
|
||||||
print(f"nf[{i}] = {self.readData.nf[i]}")
|
# print(f"nf[{i}] = {self.readData.nf[i]}")
|
||||||
print(f"tf[{i}] = {self.readData.tf[i]}")
|
# print(f"tf[{i}] = {self.readData.tf[i]}")
|
||||||
print(f"tfDir[{i}] = {self.readData.tfDir[i]}")
|
# print(f"tfDir[{i}] = {self.readData.tfDir[i]}")
|
||||||
for i in range(self.projectPara.s_prox_num):
|
# for i in range(self.projectPara.s_prox_num):
|
||||||
print(f"sProxCapData[{i}] = {self.readData.sProxCapData[i]}")
|
# print(f"sProxCapData[{i}] = {self.readData.sProxCapData[i]}")
|
||||||
for i in range(self.projectPara.m_prox_num):
|
# for i in range(self.projectPara.m_prox_num):
|
||||||
print(f"sProxCapData[{i}] = {self.readData.mProxCapData[i]}")
|
# print(f"mProxCapData[{i}] = {self.readData.mProxCapData[i]}")
|
||||||
|
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
|
|||||||
@ -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}")
|
#print(f"time={time.time()},len(buf) = {len(buf)}, tarLen={tarLen}, buf[0]={buf[0]}, buf[1]={buf[1]}, sum={checkSum}")
|
||||||
pass
|
pass
|
||||||
return False
|
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
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
6
data/test_data_20260105_143249.csv
Normal file
6
data/test_data_20260105_143249.csv
Normal 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
|
||||||
|
6
data/test_data_20260107_113555.csv
Normal file
6
data/test_data_20260107_113555.csv
Normal 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
|
||||||
|
85
data_logger.py
Normal file
85
data_logger.py
Normal 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
332
demo_auto_grasp.py
Normal 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
173
gamepad_remote.py
Normal 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
188
gamepad_remote_new_pcb.py
Normal 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
113
grasp_network_model.py
Normal 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
310
keyboard_remote_new_pcb.py
Normal 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
5
requirements.txt
Normal 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
69
robot_driver.py
Normal 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
48
serial_robot_driver.py
Normal 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.")
|
||||||
248
stm32f103_drv8870_servo_main.c
Normal file
248
stm32f103_drv8870_servo_main.c
Normal 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
228
tactile_sensor_daq.py
Normal 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
146
test_dof_control.py
Normal 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()
|
||||||
BIN
传感器iic地址和转接板位置定义.png
Normal file
BIN
传感器iic地址和转接板位置定义.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 659 KiB |
Reference in New Issue
Block a user