using Rs.Camera ;
using Rs.Framework ;
using Rs.Motion.GugaoEcat ;
using Rs.Motion.GugaoPulse ;
using Rs.Motion ;
using Rs.MotionPlat.Flow ;
using System ;
using System.Collections.Generic ;
using System.Linq ;
using System.Text ;
using System.Threading.Tasks ;
using Rs.Motion.Base ;
using static System . Windows . Forms . VisualStyles . VisualStyleElement . ToolTip ;
using Rs.Controls ;
using System.Threading ;
using System.Diagnostics ;
using Rs.MotionPlat.AuxiliaryEquipment ;
using System.Windows.Forms ;
using Rs.MotionPlat.Flow.SubFlow ;
using Rs.DataAccess ;
using System.Data ;
namespace Rs.MotionPlat.Commom
{
public class Ops
{
public static bool Pause { get ; set ; } = false ;
public static void Init ( )
{
InitDb ( ) ;
SysConfigParam . Init ( ) ;
int errNum = 0 ;
//Task.Run(() => {
if ( ! GlobalVar . VirtualAxis )
{
#region 初始化固高卡
ErrorCode errCode = GugaoCardManager . Instance . Init ( ) ; // GugaoPulseCardManager.Instance.Init();
if ( errCode > ErrorCode . Ok )
{
errNum + + ;
MessageQueue . Instance . Warn ( $"Gugao motion card load fail {errCode}" ) ;
LogHelper . Debug ( $"Gugao motion card load fail {errCode}" ) ;
}
else
{
MessageQueue . Instance . Insert ( "Gugao motion card load success" ) ;
}
# endregion
#region 初始化ztm卡
errCode = ZtmCardManager . Instance . Init ( ) ;
if ( errCode > ErrorCode . Ok )
{
errNum + + ;
MessageQueue . Instance . Warn ( $"rs motion card load fail {errCode}" ) ; return ;
}
else
{
MessageQueue . Instance . Insert ( "rs motion card load ok" ) ;
}
# endregion
#region 初始化IO
IoManager . Instance . Init ( ) ;
# endregion
#region 链接相机
//链接相机
ECameraErrorCode ceCode = HikCamera . Instance . Init ( ) ;
if ( ceCode > ECameraErrorCode . Ok )
{
errNum + + ;
//MessageQueue.Instance.Warn(ceCode.ToString());
Msg . ShowError ( "Camera open fail!!!Please try again in 30 seconds" ) ;
//System.Environment.Exit(0);
}
else
{
MessageQueue . Instance . Insert ( "Camera load ok!" ) ;
int ret = HikCamera . Instance . SetReverseX ( "upCamera" , EDir . Y , false ) ;
ret + = HikCamera . Instance . SetReverseX ( "upCamera" , EDir . X , false ) ;
if ( ret ! = 0 )
{
MessageQueue . Instance . Warn ( "Camera init error" ) ;
}
ret = 0 ;
ret = HikCamera . Instance . SetReverseX ( "locationCamera" , EDir . X , false ) ;
ret + = HikCamera . Instance . SetReverseX ( "locationCamera" , EDir . Y , true ) ;
if ( ret ! = 0 )
{
MessageQueue . Instance . Warn ( "Camera init error" ) ;
}
ret = HikCamera . Instance . StartGrab ( "upCamera" ) ;
if ( ret ! = 0 )
{
MessageQueue . Instance . Warn ( "Camera init error" ) ;
}
ret = HikCamera . Instance . StartGrab ( "locationCamera" ) ;
if ( ret ! = 0 )
{
MessageQueue . Instance . Warn ( "Camera init error" ) ;
}
ret = HikCamera . Instance . StartGrab ( "scanCamera" ) ;
if ( ret ! = 0 )
{
MessageQueue . Instance . Warn ( "Camera init error" ) ;
}
}
# endregion
//使能所有轴卡
AxisControl . AllEnable ( ) ;
SafeControl . Instance . Init ( ) ;
AxisControl . AllStop ( ) ;
if ( errNum = = 0 )
{
MessageQueue . Instance . Insert ( "Init finished" ) ;
}
else
{
MessageQueue . Instance . Warn ( "Init fail" ) ;
}
if ( ! DeviceFactory . Init ( ) )
{
Msg . ShowError ( "laser connect fail!" ) ;
}
QifuManager . Instance . Init ( ) ;
QifuManager . Instance . Write ( SysConfigParam . GetValue < float > ( "QifuValue" ) ) ;
LightManger . Instance . SetStatus ( ELightStatus . YellowBlink ) ;
Off ( "启动灯" ) ;
Off ( "停止灯" ) ;
}
NozzleManager . Init ( ) ;
TrayPointManager . LoadPoint ( ) ;
TurnoverSlotOffset . Init ( ) ;
MonitorSystemButton . Instance . Start ( ) ;
TestCenter . Instance . Init ( ) ;
SysConfigParam . Update ( "EnableVirtuleBarCode" , "False" ) ;
}
static void InitDb ( )
{
SqliteHelper db = new SqliteHelper ( ) ;
string querySql = "select * from SysParameter where fieldname='LocationFailAutoSkip'" ;
string insertSql = string . Empty ;
DataTable dt = db . GetDataTable ( querySql ) ;
if ( dt = = null | | dt . Rows . Count = = 0 )
{
insertSql = $"insert into SysParameter(fieldname,fieldtype,fieldvalue,Desc,category,enable) values('LocationFailAutoSkip','bool','True','拍照定位失败自动跳过','system',1)" ;
db . ExecuteNonQuery ( insertSql ) ;
}
querySql = "select * from SysParameter where fieldname='StockBeltSpeed'" ;
dt = db . GetDataTable ( querySql ) ;
if ( dt = = null | | dt . Rows . Count = = 0 )
{
insertSql = $"insert into SysParameter(fieldname,fieldtype,fieldvalue,Desc,category,enable) values('StockBeltSpeed','int','2','料仓皮带速度','system',1)" ;
db . ExecuteNonQuery ( insertSql ) ;
}
querySql = "select * from SysParameter where fieldname='TakeTrayFromNg2InputSpeed'" ;
dt = db . GetDataTable ( querySql ) ;
if ( dt = = null | | dt . Rows . Count = = 0 )
{
insertSql = $"insert into SysParameter(fieldname,fieldtype,fieldvalue,Desc,category,enable) values('TakeTrayFromNg2InputSpeed','int','2','搬运带料料盘速度','system',1)" ;
db . ExecuteNonQuery ( insertSql ) ;
}
querySql = "select * from SysParameter where fieldname='Socket9_16ExceptionX'" ;
dt = db . GetDataTable ( querySql ) ;
if ( dt = = null | | dt . Rows . Count = = 0 )
{
insertSql = $"insert into SysParameter(fieldname,fieldtype,fieldvalue,Desc,category,enable) values('Socket9_16ExceptionX','double','0','Socket9-16异常处理位X','system',1)" ;
db . ExecuteNonQuery ( insertSql ) ;
}
querySql = "select * from SysParameter where fieldname='Socket9_16ExceptionY'" ;
dt = db . GetDataTable ( querySql ) ;
if ( dt = = null | | dt . Rows . Count = = 0 )
{
insertSql = $"insert into SysParameter(fieldname,fieldtype,fieldvalue,Desc,category,enable) values('Socket9_16ExceptionY','double','0','Socket9-16异常处理位Y','system',1)" ;
db . ExecuteNonQuery ( insertSql ) ;
}
querySql = "select * from SysParameter where fieldname='TurnoverOneProductHeightMin'" ;
dt = db . GetDataTable ( querySql ) ;
if ( dt = = null | | dt . Rows . Count = = 0 )
{
insertSql = $"insert into SysParameter(fieldname,fieldtype,fieldvalue,Desc,category,enable) values('TurnoverOneProductHeightMin','double','0','周转盘一个产品时的高度差的最小值','tray',1)" ;
db . ExecuteNonQuery ( insertSql ) ;
}
querySql = "select * from SysParameter where fieldname='TurnoverOneProductHeightMax'" ;
dt = db . GetDataTable ( querySql ) ;
if ( dt = = null | | dt . Rows . Count = = 0 )
{
insertSql = $"insert into SysParameter(fieldname,fieldtype,fieldvalue,Desc,category,enable) values('TurnoverOneProductHeightMax','double','0','周转盘一个产品时的高度差的最大值','tray',1)" ;
db . ExecuteNonQuery ( insertSql ) ;
}
querySql = "select * from SysParameter where fieldname='SocketOneProductHeightMin'" ;
dt = db . GetDataTable ( querySql ) ;
if ( dt = = null | | dt . Rows . Count = = 0 )
{
insertSql = $"insert into SysParameter(fieldname,fieldtype,fieldvalue,Desc,category,enable) values('SocketOneProductHeightMin','double','0','Socket一个产品时的高度差的最小值','tray',1)" ;
db . ExecuteNonQuery ( insertSql ) ;
}
querySql = "select * from SysParameter where fieldname='SocketOneProductHeightMax'" ;
dt = db . GetDataTable ( querySql ) ;
if ( dt = = null | | dt . Rows . Count = = 0 )
{
insertSql = $"insert into SysParameter(fieldname,fieldtype,fieldvalue,Desc,category,enable) values('SocketOneProductHeightMax','double','0','Socket一个产品时的高度差的最大值','tray',1)" ;
db . ExecuteNonQuery ( insertSql ) ;
}
}
public static void Start ( )
{
//先检测门禁
if ( IoManager . Instance . ReadIn ( "后安全门禁" ) = = 0 )
{
MessageQueue . Instance . Warn ( "door opened,please close door!!!" ) ;
}
else
{
bool run = true ;
if ( GlobalVar . EnableVirtuleBarCode )
{
DialogResult dr = Msg . ShowQuestion ( "device run use virtual barcode?" ) ;
if ( dr = = DialogResult . Cancel )
{
run = false ;
}
}
if ( run )
{
Task . Run ( ( ) = > {
if ( MachineManage . Instance . MachineStatus = = EMachineStatus . Homed | | MachineManage . Instance . MachineStatus = = EMachineStatus . Stop )
{
MachineManage . Instance . SetLocalMachineStatus ( EMachineStatus . Working ) ;
On ( "启动灯" ) ;
Off ( "停止灯" ) ;
LightManger . Instance . SetStatus ( ELightStatus . Green ) ;
DischargeFlow . Instance . Start ( ) ;
TurnoverFlow . Instance . Start ( ) ;
MachineManage . Instance . SetCenterMachineStatus ( ERunStatus . Started ) ;
if ( MachineManage . Instance . GetLoadUnloadStatus ( ) ! = ERunState . Busying )
{
MachineManage . Instance . SetLoadUnloadStatus ( ERunState . Waiting ) ;
}
}
} ) ;
}
}
}
public static void Stop ( )
{
if ( MachineManage . Instance . MachineStatus = = EMachineStatus . Working )
{
LightManger . Instance . SetStatus ( ELightStatus . Yellow ) ;
Off ( "启动灯" ) ;
On ( "停止灯" ) ;
DischargeFlow . Instance . Stop ( ) ;
TurnoverFlow . Instance . Stop ( ) ;
MachineManage . Instance . SetCenterMachineStatus ( ERunStatus . Stopped ) ;
if ( MachineManage . Instance . GetLoadUnloadStatus ( ) = = ERunState . Waiting )
{
MachineManage . Instance . SetLoadUnloadStatus ( ERunState . Interrupt ) ;
}
MachineManage . Instance . MachineStatus = EMachineStatus . Stop ;
}
else if ( MachineManage . Instance . MachineStatus = = EMachineStatus . Homing )
{
foreach ( IAxis axis in AxisControl . GetAllAxis ( ) )
{
axis . Abort_Go_Home ( ) ;
}
}
}
public static void GoHome ( )
{
if ( MachineManage . Instance . MachineStatus = = EMachineStatus . Homed
| | MachineManage . Instance . MachineStatus = = EMachineStatus . NotInit
| | MachineManage . Instance . MachineStatus = = EMachineStatus . NotHomed
| | MachineManage . Instance . MachineStatus = = EMachineStatus . Stop
)
{
HomeFlow . Instance . StartGoHome ( ) ;
}
else
{
MessageQueue . Instance . Warn ( $"device state {MachineManage.Instance.MachineStatus} cann't home!" ) ;
}
}
/// <summary>
/// 检测轴是否报警
/// </summary>
/// <param name="axis"></param>
/// <returns></returns>
public static List < IAxis > AxiesAlarm ( params IAxis [ ] axies )
{
ErrorCode errCode = ErrorCode . Ok ;
bool bAlarm = false ;
List < IAxis > axes = new List < IAxis > ( ) ;
foreach ( IAxis axis in axies )
{
errCode = axis . GetAlarmStatus ( out bAlarm ) ;
if ( errCode = = ErrorCode . Ok )
{
if ( bAlarm )
axes . Add ( axis ) ;
}
else
{
axes . Add ( axis ) ;
}
}
return axes ;
}
public static bool IsStop ( params string [ ] axies )
{
foreach ( var axisname in axies )
{
if ( ! IsStop ( AxisControl . GetAxis ( axisname ) ) )
return false ;
}
return true ;
}
public static bool IsStop ( params IAxis [ ] axies )
{
foreach ( var axis in axies )
{
axis . IsStop ( out bool bStop ) ;
if ( ! bStop )
return false ;
}
return true ;
}
/// <summary>
/// 判断轴是否运动到位
/// </summary>
/// <param name="axies"></param>
/// <returns></returns>
public static bool IsInPosition ( params IAxis [ ] axies )
{
foreach ( var axis in axies )
{
axis . IsInPosition ( out bool bArrived ) ;
if ( ! bArrived )
return false ;
}
return true ;
}
//public static MoveResult IsStop( IAxis axis)
//{
// ErrorCode errCode = ErrorCode.Ok;
// MoveResult mr = new MoveResult();
// errCode = axis.GetAlarmStatus(out bool bAlarm);
// if (errCode == ErrorCode.Ok)
// {
// mr.IsAlarm= bAlarm;
// errCode = axis.IsStop(out bool bStop);
// if(errCode== ErrorCode.Ok)
// {
// mr.IsStop= bStop;
// }
// }
// mr.Result = errCode;
// return mr ;
//}
/// <summary>
/// 轴是否运动到位
/// </summary>
/// <param name="axies"></param>
/// <returns></returns>
public static bool IsArrived ( params string [ ] axies )
{
ErrorCode errCode = ErrorCode . Ok ;
foreach ( var axisname in axies )
{
errCode = AxisControl . GetAxis ( axisname ) . IsArrived ( out bool isArrived ) ;
if ( errCode > ErrorCode . Ok )
{
return false ;
}
else if ( ! isArrived )
{
return false ;
}
}
return true ;
}
/// <summary>
/// 轴是否运动到位
/// </summary>
/// <param name="axies"></param>
/// <returns></returns>
public static bool IsArrived ( params IAxis [ ] axies )
{
ErrorCode errCode = ErrorCode . Ok ;
foreach ( var axis in axies )
{
errCode = axis . IsArrived ( out bool isArrived ) ;
if ( errCode > ErrorCode . Ok )
{
return false ;
}
else if ( ! isArrived )
{
return false ;
}
}
return true ;
}
public static bool IsHomed ( params string [ ] axies )
{
foreach ( var axisname in axies )
{
if ( ! IsHomed ( AxisControl . GetAxis ( axisname ) ) )
return false ;
}
return true ;
}
public static bool IsHomed ( params IAxis [ ] axies )
{
foreach ( var axis in axies )
{
if ( axis . HomeStatus ! = EHomeStatus . Finished )
return false ;
}
return true ;
}
public static void On ( string ioName )
{
IoManager . Instance . WriteOut ( ioName , 1 ) ;
}
public static void Off ( string ioName )
{
IoManager . Instance . WriteOut ( ioName , 0 ) ;
}
public static bool IsOn ( string ioName , bool needRecheck = true )
{
int num = 0 ;
short val = 0 ;
if ( needRecheck )
{
while ( num < 3 )
{
val = IoManager . Instance . ReadIn ( ioName ) ;
if ( val = = 0 )
{
num + + ;
Thread . Sleep ( 20 ) ;
}
else
{
num = 0 ;
break ;
}
}
}
else
{
val = IoManager . Instance . ReadIn ( ioName ) ;
}
return val = = 1 ;
}
public static bool IsOff ( string ioName , bool needRecheck = true )
{
int num = 0 ;
short val = 0 ;
if ( needRecheck )
{
while ( num < 3 )
{
val = IoManager . Instance . ReadIn ( ioName ) ;
if ( val = = 1 )
{
num + + ;
Thread . Sleep ( 20 ) ;
}
else
{
num = 0 ;
break ;
}
}
}
else
{
val = IoManager . Instance . ReadIn ( ioName ) ;
}
return val = = 0 ;
//return IoManager.Instance.ReadIn(ioName) == 0;
}
public static bool IsOutOn ( string ioName )
{
return IoManager . Instance . ReadOut ( ioName ) = = 1 ;
}
public static bool IsOutOff ( string ioName )
{
return IoManager . Instance . ReadOut ( ioName ) = = 0 ;
}
public static void Quit ( )
{
CleanOutFlow . Instance . Quit ( ) ;
LightManger . Instance . CloseAll ( ) ;
Off ( "启动灯" ) ;
Off ( "停止灯" ) ;
AxisControl . AllDisable ( ) ;
HikCamera . Instance . Deinit ( ) ;
}
public static bool AllZHomed ( )
{
if ( AxisControl . NozzleZ1 . HomeStatus ! = EHomeStatus . Finished ) return false ;
if ( AxisControl . NozzleZ2 . HomeStatus ! = EHomeStatus . Finished ) return false ;
return true ;
}
public static bool AllZStoped ( )
{
if ( ! IsStop ( AxisControl . NozzleZ1 ) ) return false ;
if ( ! IsStop ( AxisControl . NozzleZ2 ) ) return false ;
return true ;
}
public static bool AllRStoped ( )
{
if ( ! IsStop ( AxisControl . NozzleR1 ) ) return false ;
if ( ! IsStop ( AxisControl . NozzleR2 ) ) return false ;
return true ;
}
public static bool AllRHomed ( )
{
if ( AxisControl . NozzleR1 . HomeStatus ! = EHomeStatus . Finished ) return false ;
if ( AxisControl . NozzleR2 . HomeStatus ! = EHomeStatus . Finished ) return false ;
return true ;
}
/// <summary>
/// 检查有没有轴报警
/// </summary>
/// <returns></returns>
public static bool CheckHasAlarm ( )
{
return false ;
}
public static double GetCurPosition ( string axisName )
{
IAxis axis = AxisControl . GetAxis ( axisName ) ;
return GetCurPosition ( axis ) ;
}
public static double GetCurPosition ( IAxis axis )
{
double pos = 0.0 ;
if ( axis = = null ) return 0 ;
if ( axis . Config . EnableEncoder = = 1 )
{
if ( axis . Config . AxisName . IndexOf ( "Nozzle" ) > = 0 )
{
axis . GetPrfPosition ( out pos ) ;
}
else
{
axis . GetEncoderPosition ( out pos ) ;
}
}
else
{
axis . GetPrfPosition ( out pos ) ;
}
return pos ;
}
public static void HomeAndGoStartPos ( string axisName )
{
Task . Run ( ( ) = > {
IAxis axis = AxisControl . GetAxis ( axisName ) ;
axis . Home ( ) ;
MessageQueue . Instance . Insert ( "stat home" ) ;
Thread . Sleep ( 10 ) ;
Stopwatch timeout = new Stopwatch ( ) ;
timeout . Restart ( ) ;
while ( axis . HomeStatus ! = EHomeStatus . Finished )
{
MessageQueue . Instance . Insert ( "homing" ) ;
Thread . Sleep ( 10 ) ;
}
timeout . Stop ( ) ;
if ( axis . HomeStatus = = EHomeStatus . Finished )
{
MessageQueue . Instance . Insert ( "go to start pos" ) ;
Thread . Sleep ( 100 ) ;
timeout . Restart ( ) ;
ErrorCode errCode = axis . MovePos ( SysConfigParam . GetValue < double > ( $"{axisName}StartPos" ) , GlobalVar . WholeSpeed ) ;
while ( ! Ops . IsStop ( axisName ) & & timeout . ElapsedMilliseconds < 5000 )
{
Thread . Sleep ( 10 ) ;
}
}
timeout . Stop ( ) ;
} ) ;
}
/// <summary>
/// 是否已回原并在起始位
/// </summary>
/// <param name="axisName"></param>
public static bool IsHomedAndNearStartPos ( string axisName )
{
IAxis axis = AxisControl . GetAxis ( axisName ) ;
if ( axis . HomeStatus = = EHomeStatus . Finished )
{
double curPos = GetCurPosition ( axisName ) ;
double startPos = SysConfigParam . GetValue < double > ( $"{axisName}StartPos" ) ;
if ( Math . Abs ( curPos - startPos ) < = 0.02 )
{
return true ;
}
}
return false ;
}
public static bool Go ( string axisName , double targetPos , int speedPercent = 0 ) {
return Go ( AxisControl . GetAxis ( axisName ) , targetPos , speedPercent ) ;
}
public static bool Go ( IAxis axis , double targetPos , int speedPercent = 0 )
{
ErrorCode errCode = ErrorCode . Ok ;
if ( speedPercent = = 0 )
speedPercent = GlobalVar . WholeSpeed ;
errCode = axis . MovePos ( targetPos , speedPercent ) ;
if ( errCode = = ErrorCode . Ok | | GlobalVar . VirtualAxis )
{
return true ;
}
else
{
Msg . ShowError ( $"轴{axis.Config.AxisName}运动异常, ret={errCode}" ) ;
return false ;
}
}
/// <summary>
/// 判断轴是否在原点
/// </summary>
/// <param name="axisName"></param>
/// <returns></returns>
public static bool IsInOrg ( string axisName )
{
IAxis axis = AxisControl . GetAxis ( axisName ) ;
axis . GetOrgStatus ( out bool isInOrg ) ;
if ( isInOrg )
return true ;
return false ;
}
}
}