一、安装软件环境
1.安装PIP
- apt-get install python3-pip
2. 编写安装脚本
(安装脚本参考官网教程)
- https://doc.rvspace.org/VisionFive2/Application_Notes/VisionFive2_AN_P_Using_PWM_LED/VisionFive2_ApplicationNotes/Shared_preparing_software%20-%20vf2.html
3. 运行安装脚本
- python3 Install_VisionFive_gpio.py
二、编写执行代码
MG-996R舵机是一个360度舵机,驱动信号需要是频率为50Hz,幅值大于3V的PWM信号,通过改变PWM信号的占空比来控制舵机转动的速度,而不是控制舵机转动的角度。
import time
import numpy as np
import VisionFive.gpio as GPIO
led_pin = 22
def initPin():
# Set the gpio mode as 'BOARD'.
GPIO.setmode(GPIO.BOARD)
# Configure the direction of led_pin as out.
GPIO.setup(led_pin, GPIO.OUT)
# Configure the voltage level of led_pin as high.
GPIO.output(led_pin, GPIO.HIGH)
global p
# Configure the frequency as 50,if just set the frequency as 50,it will can't drive the servo
p = GPIO.PWM(led_pin, 53)
# Initialize the duty ratio as 0.
p.start(0)
if __name__ == "__main__":
initPin()
while True:
try:
for dc in np.arange(0, 12.5, 1):
p.ChangeDutyRatio(dc)
time.sleep(1)
for dc in np.arange(12.5, 0, 1):
p.ChangeDutyRatio(dc)
time.sleep(1)
except KeyboardInterrupt:
break
文章评论(0条评论)
登录后参与讨论