GPIO口控制步进电机(3399板子)

在3399上控制步进电机,即控制GPIO口,需要使用库WiringPi,其相关资料和使用方法参考http://wiki.friendlyarm.com/wiki/index.php/WiringPi_for_RK3399/zh和http://wiki.friendlyarm.com/wiki/index.php/WiringPi-Python_for_RK3399/zh,这里只记录重点和我遇到的问题,以及问题的解决方法
(一)安装方法(参考网页中复制)
# 删除旧的实现
wget http://112.124.9.243:8888/wiringpi/friendlyelec-rk3399/remove_oldversion_wiringPi.sh
chmod 755 remove_oldversion_wiringPi.sh
sudo ./remove_oldversion_wiringPi.sh
# 下载并安装wiringPi for RK3399
wget http://112.124.9.243:8888/wiringpi/friendlyelec-rk3399/wiringpi-v2.44-friendlyelec-rk3399.deb
sudo dpkg -i  wiringpi-v2.44-friendlyelec-rk3399.deb

(二)测试安装(注意需要root权限)
root@NanoPi-M4:~# gpio readall
GPIO口控制步进电机(3399板子)_第1张图片
注意:这条命令非常重要,在后续编写shell脚本或者python代码时,步进电机通过电机控制卡连接3399上面相应的GPIO口后,需要用此命令查看对应的操作口
(三)使用shell脚本控制电机
    通过gpio readall显示各引脚相关信息,使用shell脚本时,操作的接口为对应的GPIO。例如,我的电机连接的物理引脚为7、11、13、15,则在以下的shell脚本中操作的接口分别为32、33、35、36。以下代码是根据我所使用的电机时序进行正转,且转动步数为100,反转类似,只是时序不同而已。
if [ ! -d "/sys/class/gpio/gpio32/" ];then
echo 32 > /sys/class/gpio/export
fi
echo out > /sys/class/gpio/gpio32/direction
if [ ! -d "/sys/class/gpio/gpio33/" ];then
echo 33 > /sys/class/gpio/export
fi
echo out > /sys/class/gpio/gpio33/direction
if [ ! -d "/sys/class/gpio/gpio35/" ];then
echo 35 > /sys/class/gpio/export
fi
echo out > /sys/class/gpio/gpio35/direction
if [ ! -d "/sys/class/gpio/gpio36/" ];then
echo 36 > /sys/class/gpio/export
fi
echo out > /sys/class/gpio/gpio36/direction
for((i=0;i<100;i++));do
cd /sys/class/gpio/gpio32
echo 1 > value
cd /sys/class/gpio/gpio33
echo 1 > value
cd /sys/class/gpio/gpio35
echo 0 > value
cd /sys/class/gpio/gpio36
echo 0 > value
sleep 0.001
cd /sys/class/gpio/gpio32
echo 0 > value
cd /sys/class/gpio/gpio33
echo 1 > value
cd /sys/class/gpio/gpio35
echo 1 > value
cd /sys/class/gpio/gpio36
echo 0 > value
sleep 0.001
cd /sys/class/gpio/gpio32
echo 0 > value
cd /sys/class/gpio/gpio33
echo 0 > value
cd /sys/class/gpio/gpio35
echo 1 > value
cd /sys/class/gpio/gpio36
echo 1 > value
sleep 0.001
cd /sys/class/gpio/gpio32
echo 1 > value
cd /sys/class/gpio/gpio33
echo 0 > value
cd /sys/class/gpio/gpio35
echo 0 > value
cd /sys/class/gpio/gpio36
echo 1 > value
sleep 0.001
cd /sys/class/gpio/gpio32
echo 0 > value
cd /sys/class/gpio/gpio33
echo 0 > value
cd /sys/class/gpio/gpio35
echo 0 > value
cd /sys/class/gpio/gpio36
echo 0 > value
done

(四)使用python文件控制电机
    以下代码为上述shell脚本对应的python部分代码组合,但需要注意:(1)控制口pin为(二)中所述的wPi口,例如物理引脚为7、11、13、15,对应的shell脚本中操作的接口分别为32、33、35、36,而python操作口分别为7、0、2、3;(2)python代码中时序之间的sleep时间与shell脚本中不同,shell中只需sleep 0.001,而python代码中若使用time.sleep(0.001),则会出现电机不动,但用手可以感觉到电机在震动,将sleep时间修改为time.sleep(0.005),甚至更大后,电机即可转动.
    self.pin1 = 7
    self.pin2 = 0
    self.pin2 = 2
    self.pin1 = 3

        wpi.wiringPiSetup()
        wpi.pinMode(self.pin1, wpi.GPIO.OUTPUT)  # 1)
        wpi.pinMode(self.pin2, wpi.GPIO.OUTPUT)  # 1)
        wpi.pinMode(self.pin3, wpi.GPIO.OUTPUT)  # 1)
        wpi.pinMode(self.pin4, wpi.GPIO.OUTPUT)  # 1)
        time.sleep(0.05)

        wpi.digitalWrite(self.pin1, 0)
        wpi.digitalWrite(self.pin2, 0)
        wpi.digitalWrite(self.pin3, 0)
        wpi.digitalWrite(self.pin4, 0)
        time.sleep(0.01)

        while i < 100:
            wpi.digitalWrite(self.pin1, 0)
            wpi.digitalWrite(self.pin2, 0)
            wpi.digitalWrite(self.pin3, 0)
            wpi.digitalWrite(self.pin4, 0)
            wpi.digitalWrite(self.pin1, 1)
            wpi.digitalWrite(self.pin2, 1)
            wpi.digitalWrite(self.pin3, 0)
            wpi.digitalWrite(self.pin4, 0)
            time.sleep(0.005)

            wpi.digitalWrite(self.pin1, 0)
            wpi.digitalWrite(self.pin2, 0)
            wpi.digitalWrite(self.pin3, 0)
            wpi.digitalWrite(self.pin4, 0)
            wpi.digitalWrite(self.pin1, 0)
            wpi.digitalWrite(self.pin2, 1)
            wpi.digitalWrite(self.pin3, 1)
            wpi.digitalWrite(self.pin4, 0)
            time.sleep(0.005)

            wpi.digitalWrite(self.pin1, 0)
            wpi.digitalWrite(self.pin2, 0)
            wpi.digitalWrite(self.pin3, 0)
            wpi.digitalWrite(self.pin4, 0)
            wpi.digitalWrite(self.pin1, 0)
            wpi.digitalWrite(self.pin2, 0)
            wpi.digitalWrite(self.pin3, 1)
            wpi.digitalWrite(self.pin4, 1)
            time.sleep(0.005)

            wpi.digitalWrite(self.pin1, 0)
            wpi.digitalWrite(self.pin2, 0)
            wpi.digitalWrite(self.pin3, 0)
            wpi.digitalWrite(self.pin4, 0)
            wpi.digitalWrite(self.pin1, 1)
            wpi.digitalWrite(self.pin2, 0)
            wpi.digitalWrite(self.pin3, 0)
            wpi.digitalWrite(self.pin4, 1)
            time.sleep(0.005)

            wpi.digitalWrite(self.pin1, 0)
            wpi.digitalWrite(self.pin2, 0)
            wpi.digitalWrite(self.pin3, 0)
            wpi.digitalWrite(self.pin4, 0)

            # print(i)
            i += 1

 

你可能感兴趣的:(GPIO,步进电机,gpio,3399)