This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| en:examples:motor:dc_speed [2012/05/18 07:39] – raivo.sell | en:examples:motor:dc_speed [2020/07/20 09:00] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 12: | Line 12: | ||
| Controlling the speed of DC motor can be realized either by analogous or digital signaling. | Controlling the speed of DC motor can be realized either by analogous or digital signaling. | ||
| + | |||
| + | [{{ : | ||
| In general, motor speed is dependent of the applied voltage. When powering motor with its nominal voltage, motor runs at its nominal speed at no load condition. When reducing the voltage motor speed and torque also decreases. This kind of speed control can be called as analogous motor speed control. This can be realized for example with one transistor. | In general, motor speed is dependent of the applied voltage. When powering motor with its nominal voltage, motor runs at its nominal speed at no load condition. When reducing the voltage motor speed and torque also decreases. This kind of speed control can be called as analogous motor speed control. This can be realized for example with one transistor. | ||
| - | < | ||
| - | PWM juhtimisel on olulised eelised analoogjuhtimise ees. | ||
| - | 1) Digitaalne ehk juhitav otse arvuti väljundist ainult ühe biti abil (bitt on kas 0 või 1) | ||
| - | 2) Skeemid on efektiivsemad, | ||
| - | Juhtpinge | + | In robotics DC motors are in most cases controlled by microcontrollers and as microcontrollers are digital devices it is much easier to control motor speed also digitally. To do that, instead of keeping transistor open partly, transistors need to be closed and opened constantly using pulse width modulation |
| - | Q1 töötab suletud lülitina -> VCE ~ 0 V -> seega kogu pinge Vln jääb mootorile. | + | |
| - | Transistorile jääb võimsus: P = IxV (P = IC × VCE) | + | |
| - | Kui VCE -> 0 V, siis P = IC × 0 V = 0 W | + | By using digital contorol, i.e. PWM signal to control the transistor and by this motor speed, there are several advantages over the analogous control. Most important ones for microcontroller driven systems are that speed can be controlled only by one single digital output (no need for complicated digital-analogous converter) and control is more effective (power dissipation are minimized). |
| - | -> transistoril ei haju energiat, kui mootor töötab. | + | |
| - | Kui Vctrl -> 0 V, siis Q1 on kinni ja IC -> 0 A. | + | |
| - | Võimsus antud juhul P = 0 A × VCE = 0 W | + | |
| - | -> transistoril ei haju energiat ka siis, kui mootor ei tööta. | + | |
| - | Seega kogu skeem on väga efektiivne kui mootor töötab kogu aeg või | + | [{{ : |
| - | on kogu aeg väljas. | + | |
| - | Lülitamisel on transistoritel kaod lekkevoolude tõttu, kuna neid ei saa | + | |
| - | lülitada momentaalselt st. mida suurem on sagedus, seda suuremad on | + | |
| - | dissipatsioonikaod ja seda kuumemaks läheb transistor. | + | |
| + | Simplified control schematics is shown on the figure. | ||
| + | Control voltage Vc from microcontroller output pin turns on and off the transistor Q in approx. 20 kHz frequency. When Q is turned on (saturation), | ||
| + | We can calculate the power dissipated by the transistor by using formula P = I × V. Applying this to Q: | ||
| - | In robotics DC motors are in most cases controlled by microcontrollers and as microcontrollers are digital devices it is much easier to control motor speed also digitally. To do that, instead of keeping transistor open partly, transistors need to be closed | + | P = I × Vq, and if Vq = 0 also P = 0 W |
| - | Note: do not mix up RC PWM signal with ordinary PWM signals. | + | This means that almost no power is consumed by transistor when it is ON state. Similar situation is also when transistor is closed (OFF state). In this situation nearly no current |
| + | |||
| + | P = I × Vq, and if I = 0 also P = 0 W | ||
| + | |||
| + | As a conclusion, when transistor operates only on and off states the efficiency can be very high as nearly no power is consumed by transistor itself. Compared with linear (analogous) control of the transistor when half of the power can be consumed by transistor in case motor is operated at half speed. However in practice, digital control (PWM) is also not totally lossless as transistors cannot be turned on and off instantaneously. Therefore little dissipation occurs in every transistor and every switching, by cousing bigger dissipation when frequency is higher. | ||
| + | |||
| + | |||
| + | Note: do not mix up RC Servo PWM signal with ordinary PWM signals. | ||
| ===== Practice ===== | ===== Practice ===== | ||
| + | |||
| The Motor module of the HomeLab includes motor board and DC motor equipped with integrated gearbox and encoder. Motor board allows connecting up to four DC motors. The schemes and instructions for connection are found in the chapter “Motors module”. | The Motor module of the HomeLab includes motor board and DC motor equipped with integrated gearbox and encoder. Motor board allows connecting up to four DC motors. The schemes and instructions for connection are found in the chapter “Motors module”. | ||
| Every motor is connected to H-bridge which is controlled with two digital output pins of the microcontroller. Motor speed is controlled by timers which are generating software PWM signal for H-bridge. | Every motor is connected to H-bridge which is controlled with two digital output pins of the microcontroller. Motor speed is controlled by timers which are generating software PWM signal for H-bridge. | ||
| Line 48: | Line 47: | ||
| <code c> | <code c> | ||
| - | // | ||
| - | // The setup of the pins driving pins. | ||
| - | // | ||
| static pin dcmotor_pins[4][2] = | static pin dcmotor_pins[4][2] = | ||
| { | { | ||
| Line 59: | Line 55: | ||
| }; | }; | ||
| + | static int motorindex[4][2] = | ||
| + | { | ||
| + | { 0, 1 }, | ||
| + | { 2, 3 }, | ||
| + | { 4, 5 }, | ||
| + | { 6, 7 } | ||
| + | }; | ||
| - | // | ||
| // Initialize PWM for specified DC motor. | // Initialize PWM for specified DC motor. | ||
| - | // | ||
| void dcmotor_drive_pwm_init(unsigned char index, timer2_prescale prescaler) | void dcmotor_drive_pwm_init(unsigned char index, timer2_prescale prescaler) | ||
| { | { | ||
| Line 88: | Line 89: | ||
| sei(); | sei(); | ||
| } | } | ||
| - | |||
| void dcmotor_drive_pwm(unsigned char index, signed char direction, unsigned char speed) | void dcmotor_drive_pwm(unsigned char index, signed char direction, unsigned char speed) | ||
| { | { | ||
| - | static int motorindex[4][2] = | ||
| - | { | ||
| - | { 0, 1 }, | ||
| - | { 2, 3 }, | ||
| - | { 4, 5 }, | ||
| - | { 6, 7 } | ||
| - | }; | ||
| - | |||
| if(direction == -1) | if(direction == -1) | ||
| { | { | ||
| - | compbuff[motorindex[index,0]] = 0x00; | + | compbuff[motorindex[index][0]] = 0x00; |
| - | compbuff[motorindex[index,1]] = speed; | + | compbuff[motorindex[index][1]] = speed; |
| } | } | ||
| if(direction == 1) | if(direction == 1) | ||
| { | { | ||
| - | compbuff[motorindex[index,0]] = speed; | + | compbuff[motorindex[index][0]] = speed; |
| - | compbuff[motorindex[index,1]] = 0x00; | + | compbuff[motorindex[index][1]] = 0x00; |
| } | } | ||
| } | } | ||
| Line 121: | Line 113: | ||
| <code c> | <code c> | ||
| - | // | + | |
| - | // Desc.: DC motor speed control | + | |
| - | // Hardware: ATMega2561 Controller board, Motor board with DC motor | + | |
| - | // Author: Raivo Sell, 2012 | + | |
| - | // | + | |
| #include < | #include < | ||
| #include < | #include < | ||
| Line 131: | Line 119: | ||
| int main(void) | int main(void) | ||
| { | { | ||
| - | |||
| // DC motor 0 init with no prescaler | // DC motor 0 init with no prescaler | ||
| dcmotor_drive_pwm_init(0, | dcmotor_drive_pwm_init(0, | ||
| Line 137: | Line 124: | ||
| while(1) | while(1) | ||
| { | { | ||
| - | + | // DC motor drive with half of the nominal speed | |
| - | // DC motor drive with half of the nominal speed | + | |
| dcmotor_drive_pwm(0, | dcmotor_drive_pwm(0, | ||
| } | } | ||
| Line 147: | Line 133: | ||
| <code c> | <code c> | ||
| - | // | + | |
| - | // Desc.: DC motor speed control with potentiometer | + | |
| - | // Hardware: ATMega2561 Controller board, Motor board with DC motor, Sensor board | + | |
| - | // Author: Raivo Sell, 2012 | + | |
| - | // | + | |
| #include < | #include < | ||
| - | |||
| #include < | #include < | ||
| #include < | #include < | ||
| Line 174: | Line 155: | ||
| // DC motor drive with speed from potentiometer | // DC motor drive with speed from potentiometer | ||
| - | // As potentiometer has 10-bit output but DC motor drive function | + | // As potentiometer has 10-bit output but DC motor drive |
| - | // 8-bit input the adc output have to be converted | + | // function |
| - | // e.g dividing the output with 4, or shifting bit 2 position >>2 | + | // to 8-bit e.g dividing the output with 4, or shifting bit |
| + | // 2 position >>2 | ||
| dcmotor_drive_pwm(0, | dcmotor_drive_pwm(0, | ||
| } | } | ||
| } | } | ||
| </ | </ | ||