/*
 * ZDistA.c
 *
 *  Created on: 09-03-2017
 *      Author: Krzysztof Jakubczyk
 */

#include <math.h>


#include "../tdefs.h"
#include "../misc.h"
#include "helper.h"
#include "analog_in.h"
#include "e_phi.h"

#include "ZDistA_komp.h"

#define RnaS (3.141593 / 180)
#define filtr_P 5,5

struct nast_pomoc
{
	float R1W_Zf1W_LE;
	float R1W_Zf1W_LL;
	float R1_Zf1_LE;
	float R1_Zf1_LL;
	float R2_Zf2_LE;
	float R2_Zf2_LL;
	float R3_Zf3_LE;
	float R3_Zf3_LL;
	float R4_Zf4_LE;
	float R4_Zf4_LL;
	float R5_Zf5_LE;
	float R5_Zf5_LL;
	float X1W_Zr1W_LE;
	float X1W_Zr1W_LL;
	float X1_Zr1_LE;
	float X1_Zr1_LL;
	float X2_Zr2_LE;
	float X2_Zr2_LL;
	float X3_Zr3_LE;
	float X3_Zr3_LL;
	float X4_Zr4_LE;
	float X4_Zr4_LL;
	float X5_Zr5_LE;
	float X5_Zr5_LL;
};

static float KI0_I2_Re,KI0_I2_Im,M,KI0_I2;//,Z_Z1min_old,Zmf_Z1min_old;

static int blok_do_szyn = 0;
static int blok_do_linii = 0;
static int blokada_od_spz=0;

int ZDistA_komp_initlog(void *arguments, void *logic)
{
	struct ZDistA_komp_args *args = (struct ZDistA_komp_args *)arguments;
	struct ZDistA_komp_logic *log = (struct ZDistA_komp_logic *)logic;
	float locX1,locX2,locR1,locR2;
	float sink,cosk;
	u16 i;

	if(set_bit_ptr_struct(args->io.bl_in,&log->stan_bl))
		return -1;

	if(set_bit_ptr_struct(args->io.bl_k_in,&log->Bl_K))
		return -1;

	if(set_float_ptr(args->io.i1_orta_float_in,&log->I1_orta))
		return -1;
	if(set_float_ptr(args->io.i1_ortb_float_in,&log->I1_ortb))
		return -1;
	if(set_float_ptr(args->io.i1_float_in,&log->I1))
		return -1;
	if(set_float_ptr(args->io.i2_orta_float_in,&log->I2_orta))
		return -1;
	if(set_float_ptr(args->io.i2_ortb_float_in,&log->I2_ortb))
		return -1;
	if(set_float_ptr(args->io.i2_float_in,&log->I2))
		return -1;
	if(set_float_ptr(args->io.i3_orta_float_in,&log->I3_orta))
		return -1;
	if(set_float_ptr(args->io.i3_ortb_float_in,&log->I3_ortb))
		return -1;
	if(set_float_ptr(args->io.i3_float_in,&log->I3))
		return -1;

	if(set_float_ptr(args->io.u1_orta_float_in,&log->U1_orta))
		return -1;
	if(set_float_ptr(args->io.u1_ortb_float_in,&log->U1_ortb))
		return -1;
	if(set_float_ptr(args->io.u1_float_in,&log->U1))
		return -1;
	if(set_float_ptr(args->io.u2_orta_float_in,&log->U2_orta))
		return -1;
	if(set_float_ptr(args->io.u2_ortb_float_in,&log->U2_ortb))
		return -1;
	if(set_float_ptr(args->io.u2_float_in,&log->U2))
		return -1;
	if(set_float_ptr(args->io.u3_orta_float_in,&log->U3_orta))
		return -1;
	if(set_float_ptr(args->io.u3_ortb_float_in,&log->U3_ortb))
		return -1;
	if(set_float_ptr(args->io.u3_float_in,&log->U3))
		return -1;

	if(set_float_ptr(args->io.u12_orta_float_in,&log->U12_orta))
		return -1;
	if(set_float_ptr(args->io.u12_ortb_float_in,&log->U12_ortb))
		return -1;
	if(set_float_ptr(args->io.u12_float_in,&log->U12))
		return -1;
	if(set_float_ptr(args->io.u23_orta_float_in,&log->U23_orta))
		return -1;
	if(set_float_ptr(args->io.u23_ortb_float_in,&log->U23_ortb))
		return -1;
	if(set_float_ptr(args->io.u23_float_in,&log->U23))
		return -1;
	if(set_float_ptr(args->io.u31_orta_float_in,&log->U31_orta))
		return -1;
	if(set_float_ptr(args->io.u31_ortb_float_in,&log->U31_ortb))
		return -1;
	if(set_float_ptr(args->io.u31_float_in,&log->U31))
		return -1;

	if(set_float_ptr(args->io.i1_zg_orta_float_in,&log->sI1_orta))
		return -1;
	if(set_float_ptr(args->io.i1_zg_ortb_float_in,&log->sI1_ortb))
		return -1;
	if(set_float_ptr(args->io.i1_zg_float_in,&log->sI1))
		return -1;

	if(set_float_ptr(args->io.i2_pr_orta_float_in,&log->sI2_orta))
		return -1;
	if(set_float_ptr(args->io.i2_pr_ortb_float_in,&log->sI2_ortb))
		return -1;
	if(set_float_ptr(args->io.i2_pr_float_in,&log->sI2))
		return -1;

	if(set_float_ptr(args->io.io_orta_float_in,&log->sI0_orta))
		return -1;
	if(set_float_ptr(args->io.io_ortb_float_in,&log->sI0_ortb))
		return -1;
	if(set_float_ptr(args->io.io_float_in,&log->sI0))
		return -1;

	if(set_float_ptr(args->io.u1_zg_orta_float_in,&log->sU1_orta))
		return -1;
	if(set_float_ptr(args->io.u1_zg_ortb_float_in,&log->sU1_ortb))
		return -1;
	if(set_float_ptr(args->io.u1_zg_float_in,&log->sU1))
		return -1;

	if(set_float_ptr(args->io.u2_pr_orta_float_in,&log->sU2_orta))
		return -1;
	if(set_float_ptr(args->io.u2_pr_ortb_float_in,&log->sU2_ortb))
		return -1;
	if(set_float_ptr(args->io.u2_pr_float_in,&log->sU2))
		return -1;

	if(set_float_ptr(args->io.uo_orta_float_in,&log->sU0_orta))
		return -1;
	if(set_float_ptr(args->io.uo_ortb_float_in,&log->sU0_ortb))
		return -1;
	if(set_float_ptr(args->io.uo_float_in,&log->sU0))
		return -1;

	if(set_pointer_in_ptr(args->io.i_param_an_ptr_in,(u32 *)&log->param_I))
		return -1;

	if(set_pointer_in_ptr(args->io.u_param_an_ptr_in,(u32 *)&log->param_U))
		return -1;

	if(set_bit_ptr_struct(args->io.test_in,&log->test))
		return -1;

	if(set_bit_ptr_struct(args->io.deakt_in,&log->deakt))
		return -1;

	//kompensacja pradu linii rownoleglej
	if(set_float_ptr(args->io.i_rown_orta_float_in,&log->I_row_orta))
		return -1;
	if(set_float_ptr(args->io.i_rown_ortb_float_in,&log->I_row_ortb))
		return -1;
	if(set_pointer_in_ptr(args->io.i_rown_an_ptr_in,(u32 *)&log->param_I_rown))
		return -1;

	if(set_bit_ptr_struct(args->io.wyl_in,&log->wyl))
		return -1;

	if(set_bit_ptr_struct(args->io.P1W_L1E_out,&log->P1W_L1E))
		return -1;
	if(set_bit_ptr_struct(args->io.P1W_L2E_out,&log->P1W_L2E))
		return -1;
	if(set_bit_ptr_struct(args->io.P1W_L3E_out,&log->P1W_L3E))
		return -1;

	if(set_bit_ptr_struct(args->io.P1W_L1L2_out,&log->P1W_L1L2))
		return -1;
	if(set_bit_ptr_struct(args->io.P1W_L2L3_out,&log->P1W_L2L3))
		return -1;
	if(set_bit_ptr_struct(args->io.P1W_L3L1_out,&log->P1W_L3L1))
		return -1;

	if(set_bit_ptr_struct(args->io.P1_L1E_out,&log->P1_L1E))
		return -1;
	if(set_bit_ptr_struct(args->io.P1_L2E_out,&log->P1_L2E))
		return -1;
	if(set_bit_ptr_struct(args->io.P1_L3E_out,&log->P1_L3E))
		return -1;

	if(set_bit_ptr_struct(args->io.P1_L1L2_out,&log->P1_L1L2))
		return -1;
	if(set_bit_ptr_struct(args->io.P1_L2L3_out,&log->P1_L2L3))
		return -1;
	if(set_bit_ptr_struct(args->io.P1_L3L1_out,&log->P1_L3L1))
		return -1;

	if(set_bit_ptr_struct(args->io.P2_L1E_out,&log->P2_L1E))
		return -1;
	if(set_bit_ptr_struct(args->io.P2_L2E_out,&log->P2_L2E))
		return -1;
	if(set_bit_ptr_struct(args->io.P2_L3E_out,&log->P2_L3E))
		return -1;

	if(set_bit_ptr_struct(args->io.P2_L1L2_out,&log->P2_L1L2))
		return -1;
	if(set_bit_ptr_struct(args->io.P2_L2L3_out,&log->P2_L2L3))
		return -1;
	if(set_bit_ptr_struct(args->io.P2_L3L1_out,&log->P2_L3L1))
		return -1;

	if(set_bit_ptr_struct(args->io.P3_L1E_out,&log->P3_L1E))
		return -1;
	if(set_bit_ptr_struct(args->io.P3_L2E_out,&log->P3_L2E))
		return -1;
	if(set_bit_ptr_struct(args->io.P3_L3E_out,&log->P3_L3E))
		return -1;

	if(set_bit_ptr_struct(args->io.P3_L1L2_out,&log->P3_L1L2))
		return -1;
	if(set_bit_ptr_struct(args->io.P3_L2L3_out,&log->P3_L2L3))
		return -1;
	if(set_bit_ptr_struct(args->io.P3_L3L1_out,&log->P3_L3L1))
		return -1;

	if(set_bit_ptr_struct(args->io.P4_L1E_out,&log->P4_L1E))
		return -1;
	if(set_bit_ptr_struct(args->io.P4_L2E_out,&log->P4_L2E))
		return -1;
	if(set_bit_ptr_struct(args->io.P4_L3E_out,&log->P4_L3E))
		return -1;

	if(set_bit_ptr_struct(args->io.P4_L1L2_out,&log->P4_L1L2))
		return -1;
	if(set_bit_ptr_struct(args->io.P4_L2L3_out,&log->P4_L2L3))
		return -1;
	if(set_bit_ptr_struct(args->io.P4_L3L1_out,&log->P4_L3L1))
		return -1;

	if(set_bit_ptr_struct(args->io.P5_L1E_out,&log->P5_L1E))
		return -1;
	if(set_bit_ptr_struct(args->io.P5_L2E_out,&log->P5_L2E))
		return -1;
	if(set_bit_ptr_struct(args->io.P5_L3E_out,&log->P5_L3E))
		return -1;

	if(set_bit_ptr_struct(args->io.P5_L1L2_out,&log->P5_L1L2))
		return -1;
	if(set_bit_ptr_struct(args->io.P5_L2L3_out,&log->P5_L2L3))
		return -1;
	if(set_bit_ptr_struct(args->io.P5_L3L1_out,&log->P5_L3L1))
		return -1;

	if(set_pointer_out_ptr(args->io.wy_ptr_out,(u32 *)&log->nast_.wyjscie))
		return -1;

	//debug
	if(set_float_ptr(args->io.z1_float_out,&log->z[0]))
		return -1;
	if(set_float_ptr(args->io.z2_float_out,&log->z[1]))
		return -1;
	if(set_float_ptr(args->io.z3_float_out,&log->z[2]))
		return -1;
	if(set_float_ptr(args->io.z4_float_out,&log->z[3]))
		return -1;
	if(set_float_ptr(args->io.z5_float_out,&log->z[4]))
		return -1;
	if(set_float_ptr(args->io.z6_float_out,&log->z[5]))
		return -1;

	log->nast_.on_ = (args->params.bity & 0x0001)?1:0;
	log->nast_.bl_ = (args->params.bity & 0x0002)?1:0;
	log->nast_.Bl_L[1] = (args->params.bity & 0x0004)?1:0;
	log->nast_.Bl_L[2] = (args->params.bity & 0x0008)?1:0;
	log->nast_.Bl_L[3] = (args->params.bity & 0x0010)?1:0;
	log->nast_.Bl_L[4] = (args->params.bity & 0x0020)?1:0;
	log->nast_.Bl_L[5] = (args->params.bity & 0x0040)?1:0;
	log->nast_.Bl_L[0] =  (args->params.bity & 0x0080)?1:0;

	log->nast_.z6_kolo = (args->params.bity & 0x0100)?1:0;

#define ZDISTA_POPRAWKA_1	(1) //(args->params.bity & 0x0200) // 150% imp
#define ZDISTA_POPRAWKA_2	(0) //(args->params.bity & 0x0400) // PUSTY
#define ZDISTA_POPRAWKA_3	(1) //(args->params.bity & 0x0800) // Michala P.
#define ZDISTA_POPRAWKA_4	(0) //(args->params.bity & 0x1000) // Radka
#define ZDISTA_POPRAWKA_5	(1) //(args->params.bity & 0x2000) // 150% imp miedzyfazowych - blokuj mfazowe petle
#define ZDISTA_POPRAWKA_6	(0) //(args->params.bity & 0x4000) // 150% imp miedzyfazowych - blokuj fazowe petle
#define ZDISTA_POPRAWKA_7	(0) //(args->params.bity & 0x8000) // KJ rodzaj zwarcia + energia
#define ZDISTA_POPRAWKA_4C	(0) //(args->params.bity & 0x10000) // 4C Radka
#define ZDISTA_POPRAWKA_8	(1) //(args->params.bity & 0x20000) // filtr wylacznikowy

	//wyliczenie przekladni impedancyjnej
	log->nast_.wyjscie.przekladnia = log->param_I->znam_wtor / log->param_U->znam_wtor;
	log->nast_.wyjscie.Zdist_dw = &log->dw;
	log->nast_.wyjscie.on = &log->nast_.on_;

	//wyliczenie wektorow kompensacji ziemnozwarciowej
	log->nast_.ReK1 = 3 * args->params.Kk1 * cos(RnaS * -args->params.Kk1_kat);
	log->nast_.ImK1 = 3 * args->params.Kk1 * sin(RnaS * -args->params.Kk1_kat);
	log->nast_.ReKr = 3 * args->params.KkC * cos(RnaS * -args->params.KkC_kat);
	log->nast_.ImKr = 3 * args->params.KkC * sin(RnaS * -args->params.KkC_kat);

	log->nast_.kp = args->params.kp;
	log->nast_.kpp = 1/args->params.kp;
	log->nast_.kpk = log->nast_.kpp * log->nast_.kpp;

	log->nast_.kp_obc = args->params.kp_obc;
	log->nast_.kpp_obc = 1 / log->nast_.kp_obc;

	log->nast_.typ[0] = args->params.Typ0;
	log->nast_.typ[1] = args->params.Typ1;
	log->nast_.typ[2] = args->params.Typ2;
	log->nast_.typ[3] = args->params.Typ3;
	log->nast_.typ[4] = args->params.Typ4;
	log->nast_.typ[5] = args->params.Typ5;

	sink = sin(RnaS * args->params.fi1);
	cosk = cos(RnaS * args->params.fi1);

	log->nast_.tanfi2 = tan(RnaS * args->params.fi2);
	log->nast_.tanfi1 = tan(RnaS * args->params.fi1);


	struct nast_pomoc n_pomoc;

	n_pomoc.R1W_Zf1W_LE = (float)args->params.R1W_Zf1W_LE;
	n_pomoc.R1W_Zf1W_LL = (float)args->params.R1W_Zf1W_LL;
	n_pomoc.R1_Zf1_LE = (float)args->params.R1_Zf1_LE;
	n_pomoc.R1_Zf1_LL = (float)args->params.R1_Zf1_LL;
	n_pomoc.R2_Zf2_LE = (float)args->params.R2_Zf2_LE;
	n_pomoc.R2_Zf2_LL = (float)args->params.R2_Zf2_LL;
	n_pomoc.R3_Zf3_LE = (float)args->params.R3_Zf3_LE;
	n_pomoc.R3_Zf3_LL = (float)args->params.R3_Zf3_LL;
	n_pomoc.R4_Zf4_LE = (float)args->params.R4_Zf4_LE;
	n_pomoc.R4_Zf4_LL = (float)args->params.R4_Zf4_LL;
	n_pomoc.R5_Zf5_LE = (float)args->params.R5_Zf5_LE;
	n_pomoc.R5_Zf5_LL = (float)args->params.R5_Zf5_LL;
	n_pomoc.X1W_Zr1W_LE = (float)args->params.X1W_Zr1W_LE;
	n_pomoc.X1W_Zr1W_LL = (float)args->params.X1W_Zr1W_LL;
	n_pomoc.X1_Zr1_LE = (float)args->params.X1_Zr1_LE;
	n_pomoc.X1_Zr1_LL = (float)args->params.X1_Zr1_LL;
	n_pomoc.X2_Zr2_LE = (float)args->params.X2_Zr2_LE;
	n_pomoc.X2_Zr2_LL = (float)args->params.X2_Zr2_LL;
	n_pomoc.X3_Zr3_LE = (float)args->params.X3_Zr3_LE;
	n_pomoc.X3_Zr3_LL = (float)args->params.X3_Zr3_LL;
	n_pomoc.X4_Zr4_LE = (float)args->params.X4_Zr4_LE;
	n_pomoc.X4_Zr4_LL = (float)args->params.X4_Zr4_LL;
	n_pomoc.X5_Zr5_LE = (float)args->params.X5_Zr5_LE;
	n_pomoc.X5_Zr5_LL = (float)args->params.X5_Zr5_LL;


	 //Impedancja konca pierwszej strefy
	    //dopisalem 28.01.2026 KI0/I2
	    M = _rcpsp((1+log->nast_.ImK1)* (1+log->nast_.ImK1) + (log->nast_.ReK1 * log->nast_.ReK1));
	    KI0_I2_Re  = - log->nast_.ImK1 * M;
	    KI0_I2_Im  = (1+log->nast_.ReK1) * M;
	    KI0_I2 = sqrtf((KI0_I2_Re*KI0_I2_Re) + (KI0_I2_Im*KI0_I2_Im));

	    float A,B;
	    A = 1.0f + 3.0f * log->nast_.ReK1;
	    B =  3.0f * log->nast_.ImK1;
	    KI0_I2 = _rcpsp(sqrtf((A*A) + (B*B)));

	    if(args->params.K0 <= 1)
	    {
	    	log->dw.modul_zf[0] = ((args->params.R1W_Zf1W_LE * args->params.R1W_Zf1W_LE) + (args->params.X1W_Zr1W_LE * args->params.X1W_Zr1W_LE))* (log->nast_.wyjscie.przekladnia*log->nast_.wyjscie.przekladnia) *(0.15f*0.15f) ;
	    	log->dw.modul_zmf[0] = ((args->params.R1W_Zf1W_LL * args->params.R1W_Zf1W_LL) + (args->params.X1W_Zr1W_LL * args->params.X1W_Zr1W_LL))* (log->nast_.wyjscie.przekladnia*log->nast_.wyjscie.przekladnia) *(0.15f*0.15f) ;
	    }
	    else
	    {
	    	log->dw.modul_zf[0]=9999.9f;
	        log->dw.modul_zmf[0]=9999.9f;
	    }

	    if(args->params.K1 <= 1)
	    {
	    	log->dw.modul_zf[1] = ((args->params.R1_Zf1_LE * args->params.R1_Zf1_LE) + (args->params.X1_Zr1_LE * args->params.X1_Zr1_LE))* (log->nast_.wyjscie.przekladnia*log->nast_.wyjscie.przekladnia) *(0.15f*0.15f) ;
	    	log->dw.modul_zmf[1] = ((args->params.R1_Zf1_LL * args->params.R1_Zf1_LL) + (args->params.X1_Zr1_LL * args->params.X1_Zr1_LL))* (log->nast_.wyjscie.przekladnia*log->nast_.wyjscie.przekladnia) *(0.15f*0.15f) ;
	    }
	    else
	    {
	    	log->dw.modul_zf[1]=9999.9f;
	        log->dw.modul_zmf[1]=9999.9f;
	    }

	    if(args->params.K2 <= 1)
	    {
	    	log->dw.modul_zf[2] = ((args->params.R2_Zf2_LE * args->params.R2_Zf2_LE) + (args->params.X2_Zr2_LE * args->params.X2_Zr2_LE))* (log->nast_.wyjscie.przekladnia*log->nast_.wyjscie.przekladnia) *(0.15f*0.15f) ;
	    	log->dw.modul_zmf[2] = ((args->params.R2_Zf2_LL * args->params.R2_Zf2_LL) + (args->params.X2_Zr2_LL * args->params.X2_Zr2_LL))* (log->nast_.wyjscie.przekladnia*log->nast_.wyjscie.przekladnia) *(0.15f*0.15f) ;
	    }
	    else
	    {
	    	log->dw.modul_zf[2]=9999.9f;
	        log->dw.modul_zmf[2]=9999.9f;
	    }

	    if(args->params.K3 <= 1)
	    {
	    	log->dw.modul_zf[3] = ((args->params.R3_Zf3_LE * args->params.R3_Zf3_LE) + (args->params.X3_Zr3_LE * args->params.X3_Zr3_LE))* (log->nast_.wyjscie.przekladnia*log->nast_.wyjscie.przekladnia) *(0.15f*0.15f) ;
	    	log->dw.modul_zmf[3] = ((args->params.R3_Zf3_LL * args->params.R3_Zf3_LL) + (args->params.X3_Zr3_LL * args->params.X3_Zr3_LL))* (log->nast_.wyjscie.przekladnia*log->nast_.wyjscie.przekladnia) *(0.15f*0.15f) ;
	    }
	    else
	    {
	    	log->dw.modul_zf[3]=9999.9f;
	        log->dw.modul_zmf[3]=9999.9f;
	    }

	    if(args->params.K4 <= 1)
	    {
	    	log->dw.modul_zf[4] = ((args->params.R4_Zf4_LE * args->params.R4_Zf4_LE) + (args->params.X4_Zr4_LE * args->params.X4_Zr4_LE))* (log->nast_.wyjscie.przekladnia*log->nast_.wyjscie.przekladnia) *(0.15f*0.15f) ;
	    	log->dw.modul_zmf[4] = ((args->params.R4_Zf4_LL * args->params.R4_Zf4_LL) + (args->params.X4_Zr4_LL * args->params.X4_Zr4_LL))* (log->nast_.wyjscie.przekladnia*log->nast_.wyjscie.przekladnia) *(0.15f*0.15f) ;
	    }
	    else
	    {
	    	log->dw.modul_zf[4]=9999.9f;
	        log->dw.modul_zmf[4]=9999.9f;
	    }

	    if(args->params.K5 <= 1)
	    {
	    	log->dw.modul_zf[5] = ((args->params.R5_Zf5_LE * args->params.R5_Zf5_LE) + (args->params.X5_Zr5_LE * args->params.X5_Zr5_LE))* (log->nast_.wyjscie.przekladnia*log->nast_.wyjscie.przekladnia) *(0.15f*0.15f) ;
	    	log->dw.modul_zmf[5] = ((args->params.R5_Zf5_LL * args->params.R5_Zf5_LL) + (args->params.X5_Zr5_LL * args->params.X5_Zr5_LL))* (log->nast_.wyjscie.przekladnia*log->nast_.wyjscie.przekladnia) *(0.15f*0.15f) ;
	    }
	    else
	    {
	    	log->dw.modul_zf[5]=9999.9f;
	        log->dw.modul_zmf[5]=9999.9f;
	    }

	    log->dw.Z_min=9999.9f;
	    log->dw.Z_min_mf=9999.9f;

//	    Z_Z1min_old   = ((args->params.R1_Zf1_LE * args->params.R1_Zf1_LE) + (args->params.X1_Zr1_LE * args->params.X1_Zr1_LE))* (log->nast_.wyjscie.przekladnia*log->nast_.wyjscie.przekladnia) *(0.15f*0.15f) ;
	//    Zmf_Z1min_old = ((args->params.R1_Zf1_LL * args->params.R1_Zf1_LL) + (args->params.X1_Zr1_LL * args->params.X1_Zr1_LL)) * (log->nast_.wyjscie.przekladnia*log->nast_.wyjscie.przekladnia) *(0.15f*0.15f) ;
	//----------------------------------------------------------------------------------------------


	for (i=0;i<12;i++)
	{
		if (log->nast_.typ[i/2])
		{
			log->nast_.n_pol[i/2][i%2].Z = (*(float*)(&n_pomoc.R1W_Zf1W_LE + i) + *(float*)(&n_pomoc.X1W_Zr1W_LE + i)) * log->nast_.wyjscie.przekladnia / 2;
			log->nast_.n_pol[i/2][i%2].Zp = log->nast_.n_pol[i/2][i%2].Z * args->params.kp;
			log->nast_.n_pol[i/2][i%2].Z *= log->nast_.n_pol[i/2][i%2].Z;
			log->nast_.n_pol[i/2][i%2].Zp *= log->nast_.n_pol[i/2][i%2].Zp;
			locR1 = *(float*)(&n_pomoc.R1W_Zf1W_LE + i) * cosk;
			locR2 = *(float*)(&n_pomoc.X1W_Zr1W_LE + i) * cosk;
			locX1 = *(float*)(&n_pomoc.R1W_Zf1W_LE + i) * sink;
			locX2 = *(float*)(&n_pomoc.X1W_Zr1W_LE + i) * sink;
			log->nast_.n_pol[i/2][i%2].Rr = (locR1 - (locR1 + locR2 ) / 2) * log->nast_.wyjscie.przekladnia;
			log->nast_.n_pol[i/2][i%2].Xr = (locX1 - (locX1 + locX2 ) / 2) * log->nast_.wyjscie.przekladnia;
			locR1 = *(float*)(&n_pomoc.R1W_Zf1W_LE + i) * cosk * args->params.kp;
			locR2 = *(float*)(&n_pomoc.X1W_Zr1W_LE + i) * cosk * args->params.kp;
			locX1 = *(float*)(&n_pomoc.R1W_Zf1W_LE + i) * sink * args->params.kp;
			locX2 = *(float*)(&n_pomoc.X1W_Zr1W_LE + i) * sink * args->params.kp;
			log->nast_.n_pol[i/2][i%2].Rp = (locR1 - (locR1 + locR2 ) / 2) * log->nast_.wyjscie.przekladnia;
			log->nast_.n_pol[i/2][i%2].Xp = (locX1 - (locX1 + locX2 ) / 2) * log->nast_.wyjscie.przekladnia;


		} else {
			log->nast_.n_pol[i/2][i%2].Rr = *(float *)(&n_pomoc.R1W_Zf1W_LE + i) * log->nast_.wyjscie.przekladnia;
			log->nast_.n_pol[i/2][i%2].Xr = *(float *)(&n_pomoc.X1W_Zr1W_LE + i) * log->nast_.wyjscie.przekladnia;
			log->nast_.n_pol[i/2][i%2].Rp = log->nast_.n_pol[i/2][i%2].Rr * args->params.kp;
			log->nast_.n_pol[i/2][i%2].Xp = log->nast_.n_pol[i/2][i%2].Xr * args->params.kp;

			log->nast_.n_pol[i/2][i%2].Rrtanfi1 = log->nast_.n_pol[i/2][i%2].Rr * log->nast_.tanfi1;
			log->nast_.n_pol[i/2][i%2].Rptanfi1 = log->nast_.n_pol[i/2][i%2].Rp * log->nast_.tanfi1;

		}
	}


	log->nast_.Xr1f = args->params.X1_Zr1_LE * (1 + log->nast_.tanfi2 / log->nast_.tanfi1) * log->nast_.wyjscie.przekladnia;
	log->nast_.Xr1fp = log->nast_.Xr1f * args->params.kp;
	log->nast_.Xr1Wf = args->params.X1W_Zr1W_LE * (1 + log->nast_.tanfi2 / log->nast_.tanfi1) * log->nast_.wyjscie.przekladnia;
	log->nast_.Xr1Wfp = log->nast_.Xr1Wf * args->params.kp;
	log->nast_.Igr = args->params.I_min * args->params.I_min;
	log->nast_.Igrp = log->nast_.Igr * log->nast_.kpk;

	log->nast_.Zgr = 0.01f * log->nast_.wyjscie.przekladnia * log->nast_.wyjscie.przekladnia;
	log->nast_.kierunek[0] = args->params.K0;
	log->nast_.kierunek[1] = args->params.K1;
	log->nast_.kierunek[2] = args->params.K2;
	log->nast_.kierunek[3] = args->params.K3;
	log->nast_.kierunek[4] = args->params.K4;
	log->nast_.kierunek[5] = args->params.K5;

//szukanie Zmin w kier przod lub w tyl
//	for (i=0;i<6;i++)
//	{
//		if (log->nast_.kierunek[i]==0)||(log->nast_.kierunek[i]==1)
//		{//kierunek do linii lub bez kier
//			
//		}
//	}


//Z_ZminPrzod = sqrtf((args->params.R1_Zf1_LE * args->params.R1_Zf1_LE) + (args->params.X1_Zr1_LE * args->params.X1_Zr1_LE))*1.25f;
//Z_ZminTyl = sqrtf((args->params.R3_Zf3_LE * args->params.R3_Zf3_LE) + (args->params.X3_Zr3_LE * args->params.X3_Zr3_LE))*1.25f;
	
	

//A2

	log->nast_.XKR = -tan(RnaS * args->params.fi4);
	log->nast_.XKX = -tan(RnaS * (90 - args->params.fi3));
	log->nast_.KL = tan(RnaS * args->params.fi5);
	log->nast_.RLf = args->params.RLf;
	log->nast_.RLr = - args->params.RLr;
	log->nast_.Iogr = args->params.Iomin * args->params.Iomin * 0.11111f;
	log->nast_.khio = args->params.Iokh * args->params. Iokh * 0.11111f;
	log->nast_.Uomin = args->params.Uomin * args->params.Uomin * 0.11111f;

	log->dw.log_ptr = log;
	log->nast_.wyjscie.SOTF_zwrotnie = 0xFF;

	log->l_nieustalony = 5;

	log->nast_.ReKrown = args->params.Krown * cos(RnaS * -args->params.Krown_kat);
	log->nast_.ImKrown = args->params.Krown * sin(RnaS * -args->params.Krown_kat);
	
	log->nast_.Krown_ignac =  (log->param_I_rown->znam_pierw / log->param_I->znam_wtor) /(log->param_I->znam_pierw/ log->param_I->znam_wtor);
	
	return 0;
}

void ZDistA_komp(void *arguments, void *logic)
{
	struct ZDistA_komp_logic *log = (struct ZDistA_komp_logic *)logic;
	struct ZDistA_komp_args *args = (struct ZDistA_komp_args *)arguments;
	float KIoorta,KIoortb,Iorta,Iortb,I,Uorta,Uortb;
	u8 wk1,wk2,wk1p,wk2p;
	u16 i,j;
	float local,localp,local0,local0p;
	float local1,local1p,local2,local2p,local3,local3p;
	float KRownorta,KRownotrb;

	log->dw.I1 = *log->I1;
	log->dw.I2 = *log->I2;
	log->dw.I3 = *log->I3;
	log->dw.U1 = *log->U1;
	log->dw.U2 = *log->U2;
	log->dw.U3 = *log->U3;
	
	if(log->nast_.on_ && !check_struct(&log->deakt))   // jesli zabezpieczenie aktywne
    {
		int wydl_po_wyl = 0;
		
		//poprawka
			u8 blok_szpilki=0;

			if(ZDISTA_POPRAWKA_8)
			{
				if(*log->U1 < (0.15f*0.15f) && *log->U2 < (0.15f*0.15f) && *log->U3 < (0.15f*0.15f))
				{
					blok_szpilki=1;
					if(*log->U1 > (0.01f*0.01f) && *log->U2 > (0.01f*0.01f) && *log->U3 > (0.01f*0.01f))
					{
						float phi,phi1,phi2,phi3;
						blok_szpilki=2;
						phi1=get_phase(*log->U1_ortb,*log->U1_orta);
						phi2=get_phase(*log->U2_ortb,*log->U2_orta);
						phi3=get_phase(*log->U3_ortb,*log->U3_orta);
						phi=get_phase_diff(phi1,phi2);
						phi1=get_phase_diff(phi2,phi3);
						if(fabs(phi)<5 && fabs(phi1)<5)
						{
							blok_szpilki=3;
						}
					}
				}
			}

		if (check_struct(&log->wyl) != 0 /*|| blok_szpilki==3*/)
		{
			wydl_po_wyl = 10;//10; //zmieniono 09.02.2026
		}
		else
		{
			wydl_po_wyl = 0;
		}
		
		//wyliczenie impedancji petli zwarciowych
		//--------------------------------------------------------------------------------
		//wyliczenie K * 3Io dla strefy pierwszej
		KIoorta = *log->sI0_orta * log->nast_.ReK1 -
				*log->sI0_ortb * log->nast_.ImK1;
		KIoortb = *log->sI0_orta * log->nast_.ImK1 +
				*log->sI0_ortb * log->nast_.ReK1;
		
		//wspolczynnik 
		KRownorta = *log->I_row_orta * log->nast_.ReKrown -
				*log->I_row_ortb * log->nast_.ImKrown;

		KRownotrb = *log->I_row_orta * log->nast_.ImKrown +
				*log->I_row_ortb * log->nast_.ReKrown;
		
		KRownorta *= log->nast_.Krown_ignac;
		KRownotrb *= log->nast_.Krown_ignac;
		
		//petla zwarciowa L1-E strefa pierwsza
		// I = I1 -  k * 3 Io
		Iorta = *log->I1_orta + KIoorta - KRownorta;
		Iortb = *log->I1_ortb + KIoortb - KRownotrb;

		I = _rcpsp(Iorta * Iorta + Iortb * Iortb);
		// Z = U / (I1 - k * 3Io)
		log->dw.Z.R[0] = (*log->U1_orta * Iorta +
			*log->U1_ortb * Iortb) * I;
		log->dw.Z.X[0] = (*log->U1_orta * Iortb -
				*log->U1_ortb * Iorta) * I;
		log->dw.Z.Z[0] = log->dw.Z.X[0] * log->dw.Z.X[0] + log->dw.Z.R[0] * log->dw.Z.R[0];

//		log->dw.Z.E[0] = (Iorta*Iorta) + (Iortb*Iortb);

		//petla zwarciowa L2-E strefa pierwsza
		// I = I2 -  k * 3 Io
		Iorta = *log->I2_orta + KIoorta- KRownorta;
		Iortb = *log->I2_ortb + KIoortb - KRownotrb;
		I = _rcpsp(Iorta * Iorta + Iortb * Iortb);
		// Z = U / (I2 - k * 3Io)
		log->dw.Z.R[1] = (*log->U2_orta * Iorta +
				*log->U2_ortb * Iortb) * I;
		log->dw.Z.X[1] = (*log->U2_orta * Iortb -
				*log->U2_ortb * Iorta) * I;
		log->dw.Z.Z[1] = log->dw.Z.X[1] * log->dw.Z.X[1] + log->dw.Z.R[1] * log->dw.Z.R[1];

//		log->dw.Z.E[1] = (Iorta*Iorta) + (Iortb*Iortb);

		//petla zwarciowa L3-E strefa pierwsza
		// I = I3 -  k * 3 Io
		Iorta = *log->I3_orta + KIoorta- KRownorta;
		Iortb = *log->I3_ortb + KIoortb - KRownotrb;
		I = _rcpsp(Iorta * Iorta + Iortb * Iortb);
		// Z = U / (I3 - k * 3Io)
		log->dw.Z.R[2] = (*log->U3_orta * Iorta +
				*log->U3_ortb * Iortb) * I;
		log->dw.Z.X[2] = (*log->U3_orta * Iortb -
				*log->U3_ortb * Iorta) * I;
		log->dw.Z.Z[2] = log->dw.Z.X[2] * log->dw.Z.X[2] + log->dw.Z.R[2] * log->dw.Z.R[2];

//		log->dw.Z.E[2] = (Iorta*Iorta) + (Iortb*Iortb);

		//wyliczenie K * 3Io dla pozostalych stref
		KIoorta = *log->sI0_orta * log->nast_.ReKr -
				*log->sI0_ortb * log->nast_.ImKr;
		KIoortb = *log->sI0_orta * log->nast_.ImKr +
				*log->sI0_ortb * log->nast_.ReKr;

		//petla zwarciowa L1-E pozostale strefy
		// I = I1 -  k * 3 Io
		Iorta = *log->I1_orta + KIoorta;
		Iortb = *log->I1_ortb + KIoortb;
		I = _rcpsp(Iorta * Iorta + Iortb * Iortb);
		// Z = U / (I1 - k * 3Io)
		log->dw.Z.R[3] = (*log->U1_orta * Iorta +
				*log->U1_ortb * Iortb) * I;
		log->dw.Z.X[3] = (*log->U1_orta * Iortb -
				*log->U1_ortb * Iorta) * I;
		log->dw.Z.Z[3] = log->dw.Z.X[3] * log->dw.Z.X[3] + log->dw.Z.R[3] * log->dw.Z.R[3];

//		log->dw.Z.E[3] = (Iorta*Iorta) + (Iortb*Iortb);

		//petla zwarciowa L2-E pozostale strefy
		// I = I2 -  k * 3 Io
		Iorta = *log->I2_orta + KIoorta;
		Iortb = *log->I2_ortb + KIoortb;
		I = _rcpsp(Iorta * Iorta + Iortb * Iortb);
		// Z = U / (I2 - k * 3Io)
		log->dw.Z.R[4] = (*log->U2_orta * Iorta +
				*log->U2_ortb * Iortb) * I;
		log->dw.Z.X[4] = (*log->U2_orta * Iortb -
				*log->U2_ortb * Iorta) * I;
		log->dw.Z.Z[4] = log->dw.Z.X[4] * log->dw.Z.X[4] + log->dw.Z.R[4] * log->dw.Z.R[4];

//		log->dw.Z.E[4] = (Iorta*Iorta) + (Iortb*Iortb);

		//petla zwarciowa L3-E pozostale strefy
		// I = I3 -  k * 3 Io
		Iorta = *log->I3_orta + KIoorta;
		Iortb = *log->I3_ortb + KIoortb;
		I = _rcpsp(Iorta * Iorta + Iortb * Iortb);
		// Z = U / (I3 - k * 3Io)
		log->dw.Z.R[5] = (*log->U3_orta * Iorta +
				*log->U3_ortb * Iortb) * I;
		log->dw.Z.X[5] = (*log->U3_orta * Iortb -
				*log->U3_ortb * Iorta) * I;
		log->dw.Z.Z[5] = log->dw.Z.X[5] * log->dw.Z.X[5] + log->dw.Z.R[5] * log->dw.Z.R[5];

//		log->dw.Z.E[5] = (Iorta*Iorta) + (Iortb*Iortb);

		//petla zwarciowa L1-L2 wszystkie strefy
		// I = I1 -  I2
		Iorta = *log->I1_orta - *log->I2_orta;
		Iortb = *log->I1_ortb - *log->I2_ortb;
		I = _rcpsp(Iorta * Iorta + Iortb * Iortb);
		// Z = U / (I1 - I2)
		log->dw.Z.R[6] = (*log->U12_orta * Iorta +
				*log->U12_ortb * Iortb) * I;
		log->dw.Z.X[6] = (*log->U12_orta * Iortb -
				*log->U12_ortb * Iorta) * I;
		log->dw.Z.Z[6] = log->dw.Z.X[6] * log->dw.Z.X[6] + log->dw.Z.R[6] * log->dw.Z.R[6];

//		log->dw.Z.E[6] = (Iorta*Iorta) + (Iortb*Iortb);

		//petla zwarciowa L2-L3 wszystkie strefy
		// I = I2 -  I3
		Iorta = *log->I2_orta - *log->I3_orta;
		Iortb = *log->I2_ortb - *log->I3_ortb;
		I = _rcpsp(Iorta * Iorta + Iortb * Iortb);
		// Z = U / (I1 - I2)
		log->dw.Z.R[7] = (*log->U23_orta * Iorta +
				*log->U23_ortb * Iortb) * I;
		log->dw.Z.X[7] = (*log->U23_orta * Iortb -
				*log->U23_ortb * Iorta) * I;
		log->dw.Z.Z[7] = log->dw.Z.X[7] * log->dw.Z.X[7] + log->dw.Z.R[7] * log->dw.Z.R[7];

//		log->dw.Z.E[7] = (Iorta*Iorta) + (Iortb*Iortb);

		//petla zwarciowa L3-L1 wszystkie strefy
		// I = I3 -  I1
		Iorta = *log->I3_orta - *log->I1_orta;
		Iortb = *log->I3_ortb - *log->I1_ortb;
		I = _rcpsp(Iorta * Iorta + Iortb * Iortb);
		// Z = U / (I1 - I2)
		log->dw.Z.R[8] = (*log->U31_orta * Iorta +
				*log->U31_ortb * Iortb) * I;
		log->dw.Z.X[8] = (*log->U31_orta * Iortb -
				*log->U31_ortb * Iorta) * I;
		log->dw.Z.Z[8] = log->dw.Z.X[8] * log->dw.Z.X[8] + log->dw.Z.R[8] * log->dw.Z.R[8];

//		log->dw.Z.E[8] = (Iorta*Iorta) + (Iortb*Iortb);
		
		*log->z[0]=log->dw.Z.R[0] /log->nast_.wyjscie.przekladnia; // L1-E s 0 1
		*log->z[1]=log->dw.Z.X[0] /log->nast_.wyjscie.przekladnia; // L1-E s 0 1

		*log->z[2]=log->dw.Z.R[1] /log->nast_.wyjscie.przekladnia; // L2-E s 0 1
		*log->z[3]=log->dw.Z.X[1] /log->nast_.wyjscie.przekladnia; // L2-E s 0 1

		*log->z[4]=log->dw.Z.R[6] /log->nast_.wyjscie.przekladnia; //L1L2
		*log->z[5]=log->dw.Z.X[6] /log->nast_.wyjscie.przekladnia; //L1L2

		//--------------------------------------------------------------------------------
		//Kryterium impedancji petli miedzyfazowych: musi byc mniejsze niz 1,5 najsilnieszego zwarcia jednofaz.

		// wybor najmniejszej impedancji faza -zero
		// sa wyliczane dwie: dla stref 1 i 1W oraz dla 2,3,4,5
		float Z_L1, Z_L2, Z_L3, Z_min;

		if (log->dw.Z.Z[0] < log->dw.Z.Z[3]) // petla L1-E
			Z_L1 = log->dw.Z.Z[0];
		else
			Z_L1 = log->dw.Z.Z[3];

		if (log->dw.Z.Z[1] < log->dw.Z.Z[4]) // petla L2-E
			Z_L2 = log->dw.Z.Z[1];
		else
			Z_L2 = log->dw.Z.Z[4];

		if (log->dw.Z.Z[2] < log->dw.Z.Z[5]) // petla L3-E
			Z_L3 = log->dw.Z.Z[2];
		else
			Z_L3 = log->dw.Z.Z[5];

		u8 faza = 1;
		//szukanie najmniejszej impedancji
		if ((Z_L1 <= Z_L2) && (Z_L1 <= Z_L3))
			faza = 1;
		else if ((Z_L2 <= Z_L1) && (Z_L2 <= Z_L3))
			faza = 2;
		else
			faza = 3;

		//sprawdzenie krtyerium miedzy fazowych
		//log->dw.Z.Z[6] - L1L2
		//log->dw.Z.Z[7] - L2L3
		//log->dw.Z.Z[8] - L3L1
		//szukanie najmniejszej mfazowej
		u8 mfaza = 1;

		if ((log->dw.Z.Z[6]<log->dw.Z.Z[7])&&(log->dw.Z.Z[6]<log->dw.Z.Z[8]))
			mfaza = 1;
		else if ((log->dw.Z.Z[7]<log->dw.Z.Z[6])&&(log->dw.Z.Z[7]<log->dw.Z.Z[8]))
			mfaza = 2;
		else
			mfaza = 3;
		
		
		// kryteria sa dodane do kryteriow pradowych
		u8 zezwol_L1L2 = 1;
		u8 zezwol_L2L3 = 1;
		u8 zezwol_L1L3 = 1;

		#define kwadrat_15 2.25f // 1,5 do kwadratu
		float Z_Z1min;
		Z_Z1min=log->dw.Z_min;
//		*log->z[0]=Z_Z1min_old;
//		*log->z[1]=Z_Z1min;

		switch (faza)
		{
			case 1:
				Z_min=Z_L1<Z_Z1min?Z_Z1min:Z_L1;

				if ((Z_min * kwadrat_15) < log->dw.Z.Z[6]) // L1_L2
					zezwol_L1L2 = 0;
				if ((Z_min * kwadrat_15) < log->dw.Z.Z[8]) // L1_L3
					zezwol_L1L3 = 0;
				break;
			case 2:
				Z_min=Z_L2<Z_Z1min?Z_Z1min:Z_L2;

				if ((Z_min * kwadrat_15) < log->dw.Z.Z[6]) // L1_L2
					zezwol_L1L2 = 0;
				if ((Z_min * kwadrat_15) < log->dw.Z.Z[7]) // L2_L3
					zezwol_L2L3 = 0;
				break;
			case 3:
				Z_min=Z_L3<Z_Z1min?Z_Z1min:Z_L3;

				if ((Z_min * kwadrat_15) < log->dw.Z.Z[8]) // L1_L3
					zezwol_L1L3 = 0;
				if ((Z_min * kwadrat_15) < log->dw.Z.Z[7]) // L2_L3
					zezwol_L2L3 = 0;
				break;
			default:
				break;
		}
//		*log->z[2]=(Z_min * kwadrat_15);
	//	*log->z[3]=log->dw.Z.Z[8];
		//*log->z[4]=faza;
//		*log->z[2]=Z_Z1min;
	//	*log->z[3]=Z_L1;

		u8 zezwol_mL1L2 = 1;
		u8 zezwol_mL2L3 = 1;
		u8 zezwol_mL1L3 = 1;
		float Zmf_Z1min;
		Zmf_Z1min=log->dw.Z_min_mf;
//		*log->z[2]=Zmf_Z1min_old;
	//	*log->z[3]=Zmf_Z1min;

		if (ZDISTA_POPRAWKA_5)
		{
			switch (mfaza)
			{
				case 1:
					Z_min=log->dw.Z.Z[6]<Zmf_Z1min?Zmf_Z1min:log->dw.Z.Z[6];

					if ((Z_min * kwadrat_15) <log->dw.Z.Z[7]) //L1L2*1.5 < L2L3
						zezwol_mL2L3 = 0;
					if ((Z_min * kwadrat_15) <log->dw.Z.Z[8]) //L1L2*1.5 < L3L1
						zezwol_mL1L3 = 0;
					break;
				case 2:
					Z_min=log->dw.Z.Z[7]<Zmf_Z1min?Zmf_Z1min:log->dw.Z.Z[7];

					if ((Z_min * kwadrat_15) <log->dw.Z.Z[6]) //L2L3*1.5 < L1L2
						zezwol_mL1L2 = 0;
					if ((Z_min * kwadrat_15) <log->dw.Z.Z[8]) //L2L3*1.5 < L3L1
						zezwol_mL1L3 = 0;
					break;
				case 3:
					Z_min=log->dw.Z.Z[8]<Zmf_Z1min?Zmf_Z1min:log->dw.Z.Z[8];

					if ((Z_min * kwadrat_15) <log->dw.Z.Z[6]) //L3L1*1.5 < L1L2
						zezwol_mL1L2 = 0;
					if ((Z_min * kwadrat_15) <log->dw.Z.Z[7]) //L3L1*1.5 < L2L3
						zezwol_mL2L3 = 0;
					break;
				default:
					break;
			}
    	}

		u8 zezwol_L1 = 1;
		u8 zezwol_L2 = 1;
		u8 zezwol_L3 = 1;

		if (ZDISTA_POPRAWKA_6)
		switch (mfaza)
		{
			case 1:
				Z_min=log->dw.Z.Z[6]<Z_Z1min?Z_min:log->dw.Z.Z[6];

				if ((Z_min * kwadrat_15) <Z_L1) //L1L2*1.5 < L1
					zezwol_L1 = 0;
				if ((Z_min * kwadrat_15) <Z_L2) //L1L2*1.5 < L2
					zezwol_L2 = 0;
				break;
			case 2:
				Z_min=log->dw.Z.Z[7]<Z_Z1min?Z_min:log->dw.Z.Z[7];

				if ((Z_min * kwadrat_15) <Z_L2) //L2L3*1.5 < L2
					zezwol_L2 = 0;
				if ((Z_min * kwadrat_15) <Z_L3) //L2L3*1.5 < L3
					zezwol_L3 = 0;
				break;
			case 3:
				Z_min=log->dw.Z.Z[8]<Z_Z1min?Z_Z1min:log->dw.Z.Z[8];

				if ((Z_min * kwadrat_15) <Z_L1) //L3L1*1.5 < L1
					zezwol_L1 = 0;
				if ((Z_min * kwadrat_15) <Z_L3) //L3L1*1.5 < L3
					zezwol_L3 = 0;
				break;
			default:
				break;
		}

		float k0,k2,kdiff,typ_zw=0,near_end=0;
#ifdef KRYT_ENERGII
		if(ZDISTA_POPRAWKA_7) // kryterium KJ, uwzgledniajace Energie, impedancje i K0,K2
		{
			float Emax,Esecond,Eratio;
			u8 Emax_idx, Esecond_idx;
			float Zmin,Zsecond,Zratio;
			u8 Zmin_idx, Zsecond_idx;

			float sort_tbl_z[6] = { Z_L1,Z_L2,Z_L3,log->dw.Z.Z[6],log->dw.Z.Z[7],log->dw.Z.Z[8] }; // Impedancje
			u8 sort_num_z[6] = { 0, 1, 2, 3, 4, 5 };

			float sort_tbl_e[6] = {log->dw.Z.E[0]>log->dw.Z.E[3]?log->dw.Z.E[0]:log->dw.Z.E[3],
								 log->dw.Z.E[1]>log->dw.Z.E[4]?log->dw.Z.E[1]:log->dw.Z.E[4],
							     log->dw.Z.E[2]>log->dw.Z.E[5]?log->dw.Z.E[2]:log->dw.Z.E[5],
							    		 log->dw.Z.E[6],log->dw.Z.E[7],log->dw.Z.E[8]};

			u8 sort_num_e[6] = { 0, 1, 2, 3, 4, 5 };


			for(i = 0; i < 5; i++)
			{
				for(j = 0; j < 5 - i; j++)
				{
					if(sort_tbl_z[j] > sort_tbl_z[j+1])
					{
						float tmp = sort_tbl_z[j];
						u8 tmp2 = sort_num_z[j];

						sort_num_z[j] = sort_num_z[j+1];
						sort_num_z[j+1] = tmp2;
			            sort_tbl_z[j] = sort_tbl_z[j+1];
			            sort_tbl_z[j+1] = tmp;
			        }

					if(sort_tbl_e[j] > sort_tbl_e[j+1])
					{
						float tmp = sort_tbl_e[j];
						u8 tmp2 = sort_num_e[j];

						sort_num_e[j] = sort_num_e[j+1];
						sort_num_e[j+1] = tmp2;
			            sort_tbl_e[j] = sort_tbl_e[j+1];
			            sort_tbl_e[j+1] = tmp;
			        }

				}
		    }

			Zmin = sort_tbl_z[0];
			Zmin_idx = sort_num_z[0];
			Zsecond = sort_tbl_z[1];
			Zsecond_idx = sort_num_z[1];

			if((Zmin * _rcpsp(Z_Z1_2)) < 0.5f)
				near_end=1;

			Emax = sort_tbl_e[5];
			Emax_idx = sort_num_e[5];
			Esecond = sort_tbl_e[4];
			Esecond_idx = sort_num_e[4];

			Zratio = _rcpsp(Zsecond) * Zmin;
			Eratio = _rcpsp(Esecond) * Emax;

			if(*log->sI1<0.00001)
				*log->sI1=0.00001f;
			if(*log->sI2<0.00001)
				*log->sI2=0.00001f;

			k2 = _rcpsp(*log->sI1);
			k0 = *log->sI0 * k2; // |I0|/|I1|
			k2 *= *log->sI2;	 // |I2|/|I1|
			kdiff = *log->sI0 * _rcpsp(*log->sI2); // |I0|/|I2|

//#define K0_MIN = 0.08;
//#define K2_MIN = 0.12;
//#define K_SMALL = 0.03;
//energia dla wlasciwej petli >= 0.7...0.9 maksymalnej? Kluczowa w near-end, mniej znaczaca w far-end
//mozna dodatkowo sprobowac porownania kata impedancji? max 40..60stopni roznicy?
//wykryć near-end można przez Z<0.15 Zlinii oraz I1>5...10*Inom


			if(k0>(0.2f*0.2f) && /*k2<0.12 dodać? &&*/ Eratio>2.0f && (Zratio < 0.25f/* || near_end*/)) // 1ph-G... moznaby blokować Pob_G gdy Zratio duży?
			{
				typ_zw=1; // blokowac miedzyfazowe?
		//		zezwol_L1L2=0;
		//		zezwol_L2L3=0;
		//		zezwol_L1L3=0;
			}
			else if(k0<0.05f && k2>0.2f && Eratio>1.85f) // L-L [brak I0 za to pojawia się I2]
			{
				typ_zw=2; // blokowac fazowe?
				zezwol_L1=0;
				zezwol_L2=0;
				zezwol_L3=0;
			}
			else if(k0>0.05f && k2>0.2f &&  (Eratio>0.9f && Eratio<1.1f)) // LLG kilka petli o podobnej Energii
			{
				zezwol_L1=0;
				zezwol_L2=0;
				zezwol_L3=0;
				typ_zw=3;
			}
			else if(k0<0.02 && k2<0.02/* && (Eratio>0.9f && Eratio<1.1f)*/) // LLL
			{
				typ_zw=4; // blokowac fazowe?
			}
			else
				typ_zw=0;

			*log->z[0]=k0;
			*log->z[1]=k2;
			*log->z[2]=Zratio;
			*log->z[3]=Eratio;
			*log->z[4]=typ_zw;
			//*log->z[5]=near_end;
		}
#endif
		// kryterium kata I2 U2
		float phi1,phi2,phi;

		if(ZDISTA_POPRAWKA_3||ZDISTA_POPRAWKA_4)  // poprawka Michala, a Radka bierze z niej kierunek
		{

			if( (*log->I1 < (0.1f*0.1f) && *log->U1 < (0.25f * 0.25f))
			||(*log->I2 < (0.1f*0.1f) && *log->U2 < (0.25f * 0.25f))
			||(*log->I3 < (0.1f*0.1f) && *log->U3 < (0.25f * 0.25f)) )
				blokada_od_spz=2;
			else
				if(blokada_od_spz)
					blokada_od_spz--;

			if(*log->sI2>(0.2f*0.2f) && *log->sU2>(0.02f*0.02f) && !blokada_od_spz) //przed zmiana 09.02
			//if(*log->sI2>(0.1f*0.1f) && *log->sU2>(0.02f*0.02f))
			{
				//sprawdzenie kata I2 U2
				phi1=get_phase(*log->sI2_ortb,*log->sI2_orta);
				phi2=get_phase(*log->sU2_ortb,*log->sU2_orta);

				phi=get_phase_diff(phi1,phi2);
//				if(phi>5.0f && phi<175.0f) Przed zmianami 09.02.2026
				if(phi>45.0f && phi<135.0f)
					blok_do_szyn=wydl_po_wyl?7:2;
				else
					if(blok_do_szyn)
						blok_do_szyn--;

//				if(phi<-5.0f && phi>-175.0f) Przed zmianami 09.02.2026
				if(phi<-45.0f && phi>-135.0f)
					blok_do_linii=wydl_po_wyl?7:2;
				else
					if(blok_do_linii)
						blok_do_linii--;
			}
			else
			{
				if(blok_do_linii)
					blok_do_linii--;
				if(blok_do_szyn)
					blok_do_szyn--;
			}
		}





		u8 blokady_michal=0;
		blokady_michal|=blok_do_szyn?1:0;
		blokady_michal|=blok_do_linii?2:0;
		blokady_michal|=blokada_od_spz?3:0;
		//*log->z[0]=blok_szpilki;
	/*	*log->z[1]=*log->U1;
		*log->z[2]=*log->sI2;
		*log->z[3]=*log->sU2;
*/

		u8 zezwol_RL1L2 = 1;
		u8 zezwol_RL2L3 = 1;
		u8 zezwol_RL1L3 = 1;

		if(ZDISTA_POPRAWKA_4)
		{
		//----------------------------------------------------------------------------------
		        // Kryterium Radka

		        u8 zezwol_Radka = 0;
		        float I0_I1;
		        float fiI0_I2;


		        #define KRYT_I0_I1  	0x01
		        #define KRYT_U0        	0x02
		        #define KRYT_katI0I2    0x04
		        #define KRYT_zakrI0I2   0x08

		        // 1. |I0/I1|>0.2
		        if ((*log->sI1 > 0.0005f) )
		            {
		                //I0/I1
		                I0_I1 = *log->sI0 * _rcpsp(*log->sI1);
						if(I0_I1>0.04f)
							zezwol_Radka|=KRYT_I0_I1;
		            }

		        // Kryterium Radka
		        // 2. |U0|>U0min
		        if((*log->sU0 > log->nast_.Uomin))
		        	zezwol_Radka|=KRYT_U0;

	        
				float phi1,phi2;
				// Kryterium Radka
		        // 4. Kąt (I0-I2) |fiI0_I2|
				phi1=get_phase(*log->sI0_ortb,*log->sI0_orta);
				phi2=get_phase(*log->sI2_ortb,*log->sI2_orta);
				fiI0_I2=get_phase_diff(phi1,phi2);
				
				phi1=fiI0_I2;

		        // sprawdzenie ktora faza L ma zwarcie
		        switch (faza)
		        {
					case 1:
						break;
					case 2:
						fiI0_I2 = fiI0_I2 - 120;

						break;
					case 3:
						fiI0_I2 = fiI0_I2 + 120;

						break;
					default:
						break;
		        }

		        float zakr_I0_I2;
		        float A,B;

		        if((fiI0_I2 < 30) && (fiI0_I2 > -30))
		        	zezwol_Radka|=KRYT_katI0I2;
		        //sprawdzanie zakresu |I0/I2|
		        if ((*log->sI2 > 0.0005f) )
		            zakr_I0_I2 = *log->sI0 * _rcpsp(*log->sI2);

		        //if(Z_Z1<0.9f)
				//jak sprawdzić że impedancja Z_L1/2/3 jest w przód lub w tył
				///???

					if(1/*blok_do_szyn*/)//zwarcie w przod
					{
						//ponizej chyba nie pasuje do wzoru Radka ze wzgledu na czesc wartosci w kwadracie
			        	//A=1.0f/*25f*/ - ((1-KI0_I2)/Z_Z1) * sqrtf((faza==1?Z_L1:faza==2?Z_L2:Z_L3)/(log->nast_.wyjscie.przekladnia*log->nast_.wyjscie.przekladnia)); //f(Zw)
						//if (A>0.9f)
						//	A=0.9f;
						A = KI0_I2;
						//ponizej chyba nie pasuje do wzoru Radka ze wzgledu na czesc wartosci w kwadracie
						if((zakr_I0_I2>(A * A)) && (zakr_I0_I2<(2.5f * 2.5f)))
							zezwol_Radka|=KRYT_zakrI0I2;
					}
					else if(0/*blok_do_linii*/)//zwarcie w tyl
					{
						//ponizej chyba nie pasuje do wzoru Radka ze wzgledu na czesc wartosci w kwadracie
			        	//B=1.0f/*25f*/ - ((1-KI0_I2)/Z_Z1) * sqrtf((faza==1?Z_L1:faza==2?Z_L2:Z_L3)/(log->nast_.wyjscie.przekladnia*log->nast_.wyjscie.przekladnia)) *(0.4f); //f(Zwt)
						//if (KI0_I2>B)
						//	B=KI0_I2;
						//if(B>1.1f)
						//	B=1.1f;
						B = KI0_I2;

				        //w tyl
						//ponizej chyba nie pasuje do wzoru Radka ze wzgledu na czesc wartosci w kwadracie
				        if((zakr_I0_I2>(B * B)) && (zakr_I0_I2<(1.1f * 1.1f)))
				        	zezwol_Radka|=KRYT_zakrI0I2;

					}

		        if (zezwol_Radka==(KRYT_I0_I1|KRYT_U0|KRYT_katI0I2|KRYT_zakrI0I2))
		            {
		            //ZWARCIE_1F_ZABLOKUJ_2F; 
					//Blokuj i
		                zezwol_RL1L2 =0;
		                zezwol_RL2L3 =0;
		                zezwol_RL1L3 =0;
		            }
		        //--------------------------------------------------------------------------------
				//*log->z[0]=zezwol_Radka;
				//*log->z[1]=I0_I1;
				//*log->z[2]=fiI0_I2;
				//*log->z[3]=KI0_I2;
				//*log->z[4]=A;
    	}
//Kryterium Radka B
//Blokada fałszywych zwarć doziemnych podczas poprawnego zwarcia doziemnego
/*		float fiI0_I2;
		if((ZDISTA_POPRAWKA_4)&&(ZDISTA_POPRAWKA_5))
		{
				float phi3,phi4;
				// Kryterium Radka
		        // 4. Kąt (I0-I2) |fiI0_I2|
				phi3=get_phase(*log->sI0_ortb,*log->sI0_orta);
				phi4=get_phase(*log->sI2_ortb,*log->sI2_orta);
				fiI0_I2=get_phase_diff(phi3,phi4);
				//sprzwdzanie BRAK aktywnych pętli zwarć międzyfazowych LL
				//????????????
				//czy tylko w 1 strefie, czy we wszystkich trzeba sprawdzić zwarcia LL
				
				
				
				//sprawdzenie fi
				// dla każdej fazy
					if (((fabs(fiI0_I2)>30) && (fabs(fiI0_I2-120)<25)) ||(fabs(fiI0_I2+120)<25))
							zezwol_L1 =0;
					if (((fabs(fiI0_I2-120)>30) && (fabs(fiI0_I2)<25)) ||(fabs(fiI0_I2+120)<25))
							zezwol_L2 =0;
					if (((fabs(fiI0_I2+120)>30) && (fabs(fiI0_I2)<25)) ||(fabs(fiI0_I2-120)<25))
							zezwol_L3 =0;
				
		}
*/
	       u8 zezwol_RCL1 = 1;
	       u8 zezwol_RCL2 = 1;
	       u8 zezwol_RCL3 = 1;


	        if(ZDISTA_POPRAWKA_4C)
	        {
	        //----------------------------------------------------------------------------------
	                //Blokada fałszywych zwarć doziemnych podczas zwarcia międzyfazowego
	                // 1. |I0/I2|<0.25
	        		float kdiff;
	        		kdiff = *log->sI0 * _rcpsp(*log->sI2); // |I0|/|I2|
	        		//*log->z[5]=sqrtf(kdiff);

	                if ((*log->sI0)<(0.25f*0.25f * *log->sI2))
	                        {
	                        //2. |U2|/|U0| > 8
	                            if ((*log->sU2)>(*log->sU0 * 8*8))
	                            {
	                            //3. I2>0.3
	                                if (*log->sI2 > (0.3f*0.3f))
	                                {
	                                //4.|U0|<4
	                                    if (*log->sU0 < (0.04f*0.04f))
	                                    {
	                                    //5. 3I0<3I0nast *1.2
	                                        if (*log->sI0 < (log->nast_.Iogr * 1.2f *1.2f))
	                                        {
	                                            //blokada pobudzen zwarc doziemnych
	                                            zezwol_RCL1 = 0;
	                                            zezwol_RCL2 = 0;
	                                            zezwol_RCL3 = 0;
	                                        }

	                                    }
	                                }

	                            }
	                        }
	        }

		//--------------------------------------------------------------------------------

		//Sprawdzenie wielkosci pradow fazowych

		if (log->dw.I1 > log->nast_.Igr) log->dw.Igr[0] = 1;
		if (log->dw.I1 < log->nast_.Igrp) log->dw.Igr[0] = 0;

		if (log->dw.I2 > log->nast_.Igr) log->dw.Igr[1] = 1;
		if (log->dw.I2 < log->nast_.Igrp) log->dw.Igr[1] = 0;

		if (log->dw.I3 > log->nast_.Igr) log->dw.Igr[2] = 1;
		if (log->dw.I3 < log->nast_.Igrp) log->dw.Igr[2] = 0;

		//do kryteriow pradowych dodane jest kryterium impedancji ponizej 1,5 najmniejszej
		log->dw.Igr[3] = log->dw.Igr[0] && log->dw.Igr[1] && (!ZDISTA_POPRAWKA_1 || zezwol_L1L2) && (!ZDISTA_POPRAWKA_7 || zezwol_L1L2) && (!ZDISTA_POPRAWKA_5 || zezwol_mL1L2) &&(!ZDISTA_POPRAWKA_4|| zezwol_RL1L2);
		log->dw.Igr[4] = log->dw.Igr[1] && log->dw.Igr[2] && (!ZDISTA_POPRAWKA_1 || zezwol_L2L3) && (!ZDISTA_POPRAWKA_7 || zezwol_L2L3) && (!ZDISTA_POPRAWKA_5 || zezwol_mL2L3) &&(!ZDISTA_POPRAWKA_4|| zezwol_RL2L3);
		log->dw.Igr[5] = log->dw.Igr[2] && log->dw.Igr[0] && (!ZDISTA_POPRAWKA_1 || zezwol_L1L3) && (!ZDISTA_POPRAWKA_7 || zezwol_L1L3) && (!ZDISTA_POPRAWKA_5 || zezwol_mL1L3) &&(!ZDISTA_POPRAWKA_4|| zezwol_RL1L3);

		//do kryteriow pradowych dodane jest kryterium impedancji mfazowej ponizej 1,5 najmniejszej
		log->dw.Igr[0] = log->dw.Igr[0] && (!ZDISTA_POPRAWKA_6 || zezwol_L1) && (!ZDISTA_POPRAWKA_7 || zezwol_L1) && (!ZDISTA_POPRAWKA_4C || zezwol_RCL1);
		log->dw.Igr[1] = log->dw.Igr[1] && (!ZDISTA_POPRAWKA_6 || zezwol_L2) && (!ZDISTA_POPRAWKA_7 || zezwol_L2) && (!ZDISTA_POPRAWKA_4C || zezwol_RCL2);
		log->dw.Igr[2] = log->dw.Igr[2] && (!ZDISTA_POPRAWKA_6 || zezwol_L3) && (!ZDISTA_POPRAWKA_7 || zezwol_L3) && (!ZDISTA_POPRAWKA_4C || zezwol_RCL3);

		//dodatkowe kryterium blokady po pojawieniu sie pradu
//		if ((log->dw.I1 < 0.1) && (log->dw.I2 < 0.1) && (log->dw.I3  < 0.1))
		if ((log->dw.I1 < 0.01) && (log->dw.I2 < 0.01) && (log->dw.I3  < 0.01))
		{	
			log->l_nieustalony = 5;
		}
		else
		{
			if (log->l_nieustalony > 0)
				log->l_nieustalony--;
		}

		//identyfikacja zwarc z udzialem ziemi
		local = log->nast_.Iogr;
		localp = local * log->nast_.kpk;
		wk1p = ((*log->sI0 > local)
			&& (*log->sU0 > log->nast_.Uomin))?1:0;

		wk2p = ((*log->sI0 < localp)
			|| (*log->sU0 < log->nast_.Uomin))?1:0;



		if (log->dw.I1 > log->dw.I2) local = log->dw.I1;
					else local = log->dw.I2;
		if (log->dw.I3 > local) local = log->dw.I3;


		local = local * log->nast_.khio;
		localp = local * log->nast_.kpk;


		wk1 = (wk1p &&
			(*log->sI0 > local))?1:0;
		wk2 = (wk2p ||
			(*log->sI0 < localp))?1:0;


		sprawdz_P(
			&log->dw.Iogr,
			wk1,
			wk2,
			&log->dw.liczpIo,
			3,3
			);


		// impedancyjne kryterium kierunkowe i sprawdzenie obszaru obciazenia

		local = log->nast_.XKR;
		localp = local * log->nast_.kp;
		local0 = log->nast_.XKX;
		local0p = local0 * log->nast_.kpp;



		local1 = log->nast_.RLf * log->nast_.wyjscie.przekladnia;
		local1p = local1 * log->nast_.kpp_obc;
		local2 = log->nast_.RLr * log->nast_.wyjscie.przekladnia;
		local2p = local2 * log->nast_.kpp_obc;
		local3 = log->nast_.KL;
		local3p = local3 * log->nast_.kp_obc;

		for(i=0;i<9;i++)
		{

			wk1 = log->dw.Z.X[i] > local * log->dw.Z.R[i];
			wk1p = log->dw.Z.X[i] < localp * log->dw.Z.R[i];
			wk2 = log->dw.Z.X[i] > local0 * log->dw.Z.R[i];
			wk2p = log->dw.Z.X[i] < local0p * log->dw.Z.R[i];

			for (j = 0; j < 6; j++)
			{
				int k = 0;
				if (i < 3 )
					k = i;
				else
					k = i - 3;
			
			sprawdz_P( //kierunek do przodu
				&log->dw.XKp[j][i],
				wk1 && wk2 && log->dw.P_sss[j][k],
				wk1p || wk2p,
				&log->dw.liczp11[j][i],
				7+wydl_po_wyl,
				3
				);


			sprawdz_P( //kierunek do tylu
				&log->dw.XKm[j][i],
				!wk1 && !wk2 && log->dw.P_sss[j][k],
				!wk1p || !wk2p,
				&log->dw.liczp12[j][i],
				7+wydl_po_wyl,
				3
				);
			}

			sprawdz_P( // blokada od pradow obciażenia
				&log->dw.BL_Load[i],
				(log->dw.Z.R[i] > local1 || log->dw.Z.R[i] < local2) &&
				fabs(log->dw.Z.X[i]) <  fabs(local3 * log->dw.Z.R[i]),
				(log->dw.Z.R[i] < local1p && log->dw.Z.R[i] > local2p) ||
				fabs(log->dw.Z.X[i]) >  fabs(local3p * log->dw.Z.R[i]),
				&log->dw.liczp10[i],
				filtr_P
				);


		}


		//--------------------------------------------------------------------------------
		// identyfikacja bardzo bliskich zwarc
		if (log->nast_.z6_kolo != 0)
		{	
			log->dw.Zm[0] = log->dw.Z.Z[0] < log->nast_.Zgr || log->dw.U1 < (0.01f*0.01f) ; // przed 09.02.2026 bylo 0.005f
			log->dw.Zm[1] = log->dw.Z.Z[1] < log->nast_.Zgr || log->dw.U2 < (0.01f*0.01f) ;
			log->dw.Zm[2] = log->dw.Z.Z[2] < log->nast_.Zgr || log->dw.U3 < (0.01f*0.01f) ;
			log->dw.Zm[3] = log->dw.Z.Z[3] < log->nast_.Zgr || log->dw.U1 < (0.01f*0.01f) ;
			log->dw.Zm[4] = log->dw.Z.Z[4] < log->nast_.Zgr || log->dw.U2 < (0.01f*0.01f) ;
			log->dw.Zm[5] = log->dw.Z.Z[5] < log->nast_.Zgr || log->dw.U3 < (0.01f*0.01f) ;
			log->dw.Zm[6] = log->dw.Z.Z[6] < log->nast_.Zgr || *log->U12 < (0.01f*0.01f) ;
			log->dw.Zm[7] = log->dw.Z.Z[7] < log->nast_.Zgr || *log->U23 < (0.01f*0.01f) ;
			log->dw.Zm[8] = log->dw.Z.Z[8] < log->nast_.Zgr || *log->U31 < (0.01f*0.01f) ;

		}
		else
		{
			log->dw.Zm[0] = 0;
			log->dw.Zm[1] = 0;
			log->dw.Zm[2] = 0;
			log->dw.Zm[3] = 0;
			log->dw.Zm[4] = 0;
			log->dw.Zm[5] = 0;
			log->dw.Zm[6] = 0;
			log->dw.Zm[7] = 0;
			log->dw.Zm[8] = 0;
			
		}

		//--------------------------------------------------------------------------------
		//Wyliczenie wartosci rozruchowych dla charakterystyki poligonalnej

		i = 0;
		while(i<3)
		{
				log->dw.Z.absR[i] = fabs(log->dw.Z.R[i]);
				log->dw.Z.absX[i] = fabs(log->dw.Z.X[i]);
				log->dw.Z.XRtanfi1_noabs[i] = log->dw.Z.X[i] - log->dw.Z.R[i] * log->nast_.tanfi1;
			//	log->dw.Z.XRtanfi1[i] = fabs(log->dw.Z.XRtanfi1_noabs[i]);
				log->dw.Z.XRtanfi2[i] = log->dw.Z.X[i] + log->dw.Z.R[i] * log->nast_.tanfi2;
				i++;
		}

		while(i<9)
		{
				log->dw.Z.absR[i] = fabs(log->dw.Z.R[i]);
				log->dw.Z.absX[i] = fabs(log->dw.Z.X[i]);
				log->dw.Z.XRtanfi1_noabs[i] = log->dw.Z.X[i] - log->dw.Z.R[i] * log->nast_.tanfi1;
			//	log->dw.Z.XRtanfi1[i] = fabs(log->dw.Z.XRtanfi1_noabs[i]);
				i++;
		}


		//-----------------------------------------------------------------------------
		// sprawdzenie spelnienia warunku impedancji strefy
		//-----------------------------------------------------------------------------
		// sprawdzenie strefy 1 zwarcia jednofazowe z ziemia strefa 1W
		int l_powt = 5;
		if (log->nast_.kierunek[0] == 0)
			l_powt = 10 + wydl_po_wyl;
		else
			l_powt = 5 + wydl_po_wyl;
		
		if(log->nast_.wyjscie.SOTF_zwrotnie == 0)
			l_powt = 5 + wydl_po_wyl;

		
		for(i=0;i<3;i++)
		{
			if (log->nast_.typ[0])
			{
				Iorta = log->dw.Z.R[i] - log->nast_.n_pol[0][0].Rr;
				Iortb = log->dw.Z.X[i] - log->nast_.n_pol[0][0].Xr;
				I = Iorta * Iorta + Iortb * Iortb;
				wk1 = I < log->nast_.n_pol[0][0].Z && log->dw.Igr[i];
				Iorta = log->dw.Z.R[i] - log->nast_.n_pol[0][0].Rp;
				Iortb = log->dw.Z.X[i] - log->nast_.n_pol[0][0].Xp;
				I = Iorta * Iorta + Iortb * Iortb;
				wk2 = I > log->nast_.n_pol[0][0].Zp || !log->dw.Igr[i];
			} else {
				wk1 = (/*log->dw.Z.absR[i] < log->nast_.n_pol[0][0].Rr ||*/
					   ((log->dw.Z.X[i]>=0.0f)?((log->dw.Z.XRtanfi1_noabs[i] > -log->nast_.n_pol[0][0].Rrtanfi1) && (log->dw.Z.R[i] > - log->nast_.n_pol[0][0].Rr)):((log->dw.Z.XRtanfi1_noabs[i] < log->nast_.n_pol[0][0].Rrtanfi1)&&(log->dw.Z.R[i] < log->nast_.n_pol[0][0].Rr)))        ) //here
						&& log->dw.Z.XRtanfi2[i] < log->nast_.Xr1Wf
						&& log->dw.Igr[i]
						&& log->dw.Z.absX[i] < log->nast_.n_pol[0][0].Xr
						&& log->l_nieustalony == 0;

				wk2 = (
						((log->dw.Z.X[i]>=0.0f)?((log->dw.Z.XRtanfi1_noabs[i] < -log->nast_.n_pol[0][0].Rptanfi1) || (log->dw.Z.R[i] < - log->nast_.n_pol[0][0].Rp)):((log->dw.Z.XRtanfi1_noabs[i] > log->nast_.n_pol[0][0].Rptanfi1)||(log->dw.Z.R[i] > log->nast_.n_pol[0][0].Rp)))
						/*&& log->dw.Z.absR[i] > log->nast_.n_pol[0][0].Rp*/)
						|| !log->dw.Igr[i]
						|| log->dw.Z.XRtanfi2[i] > log->nast_.Xr1Wfp
						|| log->dw.Z.absX[i] > log->nast_.n_pol[0][0].Xp
						|| log->l_nieustalony != 0;
			}
			sprawdz_P(
				&log->dw.P_s[0][i],
				wk1,
				wk2,
				&log->dw.liczps[0][i],
				l_powt, 5
				);
			log->dw.Pbk[0][i] = log->dw.P_s[0][i] && log->dw.Iogr && !(log->nast_.Bl_L[0] && log->dw.BL_Load[i]); // uwzglednienie blokady od pradow obciażenia oraz kryterium zwarc z udzialem ziemi
			log->dw.P_sss[0][i]= wk1;
		}

		// sprawdzenie strefy 1 zwarcia jednofazowe z ziemia strefa 1
		if (log->nast_.kierunek[1] == 0)
			l_powt = 10 + wydl_po_wyl;
		else
			l_powt = 5 + wydl_po_wyl;
	
		if (log->nast_.wyjscie.SOTF_zwrotnie == 1)
			l_powt = 5 + wydl_po_wyl;
			
		for(i=0;i<3;i++)
		{
			if (log->nast_.typ[1])
			{
				Iorta = log->dw.Z.R[i] - log->nast_.n_pol[1][0].Rr;
				Iortb = log->dw.Z.X[i] - log->nast_.n_pol[1][0].Xr;
				I = Iorta * Iorta + Iortb * Iortb;
				wk1 = I < log->nast_.n_pol[1][0].Z && log->dw.Igr[i];
				Iorta = log->dw.Z.R[i] - log->nast_.n_pol[1][0].Rp;
				Iortb = log->dw.Z.X[i] - log->nast_.n_pol[1][0].Xp;
				I = Iorta * Iorta + Iortb * Iortb;
				wk2 = I > log->nast_.n_pol[1][0].Zp || !log->dw.Igr[i];
			} else {
				wk1 =   (((log->dw.Z.X[i]>=0.0f)?((log->dw.Z.XRtanfi1_noabs[i] > -log->nast_.n_pol[1][0].Rrtanfi1) && (log->dw.Z.R[i] > - log->nast_.n_pol[1][0].Rr)):((log->dw.Z.XRtanfi1_noabs[i] < log->nast_.n_pol[1][0].Rrtanfi1)&&(log->dw.Z.R[i] < log->nast_.n_pol[1][0].Rr)))        )

//						(log->dw.Z.absR[i] < log->nast_.n_pol[1][0].Rr || log->dw.Z.XRtanfi1[i] < log->nast_.n_pol[1][0].Rrtanfi1)
						&& log->dw.Z.XRtanfi2[i] < log->nast_.Xr1f
						&& log->dw.Igr[i]
						&& log->dw.Z.absX[i] < log->nast_.n_pol[1][0].Xr
						&& log->l_nieustalony == 0;
				wk2 =	((log->dw.Z.X[i]>=0.0f)?((log->dw.Z.XRtanfi1_noabs[i] < -log->nast_.n_pol[1][0].Rptanfi1) || (log->dw.Z.R[i] < - log->nast_.n_pol[1][0].Rp)):((log->dw.Z.XRtanfi1_noabs[i] > log->nast_.n_pol[1][0].Rptanfi1)||(log->dw.Z.R[i] > log->nast_.n_pol[1][0].Rp)))

						//(log->dw.Z.absR[i] > log->nast_.n_pol[1][0].Rp && log->dw.Z.XRtanfi1[i] > log->nast_.n_pol[1][0].Rptanfi1)
						|| log->dw.Z.XRtanfi2[i] > log->nast_.Xr1fp
						|| !log->dw.Igr[i]
						|| log->dw.Z.absX[i] > log->nast_.n_pol[1][0].Xp
						|| log->l_nieustalony != 0;
			}
			sprawdz_P(
				&log->dw.P_s[1][i],
				wk1,
				wk2,
				&log->dw.liczps[1][i],
				l_powt, 5
				);
			log->dw.Pbk[1][i] = log->dw.P_s[1][i] && log->dw.Iogr && !(log->nast_.Bl_L[1] && log->dw.BL_Load[i]); // uwzglednienie blokady od pradow obciażenia oraz kryterium zwarc z udzialem ziemi
			log->dw.P_sss[1][i]= wk1;
		}


		//sprawdzenie pozostalych stref zwarcie jednofazowe z ziemia
		for(j=2;j<6;j++)
		{
			if (log->nast_.kierunek[j] == 0)
				l_powt = 10 + wydl_po_wyl;
			else
				l_powt = 5 + wydl_po_wyl;
			
			if  (log->nast_.wyjscie.SOTF_zwrotnie == j)
				l_powt = 5 + wydl_po_wyl;

			for(i=3;i<6;i++)
			{
				if (log->nast_.typ[j])
				{
					Iorta = log->dw.Z.R[i] - log->nast_.n_pol[j][0].Rr;
					Iortb = log->dw.Z.X[i] - log->nast_.n_pol[j][0].Xr;
					I = Iorta * Iorta + Iortb * Iortb;
					wk1 = I < log->nast_.n_pol[j][0].Z  && log->dw.Igr[i-3];
					Iorta = log->dw.Z.R[i] - log->nast_.n_pol[j][0].Rp;
					Iortb = log->dw.Z.X[i] - log->nast_.n_pol[j][0].Xp;
					I = Iorta * Iorta + Iortb * Iortb;
					wk2 = I > log->nast_.n_pol[j][0].Zp || !log->dw.Igr[i-3];
				} else {
					wk1 = //(log->dw.Z.absR[i] < log->nast_.n_pol[j][0].Rr || log->dw.Z.XRtanfi1[i] < log->nast_.n_pol[j][0].Rrtanfi1)
						(((log->dw.Z.X[i]>=0.0f)?((log->dw.Z.XRtanfi1_noabs[i] > -log->nast_.n_pol[j][0].Rrtanfi1) && (log->dw.Z.R[i] > - log->nast_.n_pol[j][0].Rr)):((log->dw.Z.XRtanfi1_noabs[i] < log->nast_.n_pol[j][0].Rrtanfi1)&&(log->dw.Z.R[i] < log->nast_.n_pol[j][0].Rr)))        )

							&& log->dw.Igr[i-3]
							&& log->dw.Z.absX[i] < log->nast_.n_pol[j][0].Xr
							&& log->l_nieustalony == 0;
					wk2 =// (log->dw.Z.absR[i] > log->nast_.n_pol[j][0].Rp && log->dw.Z.XRtanfi1[i] > log->nast_.n_pol[j][0].Rptanfi1)
							((log->dw.Z.X[i]>=0.0f)?((log->dw.Z.XRtanfi1_noabs[i] < -log->nast_.n_pol[j][0].Rptanfi1) || (log->dw.Z.R[i] < - log->nast_.n_pol[j][0].Rp)):((log->dw.Z.XRtanfi1_noabs[i] > log->nast_.n_pol[j][0].Rptanfi1)||(log->dw.Z.R[i] > log->nast_.n_pol[j][0].Rp)))

							|| !log->dw.Igr[i-3]
							|| log->dw.Z.absX[i] > log->nast_.n_pol[j][0].Xp
							|| log->l_nieustalony != 0;
				}
				sprawdz_P(
					&log->dw.P_s[j][i-3],
					wk1,
					wk2,
					&log->dw.liczps[j][i-3],
					l_powt, 5
					);

				log->dw.Pbk[j][i-3] = log->dw.P_s[j][i-3] && log->dw.Iogr && !(log->nast_.Bl_L[j] && log->dw.BL_Load[i]); // uwzglednienie blokady od pradow obciażenia oraz kryterium zwarc z udzialem ziemi
				log->dw.P_sss[j][i-3]= wk1;
			}

		}

		//sprawdzenie petli miedzyfazowych
		for(j=0;j<6;j++)
		{
			if (log->nast_.kierunek[j] == 0)
				l_powt = 10 + wydl_po_wyl;
			else
				l_powt = 5 + wydl_po_wyl;
			
			if  (log->nast_.wyjscie.SOTF_zwrotnie == j)
				l_powt = 5 + wydl_po_wyl;
			
			for(i=6;i<9;i++)
			{
				if (log->nast_.typ[j])
				{
					Iorta = log->dw.Z.R[i] - log->nast_.n_pol[j][1].Rr;
					Iortb = log->dw.Z.X[i] - log->nast_.n_pol[j][1].Xr;
					I = Iorta * Iorta + Iortb * Iortb;
					wk1 = (I < log->nast_.n_pol[j][1].Z && log->dw.Igr[i-3])?1:0;
					Iorta = log->dw.Z.R[i] - log->nast_.n_pol[j][1].Rp;
					Iortb = log->dw.Z.X[i] - log->nast_.n_pol[j][1].Xp;
					I = Iorta * Iorta + Iortb * Iortb;
					wk2 = (I > log->nast_.n_pol[j][1].Zp  || !log->dw.Igr[i-3])?1:0;
				} else {
						wk1 = //(log->dw.Z.absR[i] < log->nast_.n_pol[j][1].Rr || log->dw.Z.XRtanfi1[i] < log->nast_.n_pol[j][1].Rrtanfi1)
								(  ((log->dw.Z.X[i]>=0.0f)?((log->dw.Z.XRtanfi1_noabs[i] > -log->nast_.n_pol[j][1].Rrtanfi1) && (log->dw.Z.R[i] > - log->nast_.n_pol[j][1].Rr)):((log->dw.Z.XRtanfi1_noabs[i] < log->nast_.n_pol[j][1].Rrtanfi1)&&(log->dw.Z.R[i] < log->nast_.n_pol[j][1].Rr)))        )
								&& log->dw.Igr[i-3]
								&& log->dw.Z.absX[i] < log->nast_.n_pol[j][1].Xr
								&& log->l_nieustalony == 0;
						wk2 = //(log->dw.Z.absR[i] > log->nast_.n_pol[j][1].Rp  && log->dw.Z.XRtanfi1[i] > log->nast_.n_pol[j][1].Rptanfi1)
								((log->dw.Z.X[i]>=0.0f)?((log->dw.Z.XRtanfi1_noabs[i] < -log->nast_.n_pol[j][1].Rptanfi1) || (log->dw.Z.R[i] < - log->nast_.n_pol[j][1].Rp)):((log->dw.Z.XRtanfi1_noabs[i] > log->nast_.n_pol[j][1].Rptanfi1)||(log->dw.Z.R[i] > log->nast_.n_pol[j][1].Rp)))

								|| !log->dw.Igr[i-3]
								|| log->dw.Z.absX[i] > log->nast_.n_pol[j][1].Xp
								|| log->l_nieustalony != 0;
				}
				sprawdz_P(
					&log->dw.P_s[j][i-3],
					wk1,
					wk2,
					&log->dw.liczps[j][i-3],
					l_powt, 5
					);
				log->dw.Pbk[j][i-3] = log->dw.P_s[j][i-3] && !(log->nast_.Bl_L[j] && log->dw.BL_Load[i]); // uwzglednienie blokady od pradow obciażenia oraz minimalnej wartosci pradu
				log->dw.P_sss[j][i-3]= wk1;
			}
		}


		//-----------------------------------------------------------------------------
		// sprawdzenie dodatkowego kryterium kierunkowego

//		*log->z[1]=0;
		if (*log->sU1 < (0.05*0.05f) && *log->sU2 > 0.002f) // ok 4V // dodano zeby jednak uzywac skladowej przeciwnej gdy brak zgodnej (zalaczenie linii)
		{
//*log->z[1]=1;
			// kryterium skladowych przeciwnych
			//przesuniecia skladowych napiecia o 45 stopni
			if (*log->sI2 > 0.0003f)
			{
//*log->z[1]=2;
				Uorta = (*log->sU2_orta - *log->sU2_ortb) * 0.7071f; //
				Uortb = (*log->sU2_orta + *log->sU2_ortb) * 0.7071f;
				I = *log->sI2_orta * Uorta +
					*log->sI2_ortb * Uortb;
	
				wk1 = I < 0;
				sprawdz_P (
					&log->dw.Kp,
					wk1,
					!wk1,
					&log->dw.liczKdod,
					3,3
					);
				log->dw.Km = !log->dw.Kp;
			} else {
				log->dw.Km = log->dw.Kp = 0;
			}
		} else {
			// kryterium skladowych zgodnych
			//przesuniecia skladowych napiecia o 45 stopni
//*log->z[1]=3;

			if ((*log->sI1 > 0.0001f) &&//przed poprawka 09.02.2026
			//if ((*log->sI1 > (0.001f*0.001f)) &&
					(*log->sU1 > 0.00001f) /*&& !blok_szpilki *//*&& !wydl_po_wyl*/)
			{
//*log->z[1]=4;
				Uorta = (*log->sU1_orta - *log->sU1_ortb) * 0.7071f; //
				Uortb = (*log->sU1_orta + *log->sU1_ortb) * 0.7071f;
				I = *log->sI1_orta * Uorta +
						*log->sI1_ortb * Uortb;
				wk1 = I > 0;
				sprawdz_P (
					&log->dw.Kp,
					wk1,
					!wk1,
					&log->dw.liczKdod,
					3,3
					);
				log->dw.Km = !log->dw.Kp;
			} else {
				log->dw.Km = log->dw.Kp = 0;
			}
		}

		//*log->z[0]=blok_do_szyn?65535:0;
		//*log->z[1]=phi1;
		//*log->z[2]=phi2;
		//*log->z[3]=phi;

		//-----------------------------------------------------------------------------
		//Ustalenie pobudzen stref
		//okreslenie warunkow kierunkowych
		//zwarcie jednofazowe z ziemia strefa 1 i 1W

//		u16 Zm_vect=0;
	//	for(i=0;i<9;i++)
		//	Zm_vect|=log->dw.Zm[i]?(1<<i):0;
//Zm_vect=0;
//		for(i=0;i<6;i++)
	//		if(log->dw.P[i][1] || log->dw.P[i][3] || log->dw.P[i][4])
		//		Zm_vect|=(1<<i);

//		*log->z[4]=Zm_vect;
//		Zm_vect=0;
	//			for(i=0;i<6;i++)
		//			if(log->dw.XKp[i][1] || log->dw.XKp[i][1+3] || log->dw.XKp[i][3+3] || log->dw.XKp[i][4+3])
			//			Zm_vect|=(1<<i);

	//			*log->z[5]=Zm_vect;

		if (!check_struct(&log->Bl_K))//jeśli nie ma blokady kierunku
		{
			for(j=0;j<2;j++)
			{
			
				switch(log->nast_.kierunek[j])
				{
					case 0: //bezkierunku
						for(i=0;i<3;i++)
						{
							log->dw.P[j][i] = log->dw.Pbk[j][i];
						}
						break;
					case 1: //do linii
						if(blok_szpilki!=3)
						for(i=0;i<3;i++)
						{
							if(ZDISTA_POPRAWKA_3)
								log->dw.P[j][i] = log->dw.Pbk[j][i] && (log->dw.Zm[i]?log->dw.Kp:log->dw.XKp[j][i]) && !blok_do_linii;
							else
								log->dw.P[j][i] = log->dw.Pbk[j][i] && (log->dw.Zm[i]?log->dw.Kp:log->dw.XKp[j][i]);
						}
						break;
					case 2: // do szyn
						if(blok_szpilki!=3)
						for(i=0;i<3;i++)
						{
							if(ZDISTA_POPRAWKA_3)
								log->dw.P[j][i] = log->dw.Pbk[j][i] && (log->dw.Zm[i]?log->dw.Km:log->dw.XKm[j][i]) && !blok_do_szyn;
							else
								log->dw.P[j][i] = log->dw.Pbk[j][i] && (log->dw.Zm[i]?log->dw.Km:log->dw.XKm[j][i]);
						}
						break;
				}
			}

			//zwarcie jednofazowe z ziemia - pozostale
			for(j=2;j<6;j++)
			{
				switch(log->nast_.kierunek[j])
				{
					case 0: //bezkierunku
						for(i=3;i<6;i++)
						{
							log->dw.P[j][i-3] = log->dw.Pbk[j][i-3];
						}
						break;
					case 1: //do linii
						if(blok_szpilki!=3)
						for(i=3;i<6;i++)
						{
							if(ZDISTA_POPRAWKA_3)
								log->dw.P[j][i-3] = log->dw.Pbk[j][i-3] && (log->dw.Zm[i]?log->dw.Kp:log->dw.XKp[j][i]) && !blok_do_linii;
							else
								log->dw.P[j][i-3] = log->dw.Pbk[j][i-3] && (log->dw.Zm[i]?log->dw.Kp:log->dw.XKp[j][i]);
						}
						break;
					case 2: // do szyn
						if(blok_szpilki!=3)
						for(i=3;i<6;i++)
						{
							if(ZDISTA_POPRAWKA_3)
								log->dw.P[j][i-3] = log->dw.Pbk[j][i-3] && (log->dw.Zm[i]?log->dw.Km : log->dw.XKm[j][i]) && !blok_do_szyn;
							else
								log->dw.P[j][i-3] = log->dw.Pbk[j][i-3] && (log->dw.Zm[i]?log->dw.Km : log->dw.XKm[j][i]);
						}
						break;
				}
			}

			//zwarcie miedzyfazowe
			for(j=0;j<6;j++)
			{
				switch(log->nast_.kierunek[j])
				{
					case 0: //bezkierunku
						for(i=6;i<9;i++)
						{
							log->dw.P[j][i-3] = log->dw.Pbk[j][i-3];
						}
						break;
					case 1: //do linii
						if(blok_szpilki!=3)
						for(i=6;i<9;i++)
						{
							if(ZDISTA_POPRAWKA_3)
								log->dw.P[j][i-3] = log->dw.Pbk[j][i-3] && (log->dw.Zm[i]?log->dw.Kp:log->dw.XKp[j][i]) && !blok_do_linii;
							else
								log->dw.P[j][i-3] = log->dw.Pbk[j][i-3] && (log->dw.Zm[i]?log->dw.Kp:log->dw.XKp[j][i]);
						}
						break;
					case 2: // do szyn
						if(blok_szpilki!=3)
						for(i=6;i<9;i++)
						{
							if(ZDISTA_POPRAWKA_3)
								log->dw.P[j][i-3] = log->dw.Pbk[j][i-3] && (log->dw.Zm[i]?log->dw.Km:log->dw.XKm[j][i]) && !blok_do_szyn;
							else
								log->dw.P[j][i-3] = log->dw.Pbk[j][i-3] && (log->dw.Zm[i]?log->dw.Km:log->dw.XKm[j][i]);
						}
						break;
				}
			}
		} else {
			for(j=0;j<6;j++)
			{
				for(i=6;i<9;i++)
				{
					log->dw.P[j][i-3] = log->dw.Pbk[j][i-3]; //poprawiony strzal z boku?
				}

			}
		}

		//-----------------------------------------------------------------------------


		//obsluga wejscia testu przekaznika
		if (czy_test_R())
		{
			for(i=0;i<36;i++)
			{
				*(&log->dw.P[0][0] + i) = check_struct(&log->test)?1:0;
			}
		}
        //

        // realizacja blokady dzialania przekaznika
		if((log->nast_.bl_ && check_struct(&log->stan_bl))/* | czy_start()*/)
		{
			for(i=0;i<36;i++)
			{
				*(&log->dw.P[0][0] + i)  = 0;
			}
		}
				
		check_and_set_struct(log->dw.P[0][0], &log->P1W_L1E);
		check_and_set_struct(log->dw.P[0][1], &log->P1W_L2E);
		check_and_set_struct(log->dw.P[0][2], &log->P1W_L3E);
		check_and_set_struct(log->dw.P[0][3], &log->P1W_L1L2);
		check_and_set_struct(log->dw.P[0][4], &log->P1W_L2L3);
		check_and_set_struct(log->dw.P[0][5], &log->P1W_L3L1);
		check_and_set_struct(log->dw.P[1][0], &log->P1_L1E);
		check_and_set_struct(log->dw.P[1][1], &log->P1_L2E);
		check_and_set_struct(log->dw.P[1][2], &log->P1_L3E);
		check_and_set_struct(log->dw.P[1][3], &log->P1_L1L2);
		check_and_set_struct(log->dw.P[1][4], &log->P1_L2L3);
		check_and_set_struct(log->dw.P[1][5], &log->P1_L3L1);
		check_and_set_struct(log->dw.P[2][0], &log->P2_L1E);
		check_and_set_struct(log->dw.P[2][1], &log->P2_L2E);
		check_and_set_struct(log->dw.P[2][2], &log->P2_L3E);
		check_and_set_struct(log->dw.P[2][3], &log->P2_L1L2);
		check_and_set_struct(log->dw.P[2][4], &log->P2_L2L3);
		check_and_set_struct(log->dw.P[2][5], &log->P2_L3L1);
		check_and_set_struct(log->dw.P[3][0], &log->P3_L1E);
		check_and_set_struct(log->dw.P[3][1], &log->P3_L2E);
		check_and_set_struct(log->dw.P[3][2], &log->P3_L3E);
		check_and_set_struct(log->dw.P[3][3], &log->P3_L1L2);
		check_and_set_struct(log->dw.P[3][4], &log->P3_L2L3);
		check_and_set_struct(log->dw.P[3][5], &log->P3_L3L1);
		check_and_set_struct(log->dw.P[4][0], &log->P4_L1E);
		check_and_set_struct(log->dw.P[4][1], &log->P4_L2E);
		check_and_set_struct(log->dw.P[4][2], &log->P4_L3E);
		check_and_set_struct(log->dw.P[4][3], &log->P4_L1L2);
		check_and_set_struct(log->dw.P[4][4], &log->P4_L2L3);
		check_and_set_struct(log->dw.P[4][5], &log->P4_L3L1);
		check_and_set_struct(log->dw.P[5][0], &log->P5_L1E);
		check_and_set_struct(log->dw.P[5][1], &log->P5_L2E);
		check_and_set_struct(log->dw.P[5][2], &log->P5_L3E);
		check_and_set_struct(log->dw.P[5][3], &log->P5_L1L2);
		check_and_set_struct(log->dw.P[5][4], &log->P5_L2L3);
		check_and_set_struct(log->dw.P[5][5], &log->P5_L3L1);
	
		/*debug
		check_and_set_struct(log->dw.Igr[0], &log->P5_L2E);
		check_and_set_struct(log->dw.XKp[2][0], &log->P5_L3E);
		check_and_set_struct(log->dw.Pbk[2][0], &log->P5_L1L2);	
		check_and_set_struct(log->dw.Km, &log->P5_L2L3);
		check_and_set_struct(log->dw.Kp, &log->P5_L3L1);
		*/
    }
	else
	{
		/*for(i=0;i<36;i++)
		{
			//clear_struct((struct binary_io *)((u8 *)&log->P1W_L1E+(sizeof(struct binary_io)*i)));
		}*/
		clear_struct(&log->P1W_L1E);
		clear_struct(&log->P1W_L2E);
		clear_struct(&log->P1W_L3E);
		clear_struct(&log->P1W_L1L2);
		clear_struct(&log->P1W_L2L3);
		clear_struct(&log->P1W_L3L1);
		clear_struct(&log->P1_L1E);
		clear_struct(&log->P1_L2E);
		clear_struct(&log->P1_L3E);
		clear_struct(&log->P1_L1L2);
		clear_struct(&log->P1_L2L3);
		clear_struct(&log->P1_L3L1);
		clear_struct(&log->P2_L1E);
		clear_struct(&log->P2_L2E);
		clear_struct(&log->P2_L3E);
		clear_struct(&log->P2_L1L2);
		clear_struct(&log->P2_L2L3);
		clear_struct(&log->P2_L3L1);
		clear_struct(&log->P3_L1E);
		clear_struct(&log->P3_L2E);
		clear_struct(&log->P3_L3E);
		clear_struct(&log->P3_L1L2);
		clear_struct(&log->P3_L2L3);
		clear_struct(&log->P3_L3L1);
		clear_struct(&log->P4_L1E);
		clear_struct(&log->P4_L2E);
		clear_struct(&log->P4_L3E);
		clear_struct(&log->P4_L1L2);
		clear_struct(&log->P4_L2L3);
		clear_struct(&log->P4_L3L1);
		clear_struct(&log->P5_L1E);
		clear_struct(&log->P5_L2E);
		clear_struct(&log->P5_L3E);
		clear_struct(&log->P5_L1L2);
		clear_struct(&log->P5_L2L3);
		clear_struct(&log->P5_L3L1);
	}
}
