package com.example;

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


public class Main {
    // 错误处理工具类
    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 devNum = 0;                       // 设备序号，从0开始
            short axisNum = 0;                      // 轴号从0开始
            PHAND pDevHandle = new PHAND();         // 设备句柄
            PHAND pAxisHandle = new PHAND();        // 轴句柄
            PHAND pCrdHandle = new PHAND();         // 坐标系句柄
            
            // 打开控制器
            short rtn = NMC_DevOpen(devNum, pDevHandle);
            checkError(rtn,"打开控制器出错");
            System.out.println("打开设备 - 成功");
            HAND devHandle = pDevHandle.getHandle();

            //打开轴,并获得轴句柄
            rtn = NMC_MtOpen(devHandle, axisNum, pAxisHandle);
            checkError(rtn,"打开轴出错");
            System.out.printf("打开轴%d - 成功\n", axisNum);

            //打开轴使能(根据实际情况使用)
            HAND axisHandle = pAxisHandle.getHandle();
            rtn = NMC_MtSetSvOn(axisHandle);
            checkError(rtn,"轴使能出错");
            System.out.printf("轴%d伺服使能 - 成功\n", axisNum);

            // 打开坐标系
            rtn = NMC_CrdOpen(devHandle, pCrdHandle);
            checkError(rtn,"打开坐标系出错");
            System.out.println("打开坐标系 - 成功");
            HAND crdHandle = pCrdHandle.getHandle();
            
       double pulse2Rad = 2 * Math.PI / 288000;			//A轴旋转一圈脉冲为
        NativeLong deltaX = new NativeLong(-3687);						//0转到PI/3后重新校正到一点是的X偏移量
        NativeLong deltaY = new NativeLong(-9494);						//0转到PI/3后重新校正到一点是的Y偏移量
        NativeLongByReference toolX = new NativeLongByReference(new NativeLong(0L));
        NativeLongByReference toolY = new NativeLongByReference(new NativeLong(0L));
        rtn = NMC_CrdSetXYZAToolCalc(crdHandle, Math.PI / 3, deltaX, deltaY, toolX, toolY);//计算工具偏移
        checkError(rtn,"计算工具偏移出错");
        rtn = NMC_CrdSetXYZAPara(crdHandle, pulse2Rad, toolX.getValue(), toolY.getValue());//设置工具参数
        checkError(rtn,"设置工具参数出错");
        short[] pMapAxisArray = { 0, 1, 2, 3 };
        rtn = NMC_CrdSetTransXYZA(crdHandle, pMapAxisArray);//设置XYZA机型
        checkError(rtn,"设置XYZA机型出错");
    }
}
