This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
en:examples:motor:servo [2010/02/25 11:38] – priitj | en:examples:motor:servo [2020/07/20 09:00] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== Servomotor ====== | ====== Servomotor ====== | ||
- | //Necessary knowledge: [HW] [[en: | + | //Necessary knowledge: |
+ | [HW] [[en: | ||
+ | [AVR] [[en:avr:timers]], [AVR] [[en: | ||
+ | [LIB] [[en: | ||
===== Theory ===== | ===== Theory ===== | ||
Line 9: | Line 12: | ||
[{{ : | [{{ : | ||
- | Servo motors are often used in radio-controlled | + | RC (//radio-controlled//) servo-motors |
- | many kinds of small robotics | + | |
- | An RC servo motor includes a built-in | + | |
- | (usually potentiometer), | + | |
- | an external pulse-width modulation (PWM) signal. If a signal | + | |
- | requirements, it usually “describes” a (target) position input for the servo drive | + | |
- | electronics. Servo motor electronics compare | + | |
- | position, trying | + | |
- | is a continuous square-wave signal, as depicted in figure. | + | |
- | RC (// | + | The controlling signal of servo motor is specific pulse with modulated signal |
- | The controlling signal of servo motor is specific pulse with modulated signal (PWM), where with of the pulse determines the position of the rotor. The period of the signal | + | Traditional RC servo motor is also known as analogue-servo motor. It is because in the last decade so called digital servo motors were becoming common. The difference between those two is that in analogue servo motor the motor is controlled by the same 50 Hz PWM input signal. In digital servo motor the motor is controlled by a microcontroller with much higher frequency signal. The input signal is the same in the digital servo motor but higher modulation frequency |
- | Traditional RC servo motor is also known as analogue-servo motor. It is because in the last decade so called digital servo motors were becoming common. The difference between those two is that is analogue servo motor the motor is controlled with the same 50 Hz PWM input signal. In digital servo motor the motor is controlled by a micro controller with much higher frequency signal. The input signal is the same in the digital servo motor but higher modulation frequency of the motor enables much more precise and faster position determining. | ||
- | |||
- | ~~CL~~ | ||
===== Practice ===== | ===== Practice ===== | ||
- | On the board of module of motors of the home-lab | + | On the board of module of motors of the HomeLab |
- | The timer 1 must be set up in PWM production mode, where the maximum value of the timer is determined with ICR register. With the maximum value changed in the program and in the pace divider | + | The timer must be set up in PWM production mode, where the maximum value of the timer is determined with ICR register. With the maximum value changed in the program and in the pace divider of the timer, the precise PWM frequency for controlling the servo motor can be determined. With the comparison register of the timer, lengths of both high semi periods of PWM signal can be determined. The timers have special comparing units which are monitoring the value of the counter and in case it remains equal with the value of the comparison register they change the output value of comparing units. The following is the program code of the servo motor control library of the HomeLab. For the purpose of functionality, |
<code c> | <code c> | ||
- | // | + | // The value of the timer (20 ms)for achieving the full period of PWM |
- | // Taimeri väärtus PWM täisperioodi | + | // F_CPU is the clock rate of the microcontroller which is divided with |
- | // F_CPU on mikrokontrolleri taktsagedus, | + | // 50 Hz and 8 |
- | // ja 50 hertsiga | + | |
- | // | + | |
#define PWM_PERIOD | #define PWM_PERIOD | ||
- | // | + | // Middle position of PWM servo (5 ms / 20 ms) |
- | // PWM servo keskasend | + | // Middle position is 15/ |
- | // Keskasend saadakse täisperioodist | + | |
- | // | + | |
#define PWM_MIDDLE_POS | #define PWM_MIDDLE_POS | ||
- | // | + | // Factor for converting the percents |
- | // Kordamistegur, | + | // +1 is added to ensure that semi periods would reach to the boundaries |
- | // +1 liidetakse selleks, et poolperiood kindlasti ulatuks | + | // of 1 ms and 2 ms or // a little over |
- | // piiridesse või siis natuke üle. | + | |
- | // | + | |
#define PWM_RATIO | #define PWM_RATIO | ||
- | // | + | // Set-up of the pins |
- | // Viikude seadistus | + | |
- | // | + | |
static pin servo_pins[2] = | static pin servo_pins[2] = | ||
{ | { | ||
Line 64: | Line 47: | ||
}; | }; | ||
- | // | + | // Preparing the servo motor for working |
- | // Määratud servomootori tööks valmistamine. | + | |
- | // | + | |
void servomotor_init(unsigned char index) | void servomotor_init(unsigned char index) | ||
{ | { | ||
- | // PWM signaali viik väljundiks | + | // The pin of PWM signal for output |
pin_setup_output(servo_pins[index]); | pin_setup_output(servo_pins[index]); | ||
- | // Taimer | + | // Setup of timer 1 |
- | // Taktijagur | + | // Prescaler = 8 |
- | // Kiire PWM režiim kus TOP = ICR | + | // Fast PWM mode, where TOP = ICR |
- | // OUTA ja OUTB võrdusel madalaks | + | // OUTA and OUTB to low in comparisson |
timer1_init_fast_pwm( | timer1_init_fast_pwm( | ||
TIMER1_PRESCALE_8, | TIMER1_PRESCALE_8, | ||
Line 83: | Line 64: | ||
TIMER1_FAST_PWM_OUTPUT_DISABLE); | TIMER1_FAST_PWM_OUTPUT_DISABLE); | ||
- | // Perioodi määramine maksimaalse väärtuse kaudu | + | // Determining the period by maximum value |
timer1_set_input_capture_value(PWM_PERIOD); | timer1_set_input_capture_value(PWM_PERIOD); | ||
} | } | ||
- | // | + | // Determining the position of the servo motor |
- | // Servomootori positsiooni määramine. | + | // The parameter of the position is from -100% to +100%. |
- | // Positsiooni parameeter on -100 kuni 100 protsenti. | + | |
- | // | + | |
void servomotor_position(unsigned char index, signed short position) | void servomotor_position(unsigned char index, signed short position) | ||
{ | { | ||
Line 108: | Line 87: | ||
</ | </ | ||
- | Näiteprogramm kasutab kirjeldatud kodulabori teegi funktsioone. Programmi alguses pannakse esimese servomootori __PWM signaali? generaator tööle__? | + | The example program uses described functions of the library of the HomeLab. In the beginning of the program the first servo motor’s PWM signal generator is started with the // |
<code c> | <code c> | ||
- | // | + | // Testing program of the motors module of the HomeLab kit |
- | // Kodulabori mootorite mooduli servomootori | + | |
- | // testprogramm. | + | |
- | // | + | |
#include < | #include < | ||
#include < | #include < | ||
- | // | + | // Main program |
- | // Põhiprogramm | + | |
- | // | + | |
int main(void) | int main(void) | ||
{ | { | ||
short position; | short position; | ||
- | // ADC seadistamine | + | // Set-up of the ADC |
adc_init(ADC_REF_AVCC, | adc_init(ADC_REF_AVCC, | ||
- | // Mootori seadistamine | + | // Set-up of the motor |
- | servomotor_init(0); | + | servomotor_init(1); |
- | // Lõputu tsükkel | + | // Endless loop |
- | while (true) | + | while (1) |
{ | { | ||
- | // Potentsiomeetrist positsiooni lugemine ja servo- | + | // Reading the position of the potentiometer and |
- | // mootori piirkonda teisendamine | + | // converting the range of |
- | position = ((short)adc_get_value(3) - (short)512) / (short)5; | + | // the servo motor |
+ | // For HomeLab II ADC must be read for the corresponding channel, | ||
+ | // and use the following formula: | ||
+ | // position = ((short)adc_get_value(3) - (short)512) / (short)5; | ||
+ | position = ((short)adc_get_value(15) / 10) - 102 ; | ||
- | // Servomootori positsiooni määramine | + | // Determining the position of the servo motor |
- | servomotor_position(0, position); | + | servomotor_position(1, position); |
} | } | ||
} | } | ||
</ | </ |