This site will look much better in a browser that supports web standards, but it is accessible to any browser or Internet device.


Clubs, glossaries, museums, shops and much more......

glue-it.com

...all of your model making needs from one site.







Picaxe Robot - Code

This is continued from: Picaxe Robot - Part 1.



'####################################
'#	Nigel's robot
'####################################
'#	ver 0.1 - Initial I/O drivers
'#
'####################################


'compiler directives
	#picaxe 18m2

'########################
'#	define symbols
'########################

	'bit value inputs
	symbol RHFTouch = bit0
	symbol LHFTouch = bit1
	symbol RHRTouch = bit2
	symbol LHRTouch = bit3


	'bit value outputs
	symbol Heater = bit8
	symbol RHMotorDirection = bit9
	symbol LHMotorDirection = bit10

	'ADC inputs
	symbol RH_LDR = b2
	symbol LH_LDR = b3
	symbol Mic = b4
	symbol BattVol = b5

	'Motor drive outputs
	symbol RHMotorSpeed = b6
	symbol LHMotorSpeed = b7
	symbol RHMotorCalc = b8
	symbol LHMotorCalc = b9

	symbol Rand = b10

'########################
'#	Input / Output
'########################

	'########################
	'#	I/O assignments
	'########################

	'18M2 Dev
	'Pin	Function	Assignment
	'1	C.2
	'2	C.3		Serial Out
	'3	C.4		Serial In
	'4	C.5
	'5	Gnd
	'6	B.0		Touch switch 1 - RHF
	'7	B.1		Battery monitoring
	'8	B.2		Touch switch 2 - LHF
	'9	B.3
	'10	B.4		Touch switch 3 - RHR
	'11	B.5		Touch switch 4 - LHR
	'12	B.6		Servo Out - Right
	'13	B.7		Servo Out - Left
	'14	V+
	'15	C.6
	'16	C.7
	'17	C.0
	'18	C.1		Heater LED

	'########################
	'#	initialise I/O
	'########################

	pullup %00110101	'turn on pull ups - port B

	dirsb = %11000000	'configure port B as input

	dirsc = %00000010	'configure port C.1 as output

	adcsetup = %00000010  	'set ADC on B.1 - battery monitoring

	fvrsetup FVR4096 		'set ADV to use internal reference and set to 4.096V

'##############################
'#	initialise Variables
'##############################

	'##############################
	'#	set motor directions
	'##############################

	RHMotorDirection = 1
	LHMotorDirection = 0

	'########################
	'#	set heater LED
	'########################

	Heater = 0	'0 = off   1 = on


'##############################
'#	Main programming loop
'##############################



start:


	gosub ReadInputs	'read inputs


	' low speed
	RHMotorSpeed = 10
	random Rand

	'randomly turn the robot through 180 degrees
	if Rand<8 then
		Heater=1
		LHMotorSpeed = 0
		RHMotorSpeed = 0
		gosub ProcessOutputs

		pause 10000
		LHMotorDirection = 1
		RHMotorDirection = 1

		LHMotorSpeed = 10
		RHMotorSpeed = 10
		gosub ProcessOutputs
		pause 2000

		LHMotorSpeed = 0
		RHMotorSpeed = 0
		gosub ProcessOutputs

		pause 10000

		LHMotorDirection = 0
		RHMotorDirection = 1
		LHMotorSpeed = 10
		RHMotorSpeed = 10
		Heater = 0
		gosub ProcessOutputs
	else
		LHMotorspeed = RHMotorSpeed
	endif




		if RHFTouch=1 then


			LHMotorDirection = 1
			RHMotorDirection = 0
			gosub ProcessOutputs
			pause 1000

			RHMotorSpeed=0
			gosub ProcessOutputs
			pause 1000

			LHMotorDirection = 0
			RHMotorDirection = 1
			LHMotorSpeed=5
			RHMotorSpeed =10
			gosub ProcessOutputs
			pause 1000

			LHMotorSpeed=10
			RHMotorSpeed=10
			gosub ProcessOutputs

		endif

		if LHFTouch=1 then


			LHMotorDirection = 1
			RHMotorDirection = 0
			gosub ProcessOutputs
			pause 1000

			LHMotorSpeed=0
			gosub ProcessOutputs
			pause 1000

			LHMotorDirection = 0
			RHMotorDirection = 1
			LHMotorSpeed=10
			RHMotorSpeed =5
			gosub ProcessOutputs
			pause 1000

			LHMotorSpeed=10
			RHMotorSpeed=10
			gosub ProcessOutputs

		endif


goto start

'##################
'#	Read Inputs
'##################
ReadInputs:

	'########################
	'#	touch switches
	'########################

	' 0 = no touch
	' 1 = touch

	if pinb.0 = 1 then
		RHFTouch = 0
	else
		RHFTouch = 1
	endif

	if pinb.2 = 1 then
		LHFTouch = 0
	else
		LHFTouch = 1
	endif

	if pinb.4 = 1 then
		RHRTouch = 0
	else
		RHRTouch = 1
	endif

	if pinb.5 = 1 then
		LHRTouch = 0
	else
		LHRTouch = 1
	endif


	'analogue channels
	'these will be added at the next stage
	'readadc c.3,RH_LDR
	'readadc c.2,LH_LDR
	'readadc c.1,Mic


	'########################
	'#	Battery Voltage
	'########################

	'battery voltage is divided by 2 by a potential divider
	'vref for ADC uses internal reference (to avoid it tracking 1 diode drop below battery voltage)


	readadc b.1,BattVol	'8 bit read

return

'########################
'#	Output Drivers
'########################
'# tested - working
'########################

ProcessOutputs:

	'##################
	'#	Motors
	'##################

	'RHMotorSpeed - 0 to 74
	'Direction 0 = forward

	'Value 75  = forward
	'Value 150 = stop
	'Value 225 = Backwards

	'convert values to suit servo
	if RHMotorDirection = 0 then
		RHMotorCalc = 150 - RHMotorSpeed
	endif
	if RHMotorDirection = 1 then
		RHMotorCalc = 150 + RHMotorSpeed
	endif

	if LHMotorDirection = 0 then
		LHMotorCalc = 147 - LHMotorSpeed
	endif
	if LHMotorDirection = 1 then
		LHMotorCalc = 147 + LHMotorSpeed
	endif


	'18M2
	servo 6,RHMotorCalc
	servo 7,LHMotorCalc

	'##################
	'#	Heater
	'##################

	'Heater = 	0 - LED Off
	'			1 - LED On


	'18M2
	if Heater = 0 then
		low C.1
	else
		high c.1
	endif


return


You can read the other pages on building the robot here:

Part 1 - Picaxe Robot.

Part 2 - Picaxe Robot Code.

Part 3 - Picaxe Robot - Battery Box and Main Housing.

Part 4 - Picaxe Robot - Wings, seats and lights.

Part 5 - Picaxe Robot - Details.

See also: Car Gallery, Model engineering Gallery.