OpenVINOTM,给你看得见的未来!>>
电子产品世界 » 论坛首页 » 综合技术 » 工业控制与自动化 » 51单片机高速六轴运动控制带直线圆弧插补电路与代码

共1条 1/1 1 跳转至

51单片机高速六轴运动控制带直线圆弧插补电路与代码

菜鸟
2014-12-24 15:00:23    评分
 

51单片机高速六轴运动控制带直线圆弧插补电路与代码

 1.引言

采用单片机stc89C2051mpc006微型运动控制模块作为控制系统的核心,控制六路步进电机做运动控制。单片机发送指令给mpc006微型运动控制模块,模块信号输出给步进驱动器作高速度运动。可以定点运动,直线插补和圆弧插补。

2,系统硬件设计

硬件系统由四部分构成:

(1)       单片机部分

单片机与模块只需三根线连接,用作串口通讯的RXDTXD,用作模块缓存满输出的BUSY信号。P3.7引出一按键作为测试使用。

(2)       mpc006微型运动控制模块部分

mpc006微型运动控制模块采用5V电源供电,RXD,TXDBUSY与单片机连接。X0X1X2可作为三路电机的原点信号,P1D11轴的脉冲和方向信号。P2D22轴的脉冲和方向信号。P3D33轴的脉冲和方向信号。

3原点信号输入部分

原点采用光藕隔离输入,输入端可接NPN型光电开关来作为原点信号。

(4)       信号输出部分

输出采用NPN晶体管极电极开路输出,分别接到电机驱动器脉冲和方向信号输入端。

3,系统软件设计

MPC006微型运动控制模块与单片机串口通讯速率为115200bps,数据位为8位,停止位0位,无校验。

单片机与mpc006运动控制模块采用串口应答式通讯,单片机作主机。单片机每发送一条指令给mpc006运动控制模块,mpc006运动控制模块返回固定长度为10个字节的数据串。单片机可以取出需要的数据。只有接收到mpc006运动控制模块返回的数据后单片机才能发送下一条指令。

使用函数前先设置好单片机的串口功能,并将需要用到的函数的原型拷贝到当前程序内。本文所使用的函数原型为基础版本,已将各指令通讯过程描述出来。用户可根据所使用单片机的资源在保证通讯格式正确的情况下作出适当优化。

试验程序如下:

#include <reg52.h>

//-----STC89C2051-------

sfr IPH           =0XB7;         

sfr   CCON     =0XD8;

sfr   CMOD    =0XD9;

sfr   CL          =0XE9;

sfr   CH          =0XF9;

sfr   CCAP0L  =0XEA;

sfr   CCAP0H =0XFA;

sfr   CCAPM0 =0XDA;

sfr   CCAPM1 =0XDB;

sfr P3M1= 0XB1;

sfr P3M0= 0XB2;

sfr P1M1= 0X91;

sfr P1M0= 0X92;

sfr WAKE_CLKO= 0X8f;

sfr BRT     =0x9c;

sfr AUXR     =0x8E;

sfr AUXR1   = 0xA2;    

sfr WDT_CONTR = 0xc1;          

sfr T2MOD  = 0xC9;            

//////////////////

sbit busy = P3^2;

sbit s1     =     P3^7;

 void initial()

{

       P3M1 = 0x00; 

       P3M0 = 0x80;

       P1M1 = 0x00; 

       P1M0 = 0xf9;                                   

}

 void init_uart()

{

  PCON &= 0x7f;  //波特率不倍速

   SCON = 0x50;  //8位数据,可变波特率

   BRT = 0xFD;      //设定独立波特率发生器重装值 波特率115200bps

   AUXR |= 0x04;  //独立波特率发生器时钟为Fosc,1T

   AUXR |= 0x01;  //串口1选择独立波特率发生器为波特率发生器

   AUXR |= 0x10;  //启动独立波特率发生器

}

/*

串口发送一个字节,需根据所使用的单片机作适当更改。

*/

 void USART_Txbyte(unsigned char i)

{

    SBUF   =   i;

    while(TI ==0); //等待发送完成

    TI     =   0;  //清零串口发送完成中断请求标志

}

/*

串口接收模块返回的10个字节数据,需根据所使用的单片机作适当更改。

*/

 void receive(unsigned char *buf)

{

unsigned char i;

for(i=0;i<10;i++)

   {

   while(RI==0);

     RI=0;

   buf[i]=SBUF;

   }

}

/*

串口发送一串数据。

*/

void USRAT_transmit(unsigned char *fdata,unsigned char len)

{

   unsigned char i;                                                                            

               

   for(i=0;i<len;i++)

   {

      USART_Txbyte(fdata[i]);        

   }

  

}    

/*

函数名:   inp_move

功能:二轴直线插补

参数:

cardno    卡号

no1   X轴轴号

no2   Y轴轴号

pulse1,pulse2                       X-Y轴移动的距离,范围(-8388608~+8388607

mode  0:相对坐标  1:绝对坐标

返回值:

0 失败           1  成功

*/

unsigned char inp_move(unsigned char cardno,unsigned char no1 ,unsigned char no2 , long pulse1  ,long pulse2 ,unsigned char mode )

{

unsigned char OutByte[25];

unsigned char inbuf[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);

return 1;

}

/*

函数名: inp_arc

功能:二轴圆弧插补

参数:

cardno    卡号

no1         参与插补X轴的轴号

no2         参与插补Y轴的轴号

x,y          圆弧插补的终点位置(相对于起点),范围(-8388608~+8388607                      

i,j            圆弧插补的圆心点位置(相对于起点),范围(-8388608~+8388607

mode      0:顺时针插补   1:逆时针插补

返回值:

0 失败           1  成功

*/

unsigned char inp_arc(unsigned char cardno ,unsigned char no1,unsigned char no2, long X , long y, long i, long j,unsigned char mode )

{

unsigned char OutByte[25];

unsigned char inbuf[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);

return 1;

}

/*

函数名:  set_speed

功能:设置轴速度

参数:

cardno    卡号

axis  轴号(1-6)

acc     加速时间(ms

dec     减速时间(ms

startv     启动频率为:值*频率倍率(Hz

speed      运行频率为:值*频率倍率(Hz

range      频率倍率(1-100

返回值:

0 失败           1  成功

*/

unsigned char set_speed(unsigned char cardno ,unsigned char axis ,unsigned int acc  ,unsigned int dec  ,unsigned int startv ,unsigned int speed ,unsigned char range)

{

unsigned char OutByte[25];

unsigned char inbuf[12];

OutByte[0] = 0x68;

OutByte[1] = 0xe;

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] = range;

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);

return 1;

}

/*

函数名: set_soft_limit

功能:设置轴软件限位

参数:

cardno    卡号

axis         轴号(1-6)

pulse1            负方向限位脉冲值,范围(-8388608~0                           

pulse2            正方向限位脉冲值,范围(0~+8388607

返回值:

0 失败           1  成功

*/

unsigned char set_soft_limit(unsigned char cardno ,unsigned char axis ,unsigned char mode, long pulse1 , long pulse2 )

{

unsigned char OutByte[25];

unsigned char inbuf[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);

return 1;

}

/*

函数名: pmove

功能:单轴运行

参数:

cardno    卡号

axis         轴号(1-6)

pulse      输出的脉冲数 >0:正方向移动       <0:负方向移动   范围(-268435455~+268435455

mode      0:相对位置   1:绝对位置

返回值:

0 失败           1  成功

*/

unsigned char pmove(unsigned char cardno ,unsigned char axis,long pulse , unsigned char mode)

{

unsigned char OutByte[25];

unsigned char inbuf[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);

return 1;

}

/*

函数名: wait_delay

功能:等待延时数

参数:

cardno    卡号

value     延时量(1-10000MS

返回值:

0 失败           1  成功

*/

unsigned char wait_delay(unsigned char cardno ,unsigned int value)

{

unsigned char OutByte[25];

unsigned char inbuf[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);

return 1;

}

/*

函数名:   set_command_pos

功能: 设置轴逻辑位置

参数:

cardno    卡号

axis  轴号(1-6)

pulse      位置脉冲数,范围(-268435455~+268435455

返回值:

0 失败           1  成功

*/

unsigned char set_command_pos(unsigned char cardno ,unsigned char axis, long value )

{

unsigned char OutByte[25];

unsigned char inbuf[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);

return 1;

}

 /*

函数名: wait_pulse

功能:等待轴脉冲数

参数:

cardno    卡号

axis  轴号(1-6)

pulse      位置脉冲数,范围(-268435455~+268435455

返回值:

0 失败           1  成功

*/

unsigned char wait_pulse(unsigned char cardno ,unsigned char axis, long value )

{

unsigned char OutByte[25];

unsigned char inbuf[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);

return 1;

}

/*

函数名: write_bit

功能:写输出口状态

参数:

cardno    卡号

number  端口号(0-8  Y0-Y8

value   状态(0,1 0 输出低电平   1 输出高电平

返回值:

0 失败           1  成功

*/

unsigned char write_bit(unsigned char cardno , unsigned char number, unsigned char value)

{

unsigned char OutByte[25];

unsigned char inbuf[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);

return 1;

}

/*

函数名: sudden_stop

功能: 轴停止

参数:

cardno    卡号

axis  停止的轴号(1-6

返回值:

0 失败           1  成功

*/

unsigned char sudden_stop(unsigned char cardno ,unsigned char axis)

{

unsigned char OutByte[25];

unsigned char inbuf[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);

return 1;

}

/*

函数名: wait_in

功能: 等待输入口状态

参数:

cardno    卡号

number  端口号(0-6  X0-X6

value   状态(0,1 0 输入低电平   1 输入高电平

返回值:

0 失败           1  成功

*/

unsigned char wait_in( unsigned char cardno, unsigned char number, unsigned char value)

{

unsigned char OutByte[25];

unsigned char inbuf[12];

OutByte[0] = 0x68 ;

OutByte[1] = 0x6  ;

OutByte[2] = cardno;

OutByte[3] = 0xF  ;

OutByte[4] = number ;

OutByte[5] = value ;

OutByte[6] =OutByte[1] +OutByte[2] +OutByte[3] +OutByte[4] +OutByte[5];

USRAT_transmit(OutByte,7);

receive(inbuf);

return 1;

}

/*

函数名: wait_stop

功能:等待轴停止

参数:

cardno    卡号

axis  需要停止的轴号

返回值:

0 失败           1  成功

*/

unsigned char wait_stop(unsigned char cardno ,unsigned char axis)

{

unsigned char OutByte[25];

unsigned char inbuf[12];

OutByte[0] = 0x68  ;

OutByte[1] = 0x5 ;

OutByte[2] = cardno ;

OutByte[3] = 9       ;

OutByte[4] = axis ;

OutByte[5] =OutByte[1] +OutByte[2] +OutByte[3] +OutByte[4];

USRAT_transmit(OutByte,6);

receive(inbuf);

return 1;

}

/*

函数名:  get_number

功能:获取唯一序列号

参数:

cardno    卡号

返回值: 32位序列号

*/

unsigned long get_number(unsigned char cardno )

{

unsigned char OutByte[25];

unsigned char inbuf[12];

OutByte[0] = 0x68 ;

OutByte[1] = 0x4 ;

OutByte[2] = cardno;

OutByte[3] = 0xC ;

OutByte[4] =OutByte[1] +OutByte[2] +OutByte[3] ;

USRAT_transmit(OutByte,5);

receive(inbuf);

return (unsigned long)inbuf[4]<<24+(unsigned long)inbuf[5]<<16+(unsigned long)inbuf[6]<<8+(unsigned long)inbuf[7];

}

/*

函数名: get_status

功能:获取各轴工作状态

参数:

cardno    卡号

返回值: 8位二进制,1-6位分别代表1-6轴状态,第7位为直线插补状态,第8位为圆弧插补状态。0表示停止中,1表示运行中。

*/

unsigned char get_status( unsigned char cardno)

{

unsigned char OutByte[25];

unsigned char inbuf[12];

OutByte[0] = 0x68  ;

OutByte[1] = 0x4  ;

OutByte[2] = cardno ;

OutByte[3] = 5 ;

OutByte[4] =OutByte[1] +OutByte[2] +OutByte[3] ;

USRAT_transmit(OutByte,5);

receive(inbuf);

return inbuf[4];

}

/*

函数名: get_command_pos

功能: 获取轴逻辑位置

参数:

cardno    卡号

axis  轴号

返回值:      位置脉冲数,范围(-268435455~+268435455

*/

unsigned long  get_command_pos( unsigned char cardno, unsigned char axis)

{

unsigned char OutByte[25];

unsigned char inbuf[12];

OutByte[0] = 0x68  ;

OutByte[1] = 0x5  ;

OutByte[2] = cardno  ;

OutByte[3] = 6       ;

OutByte[4] = axis ;

OutByte[5] =OutByte[1] +OutByte[2] +OutByte[3] +OutByte[4] ;

USRAT_transmit(OutByte,6);

receive(inbuf);

return (unsigned long)inbuf[5]<<24+(unsigned long)inbuf[6]<<16+(unsigned long)inbuf[7]<<8+(unsigned long)inbuf[8];

}

/*

函数名: set_cardno

功能:设置卡号

参数:

cardno    卡号(1-255

返回值:

0 失败           1  成功

*/

unsigned char set_cardno(unsigned char cardno)

{

unsigned char OutByte[25];

unsigned char inbuf[12];

OutByte[0] = 0x68  ;

OutByte[1] = 5       ;

OutByte[2] = 0         ;

OutByte[3] = 0xFA ;

OutByte[4] = cardno ;

OutByte[5] =OutByte[1] +OutByte[2] +OutByte[3] +OutByte[4] ;

USRAT_transmit(OutByte,6);

receive(inbuf);

return 1;

}

void main(void) 

{

   initial();

   init_uart();

   set_cardno(1);                //设卡号为1

   while(1)

        {

              

        if(!s1)//按键按下

          {

           set_speed(1 ,1,1000,1000,10,200,100);         // 1轴速度

        set_speed(1 ,2,1000,1000,10,200,100);         // 2轴速度

        set_speed(1 ,3,1000,1000,10,200,100);         // 3轴速度

         /*1轴回原点*/

         pmove(1,1,-1000000,0);               //  1轴运动

         wait_in(1,0,1);                            //   等待X0为高

         sudden_stop(1,1);                      //      1轴停止

        set_command_pos(1,1,0);            //        1轴此时坐标为0

              pmove(1,2,3200,0);                //  2轴运动

              pmove(1,3,-3200,0);               //  3轴运动    

            while(!s1);

          }

        

        

        }

      

}

本文来自泰安迪科技官网:www.mpc000.com   转载请注明出处。




关键词: mpc006     运动控制    

共1条 1/1 1 跳转至

回复

匿名不能发帖!请先 [ 登陆 注册 ]