//该函数在VC++6.0下编译测试通过,可实现直线、圆弧、完整圆的逐点比较法插补;并可将插补函数计算出的数//据点写入xxx.txt文件中//若有任何疑问,欢迎邮件联系,dingjiang90@foxmail.com//版权所有,侵权必究。转载时请注明来自大连理工机械工程学院Deanjiang#includestdio.h#includemath.h#includestdlib.h/********************************************************************//*函数名:InsertPoint*//*功能:控制机床各轴进给并将进给结果写入文件中*//*参数:doublex,doubley插补点单位:毫米*//*说明:与机床硬件关联,每产生一个点调用一次*//********************************************************************/voidInsertPoint(doublexCur,doubleyCur){externFILE*fp;charch=10;printf(xCur=%f,yCur=%f\n,xCur,yCur);fprintf(fp,%f,%f,xCur,yCur);fputc(ch,fp);}/********************************************************************//*函数名:Judge_Quadrant*//*功能:判断参数坐标的所在象限并返回相应象限值*//*参数:doublexmm*//*doubleymm*//********************************************************************/unsignedshortJudge_Quadrant(doublex,doubley){unsignedshortnDir;if(x=0){//象限判断if(y=0){nDir=1;return1;}else{nDir=4;return4;}}else{if(y=0){nDir=2;return2;}else{nDir=3;return3;}}}/********************************************************************//*函数名:PBP_Line*//*功能:逐点比较法直线插补*//*参数:doublestep步长mm*//*doubleXEnd,doubleYEnd插补终点mm*//********************************************************************/voidPBP_Line(doublestep,doubleXEnd,doubleYEnd){doublelDevVal;//偏差值doublexCurVal=0,yCurVal=0;//当前点的实际位置longStepMount;//插补总步数longStepCount=0;//插补次数计数器unsignedshortnDir;//当前点所在象限nDir=Judge_Quadrant(XEnd,YEnd);//象限判断XEnd=labs(XEnd);YEnd=labs(YEnd);StepMount=(long)(XEnd+YEnd)/step;lDevVal=yCurVal*XEnd-xCurVal*YEnd;while(StepCountStepMount){//终点判别/////////////////////////////////////////////////////////////if(lDevVal=0){//偏差〉=0switch(nDir){case1:xCurVal+=step;InsertPoint(xCurVal,yCurVal);break;case2:xCurVal-=step;InsertPoint(xCurVal,yCurVal);break;case3:xCurVal-=step;InsertPoint(xCurVal,yCurVal);break;case4:xCurVal+=step;InsertPoint(xCurVal,yCurVal);break;}lDevVal-=YEnd;}else{//偏差0switch(nDir){case1:yCurVal+=step;InsertPoint(xCurVal,yCurVal);break;case2:yCurVal+=step;InsertPoint(xCurVal,yCurVal);break;case3:yCurVal-=step;InsertPoint(xCurVal,yCurVal);break;case4:yCurVal-=step;InsertPoint(xCurVal,yCurVal);break;}lDevVal+=XEnd;}StepCount++;}}/********************************************************************//*函数名:PBP_Circle*//*功能:逐点比较法圆弧插补*//*参数:doublestep步长mm*//*doubleXStart,doubleYStart插补起点mm*//*doubleXEnd,doubleYEnd插补终点mm*//*doubleradius圆弧半径mm*//*boolbIsCW圆弧插补方向0或1*//********************************************************************/voidPBP_Circle(doublestep,doubleXStart,doubleYStart,doubleXEnd,doubleYEnd,doubleradius,intbIsCW){doublelDevVal,goallDevVal;//当前误差和与终点的差值doublexCurVal=XStart,yCurVal=YStart;//当前坐标unsignedshortnDir;//象限longstepcount=0;//步数lDevVal=xCurVal*xCurVal+yCurVal*yCurVal-radius*radius;goallDevVal=(xCurVal-XEnd)*(xCurVal-XEnd)+(yCurVal-YEnd)*(yCurVal-YEnd);while(goallDevVal=(step*step)||stepcount==0){if(lDevVal=0)//误差大于等于零{if(bIsCW==0)//逆圆插补{nDir=Judge_Quadrant(xCurVal,yCurVal);switch(nDir){case1:xCurVal-=step;InsertPoint(xCurVal,yCurVal);break;case2:yCurVal-=step;InsertPoint(xCurVal,yCurVal);break;case3:xCurVal+=step;InsertPoint(xCurVal,yCurVal);break;case4:yCurVal+=step;InsertPoint(xCurVal,yCurVal);break;}}else//顺圆插补{nDir=Judge_Quadrant(xCurVal,yCurVal);switch(nDir){case1:yCurVal-=step;InsertPoint(xCurVal,yCurVal);break;case2:xCurVal+=step;InsertPoint(xCurVal,yCurVal);break;case3:yCurVal+=step;InsertPoint(xCurVal,yCurVal);break;case4:xCurVal-=step;InsertPoint(xCurVal,yCurVal);break;}}}else//误差小于零{if(bIsCW==0)//逆圆插补{nDir=Judge_Quadrant(xCurVal,yCurVal);switch(nDir){case1:yCurVal+=step;InsertPoint(xCurVal,yCurVal);break;case2:xCurVal-=step;InsertPoint(xCurVal,yCurVal);break;case3:yCurVal-=step;InsertPoint(xCurVal,yCurVal);break;case4:xCurVal+=step;InsertPoint(xCurVal,yCurVal);break;}}else//顺圆插补{nDir=Judge_Quadrant(xCurVal,yCurVal);switch(nDir){case1:xCurVal+=step;InsertPoint(xCurVal,yCurVal);break;case2:yCurVal+=step;InsertPoint(xCurVal,yCurVal);break;case3:xCurVal-=step;InsertPoint(xCurVal,yCurVal);break;case4:yCurVal-=step;InsertPoint(xCurVal,yCurVal);break;}}}lDevVal=xCurVal*xCurVal+yCurVal*yCurVal-radius*radius;goallDevVal=(xCurVal-XEnd)*(xCurVal-XEnd)+(yCurVal-YEnd)*(yCurVal-YEnd);stepcount++;}}FILE*fp;//文件指针intmain(){doubleft=1000.0*10/60/1000;intj,j1,jj;charfn[10];printf(pleaseinputfilename:\n);//输入要写入数据的文件名,如pbp_circle.txtscanf(%s,fn);if((fp=fopen(fn,w))==NULL){printf(can'topenfile\n);exit(0);}PBP_Circle(1,50,0,50,0,50,1);//输入需要插补的圆弧的起点、终点、步长、方向//PBP_Line(1,50,30);//输入需要插补的直线的终点、步长fclose(fp);return0;}