My new robot

nevil010

Member
I am trying to make a simple robot with picaxe 18m2 and picaxe 18m2 high power board...
i have ordered the chip and i have to wait 1 or 3 days..So i finished programming..
my idea is to make a robot which have two mode auto and ir controlled..
this is my code
Code:
'----- Motors 
symbol mot1p = b.7
symbol mot2p = b.5
symbol mot1n = b.6
symbol mot2n = b.4
symbol inf = c.7

'-----servo setup
symbol servop = b.0
symbol lkfw = 150
symbol lkrt = 75
symbol lklt = 225

'----- SRF05 setup
symbol SRF05_IO = c.0                   ' In/out Pin connected to center pin of SRF05
symbol RangeF = w3                   ' define the variable to hold Front SRF05 value

'led
symbol ledr = c.2
symbol ledg = c.3
symbol ledb = c.6
'----- Program variables setup
Symbol danger = 400 		  'Used in the main program when judging what to do within certain tresholds
Symbol objectinfront = 700          'Used in the main program when judging what to do within certain tresholds
symbol freespace = 800               'Used in the main program when judging what to do within certain tresholds

main:
high ledr
irin [1000,main],inf,b0	; wait for new signal
	if b0 = 1 then auto	; switch on 1
	if b0 = 4 then manual	; switch off 1
	goto main

auto:
low ledr
high ledg
setint %10000000,%10000000,C 
gosub Readrange
if rangeF > danger then
	hans:
	gosub Readrange
	gosub drivefw	
	if rangeF > freespace then
		gosub drivert
		goto hans
	endif
		if rangeF < objectinfront then
		gosub drivelt
		goto auto
	endif	
		if rangeF > objectinfront and rangeF < freespace then
		gosub drivefw
	endif
else
	gosub drivebk

	goto auto
endif

goto auto

manual:
high ledb
low ledr
irin [1000,main],inf,b1	; wait for new signal
	if b1 = 5 then gosub drivefw	; switch on 1
	if b1 = 4 then gosub drivebk	; switch off 1
	if b1 = 1 then gosub drivert	; switch on 1
	if b1 = 4 then gosub drivelt	; switch off 1
	if b1 = 4 then main	; switch off 1
	goto manual

interrupt:
low ledg
low ledb
goto main


drivefw:
high mot1p low mot1n high mot2p low mot2n
return 

drivebk:
high mot1n low mot1p high mot2n low mot2p
return 

drivelt:
high mot1p low mot1n high mot2n low mot2p
return 

drivert:
high mot2p low mot2n  high mot1n low mot1p 
return 

'----- Sub routine to work with the SRF05
ReadRange: ' sub routine to read distance
PULSOUT SRF05_IO, 10         ' issue 10uS trigger pulse
PULSIN SRF05_IO, 1, RangeF ' measure echo time, put it into the variable RangeF
pause 10
return ' end of sub routine
so is there any problem with this code? need any improvements ? i need help
This is the plan how i want to connect everything{which i have attached}
Please reply
 

Attachments

Buzby

Senior Member
Hi Nevil,

Welcome to the forum !

I've run your code through the simulator with no errors, very good !.

I'll have a closer look later, got to go out just now.

One quick thing I noticed :

Code:
gosub Readrange
if rangeF > danger then
	hans:
	gosub Readrange
	gosub drivefw	
	if rangeF > freespace then
You read the range, if > danger you read the range again, then drive some distance, then use the range value that you got *before* you moved.

Is this right ?


Cheers,

Buzby
 

nevil010

Member
Thank you buzby....
I didn't noticed it :D..
Will interupt work fine in auto mode because i cant simulate that..

Thankx again
 

Buzby

Senior Member
Hi Nevil,

For now I would suggest you just run the 'auto' section of your code, and leave the IR and interrupt stuff till later.

This will give you a robot that can navigate around properly, then you can add the other functions.

Code:
auto:
low ledr
high ledg
' setint %10000000,%10000000,C     <- commented out for now
gosub Readrange
if rangeF > danger then
        sertxd("Danger detected",cr)  ' <-- See note in post below
	hans:
	gosub Readrange
	gosub drivefw	
	if rangeF > freespace then
		gosub drivert
		goto hans
	endif
		if rangeF < objectinfront then
		gosub drivelt
		goto auto
	endif	
		if rangeF > objectinfront and rangeF < freespace then
		gosub drivefw
	endif
else
	gosub drivebk

'	goto auto   <- you don't need this here, the 'goto' after the 'endif' will work.
endif

goto auto
A useful tip is to put some 'sertxd ("Event happened",cr)' lines in various places.
This will print an event log in terminal window when running the simulator.
I find this really useful for tracing just what the code is doing.

Cheers,

Buzby
 

Buzby

Senior Member
Hi Nevil,

Use the attached code in the simulator. It will let you test your code easier, before you get your hardware.

It simulates the SRF by using an ADC on pin B.1.

Cheers,

Buzby
 

Attachments

Buzby

Senior Member
It's much easier to adjust the simulated SRF while the simulator is running, you don't need to keep stopping and starting the program flow.

Put the code in the simulator and try it.
 

nevil010

Member
i know i am annoying you but it is very important for me..

when i simulate i get this..

'sertxd output - baud 4800,n,8,1
ReadRange 90


'sertxd output - baud 4800,n,8,1
Danger


'sertxd output - baud 4800,n,8,1
ReadRange 90


'sertxd output - baud 4800,n,8,1
drivefw


'sertxd output - baud 4800,n,8,1
drivefw

is the variable of readrange in cm?
 

Buzby

Senior Member
Hi Nevil,

I don't understand where the text you show is coming from.

Are you running exactly the code I posted ?.

--

There is no need to know the units of measurement, your code only compare values.

e.g. The statement 'If rangeF > 400' is comparing two numbers. They might be cm, inches, or miles, it doesn't matter to the code.
 

nevil010

Member
Ya..I ran your code! it works fine! why?any problem?...
--
ok
--
and can u help me on how to set interupt for infrared :D .
 

Buzby

Senior Member
Hi Nevil,

I'm really confused by what you say you saw in the PE terminal.

It should look something like the attached picture. It shouldn't be printing anything about a baud rate !.

Please post your code.
 

Attachments

nevil010

Member
'sertxd output - baud 4800,n,8,1
ReadRange 50


'sertxd output - baud 4800,n,8,1
No danger


'sertxd output - baud 4800,n,8,1
drivebk


'sertxd output - baud 4800,n,8,1
ReadRange 50


'sertxd output - baud 4800,n,8,1
No danger


'sertxd output - baud 4800,n,8,1
drivebk


'sertxd output - baud 4800,n,8,1
ReadRange 50


'sertxd output - baud 4800,n,8,1
No danger


'sertxd output - baud 4800,n,8,1
drivebk
 

hippy

Ex-Staff (retired)
I'm really confused by what you say you saw in the PE terminal.
That confused me as it's not the PE Terminal but the simulation Serial Output Buffer display :)

It should look something like the attached picture. It shouldn't be printing anything about a baud rate !
Having it or not is a checkbox setting via View -> Options -> Simulation -> Display serial baud rate information.
 

Buzby

Senior Member
now what should i do?
Well, that's rather up to you.

You could build the chassis for the robot, and fit the motors, wheels, battery boxes etc.

You could build the electronics.

You could program the PICAXE.


Personally I would do all three, in that order.

Cheers,

Buzby
 
Top