#include "cprocess_laserfoucscali.h"
#include "cproductside.h"
#include "cakerfcheck_h.h"
#include "common_include.h"
CProcess_LaserFoucsCali::CProcess_LaserFoucsCali(const QString &name,
                                                 int32_t id,
                                                 int32_t iLaserPathID)
    :CMProcess(name, id)
{
    m_iLaserPathID = iLaserPathID;

    m_Variants.add_Variant(Var_Process_LaserFoucsCali_LineLength_ID,
                           QObject::trUtf8("LineLength"), double(0), false,
                           Variant::TOXML);
    m_Variants.add_Variant(Var_Process_LaserFoucsCali_StepZ_ID,
                           QObject::trUtf8("StepZ"), double(0), false,
                           Variant::TOXML);
    m_Variants.add_Variant(Var_Process_LaserFoucsCali_Count_ID,
                           QObject::trUtf8("Count"), int(0), false,
                           Variant::TOXML);
    m_Variants.add_Variant(Var_Process_LaserFoucsCali_GapDis_ID,
                           QObject::trUtf8("GapDis"), double(0), false,
                           Variant::TOXML);
    m_Variants.add_Variant(Var_Process_LaserFoucsCali_LaserFreq_ID,
                           QObject::trUtf8("LaserFreq"), double(0), false,
                           Variant::TOXML);
    m_Variants.add_Variant(Var_Process_LaserFoucsCali_LaserPower_ID,
                           QObject::trUtf8("LaserPower"), double(0), false,
                           Variant::TOXML);
    m_Variants.add_Variant(Var_Process_LaserFoucsCali_SpeedX_ID,
                           QObject::trUtf8("SpeedX"), double(0), false,
                           Variant::TOXML);

    m_Variants.add_Variant(Var_Process_LaserFoucsCali_AccRate_ID,
                           QObject::trUtf8("AccRate"), double(0), false,
                           Variant::TOXML);
    m_Variants.add_Variant(Var_Process_LaserFoucsCali_JerkRate_ID,
                           QObject::trUtf8("JerkRate"), double(0), false,
                           Variant::TOXML);

    m_Variants.add_Variant(Var_Process_LaserFoucsCali_LaserEpulse_ID,
                           QObject::trUtf8("FocsCaliEpulse"), int(100), false,
                           Variant::TOXML);
}

//打一个标记点
int CProcess_LaserFoucsCali::Laser_Mark(double dCenterX, double dCenterY,
                                        double dZpos,
                                        double dDIndex, double dBeamWidth,
                                        bool_t bCross)
{

    CMotorDriver* pMotorDriver = GET_CARD(MOTOR_DRIVER, CMotorDriver);
    Q_ASSERT(pMotorDriver);
    CLaserDriver* pLaserDriver = GET_CARD(LASER_DRIVER, CLaserDriver);
    CLaserPath* pLaserPath = GET_CHDIRECT(m_iLaserPathID, CLaserPath);
    Q_ASSERT(pLaserPath);
    CLaserPathDriver* pLPathDriver = GET_CARD(LASERPATH_DRIVER, CLaserPathDriver);
    Q_ASSERT(pLPathDriver);
    CMProduct* pProduct = GET_CHDIRECT(PRODUCT, CMProduct);
    double dDiameter = pProduct->m_Variants.get_VData(Var_Product_Diameter_ID)->toDouble();

    double dLineLength = m_Variants.get_VData(Var_Process_LaserFoucsCali_LineLength_ID)->toDouble();

	pLPathDriver->OpenShutter(m_iLaserPathID, true);
	
    /*if (m_iLaserPathID == LASERPATH_D_WIDE) {
        if (!pMotorDriver->get_iodata(IO_INPUT, IN_LASER_Shutter_Narrow_Down, true)) {
            pMotorDriver->set_cylinder(OUT_Shutter_Narrow_Open, IN_LASER_Shutter_Narrow_Down);
            pMotorDriver->set_cylinder(OUT_Shutter_Wide_Open, IN_LASER_Shutter_Narrow_Up, FALSE);
        }
    } else if (m_iLaserPathID == LASERPATH_D_NARROW) {
        if (!pMotorDriver->get_iodata(IO_INPUT, IN_LASER_Shutter_Wide_Down, true)) {
            pMotorDriver->set_cylinder(OUT_Shutter_Wide_Open, IN_LASER_Shutter_Wide_Down);
            pMotorDriver->set_cylinder(OUT_Shutter_Narrow_Open, IN_LASER_Shutter_Narrow_Up, FALSE);
        }
    }*/
    
    double dDis = sqrtf(dCenterX*dCenterX + dCenterY*dCenterY);
    if (dDis + fabs(dLineLength / 2) > dDiameter / 2) {
        MQT_THROW_CODESTR(ALARM_CUT_PARAM, QObject::trUtf8("Laser Cross err."
                                                           "Cross Position is Out Of Cell"));
    }
    
    _SysExt_Instance.AssertCheckWtVaccum();

    _SysExt_Instance.m_iCurChID = m_iLaserPathID;
    double dLaserFreq = m_Variants.get_VData(Var_Process_LaserFoucsCali_LaserFreq_ID)->toDouble();
    double dLaserPower = m_Variants.get_VData(Var_Process_LaserFoucsCali_LaserPower_ID)->toDouble();
    double dLaserEpulse = m_Variants.get_VData(Var_Process_LaserFoucsCali_LaserEpulse_ID)->toDouble();
    pLPathDriver->MovePathAngle(m_iLaserPathID, dLaserEpulse, dBeamWidth,
                                dLaserFreq, dLaserPower);

    double dSpdX = m_Variants.get_VData(Var_Process_LaserFoucsCali_SpeedX_ID)->toDouble();

    double dAccX = dSpdX*m_Variants.get_VData(Var_Process_LaserFoucsCali_AccRate_ID)->toDouble();
    double dJerkX = dAccX*m_Variants.get_VData(Var_Process_LaserFoucsCali_JerkRate_ID)->toDouble();
    double dDecX = dAccX;
    double dKdecX = dJerkX;
    pMotorDriver->setspeed(MOTORX, dSpdX, dAccX, dDecX, dJerkX, dKdecX);
    /*m_gSystem.CutPosX(dCenterX - dLineLength / 2,
                      dCenterX + dLineLength / 2,
                      dCenterY, dZpos, 0,
                      m_iLaserPathID, dOffsetY, dSpdX, -1, -1, dAccX);*/
    _SysExt_Instance.CutPos(dCenterX - dLineLength / 2,
                     dCenterX + dLineLength / 2,
                     dCenterY, dZpos, 0, 0, 
                     m_iLaserPathID, true, dSpdX, dAccX, true); 

    pLaserDriver->OpenLaserGate(LASER, FALSE);
    pMotorDriver->setdefaultspeed(MOTORX);

    if (bCross) {
        _SysExt_Instance.CutPos(dCenterY - dLineLength / 2,
                          dCenterY + dLineLength / 2,
                          dCenterX, dZpos, 0, 0, m_iLaserPathID, false, 200, 0, true);

    }

    CMPointd wtCenter(dCenterX, dCenterY);
    CMPointd laserCenter;
    CMWorkTableDriver* pWorktableDriver=GET_CARD(WORKTABLE_DRIVER,CMWorkTableDriver);
    Q_ASSERT(pWorktableDriver);
    pWorktableDriver->WtToAxis(CAM6X, wtCenter,laserCenter);
    MqDebug()<<"LaserMark Center:X"<<laserCenter.tX<<"Y"<<laserCenter.tY;
    _SysExt_Instance.absmove_xy(laserCenter.tX, laserCenter.tY);

    //
    pMotorDriver->setio(IO_OUTPUT, OUT_Shutter_All_Open, FALSE);
    return 0;
}

int CProcess_LaserFoucsCali::Laser_MarkY(double dCenterX, double dCenterY,
                                         double dZpos,
                                         double dDIndex, double dBeamWidth,
                                         bool_t bCross)
{
    CMotorDriver* pMotorDriver = GET_CARD(MOTOR_DRIVER, CMotorDriver);
    Q_ASSERT(pMotorDriver);
    CLaserDriver* pLaserDriver = GET_CARD(LASER_DRIVER, CLaserDriver);
    CLaserPath* pLaserPath = GET_CHDIRECT(m_iLaserPathID, CLaserPath);
    Q_ASSERT(pLaserPath);
    CLaserPathDriver* pLPathDriver = GET_CARD(LASERPATH_DRIVER, CLaserPathDriver);
    Q_ASSERT(pLPathDriver);
    CMProduct* pProduct = GET_CHDIRECT(PRODUCT, CMProduct);
    double dDiameter = pProduct->m_Variants.get_VData(Var_Product_Diameter_ID)->toDouble();

    double dLineLength = m_Variants.get_VData(Var_Process_LaserFoucsCali_LineLength_ID)->toDouble();

	pLPathDriver->OpenShutter(m_iLaserPathID, true);

    double dDis = sqrtf(dCenterX * dCenterX + dCenterY * dCenterY);
    if(dDis + fabs(dLineLength / 2) > dDiameter / 2) {
        MQT_THROW_CODESTR(ALARM_CUT_PARAM, QObject::trUtf8("Laser Cross err."
                                                           "Cross Position is Out Of Cell"));
    }

    _SysExt_Instance.AssertCheckWtVaccum();

    _SysExt_Instance.m_iCurChID = m_iLaserPathID;
    double dLaserFreq = m_Variants.get_VData(Var_Process_LaserFoucsCali_LaserFreq_ID)->toDouble();
    double dLaserPower = m_Variants.get_VData(Var_Process_LaserFoucsCali_LaserPower_ID)->toDouble();
    double dLaserEpulse = m_Variants.get_VData(Var_Process_LaserFoucsCali_LaserEpulse_ID)->toDouble();
    pLPathDriver->MovePathAngle(m_iLaserPathID, dLaserEpulse, dBeamWidth,
                                dLaserFreq, dLaserPower);

    double dSpdX = m_Variants.get_VData(Var_Process_LaserFoucsCali_SpeedX_ID)->toDouble();

    double dAccX = dSpdX * m_Variants.get_VData(Var_Process_LaserFoucsCali_AccRate_ID)->toDouble();
    double dJerkX = dAccX * m_Variants.get_VData(Var_Process_LaserFoucsCali_JerkRate_ID)->toDouble();
    double dDecX = dAccX;
    double dKdecX = dJerkX;
    //用Y向划线
    pMotorDriver->setspeed(MOTORY, dSpdX, dAccX, dDecX, dJerkX, dKdecX);
    _SysExt_Instance.CutPos(dCenterY - dLineLength / 2,
                     dCenterY + dLineLength / 2,
                     dCenterX, dZpos, 0, 0,
                     m_iLaserPathID, false, dSpdX, dAccX, true);

    pLaserDriver->OpenLaserGate(LASER, FALSE);
    pMotorDriver->setdefaultspeed(MOTORY);

    if(bCross) {
        //划X方向
        _SysExt_Instance.CutPos(dCenterX - dLineLength / 2,
                         dCenterX + dLineLength / 2,
                         dCenterY, dZpos, 0, 0, m_iLaserPathID, true, 200, 0, true);

    }
    pMotorDriver->setio(IO_OUTPUT, OUT_Shutter_All_Open, FALSE);
    return 0;
}

void CProcess_LaserFoucsCali::do_msg(int iID, QVariantList &vlInput, V_MsgOutput *pvlOutput)
{
    int32_t isize = vlInput.size();
    switch (iID) {
        case LASER_MARK:
            if (isize >= 6) {
                Laser_Mark(vlInput[0].toDouble(), vlInput[1].toDouble(),
                           vlInput[2].toDouble(), vlInput[3].toDouble(),
                           vlInput[4].toDouble(), vlInput[5].toDouble());
            }
            break;
    }
    CMProcess::do_msg(iID, vlInput, pvlOutput);
}


