原创 【昉·星光 2 高性能RISC-V单板计算机体验】四、使用VisionFive2驱动MG-996R舵机

2023-12-27 21:46 489 1 1 分类: MCU/ 嵌入式
一、安装软件环境
1.安装PIP
  1. apt-get install python3-pip


2. 编写安装脚本
(安装脚本参考官网教程)
  1. https://doc.rvspace.org/VisionFive2/Application_Notes/VisionFive2_AN_P_Using_PWM_LED/VisionFive2_ApplicationNotes/Shared_preparing_software%20-%20vf2.html

3. 运行安装脚本
  1. 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

            

PARTNER CONTENT

文章评论0条评论)

登录后参与讨论
EE直播间
更多
我要评论
0
1
关闭 站长推荐上一条 /3 下一条