Auto-bot driving a bit randomly

Andrei IRL

Senior Member
Hey every one.
I have finished my Autobot but it keeps bumping into things. Two servos driving a wheel each. then a servo is used for turning Ultrasonic sensor left and right to establish distance. It works OK but not 100%
It was a lot worse, then I slowed down the servos driving the wheels and it its a bit better now.
It feels like some times by the time the program circled back to MAIN: and then ping sub-routine, the car has already advanced too far forward and bumped into something.
Is there problem with the code?
I have been at it for a few days now and still none the wiser.
I will appreciate any help ye might provide on this.
Thanks very much.
Code:
'picaxe 08m
`3rd July 2015
disablebod
setfreq m4
		`Specifiyng Inputs and Outputs`
symbol motorL=4		'servo output pins 		- pin 4
symbol motorR=2		'servo output pins 		- pin 2
symbol motorN=0		'servo output pins 		- pin 0
symbol sonic=1		'output pin for ultrasonic    - pin 1

		`Specifiyng Variables`
symbol distance=w6	'result from ultrasonic
symbol distleft=w5	'distance when looking left
symbol distright=w4	'distance when looking righ

		`Specifiyng Constants`
symbol forwardL=170	`Right servo position signal for forward
symbol forwardR=152	`Left servo position signal for forward
symbol reverseL=110	`Right servo position signal for reverse
symbol reverseR=195
`symbol motorLstop=158
`symbol motorRstop=164
symbol blocked=40	'minimum safe distance 40cm

		`Servo initiating`
servo motorN,152
pause 1000
servopos motorN,192
pause 1000
servopos motorN,172
pause 1000	
servo motorL,forwardL	'set pins as servo outputs - forwards
servo motorR,forwardR		'set pins as servo outputs - forwards
pause 10
 


Main:
	gosub ping	    'check to see if anything is infront of the robot
	
	if distance>blocked then	` if nothing ahead then move forward
		gosub ahead				
		goto main
	end if
	
	gosub jump		`if something is ahead go to jump Sub-routine
	goto main
end


jump:
	low motorL	`turn off left servo
	low motorR	`turn off right servo
	gosub look	`go to look sub-routine 

	if distleft < blocked and distright < blocked then
		`high motorL,motorR
		gosub backwards	`if object to the left and right then reverse for a bit and then spin aroung 180
		gosub spin180		
		goto main
	end if
    
	if distleft>distright then				
		`high motorL,motorR
		gosub turnleft
		goto main
	end if
	`high motorL,motorR
	gosub turnright
	goto main


look:
	servopos motorN,200		' turn the head to the left
	pause 500
	gosub ping
	let distleft=distance
    
	servopos motorN,140	' turn the head to the right
	pause 600
	gosub ping
	let distright=distance
    
	servopos motorN,172		' return the head to centre
	pause 500
	return
    
ping:
	ultra sonic, distance 		
	pause 500
	return
    
ahead:
	servo motorL,forwardL
	servo motorR,forwardR
	return


backwards:
	servo motorL,reverseL
	servo motorR,reverseR
	pause 1000
	return

turnright:
	servo motorL,168
	servo motorR,174
	pause 50
	return
    
turnleft:
	servo motorL,155
	servo motorR,154
	pause 50
	return
    
spin180:
	servo motorL,120
	servo motorR,120
	pause 300
	return
Car_New2.jpg
Circuit_Diagram.png
 

erco

Senior Member
Nice Andrei. I have only looked your code enough to see that you are using the SERVO command on pin C.0, the serial output pin, for your ultrasonic sensor servo. I have not tried that myself, but it is nice to hear that it works, unless that is what's causing your problem. :)

Any chance you can Youtube a video of your bot running? That might help spot the problem.

Parallax's well-established BoeBot has a similar well-documented routine called 'Roaming with Ping' that would probably serve as a good starting point, or at least for reference.

Edit: Boebot code attached for reference. Keep in mind the BS2 is sending individual servo pulsouts (old school). It's pretty short and you can pare it down with the Picaxe's SERVO/SERVOPOS commands.
 

Attachments

Last edited:

erco

Senior Member
I notice in your MAIN loop, your ultrasonic sensor just points straight forward, and you poll it continuously, driving forward until you get a reading under 40 cm, then you stop and look only full left and full right to decide what to do. If you approach a small obstacle slightly off center (no signal reflected), or a wall at an oblique angle (ultrasonic signal reflects off at an angle instead of directly back to robot), then you'll definitely blunder into things. It's preferable to continuously scan the sensor SLOWLY full left and right, taking continuous measurements, dynamically mapping the area all around the bot. This gives you the best chance of seeing randomly aligned objects, because you are pinging at a variety of angles.

It helps to visualize what the sensor sees as it scans back & forth. Check out this very helpful tutorial on using a servo-steered ultrasonic sensor to generate a radar-like display.

https://www.youtube.com/watch?v=t2tzBEOAtjg
 

Andrei IRL

Senior Member
Hey again. I have been playing around with the code for a few more hours now and still cant ficure out how to do a PARTIAL sweep with PING.
What im trying to do in the code below, is to measure distance directly IN FRONT, TO THE LEFT and TO THE RIGHT while the car is slowly moving. And then based on the reading let it decide to go ahead, to turn left or right or to spin around.
I must be missing something as no matter whats sonar is reading the same chain of events happens. It does move the SONAR sensor left, right and centre, I hope it reads some distances (can not check with debug while servos are running, it keeps resetting the timer). Then it will just loop between reversing and spinning 180.
I'd appreciate any help at all.
Code:
'picaxe 08m
`3rd July 2015
disablebod
setfreq m4
		`Specifiyng Inputs and Outputs`
symbol motorL=4		'servo output pins 		- pin 4
symbol motorR=2		'servo output pins 		- pin 2
symbol motorN=0		'servo output pins 		- pin 0
symbol sonic=1		'output pin for ultrasonic    - pin 1

		`Specifiyng Variables`
symbol distance=w6	'result from ultrasonic
symbol distleft=w5	'distance when looking left
symbol distright=w4	'distance when looking righ
symbol distcenter=w3

		`Specifiyng Constants`
symbol forwardL=170	`Right servo position signal for forward
symbol forwardR=153	`Left servo position signal for forward
symbol reverseL=110	`Right servo position signal for reverse
symbol reverseR=195
symbol motorLstop=158
symbol motorRstop=164
symbol blocked=40	'minimum safe distance 40cm

		`Servo initiating`
servo motorN,152
pause 1000
servopos motorN,192
pause 1000
servopos motorN,172
pause 1000	
servo motorL,forwardL	'set pins as servo outputs - forwards
servo motorR,forwardR		'set pins as servo outputs - forwards
pause 10
 


Main:
	gosub look	
	
	if distcenter>blocked then
		gosub ahead
	endif
		
				
	if distleft<blocked and distright<blocked  then
		
		gosub backwards	
		gosub spin180		
		goto main
	end if
    
	if distleft>distright then				
		
		gosub turnleft
		goto main
	end if
	`high motorL,motorR
	gosub turnright
	goto main
		goto main

	gosub ahead	
	goto main
end


look:
	servopos motorN,200		' turn the head to the left
	pause 200
	gosub ping
	let distleft=distance
	
	servopos motorN,172		' return the head to centre
	gosub ping
	pause 200
	let distcenter=distance
    
	servopos motorN,140	' turn the head to the right
	pause 200
	gosub ping
	let distright=distance  
	
	servopos motorN,172
	pause 200
	
	return
    
ping:
	ultra sonic, distance 		
	pause 50
	return
    
ahead:
	servo motorL,forwardL
	servo motorR,forwardR
	return


backwards:
	servo motorL,reverseL
	servo motorR,reverseR
	pause 1000
	return

turnright:
	servo motorL,168
	servo motorR,174
	pause 50
	return
    
turnleft:
	servo motorL,155
	servo motorR,154
	pause 50
	return
    
spin180:
	servo motorL,120
	servo motorR,120
	pause 300
	return
 

Andrei IRL

Senior Member
I have been doing some testing using Alan's Idea with the SERTXD.
I used the code below to do a sweep from left to right and centre and measure distances for each position LEFT, RIGHT AND CENTER).
I get good reading for Centre and Right position but Left position always returns a very small value independently what's in front of it.
The code for all three positions is identical so I can not understand what the problem might be.
I did try to increase the PAUSE after turning the servo to the left position to give it more time before sending sonar command but it made no difference.
Does any one have any ideas?
Thanks very much in advance. I hate been a burden here,
Code:
'picaxe 08m
`3rd July 2015
disablebod
setfreq m4
		`Specifiyng Inputs and Outputs`

symbol motorN=2		'servo output pins 		- pin 0
symbol sonic=1		'output pin for ultrasonic    - pin 1

		`Specifiyng Variables`
symbol distance=w6	'result from ultrasonic
symbol distleft=w5	'distance when looking left
symbol distright=w4	'distance when looking righ
symbol distcenter=w3


		`Servo initiating`
servo motorN,152
pause 1000
 
	
main:
	servopos motorN,195		' turn the head to the left
	pause 500
	gosub ping
	let distleft=distance
	sertxd("The value of distleft is ",#distleft,13,10)
	#terminal 4800
	
	servopos motorN,172		' return the head to centre
	pause 500
	gosub ping	
	let distcenter=distance
	sertxd("The value of distcenter is ",#distcenter,13,10)
	#terminal 4800
    
	servopos motorN,140	' turn the head to the right
	pause 500
	gosub ping
	
	let distright=distance  
	sertxd("The value of distright is ",#distright,13,10)
	#terminal 4800
	
	servopos motorN,172
	pause 50
	goto main
	
ping:
	ultra sonic, distance 		
	pause 500
	
	return
And here is the print out from Terminal Window
The value of distcenter is 28
The value of distright is 120
The value of distleft is 7
The value of distcenter is 234
The value of distright is 191
The value of distleft is 7
The value of distcenter is 80
The value of distright is 121
The value of distleft is 7
The value of distcenter is 79
The value of distright is 190
The value of distleft is 7
The value of distcenter is 80
The value of distright is 193
The value of distleft is 7
The value of distcenter is 78
The value of distright is 123
The value of distleft is 7
The value of distcenter is 76
The value of distright is 43
The value of distleft is 7
The value of distcenter is 78
The value of distright is 44
The value of distleft is 7
The value of distcenter is 46
The value of distright is 121
 

Buzby

Senior Member
Your code looks fine, are you sure the sonar is not picking up something on the chassis of the robot itself ?.
 

PieM

Senior Member
Left position always returns a very small value independently what's in front of it.
Servo shaft is on the right of bot, so your ultrasonic sensor see left wheel !
Beam pattern and beam width of SRF 05: 2015-07-05 001.jpg
 
Last edited:

Andrei IRL

Senior Member
thanks very much. Moving sensor 20mm forward resolved the issue with random distance reading.

Have a question.
Is it possible to do an IF for 3 different conditions are met?
Id like to do IF distance to an object in the front, left and right is greater then ... just keep going ahead?
 

Andrei IRL

Senior Member
Just want to thank everyone for their help on this.
I finally got everything going well. The robot is driving and scanning the room at the same time. Did not bump into anything even once after 20 minutes going none stop.
I still have to do a small servo calibration and timing but apart for that its working great.
I'll upload a video as soon as I have it finished.
Here is the final code:
Code:
'picaxe 08m
`5rd July 2015 - Andrei Reaboi Autonomous Bot
disablebod
setfreq m4
		`Specifiyng Inputs and Outputs`
symbol motorL=4		'servo output pins 		- pin 4
symbol motorR=0		'servo output pins 		- pin 2
symbol motorN=2		'servo output pins 		- pin 0
symbol sonic=1		'output pin for ultrasonic    - pin 1

		`Specifiyng Variables`
symbol distance=w6	'result from ultrasonic
symbol distleft=w5	'distance when looking left
symbol distright=w4	'distance when looking righ
symbol distcenter=w3

		`Specifiyng Constants`
symbol forwardL=170	`Right servo position signal for forward
symbol forwardR=153	`Left servo position signal for forward
symbol reverseL=110	`Right servo position signal for reverse
symbol reverseR=195
symbol motorLstop=158
symbol motorRstop=164
symbol blocked=40	'minimum safe distance 40cm

		`Servo initiating`
servo motorN,152
pause 1000
servopos motorN,192
pause 1000
servopos motorN,172
pause 1000	
servo motorL,forwardL	'set pins as servo outputs - forwards
servo motorR,forwardR		'set pins as servo outputs - forwards
pause 10
 


Main:
	gosub look	
	
	if distcenter>blocked and distleft>blocked and distright>blocked then
		gosub ahead
		goto main
	endif

				
	if distleft<blocked and distright<blocked  then
		
		gosub backwards	
		gosub spin180		
		goto main
	end if
    
	if distleft>distright then				
		
		gosub turnleft
		goto main
	end if
	gosub turnright
	goto main
	
`goto main
`end


look:
	servopos motorN,195		' turn the head to the left
	pause 300
	gosub ping
	let distleft=distance
	`sertxd("The value of distleft is ",#distleft,13,10)
	`#terminal 4800
	
	servopos motorN,172		' return the head to centre
	pause 300
	gosub ping	
	let distcenter=distance
	`sertxd("The value of distcenter is ",#distcenter,13,10)
	`#terminal 4800
    
	servopos motorN,140	' turn the head to the right
	pause 300
	gosub ping
	
	let distright=distance  
	`sertxd("The value of distright is ",#distright,13,10)
	`#terminal 4800	
	return
    
ping:
	ultra sonic, distance 		
	pause 50
	return
    
ahead:
	servo motorL,forwardL
	servo motorR,forwardR
	return


backwards:
	servo motorL,reverseL
	servo motorR,reverseR
	pause 1000
	return

turnright:
	servo motorL,168
	servo motorR,174
	pause 50
	return
    
turnleft:
	servo motorL,155
	servo motorR,154
	pause 50
	return
    
spin180:
	servo motorL,120
	servo motorR,120
	pause 300
	return
 

Andrei IRL

Senior Member
As promised, here is the video of the final project. One last thing to adjust is a 180 degree spin, as you'll see it is more like 360 spin at the moment. [video]https://youtu.be/4RQFuK5j1XY[/video]
 
Top