Robot car with stepper motors. Tomas, Alexander and Mikkel 2017 IoT

We have been working with the robot car using 2x stepper motors. We started out by figuring out how to setup the hardware, using a raspberry pi a BCM board and the stepper motors. We connected the stepper motors to the board and the board to the raspberry pi.

On the picture of the raspberry pi we used these pins for each motor:

ControlPin = [4,14,15,18]
ControlPin2 = [19,16,20,21]

View post on imgur.com

Robotcar board

Be aware that hardware problems can appear, we experienced this, once with some caples not working and a driver (The little red board that are connected to the stepper motor) also we tried that our raspberry pi did not work.

When we got the hardware setup we thought about our intentions with the project, where we agreed that in the end we should be able to control it by using a laptop keyboard with the function of turning left and right as well as moving forward and backwards. We decided to use the W (forward) A ( left) S (backwards) D (right)  Z(backwards left) X(backwards right)
Before we started using keys to control our stepping motors we tried to make them drive without them just to make it work, by using two sequence code. Each of the controls were use to make the 2 motors move forward and backward, the reason we use 2 was because our motors was placed mirrored for each other.

This is the 2 sequences we used:

seq = [ [1,0,0,0],
        [1,1,0,0],
        [0,1,0,0],
        [0,1,1,0],
        [0,0,1,0],
        [0,0,1,1],
        [0,0,0,1],
        [1,0,0,1] ]

seq2= [ [1,0,0,1],
        [0,0,0,1],
        [0,0,1,1],
        [0,0,1,0],
        [0,1,1,0],
        [0,1,0,0],
        [1,1,0,0],
        [1,0,0,0] ]

After that we made our 4 definition for which way to wheels should move by using the 2 seg controls. Like our seg are 2 definition of each of our motors made 1 for forward and 1 for backward

def loop_fr():
        for i in range(512):
                for halfstep in range(8):
                        for pin in range(4):
                                GPIO.output(ControlPin2[pin], seq2[halfstep][pin])
                        time.sleep(0.001)

def loop_fl():
        for x in range (512):
                for halfstep in range(8):
                        for pin in range(4):
                                GPIO.output(ControlPin[pin], seq[halfstep][pin])
                        time.sleep(0.001)




def loop_br():
        for i in range(512):
                for halfstep in range(8):
                        for pin in range(4):
                                GPIO.output(ControlPin2[pin], seq[halfstep][pin])
                        time.sleep(0.001)

def loop_bl():
        for i in range(512):
                for halfstep in range(8):
                        for pin in range(4):
                                GPIO.output(ControlPin[pin], seq2[halfstep][pin])
                        time.sleep(0.001)

Before we got it moving with the arrow keys, we used threads and joined them to get the 2 motors to run at the same time ( we did continue using these later on also) but we had to execute each part of the program to make the wheels move.

The way we found the value for the keyboard keys was using this code which allowed us to see which value the key we press was.

char = screen.getch()
print(char) #used to find value of a keypress

When we had found the values for the keys we wanted to use we proceed to make a while true loop where we was going to control which way the car would move. the code contains if statements which each are use to make the car move a specific way by pressing the key.

We are using 2 different if statements, for moving forward and backward we are using threads to make the 2 motors to run at the same time and another statement to make it turn left or right

while True:
        char = screen.getch()
        #print(char) #used to find value of a keypress
        if (char==100):
                print ('Right') #Key D
                loop_fr()
        if (char==97):
                print('Left') # Key: A
                loop_fl()
        if (char==122):
                print('Back left') #Z
                loop_bl()
        if (char==120):
                print('Back right') #X
                loop_br()
        if (char==119):
                print ('Forward')#W
                threadA=Thread(target=loop_fr)
                threadB=Thread(target=loop_fl)
                threadA.start()
                threadB.start()
		threadA.join()
                threadB.join()
        if (char==115):
                print('Back') #S
                threadD=Thread(target=loop_br)
                threadC=Thread(target=loop_bl)
                threadD.start()
                threadC.start()
                threadD.join()
                threadC.join()
        if (char==32):
                threadA=Thread(target=loop_fr)
                threadC=Thread(target=loop_bl)
                threadA.start()
                threadC.start()
                threadA.join()
                threadC.join()
                print('breakdance!')
#breackdance code is for turning at one point without moving forward at the same time.

GPIO.cleanup()

 

The complete code is inserted under this:

import RPi.GPIO as GPIO
import time
from threading import Thread
import curses
import getch

GPIO.setmode(GPIO.BCM)
screen = curses.initscr()
ControlPin = [4,14,15,18]
ControlPin2 = [19,16,20,21]

for pin in ControlPin:
        GPIO.setup(pin,GPIO.OUT)
        GPIO.output(pin,0)

for pin in ControlPin2:
        GPIO.setup(pin,GPIO.OUT)
        GPIO.output(pin,0)


seq = [ [1,0,0,0],
        [1,1,0,0],
        [0,1,0,0],
        [0,1,1,0],
        [0,0,1,0],
        [0,0,1,1],
        [0,0,0,1],
        [1,0,0,1] ]

seq2= [ [1,0,0,1],
        [0,0,0,1],
        [0,0,1,1],
        [0,0,1,0],
        [0,1,1,0],
        [0,1,0,0],
        [1,1,0,0],
        [1,0,0,0] ]

def loop_fr():
        for i in range(512):
                for halfstep in range(8):
                        for pin in range(4):
                                GPIO.output(ControlPin2[pin], seq2[halfstep][pin])
                        time.sleep(0.001)

def loop_fl():
        for x in range (512):
                for halfstep in range(8):
                        for pin in range(4):
                                GPIO.output(ControlPin[pin], seq[halfstep][pin])
                        time.sleep(0.001)

def loop_br():
        for i in range(512):
                for halfstep in range(8):
                        for pin in range(4):
                                GPIO.output(ControlPin2[pin], seq[halfstep][pin])
                        time.sleep(0.001)

def loop_bl():
        for i in range(512):
                for halfstep in range(8):
                        for pin in range(4):
                                GPIO.output(ControlPin[pin], seq2[halfstep][pin])
                        time.sleep(0.001)




text = ('W=Forward, A=Left, D=Right, S=Back, Z=Back Left, X=Back Right')
print(text)

while True:
        char = screen.getch()
        #print(char) #used to find value of a keypress
        if (char==100):
                print ('Right')
                loop_fr()
        if (char==97):
                print('Left')
                loop_fl()
        if (char==122):
                print('Back left')
                loop_bl()
        if (char==120):
                print('Back right')
                loop_br()
        if (char==119):
                print ('Forward')
                threadA=Thread(target=loop_fr)
                threadB=Thread(target=loop_fl)
                threadA.start()
                threadB.start()
		threadA.join()
                threadB.join()
        if (char==115):
                print('Back')
                threadD=Thread(target=loop_br)
                threadC=Thread(target=loop_bl)
                threadD.start()
                threadC.start()
                threadD.join()
                threadC.join()
        if (char==32):
                threadA=Thread(target=loop_fr)
                threadC=Thread(target=loop_bl)
                threadA.start()
                threadC.start()
                threadA.join()
                threadC.join()
                print('breakdance!')

GPIO.cleanup()