

#include "busmotos.h"
#include "modbus_m.h"
#if (BUSCTRL_MOTO == RUANYI)
#include "ruanyictrl.h"
#define RYDEVIDX_Y		11		//电机Y设备地址
#define RYDEVIDX_X		12		//电机X设备地址

#elif(BUSCTRL_MOTO == LEISAI)

#include "leisaictrl.h"
#define LSDEVIDX_Y		11		//电机Y设备地址
#define LSDEVIDX_X		12		//电机X设备地址

#define MOTOP_JOGPOS	(1000000)			//电机手动位移
#define MOTON_JOGPOS	(-1000000)			//电机手动位移

#endif


void InitBusCtrlMotos(void)
{
	InitModbus_m(0, COMM_USART6, B115200, '8', 'N', '1', 'R');
#if (BUSCTRL_MOTO == RUANYI)
	InitRuanyiCom();
#elif (BUSCTRL_MOTO == LEISAI)
//	InitLeisaiMoto(LSDEVIDX_Y);
#endif
}

int BusCtrlMotoYtoAbspos(u32 pos, u32 spd)
{
	int rst = 0;
#if (BUSCTRL_MOTO == RUANYI)
	rst = MoveRyiToAbsolutePos(RYDEVIDX_Y, pos, spd);
#elif (BUSCTRL_MOTO == LEISAI)
	rst = MoveLeisaiToAbsolutePos(LSDEVIDX_Y, pos, spd);
#endif
	return rst;
}
int BusCtrlMotoXtoAbspos(s32 pos, u32 spd)
{
	int rst = 0;
#if (BUSCTRL_MOTO == RUANYI)
	rst = MoveRyiToAbsolutePos(RYDEVIDX_X, pos, spd);
#elif (BUSCTRL_MOTO == LEISAI)
	rst = MoveLeisaiToAbsolutePos(LSDEVIDX_X, pos, spd);
#endif
	return rst;
}
int BusCtrlMotoXtoRevpos(s32 pos, u32 spd)
{
	int rst = 0;
#if (BUSCTRL_MOTO == RUANYI)
	rst = MoveRyiToRelativePos(RYDEVIDX_X, pos, spd);
#elif(BUSCTRL_MOTO == LEISAI)
	rst = MoveLeisaiToRelativePos(LSDEVIDX_X, pos, spd);
#endif
	return rst;
}


//读取电机运行状态
int GetMotoXSta(void)
{
	int rslt = 0;
#if (BUSCTRL_MOTO == RUANYI)
	GetCurStatus(RYDEVIDX_X, &rslt);
	if(rslt < 0)
	{
		rslt = MOTO_WARNING;
	}
#elif(BUSCTRL_MOTO == LEISAI)
	GetLeisaiCurStatus(LSDEVIDX_X, &rslt);
#endif
	return rslt;
}
int GetMotoYSta(void)
{
	int rslt = 0;
#if (BUSCTRL_MOTO == RUANYI)
	GetCurStatus(RYDEVIDX_Y, &rslt);
	if(rslt < 0)
	{
		rslt = MOTO_WARNING;
	}
#elif(BUSCTRL_MOTO == LEISAI)
	GetLeisaiCurStatus(LSDEVIDX_Y, &rslt);
#endif
	return rslt;
}
int GetMotoYpusPos(s32 * pos)
{
	int rslt = 0;
#if (BUSCTRL_MOTO == RUANYI)
	s16 curMotoPos;
	rslt = GetCurPusPos(RYDEVIDX_Y, &curMotoPos);
	if(rslt < 0)
	{
		*pos = 0;
		rslt = MOTO_WARNING;
	}
	else
	{
		*pos = (s32)curMotoPos;
	}
#elif(BUSCTRL_MOTO == LEISAI)
	rslt = GetLeisaiCurPusPos(LSDEVIDX_Y, pos);
	if(rslt < 0)
	{
		*pos = 0;
		rslt = MOTO_WARNING;
	}
#endif
	return rslt;
}
int GetMotoXpusPos(s32 * pos)
{
	int rslt = 0;
#if (BUSCTRL_MOTO == RUANYI)
	s16 curMotoPos;
	rslt = GetCurPusPos(RYDEVIDX_X, &curMotoPos);
	if(rslt < 0)
	{
		*pos = 0;
		rslt = MOTO_WARNING;
	}
	else
	{
		*pos = (s32)curMotoPos;
	}
#elif(BUSCTRL_MOTO == LEISAI)
	rslt = GetLeisaiCurPusPos(LSDEVIDX_X, pos);
	if(rslt < 0)
	{
		*pos = 0;
		rslt = MOTO_WARNING;
	}
#endif
	return rslt;
}
/*
int GetMotoXpusPos(s16 * pos)
{
	int rslt = 0;
#if (BUSCTRL_MOTO == RUANYI)
	rslt = GetCurPusPos(RYDEVIDX_X, pos);
	if(rslt < 0)
	{
		rslt = MOTO_WARNING;
	}
#endif
	return rslt;
}*/

int MoveXbyJOG(int dir)
{
	int rslt = 0;
#if (BUSCTRL_MOTO == RUANYI)
	if(dir)
	{
		rslt= MoveRyiJOGP(RYDEVIDX_X, 0);
	}
	else
	{
		rslt = MoveRyiJOGN(RYDEVIDX_X, 0);
	}
#elif(BUSCTRL_MOTO == LEISAI)
	if(dir)
	{
		rslt= MoveLeisaiToRelativePos(LSDEVIDX_X, MOTOP_JOGPOS, MOTOX_JOGSPD);
	}
	else
	{
		rslt = MoveLeisaiToRelativePos(LSDEVIDX_X, MOTON_JOGPOS, MOTOX_JOGSPD);
	}
#endif
	return rslt;
}

int MoveYbyJOG(int dir)
{
	int rslt = 0;
#if (BUSCTRL_MOTO == RUANYI)
	if(dir)
	{
		rslt = MoveRyiJOGP(RYDEVIDX_Y, 0);
	}
	else
	{
		rslt = MoveRyiJOGN(RYDEVIDX_Y, 0);
	}
#elif(BUSCTRL_MOTO == LEISAI)
	if(dir)
	{
		rslt = MoveLeisaiToRelativePos(LSDEVIDX_Y, MOTOP_JOGPOS, MOTOX_JOGSPD);
	}
	else
	{
		rslt = MoveLeisaiToRelativePos(LSDEVIDX_Y, MOTON_JOGPOS, MOTOX_JOGSPD);
	}
#endif
	return rslt;
}

int MotoXstopEmc(void)
{
	int rslt = 0;
#if (BUSCTRL_MOTO == RUANYI)
	rslt = RyiMotoStop(RYDEVIDX_X);
#elif(BUSCTRL_MOTO == LEISAI)
	rslt = LeisaiMotoStop(LSDEVIDX_X);
#endif
	return rslt;
}

void MotoYstopEmc(void)
{
#if (BUSCTRL_MOTO == RUANYI)
	RyiMotoStop(RYDEVIDX_Y);
#elif(BUSCTRL_MOTO == LEISAI)
	LeisaiMotoStop(LSDEVIDX_Y);
#endif
}
/*
int SetMotoYCurPos(int pos)
{
	int rslt;
#if (BUSCTRL_MOTO == RUANYI)
	rslt = SetRyiCurPos(RYDEVIDX_Y, (s16)pos);
#elif(BUSCTRL_MOTO == LEISAI)

#endif
	return rslt;
}*/
int SetMotoYPosZero(void)
{
	int rslt = 0;
#if (BUSCTRL_MOTO == RUANYI)
	rslt = RyiMotoSetCurPosZero(RYDEVIDX_Y);
#elif(BUSCTRL_MOTO == LEISAI)
	rslt = LeisaiMotoSetCurPosZero(LSDEVIDX_Y);
#endif
	return rslt;
}

int SetMotoXPosZero(void)
{
	int rslt = 0;
#if (BUSCTRL_MOTO == RUANYI)
	rslt = RyiMotoSetCurPosZero(RYDEVIDX_X);
#elif(BUSCTRL_MOTO == LEISAI)
	rslt = LeisaiMotoSetCurPosZero(LSDEVIDX_X);
#endif
	return rslt;
}

int SetMotoXRetrunPos(void)
{
	int rslt = 0;
#if (BUSCTRL_MOTO == RUANYI)
	rslt = RyiMotoToPos(RYDEVIDX_X, BITTOPOS0);
#elif(BUSCTRL_MOTO == LEISAI)
	rslt = MoveLeisaiToAbsolutePos(LSDEVIDX_X, 0, 300);
#endif
	return rslt;
}

int SetMotoYmoveLimt(s32 posp, s32 posn)
{
	int rslt = 0;
#if (BUSCTRL_MOTO == RUANYI)

#elif(BUSCTRL_MOTO == LEISAI)
	rslt = LeisaiMotoSetLim(LSDEVIDX_Y, posp, posn);
#endif
	return rslt;
}
int SetMotoXmoveLimt(s32 posp, s32 posn)
{
	int rslt = 0;
#if (BUSCTRL_MOTO == RUANYI)

#elif(BUSCTRL_MOTO == LEISAI)
	rslt = LeisaiMotoSetLim(LSDEVIDX_X, posp, posn);
#endif
	return rslt;
}

int GetLeisaMotoXErrcode(u16 * errcode)
{
	int rslt = 0;
#if (BUSCTRL_MOTO == RUANYI)

#elif(BUSCTRL_MOTO == LEISAI)
	rslt = GetLeisaiCurErrcode(LSDEVIDX_X, errcode);
#endif
	return rslt;
}

int GetLeisaMotoYErrcode(u16 * errcode)
{
	int rslt = 0;
#if (BUSCTRL_MOTO == RUANYI)

#elif(BUSCTRL_MOTO == LEISAI)
	rslt = GetLeisaiCurErrcode(LSDEVIDX_Y, errcode);
#endif
	return rslt;
}
