1MODULE gCodeHandle
2
3 ! [filePath] 存放G代码的文件路径
4 ! [signalDoName] 轨迹记录控制信号的名称,用于开启\关闭仿真中的轨迹记录(模拟画笔)
5 ! [robTool] 机器人使用使用的工具
6 ! [centerPoint] 画板平面中心点坐标
7 PROC runGCode(String filePath, String signalDoName, inout tooldata robTool, robtarget centerPoint)
8 VAR iodev sourceFile;
9 !VAR signaldo ioOutput;
10 VAR string text;
11 VAR string commandLable;
12 VAR string result;
13
14 !GetDataVal signalDoName, ioOutput;
15
16 ! Open the file in read-only mode.
17 Open filePath, sourceFile\Read;
18
19 ! Run to aims point
20 !Reset ioOutput;
21 MoveL centerPoint,v100,z0,robTool;
22
23 WHILE TRUE DO
24 text := ReadStr(sourceFile);
25
26 ! Check file if end
27 IF text = EOF OR text = "" THEN
28 ! Exit proc
29 GOTO EXITRUN;
30 ENDIF
31
32 commandLable := StrPart(text, 1, 1);
33 IF commandLable = "#" OR commandLable = ";" THEN
34 ExitCycle;
35 ENDIF
36
37 ! Call handler
38 result := gCodeHandler(text, signalDoName, robTool, centerPoint);
39 IF result = "error" THEN
40 TPWrite("GCode write error!");
41 ENDIF
42 ENDWHILE
43
44 EXITRUN:
45 ! Close file.
46 ! Data placement.
47 Close sourceFile;
48 ! Robot return safe point
49 MoveL centerPoint,v100,z0,robTool;
50 ENDPROC
51
52 ! [text] 需要处理的G代码,如 G0 X12 Y77 Z9
53 ! [isWrite] 是否写入,如果设置为真函数会将转换后的G代码作为字符串返回(不会移动机器人)
54 ! [signalDoName] 轨迹记录控制信号的名称,用于开启\关闭仿真中的轨迹记录(模拟画笔)
55 ! [robTool] 机器人使用使用的工具
56 ! [centerPoint] 画板平面中心点坐标
57 FUNC string gCodeHandler(string text\switch isWrite, String signalDoName, inout tooldata robTool, robtarget centerPoint)
58 VAR num x:=0;
59 VAR num y:=0;
60 VAR num tmpX:=0;
61 VAR num tmpY:=0;
62 VAR num xLable := 0;
63 VAR num yLable := 0;
64 VAR num tmpLable := 0;
65 VAR num tmpLableTwo := 0;
66 VAR num tmpLen := 0;
67 VAR string commandLable := "";
68 VAR string tmpStr := "";
69 VAR signaldo ioOutput;
70
71 GetDataVal signalDoName, ioOutput;
72
73 ! Check command lable is then "G0" or "G1"
74 ! If not, then exit func
75 commandLable := StrPart(text, 1, 2);
76 IF commandLable <> "G0" AND commandLable <> "G1" THEN
77 RETURN "error";
78 ENDIF
79
80 ! Get x value.
81 xLable := strfind(text,1,"X");
82 IF xLable <= strLen(text) THEN
83 tmpLable := strfind(text,xLable," ");
84 !IF tmpLable > strLen(text) THEN
85 ! tmpStr := StrPart(text, xLable+1, StrLen(text)-(xLable+1));
86 !ELSE
87 ! tmpLable := strfind(text,xLable," ");
88 ! tmpLable := strfind(text,xLable," ")-(xLable+1);
89 ! tmpStr := StrPart(text, xLable+1, tmpLable)
90 !ENDIF
91 tmpLableTwo := tmpLable - (xLable + 1);
92 tmpStr := StrPart(text, xLable + 1, tmpLableTwo);
93 IF NOT StrToVal(tmpStr, tmpX) THEN
94 RETURN "error";
95 ENDIF
96 !IF (tmpX - 50) > x THEN
97 ! x := tmpX;
98 !ENDIF
99 x := tmpX;
100 ENDIF
101
102 ! Get y value.
103 yLable := strfind(text,1,"Y");
104 IF yLable <= strLen(text) THEN
105 tmpLable := strfind(text,yLable," ");
106 !IF tmpLable > strLen(text) THEN
107 ! tmpLen := StrLen(text);
108 ! tmpLableTwo := tmpLen - yLable;
109 ! tmpStr := StrPart(text, yLable+1, tmpLableTwo);
110 !ELSE
111 ! tmpStr := StrPart(text, yLable+1, tmpLable - 1);
112 !ENDIF
113 tmpLableTwo := tmpLable - (yLable + 1);
114 tmpStr := StrPart(text, yLable + 1, tmpLableTwo);
115 IF NOT StrToVal(tmpStr, tmpY) THEN
116 RETURN "error";
117 ENDIF
118 !IF (tmpY - 50) > y THEN
119 ! y := tmpY;
120 !ENDIF
121 y := tmpY;
122 ENDIF
123
124 ! Check is write data to file.
125 ! If not written, it will be executed immediately
126 IF Present(isWrite) THEN
127 ! Return result
128 RETURN strpart(text,1,2) + " X"+ValToStr(x)+" Y"+ValToStr(y);
129 ELSEIF strpart(text,1,2)="G0" THEN
130 MOveJ Offs(centerPoint,x,y,0),v500,fine,robTool;
131 ELSEIF strpart(text,1,2)="G1" THEN
132 ! Open welding torch
133 !%"Set "+signalDoName%;
134 !%"SetDO "+signalDoName+",1"%;
135 Set ioOutput;
136 MoveJ Offs(centerPoint,x,y,0),v500,fine,robTool;
137 ENDIF
138
139 ! Close welding torch
140 !%"Reset "+signalDoName%;
141 !%"SetDO "+signalDoName+",0"%;
142 Reset ioOutput;
143 ! Return default reslult
144 RETURN "success";
145
146 ENDFUNC
147
148ENDMODULE