[Edit: Part 2 is here with the circuit and components used]

PICAXE 08M chips are small & cheap but they don’t have a lot of program space,  only 256 bytes. That means about 220 lines of code at the very most. But that’s the fun bit, squeezing more into less.

Not included in that tiny 256 bytes is the Picaxe firmware already on the chip which includes all kinds of commands for controlling servos, serial, I2C & infrared communication, playing sounds and more so really it is the little chip that can. Still, 256 bytes isn’t much.

 

Picaxe 08M RobotThis little robot avoids stuff using a Picaxe 08M & Sharp Infrared sensor. On the top there is one Picaxe 08M (centre front) and behind that a SN754410 motor driver flanked on either side by a few components such as capacitors, transistors and LED’s for the required blinky light fun. A Picaxe programmer is  built in using the stereo socket and a switch for on/off control.

 

Poking out the front is a Sharp  4-30cm infrared sensor  and mounted underneath are two Solarbotics GM10 geared pager motors. Four AAA batteries are jammed in there with two on top and two underneath.

The whole lot is soldered and hot glued to the veroboard base. Here’s the underside with my dodgy soldering. I expect it will catch fire eventually ;)

Picaxe08M Robot underside

Operation is pretty simple. Pin 1 on the Picaxe 08M reads the analog voltage of from the Sharp GP2D120 infrared sensor and the code then decides to drive forward or avert danger as required – beware of socks and shoes. Picaxe output pins 4 and 0 control the direction of the motors with 2 PNP transistors acting as invertors so that the 2 pins provide logic to 4 pins on the SN754410 motor driver chip. Pin 2 provides the same PWM (Pulse Width Modulation) signal to both sides of the motor driver so the power to each wheel is the same.

The code is a simple state machine, implemented as a switch statement:

'Differential drive robot
' by Kim McCoy
'
' Drives around not running in to too many things.
'
'Configuration:
'
' Brains
'	PICAXE 08M
'
' Drive
'	SN754410 Dual H-Bridge Motor Driver Chip (Texas Instruments)
'	  similar to L293D
'	2 x Solarbotics GM10 motors and wheels
'
' Sensors
'	1 x Sharp GP2D120XJ00F (4-30cm) infrared ranger

'Variable symbols
symbol state = b4
symbol irFront = b2
symbol lastIrFront = b3
symbol stallCount = w0

'Robot states
symbol STATE_IDLE = 0
symbol STATE_FORWARD = 1
symbol STATE_SPINLEFT = 2
symbol STATE_SPINRIGHT = 3
symbol STATE_REVERSE = 4

'Setup & initialization

'motor A,B direction control pins
output 4, 0 
high 4
low 0

' set pwm duty to stop
pwmout 2,49,0 

let state = STATE_IDLE
pause 3000 'wait to get clear before letting loose

main:
	'Get front IR sensor value
	readadc 1, irFront
	
	'Detect if the robot is stuck on a sock or something
	'and take evasive action to wriggle free	
	'Time to detect a stall is emperical only, picked
	'a number that doesn't cause false positives too often.
	let stallCount  = stallCount + 1
	if stallCount > 1000 then
		gosub goBack
		pause 2000
		gosub spinLeft
		pause 1000
		gosub spinRight
		pause 1000
		let stallCount = 0
	end if
	
	if irFront >= 140 then
		'Reverse out if too close to something
		gosub goBack
	else
		select case state
			case STATE_IDLE
				'go forward
				gosub goForward
				
			case STATE_FORWARD
				if irFront > 50 and irFront < 140  then
					'Something is in front. Take evasive action
					'by spinning left
					let lastIrFront = irFront + 20
					gosub spinLeft
				end if
			
			case STATE_SPINLEFT
				if irFront > 50 and irFront < 140 then
					if lastIrFront < irFront then
						'Got closer to obstacle - try the other way instead.
						let lastIrFront = irFront + 20
						gosub spinRight
					end if			
				else
					'All clear
					gosub goForward
				end if
			
			case STATE_SPINRIGHT
				if irFront > 50 and irFront < 140 then
					if lastIrFront < irFront then
						'Got closer to obstacle - try the other way instead.	
						let lastIrFront = irFront + 20			
						gosub spinLeft
					end if			
				else
					'All clear
					gosub goForward
				end if
				
			case STATE_REVERSE
				'Back out until well clear
				if irFront < 100 then
					gosub goForward
				end if
		end select
	end if

	
goto main ' loop back to start

'Drive Forward
goForward:
	'Reset the stall count - has attempted to move forward again
	let stallCount = 0
	
	pwmout 2,49,300 ' set pwm duty
	high 4
	low 0	
	state = STATE_FORWARD
return

'Reverse out
goBack:
	low 4
	high 0
	pwmout 2,49,80 ' set pwm duty
	state = STATE_REVERSE
return

'Spin right
spinRight:	
	high 4
	high 0
	pwmout 2,49,50 ' set pwm duty
	state = STATE_SPINRIGHT
return

'Spin left
spinLeft:
	low 4
	low 0
	pwmout 2,49,50 ' set pwm duty
	state = STATE_SPINLEFT
return

 

A code walkthrough…

 

image

Looks like I’ve 16 bytes to spare. What a waste!

At the start of the main loop ‘:main’ we read the value from the Sharp GP2D120 analog sensor. This returns a value between 3.3V and 0V depending on how far the distance in front is. Picaxe readadc function converts the analog voltage to a value between 0-255 for a range of 0-5V. Since the Sharp sensor only goes to 3.3V the maximum value possible is about  168.

Ignoring the next bit about stalling, the code then enters a big switch statement that determines what to do next. If the distance in front drops below 140 the robot will spin left first. If it detects that spinning left gets it in more trouble it will spin right. So if the distance in front is decreasing while spinning then trouble looms – spin the other way. It continues to spin left or right until it finds a clear path forward.

Sometimes the robot gets too close, too fast!In this case it hits the breaks and goes into reverse ‘STATE_REVERSE’ at the bottom of the switch statement. It will then back out, go forward and probably start to turn left or right.

Back to stalling: After a while of driving around reality bytes. The robot gets stuck on a power cord, sock or jammed in a spot. Fortunately we’ve been incrementing a variable ‘stallCount’ for every loop. Eventually when this hits 1000 the robot starts the anti-stall measures, attempting to go back, left and right to wriggle free. Every time the robot goes forward the ‘stallCount’ is reset to 0 so as long as the robot moves around normally the anti-stall code is never called.

How is it built?

The circuit includes the use of a motor driver, PICAXE chip, inverter circuits and a built in PICAXE programmer. I’ll put the details up in a later post once I’ve drawn the schematic.