Right now I am having a Raspberry Pi connected to two DC Motors with light sensors which are sending a signal to the Raspberry everytime a little plastic wheel with holes in it goes through them.
Because the motors aren't equally strong they will drive a curve when I put both of them on full power. I would like the robot to drive in a straight line. I wanted to archive that with the light sensors and interrupts.
I planned to do it like that: Both motors start at the same time, after one light sensor is triggered an interrupt is going to happen. Inside this interrupt variables should be changed. In the loop there is an if question where the motor stops when the interrupt was triggered. The motor stops until the other wheel has its interrupt triggered.
Basically like that:
Left Motor triggers interrupt
right motor triggers interrupt
and so on.
If both it goes on like that the wheels should be stopping and starting at the right time so the robot drives in a straight line.
This is my Python code:
def interrupt_routinerechts(callback):
global zaehler_r
global zaehler_l
print "Interrupt Rechts"
zaehler_r = 1
zaehler_l = 2
def interrupt_routinelinks(callback):
global zaehler_l
global zaehler_r
print "Interrupt Links"
zaehler_l = 1
zaehler_r = 2
GPIO.add_event_detect(4, GPIO.FALLING, callback=interrupt_routinerechts)
GPIO.add_event_detect(6, GPIO.FALLING, callback=interrupt_routinelinks)
print "Los Geht's"
runProgram = True
while runProgram:
try:
forward()
if zaehler_r == 1:
stoppr()
elif zaehler_r == 2:
forwardr()
if zaehler_l == 1:
stoppl()
elif zaehler_l == 2:
forwardl()
except KeyboardInterrupt:
print "Exception thrown -> Abort"
runProgram = False
GPIO.cleanup()
GPIO.cleanup()
My problem is that the interrupts are not triggered how I imagined it so the robot drives in a curve. This is how they are getting triggered. "Interrupt Links" means "Interrupt Left", "Interrupt Rechts" means "Interrupt Right".
If it was not clear enough, this is what I meant with the light sensor and the plastic wheel.
The problem with your code is that you have only 2 states, in one state the robot turns left and in the other state the robot turns right.
You will need to re-write your code to have a counter for each wheel. If either of the counter is higher, the robot should turn off the specified motor. If both counters are equal, the robot should turn both motors on.
Try this:
def interrupt_right(callback):
global right_counter
print "Interrupt Right"
right_counter=right_counter+1
def interrupt_left(callback):
global left_counter
print "Interrupt left"
left_counter=left_counter+1
global left_counter=0
global right_counter=0
GPIO.add_event_detect(4, GPIO.FALLING, callback=interrupt_right)
GPIO.add_event_detect(6, GPIO.FALLING, callback=interrupt_left)
print "Los Geht's"
runProgram = True
while runProgram:
try:
if right_counter==left_counter:
forward()
elif right_counter>left_counter:
stop_right()
start_left()
elif left_counter>right_counter:
stop_left()
start_right()
except KeyboardInterrupt:
print "Exception thrown -> Abort"
runProgram = False
GPIO.cleanup()
GPIO.cleanup()
I hope this helps.