树莓派舵机控制

raspberry-pi-servo

by cld1994

通过硬件PWM来控制树莓派上的舵机. 何时触发: 当需要精确控制舵机时.

4.5k其他未扫描2026年3月23日

安装

claude skill add --url github.com/openclaw/skills/tree/main/skills/cld1994/raspberry-pi-servo

文档

Raspberry Pi Servo Skill

rpi-hardware-pwm

rpi-hardware-pwm 是一个专门用于访问树莓派硬件 PWM 功能的 Python 库.
与常见的通过 GPIO 库实现的软件 PWM 不同, 它直接调用树莓派内核提供的硬件控制接口.

检查硬件 PWM 是否启用

  1. 运行 lsmod | grep pwm 来检查系统内核是否启用了 PWM 模块.

  2. 硬件 PWM 开启后, 内核会在 /sys/class/pwm/ 下创建控制目录. 执行以下命令:

    bash
    ls /sys/class/pwm/
    

    如果返回空或文件夹不存在, 则硬件 PWM 未开启.

启用硬件 PWM

如果硬件 PWM 未开启, 则执行下列流程:

  1. /boot/firmware/config.txt 文件添加 dtoverlay=pwm-2chan.

    config.txt 是什么? 树莓派设备使用一个名为 config.txt 的配置文件, 而不是传统 PC 上的 BIOS. 树莓派操作系统会在位于 /boot/firmware/ 的启动分区中查找此文件. 注意: 在 Raspberry Pi OS Bookworm 之前,Raspberry Pi OS 将启动分区存储在 /boot/ 中.

    默认情况下, GPIO_18 作为 PWM0 的引脚, GPIO_19 作为 PWM1 的引脚.
    或者,可以使用 dtoverlay=pwm-2chan,pin=12,func=4,pin2=13,func2=4 将 GPIO_18 更改为 GPIO_12,将 GPIO_19 更改为 GPIO_13。

    在 树莓派5 上, 分别使用通道 0 和 1 来控制 GPIO_12 和 GPIO_13; 分别使用通道 2 和 3 来控制 GPIO_18 和 GPIO_19.
    在所有其他型号上, 分别使用通道 0 和 1 来控制 GPIO_18 和 GPIO_19.

  2. 申请重启树莓派 注意: 不要擅自重启, 必须向用户提出申请.

安装

  1. 激活 python 虚拟环境

    判断在用户目录下是否存在 .venv 目录

    如果不存在则执行下列命令进行创建:

    bash
    python3 -m venv ~/.venv
    

    最后, 执行下列命令激活虚拟环境:

    bash
    source ~/.venv/bin/activate
    
  2. 执行 pip 安装命令

    bash
    pip install rpi-hardware-pwm
    

使用指南

注意, 每次使用前都得确保激活位于 ~/.venv 的 python venv:

查看当前激活的是哪个虚拟环境:

bash
echo $VIRTUAL_ENV

激活默认虚拟环境:

bash
source ~/.venv/bin/activate
python
from rpi_hardware_pwm import HardwarePWM

pwm = HardwarePWM(pwm_channel=0, hz=60, chip=0)
pwm.start(100) # full duty cycle

pwm.change_duty_cycle(50)
pwm.change_frequency(25_000)

pwm.stop()

排错指南

遇到错误的时候立刻阅读 troubleshooting 了解更多信息.

相关 Skills

Claude API

by anthropic

热门

Build, debug, and optimize Claude API / Anthropic SDK apps. Apps built with this skill should include prompt caching. Also handles migrating existing Claude API code between Claude model versions (4.5 → 4.6, 4.6 → 4.7, retired-model replacements). TRIGGER when: code imports `anthropic`/`@anthropic-ai/sdk`; user asks for the Claude API, Anthropic SDK, or Managed Agents; user adds/modifies/tunes a Claude feature (caching, thinking, compaction, tool use, batch, files, citations, memory) or model (Opus/Sonnet/Haiku) in a file; questions about prompt caching / cache hit rate in an Anthropic SDK project. SKIP: file imports `openai`/other-provider SDK, filename like `*-openai.py`/`*-generic.py`, provider-neutral code, general programming/ML.

其他
安全147.7k

并行代理

by axelhu

热门

Use when facing 2 or more independent tasks that can be worked on without shared state - dispatches parallel subagents using sessions_spawn for concurrent investigation and execution, adapted for OpenClaw

其他
未扫描4.5k

高光制作器

by bwbernardweston18

热门

>

其他
未扫描4.5k

评论