This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
en:examples:motor:servo [2010/03/21 19:09] – Helen | 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 8: | Line 11: | ||
[{{ : | [{{ : | ||
- | |||
- | Servo motors are often used in radio-controlled (RC) models, and are very useful in | ||
- | different kinds of small robotics applications because they are compact and inexpensive. | ||
- | An RC servo motor includes a built-in DC motor, gearbox, position feedback sensor | ||
- | (usually potentiometer), | ||
- | an external pulse-width modulation (PWM) signal. If a signal meets RC servo timing | ||
- | requirements, | ||
- | electronics. Servo motor electronics compare the shaft position with the inputted | ||
- | position, trying to find a shaft position where they match. The position control signal | ||
- | is a continuous square-wave signal, as depicted in figure. | ||
RC (// | RC (// | ||
Line 23: | Line 16: | ||
The controlling signal of servo motor is specific pulse with modulated signal (PWM), where width of the pulse determines the position of the rotor. The period of the signal is 20 ms (50 Hz) and the width of the high period is 1 ms – 2 ms. 1 ms marks one extreme position and 2 ms marks the second one. 1,5 ms marks the middle position of the servo motor’s rotor. | The controlling signal of servo motor is specific pulse with modulated signal (PWM), where width of the pulse determines the position of the rotor. The period of the signal is 20 ms (50 Hz) and the width of the high period is 1 ms – 2 ms. 1 ms marks one extreme position and 2 ms marks the second one. 1,5 ms marks the middle position of the servo motor’s rotor. | ||
- | 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 micro controller | + | 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 |
- | ~~CL~~ | ||
===== Practice ===== | ===== Practice ===== | ||
- | On the board of module of motors of the HomeLab are two plugs for connecting RC servo motors. The PWM ends of the plugs are connected to the PB5 and PB6 pins of the micro controller, which alternative functions are outputs of comparing units A and B of the timer 1. Timer 1 is capable of producing PWM signal and due to that the control of motors is very simple in the program. Only difficulty is set-up of the timer. | + | On the board of module of motors of the HomeLab are two or four plugs for connecting RC servo motors. The PWM ends of the plugs are connected to the pins of the microcontroller, which alternative functions are outputs of comparing units of the timer. Timer is capable of producing PWM signal and due to that the control of motors is very simple in the program. Only difficulty is set-up of the timer. |
- | 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 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, | + | 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 |
- | // The value of the timer (20 ms)for achieving the full period of PWM. | + | // F_CPU is the clock rate of the microcontroller |
- | // F_CPU is the clock rate of the micro controller | + | // 50 Hz and 8 |
- | // | + | |
- | // | + | |
#define PWM_PERIOD | #define PWM_PERIOD | ||
- | // | ||
// Middle position of PWM servo (5 ms / 20 ms) | // Middle position of PWM servo (5 ms / 20 ms) | ||
- | // Middle position is 15/200 of full period. | + | // Middle position is 15/200 of full period |
- | // | + | |
#define PWM_MIDDLE_POS | #define PWM_MIDDLE_POS | ||
- | // | + | // Factor for converting the percents (-100% to 100%)to periods |
- | // Factor for converting the percents (-100% to 100%)to periods. | + | // +1 is added to ensure that semi periods would reach to the boundaries |
- | // +1 is added to ensure that semi periods would reach to the boundaries of 1 ms and 2 ms or // a little over. | + | // of 1 ms and 2 ms or // a little over |
- | // | + | |
#define PWM_RATIO | #define PWM_RATIO | ||
- | // | + | // Set-up of the pins |
- | // Set-up of the pins. | + | |
- | // | + | |
static pin servo_pins[2] = | static pin servo_pins[2] = | ||
{ | { | ||
Line 63: | Line 47: | ||
}; | }; | ||
- | // | + | // Preparing the servo motor for working |
- | // Preparing the servo motor for working. | + | |
- | // | + | |
void servomotor_init(unsigned char index) | void servomotor_init(unsigned char index) | ||
{ | { | ||
- | // The pin of PWM signal for output. | + | // The pin of PWM signal for output |
pin_setup_output(servo_pins[index]); | pin_setup_output(servo_pins[index]); | ||
- | // Setup of timer 1. | + | // Setup of timer 1 |
- | // Pace divider (taktijagur? | + | // Prescaler = 8 |
// Fast PWM mode, where TOP = ICR | // Fast PWM mode, where TOP = ICR | ||
- | // OUTA and OUTB to low in comparisson. | + | // OUTA and OUTB to low in comparisson |
timer1_init_fast_pwm( | timer1_init_fast_pwm( | ||
TIMER1_PRESCALE_8, | TIMER1_PRESCALE_8, | ||
Line 82: | Line 64: | ||
TIMER1_FAST_PWM_OUTPUT_DISABLE); | TIMER1_FAST_PWM_OUTPUT_DISABLE); | ||
- | // Determining the period by maximum value. | + | // 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 |
- | // Determining the position of the servo motor. | + | |
// The parameter of the position is from -100% to +100%. | // The parameter of the position is from -100% to +100%. | ||
- | // | ||
void servomotor_position(unsigned char index, signed short position) | void servomotor_position(unsigned char index, signed short position) | ||
{ | { | ||
Line 107: | Line 87: | ||
</ | </ | ||
- | 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 // | + | 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 |
- | // Testing program of the motors module of the HomeLab kit. | + | |
- | // | + | |
#include < | #include < | ||
#include < | #include < | ||
- | // | + | // Main program |
- | // Main program. | + | |
- | // | + | |
int main(void) | int main(void) | ||
{ | { | ||
short position; | short position; | ||
- | // Set-up of the ADC. | + | // Set-up of the ADC |
adc_init(ADC_REF_AVCC, | adc_init(ADC_REF_AVCC, | ||
- | // Set-up of the motor. | + | // Set-up of the motor |
- | servomotor_init(0); | + | servomotor_init(1); |
- | // Endless loop. | + | // Endless loop |
- | while (true) | + | while (1) |
{ | { | ||
- | // Reading the position of the potentiometer and converting the range of | + | // Reading the position of the potentiometer and |
- | // the servo motor. | + | // 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 ; | ||
- | // Determining the position of the servo motor. | + | // Determining the position of the servo motor |
- | servomotor_position(0, position); | + | servomotor_position(1, position); |
} | } | ||
} | } | ||
</ | </ |