基于串口运动控制器的VB代码
1.引言
采用PC和mpc014微型运动控制器作为控制系统的核心,控制三路步进电机或伺服电机。PC发送指令给mpc014微型运动控制器,控制器信号输出给步进驱动器作高速运动。可以定点运动,直线插补和圆弧插补,并能实时作逻辑处理。
2,系统硬件架构
硬件系统由三部分构成,如图所示:
(1) PC
(2) mpc014微型运动控制器
(3) 电气机械传动部分
mpc014微型运动控制模块采用24V电源供电,RS485通讯口A,B与PC通过USB转485转换器连接。X0,X1,X2可作为三路电机的原点信号,Y3,Y4为第1轴的脉冲和方向信号。Y5,Y6为第2轴的脉冲和方向信号。Y7,Y8为第3轴的脉冲和方向信号。
3,系统软件设计
PC端软件采用Visual Basic 6.0设计,操作系统为Microsoft Windows XP,首先将从官网下载的动态链接库mpc.dll拷入系统文件夹,然后在模块中对函数作声明。如下:
Public Declare Function inp_move Lib "mpc" (cardno As Byte, no1 As Byte, no2 As Byte, pulse1 As Long, pulse2 As Long, mode As Byte) As Byte '二轴直线插补
Public Declare Function inp_arc Lib "mpc" (cardno As Byte, no1 As Byte, no2 As Byte, X As Long, y As Long, i As Long, j As Long, mode As Byte) As Byte '二轴圆弧插补
Public Declare Function set_speed Lib "mpc" (cardno As Byte, axis As Byte, acc As Long, dec As Long, startv As Long, speed As Long) As Byte '设置轴速度
Public Declare Function set_soft_limit Lib "mpc" (cardno As Byte, axis As Byte, mode As Byte, pulse1 As Long, pulse2 As Long) As Byte '设置轴软件限位
Public Declare Function pmove Lib "mpc" (cardno As Byte, axis As Byte, pulse As Long, mode As Byte) As Byte '单轴运行
Public Declare Function wait_delay Lib "mpc" (cardno As Byte, value As Integer) As Byte '等待延时数
Public Declare Function set_command_pos Lib "mpc" (cardno As Byte, axis As Byte, value As Long) As Byte '设置轴逻辑位置
Public Declare Function wait_pulse Lib "mpc" (cardno As Byte, axis As Byte, value As Long) As Byte '等待轴脉冲数
Public Declare Function write_bit Lib "mpc" (cardno As Byte, number As Byte, value As Byte) As Byte '写输出口状态
Public Declare Function sudden_stop Lib "mpc" (cardno As Byte, axis As Byte) As Byte '轴停止
Public Declare Function wait_in Lib "mpc" (cardno As Byte, number As Byte, value As Byte) As Byte '等待输入口状态
Public Declare Function read_bit Lib "mpc" (cardno As Byte, number As Byte) As Byte '读取输入口状态
Public Declare Function get_out Lib "mpc" (cardno As Byte, number As Byte) As Byte '获取输出口状态
Public Declare Function wait_stop Lib "mpc" (cardno As Byte, axis As Byte) As Byte '等待轴停止
Public Declare Function set_da Lib "mpc" (cardno As Byte, number As Byte, value As Integer) As Byte '设置DA电压值
Public Declare Function get_number Lib "mpc" (cardno As Byte) As Long '获取唯一序列号
Public Declare Function read_fifo_count Lib "mpc" (cardno As Byte) As Integer '读剩下缓冲数目
Public Declare Function get_status Lib "mpc" (cardno As Byte) As Byte '获取各轴工作状态
Public Declare Function get_command_pos Lib "mpc" (cardno As Byte, axis As Byte) As Long '获取轴逻辑位置
Public Declare Function set_cardno Lib "mpc" (cardno As Byte) '设置卡号
Public Declare Function link Lib "mpc" () As Byte '通讯连接
Public Declare Function no_link Lib "mpc" () As Byte '取消连接
使用函数前先确保PC与控制器物理连接正常,然后使用link函数连接通讯,如成功便可发送其它指令了。
试验程序如下:
If link () =1 then '通讯连接成功
call set_cardno(1) '设卡号为1
call set_speed(1 ,1,1000,1000,10,200) ' 设1轴速度
call set_speed(1 ,2,1000,1000,10,200) ' 设2轴速度
call set_speed(1 ,3,1000,1000,10,200) '设3轴速度
'1轴回原点
call pmove(1,1,-1000000,0) ' 1轴运动
call wait_in(1,0,1) ' 等待X0为高
call sudden_stop(1,1) ' 1轴停止
call set_command_pos(1,1,0) ' 设1轴此时坐标为0
call pmove(1,2,3200,0) '2轴运动
call pmove(1,3,-3200,0) '3轴运动
end if
本文来自泰安迪科技:www.mpc000.com 转载请注明出处。
|