C4RR#4: How Can Wheels and Different Sensors be Integrated to Create an Enhanced System of Movement?

Hi everyone,

          Sorry that it has been a while since I have posted. I encountered some issues with my design that I will elaborate upon later. This week, I decided to make it so that the two wheels could turn in reverse, which would enable me to move backwards and turn. I also wanted to combine the two systems into my final obstacle detection and reaction system.

Two Wheels

          I expected turning both wheels in the opposite direction to be easy, but it was not due to a mistake in my coding. Basically, there are three variables that are controlled in order to make the motors work.

  • Enable
  • A
  • B

          Enable is basically the on/off button of the motors. If the enable is not set at HIGH, then the motors will not operate. A and B control the direction at which the wheels turn. If A is set to LOW, then B is usually set to HIGH to turn it and vice versa. If both A and B are set the same (both LOW or both HIGH), then the wheels do not turn.

Here is a diagram explaining what settings allow for movement in what direction:

Wheel Control Settings

 

My Issue

         When I tested out the code to make both wheels move in reverse, one of the two wheels worked fine and the other just stopped moving. I had no idea what the issue was and I initially credited it to faulty circuiting. However, as I soon found, my circuit was fine which meant that the problem likely existed within the code. However, I was initially uncertain how to solve the issue and spent a long period of time researching the GPIO pins and seeing if my Raspberry Pi was malfunctioning.

         I finally found the solution on a thread with a similar issue to mine. It turns out that I had switched the values for the Enable and A. This was fine for moving forwards since both are set at HIGH. However, when it came to moving in reverse, A should be set to LOW. Since my values were switched, my code basically set the Enable to LOW and turned of the motor. Once I fixed this, both wheels were working again.

Here is the code that I initially used

import RPi.GPIO as GPIO
from time import sleep

GPIO.setmode(GPIO.BOARD)

Motor1A = 16
Motor1B = 18
Motor1E = 22

Motor2A = 23 #This value should have been 19
Motor2B = 21
Motor2E = 19 #This value should have been 23

GPIO.setup(Motor1A,GPIO.OUT)
GPIO.setup(Motor1B,GPIO.OUT)
GPIO.setup(Motor1E,GPIO.OUT)

GPIO.setup(Motor2A,GPIO.OUT)
GPIO.setup(Motor2B,GPIO.OUT)
GPIO.setup(Motor2E,GPIO.OUT)

print "Going forwards"
GPIO.output(Motor1A,GPIO.HIGH)
GPIO.output(Motor1B,GPIO.LOW)
GPIO.output(Motor1E,GPIO.HIGH)

GPIO.output(Motor2A,GPIO.HIGH)
GPIO.output(Motor2B,GPIO.LOW)
GPIO.output(Motor2E,GPIO.HIGH)

sleep(2)

print "Going backwards"
GPIO.output(Motor1A,GPIO.LOW)
GPIO.output(Motor1B,GPIO.HIGH)
GPIO.output(Motor1E,GPIO.HIGH)

GPIO.output(Motor2A,GPIO.LOW)
GPIO.output(Motor2B,GPIO.HIGH)
GPIO.output(Motor2E,GPIO.HIGH)

sleep(2)

print "Now stop"
GPIO.output(Motor1E,GPIO.LOW)
GPIO.output(Motor2E,GPIO.LOW)

GPIO.cleanup()

Here is the corrected code:

import RPi.GPIO as GPIO
from time import sleep

GPIO.setmode(GPIO.BOARD)

Motor1A = 16
Motor1B = 18
Motor1E = 22

Motor2A = 19
Motor2B = 21
Motor2E = 23 #The values have been corrected

GPIO.setup(Motor1A,GPIO.OUT)
GPIO.setup(Motor1B,GPIO.OUT)
GPIO.setup(Motor1E,GPIO.OUT)

GPIO.setup(Motor2A,GPIO.OUT)
GPIO.setup(Motor2B,GPIO.OUT)
GPIO.setup(Motor2E,GPIO.OUT)

print "Going forwards"
GPIO.output(Motor1A,GPIO.HIGH)
GPIO.output(Motor1B,GPIO.LOW)
GPIO.output(Motor1E,GPIO.HIGH)

GPIO.output(Motor2A,GPIO.HIGH)
GPIO.output(Motor2B,GPIO.LOW)
GPIO.output(Motor2E,GPIO.HIGH)

sleep(2)

print "Going backwards"
GPIO.output(Motor1A,GPIO.LOW)
GPIO.output(Motor1B,GPIO.HIGH)
GPIO.output(Motor1E,GPIO.HIGH)

GPIO.output(Motor2A,GPIO.LOW)
GPIO.output(Motor2B,GPIO.HIGH)
GPIO.output(Motor2E,GPIO.HIGH)

sleep(2)

print "Now stop"
GPIO.output(Motor1E,GPIO.LOW)
GPIO.output(Motor2E,GPIO.LOW)

GPIO.cleanup()

          From this, I learned how big of an impact little details like this can have. A simple switch of two numbers temporarily stalled my entire project.

Putting The Two Systems Together

         So far, I had made two systems for my robot. One was the IR detection system and the other was the system of motors. The final step was to put both of them together. This required a lot of modification to my original design and the circuit was very complex. I had to make sure to be precise with all of my wires since it would be nearly impossible to recheck the wires once all of them were placed.

          I also had to modify the code so that it would properly match up with the new variables. Overall, it was a lot more work than I had initially imagined.

The code:

import RPi.GPIO as GPIO
import time
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(3, GPIO.IN) #Right IR sensor module
GPIO.setup(32, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) #Activation button
GPIO.setup(22, GPIO.IN, pull_up_down=GPIO.PUD_UP) #Left IR sensor module
GPIO.setup(5,GPIO.OUT) #Left motor control
GPIO.setup(7,GPIO.OUT) #Left motor control
GPIO.setup(11,GPIO.OUT) #Right motor control
GPIO.setup(13,GPIO.OUT) #Right motor control

Motor1A = 16
Motor1B = 18
Motor1E = 22

Motor2A = 19
Motor2B = 21
Motor2E = 23

GPIO.setup(Motor1A,GPIO.OUT)
GPIO.setup(Motor1B,GPIO.OUT)
GPIO.setup(Motor1E,GPIO.OUT)

GPIO.setup(Motor2A,GPIO.OUT)
GPIO.setup(Motor2B,GPIO.OUT)
GPIO.setup(Motor2E,GPIO.OUT)


GPIO.output(Motor1A,GPIO.LOW)
GPIO.output(Motor1B,GPIO.LOW)
GPIO.output(Motor1E,GPIO.LOW)

GPIO.output(Motor2A,GPIO.LOW)
GPIO.output(Motor2B,GPIO.LOW)
GPIO.output(Motor2E,GPIO.LOW)

while True:
i=GPIO.input(3) #Listening for output from right IR sensor
k=GPIO.input(22) #Listening for output from left IR sensor
if i==0: #Obstacle detected on right IR sensor
print "Obstacle detected on Right",i
#Move in reverse direction
GPIO.output(Motor1A, GPIO.LOW)
GPIO.output(Motor1B, GPIO.HIGH)
GPIO.output(Motor1E, GPIO.HIGH)

GPIO.output(Motor2A, GPIO.LOW)
GPIO.output(Motor2B, GPIO.HIGH)
GPIO.output(Motor2E, GPIO.HIGH)

time.sleep(5)

#Turn robot left
GPIO.output(Motor1A,GPIO.HIGH)
GPIO.output(Motor1B,GPIO.LOW)
GPIO.output(Motor1E,GPIO.HIGH)

GPIO.output(Motor2A,GPIO.LOW)
GPIO.output(Motor2B,GPIO.HIGH)
GPIO.output(Motor2E,GPIO.HIGH)
time.sleep(2)
if k==0: #Obstacle detected on left IR sensor
print "Obstacle detected on Left",k
#Move in reverse direction
GPIO.output(Motor1A, GPIO.LOW)
GPIO.output(Motor1B, GPIO.HIGH)
GPIO.output(Motor1E, GPIO.HIGH)

GPIO.output(Motor2A, GPIO.LOW)
GPIO.output(Motor2B, GPIO.HIGH)
GPIO.output(Motor2E, GPIO.HIGH)

time.sleep(1)

#Turn robot left
GPIO.output(Motor1A,GPIO.HIGH)
GPIO.output(Motor1B,GPIO.LOW)
GPIO.output(Motor1E,GPIO.HIGH)

GPIO.output(Motor2A,GPIO.LOW)
GPIO.output(Motor2B,GPIO.HIGH)
GPIO.output(Motor2E,GPIO.HIGH)
time.sleep(2)

elif i==0 and k==0:
print "Obstacles on both sides"
#Move in reverse direction
GPIO.output(Motor1A, GPIO.LOW)
GPIO.output(Motor1B, GPIO.HIGH)
GPIO.output(Motor1E, GPIO.HIGH)

GPIO.output(Motor2A, GPIO.LOW)
GPIO.output(Motor2B, GPIO.HIGH)
GPIO.output(Motor2E, GPIO.HIGH)

time.sleep(1)

#Turn robot left
GPIO.output(Motor1A,GPIO.HIGH)
GPIO.output(Motor1B,GPIO.LOW)
GPIO.output(Motor1E,GPIO.HIGH)

GPIO.output(Motor2A,GPIO.LOW)
GPIO.output(Motor2B,GPIO.HIGH)
GPIO.output(Motor2E,GPIO.HIGH)
time.sleep(2)

elif i==1 and k==1: #No obstacles, robot moves forward
print "No obstacles",i
#Robot moves forward
GPIO.output(Motor1A,GPIO.HIGH)
GPIO.output(Motor1B,GPIO.LOW)
GPIO.output(Motor1E,GPIO.HIGH)

GPIO.output(Motor2A,GPIO.HIGH)
GPIO.output(Motor2B,GPIO.LOW)
GPIO.output(Motor2E,GPIO.HIGH)

As for what exactly the code does, here is my video explaining it.


          This is my last research round so that is all for now. I wanted to try and wrap everything up by putting together the two systems, which seems to have worked with fair success. I will probably end up finishing the robot later, but for now that is all. Please feel free to ask me any questions or give me any suggestions for the future. I hope you enjoyed my research!

Original Post

Add Reply

×
×
×
×