树莓派 DAGU ROVER5底盘的PWM控制

年初购入了DAGU ROVER5履带机器人底盘,计划利用树莓派边学边做一部履带机器人。利用Python已实现了手机WIFI控制小车的前进、后退、左转、右转。今天实验了PWM调速的控制,基本成功。网上Rover5 四通道控制板的资料不好找,特记录下来,供以后改进参考。

一、使用到的硬件

  1. Raspberry Pi 3 B+
  2. Rover 5 底盘
  3. Rover 4通道控制板

二、硬件介绍

  1. 树莓派3 B+ GPIO针脚定义图
  2. Rover 5 底盘    路虎5底盘除了有两驱和四驱之分外,还分带编码器版本和不带编码器版本。这里用的是四驱带编码器的版本,但是因为暂时没有弄懂编码器的使用,只能闲置这个功能了。
  3. Rover 4通道控制板

    路虎5的4通道控制板比较强大,暂时只用到了DIRECTION和PWM两个接口,其余功能研究中,暂无进展……..你也可参考上图的各接口定义慢慢研究。

三、Python 软件PWM控制电机转速

树莓派3 B+有4个GPIO支持硬件PWM(脉冲宽度调制),可参见上图RPI3 B+的GPIO阵脚接口定义,这里采用的是软件PWM,软件PWM的优点是任意GPIO脚都可实现PWM控制,有人说不稳定耗CPU资源,我暂时没去考虑这方面因素,先调试起来再说吧。Python中会用到RPi.GPIO模块,要实现PWM,RPi.GPIO 模块应在 0.5.2a或以上版本。

新建一个Python文件pwm_test.py(参考Cytron上Khairul_Tajudin的代码进行修改的):

import RPi.GPIO as GPIO # using RPi.GPIO module
from time import sleep # import function sleep for delay

GPIO.setmode(GPIO.BOARD) # GPIO pins
GPIO.setwarnings(False) # enable warning from GPIO

AN1 = 32 # set pwm1 pin to GPIO 32
AN2 = 36 # set pwm2 pin to GPIO 36
AN3 = 38 # set pwm3 pin to GPIO 38
AN4 = 40 # set pwm4 pin to GPIO 40

DIG1 = 31 # set dir2 pin to GPIO 31
DIG2 = 33 # set dir1 pin to GPIO 33
DIG3 = 35 # set dir2 pin to GPIO 35
DIG4 = 37 # set dir1 pin to GPIO 37

GPIO.setup(AN1, GPIO.OUT) # set pin as output
GPIO.setup(AN2, GPIO.OUT) # set pin as output
GPIO.setup(AN3, GPIO.OUT) # set pin as output
GPIO.setup(AN4, GPIO.OUT) # set pin as output
GPIO.setup(DIG1, GPIO.OUT) # set pin as output
GPIO.setup(DIG2, GPIO.OUT) # set pin as output
GPIO.setup(DIG3, GPIO.OUT) # set pin as output
GPIO.setup(DIG4, GPIO.OUT) # set pin as output
sleep(1) # delay for 1 seconds

p1 = GPIO.PWM(AN1, 100) # set pwm for M1
p2 = GPIO.PWM(AN2, 100) # set pwm for M2
p3 = GPIO.PWM(AN3, 100) # set pwm for M3
p4 = GPIO.PWM(AN4, 100) # set pwm for M4

try:
    while True:
        print(Forward) # display "Forward" when program executed
        GPIO.output(DIG1, GPIO.HIGH) # set DIG1 as high, dir1 = forward
        GPIO.output(DIG2, GPIO.HIGH) # set DIG2 as high, dir2 = forward
        GPIO.output(DIG3, GPIO.HIGH) # set DIG3 as high, dir3 = forward
        GPIO.output(DIG4, GPIO.HIGH) # set DIG4 as high, dir4 = forward
        p1.start(50) # set speed for M1, speed=0 – 100
        p2.start(50) # set speed for M2, speed=0 – 100
        p3.start(50) # set speed for M3, speed=0 – 100
        p4.start(50) # set speed for M4, speed=0 – 100
        sleep(1) # delay for 1 seconds

        print(Backward) # display "Backward" when program executed
        GPIO.output(DIG1, GPIO.LOW) # set DIG1 as low, dir1 = backward
        GPIO.output(DIG2, GPIO.LOW) # set DIG2 as low, dir2 = backward
        GPIO.output(DIG3, GPIO.LOW) # set DIG3 as low, dir3 = backward
        GPIO.output(DIG4, GPIO.LOW) # set DIG4 as low, dir4 = backward
        p1.start(50) # set speed for M1, speed=0 – 100
        p2.start(50) # set speed for M2, speed=0 – 100
        p3.start(50) # set speed for M3, speed=0 – 100
        p4.start(50) # set speed for M4, speed=0 – 100
        sleep(1) # delay for 1 seconds

        print(Left) # display "Left" when program executed
        GPIO.output(DIG1, GPIO.LOW) # set DIG1 as low, dir2 = backward
        GPIO.output(DIG2, GPIO.HIGH) # set DIG2 as high, dir1 = forward
        GPIO.output(DIG3, GPIO.HIGH) # set DIG3 as high, dir3 = forward
        GPIO.output(DIG4, GPIO.LOW) # set DIG4 as low, dir4 = backward
        p1.start(25) # set speed for M1
        p2.start(25) # set speed for M2
        p3.start(25) # set speed for M3
        p4.start(25) # set speed for M4
        sleep(1) # delay for 1 seconds

        print(Right) # display "Right" when program executed
        GPIO.output(DIG1, GPIO.HIGH) # set DIG1 as high, dir1 = forward
        GPIO.output(DIG2, GPIO.LOW) # set DIG2 as low, dir2 = backward
        GPIO.output(DIG3, GPIO.LOW) # set DIG3 as low, dir3 = backward
        GPIO.output(DIG4, GPIO.HIGH) # set DIG4 as high, dir4 = forward
        p1.start(25) # set speed for M1
        p2.start(25) # set speed for M2
        p3.start(25) # set speed for M3
        p4.start(25) # set speed for M4
        sleep(1) # delay for 1 seconds

except: # exit program when keyboard interrupt
        p1.stop() # set M1 stop
        p2.stop() # set M2 stop
        p3.stop() # set M3 stop
        p4.stop() # set M4 stop

在终端或Putty中运行代码(运行图来自Cytron):

sudo python pwm_test.py


按 CTRL+C 结束进程。如果中途出现报错请检查Python版本,代码是基于Python3的。

标签:

热门文章

Leave a Reply