AezDropZz
Posts: 29
Joined: Tue Dec 23, 2014 2:16 pm

Automating trouble

Tue Jan 06, 2015 10:25 am

Ok, so I'm running into some trouble with my project. I have a chassis on wheels and I can control the movement of the robot with my PS3-controller. Now for the problem, I am trying to attach a range-sensor (HC-SR04) and make my robot either reverse itself or make a random backwards/Left/Right-move or let an LED and Buzzer go off. But as soon as I call for my 'def' with for the range-sensor, my script blocks and stops functioning.

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
The code for the Pygame-movement:

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)

Wherever there is a '#' in the front of the line and some code after it, those are the things I already tried to implement the range-sensor.


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 can also send the full script, if needed.
I hope someon can help me out!

Greetz
AezDropZz

User avatar
B.Goode
Posts: 10356
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Automating trouble

Tue Jan 06, 2015 10:35 am

AezDropZz wrote: 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>
That looks like the problem, because presumably you are expecting a number?

Just to clarify the code, would it help if you did something like:

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
        result = duration * 17150

   return result
(Just guessing that 'distance' might be getting used recursively? I would also print out the returned value to check.)

AezDropZz
Posts: 29
Joined: Tue Dec 23, 2014 2:16 pm

Re: Automating trouble

Tue Jan 06, 2015 10:57 am

Changing it to 'result' solved the print issue partially. If I use the print function in the definition, it prints. But outside of the definition, I get a Traceback error. Is there a different way to get the 'result'-value out of the definition then simply using "print(result)"?

AezDropZz
Posts: 29
Joined: Tue Dec 23, 2014 2:16 pm

Re: Automating trouble

Tue Jan 06, 2015 12:46 pm

I got it to work by using "distance.result" as a variable in my function. This makes the variable a global variable so I can use it outside of the function itself. But there still is the problem that it blocks after a short amount of time and my whole script freezes...

EDIT: It mostly freezes after triggering the affect of "getting to close"

Return to “Automation, sensing and robotics”