This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revision | |||
| en:iot-open:practical:hardware:itt:avr:dc [2025/09/02 14:21] – raivo.sell | en:iot-open:practical:hardware:itt:avr:dc [2025/09/02 14:27] (current) – raivo.sell | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | < | ||
| + | ====== DC motor ====== | ||
| + | ===== Theory ===== | ||
| + | |||
| + | [{{ : | ||
| + | |||
| + | Permanent magnet DC motors are very common in different applications, | ||
| + | |||
| + | 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, | ||
| + | |||
| + | ===== Practice ===== | ||
| + | |||
| + | 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 lower) 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, | ||
| + | |||
| + | ^ Input A ^ Input B ^ Output A ^ Output B ^ Result | ||
| + | | 0 | ||
| + | | 1 | ||
| + | | 1 | ||
| + | | 0 | ||
| + | |||
| + | 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. | ||
| + | |||
| + | <code c> | ||
| + | // 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. | ||
| + | |||
| + | <code c> | ||
| + | // Robotic HomeLab DC motor driving example program | ||
| + | #include < | ||
| + | |||
| + | // Main program | ||
| + | int main(void) | ||
| + | { | ||
| + | dcmotor_drive_pwm_init(1, | ||
| + | dcmotor_drive_pwm(2, | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | ==== Task to be implemented ==== | ||
| + | - Make a smooth start of the DC motor and after 10 seconds of rotating at maximum speed, slow down to a full stop. | ||