Let's Make Robots!

Picaxe programming

I ve been trying to write a code for my robot, but no luck. This is for a school project so it is sort of urgent. I tried adapting Frits Start here robot code. I will learn coding but i dont have the time at the moment.

if anybody could help that would be great !

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
symbol servo_turn = 700
main:
pulsout trig,2 ' produce 20uS trigger pulse (must be minimum of 10uS)
pulsin echo,1,range ' measures the range in 10uS steps
pause 10
let range = range * 10 / 58

if range > 20 then drivef
if range < 20 then search

goto main

drivef:
high 7 : high 5 : low 4 : low 6
goto main

totalstop:
low 4 : low 5 : low 6 : low 7
Servo 0,125
wait 1
return

 

search:
gosub totalstop

lookleft:
gosub slookleft
pause servo_turn
gosub totalstop
readadc range, b1

lookright:
gosub slookright
pause servo_turn
gosub totalstop
readadc range, b2

if b1<b2 then
gosub bodyturnleft
else
gosub bodyturnright
end if
return

bodyturnleft:
low 4 : high 5 : low 6 : low 7
pause 500
gosub totalstop
return

bodyturnright:
low 5 : high 7 : low 4 : low 6
pause 800
gosub totalstop
return

slookleft:
servo 0,175
return

slookright:
servo 0,75
return

 

 

Here is the new code with a few modifications, no mater what the distance in front of the sonar it allways trys to go right, realy odd.

 

symbol trig = 3
symbol echo = 6
symbol range = w1
symbol servo_turn = 700
main:
pulsout trig,2
pulsin echo,1,range
pause 10
let range = range * 10 / 58

if range > 20 then GOSUB drivef
if range < 20 then GOSUB search


goto main

drivef:
high 7 : high 5 : low 4 : low 6
goto main

totalstop:
low 4 : low 5 : low 6 : low 7
Servo 0,125
wait 1
return

 

search:
gosub totalstop

lookleft:
gosub slookleft
pause servo_turn
gosub totalstop
readadc range, b1

lookright:
gosub slookright
pause servo_turn
gosub totalstop
readadc range, b2

if b1<b2 then
gosub bodyturnleft
else
gosub bodyturnright
end if
return

bodyturnleft:
low 4 : high 5 : low 6 : low 7
pause 500
gosub totalstop
return

bodyturnright:
low 5 : high 7 : low 4 : low 6
pause 500
gosub totalstop
return

slookleft:
servo 0,175
return

slookright:
servo 0,75
return

Well here is the working version, thanks for all the help!!

It is the SRF005 version of the Start here robot.

There is still one problem, I need to teach the robot to back if the object in front of it on both sides are to close, I need to figure out how to programme it, I was thinking of : if b1 and b2 < 15 then goback. Unfortunately it isnt as simple as that, will look at the picaxe manual and see if it has anything in it.

symbol trig = 3
symbol echo = 6
symbol range = w1
 

main:
gosub sonarrange
if range > 30 then drivef
if range < 30 then search
goto main

search:
gosub totalstop

lookleft:
servo 0,175
pause 1000
gosub sonarrange
b1=range

lookright:
servo 0,75
pause 1000
gosub sonarrange
b2=range

strait:
servo 0,125
pause 1000
gosub sonarrange
b3=range

if b3<31 then
goto others
else
goto drivef
end if
goto main
others:

if b1>b2 then
goto bodyturnleft
else
goto bodyturnright
end if
goto main

drivef:
high 7 : high 5 : low 4 : low 6
goto main

totalstop:
low 4 : low 5 : low 6 : low 7
Servo 0,125
return

bodyturnleft:
low 4 : high 5 : low 6 : low 7
pause 400
gosub totalstop
goto main

bodyturnright:
low 5 : high 7 : low 4 : low 6
pause 400
gosub totalstop
goto main

sonarrange:
pulsout trig,2
pulsin echo,1,range
pause 10
let range = range * 10 / 58
return

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

you can't RETURN when you came just from a GOTO

which in your case is actually an implicit goto 

 

you have to options :

 

  • 1) use a GOTO instead of a RETURN

 

 if b1<b2 then

gosub bodyturnleft
else
gosub bodyturnright
end if
GOTO MAIN

 

 

  • 2) GOSUB instead of GOTO 

 

if range > 20 then GOSUB drivef

if range < 20 then GOSUB search

 

but this will need drivef to RETURN 

drivef:

high 7 : high 5 : low 4 : low 6

RETURN

 

 

at first, I thought you were over the stack limit with all your nested procedures

but the Picaxe DEBUGGER was clear : STACK ERROR - RETURN WITHOUT GOSUB 

do you use the 'Yellow Play Arrow' ? 

 

 

 

 

yellow play arrow? no cant say that i do. Is it usefull?
So whats the problem? Is it not transferring to the robot is it getting stuck, whats going on with the code as written?
it goes to the robot and the robot goes foward till there is an obstical, it turns its head both ways but then intead of going in the right dirrection it allways turns to one side.

Alright here we go:

Is this the WHOLE code... Everything you are sending to the bot?  The sonar sensor you are using does not input into a ADC channel.  The servo pause time should really be in the slookright and slookleft subroutines. You need to put the sonar code in its own subroutine. Have you run a seperate test using just the sonar (with a debug command) to be sure that 20 is the distance you want to turn at? Actually, I would eliminate the whole /58 stuff and just use the raw number coming out of the sonar. Why do you have it turning for a longer time to the right instead of the left? In the "search" run-down you are repeating the total-stop subroutine. Other than that...

 

 

 

Someone should write a book on how to write code for the picaxe. If we only had a book or a manual... I nominate Chris!
yeah! We should have like maybe 3 manuals. One that is about the feauteres of the 'axe and how it works. Then the second could be about codes'n'programming shit. Then the third could cover other stuff like electronics and connections. That would've been totally awesome!
:)

that is the entire code,

for the sonar i used the code here node/66 , the distance i get back in in centimeters and is very accurate.

i dont know why i have it turning longer to one side, must have been distected.

will try to fix the code and post it

 

Look at the code.

lookleft:
gosub slookleft
pause servo_turn
gosub totalstop
readadc range, b1

lookright:
gosub slookright
pause servo_turn
gosub totalstop
readadc range, b2

 It is looking left, then totalstop makes it look straight. So when it gets to readadc is is always looking forward. Since b1=b2 it goes right. Change each function to start with totalstop.