| Both sides previous revisionPrevious revisionNext revision | Previous revision |
| et:examples:motor:dc_speed [2013/04/29 08:17] – rellermaa | et:examples:motor:dc_speed [2020/07/20 09:00] (current) – external edit 127.0.0.1 |
|---|
| ====== DC motor speed ====== | <pagebreak> |
| | ====== Alalisvoolu mootori kiirus ====== |
| <note important>Tõlkida</note> | |
| |
| //Vajalikud teadmised: [HW] [[et:hardware:homelab:motor]], [AVR] [[et:avr:io]], [LIB] [[et:software:homelab:library:module:motor]], [LIB] [[et:software:homelab:library:delay]]// | //Vajalikud teadmised: [HW] [[et:hardware:homelab:motor]], [AVR] [[et:avr:io]], [LIB] [[et:software:homelab:library:module:motor]], [LIB] [[et:software:homelab:library:delay]]// |
| } | } |
| |
| // Timer 2 normaal resiimis, sea jagur | // Timer 2 normaal režiimis, sea jagur |
| timer2_init_normal(prescaler); | timer2_init_normal(prescaler); |
| // Timer 2 luba katkestused | // Timer 2 luba katkestused |
| </code> | </code> |
| |
| With the array dcmotor_pins in the library, the controlling pins of four motor-controllers are determined. Before controlling the motors and their speed, function dcmotor_drive_pwm_init with the number of the motor-controller (0 – 3) must be called out. It sets the pins as output. In addition prescaler - //timer2_prescale prescaler// have to be set as a second parameter. Prescaler sets PWM frequency and is important when using other timer based functions on the same time. If only motors are used TIMER2_NO_PRESCALE value is most suitable. If other timer based functions are used on the same time e.g. ultrasonic sensor TIMER2_PRESCALE_8 is recommended value. Bigger prescaler values are not suitable for DC motor speed control as vibration will be too high. However the same function can be used for example flashing the LED and in this case bigger prescaler values are important to consider. | Mootorite teegi maatriksis dcmotor_pins määratakse ära viigud, kuhu on ühendatud Mootoriplaadi 4 mootori juhtsignaalid. Enne mootori kiiruse juhtimist tuleb välja kutsuda funktsioon dcmotor_drive_pwm_init selle mootori kohta (0-3), mille kiirust on soov juhtima hakata. See seab vastavad viigud väljunditeks. Samuti tuleks seadistada Timer2 jagur //timer2_prescale//, mis määrab PWMi sageduse. Kui koodus ei kasutata rohkem taimeri peal jooksvaid süsteeme, siis on sobilik väärtus TIMER2_NO_PRESCALE. Kui aga kasutatakse näiteks ka ultraheliandurit vmt, siis tuleks valida TIMER2_PRESCALE_8, muidu võib protsessori jõudlust väheks jääda ja anduri tulemustesse võib tekkida viga. Suuremaid jaguri väärtuseid pole soovitatav kasutada, sest see muudab mootori pöörlemise katkendlikuks tekitades vibratsiooni. |
| |
| For controlling motor speed the function dcmotor_drive_pwm is included into library. With this function three parameters are needed. First is motor index (0-3), second is direction -negative direction parameter is used to give to the motor one revolving direction and other direction with positive parameter, and 0 if the motor is stopped. Third parameter is speed where the value can be from 0 to 255. The speed value is not any specific rpm, but is relative value and the actual speed is depending on the motor and power supply. The precision of motor speed control is 8-bit, meaning that smallest controlling value is 1/255 of full speed. | Mootorite kiiruse juhtimiseks kasutatakse funktsiooni dcmotor_drive_pwm. See funktsioon vajab kolme väärtust: esiteks mootori numbrit, mille kiirust reguleeritakse (0-3), teiseks suunda (-1, 0, +1), kus -1 tähistab pöörlemist ühes suunas, +1 teises ning 0 tähistab seismist, ning kolmandaks kiirust vahemikus 0 - 255. Kiiruse väärtus ei ole seotud mingi kindla pöörlemiskiirusega vaid suhteline väärtus mootori maksimaalse pöörlemiskiiruse ja seisva mootori vahel. Mootori reaalne pöörlemiskiirus sõltub mootorist, toitepingest ning koormusest. Mootori kiiruse täpsus on 8-bitine, mis tähendab, et minimaalne jutimisetäpsus on 1/255 mootori maksimaalsest kiirusest. |
| |
| The following is an example program which controls first DC motor (DC motor connector 0) so that motor speed is half of the full speed. The speed is modulated with PWM signal of controlling pin. | Järgnevalt on toodud väiteprogramm, mis paneb esimese mootori (ühendatud DC0 viikudele) pöörlema poolel kiirusel. Kiirust juhitakse PWM signaaliga. |
| |
| <code c> | <code c> |
| int main(void) | int main(void) |
| { | { |
| // DC motor 0 init with no prescaler | // Eelseadista DC0 mootor, ilma timer2 jagurita |
| dcmotor_drive_pwm_init(0, TIMER2_NO_PRESCALE); | dcmotor_drive_pwm_init(0, TIMER2_NO_PRESCALE); |
| | |
| while(1) | while(1) |
| { | { |
| // DC motor drive with half of the nominal speed | // Mootorile pool nominaalkiirusest. |
| dcmotor_drive_pwm(0, 1, 128); | dcmotor_drive_pwm(0, 1, 128); |
| } | } |
| </code> | </code> |
| |
| Another example program which controls first DC motor (DC motor connector 0) with potentiometer from Sensor module. | Teine näiteprogramm juhib esimest DC mootorit (ühendatud DC0 viikudele) potensiomeetriga, mis on ühendatud Andurite mooduli plaadile. |
| |
| <code c> | <code c> |
| int speed; | int speed; |
| |
| // Adjusting ADC | // Käivita analoog-digitaal muundir |
| adc_init(ADC_REF_AVCC, ADC_PRESCALE_8); | adc_init(ADC_REF_AVCC, ADC_PRESCALE_8); |
| |
| // DC motor 0 init with no prescaler | // Eelseadista DC0 mootor, ilma timer2 jagurita |
| dcmotor_drive_pwm_init(0, TIMER2_NO_PRESCALE); | dcmotor_drive_pwm_init(0, TIMER2_NO_PRESCALE); |
| |
| while(1) | while(1) |
| { | { |
| // Potentiometer is connected to channel 3 | // Potensiomeeter on ühendatud kanalisse 3 |
| // Average of 4 samples are acquired | // Võtame nelja lugemi keskmise väärtuse |
| speed = adc_get_average_value(3, 4); | speed = adc_get_average_value(3, 4); |
| |
| // DC motor drive with speed from potentiometer | // Loeme DC mootori kiiruse potensiomeetrist |
| // As potentiometer has 10-bit output but DC motor drive | // Kuna potensiomeetri väärtus on 10-bitine kuid DC mootori |
| // function 8-bit input the adc output have to be converted | // funktsioon on 8-bitine, tuleb adc väärtus muundada 8-bitiseks |
| // to 8-bit e.g dividing the output with 4, or shifting bit | // jagades adc väärtus neljaga või nihutades adc väärtust paremale |
| // 2 position >>2 | // 2 kohta >>2 |
| dcmotor_drive_pwm(0, 1, speed/4); | dcmotor_drive_pwm(0, 1, speed/4); |
| } | } |
| } | } |
| </code> | </code> |