Today, ZhengMotion Assistant will share with you how to perform CAD drawing import and multi-tasking development of RTBasic programs using the C# language. Before we begin, let's learn about ZhengMotion Technology's PCIE464 motion control card.
Hardware Introduction
The PCIE464 motion control card is an EtherCAT bus + pulse type, PCIE interface motion control card launched by Zheng Motion. It can be configured for 6-64 axis motion control, supports multiple high-speed digital inputs and outputs, and can easily realize multi-axis synchronous control and high-speed data transmission.
The PCIE464 motion control card is suitable for applications involving multi-axis point-to-point motion, interpolation motion, trajectory planning, handwheel control, encoder position detection, I/O control, and position latching. It is applicable to high-speed, high-precision applications in 3C electronics processing, testing equipment, semiconductor equipment, SMT processing, laser processing, optical communication equipment, lithium battery and photovoltaic equipment, and non-standard automated equipment.
Applications for PCIE4 series controller cards can be developed using software such as VC, VB, VS, C++, and C#. The program requires the dynamic library zmotion.dll to run. During debugging, the RTSys software can be connected to the controller simultaneously for convenient debugging and observation.
PCIe 464 Product Introduction
Wiring Reference
1. The digital input interfaces are distributed in the J400 (IN0-IN7) and X400 (IN8-IN39) signal interfaces.
2. OUT Digital Output Interface: Digital outputs are distributed in the J400 (OUT0-7) and X400 (OUT8-OUT39) signal interfaces.
Project development using C#
1. In VS2010, go to "File" → "New" → "Project" to start the Project Creation Wizard.
2. Select "Visual C#" as the development language and .NET Framework 4 as the Windows Forms application.
3. Locate the motion library and CAD drawing library provided by the manufacturer (taking a 32-bit library as an example).
4. Copy the C# library files and related files provided by the vendor to the newly created project, and put the C# related library files provided by the vendor into the bin\debug folder.
5. Open the newly created project file with Visual Studio. In the Solution Explorer on the right, click "Show all files". Then, right-click the zmcaux.cs file and click "Include in project". Following the steps for adding the zmcaux.cs file, add the zmclaser.cs and zmotioncad.cs files to the project.
6. Double-click Form1 in Form1.cs to open the code editing interface. At the beginning of the file, write using cszmcaux, using ZmotionCadDll, and declare the controller handle g_cardhandle and the handle g_cadhandle for using the CAD library functions used in CAD.
7. At this point, the project is complete and ready for C# project development.
CAD drawing export using C# language
In practical applications, manually calculating the coordinates of a drawing's trajectory is both time-consuming and laborious. Therefore, in this scenario, having a computer calculate the drawing's trajectory would be much more efficient. Next, we will introduce how to export CAD drawings using C#.
1. Operating Steps
First, create a new WinForm project and add a function library. Then, use the `ZMotionCadArray_ImportVectGraph` command to import and open the CAD drawing file, and perform small line segment splitting precision. Next, use the `ZMotionCadArray_GetVectNum` command to get the array length. Then, create a `struct_zcad_array` structure (this structure is already declared in the ZMotionCad library; you can define and call it directly) to store motion data, motion type, and other information. After creating this structure array, use the `ZMotionCadArray_GetVectArray` command to save the motion data of the drawing into this array.
Finally, the ZMotionCadArray_MergeSeg command is used to connect the points into a line. The operation logic is shown in the figure.
2. Instruction Introduction
3. Generate motion trajectory data
After importing the .dxf file, use the ZMotionCadArray_ImportVectGraph command to import the CAD drawing and split the small line segments.
Use ZMotionCadArray_GetVectNum to get the data length, and then create an array of type Struct_ZCad_Array.
At this point, the data of the CAD drawing is stored in this structure array, and the ZMotionCadArray_GetVectArray command is used to retrieve the graphic data.
Create an array of type StringBuilder, use a loop to access the motion type of the array of type Struct_ZCad_Array, and insert the motion coordinates of each type into the array.
Finally, using file I/O knowledge, a bas file is created, and motion data of a StringBuilder type array is appended to the end of the bas file.
1. Import the graphic file, use a thread to obtain the point information of the CAD drawing, and use a pen to display the point information on the drawing board.
The relevant code for retrieving drawing point information:
// Open the configuration file if (openFileDialog1.ShowDialog() == DialogResult.OK) { strFilePath = openFileDialog1.FileName; label11.Text = Path.GetFileName(strFilePath); textBox3.Text = textBox3.Text + "\r\n" + "Imported file: " + Path.GetFileName(strFilePath); // Import CAD drawing file m_refDistance Arc to small line segment splitting precision int iret = ZmotionCad.ZMotionCadArray_ImportVectGraph(strFilePath, 1024, 1, m_refDistance); // Import data, get array length iret = ZmotionCad.ZMotionCadArray_GetVectNum(ref ZCad_ArrayLen); // Create a structure variable ZCad_ArrayInfo = new ZmotionCad.Struct_ZCad_Array[ZCad_ArrayLen]; // Get graphic data. Put the data from the CAD file into the ZCad_ArrayInfo array. iret = ZmotionCad.ZMotionCadArray_GetVectArray(ref ZCad_ArrayInfo[0], ZCad_ArrayLen); // Whether to process only closed trajectories. iret = ZmotionCad.ZMotionCadArray_IfCloseVect(false); // Merge connected lines. iret = ZmotionCad.ZMotionCadArray_MergeSeg(0.5, If_Choose); // To determine whether to process only closed trajectories, get the array length and graphic data again. Get_Array(); // Get the left, bottom, length, and width values of the drawing with a subdivision of 0.05. iret = ZmotionCad.ZMotionCadArray_GetRange(ref Image_Left, ref Image_bottom, ref Image_Width, ref Image_Height, 0.05); if (Image_Width < 0.0001 && Image_Height < 0.0001) { Image_Left = (float)0.0; Image_bottom = (float)0.0; Image_Width = (float)100.0; Image_Height = (float)100.0; } double ObjectPixHeight, ObjectPixWidth; //The actual size of the graphics displayed on the artboard if (Image_Width * PicHeight <= Image_Height * PicWidth) { ObjectPixHeight = PicHeight; ObjectPixWidth = ObjectPixHeight * Image_Width / Image_Height; } else { ObjectPixWidth = PicWidth; ObjectPixHeight = ObjectPixWidth * Image_Height / Image_Width; } //Scale dScale = ObjectPixHeight / Image_Height; m_dUnitsPerMm = dScale * 1; //Offset m_dTranX = (MyPicture.Width - ObjectPixWidth) / 2 - Image_Left * dScale; m_dTranY = (MyPicture.Height - ObjectPixHeight) / 2 - Image_bottom * dScale; // Display the point information on the canvas using a paintbrush Show_Picture();
2. Click to run the program. The program generates motion commands through threads and sends them to the controller, which then executes the CAD point coordinates.
4. Performance
The motion trajectory on the host computer interface is compared with the motion trajectory captured by RTSys, and it is basically consistent with the trajectory on the RTSys oscilloscope.
Custom encapsulation instruction function
In actual development, to improve efficiency, if the ZMotion PC function library does not have ready-made commands for developers to use, they can implement the desired functions by customizing and encapsulating command functions to improve interaction efficiency. The following example of ZMotion PC Assistant will illustrate how to customize and encapsulate command functions using a scenario of sending multiple commands at once.
1. Instruction Description
2. Example Explanation
In the example code below, a custom encapsulation function can send multiple motion commands at once. The code logic is as follows: 1. The positive motion assistant first defines four formal parameters: handle, number of axes, axis list (starting and ending axis numbers), and motion distance for each axis (multiple point information can be used, with ";" as the end marker for each point information). 2. In the custom function, the content of the `axis_distance` parameter is cut and stored in the `dir_move` array using ";". 3. The point information in the `dir_move` array is concatenated into strings, and the motion commands `moveabs` are generated by iterative scanning and stored in a string variable named `cmdbuff`. 4. The multiple motion commands in the variable `cmdbuff` are sent to the control card or controller using the online sending command `ZAux_DirectCommand` of the ZMotion PC function library.
·
Description: // Custom encapsulation function to issue multiple motion commands at once Input: //handle Control card handle/controller handle //axis_num Total number of axes //axis_list List of axis numbers //axis_distance Coordinates of the moving point, ending with ";" to represent a motion coordinate Return: //Error code int __stdcall ZMotionCadArray_DelOne(int nDelVect);*************************************************************/public Int32 UseDefined_Moveabs(IntPtr handle,int axis_num, string axis_list,string axis_distance){ //String used to concatenate motion commands string cmdbuff = ""; string[] dir_move = axis_distance.ToString().Split(new char[] { ';' }); //Information to receive online commands StringBuilder psResponse =new StringBuilder(1024); //Check if the number of axes is reasonable if (axis_num < 0) { return -1; } // Generate command cmdbuff = "base(" + axis_list + ")"; for (int i = 0; i < dir_move.Length; i++) { // Concatenate motion commands cmdbuff = cmdbuff + "moveabs(" + dir_move[i] + ")"; } // Send the concatenated motion commands using online commands return zmcaux.ZAux_DirectCommand(handle, cmdbuff, psResponse, 1024); }
Instructions for calling custom-encapsulated functions:
·
// Get the size of the buffer for axis 0 ret = zmcaux.ZAux_Direct_GetRemain_Buffer(G_CardHandle, 0, ref buf0); err_inform("ZAux_Direct_GetRemain_Buffer0", ret); if (buf0 > 10) { // Send motion commands using a custom encapsulated function, sending 10 commands at a time ret = UseDefined_Moveabs(G_CardHandle, 2, axis, direct_str[i]); err_inform("UseDefined_Moveabs", ret); label15.Invoke(new MethodInvoker(() => direct_num(i.ToString())));
3. Code verification
(1) The data content in the formal parameter direct_str[0].
(2) Data spliced in cmdbuff.
By examining the data content of the two variables above, it is verified that the above code approach is logical and achieves efficient transmission of multiple data processing steps within a single instruction.
Real-time conversion between analog signals and speed
I. Operating Procedures
First, set the autorun task number in the Basic file under the project. You can check the maximum number of tasks the controller supports in the "Controller Status" of the RTSys software. Then, fill in the autorun task number in the Basic file (it cannot exceed the maximum number of supported tasks), and finally download it to the controller.
1. Check the maximum number of tasks the controller can handle.
2. Enter the automatic running task number.
3. After setting the automatic running task number, download the written program to the controller and run it.
4. Set the conversion ratio K value (from 0 to 1) on the host computer.
II. Instruction Introduction
Note: In this example code, the Varname parameter of ZAux_Direct_SetUserVar in the PC instruction is used with the global variable s_ratio from the Basic file. III. Example: In the Basic program below, the size of the controller's analog output port can be adjusted based on the axis speed, and the conversion ratio K between analog and speed can be modified via the host computer. After capturing the speed and analog output waveforms with an oscilloscope, it can be seen that the speed and analog output waveforms are basically consistent.
1. Basic related code.
GLOBAL s_ratio 'The ratio of speed WHILE 1' makes the relationship between speed and analog quantity proportional. AOUT(0)=4.095*VP_SPEED(0)*s_ratioWENDend
2. Relevant code in PC.
int ret = zmcaux.ZAux_Direct_SetUserVar(G_CardHandle, "s_ratio", Convert.ToSingle(textBox4.Text)); // Set the conversion ratio between analog and speed. err_inform("the ratio of ZAux_Direct_SetUserVar",ret);
IV. Video Explanation
Full code download address
▼
That concludes our sharing of the CAD export of DXF graphics from the PCIe EtherCAT real-time motion control card and the rapid processing of multiple motion commands.
For more exciting content, please follow the "Zheng Motion Assistant" WeChat official account. For related development environment and example code, please contact Zheng Motion's technical sales engineer: 400-089-8936.
This article is original content from Zheng Motion Technology. We welcome everyone to reprint it for mutual learning and to jointly improve China's intelligent manufacturing level. Copyright belongs to Zheng Motion Technology. Please indicate the source if you reprint this article.