Return HomeContact UsEnglishWelcome to KINGBELL official website!

Support

您当前的位置: 首页 > Technical Articles
SupportTechnical support
Technical ArticlesTechnical articles

Discussion on the control method of step motor based on VC++

发布时间 :2017-12-06 15:03 浏览次数 :
Stepper motor is an electrical pulse signal into the motor angular or linear displacement, but the stepper motor control usually uses assembly language or C language for software development, control the development of this paper SC3 stepper motor controller and platform as an example, the data communication introduces a case in Windows C++6.0 platform using Visual serial communication control MSComm to realize the PC machine and between step motor controller, realized by PC direct control method of stepping motor, and introduces the basic procedures and methods of serial communication program. The test results show that the designed control program is simple, easy to understand, reliable, and has a friendly human-computer interface.
 
Stepper motor is an electrical pulse into the electromechanical converter angular displacement, is widely used in the control system, but the traditional control of the stepper motor is generally used in assembly language or C language for software development, this paper use the VC++ serial communication serial communication control between MSComm PC and step motor controller. Unlike the serial communication program under DOS, Windows does not advocate application directly controlling hardware, but transmits data through the device driver provided by Windows operation system. The serial port in Win32 is handled as a file, rather than directly to the port operation, for serial communication, Win32 provides I/O file function and communication function accordingly, through the understanding of the use of these functions, can be developed in line with the needs of different communication procedures.
 
There are 3 ways to achieve serial communication: use VC++ to provide serial communication control MSComm, realize custom serial communication in single thread, and realize serial communication under multi thread. According to the actual situation, this system uses the serial communication control MSComm provided by VC++ to carry out software programming, which can easily manage and control the serial port of the computer.
 
1. System composition
 
The system of the stepper motor controlled by the PC is shown in Figure 1.
 
 
 
Figure 1 block diagram of PC control step motor system
 
The motor control of this system uses the asynchronous communication of the universal RS 232 serial port. Because RS 232 was established in the early stage to promote data communication between public telephone network, its logic level is symmetrical to the ground, which is totally different from TTL and MOS logic level. The logical 0 level is specified as +5 to +15 V, and the logic 1 level is between -5 to -15 V. Therefore, the connection between RS 232 driver and TTL level must undergo level conversion.
 
2. Control the technical parameters of hardware and software
 
This system adopts SC3 step Zhuolihanguang Instrument Co. Ltd. production into the translation stage and the realization of three-dimensional motor controller, control platform (x, y, Z). The RS 232 serial port asynchronous communication is used to complete the single step and continuous movement control of the stepping motor, and the actual position data of the motor is fed back to the PC machine.
 
The mechanical indexes of the electronic control platform are as follows:
(1) precision electronic control rotary table: model RSA200 for X axis. The rotation range is more than 40 degrees, the transmission ratio is 180: 1, the small step is 0.0003125 degrees, the mesa diameter is 200, the resolution is 0.00125 degrees, the repetition location accuracy is <0.005 degree, the maximum speed is 25/s, the center maximum load is 60 kg.
(2) heavy load type electronic control translation table: model TSA300B, used for Z axis. The minimum distance is 0.00315 mm, the precision of repeated positioning is <0.005 mm, and the effective stroke of the fixed translation table is 150 mm.
(3) ultra thin type electronic control translation table: model TSA30C, for Y axis. The minimum distance is 0.002 mm, the precision of repeated positioning is <0.005 mm, and the effective stroke is 30 mm. SC3 stepper motor controller has manual and linkage mode. Manual operation can set speed setting, returning to zero operation, direction setting, displacement setting, etc. online mode can make motor movement directly controlled by application software. Because of the two development, the application must embed the control instructions and protocols of the original controller.
The instruction system consists of the following:
Contact instruction instruction format: "? R "& CHR$(13)
The instruction sent out of 200 ms within SC3: "OK" & CHR$(10), indicating the success of the liaison.
Query instruction instruction format: "? V "& CHR$(13)
SC3 received the instruction after the return: "V number" & CHR$(10). Where number is the current speed value of the SC3 expressed by the ASC code. Range 0~255.
The coordinate query instruction instruction format: "? X "& CHR$(13) or"? " Y "& CHR$(13) or"? " Z "& CHR$(13)
SC3 receives the instruction after the return: "X+number" & CHR$(10), or "Xnumber" & CHR$(10), and the other axes are similar. The number is the current coordinate value of the SC3, represented by the ASC code, and the positive and negative sign represents the position of the current position in the positive and negative direction of the boot position (0 bits).
Speed setting instruction instruction format: "V" & number & CHR$(13)
In which number is set at the speed of the ASC code. Range 0~255.
Zero order instruction instruction format: "HX" & CHR$(13) or "HY" & CHR$(13) or "HZ" & CHR$(13)
SC3 is returned to zero after receiving such instructions. After the completion of the return to zero operation: "OK" & CHR$(10), indicating that the SC3 is finished.
Zero state query instruction instruction format: "? H "& CHR$(13)
SC3 after receiving such instructions: "H000" & CHR$(10)
000 of the meanings are:
The first value: 1 indicates that the Z axis is zero to zero successfully, and 0 indicates that the Z axis is not zero.
Second bit values: 1 indicates that the Y axis is zero successfully, and 0 indicates that the Y axis is not zero.
Third bit values: 1 indicates that the X axis is zero successfully, and 0 indicates that the X axis is not zero.

Run instruction instruction format: "Xdirectionnumber" & CHR$(13) or "Y directionnumber" & CHR$(13) or "Z directionnumber" & CHR$(13)

 
3. Software implementation
 
3.1 using the serial communication control MSComm provided by VC++ to realize serial communication
First, create a communication control in the dialog box of VC++ [5]. If there is no control in the Control toolbar, it can be inserted through menu Project to Add to, Project to Components and Control, and then the control is pulled from the toolbox to the dialog box. At this point, you only need to care about the interface of the API function provided by the control to the Windows communication driver, that is, just set and monitor the attributes and events of the MSComm control.
In the ClassWizard, we define the member object (CMSComm m_Serial) for the new communication control. By this object, we can set up the serial port property, and the MSComm control has 27 attributes, including:
Commport: set and return the communication port number, default is COM1.
Settings: set and return baud rate, parity check, data bit, stop bit in the form of string.
PortOpen: set and return the state of the communication port, and also open and close the port.
Input: returns and deletes characters from the receiving buffer.
Output: write a string to the send buffer.
InputLen: every time the number of characters read in Input is set, the default value is 0, indicating that all the content in the receiving buffer is read.
InBufferCount: returns the number of characters that have been received in the receiving buffer, and it is set to 0 to clear the receiving buffer.
InputMode: the way to define the Input attribute to obtain data (0: text mode; to 1: binary mode).
The RThreshold and SThreshold attributes indicate the number of characters that can be received in the buffer or send buffer before the OnComm event occurs.
The following is an instance of initializing the serial port by setting the control properties:
After opening the required serial port, we need to consider the timing of serial communication. In the process of receiving or sending data, we may need to monitor and respond to some events and errors, so event driving is a very effective way to deal with serial port interaction. Use the OnComm event and the CommEvent attribute to capture and check the value of the communication event and the error. When a communication event or error occurs, the OnComm event is triggered. The value of the CommEvent attribute is changed. The application checks the CommEvent attribute value and responds accordingly. Add the OnComm message processing function to the CMSComm control with ClassWizard in the program: