import static lib.mtn_lib20.*;
import static lib.mtn_lib20_com.*;
import static lib.mtn_lib20_ext.*;
import static lib.mtn_lib20_oem.*;
import static lib.mtn_lib20_err.*;
import com.sun.jna.*;
import com.sun.jna.ptr.*;

public class Main {
    static {
        String libPath = System.getProperty("user.dir") + "\\lib";
        System.setProperty("java.library.path", libPath);
        System.load(libPath + "\\msvcr100.dll");
        System.load(libPath + "\\msvcp100.dll");
        System.load(libPath + "\\nmc_lib20.dll");
    }
    private static void checkError(short rtn, String operation) {
        if (rtn != 0) {
            System.err.printf("错误: %s (代码: %d)\n", operation, rtn);
//            System.exit(1);
        }
    }
    public static void main(String[] args) {
        short rtn;					// 指令返回值
        short axisNum = 0;				// 轴号，从0开始
        HAND devHandle = new HAND();				// 设备句柄
        PHAND pDevHandle = new PHAND();
        HAND axisHandle = new HAND();			// 轴句柄
        PHAND pAxisHandle = new PHAND();

        short[] devCount = new short[1];
        TDevInfo[] devInfoList = new TDevInfo[10];
        rtn = NMC_DevSearch(TSearchMode.Ethernet, devCount, devInfoList);
        checkError(rtn,"搜索设备出错");
        rtn = NMC_DevOpen(devInfoList[0].address, pDevHandle);				//打开控制器
        checkError(rtn,"打开控制器出错");
        devHandle = pDevHandle.getHandle();
        rtn = NMC_MtOpen(devHandle, axisNum, pAxisHandle); // 打开轴
        checkError(rtn,"打开轴出错");
        axisHandle = pAxisHandle.getHandle();
        rtn = NMC_MtSetSvOn(axisHandle);//打开轴使能(根据实际情况使用)
        checkError(rtn,"轴使能出错");

        rtn = NMC_MtSetPrfMode(axisHandle, (short) 0);
        checkError(rtn,"运动模式配置出错");
        TPtpPara ptpPrm = new TPtpPara();
        ptpPrm.acc = 1;								//加速度
        ptpPrm.dec = 1;								//减加速度
        ptpPrm.endVel = 0;								//结束速度
        ptpPrm.startVel = 0;							//起跳速度
        ptpPrm.smoothCoef = 0;							//平滑系数
        rtn = NMC_MtSetPtpPara(axisHandle, ptpPrm);	// 运动参数配置
        checkError(rtn,"运动参数配置出错");
        double maxVel = 10;
        rtn = NMC_MtSetVel(axisHandle, maxVel);			// 设置规划的最高速度
        checkError(rtn,"规划速度配置出错");
        NativeLong targetPos = new NativeLong(10000);
        rtn = NMC_MtSetPtpTgtPos(axisHandle, targetPos);// 设置目标位置
        checkError(rtn,"目标位置配置出错");
        rtn = NMC_MtUpdate(axisHandle);	// 启动运动
        checkError(rtn,"运动出错");
    }
}
