/*
 * 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 "ZDistA.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;
};

int ZDistA_initlog(void *arguments, void *logic)
{
	struct ZDistA_args *args = (struct ZDistA_args *)arguments;
	struct ZDistA_logic *log = (struct ZDistA_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.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;

	//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_.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;

	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;

//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;

	return 0;
}

void ZDistA(void *arguments, void *logic)
{
	struct ZDistA_logic *log = (struct ZDistA_logic *)logic;
	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;

	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_)   // jesli zabezpieczenie aktywne
    {

		//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;


		//petla zwarciowa L1-E strefa pierwsza
		// I = I1 -  k * 3 Io
		Iorta = *log->I1_orta + KIoorta;
		Iortb = *log->I1_ortb + KIoortb;
		I = 1 / (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];

		//petla zwarciowa L2-E strefa pierwsza
		// I = I2 -  k * 3 Io
		Iorta = *log->I2_orta + KIoorta;
		Iortb = *log->I2_ortb + KIoortb;
		I = 1 / (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];

		//petla zwarciowa L3-E strefa pierwsza
		// I = I3 -  k * 3 Io
		Iorta = *log->I3_orta + KIoorta;
		Iortb = *log->I3_ortb + KIoortb;
		I = 1 / (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];


		//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 = 1 / (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];

		//petla zwarciowa L2-E pozostale strefy
		// I = I2 -  k * 3 Io
		Iorta = *log->I2_orta + KIoorta;
		Iortb = *log->I2_ortb + KIoortb;
		I = 1 / (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];

		//petla zwarciowa L3-E pozostale strefy
		// I = I3 -  k * 3 Io
		Iorta = *log->I3_orta + KIoorta;
		Iortb = *log->I3_ortb + KIoortb;
		I = 1 / (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];

		//petla zwarciowa L1-L2 wszystkie strefy
		// I = I1 -  I2
		Iorta = *log->I1_orta - *log->I2_orta;
		Iortb = *log->I1_ortb - *log->I2_ortb;
		I = 1 / (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->z[0]=log->dw.Z.R[6];
		*log->z[1]=log->dw.Z.X[6];
		//petla zwarciowa L2-L3 wszystkie strefy
		// I = I2 -  I3
		Iorta = *log->I2_orta - *log->I3_orta;
		Iortb = *log->I2_ortb - *log->I3_ortb;
		I = 1 / (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->z[2]=log->dw.Z.R[7];
		*log->z[3]=log->dw.Z.X[7];

		//petla zwarciowa L3-L1 wszystkie strefy
		// I = I3 -  I1
		Iorta = *log->I3_orta - *log->I1_orta;
		Iortb = *log->I3_ortb - *log->I1_ortb;
		I = 1 / (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->z[4]=log->dw.Z.R[8];
		*log->z[5]=log->dw.Z.X[8];

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


		//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;


		log->dw.Igr[3] = log->dw.Igr[0] && log->dw.Igr[1];
		log->dw.Igr[4] = log->dw.Igr[1] && log->dw.Igr[2];
		log->dw.Igr[5] = log->dw.Igr[2] && log->dw.Igr[0];


		//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_.kp;



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

		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];

			int licz_p = 5;
		
			/*
			if (((log->dw.Z.X[i] < 0) && (log->dw.Z.R[i] > 0)) || ((log->dw.Z.X[i] > 0) && (log->dw.Z.R[i] < 0)))
				licz_p = 20;
			else
				licz_p = 5;
			*/
			
			sprawdz_P( //kierunek do przodu
				&log->dw.XKp[i],
				wk1 && wk2,
				wk1p || wk2p,
				&log->dw.liczp11[i],
				8,
				4
				);


			sprawdz_P( //kierunek do tylu
				&log->dw.XKm[i],
				!wk1 && !wk2,
				!wk1p || !wk2p,
				&log->dw.liczp12[i],
				8,
				4
				);


			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

		log->dw.Zm[0] = log->dw.Z.Z[0] < log->nast_.Zgr || log->dw.U1 < 0.0001f ;
		log->dw.Zm[1] = log->dw.Z.Z[1] < log->nast_.Zgr || log->dw.U2 < 0.0001f ;
		log->dw.Zm[2] = log->dw.Z.Z[2] < log->nast_.Zgr || log->dw.U3 < 0.0001f ;
		log->dw.Zm[3] = log->dw.Z.Z[3] < log->nast_.Zgr || log->dw.U1 < 0.0001f ;
		log->dw.Zm[4] = log->dw.Z.Z[4] < log->nast_.Zgr || log->dw.U2 < 0.0001f ;
		log->dw.Zm[5] = log->dw.Z.Z[5] < log->nast_.Zgr || log->dw.U3 < 0.0001f ;
		log->dw.Zm[6] = log->dw.Z.Z[6] < log->nast_.Zgr || *log->U12 < 0.0001f ;
		log->dw.Zm[7] = log->dw.Z.Z[7] < log->nast_.Zgr || *log->U23 < 0.0001f ;
		log->dw.Zm[8] = log->dw.Z.Z[8] < log->nast_.Zgr || *log->U31 < 0.0001f ;



		//--------------------------------------------------------------------------------
		//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[i] = fabs(log->dw.Z.X[i] - log->dw.Z.R[i] * log->nast_.tanfi1);
				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[i] = fabs(log->dw.Z.X[i] - log->dw.Z.R[i] * log->nast_.tanfi1);
				i++;
		}



		//-----------------------------------------------------------------------------
		// sprawdzenie spelnienia warunku impedancji strefy
		//-----------------------------------------------------------------------------
		// sprawdzenie strefy 1 zwarcia jednofazowe z ziemia strefa 1W
		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.XRtanfi1[i] < log->nast_.n_pol[0][0].Rrtanfi1)
						&& log->dw.Z.XRtanfi2[i] < log->nast_.Xr1Wf
						&& log->dw.Igr[i]
						&& log->dw.Z.absX[i] < log->nast_.n_pol[0][0].Xr;
				wk2 = (log->dw.Z.XRtanfi1[i] > log->nast_.n_pol[0][0].Rptanfi1 && 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;
			}
			sprawdz_P(
				&log->dw.P_s[0][i],
				wk1,
				wk2,
				&log->dw.liczps[0][i],
				filtr_P
				);
			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
		}

		// sprawdzenie strefy 1 zwarcia jednofazowe z ziemia strefa 1
		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.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;
				wk2 = (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;
			}
			sprawdz_P(
				&log->dw.P_s[1][i],
				wk1,
				wk2,
				&log->dw.liczps[1][i],
				filtr_P
				);
			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
		}


		//sprawdzenie pozostalych stref zwarcie jednofazowe z ziemia
		for(j=2;j<6;j++)
		{
			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.Igr[i-3]
							&& log->dw.Z.absX[i] < log->nast_.n_pol[j][0].Xr;
					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.Igr[i-3]
							|| log->dw.Z.absX[i] > log->nast_.n_pol[j][0].Xp;
				}
				sprawdz_P(
					&log->dw.P_s[j][i-3],
					wk1,
					wk2,
					&log->dw.liczps[j][i-3],
					filtr_P
					);

				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
			}

		}

		//sprawdzenie petli miedzyfazowych
		for(j=0;j<6;j++)
		{
			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.Igr[i-3]
								&& log->dw.Z.absX[i] < log->nast_.n_pol[j][1].Xr;
						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.Igr[i-3]
								|| log->dw.Z.absX[i] > log->nast_.n_pol[j][1].Xp;
				}
				sprawdz_P(
					&log->dw.P_s[j][i-3],
					wk1,
					wk2,
					&log->dw.liczps[j][i-3],
					filtr_P
					);
				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

			}





		}




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

		if (*log->sU2 > 0.001f)
		{
			// kryterium skladowych przeciwnych
			//przesuniecia skladowych napiecia o 45 stopni
			if (*log->sI2 > 0.0003f)
			{
				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
			if ((*log->sI1 > 0.0005f) &&
				(*log->sU1 > 0.00001f))
			{
				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;
			}
		}





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

		if (!check_struct(&log->Bl_K))//jeli nie ma blokady kierunku
		{

			switch(log->nast_.kierunek[0])
			{
				case 0: //bezkierunku
					for(i=0;i<3;i++)
					{
						log->dw.P[0][i] = log->dw.Pbk[0][i];
						log->dw.P[1][i] = log->dw.Pbk[1][i];
					}
					break;
				case 1: //do linii
					for(i=0;i<3;i++)
					{
						wk1 = log->dw.Kp && (log->dw.XKp[i] || log->dw.Zm[i]);
						log->dw.P[0][i] = log->dw.Pbk[0][i] && wk1;
						log->dw.P[1][i] = log->dw.Pbk[1][i] && wk1;
					}
					break;
				case 2: // do szyn
					for(i=0;i<3;i++)
					{
						wk1 = log->dw.Km && (log->dw.XKm[i] || log->dw.Zm[i]);
						log->dw.P[0][i] = log->dw.Pbk[0][i] && wk1;
						log->dw.P[1][i] = log->dw.Pbk[1][i] && wk1;
					}
					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
						for(i=3;i<6;i++)
						{
							log->dw.P[j][i-3] = log->dw.Pbk[j][i-3] && log->dw.Kp && (log->dw.XKp[i] || log->dw.Zm[i]);
						}
						break;
					case 2: // do szyn
						for(i=3;i<6;i++)
						{
							log->dw.P[j][i-3] = log->dw.Pbk[j][i-3] && log->dw.Km && (log->dw.XKm[i] || log->dw.Zm[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
						for(i=6;i<9;i++)
						{
							log->dw.P[j][i-3] = log->dw.Pbk[j][i-3] && log->dw.Kp && (log->dw.XKp[i] || log->dw.Zm[i]);
						}
						break;
					case 2: // do szyn
						for(i=6;i<9;i++)
						{
							log->dw.P[j][i-3] = log->dw.Pbk[j][i-3] && log->dw.Km && (log->dw.XKm[i] || log->dw.Zm[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;
			}
		}

		/*for(i=0;i<36;i++)
		{
			check_and_set_struct(log->dw.P[i/6][i%6],(struct binary_io *)((u8 *)&log->P1W_L1E+(sizeof(struct binary_io)*i)));
		}*/

		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);
	}
	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);

	}
}
