| Both sides previous revisionPrevious revisionNext revision | Previous revision |
| en:examples:motor:dc [2015/11/11 13:02] – heikopikner | en:examples:motor:dc [2020/07/20 09:00] (current) – external edit 127.0.0.1 |
|---|
| ~~PB~~ | <pagebreak> |
| ====== DC motor ====== | ====== DC motor ====== |
| |
| //Necessary knowledge: [HW] [[en:hardware:homelab:motor]], [AVR] [[en:avr:io]], [LIB] [[en:software:homelab:library:module:motor]], [LIB] [[en:software:homelab:library:delay]]// | //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 ===== |
| P = I * Vq, and when I = 0, then P = 0 W | P = I * Vq, and when I = 0, then P = 0 W |
| |
| The driver of DC motor in the HomeLab L293D includes 2 integrated H-bridges and circuit breaking diodes. The motor is controlled with three digital signals,one of them is operation enabling signal //enable// and the other two are determining the state of the transistors in the H-bridge. Never can occur that two vertical transistors are opened, because this would short-circuit the power source. This means that the driver is designed as foolproof and only option that can be chosen is which transistor (upper or bottom) of one side of the H-bridge (of “semi-bridge”) is opened. In other words the polarity is selected using two driving signals which is applied to the two ends of the coil of the motor. | In conclusion, we can say that if the transistor is a switch element on the scheme, then the system efficiency is high and the power used by transistors is low. Compared with a linear (analog) system, where the transistor consumes of the half-open state the same amount of power than the motor, it is a very big energy savings. In practice, there is no lossless system and in fact, the losses occur when the transistor switch one state to other. Therefore, higher losses are occurring when the transistors are switched at higher frequencies. |
| |
| ===== Practice ===== | ===== Practice ===== |
| |
| The board of the motors of the HomeLab allows connecting up to four DC motors. The schemes and instructions for connection are found in the chapter “Motors module”. Basically, for every motor there is a H-bridge which is controlled with two digital output pins of the microcontroller, because the //enable// pin is constantly high. If both controlling pins have same value, then the motor is stopped if different then it revolves in the corresponding direction. The state of the H-bridge is described in the following table: | The HomLab uses a combined ships to drive DC motors, which includes 2 integrated H-bridges and circuit breaking diodes. The motor is controlled with three digital signals, one of them is operation enabling signal //enable// and the other two are determining the state of the transistors in the H-bridge. Never can occur that two vertical transistors are opened, because this would short-circuit the power source. This means that the driver is designed as foolproof and only option that can be chosen is which transistor (upper or bottom) of one side of the H-bridge (of “semi-bridge”) is opened. In other words the polarity is selected using two driving signals which is applied to the two ends of the coil of the motor. |
| | |
| | The Combo Board of the HomeLab allows connecting up to four DC motors. Basically, for every motor there is a H-bridge which is controlled with two digital output pins of the microcontroller, because the enable pin is constantly high. If both controlling pins have same value, then the motor is stopped if different then it revolves in the corresponding direction. The state of the H-bridge is described in the following table: |
| |
| ^ Input A ^ Input B ^ Output A ^ Output B ^ Result ^ | ^ Input A ^ Input B ^ Output A ^ Output B ^ Result ^ |
| | 0 | 1 | - | + | The motor revolves in direction 2 | | | 0 | 1 | - | + | The motor revolves in direction 2 | |
| |
| DC motors can be controlled by manipulating directly corresponding driver pins with microcontroller. Though, special functions for driving the motor are in the library of the HomeLab. | For each motor that is connected to the H-bridge is operated by two of the digital output of the microcontroller. The motor speed is is controlled by timers that generate a continuous PWM signals to the H-bridge, the direction of rotation of the motor is controlled to the second terminal. Motor speed is controlled a relative values from 0 to 255, where 0 means that the motor is standing and 255 is the maximum moving speed of the motor. The following code describes a function’s, which are described in the HomeLab II (ATmega2561) library to control DC motors. |
| |
| <code c> | <code c> |
| // The setup of the pins driving pins. | // The setup of the pins driving pins |
| static pin dcmotor_pins[4][2] = | static pin dcmotor_pins[4][2] = |
| { | { |
| { PIN(D, 5), PIN(D, 4) } | { PIN(D, 5), PIN(D, 4) } |
| }; | }; |
| | static int motorindex[4][2] = |
| // Allowing the control of the chosen DC motor. | { |
| void dcmotor_init(unsigned char index) | { 0, 1 }, |
| { | { 2, 3 }, |
| | { 4, 5 }, |
| | { 6, 7 } |
| | }; |
| | // Initializing a PWM to chosen motor |
| | void dcmotor_drive_pwm_init(unsigned char index, timer2_prescale prescaler) |
| | { |
| | unsigned char i, pwm; |
| | |
| pin_setup_output(dcmotor_pins[index][0]); | pin_setup_output(dcmotor_pins[index][0]); |
| pin_setup_output(dcmotor_pins[index][1]); | pin_setup_output(dcmotor_pins[index][1]); |
| } | |
| | |
| // Determining the operation and the direction of the chosen DC motor. | motor[index] = 1; |
| void dcmotor_drive(unsigned char index, signed char direction) | pwm = PWMDEFAULT; |
| { | |
| pin_set_to(dcmotor_pins[index][0], direction < 0); | // Starting all channels |
| pin_set_to(dcmotor_pins[index][1], direction > 0); | for(i=0 ; i<CHMAX ; i++) |
| | { |
| | // PWM state variable initialization |
| | compare[i] = pwm; |
| | compbuff[i] = pwm; |
| | } |
| | |
| | // Starting Timer 2 to normal mode |
| | timer2_init_normal(prescaler); |
| | // Allow Timer 2 interrupt |
| | timer2_overflow_interrupt_enable(true); |
| | |
| | // Enable global interrupts |
| | sei(); |
| | } |
| | // Generating a PWM for chosen motor |
| | void dcmotor_drive_pwm(unsigned char index, signed char direction, |
| | unsigned char speed) |
| | { |
| | if(direction == -1) |
| | { |
| | compbuff[motorindex[index][0]] = 0x00; |
| | compbuff[motorindex[index][1]] = speed; |
| | } |
| | if(direction == 1) |
| | { |
| | compbuff[motorindex[index][0]] = speed; |
| | compbuff[motorindex[index][1]] = 0x00; |
| | } |
| } | } |
| </code> | </code> |
| |
| With the array //dcmotor_pins// in the library, the controlling pins of four motor-controllers are determined. Before controlling the motors, function //dcmotor_init// with the number of the motor-controller (0 – 3) must be called out. It sets the pins as output. For controlling is the function //dcmotor_drive//, with it the 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. | The controlling pins of four motor-controllers are determined with the array dcmotor_pins in the library. Before controlling the motors, function dcmotor_drive_pwm_init with the number of the motor-controller (0 – 3) must be called out. It sets the pins as output. It should also set the timer prescaler, for HomeLab II timer2_prescale and for HomeLab III timer_prescale, which determines the frequency of the PWM signal. In case of HomeLab II, as the program does not have functions which are using timer, it is appropriate for the value TIMER2_NO_PRESCALE. When for example an ultrasound sensor are used, then should be chosen TIMER2_PRESCALE 8, otherwise the controller performance may not be sufficient and the sensor readings may be corrupted. This is not applying in the HomeLab III. Higher values of the prescaler are not recommended, because it makes the motor rotation intermittent, and generates vibration. |
| | |
| | Function dcmotor_drive_pwm is for control motor speed. This function need three input values: motor number, direction (-1, 0, +1), where -1 is the rotation in one direction, +1 other direction and 0 for stop and thirdly, the speed range of 0-255. The speed value is not linked to a specific rotational speed, it is the relative value between minimal and maximal motor speed. Motor actual speed depends on the motor type, load and the supply voltage. Motor speed accuracy is 8-bits, which means that the minimum control accuracy is 1/255 of the maximum engine speed. |
| |
| The following is an example program which controls first and second DC motor so that they alter their revolving direction after every second. The speed could be controlled if one controlling pin were modulated with PWM signal. | The following is an example program which controls first and second DC motor so that first motor rotates half of the speed and the second motor speed is controlled by a potentiometer. |
| |
| <code c> | <code c> |