当前位置:网站首页>树莓派输出PWM波驱动舵机
树莓派输出PWM波驱动舵机
2022-07-27 04:42:00 【-素心向暖】
PWM控制舵机简介
舵机转动的方向不是由占空比决定的,而是由脉冲长度 t 决定的。有的舵机使用的PWM频率为 fPWM=50HZ,其对应于的PWM周期 T=20 ms。脉冲长度 t 和转动方向之间的关系是线性的,但也取决于电机和齿轮的配合。如下图:
树莓派通过软件PWM的方式控制舵机
目的:将一个小舵机直接连接到树莓派的5伏电源上,并使用GPIO数字输出端口采用软件PWM的方式来控制它。
警告:只能使用微型舵机(如:SG90),因为树莓派5 V供电的限制,大型号的舵机电流过大对控制板安全不利。
连接舵机的棕色线(或黑色线)至树莓派的 GND, 舵机红色线至树莓派的 5 V,舵机黄色线连接至树莓派的任意 GPIO 输出端口(本文使用pin #23)。此时舵机由树莓派的 5 V供电, GPIO的控制电压是 3.3 V。(树莓派引脚可参见:树莓派的外部I/O接口)当然我们也可以使用充电宝给舵机供电。


import RPi.GPIO as GPIO
import time
P_SERVO = 23 # GPIO 端口号,根据实际修改
fPWM = 50 # HZ
a = 10
b = 2
def setup():
global pwm
GPIO.setmode(GPIO.BCM) # GPIO引脚编号的两种方式之一,另一种是BCM。所用编号方式不同,pin也不同。
GPIO.setup(P_SERVO, GPIO.OUT) # 设置P_SERVO引脚为输出模式
GPIO.setwarnings(False) # 禁用警告。
pwm = GPIO.PWM(P_SERVO, fPWM) # 在P_SERVO脚上产生频率为FPWM的PWM信号。
pwm.start(0) # 在引脚上设置一个初始PWM信号。
def setDirection(direction):
duty = a/180 * direction + b
pwm.ChangeDuttyCycle(duty) # 改变占空比
time.sleep(1)
if __name__ == '__main__':
setup()
for direction in range(0, 181, 10):
setDirection(direction) # 通过输入不同的“占空比值”来观察舵机的位置变化
direction = 0
setDirection(0)
GPIO.cleanup() # 清理GPIO。
注意:代码中的a和b参数必须与您所使用的舵机类型相匹配。如下图,舵机的占空比:
若第一次使用,可能出现
RuntimeError: No access to /dev/mem. Try running as root!
使用如下命令,将用户加入GPIO组别就好。
sudo apt install rpi.gpio-common
边栏推荐
- static和final关键字 学习 demo练习
- Huawei's entry into the commercial market: due to the trend, there are many challenges
- Final Cut Pro Chinese tutorial (1) basic understanding of Final Cut Pro
- R-score reproduction R-Precision evaluation index quantitative text generation image r-score quantitative experiment whole process reproduction (R-Precision) quantitative evaluation experiment step on
- ps怎么导入lut预设?Photoshop导入lut调色预设教程
- 写代码涉及到的斜杠/和反斜杠\
- Structural mode - facade mode
- Plane conversion (displacement, rotation, scaling)
- Solution to the third game of 2022 Hangzhou Electric Multi school league
- C language - two dimensional array, pointer
猜你喜欢

The execution process of a select statement in MySQL

ps怎么导入lut预设?Photoshop导入lut调色预设教程

STM32 Hal serial port (uart/usart) debugging experience (I) -- basic knowledge of serial port communication +hal library code understanding

单元测试chapter6

HCIA dynamic routing rip basic experiment

CDH cluster integration external Flink (improved version - keep pace with the times)

Photoshop提示暂存盘已满怎么办?ps暂存盘已满如何解决?
![[C language] detailed explanation of user-defined types (structure + enumeration + Union)](/img/d9/b10371159c63c126b5ff98bac0971a.png)
[C language] detailed explanation of user-defined types (structure + enumeration + Union)

TCP three handshakes and four disconnects

【牛客讨论区】第七章:构建安全高效的企业服务
随机推荐
测试基础5
Final Cut Pro中文教程 (1) 基础认识Final Cut Pro
The usage syntax and scene of selector, as well as the usage of background picture size, text box shadow and excessive effect
js小技巧
消防安全培训资料汇总
「Photoshop2021入门教程」调整图片到不同的长宽比
再一个技巧,每月稳赚3万+
[search] connectivity model of DFS + search order
How do I reset Photoshop preferences? PS method of resetting preferences
What if Photoshop prompts that the temporary storage disk is full? How to solve the problem that PS temporary storage disk is full?
Shell programming enhancements
Explanation of index failure principle and its common situations
安装Pygame
Easily download data in power Bi reports with power auto
Text processing tool in shell, cut [option parameter] filename Description: the default separator is the built-in variable of tab, awk [option parameter] '/pattern1/{action1}filename and awk
C language - two dimensional array, pointer
OFDM 十六讲 2- OFDM and the DFT
HCIA dynamic routing OSPF experiment
单元测试chapter6
HCIA dynamic routing rip basic experiment