/*2013-01-18T16:21:54-01:00*/

/********************************************************************
 * vanne_regulation.c
 * 
 * generated by: plcgen 2.3.253
 ********************************************************************/

#include "cb_comp.h"
#include "iec_1131.h"

#include "vanne_regulation.h"

#include <math.h>

/**************************** Variables *****************************/

CB_Index vanne_regulation__num = 0;
CB_Mem_Bool *vanne_regulation_energie380;
CB_Mem_Bool *vanne_regulation_tensionCmd;
CB_Mem_Bool *vanne_regulation_tensionCpt;
CB_Mem_Bool *vanne_regulation_cmdMarche;
CB_Mem_Float *vanne_regulation_consigne;
CB_Mem_Float *vanne_regulation_debitEntree;
CB_Mem_Bool *vanne_regulation_cptFerm;
CB_Mem_Bool *vanne_regulation_cptOuv;
CB_Mem_Float *vanne_regulation_debitSortie;
CB_Mem_Float *vanne_regulation_pourcentOuverture;
CB_Mem_Float *vanne_regulation_increment;
CB_Mem_Float *vanne_regulation_valeurOuverture;
CB_Mem_Float *vanne_regulation_unite;
CB_Mem_Int *vanne_regulation_tempsOuverture;
CB_Mem_Float *vanne_regulation_debitMaxi;
CB_Mem_Bool *vanne_regulation_logiqueCptOuv;
CB_Mem_Bool *vanne_regulation_logiqueCptFerm;
CB_Mem_Bool *vanne_regulation_debitEnHeure;
CB_Mem_Bool *vanne_regulation_debitEnMinute;
CB_Mem_Bool *vanne_regulation_d_electrique;
CB_Mem_Bool *vanne_regulation_v_d_electrique;
CB_Mem_Bool *vanne_regulation_d_blocage;
CB_Mem_Float *vanne_regulation_v_d_blocage;
CB_Mem_Bool *vanne_regulation_d_debit;
CB_Mem_Float *vanne_regulation_v_d_debit;
CB_Mem_Bool *vanne_regulation_d_FdcOuv;
CB_Mem_Bool *vanne_regulation_v_d_FdcOuv;
CB_Mem_Bool *vanne_regulation_d_FdcFerm;
CB_Mem_Bool *vanne_regulation_v_d_FdcFerm;

/**************************** Variables *****************************/

#define energie380 (vanne_regulation_energie380->CB_current_value)
#define tensionCmd (vanne_regulation_tensionCmd->CB_current_value)
#define tensionCpt (vanne_regulation_tensionCpt->CB_current_value)
#define cmdMarche (vanne_regulation_cmdMarche->CB_current_value)
#define consigne (vanne_regulation_consigne->CB_current_value)
#define debitEntree (vanne_regulation_debitEntree->CB_current_value)
#define cptFerm (vanne_regulation_cptFerm->CB_current_value)
#define cptOuv (vanne_regulation_cptOuv->CB_current_value)
#define debitSortie (vanne_regulation_debitSortie->CB_current_value)
#define pourcentOuverture (vanne_regulation_pourcentOuverture->CB_current_value)
#define increment (vanne_regulation_increment->CB_current_value)
#define valeurOuverture (vanne_regulation_valeurOuverture->CB_current_value)
#define unite (vanne_regulation_unite->CB_current_value)
#define tempsOuverture (vanne_regulation_tempsOuverture->CB_current_value)
#define debitMaxi (vanne_regulation_debitMaxi->CB_current_value)
#define logiqueCptOuv (vanne_regulation_logiqueCptOuv->CB_current_value)
#define logiqueCptFerm (vanne_regulation_logiqueCptFerm->CB_current_value)
#define debitEnHeure (vanne_regulation_debitEnHeure->CB_current_value)
#define debitEnMinute (vanne_regulation_debitEnMinute->CB_current_value)
#define d_electrique (vanne_regulation_d_electrique->CB_current_value)
#define v_d_electrique (vanne_regulation_v_d_electrique->CB_current_value)
#define d_blocage (vanne_regulation_d_blocage->CB_current_value)
#define v_d_blocage (vanne_regulation_v_d_blocage->CB_current_value)
#define d_debit (vanne_regulation_d_debit->CB_current_value)
#define v_d_debit (vanne_regulation_v_d_debit->CB_current_value)
#define d_FdcOuv (vanne_regulation_d_FdcOuv->CB_current_value)
#define v_d_FdcOuv (vanne_regulation_v_d_FdcOuv->CB_current_value)
#define d_FdcFerm (vanne_regulation_d_FdcFerm->CB_current_value)
#define v_d_FdcFerm (vanne_regulation_v_d_FdcFerm->CB_current_value)


/************************ Components offsets ************************/


/********************* Initialization function **********************/

int _bibliotheque_epo__vanne_regulation_init(void)
{
	(vanne_regulation_energie380)->CB_current_value = 1; /*energie380*/
	(vanne_regulation_tensionCmd)->CB_current_value = 1; /*tensionCmd*/
	(vanne_regulation_tensionCpt)->CB_current_value = 1; /*tensionCpt*/
	(self.Int+0)->CB_current_value = 2; /*tempsOuverture*/
	(self.Float+3)->CB_current_value = 10000.0; /*debitMaxi*/
	(self.Bool+0)->CB_current_value = 1; /*logiqueCptOuv*/
	(self.Bool+1)->CB_current_value = 1; /*logiqueCptFerm*/
	(self.Bool+2)->CB_current_value = 1; /*debitEnHeure*/
	self.Bool+=12;
	self.Int+=1;
	self.Float+=6;


	return 0;
}


/************************ Behavior function *************************/

int bibliotheque_epo__vanne_regulation(void)
{
	if (exec_term_node(self_num)) {

		/*********************** Internals variables ************************/

		vanne_regulation_increment = self.Float+0;
		vanne_regulation_valeurOuverture = self.Float+1;
		vanne_regulation_unite = self.Float+2;
		vanne_regulation_tempsOuverture = self.Int+0;
		vanne_regulation_debitMaxi = self.Float+3;
		vanne_regulation_logiqueCptOuv = self.Bool+0;
		vanne_regulation_logiqueCptFerm = self.Bool+1;
		vanne_regulation_debitEnHeure = self.Bool+2;
		vanne_regulation_debitEnMinute = self.Bool+3;
		vanne_regulation_d_electrique = self.Bool+4;
		vanne_regulation_v_d_electrique = self.Bool+5;
		vanne_regulation_d_blocage = self.Bool+6;
		vanne_regulation_v_d_blocage = self.Float+4;
		vanne_regulation_d_debit = self.Bool+7;
		vanne_regulation_v_d_debit = self.Float+5;
		vanne_regulation_d_FdcOuv = self.Bool+8;
		vanne_regulation_v_d_FdcOuv = self.Bool+9;
		vanne_regulation_d_FdcFerm = self.Bool+10;
		vanne_regulation_v_d_FdcFerm = self.Bool+11;

		/********************* Pre-conditions execution *********************/

		/*no Pre-conditions*/

		/************************ Behavior execution ************************/

		{
			/* -----------------------------------------------------------------	*/
			/* Type : Modèle de Comportement Opératif					*/
			/* Catégorie :  Associations - batch					*/
			/* Auteur : Techniques Nouvelles d'Informatique				*/
			/* Date de révision : Octobre 2002						*/
			/* -----------------------------------------------------------------	*/
			/* Ce module représente le comportement d'une vanne de			*/
			/*	regulation avec sa logique électrique de commande.		*/
			/*  La consigne doit être comprise entre 0 et 100%			*/
			/* -----------------------------------------------------------------	*/

			/* -----------------------------------------------------------------	*/
			/* Gestion de l'ouverture 							*/
			/* -----------------------------------------------------------------	*/

			if (( ! d_blocage) && (energie380) && (! d_electrique) && (tensionCmd) && (cmdMarche)) {

				/* ----------------------------------------------------------	*/
				/* calcul de l'incrément de translation en fonction de 	*/
				/* l'horloge de simulation 						*/
				/* ----------------------------------------------------------	*/
				if (tempsOuverture > 0.0)
					increment = (100.0  / tempsOuverture) * (CLOCK_PERIOD/1000000);

				/* ----------------------------------------------------------	*/
				/* calcul de l'ouverture 						*/
				/* ----------------------------------------------------------	*/
				if ((valeurOuverture < fabs(consigne))  &&  (fabs(consigne) <= 100.0)) {
					(valeurOuverture) += increment;
					if (valeurOuverture > fabs(consigne)) valeurOuverture = fabs(consigne);
				}
				if (valeurOuverture > fabs(consigne)) {
					(valeurOuverture) -= increment;
					if (valeurOuverture < fabs(consigne)) valeurOuverture = fabs(consigne);
					if (valeurOuverture < 0.0) valeurOuverture = 0.0;
				}   
			}
			if (d_blocage)
				valeurOuverture = v_d_blocage;

			/* -----------------------------------------------------------------	*/
			/* Calcul du pourcentage d'ouverture					*/
			/* -----------------------------------------------------------------	*/
			pourcentOuverture = ((valeurOuverture / 100.0) * 100.0);

			/* -----------------------------------------------------------------	*/
			/* Gestion du débit 	 							*/
			/* -----------------------------------------------------------------	*/
			if (d_debit)
				debitSortie = v_d_debit;
			else {
				debitSortie = pourcentOuverture / 100.0 * debitEntree;
				if (debitSortie > debitEntree) debitSortie = debitEntree;
				if (debitEnHeure) unite = 3600.0;
				else if (debitEnMinute) unite = 60.0;
					else unite = 1.0;
				if (debitSortie > (debitMaxi * CLOCK_PERIOD / 1000000)  / unite) 
					debitSortie = ((debitMaxi * CLOCK_PERIOD / 1000000)  / unite) * (pourcentOuverture / 100.0);
			}

			/* -----------------------------------------------------------------	*/
			/* Gestion de l'état des capteurs						*/
			/* -----------------------------------------------------------------	*/
			if (d_FdcOuv ) cptOuv = v_d_FdcOuv;
			else
				if ( tensionCpt ) 
					cptOuv = (valeurOuverture >= 100.0) ^ ! logiqueCptOuv;
				else cptOuv = 0;

			if (d_FdcFerm ) cptFerm = v_d_FdcFerm;
			else
				if ( tensionCpt ) 
					cptFerm = (valeurOuverture == 0.0) ^ ! logiqueCptFerm;
				else cptFerm = 0;


		}

		CB_post_bool(vanne_regulation_cptFerm);
		CB_post_bool(vanne_regulation_cptOuv);
		CB_post_float(vanne_regulation_debitSortie);
		CB_post_float(vanne_regulation_pourcentOuverture);

		/******************** Post-conditions execution *********************/

		/*no Post-conditions*/
	}

	/*************************** self update ****************************/

	self.Bool += 12;
	self.Int += 1;
	self.Float += 6;

	return 0;
}

