单片机与mpc运动控制模块组成的三轴运动控制系统.docx
《单片机与mpc运动控制模块组成的三轴运动控制系统.docx》由会员分享,可在线阅读,更多相关《单片机与mpc运动控制模块组成的三轴运动控制系统.docx(26页珍藏版)》请在冰点文库上搜索。
单片机与mpc运动控制模块组成的三轴运动控制系统
摘要:
采用单片机stc89C2051和mpc微型运动控制模块作为控制系统的核心,控制三路步进电机做运动实验。
单片机发送指令给mpc微型运动控制模块,模块信号输出给步进驱动器作高速度运动。
可以定点运动,直线插补和圆弧插补。
1.引言
运动控制的应用在国内已有十几年的历史,技术也相当成熟。
通常运动控制都需要用到运动控制卡,运动控制器等产品,但这些产品价格高昂,使用复杂,也不适合由单片机构成的控制系统。
而如果直接采用单片机来做运动控制,由于运动控制对系统性能要求非常高,单片机速度资源有限,难以设计出性能优良的运动控制模型。
因此,本文采用单片机和专业的mpc微型运动控制模块构成运动控制系统。
MPC微型运动控制模块采用新型FPGA设计,集成实用运动控制功能,可与普通单片机通过串口通讯对步进电机和伺服电机控制。
具有如下特点:
◆串口通讯,仅需使用几条指令,简单可靠。
◆单模块最高六轴输出,多个模块组网工作可达120轴。
◆最大脉冲输出频率为2MHz,脉冲输出使用脉冲+方向方式。
◆最高六轴独立运动控制,任意两轴直线插补,任意两轴圆弧插补。
◆每轴一路硬件回原点。
◆模块带1000级指令缓存深度,指令先进先出。
◆模块体积小巧,仅3.5*2.5*1.5cm,双排直插30脚。
2,系统硬件设计
硬件系统由四部分构成:
(1)单片机部分
单片机与模块只需三根线连接,用作串口通讯的RXD和TXD,用作模块缓存满输出的BUSY信号。
P3.7引出一按键作为测试使用。
(2)mpc微型运动控制模块部分
mpc微型运动控制模块采用5V电源供电,RXD,TXD,BUSY与单片机连接。
X0,X1,X2可作为三路电机的原点信号,P1,D1为1轴的脉冲和方向信号。
P2,D2为2轴的脉冲和方向信号。
P3,D3为3轴的脉冲和方向信号。
(3)原点信号输入部分
原点采用光藕隔离输入,输入端可接NPN型光电开关来作为原点信号。
(4)信号输出部分
输出采用NPN晶体管极电极开路输出,分别接到电机驱动器脉冲和方向信号输入端。
3,系统软件设计
MPC微型运动控制模块与单片机串口通讯速率为115200bps,数据位为8位,停止位0位,无校验。
使用函数前先设置好单片机的串口功能,并将需要用到的函数的原型拷贝到当前程序内。
试验程序如下:
#include
//-----STC89C2051-------
sfrIPH=0XB7;
sfrCCON=0XD8;
sfrCMOD=0XD9;
sfrCL=0XE9;
sfrCH=0XF9;
sfrCCAP0L=0XEA;
sfrCCAP0H=0XFA;
sfrCCAPM0=0XDA;
sfrCCAPM1=0XDB;
sfrP3M1=0XB1;
sfrP3M0=0XB2;
sfrP1M1=0X91;
sfrP1M0=0X92;
sfrWAKE_CLKO=0X8f;
sfrBRT=0x9c;
sfrAUXR=0x8E;
sfrAUXR1=0xA2;
sfrWDT_CONTR=0xc1;
sfrT2MOD=0xC9;
//////////////////
sbitbusy=P3^2;
sbits1=P3^7;
voidinitial()
{
P3M1=0x00;
P3M0=0x80;
P1M1=0x00;
P1M0=0xf9;
}
voidinit_uart()
PCON&=0x7f;//波特率不倍速
SCON=0x50;//8位数据,可变波特率
BRT=0xFD;//设定独立波特率发生器重装值波特率115200bps
AUXR|=0x04;//独立波特率发生器时钟为Fosc,即1T
AUXR|=0x01;//串口1选择独立波特率发生器为波特率发生器
AUXR|=0x10;//启动独立波特率发生器
/*
串口发送一个字节,需根据所使用的单片机作适当更改。
*/
voidUSART_Txbyte(unsignedchari)
SBUF=i;
while(TI==0);//等待发送完成
TI=0;//清零串口发送完成中断请求标志
串口接收模块返回的10个字节数据,需根据所使用的单片机作适当更改。
voidreceive(unsignedchar*buf)
unsignedchari;
for(i=0;i<10;i++)
while(RI==0);
RI=0;
buf[i]=SBUF;
串口发送一串数据。
voidUSRAT_transmit(unsignedchar*fdata,unsignedcharlen)
for(i=0;i{USART_Txbyte(fdata[i]);}} /*函数名:inp_move功能:二轴直线插补参数:cardno卡号no1X轴轴号no2Y轴轴号pulse1,pulse2X-Y轴移动的距离,范围(-8388608~+8388607)mode0:相对坐标1:绝对坐标返回值:0失败1成功*/unsignedcharinp_move(unsignedcharcardno,unsignedcharno1,unsignedcharno2,longpulse1,longpulse2,unsignedcharmode){unsignedcharOutByte[25];unsignedcharinbuf[12];OutByte[0]=0x68;OutByte[1]=0x0F;OutByte[2]=cardno;OutByte[3]=0x7;OutByte[4]=no1;OutByte[5]=no2;OutByte[6]=pulse1>>24;OutByte[7]=pulse1>>16;OutByte[8]=pulse1>>8;OutByte[9]=pulse1;OutByte[10]=pulse2>>24;OutByte[11]=pulse2>>16;OutByte[12]=pulse2>>8;OutByte[13]=pulse2;OutByte[14]=mode;OutByte[15]=OutByte[1]+OutByte[2]+OutByte[3]+OutByte[4]+OutByte[5]+OutByte[6]+OutByte[7]+OutByte[8]+OutByte[9]+OutByte[10]+OutByte[11]+\OutByte[12]+OutByte[13]+OutByte[14];USRAT_transmit(OutByte,16);receive(inbuf);return1;} /*函数名:inp_arc功能:二轴圆弧插补参数:cardno卡号no1参与插补X轴的轴号no2参与插补Y轴的轴号x,y圆弧插补的终点位置(相对于起点),范围(-8388608~+8388607)i,j圆弧插补的圆心点位置(相对于起点),范围(-8388608~+8388607)mode0:顺时针插补1:逆时针插补返回值:0失败1成功*/unsignedcharinp_arc(unsignedcharcardno,unsignedcharno1,unsignedcharno2,longX,longy,longi,longj,unsignedcharmode){unsignedcharOutByte[25];unsignedcharinbuf[12];OutByte[0]=0x68;OutByte[1]=0x17;OutByte[2]=cardno;OutByte[3]=0x7;OutByte[4]=no1;OutByte[5]=no2;OutByte[6]=X>>24;OutByte[7]=X>>16;OutByte[8]=X>>8;OutByte[9]=X;OutByte[10]=y>>24;OutByte[11]=y>>16;OutByte[12]=y>>8;OutByte[13]=y;OutByte[14]=i>>24;OutByte[15]=i>>16;OutByte[16]=i>>8;OutByte[17]=i;OutByte[18]=j>>24;OutByte[19]=j>>16;OutByte[20]=j>>8;OutByte[21]=j;OutByte[22]=mode;OutByte[23]=OutByte[1]+OutByte[2]+OutByte[3]+OutByte[4]+OutByte[5]+OutByte[6]+OutByte[7]+OutByte[8]+OutByte[9]+OutByte[10]+OutByte[11]+\OutByte[12]+OutByte[13]+OutByte[14]+OutByte[15]+OutByte[16]+OutByte[17]+OutByte[18]+OutByte[19]+OutByte[20]+OutByte[21]+OutByte[22];USRAT_transmit(OutByte,24);receive(inbuf);return1;}/*函数名:set_speed功能:设置轴速度参数:cardno卡号axis轴号(1-6)acc加速时间(ms)dec减速时间(ms)startv启动频率(*100Hz)speed运行频率(*100Hz)返回值:0失败1成功*/unsignedcharset_speed(unsignedcharcardno,unsignedcharaxis,unsignedintacc,unsignedintdec,unsignedintstartv,unsignedintspeed){unsignedcharOutByte[25];unsignedcharinbuf[12];OutByte[0]=0x68;OutByte[1]=0xD;OutByte[2]=cardno;OutByte[3]=1;OutByte[4]=axis;OutByte[5]=acc>>8;OutByte[6]=acc;OutByte[7]=dec>>8;OutByte[8]=dec;OutByte[9]=startv>>8;OutByte[10]=startv;OutByte[11]=speed>>8;OutByte[12]=speed;OutByte[13]=OutByte[1]+OutByte[2]+OutByte[3]+OutByte[4]+OutByte[5]+OutByte[6]+OutByte[7]+OutByte[8]+OutByte[9]+OutByte[10]+OutByte[11]+OutByte[12];USRAT_transmit(OutByte,14);receive(inbuf);return1;}/*函数名:set_soft_limit功能:设置轴软件限位参数:cardno卡号axis轴号(1-6)pulse1负方向限位脉冲值,范围(-8388608~0)pulse2正方向限位脉冲值,范围(0~+8388607)返回值:0失败1成功*/unsignedcharset_soft_limit(unsignedcharcardno,unsignedcharaxis,unsignedcharmode,longpulse1,longpulse2){unsignedcharOutByte[25];unsignedcharinbuf[12];OutByte[0]=0x68;OutByte[1]=0xE;OutByte[2]=cardno;OutByte[3]=0x13;OutByte[4]=axis;OutByte[5]=mode;OutByte[6]=pulse1>>24;OutByte[7]=pulse1>>16;OutByte[8]=pulse1>>8;OutByte[9]=pulse1;OutByte[10]=pulse2>>24;OutByte[11]=pulse2>>16;OutByte[12]=pulse2>>8;OutByte[13]=pulse2;OutByte[14]=OutByte[1]+OutByte[2]+OutByte[3]+OutByte[4]+OutByte[5]+OutByte[6]+OutByte[7]+OutByte[8]+OutByte[9]+OutByte[10]+OutByte[11]+OutByte[12]+OutByte[13];USRAT_transmit(OutByte,15);receive(inbuf);return1;} /*函数名:pmove功能:单轴运行参数:cardno卡号axis轴号(1-6)pulse输出的脉冲数>0:正方向移动<0:负方向移动范围(-268435455~+268435455)mode0:相对位置1:绝对位置返回值:0失败1成功*/unsignedcharpmove(unsignedcharcardno,unsignedcharaxis,longpulse,unsignedcharmode){unsignedcharOutByte[25];unsignedcharinbuf[12];OutByte[0]=0x68;OutByte[1]=0xA;OutByte[2]=cardno;OutByte[3]=2;OutByte[4]=axis;OutByte[5]=pulse>>24;OutByte[6]=pulse>>16;OutByte[7]=pulse>>8;OutByte[8]=pulse;OutByte[9]=mode;OutByte[10]=OutByte[1]+OutByte[2]+OutByte[3]+OutByte[4]+OutByte[5]+OutByte[6]+OutByte[7]+OutByte[8]+OutByte[9];USRAT_transmit(OutByte,11);receive(inbuf);return1;} /*函数名:wait_delay功能:等待延时数参数:cardno卡号value延时量(1-10000)MS返回值:0失败1成功*/unsignedcharwait_delay(unsignedcharcardno,unsignedintvalue){unsignedcharOutByte[25];unsignedcharinbuf[12];OutByte[0]=0x68;OutByte[1]=0x6;OutByte[2]=cardno;OutByte[3]=0xE;OutByte[4]=value>>8;OutByte[5]=value;OutByte[6]=OutByte[1]+OutByte[2]+OutByte[3]+OutByte[4]+OutByte[5];USRAT_transmit(OutByte,7);receive(inbuf);return1;}/*函数名:set_command_pos功能:设置轴逻辑位置参数:cardno卡号axis轴号(1-6)pulse位置脉冲数,范围(-268435455~+268435455)返回值:0失败1成功*/unsignedcharset_command_pos(unsignedcharcardno,unsignedcharaxis,longvalue){unsignedcharOutByte[25];unsignedcharinbuf[12];OutByte[0]=0x68;OutByte[1]=0x9;OutByte[2]=cardno;OutByte[3]=0x12;OutByte[4]=axis;OutByte[5]=value>>24;OutByte[6]=value>>16;OutByte[7]=value>>8;OutByte[8]=value;OutByte[9]=OutByte[1]+OutByte[2]+OutByte[3]+OutByte[4]+OutByte[5]+OutByte[6]+OutByte[7]+OutByte[8];USRAT_transmit(OutByte,10);receive(inbuf);return1;} /*函数名:wait_pulse功能:等待轴脉冲数参数:cardno卡号axis轴号(1-6)pulse位置脉冲数,范围(-268435455~+268435455)返回值:0失败1成功*/unsignedcharwait_pulse(unsignedcharcardno,unsignedcharaxis,longvalue){unsignedcharOutByte[25];unsignedcharinbuf[12];OutByte[0]=0x68;OutByte[1]=0x9;OutByte[2]=cardno;OutByte[3]=0x19;OutByte[4]=axis;OutByte[5]=value>>24;OutByte[6]=value>>16;OutByte[7]=value>>8;OutByte[8]=value;OutByte[9]=OutByte[1]+OutByte[2]+OutByte[3]+OutByte[4]+OutByte[5]+OutByte[6]+OutByte[7]+OutByte[8];USRAT_transmit(OutByte,10);receive(inbuf);return1;} /*函数名:write_bit功能:写输出口状态参数:cardno卡号number端口号(0-8)Y0-Y8value状态(0,1)0输出低电平1输出高电平返回值:0失败1成功*/unsignedcharwrite_bit(unsignedcharcardno,unsignedcharnumber,unsignedcharvalue){unsignedcharOutByte[25];unsignedcharinbuf[12];OutByte[0]=0x68;OutByte[1]=0x6;OutByte[2]=cardno;OutByte[3]=3;OutByte[4]=number;OutByte[5]=value;OutByte[6]=OutByte[1]+OutByte[2]+OutByte[3]+OutByte[4]+OutByte[5];USRAT_transmit(OutByte,7);receive(inbuf);return1;} /*函数名:sudden_stop功能:轴停止参数:cardno卡号axis停止的轴号(1-6)返回值:0失败1成功*/unsignedcharsudden_stop(unsignedcharcardno,unsignedcharaxis){unsignedcharOutByte[25];unsignedcharinbuf[12];OutByte[0]=0x68;OutByte[1]=0x5;OutByte[2]=cardno;OutByte[3]=0x17;OutByte[4]=axis;OutByte[5]=OutByte[1]+OutByte[2]+OutByte[3]+OutByte[4];USRAT_transmit(OutByte,6);receive(inbuf);return1;} /*函数名:wait_in功能:等待输入口状态参数:cardno卡号number端口号(0-6)X0-X6value状态(0,1)0输入低电平1输入高电平返回值:0失败1成功*/unsignedcharwait_in(unsignedcharcardno,unsignedcharnumber,unsignedch
USART_Txbyte(fdata[i]);
函数名:
inp_move
功能:
二轴直线插补
参数:
cardno卡号
no1X轴轴号
no2Y轴轴号
pulse1,pulse2X-Y轴移动的距离,范围(-8388608~+8388607)
mode0:
相对坐标1:
绝对坐标
返回值:
0失败1成功
unsignedcharinp_move(unsignedcharcardno,unsignedcharno1,unsignedcharno2,longpulse1,longpulse2,unsignedcharmode)
unsignedcharOutByte[25];
unsignedcharinbuf[12];
OutByte[0]=0x68;
OutByte[1]=0x0F;
OutByte[2]=cardno;
OutByte[3]=0x7;
OutByte[4]=no1;
OutByte[5]=no2;
OutByte[6]=pulse1>>24;
OutByte[7]=pulse1>>16;
OutByte[8]=pulse1>>8;
OutByte[9]=pulse1;
OutByte[10]=pulse2>>24;
OutByte[11]=pulse2>>16;
OutByte[12]=pulse2>>8;
OutByte[13]=pulse2;
OutByte[14]=mode;
OutByte[15]=OutByte[1]+OutByte[2]+OutByte[3]+OutByte[4]+OutByte[5]+OutByte[6]+OutByte[7]+OutByte[8]+OutByte[9]+OutByte[10]+OutByte[11]+\
OutByte[12]+OutByte[13]+OutByte[14];
USRAT_transmit(OutByte,16);
receive(inbuf);
return1;
inp_arc
二轴圆弧插补
no1参与插补X轴的轴号
no2参与插补Y轴的轴号
x,y圆弧插补的终点位置(相对于起点),范围(-8388608~+8388607)
i,j圆弧插补的圆心点位置(相对于起点),范围(-8388608~+8388607)
顺时针插补1:
逆时针插补
unsignedcharinp_arc(unsignedcharcardno,unsignedcharno1,unsignedcharno2,longX,longy,longi,longj,unsignedcharmode)
OutByte[1]=0x17;
OutByte[6]=X>>24;
OutByte[7]=X>>16;
OutByte[8]=X>>8;
OutByte[9]=X;
OutByte[10]=y>>24;
OutByte[11]=y>>16;
OutByte[12]=y>>8;
OutByte[13]=y;
OutByte[14]=i>>24;
OutByte[15]=i>>16;
OutByte[16]=i>>8;
OutByte[17]=i;
OutByte[18]=j>>24;
OutByte[19]=j>>16;
OutByte[20]=j>>8;
OutByte[21]=j;
OutByte[22]=mode;
OutByte[23]=OutByte[1]+OutByte[2]+OutByte[3]+OutByte[4]+OutByte[5]+OutByte[6]+OutByte[7]+OutByte[8]+OutByte[9]+OutByte[10]+OutByte[11]+\
OutByte[12]+OutByte[13]+OutByte[14]+OutByte[15]+OutByte[16]+OutByte[17]+OutByte[18]+OutByte[19]+OutByte[20]+OutByte[21]+OutByte[22];
USRAT_transmit(OutByte,24);
set_speed
设置轴速度
axis轴号(1-6)
acc加速时间(ms)
dec减速时间(ms)
startv启动频率(*100Hz)
speed运行频率(*100Hz)
unsignedcharset_speed(unsignedcharcardno,unsignedcharaxis,unsignedintacc,unsignedintdec,unsignedintstartv,unsignedintspeed)
OutByte[1]=0xD;
OutByte[3]=1;
OutByte[4]=axis;
OutByte[5]=acc>>8;
OutByte[6]=acc;
OutByte[7]=dec>>8;
OutByte[8]=dec;
OutByte[9]=startv>>8;
OutByte[10]=startv;
OutByte[11]=speed>>8;
OutByte[12]=speed;
OutByte[13]=OutByte[1]+OutByte[2]+OutByte[3]+OutByte[4]+OutByte[5]+OutByte[6]+OutByte[7]+OutByte[8]+OutByte[9]+OutByte[10]+OutByte[11]+
OutByte[12];
USRAT_transmit(OutByte,14);
set_soft_limit
设置轴软件限位
pulse1负方向限位脉冲值,范围(-8388608~0)
pulse2正方向限位脉冲值,范围(0~+8388607)
unsignedcharset_soft_limit(unsignedcharcardno,unsignedcharaxis,unsignedcharmode,longpulse1,longpulse2)
OutByte[1]=0xE;
OutByte[3]=0x13;
OutByte[5]=mode;
OutByte[14]=OutByte[1]+OutByte[2]+OutByte[3]+OutByte[4]+OutByte[5]+OutByte[6]+OutByte[7]+OutByte[8]+OutByte[9]+OutByte[10]+OutByte[11]+
OutByte[12]+OutByte[13];
USRAT_transmit(OutByte,15);
pmove
单轴运行
pulse输出的脉冲数>0:
正方向移动<0:
负方向移动范围(-268435455~+268435455)
相对位置1:
绝对位置
unsignedcharpmove(unsignedcharcardno,unsignedcharaxis,longpulse,unsignedcharmode)
OutByte[1]=0xA;
OutByte[3]=2;
OutByte[5]=pulse>>24;
OutByte[6]=pulse>>16;
OutByte[7]=pulse>>8;
OutByte[8]=pulse;
OutByte[9]=mode;
OutByte[10]=OutByte[1]+OutByte[2]+OutByte[3]+OutByte[4]+OutByte[5]+OutByte[6]+OutByte[7]+OutByte[8]+OutByte[9];
USRAT_transmit(OutByte,11);
wait_delay
等待延时数
value延时量(1-10000)MS
unsignedcharwait_delay(unsignedcharcardno,unsignedintvalue)
OutByte[1]=0x6;
OutByte[3]=0xE;
OutByte[4]=value>>8;
OutByte[5]=value;
OutByte[6]=OutByte[1]+OutByte[2]+OutByte[3]+OutByte[4]+OutByte[5];
USRAT_transmit(OutByte,7);
set_command_pos
设置轴逻辑位置
pulse位置脉冲数,范围(-268435455~+268435455)
unsignedcharset_command_pos(unsignedcharcardno,unsignedcharaxis,longvalue)
OutByte[1]=0x9;
OutByte[3]=0x12;
OutByte[5]=value>>24;
OutByte[6]=value>>16;
OutByte[7]=value>>8;
OutByte[8]=value;
OutByte[9]=OutByte[1]+OutByte[2]+OutByte[3]+OutByte[4]+OutByte[5]+OutByte[6]+OutByte[7]+OutByte[8];
USRAT_transmit(OutByte,10);
wait_pulse
等待轴脉冲数
unsignedcharwait_pulse(unsignedcharcardno,unsignedcharaxis,longvalue)
OutByte[3]=0x19;
write_bit
写输出口状态
number端口号(0-8)Y0-Y8
value状态(0,1)0输出低电平1输出高电平
unsignedcharwrite_bit(unsignedcharcardno,unsignedcharnumber,unsignedcharvalue)
OutByte[3]=3;
OutByte[4]=number;
sudden_stop
轴停止
axis停止的轴号(1-6)
unsignedcharsudden_stop(unsignedcharcardno,unsignedcharaxis)
OutByte[1]=0x5;
OutByte[3]=0x17;
OutByte[5]=OutByte[1]+OutByte[2]+OutByte[3]+OutByte[4];
USRAT_transmit(OutByte,6);
wait_in
等待输入口状态
number端口号(0-6)X0-X6
value状态(0,1)0输入低电平1输入高电平
unsignedcharwait_in(unsignedcharcardno,unsignedcharnumber,unsignedch
copyright@ 2008-2023 冰点文库 网站版权所有
经营许可证编号:鄂ICP备19020893号-2