/*
 * ukss_tools.c
 *
 *  Created on: 13 нояб. 2024 г.
 *      Author: Evgeniy_Sokolov
 */



#include <edrk_main.h>

#include <params.h>
#include <params_alg.h>
#include <params_norma.h>
#include <params_pwm24.h>
#include <params_temper_p.h>
#include <project.h>
#include "IQmathLib.h"
#include "mathlib.h"
#include <optical_bus.h>
#include "adc_tools.h"
#include "CAN_project.h"
#include "CAN_Setup.h"
#include "global_time.h"
#include "v_rotor.h"
#include "ukss_tools.h"
#include "control_station_project.h"
#include "control_station.h"
#include "sync_tools.h"


#pragma DATA_SECTION(Unites2VPU, ".slow_vars")
int Unites2VPU[SIZE_UNITS_OUT]={0};

#pragma DATA_SECTION(Unites2Zadat4ik, ".slow_vars")
int Unites2Zadat4ik[SIZE_UNITS_OUT]={0};

#pragma DATA_SECTION(Unites2BKSSD, ".slow_vars")
int Unites2BKSSD[SIZE_UNITS_OUT]={0};

#pragma DATA_SECTION(Unites2UMU, ".slow_vars")
int Unites2UMU[SIZE_UNITS_OUT]={0};



void edrk_clear_cmd_ukss(void)
{
    int i;

    for (i=0;i<SIZE_UNITS_OUT;i++)
    {
        Unites2Zadat4ik[i] = 0;
        Unites2VPU[i]      = 0;
        Unites2UMU[i]      = 0;
        Unites2BKSSD[i]    = 0;
    }

}



void update_ukss_setup(unsigned int pause)
{
    static unsigned int old_time_ukss1=0;
    int real_mbox;
    unsigned long count_send = 0, start_adr = 0;


#if (CAN_PROTOCOL_UKSS == 1) // старый протокол

    Unites2Zadat4ik[96] = 25;
    Unites2Zadat4ik[97] = 100;
    Unites2Zadat4ik[98] = 8;

    Unites2VPU[96] = 25;
    Unites2VPU[97] = 100;
    Unites2VPU[98] = 8;

    Unites2UMU[96] = 25;
    Unites2UMU[97] = 100;
    Unites2UMU[98] = 8;

    Unites2BKSSD[96] = 25;
    Unites2BKSSD[97] = 100;
    Unites2BKSSD[98] = 8;

    count_send = 3;
    start_adr = 96;
#endif


#if (CAN_PROTOCOL_UKSS == 2) // новый протокол

    Unites2Zadat4ik[ADR_CYCLES_TIMER_MAIN] = 2;
    Unites2Zadat4ik[ADR_CYCLES_TIMER_ADD] = 100;
    Unites2Zadat4ik[ADR_CYCLES_PAUSE_MAIN] = 10;
    Unites2Zadat4ik[ADR_CYCLES_PAUSE_ADD] = 10;
    Unites2Zadat4ik[ADR_CYCLES_REPEATE_MAIN] = 500;
    Unites2Zadat4ik[ADR_CYCLES_REPEATE_ADD] = 1000;
    Unites2Zadat4ik[ADR_CYCLES_REPEATE_DIGIO] = 5;

    Unites2VPU[ADR_CYCLES_TIMER_MAIN] = 2;
    Unites2VPU[ADR_CYCLES_TIMER_ADD] = 100;
    Unites2VPU[ADR_CYCLES_PAUSE_MAIN] = 10;
    Unites2VPU[ADR_CYCLES_PAUSE_ADD] = 10;
    Unites2VPU[ADR_CYCLES_REPEATE_MAIN] = 500;
    Unites2VPU[ADR_CYCLES_REPEATE_ADD] = 1000;
    Unites2VPU[ADR_CYCLES_REPEATE_DIGIO] = 1;

    Unites2UMU[ADR_CYCLES_TIMER_MAIN] = 2;
    Unites2UMU[ADR_CYCLES_TIMER_ADD] = 100;
    Unites2UMU[ADR_CYCLES_PAUSE_MAIN] = 10;
    Unites2UMU[ADR_CYCLES_PAUSE_ADD] = 10;
    Unites2UMU[ADR_CYCLES_REPEATE_MAIN] = 500;
    Unites2UMU[ADR_CYCLES_REPEATE_ADD] = 1000;
    Unites2UMU[ADR_CYCLES_REPEATE_DIGIO] = 1;

    Unites2BKSSD[ADR_CYCLES_TIMER_MAIN] = 2;
    Unites2BKSSD[ADR_CYCLES_TIMER_ADD] = 100;
    Unites2BKSSD[ADR_CYCLES_PAUSE_MAIN] = 10;
    Unites2BKSSD[ADR_CYCLES_PAUSE_ADD] = 10;
    Unites2BKSSD[ADR_CYCLES_REPEATE_MAIN] = 500;
    Unites2BKSSD[ADR_CYCLES_REPEATE_ADD] = 1000;
    Unites2BKSSD[ADR_CYCLES_REPEATE_DIGIO] = 1;

    start_adr = ADR_CYCLES_TIMER_MAIN;
    count_send = ADR_CYCLES_REPEATE_DIGIO - ADR_CYCLES_TIMER_MAIN + 1;
#endif

    if (detect_pause_milisec(pause,&old_time_ukss1))
        {
            real_mbox = get_real_out_mbox (UNITS_TYPE_BOX, ZADATCHIK_CAN);

            if (CAN_cycle_free(real_mbox))
            {
                 CAN_cycle_send( UNITS_TYPE_BOX, ZADATCHIK_CAN, start_adr, &Unites2Zadat4ik[start_adr], count_send, CAN_BOX_STANDART_ADR, CAN_BOX_PRIORITY_NORMAL);// примерно 40 мсек. идет отправка всей посылки
            }

            real_mbox = get_real_out_mbox (UNITS_TYPE_BOX, VPU_CAN);

            if (CAN_cycle_free(real_mbox))
            {
                 CAN_cycle_send( UNITS_TYPE_BOX, VPU_CAN, start_adr, &Unites2VPU[start_adr], count_send, CAN_BOX_STANDART_ADR, CAN_BOX_PRIORITY_NORMAL);// примерно 40 мсек. идет отправка всей посылки
            }

            real_mbox = get_real_out_mbox (UNITS_TYPE_BOX, UMU_CAN_DEVICE);

            if (CAN_cycle_free(real_mbox))
            {
                 CAN_cycle_send( UNITS_TYPE_BOX, UMU_CAN_DEVICE, start_adr, &Unites2UMU[start_adr], count_send, CAN_BOX_STANDART_ADR, CAN_BOX_PRIORITY_NORMAL);// примерно 40 мсек. идет отправка всей посылки
            }

            real_mbox = get_real_out_mbox (UNITS_TYPE_BOX, BKSSD_CAN_DEVICE);

            if (CAN_cycle_free(real_mbox))
            {
                CAN_cycle_send( UNITS_TYPE_BOX, BKSSD_CAN_DEVICE, start_adr, &Unites2BKSSD[start_adr], count_send, CAN_BOX_STANDART_ADR, CAN_BOX_PRIORITY_NORMAL);// примерно 40 мсек. идет отправка всей посылки
            }
        }

}

////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
//  обновлЯем задатчик для датчика момента КГНЦ
////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
void update_ukss_can_moment_kgnc(unsigned int pause)
{
    int real_mbox;
    int t1;

    static unsigned int old_time_ukss2=0, test_flag = 0;



    if (edrk.flag_second_PCH==0)
        t1 = pause;
    if (edrk.flag_second_PCH==1)
        t1 = pause;


//////


    if (test_flag == 0)
    {
        //    32  Длина посылки, слов 13
        //    33  Период посылок RS485 * 10 mсек  25

        Unites2Zadat4ik[32] = 13;//9
        Unites2Zadat4ik[33] = 25;

        //    34   Ток ЗПТ 1 канал, А
        //    35  Напряжение ЗПТ 1 канал, В
        //    36  Ток ЗПТ 2 канал, А
        //    37  Напряжение ЗПТ 2 канал, В
        //    38  Мощность, кВт
        //    39  Обороты, об/мин
        //    40  Номер ПЧ

        if (edrk.flag_second_PCH==0)
        {
            Unites2Zadat4ik[34] =  global_time.total_seconds10full;
            Unites2Zadat4ik[35] =  _IQtoF(analog.iqIin_sum)*NORMA_ACP;
            Unites2Zadat4ik[36] =  _IQtoF(filter.iqU_1_long+filter.iqU_2_long)*NORMA_ACP;
            Unites2Zadat4ik[37] =  fast_round(_IQtoF(edrk.k_stator1)*1000.0);
            Unites2Zadat4ik[38] =  _IQtoF(filter.iqIm_1 + filter.iqIm_2)*NORMA_ACP;
        }

        if (edrk.flag_second_PCH==1)
        {

            Unites2Zadat4ik[39] =  global_time.total_seconds10full;
            Unites2Zadat4ik[40] =  _IQtoF(analog.iqIin_sum)*NORMA_ACP;
            Unites2Zadat4ik[41] =  _IQtoF(filter.iqU_1_long+filter.iqU_2_long)*NORMA_ACP;
            Unites2Zadat4ik[42] =  fast_round(_IQtoF(edrk.k_stator1)*1000.0);
            Unites2Zadat4ik[43] =  _IQtoF(filter.iqIm_1 + filter.iqIm_2)*NORMA_ACP;

        }

        Unites2Zadat4ik[44] =  edrk.power_kw_full;
        Unites2Zadat4ik[45] =  _IQtoF(WRotor.iqWRotorSumFilter) * NORMA_FROTOR*6000.0;//;//_IQtoF(edrk.iq_f_rotor_hz) * NORMA_FROTOR*6000.0;


        if (edrk.pult_data.data_from_pult.nPCH==11 || edrk.pult_data.data_from_pult.nPCH==12)
            Unites2Zadat4ik[46] =  1;
        else
            Unites2Zadat4ik[46] =  2;
    }


    if (detect_pause_milisec(t1,&old_time_ukss2))
    {

            real_mbox = get_real_out_mbox (UNITS_TYPE_BOX, ZADATCHIK_CAN);

            if (CAN_cycle_full_free(real_mbox,CAN_BOX_STAT_ON))
            {
                if (edrk.flag_second_PCH==0)
                {
                      CAN_cycle_send( UNITS_TYPE_BOX, ZADATCHIK_CAN, 32, &Unites2Zadat4ik[32], 7, CAN_BOX_STANDART_ADR, CAN_BOX_PRIORITY_NORMAL);// примерно 40 мсек. идет отправка всей посылки
                      CAN_cycle_send( UNITS_TYPE_BOX, ZADATCHIK_CAN, 44, &Unites2Zadat4ik[44], 3, CAN_BOX_STANDART_ADR, CAN_BOX_PRIORITY_NORMAL);// примерно 40 мсек. идет отправка всей посылки
                }
                else
                {
                      CAN_cycle_send( UNITS_TYPE_BOX, ZADATCHIK_CAN, 32, &Unites2Zadat4ik[32], 2, CAN_BOX_STANDART_ADR, CAN_BOX_PRIORITY_NORMAL);// примерно 40 мсек. идет отправка всей посылки
                      CAN_cycle_send( UNITS_TYPE_BOX, ZADATCHIK_CAN, 39, &Unites2Zadat4ik[39], 8, CAN_BOX_STANDART_ADR, CAN_BOX_PRIORITY_NORMAL);// примерно 40 мсек. идет отправка всей посылки
                }

            }
    }


}

////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
//  обновлЯем задатчик и ВПУ
////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
#define MAX_WAIT_UPDATE_ZAD 10

void update_ukss_can(unsigned int pause)
{
    int real_mbox;
    int t1;

    static int disable_update_zadat4ik = MAX_WAIT_UPDATE_ZAD;
    static int disable_update_vpu = MAX_WAIT_UPDATE_ZAD;

    static unsigned int old_time_ukss2=0;


    static TO_ZADAT4IK prev_Zadat4ik = TO_ZADAT4IK_DEFAULT;
    static TO_VPU prev_VPU = TO_VPU_DEFAULT;



    if (edrk.flag_second_PCH==0)
        t1 = pause;
    if (edrk.flag_second_PCH==1)
        t1 = pause;

    Unites2Zadat4ik[4] = edrk.to_zadat4ik.OBOROTS1.all;
    Unites2Zadat4ik[5] = edrk.to_zadat4ik.OBOROTS2.all;
    Unites2Zadat4ik[6] = edrk.to_zadat4ik.BIG_LAMS.all;
    Unites2Zadat4ik[7] = edrk.to_zadat4ik.APL_LAMS0.all;
    Unites2Zadat4ik[8] = 0;

    Unites2VPU[4] = edrk.to_vpu.OBOROTS1.all;
    Unites2VPU[5] = edrk.to_vpu.OBOROTS2.all;
    Unites2VPU[6] = edrk.to_vpu.BIG_LAMS.all;
    Unites2VPU[7] = 0;
    Unites2VPU[8] = 0;


    if (detect_pause_milisec(t1,&old_time_ukss2))
    {

        if (disable_update_zadat4ik)
            disable_update_zadat4ik--;

        if (prev_Zadat4ik.APL_LAMS0.all != edrk.to_zadat4ik.APL_LAMS0.all
                || prev_Zadat4ik.APL_LAMS_PCH.all != edrk.to_zadat4ik.APL_LAMS_PCH.all
                || prev_Zadat4ik.BIG_LAMS.all != edrk.to_zadat4ik.BIG_LAMS.all
                || prev_Zadat4ik.OBOROTS1.all != edrk.to_zadat4ik.OBOROTS1.all
                || prev_Zadat4ik.OBOROTS2.all != edrk.to_zadat4ik.OBOROTS2.all)
            disable_update_zadat4ik = 0;

        if (disable_update_vpu)
            disable_update_vpu--;

        if (       prev_VPU.BIG_LAMS.all != edrk.to_vpu.BIG_LAMS.all
                || prev_VPU.OBOROTS1.all != edrk.to_vpu.OBOROTS1.all
                || prev_VPU.OBOROTS2.all != edrk.to_vpu.OBOROTS2.all)
            disable_update_vpu = 0;



        if (disable_update_zadat4ik==0)
        {
            real_mbox = get_real_out_mbox (UNITS_TYPE_BOX, ZADATCHIK_CAN);

            if (CAN_cycle_full_free(real_mbox,CAN_BOX_STAT_ON))
            {
                if (edrk.flag_second_PCH==0)
                      CAN_cycle_send( UNITS_TYPE_BOX, ZADATCHIK_CAN, 4, &Unites2Zadat4ik[4], 5, CAN_BOX_STANDART_ADR, CAN_BOX_PRIORITY_NORMAL);// примерно 40 мсек. идет отправка всей посылки
                else
                      CAN_cycle_send( UNITS_TYPE_BOX, ZADATCHIK_CAN, 0xa, &Unites2Zadat4ik[4], 5, CAN_BOX_STANDART_ADR, CAN_BOX_PRIORITY_NORMAL);// примерно 40 мсек. идет отправка всей посылки
            }

            prev_Zadat4ik.APL_LAMS0.all = edrk.to_zadat4ik.APL_LAMS0.all;
            prev_Zadat4ik.APL_LAMS_PCH.all = edrk.to_zadat4ik.APL_LAMS_PCH.all;
            prev_Zadat4ik.BIG_LAMS.all = edrk.to_zadat4ik.BIG_LAMS.all;
            prev_Zadat4ik.OBOROTS1.all = edrk.to_zadat4ik.OBOROTS1.all;
            prev_Zadat4ik.OBOROTS2.all = edrk.to_zadat4ik.OBOROTS2.all;

            disable_update_zadat4ik = MAX_WAIT_UPDATE_ZAD;
        }

        if (disable_update_vpu==0)
        {
            real_mbox = get_real_out_mbox (UNITS_TYPE_BOX, VPU_CAN);

            if (CAN_cycle_full_free(real_mbox,CAN_BOX_STAT_ON))
            {
                if (edrk.flag_second_PCH==0)
                        CAN_cycle_send( UNITS_TYPE_BOX, VPU_CAN, 4, &Unites2VPU[4], 4, CAN_BOX_STANDART_ADR, CAN_BOX_PRIORITY_NORMAL);// примерно 40 мсек. идет отправка всей посылки
                else
                        CAN_cycle_send( UNITS_TYPE_BOX, VPU_CAN, 0xa, &Unites2VPU[4], 4, CAN_BOX_STANDART_ADR, CAN_BOX_PRIORITY_NORMAL);// примерно 40 мсек. идет отправка всей посылки
            }

            prev_VPU.BIG_LAMS.all = edrk.to_vpu.BIG_LAMS.all;
            prev_VPU.OBOROTS1.all = edrk.to_vpu.OBOROTS1.all;
            prev_VPU.OBOROTS2.all = edrk.to_vpu.OBOROTS2.all;

            disable_update_vpu = MAX_WAIT_UPDATE_ZAD;

        }



    }

}



//////////////////////////////////////////////////////////


void update_zadat4ik(void)
{

       static unsigned int time_toggle1 = 0, time_toggle2 = 0, time_toggle3 = 0, time_toggle4 = 0;
       int b;
       static unsigned int time_toggle_leds = 500;


    // update zadatchik
//        if (edrk.from_zadat4ik.bits.MINUS || edrk.from_zadat4ik.bits.PLUS || edrk.from_ing2.bits.KEY_MINUS || edrk.from_ing2.bits.KEY_PLUS || edrk.from_vpu.bits.PLUS || edrk.from_vpu.bits.MINUS)
//        {
//          if (edrk.Mode_ScalarVectorUFConst == ALG_MODE_UF_CONST) // UFCONST
//          {
//           edrk.to_zadat4ik.OBOROTS1.all = fast_round(edrk.zadanie.fzad*100.0);              // обороты заданные
//           edrk.to_vpu.OBOROTS1.all = edrk.to_zadat4ik.OBOROTS1.all;             // обороты заданные
//          }
//          else
//          {
//              edrk.to_zadat4ik.OBOROTS1.all = (edrk.zadanie.oborots_zad);             // обороты заданные
//              edrk.to_vpu.OBOROTS1.all =  edrk.to_zadat4ik.OBOROTS1.all;             // обороты заданные
//          }
//
//        }
//        else
//        {
//
//              if (edrk.Mode_ScalarVectorUFConst == ALG_MODE_UF_CONST) // UFCONST
//              {
//               edrk.to_zadat4ik.OBOROTS1.all = fast_round(_IQtoF(edrk.f_stator)*NORMA_FROTOR*100.0);             // обороты тек.
//               edrk.to_vpu.OBOROTS1.all = edrk.to_zadat4ik.OBOROTS1.all;             // обороты тек.
//              }
//              else
//              {
//                  edrk.to_zadat4ik.OBOROTS1.all = edrk.oborots;             // обороты тек.
//                  edrk.to_vpu.OBOROTS1.all =  edrk.to_zadat4ik.OBOROTS1.all;             // обороты тек.
//              }
//
//        }

        if (edrk.Mode_ScalarVectorUFConst == ALG_MODE_UF_CONST) // UFCONST
        {
            edrk.to_zadat4ik.OBOROTS2.all = fast_round(_IQtoF(edrk.f_stator)*NORMA_FROTOR*100.0);             // обороты тек.
            edrk.to_vpu.OBOROTS2.all = edrk.to_zadat4ik.OBOROTS2.all;             // обороты тек.
        }
        else
        {
            if (control_station.active_array_cmd[CONTROL_STATION_CMD_ROTOR_POWER]==0) // по оборотам
                edrk.to_zadat4ik.OBOROTS2.all = edrk.zadanie.oborots_zad_no_dead_zone;             // обороты заданные
            else
                edrk.to_zadat4ik.OBOROTS2.all = edrk.oborots;   //оборты текущие

            edrk.to_vpu.OBOROTS2.all =  edrk.to_zadat4ik.OBOROTS2.all;             // обороты заданные
        }

        //edrk.to_zadat4ik.OBOROTS2.all = edrk.I_cur_vozbud_exp;    // ток возбудителЯ

        if (edrk.Mode_ScalarVectorUFConst == ALG_MODE_UF_CONST) // UFCONST
        {
            edrk.to_zadat4ik.OBOROTS1.all = fast_round(_IQtoF(edrk.k_stator1)*10000.0);
            edrk.to_vpu.OBOROTS1.all =  edrk.to_zadat4ik.OBOROTS1.all;
        }
        else
        {
//            edrk.to_zadat4ik.OBOROTS2.all = edrk.to_zadat4ik.OBOROTS1.all;
//            edrk.to_vpu.OBOROTS2.all = edrk.to_zadat4ik.OBOROTS2.all;
            edrk.to_zadat4ik.OBOROTS1.all = edrk.oborots;   //оборты текущие
            edrk.to_vpu.OBOROTS1.all = edrk.to_zadat4ik.OBOROTS1.all;

        }


        // Готовность2
            if (edrk.Status_Ready.bits.ready_final)
               edrk.to_zadat4ik.APL_LAMS_PCH.bits.PCH_SHEMA_SOBRANA = 1;//edrk.from_shema.bits.QTV_ON_OFF;
            else
            {
               if (edrk.SumSbor)
               {
                   if (detect_pause_milisec(time_toggle_leds,&time_toggle1))
                     edrk.to_zadat4ik.APL_LAMS_PCH.bits.PCH_SHEMA_SOBRANA = toggle_status_lamp((unsigned int)edrk.to_zadat4ik.APL_LAMS_PCH.bits.PCH_SHEMA_SOBRANA,1);
               }
               else
                   edrk.to_zadat4ik.APL_LAMS_PCH.bits.PCH_SHEMA_SOBRANA = 0;
            }


            if (edrk.Ready2_another_bs && edrk.Status_Ready.bits.ready_final)
                edrk.to_zadat4ik.BIG_LAMS.bits.GOTOV2 = 1;
            else
            {
                if (edrk.Status_Ready.bits.ready_final)
                {
                  if (detect_pause_milisec(time_toggle_leds,&time_toggle3))
                      edrk.to_zadat4ik.BIG_LAMS.bits.GOTOV2 = toggle_status_lamp((unsigned int)edrk.to_zadat4ik.BIG_LAMS.bits.GOTOV2,1);
                }
                else
                    edrk.to_zadat4ik.BIG_LAMS.bits.GOTOV2 = 0;
            }

            edrk.to_zadat4ik.APL_LAMS_PCH.bits.PCH_MESTNOE = control_station.active_control_station[CONTROL_STATION_INGETEAM_PULT_RS485];//edrk.from_ing1.bits.LOCAL_REMOUTE; // LOCAL


            if (edrk.Status_Perehod_Rascepitel==0)
                edrk.to_zadat4ik.APL_LAMS_PCH.bits.PCH_PODKLU4EN = edrk.Final_Status_Rascepitel;
            else
            {
                b =  edrk.to_zadat4ik.APL_LAMS_PCH.bits.PCH_PODKLU4EN;
                if (detect_pause_milisec(time_toggle_leds,&time_toggle2))
                  edrk.to_zadat4ik.APL_LAMS_PCH.bits.PCH_PODKLU4EN = toggle_status_lamp((unsigned int)edrk.to_zadat4ik.APL_LAMS_PCH.bits.PCH_PODKLU4EN,1);
            }

        //  edrk.to_zadat4ik.APL_LAMS_PCH.bits.PCH_PODKLU4EN = edrk.Status_Rascepitel_Ok || edrk.Status_Perehod_Rascepitel;
            edrk.to_zadat4ik.APL_LAMS_PCH.bits.PCH_READY1 = edrk.Status_Ready.bits.ready1;

        //Емкость
            edrk.to_zadat4ik.BIG_LAMS.bits.EMKOST = edrk.Status_Ready.bits.Batt;//edrk.Status_Charge; For 23550.3


            edrk.to_zadat4ik.BIG_LAMS.bits.PEREGREV = edrk.temper_limit_koeffs.code_status;
            if (edrk.power_limit.all)
                edrk.to_zadat4ik.BIG_LAMS.bits.OGRAN_POWER = 1;
            else
                edrk.to_zadat4ik.BIG_LAMS.bits.OGRAN_POWER = 0;

            if (edrk.Ready1_another_bs && edrk.Status_Ready.bits.ready1)
                edrk.to_zadat4ik.BIG_LAMS.bits.GOTOV1 = 1;
            else
            {
                if (edrk.Status_Ready.bits.ready1)
                {
                  if (detect_pause_milisec(time_toggle_leds,&time_toggle4))
                    edrk.to_zadat4ik.BIG_LAMS.bits.GOTOV1 = toggle_status_lamp((unsigned int)edrk.to_zadat4ik.BIG_LAMS.bits.GOTOV1,1);
                }
                else
                    edrk.to_zadat4ik.BIG_LAMS.bits.GOTOV1 = 0;
            }
            edrk.to_zadat4ik.APL_LAMS_PCH.bits.PCH_READY1 = edrk.Status_Ready.bits.ready1;

            edrk.to_zadat4ik.BIG_LAMS.bits.NEISPRAVNOST = edrk.warning;

            if (edrk.flag_second_PCH==0)
            {
                edrk.to_zadat4ik.APL_LAMS0.bits.PCH1_MESTNOE = edrk.to_zadat4ik.APL_LAMS_PCH.bits.PCH_MESTNOE;
                edrk.to_zadat4ik.APL_LAMS0.bits.PCH1_PODKLU4EN = edrk.to_zadat4ik.APL_LAMS_PCH.bits.PCH_PODKLU4EN;
                edrk.to_zadat4ik.APL_LAMS0.bits.PCH1_READY1 = edrk.to_zadat4ik.APL_LAMS_PCH.bits.PCH_READY1;
                edrk.to_zadat4ik.APL_LAMS0.bits.PCH1_SHEMA_SOBRANA = edrk.to_zadat4ik.APL_LAMS_PCH.bits.PCH_SHEMA_SOBRANA;
            }
            else
            {
                edrk.to_zadat4ik.APL_LAMS0.bits.PCH2_MESTNOE = edrk.to_zadat4ik.APL_LAMS_PCH.bits.PCH_MESTNOE;
                edrk.to_zadat4ik.APL_LAMS0.bits.PCH2_PODKLU4EN = edrk.to_zadat4ik.APL_LAMS_PCH.bits.PCH_PODKLU4EN;
                edrk.to_zadat4ik.APL_LAMS0.bits.PCH2_READY1 = edrk.to_zadat4ik.APL_LAMS_PCH.bits.PCH_READY1;
                edrk.to_zadat4ik.APL_LAMS0.bits.PCH2_SHEMA_SOBRANA = edrk.to_zadat4ik.APL_LAMS_PCH.bits.PCH_SHEMA_SOBRANA;
            }


        //  edrk.to_zadat4ik.APL_LAMS0.bits0.GED_PEREGRUZ = 0;
        //  edrk.to_zadat4ik.APL_LAMS0.bits0.PROVOROT = 0;





            if (control_station.active_control_station[CONTROL_STATION_INGETEAM_PULT_RS485])
            {
                edrk.to_zadat4ik.APL_LAMS0.bits.OBOROT_ZADAT = 0;
                edrk.to_zadat4ik.APL_LAMS0.bits.OBOROT_MONITOR = 0;
                edrk.to_zadat4ik.APL_LAMS0.bits.OBOROT_SVU = 0;
                edrk.to_zadat4ik.APL_LAMS0.bits.OBOROT_VPU = 0;
        //       edrk.to_zadat4ik.APL_LAMS0.bits.WORK_PMU = 0;


            }
            else // control_station.active_control_station[CONTROL_STATION_INGETEAM_PULT_RS485] == 0//edrk.RemouteFromDISPLAY==0
            {
                if (edrk.from_shema_filter.bits.SVU==0)
                {
                 edrk.to_zadat4ik.APL_LAMS0.bits.OBOROT_VPU = 0;
                 if (edrk.from_shema_filter.bits.ZADA_DISPLAY)
                 {
                   edrk.to_zadat4ik.APL_LAMS0.bits.OBOROT_MONITOR = 1;
                   edrk.to_zadat4ik.APL_LAMS0.bits.OBOROT_ZADAT = 0;
                 }
                 else
                 {
                   edrk.to_zadat4ik.APL_LAMS0.bits.OBOROT_MONITOR = 0;
                   edrk.to_zadat4ik.APL_LAMS0.bits.OBOROT_ZADAT = 1;
                 }
                }
                else // SVU==1
                {
                   edrk.to_zadat4ik.APL_LAMS0.bits.OBOROT_MONITOR = 0;
                   edrk.to_zadat4ik.APL_LAMS0.bits.OBOROT_ZADAT = 0;
                }

                if (edrk.from_shema_filter.bits.SVU)
                {

                     if (edrk.from_vpu.bits.UOM_READY_ACTIVE)
                     {
                         edrk.to_zadat4ik.APL_LAMS0.bits.OBOROT_VPU = 1;
                         edrk.to_zadat4ik.APL_LAMS0.bits.OBOROT_SVU = 0;
                     }
                     else
                     {
                         edrk.to_zadat4ik.APL_LAMS0.bits.OBOROT_SVU = 1;
                         edrk.to_zadat4ik.APL_LAMS0.bits.OBOROT_VPU = 0;
                     }

                }
                else //edrk.from_shema.bits.SVU == 0
                {
                     edrk.to_zadat4ik.APL_LAMS0.bits.OBOROT_SVU = 0;
                     edrk.to_zadat4ik.APL_LAMS0.bits.OBOROT_VPU = 0;
                }
            }


            edrk.to_zadat4ik.APL_LAMS0.bits.HOD = edrk.Go;
            edrk.to_zadat4ik.APL_LAMS0.bits.PCH1_PCH2_SYNC = sync_data.sync_ready;

        //  if (edrk.from_zadat4ik.bits.PROVOROT)
        //    edrk.to_zadat4ik.APL_LAMS0.bits.PROVOROT = 1;
        //  else
        //    edrk.to_zadat4ik.APL_LAMS0.bits.PROVOROT = 0;

            edrk.to_zadat4ik.BIG_LAMS.bits.AVARIA = edrk.to_ing.bits.SMALL_LAMPA_AVARIA;


}

//////////////////////////////////////////////////////////
