Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
en:examples:motor:servo [2010/02/25 11:38] priitjen: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:hardware:homelab:motor]], [HW] [[en:hardware:homelab:sensor]], [AVR] [[en:avr:io]], [LIB] [[en:software:homelab:library:module:motor]], [LIB] [[en:software:homelab:library:adc]]//+//Necessary knowledge:  
 +[HW] [[en:hardware:homelab:digi]], [HW] [[en:hardware:homelab:combo]],  
 +[AVR] [[en:avr:timers]], [AVR] [[en:avr:adc]],   
 +[LIB] [[en:software:homelab:library:module:motor]], [LIB] [[en:software:homelab:library:adc]]//
  
 ===== Theory ===== ===== Theory =====
Line 9: Line 12:
 [{{  :examples:motor:servo:motor_servo_signal_position.png?220|The relationship between width of the signal and position of Servo PWM.}}] [{{  :examples:motor:servo:motor_servo_signal_position.png?220|The relationship between width of the signal and position of Servo PWM.}}]
  
-Servo motors are often used in radio-controlled (RCmodels, and are very useful in +RC (//radio-controlled//servo-motors are very common actuator devices in robotics and model building. RC servo motors are consisting of small DC motor, reduction gear and control logic deviceUsually the rotor of the servo motor moves to certain position and tries to maintain that position. The position of the rotor depends of the control signal received by the servo motor. Depending on the type of the motor, the maximum revolving angle of the motor may differ. Servo motors that revolve constantly are rare. In this case, the control signal determines not the revolving angle but the speed of revolving. Servo motor “hack” is also quite common. This makes the position determining servo motor to constantly revolving servoIn this case the feedback potentiometer is replaced by two fixed resistors and the mechanical resistor that prevents full rotations is removed from the gear. A very important characteristic of servo motors is its power-weight ratio.
-many 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), and drive electronicsRC servo motors can be controlled by +
-an external pulse-width modulation (PWM) signal. If a signal meets RC servo timing +
-requirementsit usually “describes” a (target) position input for the servo drive +
-electronics. Servo motor electronics compare the shaft position with the inputted +
-position, trying to find a shaft position where they matchThe position control signal +
-is a continuous square-wave signal, as depicted in figure.+
  
-RC (//radio-controlled//servo-motors are very common actuator devices in robotics and model building. RC servo motors are consisting of a small DC motorreduction gear and control logic device. Usually the rotor of the servo motor moves to a certain position and tries to maintain that position. The position of the rotor depends of the control signal received by the servo motor. Depending on the type of the motor, the maximum revolving angle of the motor may differServo motors that revolve constantly are rare. In this case, the control signal determines not the revolving angle but the speed of revolving. Servo motor “hack” is also quite common. This makes the position determining servo motor to constantly revolving servoIn this case the feedback potentiometer is replaced by two fixed resistors and the mechanical resistor that prevents full rotations is removed from the gear. A very important characteristic of servo motors is its power-weight ratio+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 one1,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 with of the pulse determines the position of the rotor. The period of the signal is 20 ms (50 Hz) and the with of the high period is 1 ms – 2 ms1 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 microcontroller with much higher frequency signalThe 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.  
  
-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 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’s alternative functions are outputs of comparing units A and B of the timer 1. Timer is capable of producing PWM signal and due to that the control of motors is very simple in the program. Only difficulty is setup 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 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 (taktijagur?of the timer, the precise PWM frequency for controlling the servo motor con 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 if it remains equal with the value of the comparison register then they change the output value of comparing units. The following is the program code of the servo motor control library of the home-lab. For the purpose of functionality, it uses parameters for timers which are determined with macro functions. For example, the period is found using F_CPU constant, which marks the clock rate of the micro controller. When using macros, there is no need to calculate the parameters of timer for different clock rates and the compiler converts the operations with macros to constants anyway, so the program memory is not growing and does not demand more time.       +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, it uses parameters for timers which are determined with macro functions. For example, the period is found using F_CPU constant, which marks the clock rate of the microcontroller. When using macros, there is no need to calculate the parameters of timer for different clock rates and the compiler converts the operations with macros to constants anyway, so the program memory is not growing and does not demand more time. The following example of library is for HomeLab II (ATmega2561).
  
 <code c> <code c>
-// +// The value of the timer (20 ms)for achieving the full period of PWM 
-// Taimeri väärtus PWM täisperioodi (20 ms) saavutamiseks +// F_CPU is the clock rate of the microcontroller which is divided with  
-// F_CPU on mikrokontrolleri taktsagedus, mis jagatakse 8-ga +// 50 Hz and 8
-// ja 50 hertsiga +
-//+
 #define PWM_PERIOD      (F_CPU / 8 / 50) #define PWM_PERIOD      (F_CPU / 8 / 50)
    
-// +// Middle position of PWM servo (5 ms / 20 ms) 
-// PWM servo keskasend (1.5 ms / 20 ms) +// Middle position is 15/200 of full period
-// Keskasend saadakse täisperioodist 15/200 võtmisega. +
-//+
 #define PWM_MIDDLE_POS  (PWM_PERIOD * 15 / 200) #define PWM_MIDDLE_POS  (PWM_PERIOD * 15 / 200)
    
-// +// Factor for converting the percents (-100% to 100%)to periods 
-// Kordamistegur, et protsentidest (-100% kuni 100%) vajalik periood saada. +// +1 is added to ensure that semi periods would reach to the boundaries  
-// +1 liidetakse selleks, et poolperiood kindlasti ulatuks ja 2 ms +// of ms and 2 ms or // a little over
-// piiridesse või siis natuke üle. +
-//+
 #define PWM_RATIO       (PWM_PERIOD / 20 / 2 / 100 + 1) #define PWM_RATIO       (PWM_PERIOD / 20 / 2 / 100 + 1)
  
-// +// 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 seadistamine + // Setup of timer 
- // Taktijagur + // Prescaler = 
- // 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:
 </code> </code>
  
-Näiteprogramm kasutab kirjeldatud kodulabori teegi funktsiooneProgrammi alguses pannakse esimese servomootori __PWM signaali? generaator tööle__? //servomotor_init// funktsioonigaServomootori asendi väärtus saadakse analoog-digitaalmuunduri kanalist 3kuhu on andurite plaadil ühendatud potentsiomeeterEt servomootori juhtimiseks vajalikku -100 kuni 100 vahemikku saavutadalahutatakse ADC väärtusest pool maksimumi (ehk 512) ja jagatakse viiegaTulemuseks on küll ±102kuid pisike ebatäpsus ei loe, sest servomootorid ise ka erinevad PWM signaali ja pöördenurga suhteltLõplikult tuleb rakendustes PWM poolperioodi laius siiski katseja eksitusmeetodil määrataKa raadiojuhitavate mudelite pultidel on täpseks seadistuseks vastavad võimalusedProgrammi käivitades muutub vastavalt potentsiomeetri asendile ka servomootori rootori asend.+The example program uses described functions of the library of the HomeLabIn the beginning of the program the first servo motor’s PWM signal generator is started with the //servomotor_init// functionThe value of the position of the servo motor is obtained from the channel of the analogue-digital converterwhere a potentiometer on the board of sensors is connectedTo get the range -100 % - +100 % necessary for controlling the servo motorhalf of the maximum (512) is subtracted of the ADC value and the result is divided with 5 The result is +/- 102but small inaccuracy does not count because servo motors also differ by the relation of the PWM signal and revolving angleFinal PWM‘s semi period’s width in applications has to be determined using test-and-error methodAlso the remote controls of RC models have corresponding opportunities for precise setup (trim function)When the program is started the rotors position of the servomotor is changed according to the position of the potentiometer
  
 <code c> <code c>
-// +// Testing program of the motors module of the HomeLab kit
-// Kodulabori mootorite mooduli servomootori +
-// testprogramm. +
-//+
 #include <homelab/adc.h> #include <homelab/adc.h>
 #include <homelab/module/motors.h> #include <homelab/module/motors.h>
  
-// +// 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_PRESCALE_8);  adc_init(ADC_REF_AVCC, ADC_PRESCALE_8);
  
- // 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);
  }  }
 } }
 </code> </code>
en/examples/motor/servo.1267097938.txt.gz · Last modified: 2020/07/20 09:00 (external edit)
CC Attribution-Share Alike 4.0 International
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0