0 - 5 volts to servo positon. — Parallax Forums

# 0 - 5 volts to servo positon.

Posts: 22
edited 2005-01-19 05:56 in Robotics
Hi need help

I have adc0838 A/D converter reading 8 pots in single ended mode.

I get 0 - to 5 volts output from the A/D AS 0012 - 0500

I have been able to use some math to get usable pulse with numbers from a variable but not what i want.

How do I trasnlate the output to pulse withs, and·What is offseting

please help any links to reading matiral will help

·

## Comments

• Posts: 1,917
edited 2004-12-10 04:58
I found this program with Google using the search term:

ADC0838 BASIC Stamp

Maybe it will contain some useful hints:

http://web.bii.a-star.edu.sg/~james/files/A2D_box/program.bs2.old
• Posts: 22
edited 2004-12-10 12:57
thanks

also 0012 - 0500 is using word size var (close to actual votage)

or I can get 0 - 255 using byte size var
• Parallax Engineering Posts: 14,406
edited 2004-12-10 14:46
AJI,

·· While Tracy Allen is normally the math guru here, I was looking at your post, and today for some reason (Maybe because I am not fully awake yet), it made a little sense, and sparked an idea.· If you're getting values of 12-500 from your readings on the ADC, and want to translate them into servo positions, that actually seems easy...

·· Since I am not completely with it I will just try to make sense...On a stock BS2, the PULSOUT command for moving the servo ranges from roughly 500 to 1000...Since you're getting the same range, just off by around 500, then I would try something like:

ServoPos = ADC_Val + 500
PULSOUT Pin, ServoPos

·· Of course, you will need to integrate the PULSOUT·into your refresh loop.· But it seems reasonable to me that this should work, although I have not tried it.· Thought?· Comments?· Caffiene?!?

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage

Knight Designs
324 West Main Street
P.O. Box 97
Montour Falls, NY 14865
(607) 535-6777

Business Page:·· http://www.knightdesigns.com
Personal Page:··· http://www.lightlink.com/dream/chris
Designs Page:··· http://www.lightlink.com/dream/designs
·
• Posts: 6,491
edited 2004-12-10 16:00
It's odd that you're getting 9-bit values from an 8-bit ADC... perhaps your interface has too many clock bits in the data section and what you're actually getting in your LSB (bit0) is a repeat as the ADC0838 sends channel data out MSBFIRST then LSBFIST if you keep clocking bits. While I don't actually have an ADC0838, I have used the ADC0832 which has similar setup requirements. I updated to my ADC0832 program to work with the ADC0838 -- give it a try and see if you get proper 8-bit values. The rest is easy.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
Dallas Office

Post Edited (Jon Williams) : 12/10/2004 4:03:01 PM GMT
• Posts: 22
edited 2004-12-10 17:40
yes that is true.

·SHIFTIN Dio, Clk, MSBPOST, [noparse][[/noparse]adc0\9]····· ' read selected input

but my adc0 variable is not byte it is word.· Doesent" \9 " aquire 9 bits from the adc

and actualy it works better for what I need. The numbers are easier to work with.· Go figure dumb luck I tried diferent configurations to get digits that were easy.

my servos go from 360 - 1260 and I get almost 180 deg of throw.

now I go from pot( 5k) to servo with little code. here is what I came up with.

see attached.

thanks for every ones help.

what do you think ??

(50k pot has bad voltage fluctuations)
• Posts: 6,491
edited 2004-12-10 18:07
No. If you look at the ADC0838 docs the is an extra bit at the beginning of the data output sequence that is not part of the output value.· We do need the extra clock, but not that bit.· When shifting nine bits into an 8-bit variable the first bit in (extra) ultimately gets shifted out of the variable we're using.· In effect, SHIFTIN in that program works like this:

```SHIFTIN_ADC083x:
FOR clock = 1 TO 9
outData = outData << 1    ' adjust output variable
PULSOUT Clk, 50           ' provide clock pulse
outData.BIT0 = Dio        ' sample DO line from device
NEXT
```

Notice out the output variable is shifted left then the data line is sampled and dumped into BIT0?· If you work through you'll see that the extra bit gets dumped off the MSB end after the 9th clock pulse.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
Dallas Office
• Posts: 22
edited 2004-12-10 18:51
Yes I see

But like I said my variable is not byte size. It is word size so I get to keep the extra bit.

Again go figure dumb luck, I get a value that is easier to covert for pulse widths.

It might not be a useable value anywhere else but it works here.

I realy am just learning all this as I go along and it is a blast.·Some of it I get and some of it is just a mystery.

It might not make any logical sence but somehow it works.

It is a little touchy when I grab the R/C style joystick but once moving the servos are smooth.

when I try Byte size I don·not get a liniar increase in digits. I get·0-255 then as the pot keeps turning is start at 0 again and then back to 255.

If i need only partial throw I will go back to this·and adjust the math.

This is what I call trial by FIRE·
·
• Posts: 6,491
edited 2004-12-10 19:17
What you can do then is clear that extra bit from your word variable:

adcVal = adcVal & \$00FF

before doing your other calculations.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
Dallas Office
• Parallax Engineering Posts: 14,406
edited 2004-12-10 21:24
Okay, so this may nullify my reply...Since the data I was working with isn't what's expected...When you start getting the range of numbers you're supposed to be getting, then we can find a better formula for your application.· Jon's right though, you should be stripping that bit, or using a byte variable...By using trial and error, you might get something that works, but later down the road when you look at it, you may not even remember why you did it that way, and anyone who helps you on it will be stumped.· I didn't have the datasheet, so I wasn't privy to what you were supposed to be getting.· I just assumed that section was working properly.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage

Knight Designs
324 West Main Street
P.O. Box 97
Montour Falls, NY 14865
(607) 535-6777

Business Page:·· http://www.knightdesigns.com
Personal Page:··· http://www.lightlink.com/dream/chris
Designs Page:··· http://www.lightlink.com/dream/designs
·
• Posts: 22
edited 2004-12-11 04:31
ok I get the point

Lets say I do it right. Variable goes back to byte size

Now I have the problem that the the byte side variable resets back to 0 half way between 0 and 5 volts from the pot.

I measured the voltag at the pot and I do have 0-5 volts variable.

AGND,·DGND, and COM are all conected Vss

one side of pot is +5v form pin20 other side is at Vss, Center of the pot is at pin 0

Power supply is 12vdc into the regulator.

what should I be getting for·byte values , what is expected?
• Posts: 6,491
edited 2004-12-11 14:24
What's connected to Vref? If you want to get 0 - 255 out with 0 - 5v in, you need to connect 5v to Vref.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
Dallas Office
• Posts: 22
edited 2004-12-11 15:27
Sorry Vref is connected to Vdd if I remove it I get no reading at all.

ok here is smomething strange, I can get 0-255 out ·with 0-5v in

if I change shiftin to SHIFTIN Dio, Clk, [noparse]/noparse][b]adc0\8[/b

Removing the extra clk pulse lets it work corectly ????

The timing must be diferent on the 0838.

Now I go

·adco=adc0*5
·IF adc0<330 then adc0=330
·if adc0>1230 then adc0=1230

and I get my usable range of 330-1230 if I need to reduce the throw of the pot I only have to change the *5 to a larger value. I lose some resolutin but I guess thats what hapens when you squeeze 180deg of movment into 80 deg of controll lever movment.

·
• Posts: 6,491
edited 2004-12-11 15:36
Since you're getting 0 - 255 now (I don't have a part to test so I will have to go with your assertion -- though it doesn't match the data sheet), you can use a simple equation to get your desired output range from the pot's span.

You want 0 - 255 to become 330 to 1230 ... so try this mx+b equation:

adc = adc */ \$388 + 330

How does it work? Well, your output range has a span of 900, so you divide that by 255 (max pot value)·to get the slope (3.5294 -- the 'm' in mx + b). We can use the */ operator to multiply by fractions (in increments of 1/256). To use */ you take your fractional value (3.5294) and multiply it by 256; you 903.5 -- round up to 904.· I like to convert to HEX as I can see the whole part in the upper byte, the fractional part in the lower byte.· 904 in hex is \$388.· Of course, we have to add the minimum value; then we're done.·

You can run this little test code to see that it works:

```Main:
FOR adc = 0 TO 255 STEP 5
DEBUG DEC3 adc, TAB, DEC (adc */ \$388 + 330), CR
PAUSE 100
NEXT
END
```

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
Dallas Office

Post Edited (Jon Williams) : 12/11/2004 3:43:07 PM GMT
• Posts: 22
edited 2004-12-11 17:47
Jon you are the master.

·Works perfect. That was what I was looking for.

Now On R/C gimbal I only have partal throw of the pot. I see the max value IE 125, the formula incrases the·slope and I get full out put with less pot travel.

Thanks. Now how do I stabalize the voltage form the pot so the servos don't twitch??

PS the timing sheet I have for the 8038 shows·7-0 ·bits for the first MSB

Post Edited (AJI) : 12/11/2004 5:50:57 PM GMT
• Posts: 6,491
edited 2004-12-12 04:04
Digital filtering. A simple way to it is to add a portion of the last reading with a portion of the current reading. For example:

adcNew = (adcNew / 2) + (adcOld / 2)
adcOld = adcNew

When you do this the proportions (which need to add up to 100%) will control how fast your value moves from old to new. For a slower response, use a larger portion of the old value. This values will give you 25% new, 75% old:

adcNew = (adcNew */ \$40) + (adcOld */ \$C0)
adcOld = adcNew

As you can see in the examples above, you must save your new "filtered" value before calling your ADC input again.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
Dallas Office
• Posts: 236
edited 2004-12-22 02:45
Can this filtering be used on the output of a DS1620? How would it be modified to work with word size variables?· This looks nicer than having 16 samples or so to do a rolling averaging.
• Posts: 6,491
edited 2004-12-22 03:11
Yes, it can be used with any size variable. No modification is required -- just make sure the output of the equation can hold the [noparse][[/noparse]final] result.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
Dallas Office
• Posts: 12
edited 2005-01-19 05:56
```You want 0 - 255 to become 330 to 1230 ... so try this mx+b equation:
```

That really worked well for my adc...Thanks

now suppose i get something like 10600 - 20250
and i need to convert it to something like 120 - 1100..
..how do i do it..

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
**·ice**

Gerard Sequeira
Machinegrid.com·:: Robots at Work!!
·
Sign In or Register to comment.