Autonomous avoidance Robot.

pjl83

Member
Hi All,

I thought I'd post a picture and a video for you see my first attempt at a robot. It's a pretty simple 2 motor set-up driven by an L239D motor driver on a 28X1 project board. The range sensor is an SRF005.

Now that I am happy with the code I will build a real "body" to make it look good. I will update this thread later when I have a photo of more progress.

Here is a link to a video.... http://www.youtube.com/watch?v=DaSoOqB5Z58

Thanks for looking
Paul
 

Attachments

russbow

Senior Member
Nice one Paul.Just love the SRF scans, remainds me of Walle (?).

Would you like to share the code?

Russ.
 

pjl83

Member
Haha, It does look a bit like Walle. Once the unit is built into the final body the eyes won't be so prominent so it will look a little different. The whole thing will also be a lot shorter. This one bumps into thing sideways sometimes and drags it wheels against things if it gets a little too close.

Here's the code. I have to admit that it was copied from the LMR site. I had to add the sensor code as it was different to the one that they used and I also added the LED and a routine which meant that if both ways are less than the danger level when looking left and right, the whole thing reverses and spins around. This was added after I found it was getting stuck in corners for too long.

I made the mistake of copying the code the first time and expecting it to work perfect first time. It didn't and it was impossible to fault find having not fully understood the code. I took advice from others on here and broke everything down to simplify. I only added things as I understood them and tested them individually.

It worked quite well in the end :)

Code:
symbol dangerlevel=50 'how far away should things be before we react?
symbol turn=750 'this sets how much should be turned
symbol servo_turn= 300 'this sets for how long time we should wait for the servo to turn
symbol trig= 3 'define output pin for trigger pulse
symbol echo= 6 'define input pin for echo pulse
symbol range= w1 '16 bit word variable for range


servo 0, 150
wait 1

main: ' the main loop
	low 2
	pulsout trig,2 'produce 20uS trigger pulse (must be minimum of 10Us)
	pulsin echo,1,range 'measures the range in 10uS steps
	pause 10 'recahrge period after ranging completes
	'now convert range to cm (divide by 5.8) or inches (divide by 14.8)	 
	'as picaxe cannot use 5.8, multiply by 10 then divide by 58 instead
	let range = range*10/58 ' multiply by 10 then divide by 58
	debug range 'display range via debug command
	if range > dangerlevel then
	gosub nodanger 'if nothing ahead, drive forward
	else
	gosub whichway 'if obstacle ahead then decide which way is better
	end if 
	goto main 'end of main loop, all others are sub-routines
	
nodanger: 'drive forward
	high 4 : low 5 : high 6 : low 7
	return

whichway:
	high 2
	gosub totalhalt 'first stop!
	
'look one way
	gosub lturn 'look to one side
	pause servo_turn ' wait for the servo to be finished turning
	pulsout trig,2 'produce 20uS trigger pulse (must be minimum of 10Us)
	pulsin echo,1,range 'measures the range in 10uS steps
	pause 10 'recahrge period after ranging completes
	'now convert range to cm (divide by 5.8) or inches (divide by 14.8)	 
	'as picaxe cannot use 5.8, multiply by 10 then divide by 58 instead
	let range = range*10/58 ' multiply by 10 then divide by 58
	debug range
	w2 = range
	gosub totalhalt
	
'look the other way
	gosub rturn 'to another side
	pause servo_turn 
	pulsout trig,2 'produce 20uS trigger pulse (must be minimum of 10Us)
	pulsin echo,1,range 'measures the range in 10uS steps
	pause 10 'recahrge period after ranging completes
	'now convert range to cm (divide by 5.8) or inches (divide by 14.8)	 
	'as picaxe cannot use 5.8, multiply by 10 then divide by 58 instead
	let range = range*10/58 ' multiply by 10 then divide by 58
	debug range
	w3 = range
	gosub totalhalt

'decide which is the better way
	if w2<dangerlevel and w3<dangerlevel then
	gosub backwards
	else
	gosub decide
	end if
	return
	
decide:
	if w2<w3 then
	gosub body_lturn
	else
	gosub body_rturn
	end if 
	return

rturn:
	servo 0, 200
	return

lturn:
	servo 0, 100
	return

body_lturn:
	high 4 : low 5 : high 7 : low 6
	pause turn : gosub totalhalt
	return

body_rturn:
	high 5 : low 4 : high 6 : low 7
	pause turn : gosub totalhalt
	return

totalhalt:
	low 4 : low 5 : low 6 : low 7
	servo 0, 150
	pause servo_turn
	return

backwards:
	high 5 : low 4 : high 7 : low 6
	pause turn
	high 4 : low 5 : high 7 : low 6
	pause turn
	pause turn
	return
Thanks
Paul
 
Top