PDA

View Full Version : servo control via pot ?



n0oxv
- 15th September 2004, 01:19
Question from a newbe !

I have a 5K pot I would like to control a servo with it
I know I can use the pot command with the scale function
but is there a better way ? using adcin ?

I am not using any regulator and just running from a 4.8 V nicad battery pack and 4 mhz

currently using a 16f628
Using the pot command really seems to slow down sending pulses to the servo

<RC Servo 1 - 2 ms>

Thank You
Gary

magix
- 15th September 2004, 01:44
It is a good idea to convert a voltage with AD converter, but microcontroller 16F628 does not have internal AD converter, So you can not use instruction ADCIN in this case.
You can try 16F873 or sth. like that.

Bruce
- 15th September 2004, 03:45
You'll need a PIC with onboard A/D to use ADCIN. This works on an F876.



DEFINE OSC 4
DEFINE ADC_BITS 8 ' Set number of bits in result
DEFINE ADC_CLOCK 2 ' Set clock source (3=rc)
DEFINE ADC_SAMPLEUS 50 ' Set sampling time in uS
SVO_POS VAR BYTE

' 90 deg horn rotation (adjust for servo)
'LEFT CON 100 ' Left position 1mS
'RIGHT CON 200 ' Right position 2mS

' 180 deg horn rotation (adjust for servo)
LEFT CON 70 ' Left position 0.70mS
RIGHT CON 240 ' Right position 2.4mS

TRISA = %00001001 ' RA0 = A/D input
ADCON1.7 = 0 ' RA.3 = +Vref, Set PORTA analog and left justify result
LOW 0 ' Setup RB0 for high-going pulseout

Loop:
ADCIN 0,SVO_POS ' Read A/D channel
IF SVO_POS < LEFT THEN SVO_POS = LEFT
IF SVO_POS > RIGHT THEN SVO_POS = RIGHT
PULSOUT 0,SVO_POS ' Move servo
PAUSE 15 ' ~60Hz update-rate
GOTO Loop ' Forever

charudatt
- 19th July 2006, 16:06
I am interested in controlling 4 Servos, sensing 4 Pots Connected to the 4 Analog Inputs of 16F873.

I tried with just one Pot and One Servo , works just fine , But with 4 Pots and four Servo's, it jerks.

I am sure , i am doing some thing wrong and need some help. I am running the PIC @ 4Mhz.

Mainloop:

LOW SERV1 : LOW SERV2 :LOW SERV3 :LOW SERV4 :

GOSUB READ_POTS
'-----------------------------------------------------------------
SERVO = 150*TEMP1 : SERVO = SERVO / 255 : SERVO = SERVO + 90
pulsout serv1 ,servo : PAUSE 15
'-----------------------------------------------------------------
'-----------------------------------------------------------------
SERVO = 150*TEMP2 : SERVO = SERVO / 255 : SERVO = SERVO + 90
pulsout serv2 ,servo : PAUSE 15
'-----------------------------------------------------------------
'-----------------------------------------------------------------
SERVO = 150*TEMP3 : SERVO = SERVO / 255 : SERVO = SERVO + 90
pulsout serv3 ,servo : PAUSE 15
'-----------------------------------------------------------------
'-----------------------------------------------------------------
SERVO = 150*TEMP4 : SERVO = SERVO / 255 : SERVO = SERVO + 90
pulsout serv4 ,servo : PAUSE 15
'-----------------------------------------------------------------
pause 18

GOTO Mainloop

END

READ_POTS:

Adcin 0, TEMP1 ' Read FIRST POT
Adcin 1, TEMP2 ' Read SECOND POT
Adcin 2, TEMP3 ' Read THIRD POT
Adcin 3, TEMP4 ' Read FOURTH POT

RETURN

regards

blainecf
- 19th July 2006, 19:31
Performing calculations and a series of reads in a row is your major prob. Here's an idea - (pseudo code):

#1:

define prev_value vars

Loop:
for i = 1 to 4

read one pot (based on i)

check to see if it changed (compared to a previously saved value)

if changed, calc new value

pulse_out each of your motors
next i
goto loop


This way you're only doing one A/D read per loop, and only doing the math when the value of the pot changes (multiplication and (even more so) division are clock cycle suckers!

------------------

#2: do away with the gosubs in speed-critical situations. In-line code is harder to read, but faster

Regards,

charudatt
- 19th July 2006, 19:51
Thanks , I shall give it a try and post the results tomorrow.

regards

dhari
- 28th February 2007, 15:00
[QUOTE=charudatt;23362]I am interested in controlling 4 Servos, sensing 4 Pots Connected to the 4 Analog Inputs of 16F873.

malc-c
- 28th February 2007, 22:23
I am interested in controlling 4 Servos, sensing 4 Pots Connected to the 4 Analog Inputs of 16F873.

Not sure if this is relevant or not, but in the thread I started on using a PIC as the basis of a model train controller ( http://www.picbasic.co.uk/forum/showthread.php?t=5687 ) I needed to control four independant trains via for pots using PWM.

This used Darrel's excellent PWM routines and with an 16F676 running at 20 Mhz performed very well. This used 4 x AD convertors and used the value to adjust the PWM, and I'm sure with a simple bit of modification it would do what you want

Also have a look at this thread http://www.picbasic.co.uk/forum/showthread.php?t=3251

skimask
- 28th February 2007, 23:13
Disregard below. I didn't notice the dates on the posts above...ya know, the ones from 2004 !


You've got 78ms worth of pause's in your loop. At best, 78ms worth of pause's limits you to an update rate of just over 12hz. Servo's want an update rate of about 50hz or so.
Try this:

Mainloop:
GOSUB READ_POTS
SERVO = 150 * TEMP1 : SERVO = SERVO / 255 : SERVO = SERVO + 90 : pulsout serv1 ,servo
SERVO = 150 * TEMP2 : SERVO = SERVO / 255 : SERVO = SERVO + 90 : pulsout serv2 ,servo
SERVO = 150 * TEMP3 : SERVO = SERVO / 255 : SERVO = SERVO + 90 : pulsout serv3 ,servo
SERVO = 150 * TEMP4 : SERVO = SERVO / 255 : SERVO = SERVO + 90 : pulsout serv4 ,servo
GOTO Mainloop

READ_POTS:
Adcin 0, TEMP1:Adcin 1, TEMP2:Adcin 2, TEMP3:Adcin 3, TEMP4 : RETURN

charudatt
- 1st March 2007, 05:27
Thanks Skimask,

Information is never old. The project still stands where it was , and you seem to have put in new life after so many years.

At present I don't have that many servo's but would surely give it a try with what ever I have.

OK just a small querry. I am planning to make a Servo Signal reader, Any inputs on it. Just a servo input and display on a LCD.

regards

sayzer
- 1st March 2007, 07:11
...I am planning to make a Servo Signal reader, Any inputs on it. Just a servo input and display on a LCD.

regards



Pulsout & Pulsin