Lens driving method and device based on single-chip microcomputer and related equipment
By adopting a composite control method of inverting PWM wave and inversion level in a microcontroller, combined with interrupt count feedback, the motor drive deviation problem when the timer output pin is occupied is solved, and accurate lens position control and stable Motor drive.
Patent Information
- Application Number
- CN202410958999.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-07-17
- Publication Date
- 2024-11-08
AI Technical Summary
In a microcontroller, when the timer output pin is occupied, it causes deviations when the motor drives the lens to move, making it difficult to accurately control the lens position, and adding an additional timer will increase power consumption and initialization error.
The composite control method of inverted PWM wave and inversion level is adopted, combined with the displacement feedback method of interrupt counting, and the precise control of the motor is achieved through the universal pin assistance of the microcontroller, reducing the deviation of the motor driving lens movement, and presetting it through the pulse counter Overflow and underflow parameters avoid count overflow.
It realizes that the motor steering and speed can be accurately controlled when the output pin of the microcontroller timer is occupied, reduces lens movement deviation, and improves the accuracy of lens position determination and the stability of motor driving.
Smart Images

Figure CN118920954A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of single-chip microcomputer driving technology, and in particular to a lens driving method, device and related equipment based on a single-chip microcomputer. Background Art
[0002] Lens movement is an indispensable function in modern cameras and optical systems. Whether it is to realize autofocus, zoom, anti-shake and other functions, it is necessary to accurately control the displacement of the lens. In related technologies, high-precision timers in microcontrollers are often used to output pulse width modulation (PWM) waves to achieve precise control of various motors such as DC motors, stepper motors, and brushless DC motors, and then the motor drives the lens to move to meet the precise movement requirements of the lens.
[0003] In actual use, single-chip microcomputers need to integrate multiple functions. At the same time, the output resources of single-chip microcomputers are limited. A motor is usually only connected to one timer. When the output pin of the timer outputting the PWM wave is occupied by other functional modules, the relevant technology often adopts the method of adding additional timers, that is, using additional timers to replace the occupied pins to control the operation of the motor. However, in practice, this method will not only increase the power consumption of the single-chip microcomputer due to the additional timer, but also the additional timer needs to reinitialize the output PWM wave to keep it synchronized with the output of the original timer. Therefore, if there is a certain error between the newly added timer and the original timer during initialization, it will directly affect the precise control of the motor, causing the motor to deviate when driving the lens to move, making it difficult for the subsequent single-chip microcomputer to determine the precise movement position of the lens. Summary of the invention
[0004] The present application provides a lens driving method, device and related equipment based on a single-chip microcomputer, which can reduce the deviation of the motor-driven lens movement when the output pin of the single-chip microcomputer timer is occupied, so that the single-chip microcomputer can accurately determine the lens movement position.
[0005] In a first aspect, the present application provides a lens driving method based on a single-chip microcomputer, which is applied to the single-chip microcomputer, wherein the single-chip microcomputer includes a timer and a pulse counter, wherein a first output pin and a second output pin of the timer are respectively connected to two poles of a motor for driving the lens, and the two poles of the motor are also respectively connected to two general pins of the single-chip microcomputer, and the motor is connected to the pulse counter via an encoder, and the method includes: When it is detected that the first output pin in the timer is occupied, respectively acquiring the first PWM wave of the first output pin before being occupied and the level state of the second output pin before being occupied; Inverting the first PWM wave to obtain a second PWM wave output by the second output pin; Determine a first universal pin of the two universal pins that is connected to the same pole of the motor as the first output pin, and perform level flipping on the level state of the second output pin to obtain an output voltage value of the first universal pin; Outputting the second PWM wave and the output voltage value to the motor via the second output pin and the first general pin respectively, so as to control the motor to drive the lens to move; The pulse signal generated by the motor when rotating and collected by the encoder is received, and the pulse signal is interrupted and counted using the overflow parameter and the underflow parameter preset in the pulse counter to determine the moving position of the lens driven by the motor.
[0006] By adopting the above technical solution, when the output pin of the single-chip timer is occupied, a composite control method of inverted PWM wave and inverted level is adopted to achieve accurate control of the motor direction and speed, and at the same time, combined with the displacement feedback method of interrupt counting, the actual movement displacement of the lens can be accurately tracked and calculated. Compared with the method of replacing the output pin with other timers in the related art, the motor can be accurately controlled through the existing hardware resources, thereby reducing the deviation of the motor-driven lens movement, so that the single-chip microcomputer can accurately determine the lens movement position.
[0007] Optionally, determining a first general pin of the two general pins that is connected to the same pole of the motor as the first output pin, and performing a level flip on a level state of the second output pin to obtain an output voltage value of the first general pin includes: Obtaining a voltage amplitude of a first PWM wave of the first output pin before being occupied, and determining a first general pin of the two general pins that is connected to the same pole of the motor as the first output pin; If the level state of the second output pin is a high level before being occupied, the output voltage value of the first general pin is determined to be a low level; If the level state of the second output pin is a low level before being occupied, the output voltage value of the first general pin is determined to be a high level equal to the voltage amplitude of the first PWM wave.
[0008] By adopting the above technical solution, the voltage amplitude parameter of the original first PWM wave is obtained, and the same high level value is restored on the first general pin according to the level state of the second output pin, so that the voltage difference between the two poles of the motor that is exactly the same as the first PWM wave can be accurately reconstructed. This precise voltage difference reconstruction measure ensures that when the output pin of the first high-precision timer is occupied, the motor can still achieve the same driving control capability as before through the assistance of the second general timer and the general pin, avoiding the reduction of control accuracy.
[0009] Optionally, the single chip microcomputer includes a plurality of timers, the timers are connected to the motors in a one-to-one correspondence, the receiving of the pulse signal generated when the motor rotates and collected by the encoder, using the overflow parameter and the underflow parameter preset in the pulse counter to interrupt the counting of the pulse signal, and determining the moving position of the lens driven by the motor, further comprising: Receiving the pulse signal generated by the encoder when the motor rotates, and determining the target motor that is currently in a rotating state; The pulse counter is configured to the transmission path of the pulse signal corresponding to the target motor, and the pulse signal is interrupted and counted using the overflow parameter and underflow parameter preset in the pulse counter to determine the moving position of the lens driven by the motor.
[0010] By adopting the above technical solution, the single-chip microcomputer integrates multiple timers and connects multiple motors respectively, realizing the hardware support of the multi-motor control system. When it is necessary to control the movement of a certain target motor, the pulse counter can be automatically switched to the pulse signal path of the motor to perform accurate displacement counting. This expands the control capability of the single-chip microcomputer, enabling it to manage the movement of multiple independent motors at the same time, and improves the adaptability and flexibility of the lens drive.
[0011] Optionally, the receiving of the pulse signal generated by the encoder when the motor rotates, and using the overflow parameter and the underflow parameter preset in the pulse counter to interrupt the counting of the pulse signal to determine the rotation position of the motor includes: Receiving the pulse signal of the motor collected by the encoder; When the current count value of the pulse signal reaches the overflow parameter preset in the pulse counter, the overflow interrupt state of the pulse counter is triggered and cleared, and an interrupt count value with a value plus one is obtained, and the rotation position of the motor is determined according to the interrupt count value; or, When the current count value of the pulse signal reaches the underflow parameter preset in the pulse counter, the underflow interrupt state of the pulse counter is triggered and cleared, and an interrupt count value minus one is obtained, and the rotation position of the motor is determined based on the interrupt count value.
[0012] By adopting the above technical solution, presetting overflow and underflow parameters, and using the interrupt counting method, the counting overflow problem caused by the limited number of bits of the microcontroller register can be effectively avoided, thereby greatly improving the accuracy and reliability of displacement calculation. At the same time, the overflow and underflow interrupts can respectively record the number of forward and reverse rotation cycles of the motor, thereby accurately determining the rotation direction, further enhancing the accuracy of position detection.
[0013] Optionally, the pulse signal includes a first pulse signal and a second pulse signal, the phase difference between the first pulse signal and the second pulse signal is 90°, the receiving of the pulse signal generated when the motor rotates and acquired by the encoder, using the overflow parameter and the underflow parameter preset in the pulse counter to interrupt the counting of the pulse signal, and before determining the rotation position of the motor, including: The mode of the pulse counter is set to a dual-channel orthogonal pulse counting mode, and the filter enable and timeout enable of the pulse counter are turned off; When a rising edge or a falling edge of the first pulse signal or the second pulse signal is detected, the pulse signal is read and recorded.
[0014] By adopting the above technical solution, using the two-way orthogonal pulse signal output by the encoder, through the dual-channel orthogonal counting mode of the internal counter of the single-chip microcomputer, the motor's motion state feedback such as direction and speed can be efficiently obtained. And by recording each pulse edge event detected, the phase difference between the two pulses can be accurately analyzed, so as to accurately determine the rotation direction of the motor.
[0015] Optionally, after receiving the pulse signal generated by the encoder when the motor rotates and collecting the pulse signal, interrupting and counting the pulse signal using the overflow parameter and the underflow parameter preset in the pulse counter, and determining the rotation position of the motor, the method further includes: Obtaining the measuring range of the motor and determining a test position within the measuring range; Controlling the motor to move to an initial position where the pulse signal collected by the encoder returns to zero, and determining the rotation parameters of the motor moving from the initial position to the test position; The inertia compensation parameters generated during the rotation of the motor and the actual position of the motor are obtained, and the rotation parameters are adjusted.
[0016] By adopting the above technical solution, setting the test position in advance and moving the motor to a known initial position, the initial error of the position count can be eliminated. At the same time, the solution combines multiple control methods such as inertia compensation and actual feedback correction, which can effectively suppress and eliminate the influence of factors such as inertia and load changes on the motor motion trajectory, thereby significantly improving the accuracy and stability of the motor-driven lens movement.
[0017] Optionally, before receiving the pulse signal generated by the encoder when the motor rotates, and using the overflow parameter and the underflow parameter preset in the pulse counter to interrupt and count the pulse signal, and determining the rotation position of the motor, the method further includes: Obtaining the resolution of the encoder and the acquisition accuracy required for the lens movement; The overflow parameter of the pulse counter is determined according to the resolution of the encoder and the acquisition accuracy required for the lens movement.
[0018] By adopting the above technical solution, combined with the two key parameters of encoder resolution and lens movement acquisition accuracy, the overflow parameter of the pulse counter can be reasonably and accurately determined so that it has a sufficient counting range to meet the displacement detection requirements of the lens throughout the entire stroke.
[0019] Optionally, the performing inversion processing on the first PWM wave to obtain a second PWM wave output by the second output pin includes: Obtaining a duty cycle of the first PWM wave; The first PWM wave is inverted according to the duty cycle of the first PWM wave to obtain a second PWM wave corresponding to the second output pin of the first timer.
[0020] By adopting the above technical solution, the key duty cycle parameters of the first PWM wave are obtained, and the second PWM wave with the opposite phase is generated accordingly. When the output pin of the first timer is occupied, it is possible to seamlessly switch to the second timer and continue to output a PWM wave that is completely equivalent to the previous one to drive the motor to operate, thereby achieving an uninterrupted transition of the motor control and ensuring the continuous accuracy of the motor-driven lens movement.
[0021] In a second aspect, the present application provides a lens driving device based on a single chip microcomputer, the device comprising: A signal acquisition module, for acquiring, when detecting that the first output pin in the timer is occupied, a first PWM wave of the first output pin before being occupied and a level state of the second output pin before being occupied; a PWM wave determination module, configured to perform inversion processing on the first PWM wave to obtain a second PWM wave outputted by the second output pin; an output voltage value determination module, used to determine a first universal pin of the two universal pins connected to the same pole of the motor as the first output pin, and to perform a level flip on the level state of the second output pin to obtain an output voltage value of the first universal pin; A motor driving module, used for outputting the second PWM wave and the output voltage value to the motor via the second output pin and the first general pin respectively, so as to control the motor to drive the lens to move; The position determination module is used to receive the pulse signal generated by the motor when the encoder rotates, and use the overflow parameter and underflow parameter preset in the pulse counter to interrupt the pulse signal and determine the moving position of the lens driven by the motor.
[0022] In a third aspect, the present application provides a computer storage medium, wherein the computer storage medium stores a plurality of instructions, wherein the instructions are suitable for being loaded by a processor and executing any one of the above methods.
[0023] In a fourth aspect, the present application provides an electronic device, comprising a processor, a memory and a transceiver, wherein the memory is used to store instructions, the transceiver is used to communicate with other devices, and the processor is used to execute the instructions stored in the memory so that the electronic device performs any one of the methods described above.
[0024] In summary, the beneficial effects brought about by the technical solution of this application include: When the output pin of the single-chip timer is occupied, the composite control method of the inverted PWM wave and the inverted level is adopted to realize the precise control of the motor direction and speed. At the same time, the displacement feedback method of the interrupt count can accurately track and calculate the actual movement displacement of the lens. Compared with the method of replacing the output pin with other timers in the related art, the motor can be accurately controlled through the existing hardware resources, thereby reducing the deviation of the motor-driven lens movement, so that the single-chip microcomputer can accurately determine the lens movement position. BRIEF DESCRIPTION OF THE DRAWINGS
[0025] Figure 1 It is a flowchart of a lens driving method based on a single chip microcomputer according to an embodiment of the present application; Figure 2 is a structural schematic diagram of a lens driving device based on a single chip microcomputer according to an embodiment of the present application; Figure 3 It is a structural schematic diagram of an electronic device provided in an embodiment of the present application.
[0026] Description of reference numerals: 300, electronic device; 301, processor; 302, communication bus; 303, user interface; 304, network interface; 305, memory. DETAILED DESCRIPTION
[0027] In order to enable those skilled in the art to better understand the technical solutions in this specification, the technical solutions in the embodiments of this specification will be clearly and completely described below in conjunction with the drawings in the embodiments of this specification. Obviously, the described embodiments are only part of the embodiments of this application, not all of the embodiments.
[0028] In the description of the embodiments of the present application, words such as "illustrative", "for example" or "for example" are used to indicate examples, illustrations or descriptions. Any embodiment or design described as "illustrative", "for example" or "for example" in the embodiments of the present application should not be interpreted as being more preferred or more advantageous than other embodiments or designs. Specifically, the use of words such as "illustrative", "for example" or "for example" is intended to present related concepts in a specific way.
[0029] In the description of the embodiments of the present application, the meaning of the term "multiple" refers to two or more. For example, multiple systems refer to two or more systems, and multiple screen terminals refer to two or more screen terminals. In addition, the terms "first" and "second" are used for descriptive purposes only and cannot be understood as indicating or implying relative importance or implicitly indicating the indicated technical features. Thus, the features defined as "first" and "second" may explicitly or implicitly include one or more of the features. The terms "include", "comprise", "have" and their variations all mean "including but not limited to", unless otherwise specifically emphasized.
[0030] First, the connection relationship and architecture in the implementation scenario of this application are briefly described.
[0031] The executor of the present application is a single-chip microcomputer for controlling the rotation of a motor, wherein the single-chip microcomputer includes a timer and a pulse counter, wherein a first output pin and a second output pin of the timer are respectively connected to two poles of a motor for driving a lens, and the two poles of the motor are also respectively connected to two common pins of the single-chip microcomputer, and the motor is connected to the pulse counter via an encoder.
[0032] A single-chip microcomputer generally refers to a microcomputer circuit that integrates multiple functions such as a CPU, memory (RAM and ROM), counter / timer, serial interface, parallel interface, clock circuit, watchdog circuit, etc. on a silicon chip. A single-chip microcomputer generally includes multiple peripheral function modules to perform corresponding tasks or realize corresponding functions. In the embodiment of the present application, the main function of the single-chip microcomputer is to control the rotation of the motor through the PWM wave output of the internal timer, and to detect the movement position of the motor and the lens by capturing the pulse signal output by the encoder through the pulse capture counter, so as to accurately drive and position the lens.
[0033] The timer can be understood as a hardware timing unit integrated inside the microcontroller, which can time according to the system clock and generate various forms of timing output signals, such as PWM waveforms, etc. In the embodiment of the present application, the timer is used to generate PWM waveform outputs with different duty cycles to drive the forward and reverse rotation and speed of the lens motor.
[0034] The pulse counter can be understood as a hardware counting unit inside the microcontroller, which can capture and count the pulse signals from the encoder output related to the rotation of the motor that drives the lens movement. The main function of the pulse counter is to capture the encoder output pulse signal, trigger an interrupt when the threshold is reached according to the preset overflow and underflow interrupt thresholds, and thus determine the accumulated rotation position of the motor and the movement position of the lens.
[0035] The output pin of the timer refers to the pin of the timer on the single-chip microcomputer for outputting signals. The main purpose of the timer output pin in this embodiment is to output a PWM waveform signal for controlling the forward and reverse motion and speed of the motor driving the lens movement.
[0036] The general pins of the microcontroller refer to general type pins on the microcontroller that can be programmably configured as input or output. In the embodiment of the present application, the general pins can output corresponding DC levels to the motor under the control of the microcontroller to continue to drive the motor to rotate precisely when the output pins of the timer are occupied.
[0037] In order to understand the technical solution of the present application in detail, the principle of controlling the motor by PWM wave in the embodiment of the present application is briefly described below. The motor in the embodiment of the present application is a DC motor, and the specific motor types include but are not limited to DC motors, stepper motors, and brushless DC motors. In practice, all motors that can be controlled by PWM waves should be covered within the protection scope of the embodiment of the present application.
[0038] PWM waves control the speed and direction of the motor by simulating the average voltage of the motor's power supply. The direction of the DC motor is determined by the direction of the current. By changing the polarity of the PWM wave (that is, changing the direction of the current), the direction of the motor's rotation can be changed. When using PWM to control a DC motor, the average voltage across the motor can be changed by adjusting the PWM duty cycle. A higher duty cycle means a higher voltage across the motor and a faster motor speed; a lower duty cycle means a lower voltage across the motor and a slower motor speed.
[0039] See also Figure 1 , is a flow chart of a lens driving method based on a single-chip microcomputer provided in an embodiment of the present application. The method can be implemented by a computer program, can be implemented by a single-chip microcomputer, or can be run on a lens driving device based on a single-chip microcomputer based on a von Neumann system. The computer program can be integrated into an application or run as an independent tool application. The specific steps of the lens driving method based on a single-chip microcomputer are described in detail below.
[0040] S101: When it is detected that a first output pin in a timer is occupied, a first PWM wave of the first output pin before being occupied and a level state of the second output pin before being occupied are respectively acquired.
[0041] The first output pin can be understood as an output pin of the internal timer of the single-chip microcomputer, which is used to output a PWM waveform signal. Under normal circumstances, a potential difference is formed between the PWM waveform signal output by the first output pin and the other pole of the motor, thereby realizing the forward and reverse rotation of the motor and speed control.
[0042] The first PWM wave refers to the PWM waveform signal for driving the lens motor output from the first output pin before the first output pin is occupied by other functions of the single-chip microcomputer. By obtaining the relevant parameters (such as duty cycle) of the first PWM wave before it is occupied, the output waveform of the driving motor can be determined according to the first PWM wave when the first output pin is occupied later, thereby achieving a smooth transition for the motor to continue to operate.
[0043] The second output pin refers to another PWM waveform output pin of the single-chip microcomputer timer in addition to the first output pin. In the embodiment of the present application, the first output pin and the second output pin alternately output PWM waves to control the forward and reverse rotation of the motor respectively. The second output pin here is an output pin that does not output PWM waves but outputs a fixed level when the pin is occupied.
[0044] The single-chip microcomputer can continuously monitor the output status of each output pin of the timer through the monitoring program. When it is detected that the first output pin that is outputting the PWM wave is occupied by other functions and cannot output the predetermined first PWM wave to the motor, the monitoring program will immediately trigger the acquisition operation. At this time, the single-chip microcomputer can obtain the relevant parameters of the first PWM wave output by the first output pin before being occupied to drive the lens motor through the acquisition program, such as its duty cycle, frequency, amplitude and other information. At the same time, the acquisition program will also read the level state of the second output pin at this time, and record whether the level state is high or low.
[0045] Through the above operation, the single chip microcomputer can maintain real-time monitoring of the timer of the driving motor, and promptly discover the occupancy fault when the output of the first output pin is abnormal, so that the occupancy fault can be resolved as soon as possible to ensure that the motor driving the lens can run continuously.
[0046] S102: Invert the first PWM wave to obtain a second PWM wave output by the second output pin.
[0047] The second PWM wave refers to a PWM waveform signal generated by inverting the first PWM wave after the first output pin is occupied and cannot output the first PWM wave. It should be understood that the first output pin and the second output pin of the timer are respectively connected to the two poles of the motor driving the lens. When the first output pin fails, the second PWM wave output by the second output pin belongs to the same timer as the first output pin, and the second PWM wave is obtained by inverting the first PWM wave. Therefore, the first PWM wave and the second PWM wave have almost the same properties in terms of waveform-related parameters such as frequency, amplitude, and accuracy, so that the accuracy of the motor rotation amount can be guaranteed when the movement position of the lens is determined by the motor rotation amount in the future.
[0048] In an optional implementation, the duty cycle of the first PWM wave is obtained; the first PWM wave is inverted according to the duty cycle of the first PWM wave to obtain a second PWM wave corresponding to the second output pin of the first timer.
[0049] Specifically, the higher the duty cycle of the PWM wave, the higher the effective voltage between the two poles of the motor, the greater the driving force obtained by the motor, and the faster the speed. Conversely, when the duty cycle decreases, the effective voltage decreases, the motor will be subject to a smaller driving force, and the speed will also slow down accordingly. Obtaining the duty cycle of the first PWM wave is the basis for inverting the PWM waveform. The duty cycle of the PWM wave refers to the proportion of the time that the waveform maintains a high level in one cycle to the entire cycle. The specific method of inverting is to change the original high level part to a low level and the original low level part to a high level in one cycle. In this way, the high and low level distribution of the newly generated second PWM wave will be exactly opposite to the original first PWM wave. After the second PWM wave is output to the second output pin, one end is the second PWM wave on the second output pin, and the other end is the high level of the first PWM wave reproduced by the first general pin. Since the high and low level distributions of the two waveforms are exactly opposite, at any time, their voltage difference will be equal to the voltage amplitude of the first PWM wave.
[0050] S103: Determine a first universal pin of the two universal pins that is connected to the same pole of the motor as the first output pin, and perform a level flip on the level state of the second output pin to obtain an output voltage value of the first universal pin.
[0051] Among them, the first universal pin refers to the universal pin connected to the same pole of the motor as the first output pin. When it is detected that the first output pin cannot output the predetermined first PWM wave, the first universal pin can output the corresponding voltage value under the control of the single-chip microcomputer to cooperate with the second output pin to continue to drive the motor to run. It should be understood that in the embodiment of the present application, since the two poles of a motor are respectively connected to the two output pins of the timer and the two universal pins of the single-chip microcomputer, when one of the output pins is occupied, maintaining the continued operation of the motor is achieved through the first universal pin and the second output pin, and subsequent other operating modes of the motor (such as switching forward and reverse, acceleration and deceleration of the motor, etc.) can still be achieved through the first universal pin and the second output pin.
[0052] The output voltage value of the first general pin refers to the voltage value set by the microcontroller to drive one end of the lens motor by controlling the output of the first general pin according to the level state of the second output pin when the first output pin is occupied and the first PWM wave cannot be output normally.
[0053] Since the hardware connection relationship between the single-chip microcomputer and the motor is known, when it is detected that the first output pin of the timer is occupied, the first general pin connected to the same pole of the motor as the first output pin is directly determined according to the wiring method of the single-chip microcomputer and the motor. The level state of the second output pin before being occupied is obtained, and the level state of the second output pin is flipped to obtain the output voltage value of the first general pin. That is, if the level state of the second output pin before being occupied is a high level, the output voltage value of the first general pin is determined to be a low level; conversely, if the level state of the second output pin before being occupied is a low level, the output voltage value of the first general pin is determined to be a high level.
[0054] S104: outputting the second PWM wave and the output voltage value to the motor via the second output pin and the first general pin respectively, so as to control the motor to drive the lens to move.
[0055] After the previous steps, the second PWM wave and the output voltage value of the first general pin have been obtained. Next, they need to be output to the two poles of the motor through the second output pin and the first general pin respectively to control the motor to drive the lens to move.
[0056] The second PWM wave obtained by the inverted processing is output to one pole of the motor connected to it through the second output pin; at the same time, the output voltage value obtained by level flipping the level state of the second output pin is output to the other pole of the motor connected to it through the first general pin. The two poles of the motor are controlled by the second PWM wave and the flip level at the same time. The second PWM wave determines the PWM drive of the motor and controls the speed of the motor; and the flip level determines the direction of the motor, that is, forward and reverse rotation. When the timing output pin is occupied, the limited hardware resources of the microcontroller may not be able to realize the synchronous control of PWM and level alone. However, through this composite control method of inverted PWM and reverse level, the PWM wave and level are cleverly applied to the two poles of the motor synchronously. Therefore, the complete function of PWM control of the direction and speed of the motor can still be realized under the condition of limited hardware resources, without reconfiguring a new timer or interrupting the occupation of the first output pin, so that the running state of the motor can be accurately controlled.
[0057] S105: receiving a pulse signal generated by the encoder when the motor rotates, and using overflow parameters and underflow parameters preset in the pulse counter to interrupt and count the pulse signal to determine the moving position of the motor-driven lens.
[0058] Among them, the encoder is a sensor that converts motion information into digital signal output. It is usually composed of components such as a rotating shaft, a photoelectric element and a coding disk. When the coding disk rotates with the shaft, the photoelectric element detects the periodic interference of the coding disk and converts the motion information into a pulse sequence output. In the embodiment of the present application, the encoder can be understood as an incremental rotary encoder connected to the rotating shaft of the motor. It can accurately convert motion information such as the rotational position and speed of the motor rotor into a series of pulse signal outputs. Parameters such as the number and frequency of these pulse signals reflect the actual motion state of the motor.
[0059] The encoder collects two orthogonal pulse sequences output when the motor rotates. The high and low level changes, number of pulses, frequency and other parameters of these two pulse sequences reflect the actual speed, direction, displacement and other motion information of the motor.
[0060] The overflow parameter refers to a maximum count value set when the pulse counter is working. When the count value increases to this maximum value, a count overflow occurs, and the counter or timer will start counting again from the beginning. In the embodiment of the present application, the overflow parameter can be understood as an overflow critical value pre-set in the pulse counter of the single-chip microcomputer. When the pulse counter counts the pulse signal output by the encoder, once the count value reaches this overflow critical value, an overflow interrupt will be triggered, and the interrupt state will be cleared, and the count value will start to accumulate again from 0. By establishing a mapping relationship between the overflow interrupt count value and the actual rotation amount of the motor, the cumulative displacement of the motor is calculated according to the number of interruptions, and then the moving position of the lens is determined.
[0061] The underflow parameter refers to a minimum count value set when the counter or timer is working. When the count value decreases to this minimum value, a count underflow occurs, and the counter or timer will start counting again from the maximum value. In the embodiment of the present application, the underflow parameter can be understood as an underflow critical value pre-set in the pulse counter of the single-chip microcomputer. When the pulse counter counts the pulse signal output by the encoder, once the count value decreases to this underflow critical value, an underflow interrupt will be triggered, and the interrupt state will be cleared, and the count value will be reset to the maximum value to continue counting. The underflow parameter sets a reasonable lower count limit for the pulse counter, so that it can trigger an interrupt in a timely manner during the counting process, and accurately calculate and update the forward and reverse motion displacement of the motor based on the number of interrupts.
[0062] In the embodiment of the present application, it is necessary to receive the pulse signal output by the encoder when collecting the rotational motion of the motor, and use the pulse counter of the single-chip microcomputer to count these pulse signals. The reason for adopting this interrupt-based displacement counting method is that the number of bits of the internal register of the single-chip microcomputer is limited, and there is a risk of count overflow if it relies on simple counting. By setting the overflow and underflow parameters, actively triggering the interrupt when the count reaches the critical value and performing cumulative count addition or subtraction, the count overflow can be effectively avoided, and the accuracy and reliability of displacement calculation can be greatly improved.
[0063] In order to accurately calculate the forward and reverse displacement of the motor, overflow parameters and underflow parameters are set in the pulse counter in advance. The pulse signal output by the encoder is connected to the pulse counter input of the single-chip microcomputer. During the counting process, once the count value increases and reaches the preset overflow parameter value, an overflow interrupt will be triggered; and when the count value decreases and reaches the underflow parameter, an underflow interrupt will be triggered. The functions of the overflow interrupt and the underflow interrupt are to clear and update the interrupt status in time, and correspond to the forward and reverse displacement of the motor respectively. Whenever an overflow interrupt occurs, the forward displacement count of the motor is accumulated once; whenever an underflow interrupt occurs, the forward displacement count of the motor is accumulated once. Through this interrupt counting method, the accumulated displacement of the motor at any time can be accurately obtained. Combined with the coupling transmission relationship between the motor and the lens, the actual moving position of the lens can be accurately calculated.
[0064] The above embodiment roughly describes the adjustment when the first output pin is occupied and the interrupt counting by the pulse counter. Based on the above embodiment, the specific adjustment method for determining the first output pin in S103 will be described in detail below.
[0065] In an optional implementation, the output voltage value of the first universal pin may be determined by the following steps, where step S102 further includes steps S201 - S203 .
[0066] S201: Obtain the voltage amplitude of the first PWM wave of the first output pin before being occupied, and determine a first common pin of the two common pins that is connected to the same pole of the motor as the first output pin.
[0067] The voltage amplitude of the first PWM wave of the first output pin before being occupied is obtained, and the first common pin of the two common pins connected to the same pole of the motor as the first output pin is determined, in order to establish a voltage difference between the two poles of the motor that is exactly the same as the original first PWM wave, thereby achieving precise rotation control of the motor.
[0068] Since the first output pin of the timer is occupied, the first PWM wave cannot be directly applied to one pole of the motor. Therefore, the second output pin needs to be used to output the inverted second PWM wave to apply to the other pole of the motor. However, the second PWM wave alone cannot drive the motor to rotate. The first general pin needs to output a high level or a low level at one pole of the motor to establish the correct voltage difference between the two poles of the motor.
[0069] In the specific implementation, it is necessary to obtain the voltage amplitude of the first PWM wave output by the first output pin before it is occupied, that is, the specific voltage value corresponding to the high level. Then, it is necessary to determine which of the two general pins is connected to the same pole of the motor as the first output pin, as the first general pin. With the high-level voltage value and the determination of the first general pin, it is possible to accurately reproduce the voltage output value with the same voltage amplitude as the first PWM wave when the first general pin needs to output a high level.
[0070] S202: If the level state of the second output pin before being occupied is a high level, determine the output voltage value of the first general pin to be a low level.
[0071] Since the second output pin will output the inverted second PWM wave, its original level state is critical. If the original level of the second output pin is high, then the low level of the inverted second PWM wave corresponds to the high level voltage value of the first PWM wave. In order to maintain this high level voltage value at the other pole of the motor so that the voltage difference between the two poles is consistent with the first PWM wave, the output of the first general pin needs to be set to a low level.
[0072] S203: If the level state of the second output pin is a low level before being occupied, determine the output voltage value of the first general pin to be a high level equal to the voltage amplitude of the first PWM wave.
[0073] After obtaining the voltage amplitude of the first PWM wave of the first output pin and determining the first general pin connected to the same pole of the motor, it is necessary to further reproduce the high-level voltage value of the first PWM wave on the first general pin. The second output pin will output the inverted second PWM wave.
[0074] Although the first output pin of the timer is occupied and cannot directly output the first PWM wave, through this method of obtaining the voltage amplitude, inverting the PWM wave, and setting the output value of the general pin, it is still possible to establish a voltage difference between the two poles of the motor that is exactly the same as the first PWM wave, thereby achieving precise control of the motor's rotation direction and state.
[0075] As an optional implementation, in order to achieve the precise displacement of the lens, there may be multiple motors that are driven in sections in practice, so step S105 may also include the following steps: S301: receiving a pulse signal generated by the encoder when the motor rotates, and determining the target motor that is currently in a rotating state; When a motor needs to be controlled to move (such as driving a lens assembly to adjust the focal length, etc.), the motor is determined as a target motor.
[0076] Specifically, the single chip microcomputer includes a plurality of timers, and the timers are connected to the motors in a one-to-one correspondence.
[0077] It should be understood that at the same time, the microcontroller will only control one of the motors to operate, and will not control multiple motors to operate synchronously at the same time.
[0078] Therefore, during the implementation process, it is necessary to first determine which motor needs to be run at present and determine the target motor. Then set the timer connected to the target motor to the working state, and output PWM waves to control the operation of the motor. At the same time, other timers will be temporarily in an idle state, not outputting PWM waves, and other motors connected to them will also stop running temporarily. When it is necessary to switch to control other motors, other timers will be switched to the working state accordingly, and the original working timer will be switched to the idle state. In this way, under limited microcontroller resources, a motor only needs one timer to work, avoiding resource conflicts and reducing the burden on the microcontroller.
[0079] Receiving the pulse signals generated by the motor when it rotates, and determining the target motor that is currently rotating based on these pulse signals, is to achieve real-time monitoring and feedback control of the motor's operating status, ensuring that the motor can accurately run along the expected trajectory. Each motor is connected to an encoder. The encoder can convert the actual rotation of the motor into a series of pulse signal outputs. These pulse signals can not only reflect whether the motor is rotating, but also accurately calculate the motor's motion parameters such as speed and rotation position.
[0080] S302: configuring a pulse counter to a transmission path of a pulse signal corresponding to a target motor, and using overflow parameters and underflow parameters preset in the pulse counter to interrupt and count the pulse signal, so as to determine a moving position of the motor-driven lens.
[0081] After determining the current target motor and receiving the pulse signal output by its encoder, it is necessary to configure the pulse counter to the pulse signal transmission path corresponding to the target motor, and use the overflow and underflow parameters preset in the pulse counter to interrupt the counting of the pulses, so as to determine the actual moving position of the motor-driven lens. In specific implementation, a mapping relationship is established between the number of pulses output by the encoder and the actual rotation displacement of the motor. For example, the overflow parameter can be set to the total number of pulses corresponding to the number of pulses of the encoder for one rotation of the motor. In this way, every time the counter counts an overflow cycle, it corresponds to one rotation of the motor. Therefore, the number of rotation cycles and displacement of the motor can be accurately calculated based on the count value. When the count value reaches the overflow or underflow parameter, an interrupt signal is triggered to notify the current counting state of the single-chip microcomputer chip. Through this interrupt counting method, the single-chip microcomputer can efficiently track the changes in the pulse count value without frequently querying the register. Finally, based on the count value obtained from the pulse counter and combined with the known pulse-displacement conversion relationship, the current actual rotation position of the target motor can be calculated in real time, thereby determining the moving position of the motor-driven lens.
[0082] In an optional implementation, the interrupt counting may be implemented through the following steps, where step S105 further includes steps S401 - S403 .
[0083] S401: Receive the pulse signal of the motor collected by the encoder.
[0084] In the specific implementation, the pulse signal output by the encoder needs to be connected to the pulse counter inside the microcontroller so that the pulse counter can count the number of pulses received in real time and temporarily store the count value in the internal register. Since there are multiple motors, the microcontroller needs to set up a pulse output path for each motor to ensure that the pulse signals output by the encoders of different motors can be received and counted separately. In the implementation process, it is necessary to initialize and start the pulse counting path connected to the target motor currently being controlled so that it is ready to receive the pulse signal of the motor encoder.
[0085] S402: When the current count value of the pulse signal reaches the preset overflow parameter in the pulse counter, the overflow interrupt state of the pulse counter is triggered and cleared, and the interrupt count value plus one is obtained, and the rotation position of the motor is determined according to the interrupt count value.
[0086] After the pulse counter is configured to the pulse output path of the target motor encoder and started, the pulse counter will receive the pulse signal output by the encoder in real time and count. In order to accurately calculate the number of rotation cycles and displacement of the motor, an overflow parameter needs to be set in advance. The overflow parameter corresponds to a counting cycle, that is, the total number of pulses output by the encoder when the motor rotates one circle. When the count value of the pulse counter reaches this overflow parameter, it means that the motor has rotated a complete cycle. In order to continue to monitor the subsequent motion state, the counter will trigger an overflow interrupt signal when the count reaches the overflow parameter to notify the microcontroller of the current counting state. At the same time, the pulse counter will automatically clear the current count value and restart the next round of counting. The microcontroller will record this overflow interrupt and add one to the interrupt count value, which records the number of cycles the motor has rotated.
[0087] In an optional implementation, the overflow parameter of the pulse counter may be determined by the following steps, and before step S402, steps S4021-S4022 are also included.
[0088] S4021: Obtain the resolution of the encoder and the acquisition accuracy required for lens movement.
[0089] The acquisition accuracy required for lens movement refers to the accuracy level required for detecting and measuring the lens displacement when the camera lens is adjusted and focused. In the embodiments of the present application, it can be understood that a target accuracy value for lens displacement acquisition is pre-set according to the requirements of imaging quality, such as ±0.01 mm.
[0090] Encoder resolution refers to the smallest displacement unit that the encoder can detect, usually given in the form of lines or pulses per revolution. In the process of controlling the motor to drive the lens to move, encoders and other position sensors need to be able to detect and feedback the actual displacement of the lens. The resolution of the encoder directly determines the minimum displacement it can detect, which in turn affects the position acquisition accuracy of the motor.
[0091] S4022: Determine an overflow parameter of a pulse counter according to the resolution of the encoder and the acquisition accuracy required for lens movement.
[0092] During the movement of the lens, an incremental encoder is usually used to detect the displacement of the lens. The encoder will output a series of pulse signals according to the actual movement of the lens. These pulse signals are sent to the pulse counter for accumulation to obtain the displacement count value of the lens. The resolution of the encoder determines the minimum pulse increment unit it can output, that is, the minimum detection displacement. In order to meet the acquisition accuracy requirements, the counting range of the pulse counter must be large enough to cover the theoretical displacement of the lens in the entire stroke. Specifically, first obtain the resolution parameter of the encoder, which is usually given in the form of "number of lines" or "number of pulses / turn". Convert it to the actual minimum detection displacement increment. For example, a 2000-line incremental encoder means that it can output 2000 pulses per rotation, that is, its minimum detection displacement unit is 1 / 2000 of the lens stroke. Then obtain the travel range of the lens, and then divide the total lens travel by the encoder's minimum detection displacement unit to calculate the maximum pulse count value required in theory. For example, if the lens movement stroke is 10 mm and the encoder resolution is 0.005 mm / pulse, then the theoretical maximum pulse count is 10 / 0.005 = 2000. After determining the theoretical maximum pulse count, combined with the requirements for lens movement acquisition accuracy, an appropriate overflow threshold can be set.
[0093] S403: When the current count value of the pulse signal reaches the underflow parameter preset in the pulse counter, the underflow interrupt state of the pulse counter is triggered and cleared, and the interrupt count value minus one is obtained, and the rotation position of the motor is determined according to the interrupt count value.
[0094] In actual applications, the motor may not only rotate forward, but also reverse. In order to accurately calculate the rotation position and direction of the motor, it is necessary to set the overflow and underflow interrupt parameters at the same time. The overflow parameter corresponds to the pulse count value of one forward rotation, while the underflow parameter corresponds to the minimum pulse count value of one reverse rotation. When the pulse count value drops below the preset underflow parameter, it indicates that the motor has reversed. At this time, the underflow interrupt will be triggered, and the count value will be automatically reset to the maximum count value corresponding to the overflow parameter, and continue counting. At the same time, the interrupt count value will be reduced by one to record the number of cycles of the motor's reverse rotation. By combining the overflow and underflow interrupt modes, the microcontroller can accurately determine the current rotation state, direction, and number of rotation cycles of the motor. Then, based on this information and the known pulse-displacement conversion relationship, the actual rotation position of the motor at that moment can be accurately calculated.
[0095] In an optional implementation, a method for acquiring a pulse signal is provided, and before step S105, steps S501-S502 are also included.
[0096] S501: Set the mode of the pulse counter to the dual-channel orthogonal pulse counting mode, and turn off the filter enable and timeout enable of the pulse counter.
[0097] In the embodiment of the present application, the pulse signal includes a first pulse signal and a second pulse signal, and the phase difference between the first pulse signal and the second pulse signal is 90°.
[0098] The encoder can convert the rotation of the motor into a series of pulse sequences, usually outputting two orthogonal pulse signals of phase A and phase B with a phase difference of 90°, namely the first pulse signal and the second pulse signal. Although the two pulses have the same frequency, due to the existence of a fixed 90° phase difference, their high and low level states must be different at any time. Using this feature, the actual rotation direction of the motor can be determined by judging the phase sequence of the A phase and B phase pulses. If the phase of phase A leads that of phase B, it is judged that the motor is rotating forward; conversely, if the phase of phase B leads that of phase A, it is judged that the motor is rotating reversely. In this way, the direction of rotation of the motor can be accurately known through a simple phase comparison. In addition to the direction, the frequency of the two orthogonal pulses is also proportional to the actual speed of the motor. By counting the two pulses, the number of pulses per unit time can be obtained, and then the accurate motor speed value can be calculated based on the known pulse-speed conversion relationship.
[0099] The dual-channel orthogonal pulse counting mode refers to the use of the first pulse signal and the second pulse signal of the two orthogonal pulses output by the encoder. By correctly counting and judging the two pulses, the rotation direction and speed of the motor can be accurately determined. In the embodiment of the present application, it can be understood that when the motor position feedback is performed, the two orthogonal pulse outputs of the encoder are collected, and they are respectively connected to the pulse counter channels inside the single-chip microcomputer, and the pulse counting channels are used to coordinately count and judge the A / B two-way orthogonal pulses, so that the precise direction of the motor (forward or reverse) can be judged according to the phase relationship of the two pulses, and the speed value is calculated according to the pulse frequency.
[0100] Filter enable refers to an enabling control mechanism for filtering input signals in digital signal processing systems. By enabling or disabling the filtering function through software or hardware, you can choose whether to filter the input signal according to actual needs.
[0101] Timeout enable refers to setting a timeout threshold in a digital system. If an operation or process fails to complete within the specified time, a timeout interrupt or error handling mechanism is triggered. It provides time protection for the system's exception handling.
[0102] The pulse counter is configured in dual-channel orthogonal pulse counting mode in order to accurately determine the rotation direction and speed of the motor, thereby obtaining accurate feedback data of the motor's motion state. At the same time, the filter enable and timeout enable of the counter need to be turned off to ensure maximum utilization of hardware resources and obtain the original high-resolution pulse signal under low interference.
[0103] Specifically, the encoder will output two orthogonal pulse signals, phase A and phase B, with a phase difference of 90 degrees. By setting the pulse counter to dual-channel mode, the two pulse signals can be captured separately, and the rotation direction of the motor can be determined by analyzing their phase relationship. If the phase of phase A leads that of phase B, the motor is judged to be rotating forward; conversely, if the phase of phase B leads that of phase A, it is judged to be rotating reversely. At the same time, the actual speed value of the motor can also be accurately calculated based on the frequency of the two pulses.
[0104] During the implementation process, the pulse counter must first be set to dual-channel orthogonal counting mode through programming or hardware configuration. Then, the two orthogonal pulse signals A and B output by the encoder are connected to the two input channels of the pulse counter respectively. In this way, the counter can count and analyze the phases of the two pulses synchronously to obtain the direction and speed information of the motor.
[0105] At the same time, it is also required to turn off the filter enable and timeout enable of the pulse counter. The purpose of turning off the filter enable is to avoid the delay caused by the digital filtering process, ensure the real-time nature of the pulse counting, and save computing resources. At the same time, in this embodiment, it is assumed that the environment in which the encoder is located is relatively simple and the external interference is small, and the original unfiltered pulse sequence can be directly used.
[0106] S502: When a rising edge or a falling edge of the first pulse signal or the second pulse signal is detected, the pulse signal is read and recorded.
[0107] In order to obtain the steering information, it is necessary to analyze the phase difference between phase A and phase B. According to their phase sequence, it can be determined whether the motor is rotating forward or reverse. To analyze the phase difference, it is necessary to capture and record the occurrence time of each pulse edge (including rising and falling edges), and then calculate the time interval between adjacent edges to obtain the phase difference between phase A and phase B. Specifically, in the implementation process, it is necessary to set the interrupt control register of the pulse counter by programming so that it can trigger the corresponding interrupt service program when the rising or falling edge of the A-phase or B-phase pulse signal is detected.
[0108] Once these edge events occur, the interrupt service program will be called and executed. In the interrupt service program, it is necessary to first determine whether the interrupt source is phase A or phase B, and whether it is a rising edge or a falling edge, so as to record the corresponding edge event and its occurrence time. For example, the capture unit of the microcontroller can be used to read and record the current time series information in the capture unit. By continuously capturing and recording all edge events of phase A and phase B, the complete time series information between them can be obtained, and then the phase difference value can be calculated. According to the positive or negative phase difference, the current rotation direction can be determined. At the same time, according to the time interval between edge events, the pulse frequency can also be calculated to obtain an accurate speed value.
[0109] In an optional implementation, when the mapping relationship between the motor rotation and the displacement position of the lens needs to be configured for the first time and the control parameters of the driving motor need to be determined, the following steps can be used to implement it. After step S105, steps S601-S603 are also included.
[0110] S601: Obtain the measuring range of the motor and determine the test position within the measuring range.
[0111] The test position refers to a special position point pre-set in the motor control system for testing the motion control accuracy of the motor. In the embodiments of the present application, it can be understood that the coordinate values of the test position are pre-planned and stored within the motion range of the motor as a detection point for the closed-loop control performance of the motor.
[0112] In the specific implementation process, it is necessary to first consult the technical parameter manual of the motor model or consult the supplier to accurately know the range limit of the motor, including the maximum travel range, maximum speed, etc. After understanding the range, you can reasonably select a position within the range as the only test point in combination with the needs of the actual application scenario. The test position can be set at the midpoint of the motor, so that the control accuracy of forward and reverse motion can be detected at the same time. The test position can also be set at a specific position with the highest and most critical control accuracy requirements to focus on evaluating the performance of moving to this position. Depending on the application scenario, the test position can also be set near the critical position of the range, such as the maximum travel point or the highest speed point, to detect the control accuracy under extreme conditions. The selection of test points is not limited here, so as to reflect the most critical control accuracy requirements for the application scenario as much as possible.
[0113] S602: Control the motor to move to an initial position where the pulse signal collected by the encoder returns to zero, and determine the rotation parameters of the motor moving from the initial position to the test position.
[0114] The initial position refers to the starting reference point for position counting and motion control in the motor control system. In the embodiments of the present application, it can be understood that by detecting the pulse signal output by the encoder, the pulse count zero point corresponding to a specific angular position of the motor is identified, and the position is determined as the initial position.
[0115] Rotation parameters refer to a series of parameters related to motor rotation that the control system needs to calculate and set in order to achieve the expected trajectory of the motor. In the embodiment of the present application, it can be understood that the control system determines the rotation angle, rotation distance, segmented displacement and corresponding torque, rotation direction, target speed curve and other parameters required for the motor to drive the lens from the initial position to the test position based on the current initial position of the motor and the coordinates of the target test position.
[0116] The motor-controlled lens is moved to the initial position where the pulse signal collected by the encoder returns to zero in order to obtain a clear position reference point so that the rotation parameters required for the motor to move from the initial position to the preset test position can be accurately calculated.
[0117] By detecting the pulse sequence output by the encoder, the pulse count zero point corresponding to a specific angle position of the motor is identified. Different types of encoders may correspond to different pulse count zero points at different angle positions. The control system needs to set appropriate pulse count zero point identification rules according to the parameters of the encoder used. After the pulse count zero point is identified, the motor can be controlled to move so that it rotates to the initial reference position corresponding to the pulse count zero point. After reaching this position, the current position coordinates of the motor can be set to the known origin coordinates as a reference for subsequent motion control. At the same time, the encoder counter needs to be cleared and reset to ensure that the position count starts from zero from the initial position. After the motor moves to the initial position, the control system can read the test position coordinate value pre-set in the memory. According to the initial position coordinates and the target test position coordinates of the current motor, the various rotation parameters required for the motor to move from the initial position to the test position can be determined.
[0118] S603: Acquire inertia compensation parameters generated during the rotation of the motor and the actual position of the motor, and adjust the rotation parameters.
[0119] Inertia compensation parameters refer to some parameter compensation and adjustment of the control system in order to overcome the inertia effect during the movement of the motor. In the embodiment of the present application, it can be understood that the control system pre-calculates a set of inertia compensation parameters, mainly including inertia compensation torque and inertia movement compensation distance, etc., based on the physical parameters such as the mass and inertia moment of the motor and the desired motion state (such as acceleration, speed, etc.).
[0120] During the actual rotation of the motor, due to the presence of mass and inertia, its motion state often deviates from the theoretical trajectory, which is manifested as the actual position leading or lagging behind the theoretical position, and the actual acceleration or speed may not match the set value. This inertial effect will lead to a decrease in control accuracy. In order to solve this problem, the control system needs to first obtain the inertia parameters of the motor itself, such as mass, moment of inertia, etc. At the same time, it is also necessary to understand the current required motion state, such as the expected acceleration, maximum speed, etc. Based on these parameters, the control system can pre-calculate the inertia compensation parameters required in this motion state. It mainly includes inertia compensation torque and inertia movement compensation distance. Inertia compensation torque is used to overcome the hindering effect of inertia on the motor, generate a larger actual driving torque, and ensure that the motor can start and decelerate smoothly according to the expected acceleration. Inertia movement compensation distance estimates and compensates for the influence of inertia on the position, and offsets the actual displacement deviation caused by inertia by advancing or delaying the theoretical position command by a certain distance. After the motor starts to rotate, the control system needs to obtain its current actual position and speed feedback in real time. And compare these feedback values with the position and speed commands of the theoretical trajectory to calculate the position deviation and speed deviation. Based on these deviation values, the control system can dynamically adjust the inertia compensation parameters calculated previously, instantly correct the inertia compensation torque and inertia movement compensation distance, and superimpose them on the original rotation parameters.
[0121] The following are device embodiments of the present application, which can be used to execute the method embodiments of the present application. For details not disclosed in the device embodiments of the present application, please refer to the method embodiments of the present application.
[0122] See also Figure 2 , which shows a schematic diagram of the structure of a lens driving device based on a single-chip microcomputer provided by an exemplary embodiment of the present application. The device can be implemented as all or part of the device through software, hardware, or a combination of both. The lens driving device based on a single-chip microcomputer may include: A signal acquisition module, for acquiring, when detecting that the first output pin in the timer is occupied, the first PWM wave of the first output pin before being occupied and the level state of the second output pin before being occupied; A PWM wave determination module, used for inverting the first PWM wave to obtain a second PWM wave outputted by a second output pin; An output voltage value determination module is used to determine the first universal pin of the two universal pins that is connected to the same pole of the motor as the first output pin, and to perform a level flip on the level state of the second output pin to obtain an output voltage value of the first universal pin; A motor driving module, used for outputting a second PWM wave and an output voltage value to the motor via the second output pin and the first general pin respectively, so as to control the motor to drive the lens to move; The position determination module is used to receive the pulse signal generated by the motor rotation collected by the encoder, use the overflow parameter and underflow parameter preset in the pulse counter to interrupt the pulse signal and count the movement position of the motor-driven lens.
[0123] Based on the above embodiment, as an optional embodiment, the PWM wave determination module is also used to obtain the duty cycle of the first PWM wave; invert the first PWM wave according to the duty cycle of the first PWM wave to obtain the second PWM wave corresponding to the second output pin of the first timer.
[0124] On the basis of the above embodiment, as an optional embodiment, the output voltage value determination module is also used to obtain the voltage amplitude of the first PWM wave of the first output pin before being occupied, and determine the first general pin of the two general pins that is connected to the same pole of the motor as the first output pin; if the level state of the second output pin before being occupied is a high level, the output voltage value of the first general pin is determined to be a low level; if the level state of the second output pin before being occupied is a low level, the output voltage value of the first general pin is determined to be a high level equal to the voltage amplitude of the first PWM wave.
[0125] Based on the above embodiment, as an optional embodiment, the position determination module is also used to receive the pulse signal generated by the encoder when the motor rotates, and determine the target motor that is in the rotating state at the current moment; configure the pulse counter to the transmission path of the pulse signal corresponding to the target motor, and use the overflow parameter and underflow parameter preset in the pulse counter to interrupt the pulse signal and count the pulse signal to determine the moving position of the motor-driven lens.
[0126] On the basis of the above embodiments, as an optional embodiment, the position determination module is also used to receive the pulse signal of the motor collected by the encoder; when the current count value of the pulse signal reaches the overflow parameter preset in the pulse counter, the overflow interrupt state of the pulse counter is triggered and cleared, and the interrupt count value with a value plus one is obtained, and the rotation position of the motor is determined according to the interrupt count value; or, when the current count value of the pulse signal reaches the underflow parameter preset in the pulse counter, the underflow interrupt state of the pulse counter is triggered and cleared, and the interrupt count value with a value minus one is obtained, and the rotation position of the motor is determined according to the interrupt count value.
[0127] Based on the above embodiment, as an optional embodiment, the position determination module is also used to set the mode of the pulse counter to a dual-channel orthogonal pulse counting mode, and turn off the filtering enable and timeout enable of the pulse counter; when the rising edge or falling edge of the first pulse signal or the second pulse signal is detected, the pulse signal is read and recorded.
[0128] Based on the above embodiment, as an optional embodiment, the position determination module is also used to obtain the range of the motor and determine the test position within the range; control the motor to move to the initial position where the pulse signal collected by the encoder is zeroed, and determine the rotation parameters of the motor moving from the initial position to the test position; obtain the inertia compensation parameters generated during the rotation of the motor and the actual position of the motor, and adjust the rotation parameters.
[0129] Based on the above embodiment, as an optional embodiment, the position determination module is further used to obtain the resolution of the encoder and the acquisition accuracy required for the lens movement; The overflow parameter of the pulse counter is determined according to the resolution of the encoder and the acquisition accuracy required for the lens movement.
[0130] The present application also provides a computer storage medium that can store multiple instructions, which are suitable for being loaded and executed by a processor as described above. Figure 1 The lens driving method based on a single chip microcomputer in the embodiment shown in the figure can be specifically referred to in the following Figure 1 The specific description of the illustrated embodiment will not be repeated here.
[0131] See also Figure 3 , is a schematic diagram of the structure of an electronic device provided in an embodiment of the present application. Figure 3 As shown, the electronic device 300 may include: at least one processor 301 , at least one network interface 304 , a user interface 303 , a memory 305 , and at least one communication bus 302 .
[0132] The communication bus 302 is used to realize the connection and communication between these components.
[0133] The user interface 303 may include a standard wired interface or a wireless interface.
[0134] The network interface 304 may optionally include a standard wired interface or a wireless interface (such as a WI-FI interface).
[0135] Among them, the processor 301 may include one or more processing cores. The processor 301 uses various interfaces and lines to connect various parts in the entire server, and executes various functions of the server and processes data by running or executing instructions, programs, code sets or instruction sets stored in the memory 305, and calling data stored in the memory 305. Optionally, the processor 301 can be implemented in at least one hardware form of digital signal processing (Digital Signal Processing, DSP), field programmable gate array (Field-Programmable Gate Array, FPGA), and programmable logic array (Programmable Logic Array, PLA). The processor 301 can integrate one or a combination of a central processing unit (Central Processing Unit, CPU), a graphics processing unit (Graphics Processing Unit, GPU) and a modem. Among them, the CPU mainly processes the operating system, user interface and application programs; the GPU is responsible for rendering and drawing the content to be displayed on the display screen; the modem is used to process wireless communications. It can be understood that the above-mentioned modem may not be integrated into the processor 301, and it can be implemented separately through a chip.
[0136] Among them, the memory 305 may include a random access memory (Random Access Memory, RAM) and may also include a read-only memory (Read-Only Memory). Optionally, the memory 305 includes a non-transitory computer-readable storage medium. The memory 305 can be used to store instructions, programs, codes, code sets or instruction sets. The memory 305 may include a program storage area and a data storage area, wherein the program storage area may store instructions for implementing an operating system, instructions for at least one function (such as a touch function, a sound playback function, an image playback function, etc.), instructions for implementing the above-mentioned various method embodiments, etc.; the data storage area may store data involved in the above-mentioned various method embodiments, etc. The memory 305 may also be optionally at least one storage device located away from the aforementioned processor 301. As Figure 3 As shown, the memory 305 as a computer storage medium may include an operating system, a network communication module, a user interface module, and an application program of a lens driving method based on a single chip microcomputer.
[0137] exist Figure 3In the electronic device 300 shown, the user interface 303 is mainly used to provide an input interface for the user and obtain data input by the user; and the processor 301 can be used to call an application program stored in the memory 305 for a lens driving method based on a single-chip microcomputer. When executed by one or more processors, the electronic device executes one or more methods in the above-mentioned embodiments.
[0138] An electronic device readable storage medium stores instructions, which, when executed by one or more processors, enable the electronic device to execute one or more methods in the above embodiments.
[0139] It should be noted that, for the aforementioned method embodiments, for the sake of simplicity, they are all described as a series of action combinations, but those skilled in the art should be aware that the present application is not limited by the order of the actions described, because according to the present application, certain steps can be performed in other orders or simultaneously. Secondly, those skilled in the art should also be aware that the embodiments described in the specification are all preferred embodiments, and the actions and modules involved are not necessarily required for the present application.
[0140] In the above embodiments, the description of each embodiment has its own emphasis. For parts that are not described in detail in a certain embodiment, reference can be made to the relevant descriptions of other embodiments.
[0141] In the several embodiments provided in this application, it should be understood that the disclosed devices can be implemented in other ways. For example, the device embodiments described above are only schematic, such as the division of units, which is only a logical function division. There may be other division methods in actual implementation, such as multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some service interfaces, and the indirect coupling or communication connection of devices or units can be electrical or other forms.
[0142] The units described as separate components may or may not be physically separated, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed on multiple network units. Some or all of the units may be selected according to actual needs to achieve the purpose of the solution of this embodiment.
[0143] In addition, each functional unit in each embodiment of the present application may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit. The above-mentioned integrated unit may be implemented in the form of hardware or in the form of software functional units.
[0144] If the integrated unit is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-readable memory. Based on this understanding, the technical solution of the present application, or the part that contributes to the prior art, or all or part of the technical solution can be embodied in the form of a software product, which is stored in a memory and includes several instructions for a computer device (which can be a personal computer, server or network device, etc.) to execute all or part of the steps of the various embodiments of the present application. The aforementioned memory includes: various media that can store program codes, such as USB flash drives, rotating hard disks, magnetic disks or optical disks.
[0145] The above are only exemplary embodiments of the present disclosure and cannot be used to limit the scope of the present disclosure. That is, any equivalent changes and modifications made according to the teachings of the present disclosure are still within the scope of the present disclosure. After considering the disclosure of the specification and the truth of practice, it will be easy for those skilled in the art to think of other embodiments of the present disclosure. This application is intended to cover any variation, use or adaptive change of the present disclosure, which follows the general principles of the present disclosure and includes common knowledge or customary technical means in the technical field that are not recorded in the present disclosure.
Claims
1. A lens driving method based on a single chip microcomputer, It is characterized in that The method is applied to a single-chip microcomputer, the single-chip microcomputer includes a timer and a pulse counter, the first output pin and the second output pin of the timer are respectively connected to the two poles of a motor driving a lens, the two poles of the motor are also respectively connected to two general pins of the single-chip microcomputer, the motor is connected to the pulse counter via an encoder, and the method includes: When it is detected that the first output pin in the timer is occupied, respectively acquiring the first PWM wave of the first output pin before being occupied and the level state of the second output pin before being occupied; Inverting the first PWM wave to obtain a second PWM wave output by the second output pin; Determine a first universal pin of the two universal pins that is connected to the same pole of the motor as the first output pin, and perform level flipping on the level state of the second output pin to obtain an output voltage value of the first universal pin; Outputting the second PWM wave and the output voltage value to the motor via the second output pin and the first general pin respectively, so as to control the motor to drive the lens to move; The pulse signal generated by the motor when rotating and collected by the encoder is received, and the pulse signal is interrupted and counted using the overflow parameter and the underflow parameter preset in the pulse counter to determine the moving position of the lens driven by the motor.
2. The method according to claim 1, It is characterized in that The step of determining a first universal pin of the two universal pins that is connected to the same pole of the motor as the first output pin, and performing a level flip on the level state of the second output pin to obtain an output voltage value of the first universal pin includes: Obtaining a voltage amplitude of a first PWM wave of the first output pin before being occupied, and determining a first general pin of the two general pins that is connected to the same pole of the motor as the first output pin; If the level state of the second output pin is a high level before being occupied, the output voltage value of the first general pin is determined to be a low level; If the level state of the second output pin is a low level before being occupied, the output voltage value of the first general pin is determined to be a high level equal to the voltage amplitude of the first PWM wave.
3. The method according to claim 1, It is characterized in that The single chip microcomputer includes a plurality of timers, the timers are connected to the motors in a one-to-one correspondence, the receiving of the pulse signal generated by the motor when the motor rotates and collected by the encoder, using the overflow parameter and the underflow parameter preset in the pulse counter to interrupt the counting of the pulse signal, and determining the moving position of the lens driven by the motor, further comprising: Receiving the pulse signal generated by the encoder when the motor rotates, and determining the target motor that is currently in a rotating state; The pulse counter is configured to the transmission path of the pulse signal corresponding to the target motor, and the pulse signal is interrupted and counted using the overflow parameter and underflow parameter preset in the pulse counter to determine the moving position of the lens driven by the motor.
4. The method according to claim 1, It is characterized in that The receiving of the pulse signal generated by the motor when the motor rotates and collected by the encoder, and using the overflow parameter and the underflow parameter preset in the pulse counter to interrupt and count the pulse signal to determine the rotation position of the motor include: Receiving the pulse signal of the motor collected by the encoder; When the current count value of the pulse signal reaches the overflow parameter preset in the pulse counter, the overflow interrupt state of the pulse counter is triggered and cleared, and an interrupt count value with a value plus one is obtained, and the rotation position of the motor is determined according to the interrupt count value; or, When the current count value of the pulse signal reaches the underflow parameter preset in the pulse counter, the underflow interrupt state of the pulse counter is triggered and cleared, and an interrupt count value minus one is obtained, and the rotation position of the motor is determined based on the interrupt count value.
5. The method according to claim 1, It is characterized in that The pulse signal includes a first pulse signal and a second pulse signal, the phase difference between the first pulse signal and the second pulse signal is 90°, the receiving of the pulse signal generated when the motor rotates and collected by the encoder, using the overflow parameter and the underflow parameter preset in the pulse counter to interrupt the counting of the pulse signal, and determining the rotation position of the motor, including: The mode of the pulse counter is set to a dual-channel orthogonal pulse counting mode, and the filter enable and timeout enable of the pulse counter are turned off; When a rising edge or a falling edge of the first pulse signal or the second pulse signal is detected, the pulse signal is read and recorded.
6. The method according to claim 1, It is characterized in that After receiving the pulse signal generated by the motor when the motor rotates and collected by the encoder, interrupting and counting the pulse signal using the overflow parameter and the underflow parameter preset in the pulse counter, and determining the rotation position of the motor, the method further includes: Obtaining the measuring range of the motor and determining a test position within the measuring range; Controlling the motor to move to an initial position where the pulse signal collected by the encoder returns to zero, and determining the rotation parameters of the motor moving from the initial position to the test position; The inertia compensation parameters generated during the rotation of the motor and the actual position of the motor are obtained, and the rotation parameters are adjusted.
7. The method according to claim 1, It is characterized in that The method further comprises: receiving a pulse signal generated by the encoder when the motor rotates, and using an overflow parameter and an underflow parameter preset in the pulse counter to interrupt and count the pulse signal, and determining the rotation position of the motor. Obtaining the resolution of the encoder and the acquisition accuracy required for the lens movement; The overflow parameter of the pulse counter is determined according to the resolution of the encoder and the acquisition accuracy required for the lens movement.
8. The method according to claim 1, It is characterized in that The inverting the first PWM wave to obtain a second PWM wave output by the second output pin includes: Obtaining a duty cycle of the first PWM wave; The first PWM wave is inverted according to the duty cycle of the first PWM wave to obtain a second PWM wave corresponding to the second output pin of the first timer.
9. A lens driving device based on a single chip microcomputer, It is characterized in that The device comprises: A signal acquisition module, for acquiring, when detecting that the first output pin in the timer is occupied, a first PWM wave of the first output pin before being occupied and a level state of the second output pin before being occupied; a PWM wave determination module, configured to perform inversion processing on the first PWM wave to obtain a second PWM wave outputted by the second output pin; an output voltage value determination module, used to determine a first universal pin of the two universal pins connected to the same pole of the motor as the first output pin, and to perform a level flip on the level state of the second output pin to obtain an output voltage value of the first universal pin; A motor driving module, used for outputting the second PWM wave and the output voltage value to the motor via the second output pin and the first general pin respectively, so as to control the motor to drive the lens to move; The position determination module is used to receive the pulse signal generated by the motor when the encoder rotates, and use the overflow parameter and underflow parameter preset in the pulse counter to interrupt the pulse signal and determine the moving position of the lens driven by the motor.
10. An electronic device, It is characterized in that It includes a processor, a memory, a user interface and a network interface, the memory is used to store instructions, the user interface and the network interface are used to communicate with other devices, and the processor is used to execute the instructions stored in the memory so that the electronic device executes the method as described in any one of claims 1-7.
