Author Topic: servo motor control through potentiomer  (Read 4411 times)

0 Members and 1 Guest are viewing this topic.

Offline e.hamzaTopic starter

  • Beginner
  • *
  • Posts: 4
  • Helpful? 0
servo motor control through potentiomer
« on: May 20, 2011, 03:25:42 PM »
i am supposed to control a servo motor movement using this code through the resistance change of a potentiometer

The code i've reached so far is

void main() {

int value,x1;

setup_adc_ports(ALL_ANALOG); setup_adc(ADC_CLOCK_INTERNAL); setup_psp(PSP_DISABLED); setup_spi(SPI_SS_DISABLED); setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1); setup_timer_1(T1_DISABLED); setup_timer_2(T2_DISABLED,0,1); setup_comparator(NC_NC_NC_NC); setup_vref(FALSE);

while (1) { set_adc_channel(0); delay_us(10);

value=read_adc();

x1 = value +1000 ;

output_high(PIN_b0); delAY_us(x1); output_low(PIN_b0); delay_ms(20); } }

any ideas guys ?please :S

Offline Gertlex

  • Supreme Robot
  • *****
  • Posts: 763
  • Helpful? 24
  • Nuclear Engineer ยท Roboticist
    • Index of Oddities
Re: servo motor control through potentiomer
« Reply #1 on: May 21, 2011, 08:45:47 AM »
Put [ code ] [ /code ] brackets around your code to make it easier to read; it will preserve whitespace :) (you can modify the post)

Unfortunately I don't know enough to help you here, but someone will.
I

Offline mstacho

  • Supreme Robot
  • *****
  • Posts: 376
  • Helpful? 10
Re: servo motor control through potentiomer
« Reply #2 on: May 24, 2011, 11:04:29 AM »
Can you comment your code a bit and tell us what you're trying to do with it?  It seems like you don't actually use the code to move the motor yet, is that right?

MIKE
Current project: tactile sensing systems for multifingered robot hands

Offline e.hamzaTopic starter

  • Beginner
  • *
  • Posts: 4
  • Helpful? 0
Re: servo motor control through potentiomer
« Reply #3 on: May 24, 2011, 04:40:30 PM »
the code should connect the servo to a potentiometer as the resistance of the potentiometer varies the servo motor angel varies  it worked just fine in simulation but wouldn't work in real life :S

Offline mstacho

  • Supreme Robot
  • *****
  • Posts: 376
  • Helpful? 10
Re: servo motor control through potentiomer
« Reply #4 on: May 24, 2011, 07:25:29 PM »
Well...the code doesn't really make sense.  You're using the pot as a position sensor, right?  And it seems that your idea is to turn the digital IO pin ON for some time that is dependent on the ADC reading -- the higher the ADC reading, the longer it's on for, and therefore the servo moves to some position depending on the on-time...or something?

Or is it this:  you have a pot that you control.  You want to move the pot to some position and have the servo follow that position?

Which microcontroller are you using?  It'll probably be easier if it has some kind of native servo code.  You take the output of the pot, determine the number of degrees it is turned by reading the ADC, and having the uC's servo control code do it all for you. 

There are some other reaons it MAY be screwing up:

1) your pot is bad, and isn't giving you any voltage at all, or some weird thing like that.  It happens with potentiometers, so make sure it isn't this.

2) your pot gives you less voltage than you think (I assume you're reading it with a pull-down (or pull-up) resistor?), so it actually IS moving, but the ADC just keeps registering like 20 or 30 instead of the 500 you're expecting

3) (probably not): your servo commands are being sent wrong, because your servo is some nonstandard part.

Just some possibilities to check.  The code actually looks OK as long as it's the second option I wrote out there.  What exactly isn't it doing?

MIKE
Current project: tactile sensing systems for multifingered robot hands

Offline e.hamzaTopic starter

  • Beginner
  • *
  • Posts: 4
  • Helpful? 0
Re: servo motor control through potentiomer
« Reply #5 on: May 25, 2011, 01:46:51 AM »
yea it's the second one but the problem is when i try it it just moves to +90 and stops :s

Offline mstacho

  • Supreme Robot
  • *****
  • Posts: 376
  • Helpful? 10
Re: servo motor control through potentiomer
« Reply #6 on: May 25, 2011, 06:42:36 AM »
What is the voltage coming out of the pot? 

here's a guess:  your ADC goes to 512 (that would make it...9 bit? 10?)  You turn it all the way around once.  This makes the digital IO pin go on for 1500us, which is typically 90 degrees on a servo.  Is that sort of what happens?  Can you get it to go to 45 degrees by turning the pot only, say, halfway or something, or does it always just rotate to 90 no matter what the pot says?

MIKE
Current project: tactile sensing systems for multifingered robot hands

Offline e.hamzaTopic starter

  • Beginner
  • *
  • Posts: 4
  • Helpful? 0
Re: servo motor control through potentiomer
« Reply #7 on: May 25, 2011, 03:42:18 PM »
yes it always rotate 90 degrees regardless of what the potentiometer resistance is
 and when i tried a simple code that uses pwm without potentiometer it did the same is that because am using a crystal 20MHz ?

Offline newInRobotics

  • Supreme Robot
  • *****
  • Posts: 1,015
  • Helpful? 48
  • N.I.R.
Re: servo motor control through potentiomer
« Reply #8 on: May 25, 2011, 04:39:09 PM »
I think You should use trimmer as part pf voltage divider, then You can get well controlled voltage reference. Then with linear function You can get linear responce of servo.

Another thing, use internal, rather than crystal, uC oscillator to avoid complications. You really don't need 20MHz sampling rate in order to to sample ADC.

output_high(PIN_b0); delAY_us(x1); output_low(PIN_b0); delay_ms(20); } }
This is an attempt to simulate PWM in software, try using hardware PWM instead.

Why do You use
Quote
x1 = value +1000
? Your ADC already gives You 8 bit representation of pot voltage, can't You just use it directly in PWM control?
"Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian W

Offline mstacho

  • Supreme Robot
  • *****
  • Posts: 376
  • Helpful? 10
Re: servo motor control through potentiomer
« Reply #9 on: May 25, 2011, 05:29:15 PM »
I agree with the voltage divider idea, but the OP is using an actual servo, not just a motor.  They're trying to use the pot as an input device that tells the uC where to send the servo, so no PWM is needed. 

I'm pretty sure that your potentiometer isn't working like you think.  Stick a voltmeter in there and see what you get as the voltage across it.  If it's what you expect, then you're probably reading it wrong.  What kind of setup do you have to read it?

MIKE
Current project: tactile sensing systems for multifingered robot hands

Offline newInRobotics

  • Supreme Robot
  • *****
  • Posts: 1,015
  • Helpful? 48
  • N.I.R.
Re: servo motor control through potentiomer
« Reply #10 on: May 26, 2011, 07:10:54 AM »
They're trying to use the pot as an input device that tells the uC where to send the servo, so no PWM is needed.
That's what voltage divider is, it provides voltage for uC to read. As voltage is fed to uC the only way to control servo is by PWM (as far as I know) generated proportionally by uC.

You don't necessary need uC in order to achieve PWM with variable duty cycle. I was just working on a circuit that does just that. By changing capacitor value one can alter frequency of PWM (for servos it is 20kHz as far as I know). By adding trimmer (pot) to voltage divider that is next to lower left op-amp one can alter duty cycles that would directly translate to position of servo.
"Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian W

 

SMF spam blocked by CleanTalk