Let's Make Robots!

Sharp IR tracking code

I'm trying to get a Sharp IR sensor on a servo to track the left edge of an object, i came up with this code:

symbol detection = 50

main:
readadc 4, b1
if b1 > detection then
goto servoleft
else
goto servoright
end if
goto main

servoleft:
servo 3, 100
return

servoright:
servo 3, 200
return

 

It detects objects, but it will see something, then go all the way left, and if not, it will go all the way to the right.  How would one go about getting it to find and lock onto the left edge of an object?  or, if there was a  way to get it not to go 100 or 200, but to just generally turn left or right without giving the servo a specific position.

 Any thoughts?

Comment viewing options

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

I got it!

 

symbol detection = 50

main:
readadc 4, b1
if b1 > detection then
servo 3, 100
else
servo 3, 200
end if
goto main

 

this makes it converge right onto it with no problems.

Is it possible to get a value of the position of a servo at a point in time and put it into a variable.  so you can tell it to servo 3, 200 , but when servopos = 150 while trying to go to 200, then do some action?

This is all "psudo code" you will have to clean it up.

First off, get rid of the servo 3,100 and 3,200. Instead the 100 and 200 need to be a variable. Start the code (up with symbol) with let b1=150 then servo 3,b1 to get things centered to start.

Allright the main body:

You don't want the servo to slam from stop to stop, you just want it to nudge over (one direction or the other) a little bit if it sees an edge or not. Get a good "check adc / move servo loop" going. In that loop you can use a inc b1 or dec b1 as a command. Inc and dec simply add or subtract 1 to the variable. Also min and max will keep it from going too far. Something like:

main:

readadc  4,b1

if b1 > dectection then gosub servoleft (or right, I dunno)

if b1 < dectection then gosub servoright (or left, I dunno)

goto main

 

servoleft:

inc b2

servo 3,b2 max 200

return

servoright:

dec b2

servo 3,b2 min 100

return

--You will have to add some code to swing back to the start if the servo makes a full sweep without seeing anything. But this is the VERY basic idea. 

And yes you can get the value of the servo posistion. You know the position because you told it where to be in the first place.

This makes him track and follow objects:

symbol detection = 50
let b1=150

main:
readadc 4,b1
if b2 < 180 and b2 > 120 then
low 5 : high 6 : high 4 : low 7
end if
if b2 < 120 then
low 6 : low 5 : high 7 : high 4
end if
if b2 > 180 then
low 7 : low 4 : high 5 : high 6
end if
if b1 > detection then gosub servoleft
if b1 < detection then gosub servoright
goto main

servoleft:
let b2=b2 max 200 min 100
inc b2
servo 3,b2
return

servoright:
let b2=b2 max 200 min 100
dec b2
servo 3,b2
return

 I'm going to upload a video in about 5 minutes. (youtube is processing it right now)

I'm rather new to programming, but, i just finished some code that sets a sort of, "mode bit", or whatever the official programmer guru term is for this, to make the robot stop when it hasn't found anything for a while, and then start back up following and object when it gets in range. b5 and b6 are 0 or 1 for the motor left and motor right routines to go to hault.

 symbol detection = 40
let b2=150
let b2=b2 min 100 max 200
let b5 = 0

main:

readadc 4,b1
if b1 > detection then gosub servoleft
if b1 < detection then gosub servoright
if b2 < 180 and b2 > 120 then gosub onward ' if facing somewhat forward, drive forward
if b2 < 120 then gosub rightward ' if servo is right then move right
if b2 > 180 then gosub leftward ' if servo is left then move left
goto main

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

rightward:
let b4 = b4 min 0 max 100
inc b4
b3 = 0
if b4 = 100 then
b5 = 1
end if
if b5 = 1 then
dec b4
end if
if b4 > 0 and b5 = 1 then gosub staticalert
if b4 = 0 then
b5 = 0
end if
low 6 : low 5 : high 7 : high 4
return

leftward:
let b3 = b3 min 0 max 100
inc b3
b4 = 0
if b3 = 100 then
b6 = 1
end if
if b6 = 1 then
dec b3
end if
if b3 > 0 and b6 = 1 then gosub staticalert
if b3 = 0 then
b6 = 0
end if
low 7 : low 4 : high 5 : high 6
return

servoleft:
inc b2
let b2 = b2 max 200 min 100
servo 3,b2
return

servoright:
dec b2
let b2 = b2 max 200 min 100
servo 3,b2
return

staticalert:
low 6 : low 5 : low 7 : low 4
goto main

 

anything i should be doing differently or maybe some best practice tips?