Picaxe programming, problem with head turning and scanning

 

Picaxe programming, problem with head turning and scanning.

 

For some time now i have been playing around with my code to get it just right, but i have hit a snag that i cant seem to fix.

 

If you are good at programming you will see the mistake

 

main:
for b1 = 80 to 175 step 5
servo 0,b1
pause 15
next b1
for b1 =175 to 80 step -5
servo 0,b1
pause 15
next b1
gosub sonarrange
if range < 30 then search
if range > 30 then drivef
goto main

 

This code was designed so that the robot could turn it's head while turning so that he could see object coming from both sides, but with this code he only see objects coming from the right...

 

I have looked at many code to fin inspiration but nothing came!!

 

Does anybody know how to fix this?

The three main measurements angles are 80, 125 and 175.

 

Update

Here is the entire code, might make things simpler

 

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

main:
for b1 = 80 to 175 step 5
servo 0,b1
pause 15
next b1
for b1 =175 to 80 step -5
servo 0,b1
pause 15
next b1
gosub sonarrange
if range < 30 then search
if range > 30 then drivef
goto main

 

search:
gosub totalstop
gosub lookstrait
if b3<40 then
goto landr
else
goto drivef
end if
goto main

 

 

landr:
gosub lookleft
gosub lookright
if b1<25 or b2<25 then
goto wtf
else
goto itisoktoscan
end if
goto main

itisoktoscan:
if b1>b2 then bodyturnleft
if b1<b2 then bodyturnright
goto main


wtf:
gosub goback
gosub totalstop
if b1>b2 then bodyturnleft
if b1<b2 then bodyturnright
goto main

 

 


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


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


lookstrait:
pause 100
servo 0,125
pause 1000
gosub sonarrange
b3=range
return


' this is what you will need to change to suit your robot
drivef:
high 7 : high 5 : low 4 : low 6
goto main

goback:
high 6 : high 4 : low 5 : low 7
return

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

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

bodyturnright:
gosub totalstop
low 5 : high 7 : low 4 : low 6
pause 400
gosub totalstop
goto main
' this is what you will need to change to suit your robot

 

 


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


look carefully…
Are you going to your sonarrange subroutine during each step of the “sweep” or are you only doing it once after a full sweep is finished? Also, with the (2) if range’s at the end, you are only going to make a decision after a full sweep. Get the sonar check and the if’s within the servo back and forth code.

From the test I ran it goes

From the test I ran it goes to the sonar sub routine after a left and right sweep witch meen it only detects objects from 1 side.

If i make it go to the sonar sub routine everu step it would make the scanning process very slow(at least i think)

main:
for b1 = 80 to 175 step 5
servo 0,b1
pause 15
next b1

if range < 30 then search
if range > 30 then ( what comand do i use here to tell it to continue down the line? )

for b1 =175 to 80 step -5
servo 0,b1
pause 15
next b1
gosub sonarrange
if range < 30 then search
if range > 30 then drivef

and now the problem with this code is that it wont see stuff infront


goto main

you’re right --it won’t

Within the sweep code:

if b1=left then gosub check

if b1=center then gosub check

if b1=right then gosub check

 

if range > 30 then ( what comand do i use here to tell it to continue down the line? )

there is no command --the program will just keep going

how about this?

Based on your description of the robot behavior (scan while moving, measure at 3 main points) this is what i would do:

[this is done without looking at CtC’s code above; that might be just as good or better]

 

symbol servTurn = 80 'pause for servo turning 50 or 55 degrees. adjust to match your servo
symbol range = b2

main:
gosub drivef
for b0 = 0 to 3

select b0 'if you only have three measure points, don’t waste time with the servo going back and forth
case 0
servo 0, 80
case 1
servo 0, 125
case 2
servo 0, 175
case 3
servo 0, 125 'looks back in the middle
endselect
pause servTurn
low 0

gosub sonarrange 'go measure
if range < 30 then
gosub search
endif

next b0
goto main

’i’m making these sub-routines instead of adresses in order to keep the head in the right place in the loop

sonarrange: 'this is where you will put the sonar instructions
return

search: 'this subroutine should calculate wich way to go
return

drivef:
'this is where you put the instruction to drive forward
return

I think I understood what

I think I understood what you meen, here is what the code looks like

It seems to work well but only time will tell, how do you adjust the position of were it turns to or the speed?

symbol trig = 3
symbol echo = 6

 

symbol servTurn = 80 'pause for servo turning 50 or 55 degrees. adjust to match your servo
symbol range = w1

main:
gosub drivef
for b0 = 0 to 3

select b0 'if you only have three measure points, don’t waste time with the servo going back and forth
case 0
servo 0, 80

case 1
servo 0, 125

case 2
servo 0, 175

case 3
servo 0, 125 'looks back in the middle
endselect
pause servTurn
low 0

gosub sonarrange ‘go measure
if range < 30 then
gosub search
endif

next b0
goto main

 

search:
gosub totalstop
gosub lookstrait
if b3<40 then
goto landr
else
goto drivef
end if
return

 

 

landr:
gosub lookleft
gosub lookright
if b1<25 or b2<25 then
goto wtf
else
goto itisoktoscan
end if
goto main

itisoktoscan:
if b1>b2 then bodyturnleft
if b1<b2 then bodyturnright
goto main


wtf:
gosub goback
gosub totalstop
if b1>b2 then bodyturnleft
if b1<b2 then bodyturnright
goto main

 

 


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


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


lookstrait:
pause 100
servo 0,125
pause 1000
gosub sonarrange
b3=range
return


’ this is what you will need to change to suit your robot
drivef:
high 7 : high 5 : low 4 : low 6
return

goback:
high 6 : high 4 : low 5 : low 7
return

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

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

bodyturnright:
gosub totalstop
low 5 : high 7 : low 4 : low 6
pause 400
gosub totalstop
goto main
’ this is what you will need to change to suit your robot

 

 


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

 

it works?

Good news. After i posted, i noticed a flaw in my code: It handles an object on the side(s) the same as an object in front. That might work well enough, though.

As far as adjusting where it turns to or the speed …[I assume you mean the robot <i>body</i>, not the servo.]

How far it turns is based on the amount of PAUSE after the turnleft/turnright commands. You can set a symbol for that number (like “turnAmount” or something) so you only have to enter it once.

The speed: if you’re making a Fritz-class (Start Here bot), you can’t adjust the speed, only the amount of time it’s on.

Good luck! Looking forward to reading about your bot.

 

I noticed the code aswell,

I noticed the code aswell, the robot kept stopping when an object was on eather side but thanks to a litle extra bit of code that i added before, because in the early phases of coding it would stop for no reason and then turn witch was annoying, went the robot thinks there is an object infront it stops, puts the servo strait and test the distance again just to be sure.

Actualy this is for my current robot " The Wanderer "

I was talking about the servo not the body, i know how to change how long it turns and all that, I was asking if was possible to change how far to each side the servo turns so that the robot could see object comming from the side at a steep angle.

uhhh… I am kinda good at
uhhh… I am kinda good at coding but I don’t understand the CASE and FOR commands, so I’m useless here :slight_smile:

I dont even know what they
I dont even know what they are! this is a real basic code so it was sort if easy

it’s a loop

It’s all in the Picaxe manual part 2…

the FOR statement means

"do this loop four times. each tim, b0 will be +1"

the SELECT/CASE statements mean “when b0 is 0, do THIS, when b0 is 1, do THIS”

…so it’s basically a more optimized version of:


servo 0, 80

pause servTurn

gosub sonarrange 'go measure
if range < 30 then
gosub search
endif

servo 0, 125

gosub sonarrange 'go measure
if range < 30 then
gosub search
endif

servo 0, 175

pause servTurn

gosub sonarrange 'go measure
if range < 30 then
gosub search
endif

servo 0, 125 'looks back in the middle

pause servTurn

gosub sonarrange 'go measure
if range < 30 then
gosub search
endif

 

 

Case…
Just a head’s up --When you go looking for “Case” in the manual, it is “Select Case” so look under the S’s not the C’s.

thanks
thank you chris and calculon. Just a couple more things: (calculon) were you the guy that made that cardboard robot that you have as your icon? i saw it in the Solarbotics news. Its cute! Also, for some reason my PICAXE doesn’t support GOSUB, so i use GOTO. nothing important, just hoping somebody else may have had that prob and could help me out a little

no, i didn’t make it, i just

no, i didn’t make it, i just thought it was the coolest robot i’ve seen so far, 'specially since it gets such a reaction out of meatbags (aka humans).

The reason your GOSUB command isn’t working is because you have to go to a subroutine, not an adress.

The difference: A SUBroutine has RETURN at the end of it. The axe won’t execute a subroutine without a GOSUB telling it to do so. Likewise, a GOTO will only work on an address (like MAIN:).

Also, an IF statement can goto an adress in a single line (ex. If b0 > 3 then ADDRESS) but not a subroutine (which needs an endif).

 

yup
I know, but any program I use GOSUB with will not upload. It says “hardware not found.” That’s why the Start Here code wouldn’t work. It’s really weird, I have two 28X1s and neither works with GOSUB. And yes, I do use RETURN.

that IS weird.
that IS weird.

it might be your editor, did

it might be your editor, did you try downloading a new PICAXE Programming Editor ?

thanks Calculon320 !!! the
thanks Calculon320 !!! the explanation to your code realy helped and thanx to it I optimised my code and it is now better than ever !!

np
Calculon is glad to help.