TomTheGeek

All the geeky stuff that gets me hot.

Friday, January 29, 2010

PicAxe 08M Lego Tank

Here's my first PicAxe 08M project, an IR controlled Lego tank. It features PWM Lego Power Function (LPF) motor control and a laser pointer "cannon". I built this because I wanted something fun to do with the PicAxe and Lego doesn't make any tank kits. This can be easily replicated using the LPF IR remote and receiver but where's the fun in that? The turret rotates but only by hand, the 08M doesn't have enough pins to drive more than two motors. In the next version of the tank I plan on adding motorized turret rotation and elevation.

IR control is done using a standard Sony TV remote control and IR receiver breakout from Sparkfun.com. You can see the receiver taped to the top of the turret. You could use most any IR receiver though, IR on the PicAxe is super easy to get working.

Power is supplied through the LPF 9v battery box. This is the voltage the motors are designed to work with and I didn't want to add a second battery box just for the PicAxe so I used a LM317 variable power regulator to drop that down to 5v on the development board. By using only the LPF battery box I can keep the battery weight down low for better stability.

The treads are direct driven using two LPF motors. In the next version I would like to build an adder/subtractor drive but I don't have the second differential gear needed yet. To drive the motors I used an SN754410 in a circuit I found at the Society Of Robots. The PicAxe 08M only has one PWM output so I tied both PWM inputs to the same signal. This means I can't run the treads at different speeds; steering would be smoother if I could but it works well enough for now. I found the wiring schematic and motor specifications for the LPF motors here. The LPF battery box and motors were connected using a LPF extension wire cut in half so I didn't have to cut the connectors off the motors. I feel a lot better hacking up a $2.99 wire rather than mangling the more expensive motors. The extension wire is the only Lego bit I had to modify, everything else is used stock.

The laser pointer cannon is hooked up to a spare output, I may eventually add a sound effects module for a more entertaining firing sequence. The laser pointer was added after these pictures were taken but you can see it in the video. I put a clear red cone that the laser lights up when it fires for effect.



Here's a (terrible) cell phone video demonstrating the tank.



This is the source code for the PicAxe, feel free to use this however you wish. It was somewhat challenging getting everything to fit in the limited memory of the 08M but by using a function for code that was re-used a bunch I managed to fit it all in there.

' IR Controlled PWM motor driver for the PICAXE-08M
' By TomTheGeek
' 1/20/2010

' Set Pin Variables
symbol RED_LED = 0 'FIRE ZE MISSLES!
symbol DIRRIGHT = 1 'foward/stop/reverse
symbol SPEEDCTRL = 2 'PWM output
symbol IR_IN = 3 'Infrared in
symbol DIRLEFT = 4 'foward/stop/reverse

' Other constants
symbol SPEEDMIN = 400
symbol SPEEDMAX = 1000

' Set Variable Registers
symbol SPEED = W0 'W0 = B1:B0
symbol PERIOD = B2
symbol motion_state = B3

' Initialize variables
SPEED = SPEEDMIN
PERIOD = 249 ' 4KHz PWM operation
let motion_state = 0

'Motion states:
' stopped = 0
' forward = 1
' backward = 2
' turn left = 3
' turn right = 4
' rotate left = 5
' rotate right = 6

init:
low SPEEDCTRL
PWMOUT SPEEDCTRL, PERIOD, SPEED

main:
infrain2 'wait for new signal
select case infra
case 116 'forward
gosub pwm_on
high DIRRIGHT
low DIRLEFT
let motion_state = 1
case 117 'reverse
if motion_state = 1 Then 'going forward, pause before reversing to prevent tip-overs.
pwmout SPEEDCTRL, OFF
low SPEEDCTRL
PAUSE 300
endif
gosub pwm_on
low DIRRIGHT
high DIRLEFT
let motion_state = 2
case 52 'left
gosub pwm_on
if motion_state = 3 or motion_state = 0 Then 'drive tracks in opposite directions to spin
low DIRRIGHT
low DIRLEFT
let motion_state = 5
Else 'stop one tread and keep the other going to skid steer.
let dirs = 010000 'change pin to input to stop tread
low DIRLEFT
let motion_state = 3
end if
case 51 'right
gosub pwm_on
if motion_state = 4 or motion_state = 0 Then 'drive tracks in opposite directions to spin
high DIRRIGHT
high DIRLEFT
let motion_state = 6
else 'stop one tread and keep the other going to skid steer.
let dirs = 000010 'change pin to input to stop tread
high DIRRIGHT
let motion_state = 4
end if
case 16 'faster
if SPEED < speed =" SPEED"> SPEEDMIN then
SPEED = SPEED - 100
endif
PWMOUT SPEEDCTRL, PERIOD, SPEED
pause 250
case 21 'FIRE ZE MISSLES!
HIGH RED_LED
PAUSE 500
LOW RED_LED
else 'stop
pwmout SPEEDCTRL, OFF
low SPEEDCTRL
let motion_state = 0
endselect
goto main

pwm_on:
high SPEEDCTRL
PWMOUT SPEEDCTRL, PERIOD, SPEED
return

Labels: ,