Thursday, September 29, 2016

Raspberry Pi & Arch Linux - Day 7 - Stepper Motor




Today, we made significant progress: we set up the rotational radar mentioned in the structure last time.

The basic components of the rotational radar are:




























To connect them together, we will need some LEGO bricks. The final structure looks like this:














Before we move forward, we have to have some basic understanding of the stepper motors. There are very good documentations and posts online, here is a list of them:


With the driver board, wiring is very easy. Just keep in mind that the motor and the raspberry should share the same Ground. The raspberry pi 5V pin should be able to driver the motor without any issues. If your motor vibrates but not spins, you may check:

  • the setting of speed is not too fast or too slow. If the speed is set too high, the motor will fall behind the control signal; if the speed is set too low, then movement of the motor is not visible.
  • the step sequences are wrong.

The notion of speed (step angle, revolution) and step sequences can be found in the linked above. The speed is controlled by setting the delay between to two output signals.

I attached my code here. If you want to use the code, make sure you update the pin number.



import RPi.GPIO as gpio
import time

gpio.setmode(gpio.BOARD)


in_1 = 3
in_2 = 5
in_3 = 7
in_4 = 11


# set all motor input to 0
gpio.setup(in_1, gpio.OUT, initial=0)
gpio.setup(in_2, gpio.OUT, initial=0)
gpio.setup(in_3, gpio.OUT, initial=0)
gpio.setup(in_4, gpio.OUT, initial=0)


time.sleep(1.5)


def rotate(pins, degree=None, clockwise=False, delay=0.002):
    if clockwise:
        in_1, in_2, in_3, in_4 = pins[::-1]
    else:
        in_1, in_2, in_3, in_4 = pins

    if degree is None:
        remaining = 1
    else:
        remaining = degree
    
    print(in_1,in_2,in_3,in_4)

    stepsize = 360. / 4096.  * 8
    while degree is None or remaining > 0:
        print('remaining: {}'.format(remaining))
        gpio.output(in_1, 1); time.sleep(delay); 
        gpio.output(in_4, 0); time.sleep(delay); 
        gpio.output(in_2, 1); time.sleep(delay); 
        gpio.output(in_1, 0); time.sleep(delay); 
        gpio.output(in_3, 1); time.sleep(delay); 
        gpio.output(in_2, 0); time.sleep(delay); 
        gpio.output(in_4, 1); time.sleep(delay); 
        gpio.output(in_3, 0); time.sleep(delay); 
        remaining -= stepsize

def swing(pins, degree=180, delay=0.002):
    while True:
        rotate(pins, degree=degree, clockwise=True,delay=delay)
        for pin in pins:
            gpio.output(pin,0)
        time.sleep(3 * delay)
        rotate(pins, degree=degree, clockwise=False,delay=delay)
        for pin in pins:
            gpio.output(pin,0)
        time.sleep(3 * delay)

if __name__ == '__main__':
    print("starat")
    time.sleep(2)
    pins = [in_1, in_2, in_3, in_4 ]
    #rotate([in_1, in_2, in_3, in_4], degree=360)
    swing(pins, degree=180)
    


Here is the video:



The next step is to connect the distance sensor to raspberry pi and write code to process the data.



No comments:

Post a Comment