这个例子在选定的程序上迭代,改变速度,并删除任何暂停指令和添加自定义程序调用#另见:# //www.x7093.com/doc/en/PythonAPI/robolink.html#robolink.Robolink.AddProgram从robolink导入* # API与RoboDK从RoboDK导入* #基本矩阵操作导入json #---------------------------------------- #设置振幅,单位为mm振幅= 5 #设置路径频率(周期/mm) #频率= 0.5频率= 1/4 #- > 4毫米为一个周期#------------------------------------ #设置间距,在毫米间距= 0.2 #间距=(1 /频率)/ 4 # #三角曲折# 4样品每周期三角曲折(4样品/周期)# Samples_x_Cycle = 10 # = 1 /(频率* Samples_x_Cycle间隔 ) #------------------------------------ # 从一开始就使用波APPLY_WAVE = False #程序触发电弧开始START_KEYWORD = ' ArcStart #程序触发电弧结束END_KEYWORD = ' ArcEnd #正弦波抵消mm抵消= 0 #--------------------------------- # 有一个梯形波的类型:#每波使用4个样本#使用1/8的正弦波作为偏移量#间距=(1/频率)/4 #每个点是0.25毫米分开#偏移= -(1/频率)/8 #有一个三角形类型的波:#每波使用4个样本#使用0的正弦波作为偏移量#间距=(1/频率)/4 #每个点是0.25毫米分开#偏移= 0 #有一个正弦波类型:#每波使用8个样本或更多#使用正弦波的0作为偏移量#间距=(1/频率)/8 #每个点间隔为0.25 mm #偏移量= 0 #侧运动:设置为True的正弦波的XY平面工具(默认)#将Falso上下移动Z轴SIDE_MOTION = True #---------------------------------------- #---------------------------------------- #---------------------------------------- #---------------------------------------- #---------------------------------------- Xcount =抵消WaveApplied = False #剩余不改变这个变量= 0 #开始RoboDK API RDK = Robolink() #计算样本每循环samples_x_cycle = 1/(Spacing * Frequency) print("Samples per cycle: " + str(samples_x_cycle)) if samples_x_cycle < 4: RDK.ShowMessage("Warning: Samples per cycle is %.1f.
建议减小正弦波频率或减小间距。每个周期至少有4个样品。”% (samples_x_cycle)) #角度公差考虑2个向量并行ANG_TOL = 0.1 * pi/180 #在弧度def PoseSplit(pose1, pose2, delta_mm, offst=0, dtot=0): pose_delta = invH(pose1) * pose2 Angle = pose_angle(pose_delta)*180/pi dist = norm(pose_delta. pos ()) d = delta_mm - offst x_list = [] pose_list = [] if d > dist或角度> 179.999:return [], [], d-dist, dtot x,y,z,w,p,r = pose_ur (pose_delta) steps = max(1,int(dist/delta_mm)) xd = x/dist yd = y/dist zd = z/dist wd = w/dist pd = p/dist rd = r/dist while d < dist + 1e-3: dtot += delta_mm x_list.append(dtot) pose_list。append(pose1 * UR_2_Pose([xd*d,yd*d,zd*d,wd*d,pd*d,rd*d])) d += delta_mm offst = d-dist return pose_list, x_list, offst, dtot START_KEYWORD = START_KEYWORD.lower() END_KEYWORD = END_KEYWORD.lower() #要求用户选择一个程序:prog = RDK。ItemUserPick("选择一个程序来创建正弦波",ITEM_TYPE_PROGRAM)如果不是Program . valid (): print("操作取消或没有程序可用")quit() #要求用户输入一个函数调用,该函数调用将在每次移动后添加:print("程序选定:" + prog.Name()) prog2 = RDK.AddProgram(prog.Name() + " sin ", prog.getLink(ITEM_TYPE_ROBOT)) prog2. showinstructions (False) RDK.Render(False) lastpose = None #迭代所有指令(索引起始为0,也可以对最后一条指令使用-1)ins_count = prog.InstructionCount() print("指令:" + str(ins_count)) for ins_id in range(ins_count): ins_dict = prog.setParam(ins_id) print("指令:" + str(ins_id)) #注意:该类型对于每条指令都是唯一的,不能更改。#然而,设置类型值1将删除指令(InstructionDelete一样())如果ins_dict['类型']= = INS_TYPE_MOVE: ins_name, ins_type, mv_type, isjointtarget,姿势,关节= prog.Instruction (ins_id)如果APPLY_WAVE mv_type = = MOVE_TYPE_LINEAR而不是isjointtarget: #分裂构成如果lastpose不是没有:#应用从lastpose正弦波姿势v_path = subs3 (pose.Pos (), lastpose.Pos()) #确保我们有一个运动如果规范(v_path) < 0.001:print(“移动到相同的姿势”)else: #只分裂如果移动不与Z轴并行(我们可以做一个方法)ang1 = angle3(v_path, pose. vz ()) ang2 = angle3(v_path, lastpose. vz ()) if ang1 > ANG_TOL和abs(pi-ang1) > ANG_TOL和ang2 > ANG_TOL和abs(pi-ang2) > ANG_TOL: hs,xs,剩余,Xcount = PoseSplit(lastpose, pose, Spacing, Remaining, Xcount) print(“分裂移动%s在%i步”% (ins_dict['Name'], len(hs)) for h,x in zip(hs,xs): # Calculate v_path in relative coordinate system (vx), relative to the TCP vz = [0,0,1] # h.VZ() vx = h[:3,:3].tr()*v_path # vector rotation angxz = angle3(vx,vz) if angxz > ANG_TOL and abs(pi-angxz) > ANG_TOL: vy = cross(vz, vx) #amplitude direction vy = normalize3(vy) a = -Amplitude * sin(x*Frequency*2*pi) if SIDE_MOTION: tool_move = mult3(vy, a) else: tool_move = mult3(vz, a) newpose = h*transl(tool_move) prog2.MoveL(newpose) else: print("Warning! Tool Z axis is parallel to the movement!") lastpose = pose continue else: print("Move is parallel to Z axis") lastpose = pose #prog2.setInstruction(ins_id, ins_name, ins_type, move_type, isjointtarget, pose, joints) # Add a normal move instruction tgt = pose if isjointtarget: tgt = joints.list() if mv_type == MOVE_TYPE_LINEAR: prog2.MoveL(tgt) elif mv_type == MOVE_TYPE_JOINT: prog2.MoveJ(tgt) else: RDK.ShowMessage("Circular movements not supported for splitting") quit() continue elif ins_dict['Type'] == INS_TYPE_CODE: # Add a new program call code_lower = ins_dict['Code'].lower() if START_KEYWORD in code_lower: APPLY_WAVE = True WaveApplied = True elif END_KEYWORD in code_lower: APPLY_WAVE = False # Reset sine wave to start again Xcount = Offset elif ins_dict['Type'] == INS_TYPE_CHANGESPEED: # Add a new program call speed_mms = ins_dict['Speed'] if speed_mms < 100.0: APPLY_WAVE = True WaveApplied = True elif speed_mms >= 1000.0: APPLY_WAVE = False # Reset sine wave to start again Xcount = Offset # Add copy of instruction to new program prog2.setParam("last", ins_dict) if not WaveApplied: RDK.ShowMessage("Add speed instructions to indicate the weld area (a speed of less than 100 mm/s is considered as weld area). You can also trigger program calls to ArcStart and ArcEnd to indicate the weld area.") quit() # Update joint values for all targets prog2.setParam("RecalculateTargets") # Remove selection automatically created for new instructions RDK.setSelection([])