This is an old revision of the document!
Permanent magnet DC motors are very common in different applications, where small dimensions, high power, and low price are essential. Due to their fairly high speed, they are used together with a transmission (to output a lower speed and higher torque). Permanent magnet DC motors have a relatively simple construction, and their control is straightforward. Although controlling is easy, their speed is not precisely determined by the control signal because it depends on several factors, primarily on the torque applied to the shaft and the feeding current. The relationship between torque and speed of an ideal DC motor is linear, which means: the higher the load on the shaft, the lower the speed of the shaft, and the higher the current through the coil.
Brushed DC motors use DC voltage and basically do not need special control electronics because all necessary communication is done inside the motor. When the motor is operating, two static brushes are sliding on the revolving commutator and holding the voltage on the coils. The direction of rotation of the motor is determined by the polarity of the current. If the motor must revolve in only one direction, then the current may come through a relay or some other simple connection. If the motor has to revolve in both directions, then an electronic circuit called an H-bridge is used.
In the H-bridge are four transistors (or four groups) directing the current for driving the motor. The electrical scheme of the H-bridge is similar to the letter H, and that is where it gets its name. The peculiarity of the H-bridge is the possibility of applying both directional polarities to the motor. An H-bridge can also change the direction of rotation and the rotation speed of the motor. There also exist integrated H-bridges for conducting smaller currents, for higher currents, special power MOSFETs are used. The H-bridge with other electronics is called a motor controller or driver.
DC motors are controlled by microcontrollers, and because microcontrollers are digital devices, it is also reasonable to control the motors digitally. This is achieved by using pulse width modulation (PWM), by switching transistors quickly on and off. The total motor power is something in between standing and full speed. The time of the entire PWM period when the transistor is opened, called the duty cycle, is denoted by a percentage. 0% means that the transistor is constantly closed and does not conduct, 100% means that the transistor is open and conducts. The PWM frequency should be high enough to prevent vibration of the motor shaft. At low frequencies, the motor produces noise and is therefore used with a modulating frequency above 20 kHz. However, the transistors' efficiency is suffering from very high frequencies. Compared to the analog control, a digital control has a number of advantages. The main advantage of microcontroller-controlled systems is that they require only a single digital output, and there is no need for a complicated digital-to-analog converter. The digital control is also more efficient because less energy is converted into heat.
The HomLab uses a combined chip to drive DC motors, which includes two integrated H-bridges and circuit-breaking diodes. The motor is controlled with three digital signals, one of them is the operation enabling signal enable, and the other two are determining the state of the transistors in the H-bridge. It can never 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 the 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 are 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 an 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 the same value, then the motor is stopped; if they have different values, 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 |
|---|---|---|---|---|
| 0 | 0 | - | - | The motor is stopped |
| 1 | 1 | + | + | The motor is stopped |
| 1 | 0 | + | - | The motor revolves in direction 1 |
| 0 | 1 | - | + | The motor revolves in direction 2 |
Each motor that is connected to the H-bridge is operated by two of the digital outputs of the microcontroller. The motor speed is controlled by timers that generate a continuous PWM signal to the H-bridge, and the direction of rotation of the motor is controlled by the second terminal. Motor speed is controlled by relative values from 0 to 255, where zero means that the motor is standing and 255 is the maximum moving speed of the motor. The following code describes a function which are described in the HomeLab II library to control DC motors.
// The setup of the pins driving pins static pin dcmotor_pins[4][2] = { { PIN(B, 7), PIN(B, 4) }, { PIN(D, 1), PIN(D, 0) }, { PIN(D, 7), PIN(D, 6) }, { PIN(D, 5), PIN(D, 4) } }; static int motorindex[4][2] = { { 0, 1 }, { 2, 3 }, { 4, 5 }, { 6, 7 } }; // Initializing a PWM to the 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][1]); motor[index] = 1; pwm = PWMDEFAULT; // Starting all channels 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 the 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; } }
The controlling pins of four motor-controllers are determined with the array dcmotor_pins in the library. Before controlling the motors, the 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, which determines the frequency of the PWM signal.
The dcmotor_drive_pwm function is used for controlling motor speed. This function needs three input values: motor number, direction (-1, 0, +1), where -1 is the rotation in one direction, +1 is the other direction, and 0 is 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 the minimum and maximum 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 that controls the DC motor.
// Robotic HomeLab DC motor driving example program #include <homelab/module/motors.h> // Main program int main(void) { dcmotor_drive_pwm_init(1, TIMER2_NO_PRESCALE); dcmotor_drive_pwm(2, 1, 128); }