The def:
Code: Select all
def distance():
# Send pulse
GPIO.output(TRIG,1)
time.sleep(0.00001)
GPIO.output(TRIG,0)
# Register input
while GPIO.input(ECHO) == 0:
start = time.time()
while GPIO.input(ECHO) == 1:
end = time.time()
time.sleep(.5)
# Calculate difference
duration = end - start
distance = duration * 17150
return distance
Code: Select all
try:
while True:
# Start measuring distance
distance()
# if (distance < 5):
# GPIO.output(20,1)
# else:
# GPIO.output(20,0)
# Check for any queued events and then process each one
events = pygame.event.get()
for event in events:
UpdateMotors = 0
# Check if button was pressed or joystick moved
if event.type == pygame.JOYBUTTONDOWN:
if event.button == PS3_BUTTON_SELECT:
print("Select button")
State_BLUE = State_BLUE + 1
if State_BLUE == 1:
GPIO.output(21,1)
elif State_BLUE == 2:
State_BLUE = 0
GPIO.output(21,0)
elif event.button == PS3_BUTTON_TRI:
print("Triangle button")
State_RED = State_RED + 1
if State_RED == 1:
GPIO.output(20,1)
elif State_RED == 2:
State_RED = 0
GPIO.output(20,0)
elif event.button == PS3_BUTTON_X:
print("X button")
BUZZ()
if event.type == pygame.JOYAXISMOTION:
if event.axis == 2:
LefRig = event.value
UpdateMotors = 1
elif event.axis == 3:
ForBac = event.value
UpdateMotors = 1
# Motor update?
if UpdateMotors:
if (LefRig > threshold): # Right
GPIO.output(m1a,0)
GPIO.output(m1b,1)
GPIO.output(m2a,1)
GPIO.output(m2b,0)
speed = int(LefRig*100)
p2.ChangeDutyCycle(speed)
p3.ChangeDutyCycle(speed)
elif (LefRig < -threshold): # Left
GPIO.output(m1a,1)
GPIO.output(m1b,0)
GPIO.output(m2a,0)
GPIO.output(m2b,1)
speed = -1*(int(LefRig*100))
p1.ChangeDutyCycle(speed)
p4.ChangeDutyCycle(speed)
elif (ForBac > threshold): # Forwards
# Dist = distance()
# if Dist < 5:
# print("Too close!")
# GPIO.output(m1a,0)
# GPIO.output(m1b,0)
# GPIO.output(m2a,0)
# GPIO.output(m2b,0)
# speed = 0
# p2.ChangeDutyCycle(speed)
# p4.ChangeDutyCycle(speed)
# else:
GPIO.output(m1a,0)
GPIO.output(m1b,1)
GPIO.output(m2a,0)
GPIO.output(m2b,1)
speed = int(ForBac*100)
p2.ChangeDutyCycle(speed)
p4.ChangeDutyCycle(speed)
elif (ForBac < -threshold): # Backwards
GPIO.output(m1a,1)
GPIO.output(m1b,0)
GPIO.output(m2a,1)
GPIO.output(m2b,0)
speed = -1*(int(ForBac*100))
p1.ChangeDutyCycle(speed)
p3.ChangeDutyCycle(speed)
else: # Motors off
GPIO.output(m1a,0)
GPIO.output(m1b,0)
GPIO.output(m2a,0)
GPIO.output(m2b,0)
p1.ChangeDutyCycle(0)
p2.ChangeDutyCycle(0)
p3.ChangeDutyCycle(0)
p4.ChangeDutyCycle(0)
Things that happen:
1: When implemented in the "UpdateMotors", it works perfectly for left and right and perfect for the forward (except it does not stop when it is too close). But when I move the joystick backwards, it just goes backwards for a long time or lags and eventually everything stops working.
2: When implemented in the beginning (see # Start measuring distance), nothing works and when I use print(distance), it gives me something crazy:
Code: Select all
<function distance at 0xb6d0c3f0>I hope someon can help me out!
Greetz
AezDropZz