HCBoardManager_Axis.cs 34.8 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HuichuanLibrary
{
    partial class HCBoardManager
    {
        #region 单轴控制
        /// <summary>
        /// 打开轴
        /// </summary>
        /// <param name="axisNo">轴号</param>
        /// <returns></returns>
        public static bool ServoOn(short axisNo)
        {
            if (!CardInitOk())
            {
                return false;
            }

            //【2】上使能
            UInt32 ret = ImcApi.IMC_AxServoOn(nCardHandle, axisNo, 1);
            if (ret != 0)
            {
                ShowErrorLog(" Axis[" + axisNo + "] ServoOn FAIL,",ret);
                return false;
            }
            else
            {
                ShowLog(" Axis[" + axisNo + "] ServoOn OK");
                return true;
            }
        }
        /// <summary>
        /// 关闭轴
        /// </summary>
        /// <param name="axisNo">轴号</param>
        /// <returns></returns>
        public static bool ServoOff(short axisNo)
        {
            if (!CardInitOk())
            {
                return false;
            }
            //【2】下使能
            UInt32 ret = ImcApi.IMC_AxServoOff(nCardHandle, axisNo, 1);
            if (ret != 0)
            {
                ShowErrorLog(" Axis[" + axisNo + "] ServoOff FAIL, ",ret);
                return false;
            }
            else
            {
                ShowLog(" Axis[" + axisNo + "] ServoOff OK");
                return true;
            }
        }

        #region 回零处理

        /// <summary>
        /// 开始回零
        /// </summary>
        /// <param name="axisNo">轴号</param>
        /// <param name="highVel">回零高速</param>
        /// <param name="lowVel">回零低速</param>
        /// <param name="acc">回零加速度</param> 
        /// <param name="homeMethod">回零方法,默认28</param>
        /// <param name="offset">回零偏移,默认0</param>
        /// <param name="overtime">超时时间,默认120000</param>
        /// <param name="posSrc">端子板信号源,默认0</param>
        /// <returns></returns>
        public static bool StartHomeMove(short axisNo, uint highVel, uint lowVel, uint acc, short homeMethod = 28, int offset = 0, uint overtime = 120000, short posSrc = 0)
        {
            if (!CardInitOk())
            {
                return false;
            }

            //【2】写入回原参数
            ImcApi.THomingPara tHomingPara = new ImcApi.THomingPara();
            tHomingPara.homeMethod = homeMethod;
            tHomingPara.offset = offset;
            tHomingPara.highVel = highVel;
            tHomingPara.lowVel = lowVel;
            tHomingPara.acc = acc;
            tHomingPara.overtime = overtime;
            tHomingPara.posSrc = posSrc;
            HCLogUtil.info("Axis[" + axisNo + "] StartHoming highVel[" + highVel + "],lowVel[" + lowVel + "],acc[" + acc + "],homeMethod[" + homeMethod + "],offset[" + offset + "],overtime[" + overtime + "],posSrc[" + posSrc + "]");
            //【3】开始回原
            UInt32 ret = ImcApi.IMC_StartHoming(nCardHandle, axisNo, ref tHomingPara);
            if (ret != 0)
            {
                ShowErrorLog(" Axis[" + axisNo + "]StartHoming FAIL, ",ret);
                return false;
            }
            else
            {
                ShowLog(" Axis[" + axisNo + "] StartHoming OK");
                return true;
            }
        }
        /// <summary>
        /// 停止原点返回
        /// </summary>
        /// <param name="axisNo">轴号</param>
        /// <returns></returns>
        public static bool StopHomeMove(short axisNo)
        {
            if (!CardInitOk())
            {
                return false;
            }
            //【2】触发停止回原
            UInt32 ret = ImcApi.IMC_StopHoming(nCardHandle, axisNo, 0); //0:表示平滑停止 1:表示急停
            if (ret != 0)
            {
                ShowErrorLog(" Axis[" + axisNo + "] StopHoming FAIL, ",ret);
                return false; ;
            }
            else
            {
                ShowLog(" Axis[" + axisNo + "] StopHoming OK");
                return true;
            }
        }
        /// <summary>
        /// 获取回原状态
        /// </summary>
        /// <param name="axisNo">轴号</param>
        /// <returns></returns>
        public static short GetHomeStatus(short axisNo)
        {
            if (!CardInitOk())
            {
                return -1;
            }
            //【2】获取回原状态
            short nHomingStatus = 0;
            UInt32 ret = ImcApi.IMC_GetHomingStatus(nCardHandle, axisNo, ref nHomingStatus);
            if (ret != 0)
            {
                ShowErrorLog(" Axis[" + axisNo + "] GetHomingStatus  FAIL, ",ret);
                return -1;
            }
            else
            {
                //ShowLog("轴回原状态为" + nHomingStatus.ToString());
                return nHomingStatus;
            }
        }

        public  static bool FinishHome(short axisNo)
        {
            if (!CardInitOk())
            {
                return false;
            }

            //【2】退出回原模式
            UInt32 ret = ImcApi.IMC_FinishHoming(nCardHandle, axisNo);
            if (ret != 0)
            {
                ShowErrorLog(" Axis[" + axisNo + "] FinishHoming FAIL, ",ret);
                return false;
            }
            else
            {
                ShowLog(" Axis[" + axisNo + "] FinishHoming OK!");
                return true;
            }
        }

        /// <summary>
        /// 判断回零是否完成
        /// </summary>
        ///<param name="axisNo">轴号</param>
        /// <returns></returns>
        public static bool HomeingIsEnd(short axisNo)
        {
            //回零状态结束,并且不再忙碌中
            AxisSts sts = GetAxisSts(axisNo);
            if (sts.BUSY.Equals(0) && sts.INP.Equals(1))
            {

                short homests = GetHomeStatus(axisNo);
                if (homests.Equals(HomeSts_OK))
                {
                    return true;
                }

            }
            return false;
        }

        #endregion
        /// <summary>
        /// 判断轴是否在指定的位置
        /// </summary>
        /// <param name="axisNo">轴号</param>
        /// <param name="targetPos">目标位置</param>
        /// <param name="maxError">最大误差脉冲值</param>
        /// <returns></returns>
        public static bool IsInPosition(short axisNo, double targetPos, double maxError = 10)
        {
            double currP = GetAxisCurrPos(axisNo);
            double chazhi = Math.Abs(Math.Abs(currP) - Math.Abs(targetPos)); 
            if (chazhi < maxError)
            {
                return true;
            }
            return false;
        }
        /// <summary>
        /// 判断轴是否运动结束,忙碌=0且到位信号=1
        /// </summary>
        /// <param name="axisNo">轴号</param>
        /// <returns></returns>
        public static bool MoveIsEnd(short axisNo)
        {
            AxisSts sts = GetAxisSts(axisNo);
            if (sts.BUSY.Equals(0) && sts.INP.Equals(1))
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 轴绝对运动
        /// </summary>
        /// <param name="axisNo">轴号</param>
        /// <param name="ptpPos">绝对位置</param>
        /// <param name="ptpVel">速度</param>
        /// <param name="ptpAcc">加速度</param>
        /// <param name="ptpDec">减速度</param>
        /// <returns></returns>
        public static bool AbsMove(short axisNo, double ptpPos, double ptpVel, double ptpAcc, double ptpDec)
        {
            if (!CardInitOk())
            {
                return false;
            }
            if (ptpVel > 0 && ptpAcc > 0 && ptpDec > 0)
            {
                if (!SetPTPSpeed(axisNo, ptpVel, ptpAcc, ptpDec))
                {
                    return false;
                }
            }

            short ptpType = 0;
            return StartPtpMove(axisNo, ptpType, ptpPos);            
        }
        /// <summary>
        /// 轴相对运动
        /// </summary>
        /// <param name="axisNo">轴号</param>
        /// <param name="ptpPos">相对位置</param>
        /// <param name="ptpVel">速度</param>
        /// <param name="ptpAcc">加速度</param>
        /// <param name="ptpDec">减速度</param>
        /// <returns></returns>
        public static bool RelMove(short axisNo, double ptpPos, double ptpVel, double ptpAcc, double ptpDec)
        {
            if (!CardInitOk())
            {
                return false;
            }
            if (ptpVel > 0 && ptpAcc > 0 && ptpDec > 0)
            {
                if (!SetPTPSpeed(axisNo, ptpVel, ptpAcc, ptpDec))
                {
                    return false;
                }
            }
            short ptpType = 1;
            return StartPtpMove(axisNo, ptpType, ptpPos);
        }

        private static bool StartPtpMove(short axisNo, short ptpType, double ptpPos)
        {
            ////【3】启动PTP 
            //UInt32 ret = ImcApi.IMC_StartPtpMove(nCardHandle, axisNo, ptpPos, ptpType);
            //if (ret != 0)
            //{
            //    ShowErrorLog(" Axis[" + axisNo + "] ptpPos["+ ptpPos + "] StartPtpMove " + ptpType + " FAIL, ",ret);
            //    return false;
            //}
            //else
            //{
            //    DebugLog(" Axis[" + axisNo + "] ptpPos[" + ptpPos + "] StartPtpMove " + ptpType + " OK",ret);
            //    return true;
            //}
            return StartPtpMoveEx(axisNo, ptpType, ptpPos);
        }
        private static bool StartPtpMoveEx(short axisNo, short ptpType, double ptpPos)
        {
            //【3】启动PTP Ex
            UInt32 ret = ImcApi.IMC_StartPtpMove_Ex(nCardHandle, axisNo, ptpPos, ptpType);
            if (ret != 0)
            {
                ShowErrorLog(" Axis[" + axisNo + "] ptpPos[" + ptpPos + "] StartPtpMove_Ex " + ptpType + " FAIL, ", ret);
                return false;
            }
            else
            {
                DebugLog(" Axis[" + axisNo + "] ptpPos[" + ptpPos + "] StartPtpMove_Ex " + ptpType + " OK", ret);
                return true;
            }
        }
        /// <summary>
        /// 设置点到点速度
        /// </summary>
        /// <param name="axisNo">轴号</param>
        /// <param name="ptpVel">目标速速</param>
        /// <param name="ptpAcc">加速度</param>
        /// <param name="ptpDec">减速度</param>
        /// <returns></returns>
        public static bool SetPTPSpeed(short axisNo, double ptpVel, double ptpAcc, double ptpDec)
        {
            if (!CardInitOk())
            {
                return false;
            }
            UInt32 ret = ImcApi.IMC_SetSingleAxMvPara(nCardHandle, axisNo, ptpVel, ptpAcc, ptpDec);
            if (ret != 0)
            {
                ShowErrorLog(" Axis[" + axisNo + "] SetSingleAxMvPara ptpVel [" + ptpVel + "]  ptpAcc [" + ptpAcc + "]  ptpDec [" + ptpDec + "] FAIL, ",ret);
                return false;
            }
            else
            {
                DebugLog(" Axis[" + axisNo + "] SetSingleAxMvPara ptpVel [" + ptpVel + "]  ptpAcc [" + ptpAcc + "]  ptpDec [" + ptpDec + "] OK",ret);
                return true;
            }
        }


        /// <summary>
        /// 轴停止运动
        /// </summary>
        /// <param name="axisNo">轴号</param>
        /// <returns></returns>
        public static bool AxisStop(short axisNo)
        {
            if (!CardInitOk())
            {
                return false;
            }
            //【2】轴停止
            UInt32 ret = ImcApi.IMC_AxMoveStop(nCardHandle, axisNo, 1); //StopType=1,立即停止
            if (ret != 0)
            {
                ShowErrorLog(" Axis[" + axisNo + "] MoveStop FAIL, ",ret);
                return false;
            }
            else
            {
                DebugLog(" Axis[" + axisNo + "] MoveStop OK",ret);
                return true;
            }
        }
        /// <summary>
        /// 设置齿轮比
        /// </summary>
        /// <param name="axisNo">轴号</param>
        /// <param name="Motor_Revolution">电子齿轮分子:电机分辨率</param>
        /// <param name="Shaft_Revolution">电子齿轮分母:一圈脉冲数</param>
        /// <returns></returns>
        public static bool SetEcatSdo(ushort axisNo, int Motor_Revolution, int Shaft_Revolution)
        {
            if (!CardInitOk())
            {
                return false;
            }
            //【3】获取轴号对应的实际地址
            short nphyStationId = 0, nphySlotId = 0;
            uint abort_code = 0;
            UInt32 ret = ImcApi.IMC_GetAxEcatStation(nCardHandle, axisNo, ref nphyStationId, ref nphySlotId);
            if (ret != 0)
            {
                ShowErrorLog(" Axis[" + axisNo + "] IMC_GetAxEcatStation FAIL, ",ret);
                return false;
            }

            //【4】设置电子齿轮分子
            ret = ImcApi.IMC_SetEcatSdo(nCardHandle, nphyStationId, 0x6091, 0x01, BitConverter.GetBytes(Motor_Revolution), 4, ref abort_code);
            if (ret != 0 | abort_code != 0)
            {
                if (ret != 0)
                {
                    ShowErrorLog(" Axis[" + axisNo + "] IMC_SetEcatSdo_1 FAIL, ",ret);
                    return false;
                }
                else
                {
                    ShowErrorLog(" Axis[" + axisNo + "] IMC_SetEcatSdo_1 FAIL, ErrorCode=0x" ,abort_code);
                    return false;
                }

            }

            //【4】设置电子齿轮分母
            ret = ImcApi.IMC_SetEcatSdo(nCardHandle, nphyStationId, 0x6091, 0x02, BitConverter.GetBytes(Shaft_Revolution), 4, ref abort_code);
            if (ret != 0 | abort_code != 0)
            {
                if (ret != 0)
                {
                    ShowErrorLog(" Axis[" + axisNo + "] IMC_SetEcatSdo_2 FAIL, ",ret);
                    return false;
                }
                else
                {
                    ShowErrorLog(" Axis[" + axisNo + "] IMC_SetEcatSdo_2 FAIL, ErrorCode=0x" , abort_code );
                    return false;
                }
            }
            return true;
        }
        /// <summary>
        /// 轴匀速运动
        /// </summary>
        /// <param name="axisNo">轴号</param>
        /// <param name="targetVel">目标速度</param>
        /// <returns></returns>
        public static bool SpeedMove(short axisNo, double targetVel)
        {
            if (!CardInitOk())
            {
                return false;
            }
            UInt32 ret = ImcApi.IMC_StartJogMove(nCardHandle, axisNo, targetVel);
            if (ret != 0)
            {
                ShowErrorLog(" Axis[" + axisNo + "] speed [" + targetVel + "] StartJogMove FAIL, ",ret);
                return false;
            }
            else
            {
                ShowLog(" Axis[" + axisNo + "] speed [" + targetVel + "] StartJogMove OK");
                return true;
            }
        }

        /// <summary>
        /// 获取轴规划模式
        /// </summary>
        ///<param name="axisNo">轴号</param>
        /// <returns></returns>
        public static short GetAxisPrfMode(short axisNo)
        {
            if (!CardInitOk())
            {
                return -1;
            }
            try
            {
                Int16[] nTimerAxPrfMode = { 0 };
                UInt32 ret = ImcApi.IMC_GetAxPrfMode(nCardHandle, axisNo, nTimerAxPrfMode, 1);  //变量需要定义为数组,默认读取1个轴
                if (ret != 0)
                {
                    ShowErrorLog(" Axis[" + axisNo + "] GetAxPrfMode FAIL, ",ret);
                    return -1;
                }
                if (nTimerAxPrfMode.Length > 0)
                {
                    return nTimerAxPrfMode[0];
                }
            }
            catch (Exception ex)
            {
                ShowErrorLog("" + ex.ToString());
            }
            return -1;
        }
        /// <summary>
        /// 获取轴状态
        /// </summary>
        ///<param name="axisNo">轴号</param>
        /// <returns></returns>
        public static AxisSts GetAxisSts(short axisNo)
        {
            //AxisSts sts = new AxisSts();
            //if (!CardInitOk())
            //{
            //    return sts;
            //}
            //Int32[] nTimerAxSts = { 0 };
            //UInt32 ret = ImcApi.IMC_GetAxSts(nCardHandle, axisNo, nTimerAxSts, 1);
            //if (ret != 0 || nTimerAxSts.Length <= 0)
            //{
            //    ShowErrorLog(" Axis[" + axisNo + "] GetAxSts FAIL, ",ret);
            //    return sts;
            //}

            //int axisState = nTimerAxSts[0];

            ////【3】更新界面显示

            //sts.ALM = ((axisState & 0x01) == 0x01) ? 1 : 0;   //报警信号
            //sts.ServoOn = ((axisState & 0x02) == 0x02) ? 1 : 0;   //使能信号
            //sts.BUSY = ((axisState & 0x04) == 0x04) ? 1 : 0;   //轴忙信号
            //sts.INP = ((axisState & 0x08) == 0x08) ? 1 : 0;   //轴到位信号
            //sts.EMG = ((axisState & 0x200) == 0x200) ? 1 : 0;   //急停信号
            //sts.WARN = ((axisState & 0x1000) == 0x1000) ? 1 : 0;   //警告信号
            //sts.ECAT = ((axisState & ImcApi.AX_ECAT_BIT) == ImcApi.AX_ECAT_BIT) ? 1 : 0;//总线轴标志

            //Int16 nTimerAxInput = 0;
            //ret = ImcApi.IMC_GetAxEcatDigitalInput(nCardHandle, axisNo, ref nTimerAxInput);
            //if (ret != 0)
            //{
            //    ShowErrorLog(" Axis[" + axisNo + "]GetAxEcatDigitalInput ,",ret);
            //    return sts;
            //}

            //sts.NEL = ((nTimerAxInput & 0x01) == 0x01) ? 1 : 0;   //负限位信号
            //sts.PEL = ((nTimerAxInput & 0x02) == 0x02) ? 1 : 0;   //正限位信号
            //sts.ORG = ((nTimerAxInput & 0x04) == 0x04) ? 1 : 0;   //原点信号

            //return sts;
            return GetAxisStsEx(axisNo);
        }

        /// <summary>
        /// 获取轴状态-新
        /// </summary>
        ///<param name="axisNo">轴号</param>
        /// <returns></returns>
        public static AxisSts GetAxisStsEx(short axisNo)
        {
            AxisSts sts = new AxisSts();
            if (!CardInitOk())
            {
                return sts;
            }
            Int32[] nTimerAxSts = { 0 };
            UInt32 ret = ImcApi.IMC_GetAxStsEx(nCardHandle, axisNo, nTimerAxSts, 1);
            if (ret != 0 || nTimerAxSts.Length <= 0)
            {
                ShowErrorLog(" Axis[" + axisNo + "] GetAxStsEx FAIL, ", ret);
                return sts;
            }

            int axisState = nTimerAxSts[0];

            //【3】更新界面显示

            sts.ALM = ((axisState & 0x01) == 0x01) ? 1 : 0;   //报警信号
            sts.ServoOn = ((axisState & 0x02) == 0x02) ? 1 : 0;   //使能信号
            sts.BUSY = ((axisState & 0x04) == 0x04) ? 1 : 0;   //轴忙信号
            sts.INP = ((axisState & 0x08) == 0x08) ? 1 : 0;   //轴到位信号
            sts.PEL = ((axisState & 0x10) == 0x10) ? 1 : 0;   //正限位报警信号
            sts.NEL = ((axisState & 0x20) == 0x20) ? 1 : 0;   //负限位报警信号
            sts.EMG = ((axisState & 0x200) == 0x200) ? 1 : 0;   //急停信号
            sts.WARN = ((axisState & 0x1000) == 0x1000) ? 1 : 0;   //警告信号
            sts.ECAT = ((axisState & ImcApi.AX_ECAT_BIT) == ImcApi.AX_ECAT_BIT) ? 1 : 0;//总线轴标志
            sts.ORG = ((axisState & 0x2000) == 0x2000) ? 1 : 0;   //原点信号
            return sts;
        }

        /// <summary>
        /// 获取轴规划位置
        /// </summary>
        ///<param name="axisNo">轴号</param>
        /// <returns></returns>
        public static double GetAxisPrfPos(short axisNo)
        {
            if (!CardInitOk())
            {
                return -1;
            }
            double[] nTimerAxPrfPos = { 0 };

            UInt32 ret = ImcApi.IMC_GetAxPrfPos(nCardHandle, axisNo, nTimerAxPrfPos, 1);
            if (ret != 0)
            {
                ShowErrorLog(" Axis[" + axisNo + "] GetAxPrfPos FAIL, ",ret);
                return -1;
            }
            return nTimerAxPrfPos[0];
        }
        /// <summary>
        /// 获取轴规划速度
        /// </summary>
        ///<param name="axisNo">轴号</param>
        /// <returns></returns>
        public static double GetAxisPrfVel(short axisNo)
        {
            if (!CardInitOk())
            {
                return -1;
            }
            double[] nTimerAxPrfVel = { 0 };

            UInt32 ret = ImcApi.IMC_GetAxPrfVel(nCardHandle, axisNo, nTimerAxPrfVel, 1);
            if (ret != 0)
            {
                ShowErrorLog(" Axis[" + axisNo + "] GetAxPrfVel FAIL, ",ret);
                return -1;
            }
            return nTimerAxPrfVel[0];
        }
        /// <summary>
        /// 获取轴反馈位置
        /// </summary>
        ///<param name="axisNo">轴号</param>
        /// <returns></returns>
        public static double GetAxisCurrPos(short axisNo)
        {
            if (!CardInitOk())
            {
                return -1;
            }
            double[] nTimerAxEncPos = { 0 };
            UInt32 ret = ImcApi.IMC_GetAxEncPos(nCardHandle, axisNo, nTimerAxEncPos, 1);
            if (ret != 0)
            {
                ShowErrorLog(" Axis[" + axisNo + "] GetAxEncPos FAIL, ",ret);
                return -1;
            }
            return nTimerAxEncPos[0];
        }
        /// <summary>
        ///获取轴反馈速度
        /// </summary>
        ///<param name="axisNo">轴号</param>
        /// <returns></returns>
        public static double GetAxisCurrVel(short axisNo)
        {
            if (!CardInitOk())
            {
                return -1;
            }
            double[] nTimerAxEncVel = { 0 };
            UInt32 ret = ImcApi.IMC_GetAxEncVel(nCardHandle, axisNo, nTimerAxEncVel, 1);
            if (ret != 0)
            {
                ShowErrorLog(" Axis[" + axisNo + "] GetAxEncVel FAIL, ",ret);
                return -1;
            }
            return nTimerAxEncVel[0];
        }
        /// <summary>
        /// 获取轴反馈加速度
        /// </summary>
        ///<param name="axisNo">轴号</param>
        /// <returns></returns>
        public static double GetAxisCurrAcc(short axisNo)
        {
            if (!CardInitOk())
            {
                return -1;
            }
            double[] nTimerAxEncAcc = { 0 };
            UInt32 ret = ImcApi.IMC_GetAxEncAcc(nCardHandle, axisNo, nTimerAxEncAcc, 1);
            if (ret != 0)
            {
                ShowErrorLog(" Axis[" + axisNo + "] GetAxEncAcc FAIL, ",ret);
                return -1;
            }
            return nTimerAxEncAcc[0];
        }
        /// <summary>
        /// 清除轴报警状态
        /// </summary>
        ///<param name="axisNo">轴号</param>
        /// <returns></returns>
        public static bool ClearAxisSts(short axisNo)
        {
            if (!CardInitOk())
            {
                return false;
            }
            UInt32 ret = ImcApi.IMC_ClrAxSts(nCardHandle, axisNo);
            if (ret != 0)
            {
                ShowErrorLog(" Axis[" + axisNo + "] ClrAxSts FAIL, ",ret);
                return false;
            }
            return true;
        }
        /// <summary>
        /// 获取错误码
        /// </summary>
        /// <param name="axisNo">轴号</param>
        /// <returns></returns>
        public static short GetAxErrCode(short axisNo)
        {
            if (!CardInitOk())
            {
                return -1;
            }
            short outV = -1;
            UInt32 ret = ImcApi.IMC_GetAxErrCode(nCardHandle, axisNo, ref outV);
            if (ret != 0)
            {
                ShowErrorLog(" Axis[" + axisNo + "] GetAxEncAcc FAIL, ",ret);
                return -1;
            }
            return outV;
        }
        /// <summary>
        /// 获取轴类型
        /// </summary>
        /// <param name="axisNo">轴号</param>
        /// <returns></returns>
        public static string GetAxType(short axisNo)
        {
            string res = "";
            if (!CardInitOk())
            {
                return res;
            }
            short pAxType = -1;
            short pOutChn = -1;
            UInt32 ret = ImcApi.IMC_GetAxType(nCardHandle, axisNo, ref pAxType, ref pOutChn);
            res = "轴类型[" + pAxType + "],物理通道[" + pOutChn + "]";
            if (ret != 0)
            {
                ShowErrorLog(" Axis[" + axisNo + "]  GetAxType FAIL, ",ret);

            }
            return res;
        }

        /// <summary>
        /// 获取EtherCAT类型轴对应的正向力矩限制
        /// </summary>
        /// <param name="axisNo">轴号0-31</param>
        /// <returns>正向力矩限制</returns>
        public static short GetEcatAxPosTorqLmt(short axisNo)
        {
            if (!CardInitOk())
            {
                return -1;
            }
            short outV = -1;
            UInt32 ret = ImcApi.IMC_GetEcatAxPosTorqLmt(nCardHandle, axisNo, ref outV);
            if (ret != 0)
            {
                ShowErrorLog(" Axis[" + axisNo + "] GetEcatAxPosTorqLmt FAIL, ",ret);
                return -1;
            }
            return outV;
        }
        /// <summary>
        /// 设置EtherCAT类型轴对应的正向力矩限制
        /// </summary>
        /// <param name="axisNo">轴号0-31</param>
        /// <param name="value">正向力矩限制</param>
        /// <returns></returns>
        public static bool SetEcatAxPosTorqLmt(short axisNo, short value)
        {
            if (!CardInitOk())
            {
                return false;
            }
            UInt32 ret = ImcApi.IMC_SetEcatAxPosTorqLmt(nCardHandle, axisNo, value);
            if (ret != 0)
            {
                ShowErrorLog(" Axis[" + axisNo + "][" + value + "] SetEcatAxPosTorqLmt FAIL, ",ret);
                return false;
            }
            return true;
        }
        /// <summary>
        /// 获取EtherCAT类型轴对应的负向力矩限制
        /// </summary>
        /// <param name="axisNo">轴号0-31</param>
        /// <returns>负向力矩限制</returns>
        public static short GetEcatAxNegTorqLmt(short axisNo)
        {
            if (!CardInitOk())
            {
                return -1;
            }
            short outV = -1;
            UInt32 ret = ImcApi.IMC_GetEcatAxNegTorqLmt(nCardHandle, axisNo, ref outV);
            if (ret != 0)
            {
                ShowErrorLog(" Axis[" + axisNo + "] GetEcatAxNegTorqLmt FAIL, ",ret);
                return -1;
            }
            return outV;
        }
        /// <summary>
        /// 设置EtherCAT类型轴对应的负向力矩限制
        /// </summary>
        /// <param name="axisNo">轴号0-31</param>
        /// <param name="value">负向力矩限制</param>
        /// <returns></returns>
        public static bool SetEcatAxNegTorqLmt(short axisNo, short value)
        {
            if (!CardInitOk())
            {
                return false;
            }
            UInt32 ret = ImcApi.IMC_SetEcatAxNegTorqLmt(nCardHandle, axisNo, value);
            if (ret != 0)
            {
                ShowErrorLog(" Axis[" + axisNo + "][" + value + "] SetEcatAxNegTorqLmt FAIL, ",ret);
                return false;
            }
            return true;
        }
        /// <summary>
        ///  获取EtherCAT类型轴对应的最大力矩限制
        /// </summary>
        /// <param name="axisNo">轴号0-31</param>
        /// <returns>最大力矩限制</returns>
        public static short GetEcatAxMaxTorqLmt(short axisNo)
        {
            if (!CardInitOk())
            {
                return -1;
            }
            short outV = -1;
            UInt32 ret = ImcApi.IMC_GetEcatAxMaxTorqLmt(nCardHandle, axisNo, ref outV);
            if (ret != 0)
            {
                ShowErrorLog(" Axis[" + axisNo + "] GetEcatAxMaxTorqLmt FAIL, ",ret);
                return -1;
            }
            return outV;
        }
        /// <summary>
        /// 设置EtherCAT类型轴对应的最大力矩限制
        /// </summary>
        /// <param name="axisNo">轴号0-31</param>
        /// <param name="value">最大力矩限制</param>
        /// <returns></returns>
        public static bool SetEcatAxMaxTorqLmt(short axisNo, short value)
        {
            if (!CardInitOk())
            {
                return false;
            }
            UInt32 ret = ImcApi.IMC_SetEcatAxMaxTorqLmt(nCardHandle, axisNo, value);
            if (ret != 0)
            {
                ShowErrorLog(" Axis[" + axisNo + "][" + value + "] SetEcatAxMaxTorqLmt FAIL, ",ret);
                return false;
            }
            return true;
        }

        /// <summary>
        /// 设置反向间隙
        /// HuichuanLibrary.HCBoardManager.SetAxBacklash(MiddleAxis.Config.GetAxisValue(),Config.MiddleAxis_Reverse_Offset, MiddleAxis.Config.HomeLowSpeed,-1);
        /// </summary>
        /// <param name="axisNo"></param>
        /// <param name="wholdCmpVal"></param>
        /// <param name="cmpVel"></param>
        /// <param name="cmpDir"></param>
        /// <returns></returns>
        public static bool SetAxBacklash(short axisNo, int wholdCmpVal, int cmpVel, short cmpDir)
        {
            if (!CardInitOk())
            {
                return false;
            }
            var ret = ImcApi.IMC_SetAxBacklash(nCardHandle, axisNo, wholdCmpVal, cmpVel, cmpDir);
            if (ret != 0)
            {
                ShowErrorLog(" Axis[" + axisNo + "][wholdCmpVal:" + wholdCmpVal + "][cmpVel:" + cmpVel + "][cmpDir:" + cmpDir + "] SetAxBacklash FAIL, ErrorCode=0x" + ret.ToString("x8"));
                return false;
            }
            return true;
        }
        #endregion

        #region 轴状态

        public static string AxisPrfMode(short axCtrlMode)
        {
            axCtrlMode = (short)(axCtrlMode & 0xf);
            string strCtrlMode = string.Empty;
            switch (axCtrlMode)
            {
                case 0:
                    strCtrlMode = "0=无模式";
                    break;
                case 1:
                    strCtrlMode = "1=点对点";
                    break;
                case 2:
                    strCtrlMode = "2=JOG";
                    break;
                case 3:
                    strCtrlMode = "3=电子齿轮";
                    break;
                case 4:
                    strCtrlMode = "4=电子凸轮";
                    break;
                case 5:
                    strCtrlMode = "5=PVT";
                    break;
                case 6:
                    strCtrlMode = "6=龙门";
                    break;
                case 7:
                    strCtrlMode = "7=手轮";
                    break;
                case 9:
                    strCtrlMode = "8=点位连续";
                    break;
                case 11:
                    strCtrlMode = "11=插补同步轴";
                    break;
                case 15:
                    strCtrlMode = "15=回零";
                    break;
                default:
                    break;
            }
            return strCtrlMode;
        }


        public static string AxisHomeSts(short homeStatus)
        {
            string sStr = "";
            switch (homeStatus)
            {
                case 0:
                    sStr = "0=正在回零中";
                    break;
                case 1:
                    sStr = "1=回零中断或者没有开始启动";
                    break;
                case 2:
                    sStr = "2=回零结束,但没有到设定的目标位置";
                    break;
                case 3:
                    sStr = "3=回零完成";
                    break;
                case 4:
                    sStr = "4=回零中发生错误,同时速度不为0";
                    break;
                case 5:
                    sStr = "5=回零中发生错误,同时速度为0";
                    break;

                default:
                    break;
            }
            return sStr;
        }
        #endregion

         
        /// <summary>
        /// 获取轴的负载率
        /// </summary>
        /// <param name="axisNo"></param>
        /// <returns></returns>
        public static string GetAxisLoadRate(short axisNo)
        {
            short pPhyStation_Id = 0;
            short pPhySlot_id = 0;
            UInt32 ret1 = ImcApi.IMC_GetAxEcatStation(nCardHandle, (ushort)axisNo, ref pPhyStation_Id, ref pPhySlot_id);

            ushort index = 0x200b;
            ushort subindex = 13;
            uint targetsize = 2;
            int datasize = 2;
            uint resultSize = 0;
            uint abortCode = 0;
            Byte[] targetData = new Byte[targetsize];

            UInt32 ret = ImcApi.IMC_GetEcatSdo(nCardHandle, pPhyStation_Id, index, subindex, targetData, targetsize, ref resultSize, ref abortCode);

            if (resultSize > 0)
            {
                string strFromat = "{0:X2}";
                string str = "";
                foreach (byte b in targetData)
                {
                    str += string.Format(strFromat, b);
                }
                int value = Convert.ToInt32(str.Trim().Replace(" ", ""), 16);

                HCLogUtil.info($"axisNo={axisNo},pPhyStation_Id={pPhyStation_Id},index={index},subindex= {subindex},abortCode ={abortCode},resultSize={resultSize}, str={str},value={value}" );
                return value.ToString();
            }
            else
            {
                HCLogUtil.info($"axisNo={axisNo},pPhyStation_Id={pPhyStation_Id},index={index},subindex= {subindex},abortCode ={abortCode},resultSize={resultSize},");
            }
            return "";
        }

        /// <summary>
        ///  获取轴的扭矩
        /// </summary>
        /// <param name="axisNo"></param>
        /// <returns></returns>
        public static short GetAxActTorq(short axisNo)
        {
            short pActTrq = 0;
            UInt32 ret = ImcApi.IMC_GetAxActTorq(nCardHandle, axisNo, ref pActTrq);
            HCLogUtil.info($"axisNo={axisNo},pActTrq={pActTrq},");
            return pActTrq;
        }
         


        /// <summary>
        /// 回原点状态:0=正在回零中
        /// </summary>
        public static short HomeSts_Moveing = 0;
        /// <summary>
        /// 回原点状态:1=回零中断或者没有开始启动
        /// </summary>
        public static short HomeSts_NotStart = 1;
        /// <summary>
        /// 回原点状态:3=回零完成
        /// </summary>
        public static short HomeSts_OK = 3;
    }
}