how to set servomotor to an angle

Discussion in 'Embedded Systems and Microcontrollers' started by Farhan Sheikh Ahmad, Apr 2, 2016.

  1. Farhan Sheikh Ahmad

    Thread Starter New Member

    Mar 31, 2016
    2
    0
    i was using following code to control my servomotor by atmega8. instead of stopping servo to the angle it keep on moving to random angles until the port being used stops giving pulse current

    #define F_CPU 4000000UL
    #include <avr/io.h>
    #include <util/delay.h>
    int main(void) {
    DDRC = 0b00100000;

    while(1) {
    PORTC ^= 1 << PORTC5;
    _delay_ms(100);
    }
    return 0;
    }

    Please any one help me and it it could be then suggest me some better way to do so i'll be very thankful.
     
  2. MaxHeadRoom

    Expert

    Jul 18, 2013
    10,542
    2,369
    You need feedback of some kind to position a motor accurately unless it is a stepper motor.
    Max.
     
  3. Papabravo

    Expert

    Feb 24, 2006
    10,142
    1,790
    https://en.wikipedia.org/wiki/Servo_control

    To set the servo to a given angle you must precisely control the ratio of on time to off time within a fixed time period. Your loop toggles the output every 100 milliseconds, producing a 50% duty cycle over a period of 200 milliseconds, which is pretty much guaranteed to not work as you might expect.
     
  4. Papabravo

    Expert

    Feb 24, 2006
    10,142
    1,790
    RC servos have the feedback internally and respond only to the ratio of on time within a fixed period o f 20 ms IIRC.
     
  5. MaxHeadRoom

    Expert

    Jul 18, 2013
    10,542
    2,369
    Then why didn't the OP state an RC SERVO??
    Servo control has an extremely wide technology range!.
    Max.
     
  6. shteii01

    AAC Fanatic!

    Feb 19, 2010
    3,387
    497
    OP has another, earlier, thread where they tell that they want to use PWM, ATmega8 to control servo motor. Like Papa said, since they are using PWM (which you would not know since you did not read earlier thread), the control is through the ratio of On and Off time.

    The funny thing is how OP jumped straight into the code.
     
  7. John P

    AAC Fanatic!

    Oct 14, 2008
    1,634
    224
    On discussion boards like this, most people who say "servo motor" are talking about a hobby servo, not an expensive motor with encoder feedback and all sorts of electronics. You can't stop them doing it, so you just have to check the context to be sure (fairly sure, at least) what kind of setup they're building.
     
  8. MaxHeadRoom

    Expert

    Jul 18, 2013
    10,542
    2,369
    It is a prevalent problem, seems to point to the need for an RC forum to be included?
    Max.
     
  9. shteii01

    AAC Fanatic!

    Feb 19, 2010
    3,387
    497
    Not really. The problem is that people do not present information in clear and organized manner. From what I have seen of OP thus far, I don't expect them to get far or get much help.

    And on top of everything, I am pretty sure OP is not a native English speaker. That might explain why their messages are so short and so devoid of detail. It is probably a struggle for them just to put together the messages that they did thus far.
     
Loading...