Need help w/ prog. controlling servo with GP2D12

Hello, I need to make a program that will move a servo until the GP2D12 reaches a desired value. The min range is 1" and max 14". The part that i don’t get is if i tell (in my program) for a servo to reach a value (ex. 275), the value 275 exists in two different areas on the IR sensors data sheet. Does anyone have a program that can determined between the two possable values and choose the right one?

Bane

The minimum range of the GP2D12 is around 4". You should not try to measure distances less then this as they will be in error.

I used formulas from this page:

maslab.csail.mit.edu/2002/wiki/S … D12_E.html

Alan KM6VV

Our results were like this. This is what we read from the Bot Board II with an Atom Pro 28.

3" - 525
4" - 500
5" - 425
6" - 360
7" - 315
8" - 280
9" - 250
10" - 235
11" - 220
12" - 210
13" - 200
14" - 190
15" - 185
16" - 180
17" - 175
18" - 170
19" - 165
20" - 160
22" - 155
24" - 150
26" - 145
28" - 140
30" - 135

I’m using just theAtom Basic and these are the values that i got

(all in inches)
1-254
2-385
3-568
4-510
5-405
6-368
7-321
8-284
9-252
10-220

pretty close huh, you know, is there a way to have the debugges data to go into a spreadsheet than just watching for the value and writting it down?

O.K., the bad news, i’ve tried to make a few programs but they have utterly failed :angry: . But the good news is i’ve got LYNXMOTION!
Could i get some pointers on what types of commands to use as i describe the program?

I have a 14" track with roller tray controlled by a servo and a GP2D12 ir sensor for a “tray-positioning device”. I need a program that i can tell a servo to go until the ir sensor has reached a certain value.

so im going to attempt to make a program.

[code]
irvariable var byte
loop:
adin ax0,1,ad_ron,irvariable

“now i need a command that will make the servo move until irvariable reaches that value”

if irvariable >500 then
gosub servo_pan_right

ir irvariable <500 then
gosub servo_pan_left

'subroutines

… [/code]

Any ideas of how to make this program work?

bane

I’ve been reading up on the “until and repeat” command in the hand book. However, my program still has errors&(.

It says that there is a error on line 8

[code]counter var word
irvariable var byte

loopyloop:
adin ax0,1,ad_ron,irvariable
repeat
irvariable = irvariable * 2
servo_pan_r [dec irvariable]
until irvariable < 400
next

repeat
irvariable = irvariable * 2
servo_pan_l [dec irvariable]
until irvariable > 1000
next
goto loopyloop

servo_pan_r
for counter = 1 to 40
pulsout p0, 1400
pause 10
next

servp_pan_l
for counter = 1 to 40
pulsout p0, 1600
pause 10
next
[/code]

Please help!

Bane

I’m not a programmer, but… I don’t think you need a “next” when using “repeat…until”. No idea if this’ll fix your problem, but hey, I might as well mention it. :smiley:

Check out the example on the BM Wiki:

basicmicro.wikia.com/wiki/MBasic … T…_UNTIL

thanks, i probably needed to put that in too:). Still, my compiler doesn’t like the servo_pan_r [dec irvariable] part of it. ???
anybody know how to make this work?

Oh, I think I see what the problem is. Looks like you’re trying to call your “servo_pan_r” subroutine, eh? For that, you need to use gosub. Look at the second syntax example here for your application:

basicmicro.wikia.com/wiki/MBasic … …_RETURN

Also, you need a “return” at the end of your subroutines, after “next”.

Hope this helps!

Ok, it compiles :smiley: , but it now doesn’t stop when it reaches my desired value

[code]counter var word
irvariable var byte

loopyloop:
adin ax0,1,ad_ron,irvariable
repeat
irvariable = irvariable * 2
gosub servoR
until irvariable < 400

repeat
irvariable = irvariable * 2
gosub servoL
until irvariable > 1000

goto loopyloop

servoR:
for counter = 1 to 40
pulsout p0, 1400
pause 10
next
return

servoL:
for counter = 1 to 40
pulsout p0, 1600
pause 10
next
return[/code]

One problem is that your variable irvariable is defined as a byte. A byte can have a maximum value of 255, so your testing against 400 or 1000 will not work. Try changing it to a word. That will give it a maximum value of 65535.

Good Luck
Kurt

same thing:(
i tried debugging the ir variable and the value is all screwy. I’m checking my connections

[code]counter var word
irvariable var word

loopyloop:
adin ax0,1,ad_ron,irvariable
repeat
gosub servoR
until irvariable < 200

repeat
gosub servoL
until irvariable > 450

goto loopyloop

servoL:
for counter = 1 to 10
pulsout p0, 2000
pause 10
next
return

servoR:
for counter = 1 to 10
pulsout p0, 700
pause 10
next
return[/code]

I am not sure what this code is supposed to do. It appears to me like this code is very likly to hang:

For example suppose the first adin gives you a result of lets say 250.

Now you have the code:

repeat 
    gosub servoR 
  until irvariable < 200 

It says to repeat calling ServoR until the irvariable is less than 200. But there is nothing in this loop that changes the value of irvariable, so you will probably simply hang there. I am not sure exactaly how you wish for this code to work. If you are simply wanting to go right and go left, you might change the code to look something like:

[code]counter var word
irvariable var word

loopyloop:
repeat
adin ax0,1,ad_ron,irvariable
gosub servoR
until irvariable < 200

repeat 
    adin ax0,1,ad_ron,irvariable 
    gosub servoL 
until irvariable > 450 

goto loopyloop 

servoL:
for counter = 1 to 10
pulsout p0, 2000
pause 10
next
return

servoR:
for counter = 1 to 10
pulsout p0, 700
pause 10
next
return[/code]

Oh that makes sense. So the adin ax0,1,ad_ron,irvariable part makes irvariable equal the sensor reading. Now it should constinely get new values for irvariable :slight_smile: .
What is program is doing is using the GP2D12 as a positioning device for a servo on a linear track. the robot track looks simular to this one lynxmotion.com/images/html/proj038.htm

I would post a image if it was possable to do from my desktop :smiley: .

Hmm, still doesn’t stop though then is approches the value…???

is there anyway to debug the irvarable into a spread sheet?

Just had a thought. If i had irvarable set to a byte then changed it to a word, does the reading value of the sensor stay the same?

Bane

I believe the documentation said that the AtoD converter was a 10 bit converter. So it has a range of 0-1023 which will not fit into a byte.

If it were me, I would probably add some debug messaging to the loop. For the Pro: I would do something like:
serout S_OUT,I2400,[dec var,13,10]…

Note: you need to set the appropriate baudmode for that atom and var is the name of the variable you wish to output. You can then connect one of the terminal windows at the bottom of the IDE to your communication port and output the values to see where they are going. If you find that it is outputing to many, you can make it sophisticated, like only do an output if the old and new values do not match, or only every so many times through the loop, or after a certain amount of time has elapsed…

Good Luck