A servo motor , also known as a servo motor, mainly consists of a housing, circuit board, motor, reduction gear, and potentiometer. Servos are primarily used in control systems that require continuous angle changes and maintenance, such as the arms and legs of humanoid robots, and the direction control of model cars and airplanes. Currently, mature servo motor products on the market are mainly from Japan, South Korea, and Taiwan. This paper, based on the fundamental principles of servo motor operation, uses the Atmega8L microcontroller as the control chip for the servo motor circuit board and conducts a series of experiments on servo motor control, achieving excellent experimental results.
Servo working principle
The control signal enters the signal modulation chip through the receiver's channel to obtain a DC bias voltage. Internally, it has a reference circuit that generates a reference signal with a period of 20ms and a width of 1.5ms. This reference signal is compared with the voltage of the potentiometer to obtain the voltage difference output. Finally, the positive or negative value of the voltage difference is output to the motor driver chip to determine the motor's forward and reverse rotation. When the motor speed is constant, the cascaded reduction gears drive the potentiometer to rotate, causing the voltage difference to reach zero, and the motor stops rotating.
Schematic diagram of servo motor drive circuit based on AVR microcontroller
1. The servo drive circuit board receives PWM signals from the host computer to control the motor.
The rotation angle range of a servo motor is typically 0 to 180 degrees. The rotation angle is usually controlled by pulse width modulation (PWM). A typical servo motor has three input lines (positive power, ground, and signal line). The PWM signal is input through the signal line. The host computer generates a square wave with a period of approximately 20ms as the input signal. The duty cycle of the square wave determines the rotation angle of the servo motor. (See Figure 1.)
Based on the above principles, the following experimental circuit was designed (Figure 2). The basic working principle of the servo motor was realized by connecting the Atmega8L microcontroller and the L298N motor-specific driver chip.
The AVR microcontroller is an 8-bit RISC microcontroller from Atmel Corporation. It features in-system programmable memory (Flash), electrically erasable programmable memory (EEPROM), random access memory (RAM), an analog-to-digital converter (A/D converter), numerous I/O ports, 16/8-bit timers, an RS-232 communication interface (UART), a two-wire serial interface (TWI), and many other functionalities. This article uses the commonly used AVR series product, the Atmel Mega8L, with a system clock frequency of 7.3728MHz using an external crystal oscillator and an operating voltage of 5V.
The L298 is a dual H-bridge high-voltage, high-current integrated circuit that can be used to drive relays, coils, DC motors, and stepper motors. The schematic is shown in Figure 3. Vss is connected to the logic control power supply, while Vs is the motor drive power supply. The IN1-IN4 input pins are standard TTL logic level signals, and the ENA/ENB pin is the enable control terminal. Originally, the IN1-IN4 inputs were used to control the opening and closing of the H-bridge, thus achieving forward and reverse rotation of the motor. The ENA and ENB enable control terminals are used to input PWM signals to achieve motor speed control. This paper uses one set of H-bridges from the L298N, with the ENA enable control terminal always on, and inputting IN1 and IN2 PWM signals to control the motor speed and direction.
2. Microcontroller initialization includes the initialization of I/O ports, timers, and A/D converters.
The PWM signal sent by the host computer is read through an I/O port of the Atmega8L. To read the high-level duration of the PWM signal, a counting method is used, employing the Atmega8L's T0 timer. T0 is an 8-bit timer with a frequency divider of 8, TCCR0=0x02. If the PWM signal read from PD0 is high, T0 starts counting, and the count value TCNTO of T0 counts from 0 to 255, generating a count overflow interrupt. An accumulator COUNT is set in the interrupt service routine, incrementing by 1 each time the interrupt service routine is entered. When the PWM signal read from PD0 is low, T0 stops counting, and the total PWM high-level duration is calculated as: INPUTPWM = (COUNT x 255 + TCNT0) / 921.6 (ms). If the PWM high-level duration read from PD0 is less than 1ms, the program processes it as INPUTPWM = 1ms; if it is greater than 2ms, then INPUTPWM == 2ms.
During the experiment, in order to avoid the first timing failing to start from the high level of the PWM signal, the time of the first high level of the PWM signal was ignored, and the timing started from the second high level read from the PD0 port.
The potentiometer voltage is read by A/D conversion (ADC). The Atmega8L provides a maximum A/D conversion accuracy of 10 bits, meaning the converted voltage value ranges from 0 to 1023. Based on this consideration, the PWM signal read by the PDO is converted into a voltage value target = (INPUTPWM-1) * 1023. This design helps to reduce the complex process of converting the PWM signal into the corresponding voltage value.
Two 16-bit PWM signals are generated using the T1 timer of the Atmega8L. The duty cycle of these signals determines the motor speed; a larger duty cycle results in a longer current duration and faster motor rotation, and vice versa. To match the maximum value of 1023 for the A/D converter and reduce computational complexity, the T1 timer uses phase and frequency correction PWM mode (ICR1 = 1023), with a comparison value OCR1 = (ADC - target). To control the motor's direction, if (ADC - target), the voltage value is calculated. As the motor rotates, the voltage value of the acquired potentiometer continuously approaches the target value, the value of OCR1 decreases, the duty cycle decreases, and the motor speed continuously decreases. Theoretically, when the ADC equals the target, the duty cycle is 0, the motor reaches the target position, and stops rotating. The motor control flowchart is shown in Figure 4.
3. PID control
Theoretically, when the motor reaches the target position, it should stop rotating, and no current should flow through it. However, a servo motor is a system that needs to maintain an angle, and the greater the holding force, the better—that is, the greater the torque of the servo motor, the better. Specifically, when the motor reaches the target position, it stops rotating, but at this point, even a slight external force rotating the motor will cause a current to flow through it in the opposite direction of the external force to maintain the angle; this current is called the stall current. Therefore, on the one hand, it is required that the current of the motor decreases as it reaches the target position, making it easier to stop; on the other hand, it is required that the motor have a large stall current in a small area deviating from the target position. PID (Proportional Integral Differential) regulation can effectively solve this contradiction.
PID control can effectively control a motor to quickly reach the target position without jitter. For a servo motor, the comparison value OCR1 of timer T1 mentioned above is simply given as the difference between the current motor position and the target position. After introducing PID control, this term is multiplied by a coefficient kp as the proportional term of OCR1; the difference between the motor position in the previous cycle and the motor position in this cycle is multiplied by a coefficient kd as the derivative term of OCR1. This term mainly increases the motor speed if the difference between the two motor positions is large; the average value of the difference between the motor position and the target position in each cycle is multiplied by a coefficient ki as the integral term of OCR1. This term dampens the motor to reduce jitter. These three terms are added together to obtain the value of OCR1, which is used as the comparison value of timer T1. The formula is as follows:
OCRl=kpX(ADC-target)+kiX((ADC-target)/n)+HkdX(adcvalpre-ADC)
Wherein, ADC is the current potentiometer value acquired, target is the voltage value after target position conversion, is the number of cycles, adcvalpre is the voltage value of the previous cycle, kp, ki, and kd are the selected parameters. Selecting appropriate parameters can ensure that the motor reaches the target position quickly and stably.
4. The servo drive board communicates with the host computer via TWI to control the servo.
TWI (Two-wire Serial Interface), as a communication interface of the Atmega8L, provides a data transfer rate of up to 400kHz. The TWI protocol allows system designers to interconnect 128 different devices using only two bidirectional transmission lines. These two lines are the clock (SCL) and the data (SDA). The main advantages of using TWI communication are the precise transmission of the servo motor's desired position and the convenient adjustment of kp, ki, and kd coefficients. This paper uses two Atmega8L microcontrollers for TWI communication. The PC communicates with one of the microcontrollers via RS-232, as shown in Figure 5.
The PC side uses a serial communication program written in VC6 to communicate with microcontroller A. Microcontroller A mainly processes the data transmitted via RS-232 and repackages it to send it to the servo control circuit board via TWI. This allows for convenient transmission of target position, kp, ki, kd, and other parameters via the PC's serial communication program during experiments, facilitating debugging.
This paper uses the mechanical components of the Futaba S3003 servo motor and the servo control circuit shown in Figure 4 to effectively control the motor to reach the target position and generate significant torque. As an experimental product, it achieved the expected results. The next step is to seek a better adjustment algorithm to more stably control the motor and generate even greater torque.