/*==========================================*/ /* HPCI-CPD530 series */ /* Library lebel1a function for Visual C */ /* */ /* file name:cp530l1a.c */ /* date :2003/07/02 */ /* version :2.0.0.1 */ /* */ /* Copyright(C) 2001-2003 Hivertec Inc. */ /* All Rights Reserved. */ /*==========================================*/ #include #include "cp530l1a.h" /* 軸番号 */ #define X_AX 0 #define Y_AX 1 #define Z_AX 2 #define U_AX 3 #define V_AX 4 #define W_AX 5 #define A_AX 6 #define B_AX 7 #define ILLEGAL_PRM 0x0100 #define OTHER_BOARD 0x0200 /*----------------------------------------------*/ /* 動作モード整合性診断テーブル 1:正常 */ /*----------------------------------------------*/ WORD mod_tbl[128] = { /* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xa xb xc xd xe xf */ 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, /* 00-0f */ 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, /* 10-1f */ 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, /* 20-2f */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 30-3f */ 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, /* 40-4f */ 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 50-5f */ 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 60-6f */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 /* 70-7f */ }; /*======================================================================*/ /* デバイス関係の関数 */ /*======================================================================*/ /************************************************************/ /* ボード枚数,デバイス情報の取得 */ /* 引数:ボード枚数のアドレス,デバイス情報のアドレス */ /************************************************************/ DWORD hcp530_GetDevInfo(DWORD* DevCnt, HPCDEVICEINFO* DevInfo) { DWORD dwDevCnt; /* CPD530ボード枚数 */ DWORD dwRet; /* 戻り値 */ DWORD dwIx; HPCDEVICEINFO HpcDevInfo[16]; /* CPD530デバイス情報 */ dwRet = cp530_GetDeviceCount(&dwDevCnt); /* ボード枚数取得 */ if(dwRet) return dwRet; /* ボード枚数が0枚または16枚以上の時はエラー */ if((!dwDevCnt) || (dwDevCnt >= 16)) return ILLEGAL_PRM; *DevCnt = dwDevCnt; dwRet = cp530_GetDeviceInfo(&dwDevCnt, &HpcDevInfo[0]); /* デバイス情報取得 */ if(dwRet) return dwRet; for(dwIx = 0; dwIx < dwDevCnt; dwIx++) { *DevInfo = HpcDevInfo[dwIx]; *DevInfo++; } return 0; } /********************************************************************/ /* デバイスのオープン,デバイスの初期化 */ /* 引数:デバイスハンドルのアドレス,デバイス情報のアドレス */ /********************************************************************/ DWORD hcp530_DevOpen(DWORD* hDeviceID, HPCDEVICEINFO* DevInfo) { WORD wAx; /* 軸 */ WORD wSts; /* ステータス */ WORD wCode; /* ボード固有コード */ WORD wMaxAx = 8; /* 最大軸数 */ DWORD hDevID; /* デバイスハンドル */ DWORD dwRet; /* 戻り値 */ dwRet = cp530_OpenDevice(&hDevID, DevInfo); if(dwRet) return dwRet; else *hDeviceID = hDevID; /* ボード判別 */ dwRet = cp530_GetBoardCode(hDevID, &wCode); if(wCode == 0x578a) { /* HPCI-CPD578A */ wMaxAx = 8; } else if(wCode == 0x5254) { for(wAx = X_AX; wAx < (B_AX + 1); wAx++) { dwRet |= cp530_rMstsW(hDevID, wAx, &wSts); if(wSts & 0x2004) { wMaxAx = wAx; break; } } } else { return OTHER_BOARD; } /* オプションポート設定 */ if(wCode == 0x578a) { /* HPCI-CPD578A */ dwRet |= cp530_wPortB(hDevID, CP53_ELS_PORT, 0x00); /* ELS極性選択ポート書込(B接) */ dwRet |= cp530_wPortB(hDevID, CP53_DLS_PORT, 0x0f); /* DLS/PCS切替ポート書込(PCS) */ dwRet |= cp530_wPortB(hDevID, CP53_CP4_PORT, 0x00); /* コンパレータ4出力切替ポート書込(出力不可) */ dwRet |= cp530_wPortB(hDevID, CP53_CP5_PORT, 0x00); /* コンパレータ5出力切替ポート書込(出力不可) */ dwRet |= cp530_wPortB(hDevID, CP57_J3CO_PRT, 0x00); /* J3出力マスクポート(出力マスク) */ dwRet |= cp530_wPortB(hDevID, CP57_XUCO_PRT, 0xff); /* X-U軸CMP出力切替ポート(出力不可) */ dwRet |= cp530_wPortB(hDevID, CP57_VBCO_PRT, 0xff); /* V-B軸CMP出力切替ポート(出力不可) */ dwRet |= cp530_wPortB(hDevID, CP53_INT_PORT, 0x00); /* 割込イネーブルポート書込(出力不可) */ } else if(wMaxAx == 2) { /* HPCI-CPD532 */ dwRet = cp530_wPortB(hDevID, CP53_ELS_PORT, 0x00); /* ELS極性選択ポート書込(B接) */ dwRet |= cp530_wPortB(hDevID, CP53_DLS_PORT, 0x03); /* DLS/PCS切替ポート書込(PCS) */ dwRet |= cp530_wPortB(hDevID, CP53_CP4_PORT, 0x00); /* コンパレータ4出力切替ポート書込(出力不可) */ dwRet |= cp530_wPortB(hDevID, CP53_CP5_PORT, 0x00); /* コンパレータ5出力切替ポート書込(出力不可) */ dwRet |= cp530_wPortB(hDevID, CP53_INT_PORT, 0x00); /* 割込イネーブルポート書込(出力不可) */ } else if(wMaxAx == 4) { /* HPCI-CPD534 */ dwRet = cp530_wPortB(hDevID, CP53_ELS_PORT, 0x00); /* ELS極性選択ポート書込(B接) */ dwRet |= cp530_wPortB(hDevID, CP53_DLS_PORT, 0x0f); /* DLS/PCS切替ポート書込(PCS) */ dwRet |= cp530_wPortB(hDevID, CP53_CP4_PORT, 0x00); /* コンパレータ4出力切替ポート書込(出力不可) */ dwRet |= cp530_wPortB(hDevID, CP53_CP5_PORT, 0x00); /* コンパレータ5出力切替ポート書込(出力不可) */ dwRet |= cp530_wPortB(hDevID, CP53_INT_PORT, 0x00); /* 割込イネーブルポート書込(出力不可) */ } else if(wMaxAx == 8) { /* HPCI-CPD508 */ dwRet = cp530_wPortB(hDevID, CP53_ELS_PORT, 0x00); /* ELS極性選択ポート書込(B接) */ dwRet |= cp530_wPortB(hDevID, CP53_DLS_PORT, 0xff); /* DLS/PCS切替ポート書込(PCS) */ dwRet |= cp530_wPortB(hDevID, CP53_CP4_PORT, 0x00); /* コンパレータ4出力切替ポート書込(出力不可) */ dwRet |= cp530_wPortB(hDevID, CP53_OLS_PORT, 0x00); /* BOLS/PCS切替ポート書込(BOLS) */ dwRet |= cp530_wPortB(hDevID, CP53_ALM_PORT, 0x00); /* SVALM/DI/EMG切替ポート書込(SVALM) */ dwRet |= cp530_wPortB(hDevID, CP53_INT_PORT, 0x00); /* 割込イネーブルポート書込(出力不可) */ } /* レジスタ初期化 */ if(wMaxAx == 8) { /* HPCI-CPD578A,508 */ dwRet |= cp530_wCmdW(hDevID, X_AX, 0x04); /* X-U軸ソフトウェアリセット */ dwRet |= cp530_wCmdW(hDevID, V_AX, 0x04); /* V-B軸ソフトウェアリセット */ } else if(wMaxAx < 5) { /* HPCI-CPD534,532 */ dwRet |= cp530_wCmdW(hDevID, X_AX, 0x04); /* X-U軸ソフトウェアリセット */ } for(wAx = X_AX; wAx < wMaxAx; wAx++) { /* レジスタ書込(ベース速度レジスタ:PRFL = 200) */ dwRet |= cp530_wReg (hDevID, wAx, 0x81, 200l); /* レジスタ書込(動作速度レジスタ:PRFH = 2000) */ dwRet |= cp530_wReg (hDevID, wAx, 0x82, 2000l); /* レジスタ書込(直線加速時加速レートレジスタ) */ /* 基準クロック周波数 = 19,660,800Hz, 加速時間 = 減速時間 = 0.5秒 */ /* PRFH = 2000, PRFL = 200, PRUR:加速レート */ /* 加速時間(秒) = (PRFH - PRFL) * (PRUR + 1) * 4 / 19660800 */ /* PRUR = 1364 */ dwRet |= cp530_wReg (hDevID, wAx, 0x83, 1364l); /* レジスタ書込(倍率 = 1倍, PRMG = 299) */ dwRet |= cp530_wReg (hDevID, wAx, 0x85, 299l); /* レジスタ書込(動作モード:PRMD = 0x8008000) */ dwRet |= cp530_wReg (hDevID, wAx, 0x87, 0x8008000); /* レジスタ書込(移動量補正速度:RFA = 200) */ dwRet |= cp530_wReg (hDevID, wAx, 0x9b, 200l); /* レジスタ書込(環境設定1:RENV1 = 0x20434004) */ /* 指令出力:個別パルス指令の正論理 */ /* OLS & DLS & SVALM = B接, INPOS = A接, */ /* ELS & SVALM入力時即停止,DLS検出時動作 = 減速のみ,ラッチしない, */ /* サーボエラー、原点復帰完了カウンタクリア出力しない */ dwRet |= cp530_wReg (hDevID, wAx, 0x9c, 0x20434004); /* 汎用入出力設定等 */ if(wCode == 0x578a) { /* HPCI-CPD578A */ /* レジスタ書込(環境設定2:RENV2 = 0x0020fd55) */ dwRet |= cp530_wReg(hDevID, wAx, 0x9d, 0x0020fd55); } else if(wCode == 0x5254) { /* HPCI-CPD534,532,508 */ /* レジスタ書込(環境設定2:RENV2 = 0x0020f555) */ dwRet |= cp530_wReg(hDevID, wAx, 0x9d, 0x0020f555); } /* レジスタ書込(環境設定3:RENV3 = 0x00f00002) */ /* 原点復帰:センサ+エンコーダ原点復帰(OLS+Z相), */ /* CLR入力OFF→ON & 原点復帰完了時カウンタクリア */ dwRet |= cp530_wReg (hDevID, wAx, 0x9e, 0x00f00002l); /* レジスタ書込(イベントINT設定:RIRQ = 1(正常停止) */ dwRet |= cp530_wReg (hDevID, wAx, 0xac, 1l); /* SVON OFF */ dwRet |= cp530_wCmdW(hDevID, wAx, 0x10); /* SVRST OFF */ dwRet |= cp530_wCmdW(hDevID, wAx, 0x11); /* 減速停止 */ dwRet |= cp530_wCmdW(hDevID, wAx, 0x4a); } return dwRet; } /********************************************************/ /* デバイスのクローズ */ /* 引数:デバイスハンドル */ /********************************************************/ DWORD hcp530_DevClose(DWORD hDevID) { DWORD dwRet; /* 戻り値 */ dwRet = cp530_CloseDevice(hDevID); /* デバイスのクローズ */ return dwRet; } /*===============================================================*/ /* 初期設定系の関数 */ /*===============================================================*/ /********************************************************/ /* 原点復帰方式の設定 */ /* 引数:デバイスハンドル,軸,原点復帰モード */ /********************************************************/ DWORD hcp530_SetOrgMode(DWORD hDevID, WORD axis, WORD mode) { DWORD dwRet; /* 戻り値 */ DWORD dwData; /* 軸番号が0〜7以外または */ /* 原点復帰モードが0〜12以外の時は引数エラー */ if((axis < 0) || (7 < axis)) return ILLEGAL_PRM; if((mode < 0) || (mode > 12)) return ILLEGAL_PRM; /* 環境レジスタ3(RENV3)読込み */ dwRet = cp530_rReg(hDevID, axis, 0xde, &dwData); if(dwRet) return dwRet; /* Orgmode9の時,CTR2入力を指令位置にする */ /* (Orgmode9以外の時,CTR2入力は機械位置推奨) */ if(mode == 9) { dwData &= (DWORD)~0x00000300; dwData |= (DWORD) 0x00000100; } dwData &= (DWORD)~0x0000000f; dwData |= mode; /* 環境レジスタ3(RENV3)書込み */ dwRet = cp530_wReg(hDevID, axis, 0x9e, dwData); return dwRet; } /************************************************************************/ /* ソフトリミットの設定 */ /* 引数:デバイスハンドル,軸,+SLS,-SLS,SLS使用/不使用,停止方法 */ /************************************************************************/ DWORD hcp530_SetSls(DWORD hDevID, WORD axis, long psls, long msls, WORD selctr, WORD stop) { DWORD dwRet; /* 戻り値 */ DWORD dwEnv; /* 軸番号が0〜7以外 */ /* ソフトリミット使用時に+SLS < -SLSの時は引数エラー */ if((axis < 0) || (7 < axis)) return 0x100; if((selctr) && (psls <= msls)) return 0x100; /* 環境レジスタ4(RENV4)読込み */ dwRet = cp530_rReg(hDevID, axis, 0xdf, &dwEnv); if(dwRet) return dwRet; if(!selctr) { /* SLS不使用 */ dwEnv &= 0xffffe3e3; } else { /* SLS使用 */ /* +SLSデータ設定 */ dwRet = cp530_wReg(hDevID, axis, 0xa7, psls); if(dwRet) return dwRet; /* -SLSデータ設定 */ dwRet = cp530_wReg(hDevID, axis, 0xa8, msls); if(dwRet) return dwRet; dwEnv &= 0xffff0000; dwEnv |= 0x00001818; if(stop) dwEnv |= 0x00002020; else dwEnv |= 0x00004040; } /* 環境レジスタ4(RENV4)書込み */ dwRet = cp530_wReg(hDevID, axis, 0x9f, dwEnv); return dwRet; } /********************************************************/ /* ELSの設定 */ /* 引数:デバイスハンドル,軸,極性,停止方法 */ /********************************************************/ DWORD hcp530_SetEls(DWORD hDevID, WORD axis, WORD pol, WORD stop) { BYTE byData; DWORD dwRet; /* 戻り値 */ DWORD dwData; /* 軸番号が0〜7以外の時は引数エラー */ if((axis < 0) || (7 < axis)) return ILLEGAL_PRM; /* ELS極性ポート読込み */ dwRet = cp530_rPortB(hDevID, CP53_ELS_PORT, &byData); if(dwRet) return dwRet; if(pol) byData |= (BYTE) (0x01 << axis); /* A接 */ else byData &= (BYTE)~(0x01 << axis); /* B接 */ /* ELS極性ポート書込み */ dwRet = cp530_wPortB(hDevID, CP53_ELS_PORT, byData); if(dwRet) return dwRet; /* 環境レジスタ1(RENV1)読込み */ dwRet = cp530_rReg(hDevID, axis, 0xdc, &dwData); if(dwRet) return dwRet; if(stop) dwData |= (DWORD) 0x8; /* 減速停止 */ else dwData &= (DWORD)~0x8; /* 即停止 */ /* 環境レジスタ1(RENV1)書込み */ dwRet = cp530_wReg(hDevID, axis, 0x9c, dwData); return dwRet; } /********************************************************/ /* OLSの設定 */ /* 引数:デバイスハンドル,軸,極性 */ /********************************************************/ DWORD hcp530_SetOls(DWORD hDevID, WORD axis, WORD pol) { DWORD dwData; DWORD dwRet; /* 戻り値 */ /* 軸番号が0〜7以外の時は引数エラー */ if((axis < 0) || (7 < axis)) return ILLEGAL_PRM; /* 環境レジスタ1(RENV1)読込み */ dwRet = cp530_rReg(hDevID, axis, 0xdc, &dwData); if(dwRet) return dwRet; if(pol) dwData |= (DWORD) 0x80; /* A接 */ else dwData &= (DWORD)~0x80; /* B接 */ /* 環境レジスタ1(RENV1)書込み */ dwRet = cp530_wReg(hDevID, axis, 0x9c, dwData); return dwRet; } /********************************************************/ /* SVALMの設定 */ /* 引数:デバイスハンドル,軸,極性,停止方法 */ /********************************************************/ DWORD hcp530_SetSvAlm(DWORD hDevID, WORD axis, WORD pol, WORD stop) { DWORD dwData; DWORD dwRet; /* 戻り値 */ /* 軸番号が0〜7以外の時は引数エラー */ if((axis < 0) || (7 < axis)) return ILLEGAL_PRM; /* 環境レジスタ1(RENV1)読込み */ dwRet = cp530_rReg(hDevID, axis, 0xdc, &dwData); if(dwRet) return dwRet; if(pol) dwData |= (DWORD) 0x200; /* A接 */ else dwData &= (DWORD)~0x200; /* B接 */ if(stop) dwData |= (DWORD) 0x0100; /* 減速停止 */ else dwData &= (DWORD)~0x0100; /* 即停止 */ /* 環境レジスタ1(RENV1)書込み */ dwRet = cp530_wReg(hDevID, axis, 0x9c, dwData); return dwRet; } /********************************************************/ /* INPOSの設定 */ /* 引数:デバイスハンドル,軸,使用/不使用,極性 */ /********************************************************/ DWORD hcp530_SetInpos(DWORD hDevID, WORD axis, WORD enable, WORD pol) { DWORD dwData; DWORD dwRet; /* 戻り値 */ /* 軸番号が0〜7以外の時は引数エラー */ if((axis < 0) || (7 < axis)) return ILLEGAL_PRM; /* 動作モード(RMD)読込み */ dwRet = cp530_rReg(hDevID, axis, 0xd7, &dwData); if(dwRet) return dwRet; if(enable) dwData |= (DWORD) 0x200; /* 使用 */ else dwData &= (DWORD)~0x200; /* 不使用 */ /* 動作モード(PRMD)書込み */ dwRet = cp530_wReg(hDevID, axis, 0x87, dwData); if(dwRet) return dwRet; if(enable) { /* 環境レジスタ1(RENV1)読込み */ dwRet = cp530_rReg(hDevID, axis, 0xdc, &dwData); if(dwRet) return dwRet; if(pol) dwData |= (DWORD) 0x400000; /* A接 */ else dwData &= (DWORD)~0x400000; /* B接 */ /* 環境レジスタ1(RENV1)書込み */ dwRet = cp530_wReg(hDevID, axis, 0x9c, dwData); } return dwRet; } /************************************************************************/ /* エンコーダZ相の設定 */ /* 引数:デバイスハンドル,軸,原点復帰で使用するZ相回数,極性 */ /************************************************************************/ DWORD hcp530_SetEz(DWORD hDevID, WORD axis, WORD count, WORD pol) { DWORD dwData; DWORD dwRet; /* 戻り値 */ /* 軸番号が0〜7以外の時は引数エラー */ if((axis < 0) || (7 < axis)) return ILLEGAL_PRM; /* 原点復帰で使用するZ相回数 */ count &= 0x000f; /* 環境レジスタ3(RENV3)読込み */ dwRet = cp530_rReg(hDevID, axis, 0xde, &dwData); if(dwRet) return dwRet; dwData &= 0xffffff0f; dwData |= count << 4; /* 環境レジスタ3(RENV3)書込み */ dwRet = cp530_wReg(hDevID, axis, 0x9e, dwData); if(dwRet) return dwRet; /* 極性選択 */ /* 環境レジスタ2(RENV2)読込み */ dwRet = cp530_rReg(hDevID, axis, 0xdd, &dwData); if(dwRet) return dwRet; if(pol) dwData |= (DWORD) 0x800000; /* 立ち下がり */ else dwData &= (DWORD)~0x800000; /* 立ち上がり */ /* 環境レジスタ2(RENV2)書込み */ dwRet = cp530_wReg(hDevID, axis, 0x9d, dwData); return dwRet; } /********************************************************/ /* サーボエラーカウンタクリア出力の設定 */ /* 引数:デバイスハンドル,軸,使用方法 */ /********************************************************/ DWORD hcp530_SetSvCtrCl(DWORD hDevID, WORD axis, WORD enable) { DWORD dwData; DWORD dwRet; /* 戻り値 */ /* 軸番号が0〜7以外または */ /* 使用方法が0〜3以外の時は引数エラー */ if((axis < 0) || (7 < axis)) return 0x100; if((enable < 0) || (enable > 3)) return 0x100; /* 環境レジスタ1(RENV1)読込み */ dwRet = cp530_rReg(hDevID, axis, 0xdc, &dwData); if(dwRet) return dwRet; dwData &= (DWORD)~0xC00; switch(enable) { case 0: break; /* 不使用 */ case 1: dwData |= (DWORD) 0x800; break; /* 原点復帰完了時 */ case 2: dwData |= (DWORD) 0x400; break; /* 異常停止時 */ case 3: dwData |= (DWORD) 0xC00; break; /* 原点復帰完了及び異常停止時 */ } /* 環境レジスタ1(RENV1)書込み */ dwRet = cp530_wReg(hDevID, axis, 0x9c, dwData); return dwRet; } /************************************************************/ /* 指令パルスの出力形式の設定 */ /* 引数:デバイスハンドル,軸, */ /* 指令パルスの出力形式(個別パルス/共通パルス) */ /************************************************************/ DWORD hcp530_SetCmdPulse(DWORD hDevID, WORD axis, WORD cmdpls) { DWORD dwData; DWORD dwRet; /* 戻り値 */ /* 軸番号が0〜7以外 または */ /* 指令パルスの出力形式が0〜3以外の時は引数エラー */ if((axis < 0) || (7 < axis)) return ILLEGAL_PRM; if((cmdpls < 0) || (cmdpls > 3)) return ILLEGAL_PRM; /* 環境レジスタ1(RENV1)読込み */ dwRet = cp530_rReg(hDevID, axis, 0xdc, &dwData); if(dwRet) return dwRet; dwData &= (DWORD)~0x7; switch(cmdpls) { case 0: dwData |= (DWORD)0x04; break; /* 個別 */ case 1: dwData |= (DWORD)0x02; break; /* 共通 */ case 2: dwData |= (DWORD)0x07; break; case 3: dwData |= (DWORD)0x00; break; } /* 環境レジスタ1(RENV1)書込み */ dwRet = cp530_wReg(hDevID, axis, 0x9c, dwData); return dwRet; } /************************************************************/ /* 加減速形式設定 */ /* 引数:デバイスハンドル,軸,加減速形式(直線/S字) */ /************************************************************/ DWORD hcp530_SetAccProfile(DWORD hDevID, WORD axis, WORD accpro) { DWORD dwData; DWORD dwRet; /* 戻り値 */ /* 軸番号が0〜7以外の時は引数エラー */ if((axis < 0) || (7 < axis)) return ILLEGAL_PRM; /* 動作モード(RMD)読込み */ dwRet = cp530_rReg(hDevID, axis, 0xd7, &dwData); if(dwRet) return dwRet; if(accpro) dwData |= (DWORD) 0x400; /* S字加減速 */ else dwData &= (DWORD)~0x400; /* 直線加減速 */ /* 動作モード(PRMD)書込み */ dwRet = cp530_wReg(hDevID, axis, 0x87, dwData); return dwRet; } /************************************************************************/ /* 減速点の設定方式 */ /* 引数:デバイスハンドル,軸,減速点の設定方式(自動/マニュアル) */ /************************************************************************/ DWORD hcp530_SetAutoDec(DWORD hDevID, WORD axis, WORD para) { DWORD dwData; DWORD dwRet; /* 戻り値 */ /* 軸番号が0〜7以外の時は引数エラー */ if((axis < 0) || (7 < axis)) return ILLEGAL_PRM; /* 動作モード(RMD)読込み */ dwRet = cp530_rReg(hDevID, axis, 0xd7, &dwData); if(dwRet) return dwRet; if(para) dwData |= (DWORD) 0x2000; /* マニュアル */ else dwData &= (DWORD)~0x2000; /* 自動 */ /* 動作モード(PRMD)書込み */ dwRet = cp530_wReg(hDevID, axis, 0x87, dwData); return dwRet; } /************************************************************/ /* DLS/PCS切替設定 */ /* 引数:デバイスハンドル,軸,DLS/PCS使用切り替え, */ /* 極性,DLS入力時の動作,ラッチする/しない */ /************************************************************/ DWORD hcp530_SetDlsSel(DWORD hDevID, WORD axis, WORD enable, WORD pol, WORD motion, WORD latch) { BYTE byData; DWORD dwRmd; DWORD dwEnv; DWORD dwRet; /* 戻り値 */ /* 軸番号が0〜7以外 または */ /* DLS/PCS使用切り替えが0〜2以外の時は引数エラー */ if((axis < 0) || (7 < axis)) return ILLEGAL_PRM; if((enable < 0) || (enable > 2)) return ILLEGAL_PRM; /* DLS/PCS切替ポート読込 */ dwRet = cp530_rPortB(hDevID, CP53_DLS_PORT, &byData); if(dwRet) return dwRet; /* 動作モード(RMD)読込 */ dwRet = cp530_rReg(hDevID, axis, 0xd7, &dwRmd); if(dwRet) return dwRet; /* 環境レジスタ1(RENV1)読込み */ dwRet = cp530_rReg(hDevID, axis, 0xdc, &dwEnv); if(dwRet) return dwRet; /* DLS/PCS使用選択 */ switch(enable) { case 0: byData &= (BYTE)~(0x01 << axis); /* DLS入力 */ dwRmd |= (DWORD) 0x100; /* DLS使用 */ dwEnv &= (DWORD)~0x1000000; /* PCS B接 */ if(pol) dwEnv |= (DWORD) 0x40; /* DLS A接 */ else dwEnv &= (DWORD)~0x40; /* DLS B接 */ break; case 1: byData |= (BYTE) (0x01 << axis); /* PCS入力 */ dwRmd &= (DWORD)~0x100; /* DLS不使用 */ dwEnv &= (DWORD)~0x40; /* DLS B接 */ if(pol) dwEnv |= (DWORD) 0x1000000; /* PCS A接 */ else dwEnv &= (DWORD)~0x1000000; /* PCS B接 */ break; case 2: byData &= (BYTE)~(0x01 << axis); /* DLS, PCSとも不使用 */ dwRmd &= (DWORD)~0x100; /* DLS不使用 */ break; default : return ILLEGAL_PRM; /* 引数エラー */ } /* DLS入力時の動作選択 */ if(motion) dwEnv |= (DWORD) 0x10; /* 減速停止 */ else dwEnv &= (DWORD)~0x10; /* 減速のみ */ /* DLS入力ラッチ選択 */ if(latch) dwEnv |= (DWORD) 0x20; /* ラッチする */ else dwEnv &= (DWORD)~0x20; /* ラッチしない */ /* 環境レジスタ1(RENV1)書込み */ dwRet = cp530_wReg(hDevID, axis, 0x9c, dwEnv); if(dwRet) return dwRet; /* 動作モード(PRMD)書込み */ dwRet = cp530_wReg(hDevID, axis, 0x87, dwRmd); if(dwRet) return dwRet; /* DLS/PCS切替ポート書込み */ dwRet = cp530_wPortB(hDevID, CP53_DLS_PORT, byData); return dwRet; } /*===============================================================*/ /* 読込み系の関数 */ /*===============================================================*/ /********************************************************/ /* メインステータス読込み */ /* 引数:デバイスハンドル,軸,メインステータス */ /********************************************************/ DWORD hcp530_ReadMainSts(DWORD hDevID, WORD axis, WORD* msts) { WORD wData; DWORD dwRet; /* 戻り値 */ /* 軸番号が0〜7以外の時は引数エラー */ if((axis < 0) || (7 < axis)) return ILLEGAL_PRM; /* メインステータス読込み */ dwRet = cp530_rMstsW(hDevID, axis, &wData); if(dwRet) return dwRet; *msts = wData; return 0; } /************************************************************/ /* エラーステータス読込み */ /* 引数:デバイスハンドル,軸,エラーステータス */ /************************************************************/ DWORD hcp530_ReadErrorSts(DWORD hDevID, WORD axis, DWORD* ests) { DWORD dwData; DWORD dwRet; /* 戻り値 */ /* 軸番号が0〜7以外の時は引数エラー */ if((axis < 0) || (7 < axis)) return ILLEGAL_PRM; /* エラーステータス読込み */ dwRet = cp530_rReg(hDevID, axis, 0xf2, &dwData); if(dwRet) return dwRet; *ests = dwData; return 0; } /************************************************************/ /* イベントステータス読込み */ /* 引数:デバイスハンドル,軸,イベントステータス */ /************************************************************/ DWORD hcp530_ReadEventSts(DWORD hDevID, WORD axis, DWORD* ists) { DWORD dwData; DWORD dwRet; /* 戻り値 */ /* 軸番号が0〜7以外の時は引数エラー */ if((axis < 0) || (7 < axis)) return ILLEGAL_PRM; /* イベントステータス読込み */ dwRet = cp530_rReg(hDevID, axis, 0xf3, &dwData); if(dwRet) return dwRet; *ists = dwData; return 0; } /********************************************************/ /* サブステータス読込み */ /* 引数:デバイスハンドル,軸,サブステータス */ /********************************************************/ DWORD hcp530_ReadSubSts(DWORD hDevID, WORD axis, WORD* ssts) { WORD wData; DWORD dwRet; /* 戻り値 */ /* 軸番号が0〜7以外の時は引数エラー */ if((axis < 0) || (7 < axis)) return ILLEGAL_PRM; /* サブステータス読込み */ dwRet = cp530_rSstsW(hDevID, axis, &wData); if(dwRet) return dwRet; *ssts = wData; return 0; } /********************************************************/ /* 拡張ステータス読込み */ /* 引数:デバイスハンドル,軸,拡張ステータス */ /********************************************************/ DWORD hcp530_ReadExSts(DWORD hDevID, WORD axis, DWORD* exsts) { DWORD dwData; DWORD dwRet; /* 戻り値 */ /* 軸番号が0〜7以外の時は引数エラー */ if((axis < 0) || (7 < axis)) return ILLEGAL_PRM; /* 拡張ステータス読込み */ dwRet = cp530_rReg(hDevID, axis, 0xf1, &dwData); if(dwRet) return dwRet; *exsts = dwData; return 0; } /****************************************************/ /* 補間ステータス読込み */ /* 引数:デバイスハンドル,軸,補間ステータス */ /****************************************************/ DWORD hcp530_ReadIpSts(DWORD hDevID, DWORD* ipsts) { DWORD dwData; DWORD dwRet; /* 戻り値 */ /* 補間ステータス読込み */ dwRet = cp530_rReg(hDevID, 0, 0xff, &dwData); if(dwRet) return dwRet; *ipsts = dwData; return 0; } /********************************************/ /* 速度読込み */ /* 引数:デバイスハンドル,軸,速度 */ /********************************************/ DWORD hcp530_ReadSpd(DWORD hDevID, WORD axis, WORD* spd) { DWORD dwData; DWORD dwRet; /* 戻り値 */ /* 軸番号が0〜7以外の時は引数エラー */ if((axis < 0) || (7 < axis)) return ILLEGAL_PRM; /* 速度読込 */ dwRet = cp530_rReg(hDevID, axis, 0xf5, &dwData); if(dwRet) return dwRet; dwData &= (DWORD)0x0000ffff; *spd = (WORD)dwData; return 0; } /****************************************************************/ /* カウンタ読込み */ /* 引数:デバイスハンドル,軸,読込カウンタ,カウンタ値 */ /****************************************************************/ DWORD hcp530_ReadCtr(DWORD hDevID, WORD axis, WORD selctr, long* count) { DWORD dwData; DWORD dwRet; /* 戻り値 */ /* 軸番号が0〜7以外または */ /* 読込カウンタが1〜4以外の時は引数エラー */ if((axis < 0) || (7 < axis)) return ILLEGAL_PRM; if((selctr < 1) || (selctr > 4)) return ILLEGAL_PRM; /* カウンタ読込 */ dwRet = cp530_rReg(hDevID, axis, (BYTE)(0xe2 + selctr), &dwData); if(dwRet) return dwRet; *count = dwData; return 0; } /*===============================================================*/ /* 動作設定系の関数 */ /*===============================================================*/ /************************************************************/ /* イベントマスクの設定 */ /* 引数:デバイスハンドル,軸,イベントマスクデータ */ /************************************************************/ DWORD hcp530_SetEventMask(DWORD hDevID, WORD axis, DWORD mask) { DWORD dwRet; /* 戻り値 */ /* 軸番号が0〜7以外または */ /* イベントマスクデータが 0〜0x0003ffff以外の時は引数エラー */ if((axis < 0) || (7 < axis)) return ILLEGAL_PRM; if(mask & 0xfffc0000) return ILLEGAL_PRM; /* イベントマスク(RIRQ)レジスタの書込み */ dwRet = cp530_wReg(hDevID, axis, 0xac, mask); return dwRet; } /********************************************************/ /* ベース速度の設定 */ /* 引数:デバイスハンドル,軸,ベース速度レジスタ */ /********************************************************/ DWORD hcp530_SetFLSpd(DWORD hDevID, WORD axis, DWORD spd) { DWORD dwRet; /* 戻り値 */ /* 軸番号が0〜7以外または */ /* 速度レジスタが1〜0xffff以外の時は引数エラー */ if((axis < 0) || (7 < axis)) return ILLEGAL_PRM; if((!spd) || (spd > 0xffff)) return ILLEGAL_PRM; /* PRFLの書込み */ dwRet = cp530_wReg(hDevID, axis, 0x81, spd); return dwRet; } /********************************************************/ /* 補助速度の設定 */ /* 引数:デバイスハンドル,軸,補助速度レジスタ */ /********************************************************/ DWORD hcp530_SetAuxSpd(DWORD hDevID, WORD axis, DWORD spd) { DWORD dwRet; /* 戻り値 */ /* 軸番号が0〜7以外または */ /* 速度レジスタが1〜0xffff以外の時は引数エラー */ if((axis < 0) || (7 < axis)) return ILLEGAL_PRM; if((!spd) || (spd > 0xffff)) return ILLEGAL_PRM; /* RFAの書込み */ dwRet = cp530_wReg(hDevID, axis, 0x9b, spd); return dwRet; } /********************************************************************************/ /* 加速レートの設定 */ /* 引数:デバイスハンドル,軸,加速レート */ /* 基準クロック周波数 = 19660800Hz */ /* 直線加速時加速時間(秒) = (PRFH - PRFL) * (PRUR + 1) * 4 / 19660800 */ /* S字加速時加速時間(秒) = (PRFH - PRFL) * (PRUR + 1) * 8 / 19660800 */ /********************************************************************************/ DWORD hcp530_SetAccRate(DWORD hDevID, WORD axis, DWORD rate) { DWORD dwRet; /* 戻り値 */ /* 軸番号が0〜7以外または */ /* 加速レートが1〜0xffff以外の時は引数エラー */ if((axis < 0) || (7 < axis)) return ILLEGAL_PRM; if((!rate) || (rate > 0xffff)) return ILLEGAL_PRM; /* PRURの書込み */ dwRet = cp530_wReg(hDevID, axis, 0x83, rate); return dwRet; } /********************************************************/ /* 減速レートの設定 */ /* 引数:デバイスハンドル,軸,減速レート */ /********************************************************/ DWORD hcp530_SetDecRate(DWORD hDevID, WORD axis, DWORD rate) { DWORD dwRet; /* 戻り値 */ /* 軸番号が0〜7以外の時は引数エラー */ if((axis < 0) || (7 < axis)) return ILLEGAL_PRM; if(rate > 0xffff) return ILLEGAL_PRM; /* PRDRの書込み */ dwRet = cp530_wReg(hDevID, axis, 0x84, rate); return dwRet; } /********************************************************/ /* 倍率設定値(MG)の設定 */ /* 引数:デバイスハンドル,軸,倍率設定値(MG) */ /* MG = 300 / 倍率 - 1 */ /********************************************************/ DWORD hcp530_SetMult(DWORD hDevID, WORD axis, DWORD rmg) { DWORD dwRet; /* 戻り値 */ /* 軸番号が0〜7以外または */ /* 倍率設定値が1以下の時は引数エラー */ if((axis < 0) || (7 < axis)) return ILLEGAL_PRM; if(rmg < 2) return ILLEGAL_PRM; /* PRMGの書込み */ dwRet = cp530_wReg(hDevID, axis, 0x85, rmg); return dwRet; } /************************************************/ /* 減速点の設定 */ /* 引数:デバイスハンドル,軸,減速点 */ /************************************************/ DWORD hcp530_SetDecPoint(DWORD hDevID, WORD axis, long dstnc) { DWORD dwRet; /* 戻り値 */ /* 軸番号が0〜7以外の時は引数エラー */ if((axis < 0) || (7 < axis)) return ILLEGAL_PRM; /* PRDPの書込み */ dwRet = cp530_wReg(hDevID, axis, 0x86, dstnc); return dwRet; } /*==============================================================*/ /* 運用設定系の関数 */ /*==============================================================*/ /****************************************************/ /* 動作モード書込み */ /* 引数:デバイスハンドル,軸,動作モード */ /****************************************************/ DWORD hcp530_WritOpeMode(DWORD hDevID, WORD axis, WORD mode) { DWORD dwData; DWORD dwRet; /* 戻り値 */ /* 軸番号が0〜7以外または */ /* 動作モードが異常の時は引数エラー */ if((axis < 0) || (7 < axis)) return ILLEGAL_PRM; if(mode & 0xff80) return ILLEGAL_PRM; if(!mod_tbl[mode]) return ILLEGAL_PRM; /* 動作モード(RMD)読込み */ dwRet = cp530_rReg(hDevID, axis, 0xd7, &dwData); if(dwRet) return dwRet; dwData &= 0xef3a700; if(mode == 0x42) dwData |= 0x4041; else dwData |= mode; /* 動作モード(PRMD)書込み */ dwRet = cp530_wReg(hDevID, axis, 0x87, dwData); return dwRet; } /********************************************************/ /* 動作速度レジスタの設定 */ /* 引数:デバイスハンドル,軸,動作速度レジスタ */ /********************************************************/ DWORD hcp530_WritFHSpd(DWORD hDevID, WORD axis, DWORD spd) { DWORD dwRet; /* 戻り値 */ /* 軸番号が0〜7以外または */ /* 速度レジスタが1〜0xffff以外の時は引数エラー */ if((axis < 0) || (7 < axis)) return ILLEGAL_PRM; if((!spd) || (spd > 0xffff)) return ILLEGAL_PRM; /* PRFHの書込み */ dwRet = cp530_wReg(hDevID, axis, 0x82, spd); return dwRet; } /************************************************/ /* 位置決め移動量の設定 */ /* 引数:デバイスハンドル,軸,移動量 */ /************************************************/ DWORD hcp530_WritPos(DWORD hDevID, WORD axis, long dstnc) { DWORD dwRet; /* 戻り値 */ /* 軸番号が0〜7以外の時は引数エラー */ if((axis < 0) || (7 < axis)) return ILLEGAL_PRM; /* 移動量のチェック */ if((dstnc < -134217728) || (134217727 < dstnc)) return ILLEGAL_PRM; /* PRMVの書込み */ dwRet = cp530_wReg(hDevID, axis, 0x80, dstnc); return dwRet; } /************************************************/ /* 直線補間の移動量の設定 */ /* 引数:デバイスハンドル,軸,移動量 */ /************************************************/ DWORD hcp530_WritLine(DWORD hDevID, WORD axis, long dstnc) { DWORD dwRet; /* 戻り値 */ /* 軸番号が0〜7以外の時は引数エラー */ if((axis < 0) || (7 < axis)) return ILLEGAL_PRM; /* 移動量のチェック */ if((dstnc < -134217728) || (134217727 < dstnc)) return ILLEGAL_PRM; /* PRMVの書込み */ dwRet = cp530_wReg(hDevID, axis, 0x80, dstnc); return dwRet; } /************************************************************/ /* 円弧補間のデータ設定 */ /* 引数:デバイスハンドル,軸組み合わせ,終点位置1, */ /* 終点位置2,中心位置1,中心位置2 */ /************************************************************/ DWORD hcp530_WritCircl(DWORD hDevID, WORD axis, long dstnc1, long dstnc2, long center1,long center2) { DWORD dwRet; /* 戻り値 */ WORD ax1,ax2; /* 補間軸の組み合わせの引数が0〜11以外の時はエラー */ if((axis < 0) || (axis > 11)) return ILLEGAL_PRM; if((dstnc1 < -134217728) || (134217727 < dstnc1)) return ILLEGAL_PRM; if((dstnc2 < -134217728) || (134217727 < dstnc2)) return ILLEGAL_PRM; if((center1 < -134217728) || (134217727 < center1)) return ILLEGAL_PRM; if((center2 < -134217728) || (134217727 < center2)) return ILLEGAL_PRM; switch(axis) { case 0: ax1 = 0; ax2 = 1; break; /* X(0), Y(1) */ case 1: ax1 = 0; ax2 = 2; break; /* X(0), Z(2) */ case 2: ax1 = 0; ax2 = 3; break; /* X(0), U(3) */ case 3: ax1 = 1; ax2 = 2; break; /* Y(1), Z(2) */ case 4: ax1 = 1; ax2 = 3; break; /* Y(1), U(3) */ case 5: ax1 = 2; ax2 = 3; break; /* Z(2), U(3) */ case 6: ax1 = 4; ax2 = 5; break; /* V(4), W(5) */ case 7: ax1 = 4; ax2 = 6; break; /* V(4), A(6) */ case 8: ax1 = 4; ax2 = 7; break; /* V(4). B(7) */ case 9: ax1 = 5; ax2 = 6; break; /* W(5), A(6) */ case 10: ax1 = 5; ax2 = 7; break; /* W(5), B(7) */ case 11: ax1 = 6; ax2 = 7; break; /* A(6), B(7) */ } /* PRMV(終点位置)の書込み */ dwRet = cp530_wReg(hDevID, ax1, 0x80, dstnc1); if(dwRet) return dwRet; /* X,X,X,Y,Y,Z,V,V,V,W,W,A */ dwRet = cp530_wReg(hDevID, ax2, 0x80, dstnc2); /* Y,Z,U,Z,U,U,W,A,B,A,B,B */ if(dwRet) return dwRet; /* PRIP(中心位置)の書込み */ dwRet = cp530_wReg(hDevID, ax1, 0x88, center1); if(dwRet) return dwRet; /* X,X,X,Y,Y,Z,V,V,V,W,W,A */ dwRet = cp530_wReg(hDevID, ax2, 0x88, center2); /* Y,Z,U,Z,U,U,W,A,B,A,B,B */ return dwRet; } /****************************************************************/ /* カウンタプリセット */ /* 引数:デバイスハンドル,軸、 プリセット値,カウンタ選択 */ /****************************************************************/ DWORD hcp530_WritCtr(DWORD hDevID, WORD axis, long preset, WORD selctr) { BYTE byCmd; DWORD dwRet; /* 戻り値 */ /* 軸番号0〜7以外またはカウンタが1〜4以外の時はエラー */ if((axis < 0) || (7 < axis)) return ILLEGAL_PRM; if((selctr < 1) || (selctr > 4)) return ILLEGAL_PRM; /* プリセット値のチェック */ if((preset < -134217728) || (134217727 < preset)) return ILLEGAL_PRM; /* プリセット値書込み */ byCmd = (BYTE)(0xa2 + selctr); /* カウンタ選択 */ dwRet = cp530_wReg(hDevID, axis, byCmd, preset); return dwRet; } /*==============================================================*/ /* 動作制御系の関数 */ /*==============================================================*/ /****************************************************/ /* 減速停止 */ /* 引数:デバイスハンドル,軸組み合わせ(HEX) */ /****************************************************/ DWORD hcp530_DecStop(DWORD hDevID, WORD axis) { DWORD dwRet = 0; /* 戻り値 */ /* 軸番号0x01〜0xff以外の時はエラー */ if((axis <= 0) || (axis > 0xff)) return ILLEGAL_PRM; /* X - U : X軸に減速停止 */ if(axis & 0x0f) dwRet = cp530_wCmdW(hDevID, 0, (WORD)(((axis & 0x0f) << 8) | 0x4a)); /* V - B : V軸に減速停止 */ if( (axis & 0xf0) && (!dwRet) ) dwRet = cp530_wCmdW(hDevID, 4, (WORD)(((axis & 0xf0) << 4) | 0x4a)); return dwRet; } /****************************************************/ /* 即停止 */ /* 引数:デバイスハンドル,軸組み合わせ(HEX) */ /****************************************************/ DWORD hcp530_QuickStop(DWORD hDevID, WORD axis) { DWORD dwRet = 0; /* 戻り値 */ /* 軸番号0x01〜0xff以外の時はエラー */ if((axis <= 0) || (axis > 0xff)) return ILLEGAL_PRM; /* X - U : X軸に即停止 */ if(axis & 0x0f) dwRet = cp530_wCmdW(hDevID, 0, (WORD)(((axis & 0x0f) << 8) | 0x49)); /* V - B : V軸に即停止 */ if( (axis & 0xf0) && (!dwRet) ) dwRet = cp530_wCmdW(hDevID, 4, (WORD)(((axis & 0xf0) << 4) | 0x49)); return dwRet; } /****************************************************/ /* 非常停止 */ /* 引数:デバイスハンドル,軸組み合わせ(HEX) */ /****************************************************/ DWORD hcp530_EmgStop(DWORD hDevID, WORD axis) { DWORD dwRet = 0; /* 戻り値 */ /* 軸番号0x01〜0xff以外の時はエラー */ if((axis <= 0) || (axis > 0xff)) return ILLEGAL_PRM; /* X - U : X軸に非常停止 */ if(axis & 0x0f) dwRet = cp530_wCmdW(hDevID, 0, (WORD)(((axis & 0x0f) << 8) | 0x05)); /* V - B : V軸に非常停止 */ if( (axis & 0xf0) && (!dwRet) ) dwRet = cp530_wCmdW(hDevID, 4, (WORD)(((axis & 0xf0) << 4) | 0x05)); return dwRet; } /****************************************************/ /* 同時減速停止 */ /* 引数:デバイスハンドル,軸組み合わせ(HEX) */ /****************************************************/ DWORD hcp530_SyDecStop(DWORD hDevID, WORD axis) { WORD wCmd; WORD wAx; DWORD dwData; DWORD dwRet; /* 戻り値 */ WORD axbit[8] = { 0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80 }; /* 軸番号0x01〜0xff以外の時はエラー */ if((axis <= 0) || (axis > 0xff)) return ILLEGAL_PRM; for(wAx=0; wAx<8; wAx++) { if(axis & axbit[wAx]) { /* 即停止:指令軸 */ /* 環境レジスタ1(RENV1): 減速停止 */ dwRet = cp530_rReg(hDevID, wAx, 0xdc, &dwData); if(dwRet) return dwRet; dwData |= (DWORD) 0x80000; /* 減速停止 */ dwRet = cp530_wReg(hDevID, wAx, 0x9c, dwData); if(dwRet) return dwRet; /* 動作モード(RMD): 同時停止 */ dwRet = cp530_rReg(hDevID, wAx, 0xd7, &dwData); if(dwRet) return dwRet; dwData |= 0x1000000; dwRet = cp530_wReg(hDevID, wAx, 0x97, dwData); if(dwRet) return dwRet; } else { /* 即停止:非指令軸 */ /* 動作モード(RMD): 同時停止キャンセル */ dwRet = cp530_rReg(hDevID, wAx, 0xd7, &dwData); if(dwRet) return dwRet; dwData &= 0x0effffff; dwRet = cp530_wReg(hDevID, wAx, 0x97, dwData); if(dwRet) return dwRet; } } axis &= 0xf0; wCmd = (axis << 4) | 0x07; /* V〜B軸 + 同時停止 */ dwRet = cp530_wCmdW(hDevID, 4, wCmd); /* V軸:コマンド書込み */ /* 動作モード(RMD): 同時停止キャンセル */ for(wAx = 0; wAx < 8; wAx++) { if(axis & axbit[wAx]) { dwRet = cp530_rReg(hDevID, wAx, 0xd7, &dwData); if(dwRet) return dwRet; dwData &= 0xeffffff; dwRet = cp530_wReg(hDevID, wAx, 0x87, dwData); if(dwRet) return dwRet; } } return dwRet; } /****************************************************/ /* 同時即停止 */ /* 引数:デバイスハンドル,軸組み合わせ(HEX) */ /****************************************************/ DWORD hcp530_SyQuickStop(DWORD hDevID, WORD axis) { WORD wCmd; WORD wAx; DWORD dwData; DWORD dwRet; /* 戻り値 */ WORD axbit[8] = { 0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80 }; /* 軸番号0x01〜0xff以外の時はエラー */ if((axis <= 0) || (axis > 0xff)) return ILLEGAL_PRM; for(wAx=0; wAx<8; wAx++) { if(axis & axbit[wAx]) { /* 即停止:指令軸 */ /* 環境レジスタ1(RENV1): 即停止 */ dwRet = cp530_rReg(hDevID, wAx, 0xdc, &dwData); if(dwRet) return dwRet; dwData &= (DWORD)0xfff7ffff; /* 即停止 */ dwRet = cp530_wReg(hDevID, wAx, 0x9c, dwData); if(dwRet) return dwRet; /* 動作モード(RMD): 同時停止 */ dwRet = cp530_rReg(hDevID, wAx, 0xd7, &dwData); if(dwRet) return dwRet; dwData |= 0x01000000; /* 同時停止 */ dwRet = cp530_wReg(hDevID, wAx, 0x97, dwData); if(dwRet) return dwRet; } else { /* 即停止:非指令軸 */ /* 動作モード(RMD): 同時停止キャンセル */ dwRet = cp530_rReg(hDevID, wAx, 0xd7, &dwData); if(dwRet) return dwRet; dwData &= 0x0effffff; dwRet = cp530_wReg(hDevID, wAx, 0x97, dwData); if(dwRet) return dwRet; } } axis &= 0xf0; wCmd = (axis << 4) | 0x07; /* V〜B軸 + 同時停止 */ dwRet = cp530_wCmdW(hDevID, 4, wCmd); /* V軸:コマンド書込み */ /* 動作モード(PRMD): 同時停止キャンセル */ for(wAx = 0; wAx < 8; wAx++) { if(axis & axbit[wAx]) { dwRet = cp530_rReg(hDevID, wAx, 0xd7, &dwData); if(dwRet) return dwRet; dwData &= 0xeffffff; dwRet = cp530_wReg(hDevID, wAx, 0x87, dwData); if(dwRet) return dwRet; } } return dwRet; } /****************************************************/ /* 加速スタート */ /* 引数:デバイスハンドル,軸組み合わせ(HEX) */ /****************************************************/ DWORD hcp530_AccStart(DWORD hDevID, WORD axis) { WORD wCmd; WORD wAx; DWORD dwRet; /* 戻り値 */ DWORD dwData; WORD axbit[8] = { 0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80 }; /* 軸番号0x01〜0xff以外の時はエラー */ if((axis <= 0) || (axis > 0xff)) return ILLEGAL_PRM; if(!(axis & 0xf0)) { /* X〜U軸(4軸まで) */ wCmd = (axis << 8) | 0x53; dwRet = cp530_wCmdW(hDevID, 0, wCmd); /* コマンド書込み */ } else { /* 5軸以上 */ if(!(axis & 0x0f)) { /* 5軸以上、但しV〜B軸 */ wCmd = (axis << 4) | 0x53; dwRet = cp530_wCmdW(hDevID, 4, wCmd); /* コマンド書込み */ } else { /* 5軸以上:X〜U軸とV〜B軸が混在 */ for(wAx = 0; wAx < 8; wAx++) { if(axis & axbit[wAx]) { /* 加速スタート:指令軸 */ /* 動作モード(RMD): 同時スタート */ dwRet = cp530_rReg(hDevID, wAx, 0xd7, &dwData); if(dwRet) return dwRet; dwData |= 0x40000; dwRet = cp530_wReg(hDevID, wAx, 0x97, dwData); if(dwRet) return dwRet; dwRet = cp530_wCmdW(hDevID,wAx, 0x53); /* スタートコマンド書込み */ } else { /* 加速スタート:非指令軸 */ /* 動作モード(RMD): 即スタート(非同時スタート) */ dwRet = cp530_rReg(hDevID, wAx, 0xd7, &dwData); if(dwRet) return dwRet; dwData &= 0xff3ffff; dwRet = cp530_wReg(hDevID, wAx, 0x97, dwData); if(dwRet) return dwRet; } } axis &= 0xf0; wCmd = (axis << 4) | 0x06; /* 同時スタート */ dwRet = cp530_wCmdW(hDevID, 4, wCmd); /* V軸:コマンド書込み */ for(wAx = 0; wAx < 8; wAx++) { if(axis & axbit[wAx]) { /* 動作モード(PRMD): 即スタート(非同時スタート) */ dwRet = cp530_rReg(hDevID, wAx, 0xd7, &dwData); if(dwRet) return dwRet; dwData &= 0xff3ffff; /* 動作モード(PRMD)書込み */ dwRet = cp530_wReg(hDevID, wAx, 0x87, dwData); if(dwRet) return dwRet; } } } } return dwRet; } /****************************************************/ /* FH定速スタート */ /* 引数:デバイスハンドル,軸組み合わせ(HEX) */ /****************************************************/ DWORD hcp530_CnstStartFH(DWORD hDevID, WORD axis) { WORD wCmd; WORD wAx; DWORD dwRet; /* 戻り値 */ DWORD dwData; WORD axbit[8] = { 0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80 }; /* 軸番号0x01〜0xff以外の時はエラー */ if((axis <= 0) || (axis > 0xff)) return ILLEGAL_PRM; if(!(axis & 0xf0)) { /* X〜U軸(4軸まで) */ wCmd = (axis << 8) | 0x51; dwRet = cp530_wCmdW(hDevID, 0, wCmd); /* コマンド書込み */ } else { /* 5軸以上 */ if(!(axis & 0x0f)) { /* 5軸以上、但しV〜B軸 */ wCmd = (axis << 4) | 0x51; dwRet = cp530_wCmdW(hDevID, 4, wCmd); /* コマンド書込み */ } else { /* 5軸以上:X〜U軸とV〜B軸が混在 */ for(wAx = 0; wAx < 8; wAx++) { if(axis & axbit[wAx]) { /* 加速スタート:指令軸 */ /* 動作モード(RMD): 同時スタート */ dwRet = cp530_rReg(hDevID, wAx, 0xd7, &dwData); if(dwRet) return dwRet; dwData |= 0x40000; dwRet = cp530_wReg(hDevID, wAx, 0x97, dwData); if(dwRet) return dwRet; dwRet = cp530_wCmdW(hDevID,wAx, 0x51); /* スタートコマンド書込み */ } else { /* 加速スタート:非指令軸 */ /* 動作モード(RMD): 即スタート(非同時スタート) */ dwRet = cp530_rReg(hDevID, wAx, 0xd7, &dwData); if(dwRet) return dwRet; dwData &= 0xff3ffff; dwRet = cp530_wReg(hDevID, wAx, 0x97, dwData); if(dwRet) return dwRet; } } axis &= 0xf0; wCmd = (axis << 4) | 0x06; /* 同時スタート */ dwRet = cp530_wCmdW(hDevID, 4, wCmd); /* V軸:コマンド書込み */ for(wAx = 0; wAx < 8; wAx++) { if(axis & axbit[wAx]) { /* 動作モード(PRMD): 即スタート(非同時スタート) */ dwRet = cp530_rReg(hDevID, wAx, 0xd7, &dwData); if(dwRet) return dwRet; dwData &= 0xff3ffff; /* 動作モード(PRMD)書込み */ dwRet = cp530_wReg(hDevID, wAx, 0x87, dwData); if(dwRet) return dwRet; } } } } return dwRet; } /****************************************************/ /* FL定速スタート */ /* 引数:デバイスハンドル,軸組み合わせ(HEX) */ /****************************************************/ DWORD hcp530_CnstStartFL(DWORD hDevID, WORD axis) { WORD wCmd; WORD wAx; DWORD dwRet; /* 戻り値 */ DWORD dwData; WORD axbit[8] = { 0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80 }; /* 軸番号0x01〜0xff以外の時はエラー */ if((axis <= 0) || (axis > 0xff)) return ILLEGAL_PRM; if(!(axis & 0xf0)) { /* X〜U軸(4軸まで) */ wCmd = (axis << 8) | 0x50; dwRet = cp530_wCmdW(hDevID, 0, wCmd); /* コマンド書込み */ } else { /* 5軸以上 */ if(!(axis & 0x0f)) { /* 5軸以上、但しV〜B軸 */ wCmd = (axis << 4) | 0x50; dwRet = cp530_wCmdW(hDevID, 4, wCmd); /* コマンド書込み */ } else { /* 5軸以上:X〜U軸とV〜B軸が混在 */ for(wAx = 0; wAx < 8; wAx++) { if(axis & axbit[wAx]) { /* 加速スタート:指令軸 */ /* 動作モード(RMD): 同時スタート */ dwRet = cp530_rReg(hDevID, wAx, 0xd7, &dwData); if(dwRet) return dwRet; dwData |= 0x40000; dwRet = cp530_wReg(hDevID, wAx, 0x97, dwData); if(dwRet) return dwRet; dwRet = cp530_wCmdW(hDevID,wAx, 0x50); /* スタートコマンド書込み */ } else { /* 加速スタート:非指令軸 */ /* 動作モード(RMD): 即スタート(非同時スタート) */ dwRet = cp530_rReg(hDevID, wAx, 0xd7, &dwData); if(dwRet) return dwRet; dwData &= 0xff3ffff; dwRet = cp530_wReg(hDevID, wAx, 0x97, dwData); if(dwRet) return dwRet; } } axis &= 0xf0; wCmd = (axis << 4) | 0x06; /* 同時スタート */ dwRet = cp530_wCmdW(hDevID, 4, wCmd); /* V軸:コマンド書込み */ for(wAx = 0; wAx < 8; wAx++) { if(axis & axbit[wAx]) { /* 動作モード(PRMD): 即スタート(非同時スタート) */ dwRet = cp530_rReg(hDevID, wAx, 0xd7, &dwData); if(dwRet) return dwRet; dwData &= 0xff3ffff; /* 動作モード(PRMD)書込み */ dwRet = cp530_wReg(hDevID, wAx, 0x87, dwData); if(dwRet) return dwRet; } } } } return dwRet; } /****************************************************/ /* FH定速スタート後減速停止 */ /* 引数:デバイスハンドル,軸組み合わせ(HEX) */ /****************************************************/ DWORD hcp530_ConstStartByDec(DWORD hDevID, WORD axis) { WORD wCmd; WORD wAx; DWORD dwRet; /* 戻り値 */ DWORD dwData; WORD axbit[8] = { 0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80 }; /* 軸番号0x01〜0xff以外の時はエラー */ if((axis <= 0) || (axis > 0xff)) return ILLEGAL_PRM; if(!(axis & 0xf0)) { /* X〜U軸(4軸まで) */ wCmd = (axis << 8) | 0x52; dwRet = cp530_wCmdW(hDevID, 0, wCmd); /* コマンド書込み */ } else { /* 5軸以上 */ if(!(axis & 0x0f)) { /* 5軸以上、但しV〜B軸 */ wCmd = (axis << 4) | 0x52; dwRet = cp530_wCmdW(hDevID, 4, wCmd); /* コマンド書込み */ } else { /* 5軸以上:X〜U軸とV〜B軸が混在 */ for(wAx = 0; wAx < 8; wAx++) { if(axis & axbit[wAx]) { /* 加速スタート:指令軸 */ /* 動作モード(RMD): 同時スタート */ dwRet = cp530_rReg(hDevID, wAx, 0xd7, &dwData); if(dwRet) return dwRet; dwData |= 0x40000; dwRet = cp530_wReg(hDevID, wAx, 0x97, dwData); if(dwRet) return dwRet; dwRet = cp530_wCmdW(hDevID,wAx, 0x52); /* スタートコマンド書込み */ } else { /* 加速スタート:非指令軸 */ /* 動作モード(RMD): 即スタート(非同時スタート) */ dwRet = cp530_rReg(hDevID, wAx, 0xd7, &dwData); if(dwRet) return dwRet; dwData &= 0xff3ffff; dwRet = cp530_wReg(hDevID, wAx, 0x97, dwData); if(dwRet) return dwRet; } } axis &= 0xf0; wCmd = (axis << 4) | 0x06; /* 同時スタート */ dwRet = cp530_wCmdW(hDevID, 4, wCmd); /* V軸:コマンド書込み */ for(wAx = 0; wAx < 8; wAx++) { if(axis & axbit[wAx]) { /* 動作モード(PRMD): 即スタート(非同時スタート) */ dwRet = cp530_rReg(hDevID, wAx, 0xd7, &dwData); if(dwRet) return dwRet; dwData &= 0xff3ffff; /* 動作モード(PRMD)書込み */ dwRet = cp530_wReg(hDevID, wAx, 0x87, dwData); if(dwRet) return dwRet; } } } } return dwRet; } /****************************************************/ /* サーボオン */ /* 引数:デバイスハンドル,軸組み合わせ(HEX) */ /****************************************************/ DWORD hcp530_SvOn(DWORD hDevID, WORD axis) { DWORD dwRet = 0; /* 戻り値 */ /* 軸番号0x01〜0xff以外の時はエラー */ if((axis <= 0) || (axis > 0xff)) return ILLEGAL_PRM; /* X - U : X軸にサーボON */ if(axis & 0x0f) dwRet = cp530_wCmdW(hDevID, 0, (WORD)(((axis & 0x0f) << 8) | 0x18)); /* V - B : V軸にサーボON */ if( (axis & 0xf0) && (!dwRet) ) dwRet = cp530_wCmdW(hDevID, 4, (WORD)(((axis & 0xf0) << 4) | 0x18)); return dwRet; } /****************************************************/ /* サーボオフ */ /* 引数:デバイスハンドル,軸組み合わせ(HEX) */ /****************************************************/ DWORD hcp530_SvOff(DWORD hDevID, WORD axis) { DWORD dwRet = 0; /* 戻り値 */ /* 軸番号0x01〜0xff以外の時はエラー */ if((axis <= 0) || (axis > 0xff)) return ILLEGAL_PRM; /* X - U : X軸にサーボOFF */ if(axis & 0x0f) dwRet = cp530_wCmdW(hDevID, 0, (WORD)(((axis & 0x0f) << 8) | 0x10)); /* V - B : V軸にサーボOFF */ if( (axis & 0xf0) && (!dwRet) ) dwRet = cp530_wCmdW(hDevID, 4, (WORD)(((axis & 0xf0) << 4) | 0x10)); return dwRet; } /****************************************************/ /* サーボリセットオン */ /* 引数:デバイスハンドル,軸組み合わせ(HEX) */ /****************************************************/ DWORD hcp530_SvResetOn(DWORD hDevID, WORD axis) { DWORD dwRet = 0; /* 戻り値 */ /* 軸番号0x01〜0xff以外の時はエラー */ if((axis <= 0) || (axis > 0xff)) return ILLEGAL_PRM; /* X - U : X軸にサーボリセットON */ if(axis & 0x0f) dwRet = cp530_wCmdW(hDevID, 0, (WORD)(((axis & 0x0f) << 8) | 0x19)); /* V - B : V軸にサーボリセットON */ if( (axis & 0xf0) && (!dwRet) ) dwRet = cp530_wCmdW(hDevID, 4, (WORD)(((axis & 0xf0) << 4) | 0x19)); return dwRet; } /****************************************************/ /* サーボリセットオフ */ /* 引数:デバイスハンドル,軸組み合わせ(HEX) */ /****************************************************/ DWORD hcp530_SvResetOff(DWORD hDevID, WORD axis) { DWORD dwRet = 0; /* 戻り値 */ /* 軸番号0x01〜0xff以外の時はエラー */ if((axis <= 0) || (axis > 0xff)) return ILLEGAL_PRM; /* X - U : X軸にサーボリセットOFF */ if(axis & 0x0f) dwRet = cp530_wCmdW(hDevID, 0, (WORD)(((axis & 0x0f) << 8) | 0x11)); /* V - B : V軸にサーボリセットOFF */ if( (axis & 0xf0) && (!dwRet) ) dwRet = cp530_wCmdW(hDevID, 4, (WORD)(((axis & 0xf0) << 4) | 0x11)); return dwRet; } /****************************************************/ /* パルスモータオン */ /* 引数:デバイスハンドル,軸組み合わせ(HEX) */ /****************************************************/ DWORD hcp530_PMOn(DWORD hDevID, WORD axis) { DWORD dwRet = 0; /* 戻り値 */ /* 軸番号0x01〜0xff以外の時はエラー */ if((axis <= 0) || (axis > 0xff)) return ILLEGAL_PRM; /* X - U : X軸にパルスモータON */ if(axis & 0x0f) dwRet = cp530_wCmdW(hDevID, 0, (WORD)(((axis & 0x0f) << 8) | 0x10)); /* V - B : V軸にパルスモータON */ if( (axis & 0xf0) && (!dwRet) ) dwRet = cp530_wCmdW(hDevID, 4, (WORD)(((axis & 0xf0) << 4) | 0x10)); return dwRet; } /****************************************************/ /* パルスモータオフ */ /* 引数:デバイスハンドル,軸組み合わせ(HEX) */ /****************************************************/ DWORD hcp530_PMOff(DWORD hDevID, WORD axis) { DWORD dwRet = 0; /* 戻り値 */ /* 軸番号0x01〜0xff以外の時はエラー */ if((axis <= 0) || (axis > 0xff)) return ILLEGAL_PRM; /* X - U : X軸にパルスモータOFF */ if(axis & 0x0f) dwRet = cp530_wCmdW(hDevID, 0, (WORD)(((axis & 0x0f) << 8) | 0x18)); /* V - B : V軸にパルスモータOFF */ if( (axis & 0xf0) && (!dwRet) ) dwRet = cp530_wCmdW(hDevID, 4, (WORD)(((axis & 0xf0) << 4) | 0x18)); return dwRet; } /********************************************************/ /* 加減速レートの計算 */ /* 引数:加減速レート,加減速時間,FH, */ /* FL,加減速形式,予約 */ /********************************************************/ DWORD hcp530_CalAccRate(DWORD* rate, DWORD dwtime, DWORD fh, DWORD fl, WORD pro, WORD s) { double dblData; double dblTime; DWORD accrate; if(!fh) return ILLEGAL_PRM; if(!fl) return ILLEGAL_PRM; if(fh <= fl) return ILLEGAL_PRM; if(!dwtime) return ILLEGAL_PRM; dblTime = (double)dwtime / 1000; /* 加速時間(秒) */ if(s) { /* 直線部分のあるS字加減速レート:PRUR = 2457600 * (加減速時間 / (FH - FL)) - 1) */ dblData = dblTime / (fh - fl + 2 * s); dblData = 2457600l * dblData - 1; accrate = (DWORD)dblData; } else { if(pro) { /* 直線部分のないS字加減速レート:PRUR = 2457600 * (加減速時間 / (FH - FL)) - 1) */ dblData = dblTime / (fh - fl); dblData = 2457600l * dblData - 1; accrate = (DWORD)dblData; } else { /* 直線加減速レート:PRUR = 4915200 * (加減速時間 / (FH - FL)) - 1) */ dblData = dblTime / (fh - fl); dblData = 4915200l * dblData - 1; accrate = (DWORD)dblData; } } if((accrate > 0xffff) || (!accrate)) return ILLEGAL_PRM; *rate = accrate; return 0; }