Shop OBEX P1 Docs P2 Docs Learn Events
Assistance Needed. . . — Parallax Forums

Assistance Needed. . .

ArchiverArchiver Posts: 46,084
edited 2001-08-13 00:04 in General Discussion
Hi all!

Take a look at this code and see if you can help me with this.

********Beginning of Code***********

'Robot Brains with Sonar Following and Collision Avoidance
'Copyright Doug Simpson 8/2001
dirs = %1011101111011111
'declaration of constants

pingl con 15
pingr con 12
retl con 14
retr con 11
enal con 13
enar con 10
timel var word
timer var word
inchesl var word
inchesr var word
ping var word
timel = 0
timer = 0

start:

output enal 'sets pin 13 as an output
output pingl 'sets pin 15 as an output
input retl 'sets pin 14 as an input

low pingl 'turn ping off
high enal 'turn ena (power) off. A 1 turns off the left sonar.

output enar 'sets pin 10 as an output
output pingr 'sets pin 12 as an output
input retr 'sets pin 11 as an input

low pingr 'turn ping off.

high enar 'turn ena (power) off. A 1 turns off the right sonar.


tping:

for ping = 1 to 10

if ping = 5 then pingleft
if ping = 10 then pingright

'displays microseconds for left, right, and ping count
debug dec timer ," r ", dec timel, " l ",dec ping,cr
'drive system controller subroutines

if timel <5000 AND timer <5000 then halt
if timel <9000 AND timer <9000 then slowboth
if timel <9000 AND timer >9000 then vright
if timel >9000 AND timer <9000 then vleft
if timel >9000 AND timer >9000 then forward

halt:
low 1 : low 4 : low 2 : low 3 'Stop the 'bot.
goto cycle

slowboth:
pwm 1,200,50 : pwm 4,200,50 'Slow the 'bot.
goto cycle

forward:
high 1 : high 4 'Full speed forward.

goto cycle

vright:
high 1 : low 4 'Veer Right
goto cycle

vleft:
high 4 : low 1 'Veer Left
goto cycle

pingleft: 'Does a ping on the left.

low enal 'turn power on
pause 5 'wait a tad
high pingl
pulsout 0,1500 'wait 1.2ms on a dummy line

rctime retl,0,timel 'now, wait for return pulse on the left input


low pingl 'turn ping off
high enal 'turn power off
goto cycle

pingright:

low enar 'turn power on
pause 5 'wait a tad
high pingr
pulsout 0,1500 'wait 1.2ms on a dummy line

rctime retr,0,timer 'now, wait for return pulse on the right input


low pingr 'turn ping off
high enar 'turn power off
goto cycle

cycle:

next
goto tping

******END OF CODE*******

What this is supposed to do is make my robot "follow" me. I have
two sonar rangers installed on the front of the 'bot. The sonar for
the left field of range is mounted on the right side of the bot, angled
across to the left-front, and the sonar for the right field of range is
mounted on the left, angled across to the right-front. They cross at
a point about 18 inches in front of the 'bot. The reason for this
configuration is because the sonars can't detect less than about 20
inches meaning it wouldn't go through a door less than 40 inches
wide. Plus, there is no center field. They are crossed making the
effective range from each sonar to the opposite side of the bot is
outside the 20 inch range.

This works pretty good. It was designed as a "following" system.
If I get directly in front of the 'bot, the range is less than 5000us and
the 'bot stops. If I move to either side, the sonar ranger detects
more than 5000us and turns on the appropriate motor to get back
to the 'lock' condition. If the range is less than 9000us but more
than 5000us, it makes both motors pulse to move the bot slowly.
This also happens when it goes through a narrow door. If the
rangers detect more than 9000us on both sides, the motors go on
full power because there is nothing in the way.

Now for the problem.

This thing is like a bull in a china closet with hotsticks sticking out
all over. in the house, it is very sluggish to make corrections
because the motors 'coast' too far when the power is removed. I
need brakes.
I do have a reversing circuit but if I set the halt: section to reverse-
for-a-short-time-to-brake, then turn off, then when the robot is in
'lock' condition, (it found me and is sitting there) the brake (reverse)
relay chatters every time through the program cycle.

The reason for the:

for ping = 1 to 10

if ping = 5 then pingleft
if ping = 10 then pingright

is that the sonar units require a power-off between each ping for a
short time before you can ping again. This makes the sonars
alternate once for every 10 times through the program cycle,
allowing for enough time 'off' for the sonars to be able to ping again.
It is so close that if I ' the debug line, the ping stops. the debug
adds enough delay to give the sonars enough 'off' time. And it still
allows for a ping on each side of the bot once every approximately
1/5 of a second.

Any one have any ideas how I can clean up this code or make the
brakes a one-shot thing when the 'bot is in 'lock' so the relay don't
just chatter?

Thanks for any help.
Sign In or Register to comment.