GPIO port controls the stepper motor (3399 board)

To control the stepper motor on the 3399, that is, to control the GPIO port, you need to use the library WiringPi. For related information and usage methods, refer to http://wiki.friendlyarm.com/wiki/index.php/WiringPi_for_RK3399/zh and http://wiki .friendlyarm.com/wiki/index.php/WiringPi-Python_for_RK3399/zh, here only record the key points, the problems I encountered, and the solutions to the problems (
1) Installation method (copy from the reference page)
# Delete the old implementation
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
# Download and install 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

(2) Test installation (note that root authority is required)
root@NanoPi-M4:~# gpio readall

Note: This command is very important. When writing shell scripts or python codes, the stepper motor is connected to the 3399 through the motor control card. After the GPIO port is installed, you need to use this command to view the corresponding operation port
(3) Use the shell script to control the motor
    and display the relevant information of each pin through gpio readall. When using the shell script, the operating interface is the corresponding GPIO. For example, the physical pins connected to my motor are 7, 11, 13, and 15, and the interfaces operated in the following shell script are 32, 33, 35, and 36 respectively. The following code is based on the timing of the motor I am using for forward rotation, and the number of rotation steps is 100. The reverse rotation is similar, but the timing is different.
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

(4) Use the python file to control the motor
    The following codes are part of the python code combination corresponding to the above shell script, but attention should be paid: (1) The pin of the control port is the wPi port described in (2), for example, the physical pins are 7 and 11 , 13, 15, the corresponding operation interfaces in the shell script are 32, 33, 35, 36, and the python operation interfaces are 7, 0, 2, 3 respectively; (2) the sleep time between timings in the python code and It is different in the shell script, only sleep 0.001 is needed in the shell, and if time.sleep(0.001) is used in the python code, the motor will not move, but the motor can be felt by hand, so change the sleep time to time.sleep (0.005), or even larger, the motor can rotate.
    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

 

Guess you like

Origin blog.csdn.net/ali1174/article/details/92675618