/*2013-01-18T16:22:42-01:00*/

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

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

#include "axe.h"



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

CB_Index axe__num = 0;
CB_Mem_Float *axe_vitesse;
CB_Mem_Float *axe_positionTranslat;
CB_Mem_Float *axe_pourcentTranslat;
CB_Mem_Float *axe_incTranslat;
CB_Mem_Bool *axe_premierCycle;
CB_Mem_Float *axe_nbCycleParSec;
CB_Mem_Float *axe_initTranslat;
CB_Mem_Float *axe_maxTranslat;
CB_Mem_Float *axe_minTranslat;
CB_Mem_Bool *axe_d_blocage;
CB_Mem_Bool *axe_v_d_blocage;

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

#define vitesse (axe_vitesse->CB_current_value)
#define positionTranslat (axe_positionTranslat->CB_current_value)
#define pourcentTranslat (axe_pourcentTranslat->CB_current_value)
#define incTranslat (axe_incTranslat->CB_current_value)
#define premierCycle (axe_premierCycle->CB_current_value)
#define nbCycleParSec (axe_nbCycleParSec->CB_current_value)
#define initTranslat (axe_initTranslat->CB_current_value)
#define maxTranslat (axe_maxTranslat->CB_current_value)
#define minTranslat (axe_minTranslat->CB_current_value)
#define d_blocage (axe_d_blocage->CB_current_value)
#define v_d_blocage (axe_v_d_blocage->CB_current_value)


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


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

int _bibliotheque_epo__axe_init(void)
{
	(self.Float+2)->CB_current_value = 10000.0; /*maxTranslat*/
	self.Bool+=3;
	self.Float+=4;


	return 0;
}


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

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

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

		axe_premierCycle = self.Bool+0;
		axe_nbCycleParSec = self.Float+0;
		axe_initTranslat = self.Float+1;
		axe_maxTranslat = self.Float+2;
		axe_minTranslat = self.Float+3;
		axe_d_blocage = self.Bool+1;
		axe_v_d_blocage = self.Bool+2;

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

		/*no Pre-conditions*/

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

		{
			/* -----------------------------------------------------------------	*/
			/* Type : Modèle de Comportement Opératif					*/
			/* Catégorie :  Actionneurs							*/
			/* Auteur : Techniques Nouvelles d'Informatique				*/
			/* Date de révision : Octobre 2002						*/
			/* -----------------------------------------------------------------	*/
			/* Ce module représente l'évolution d'un axe borné sur lequel		*/
			/*   on imprime une vitesse d'entrainement en translation	.	*/
			/* -----------------------------------------------------------------	*/

			/* -----------------------------------------------------------------	*/
			/* Initialisation de la position de l'axe au premier cycle		*/
			/*	 de simulation.							*/
			/* -----------------------------------------------------------------	*/
			if ( ! premierCycle) {
				if (initTranslat > maxTranslat) positionTranslat = maxTranslat;
				else if (initTranslat < minTranslat) positionTranslat = minTranslat;
					else positionTranslat = initTranslat;
				premierCycle = 1;
			}

			/* -----------------------------------------------------------------	*/
			/* Traitement de la translation						*/
			/* -----------------------------------------------------------------	*/
			/* calcul de l'incrément de translation en fonction de  l'horloge de simulation */
			incTranslat = 0.0;
			if (! d_blocage) {
				nbCycleParSec = CLOCK_PERIOD/1000000;
				incTranslat = vitesse * nbCycleParSec;
			}
			positionTranslat = positionTranslat + incTranslat;

			/* -----------------------------------------------------------------	*/
			/* Limitation de la translation de l'organe 				*/
			/* -----------------------------------------------------------------	*/
			if (positionTranslat > maxTranslat) {
				incTranslat -= positionTranslat - maxTranslat;
				positionTranslat = maxTranslat;
			}
			if (positionTranslat < minTranslat) {
				incTranslat -= positionTranslat - minTranslat;
				positionTranslat = minTranslat;
			}

			/* -----------------------------------------------------------------	*/
			/* Calcul du pourcentage entre la position et la borne			*/
			/* -----------------------------------------------------------------	*/
			pourcentTranslat = (100/(maxTranslat - minTranslat)) * (positionTranslat - minTranslat)  ;
					



		}

		CB_post_float(axe_positionTranslat);
		CB_post_float(axe_pourcentTranslat);
		CB_post_float(axe_incTranslat);

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

		/*no Post-conditions*/
	}

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

	self.Bool += 3;
	self.Float += 4;

	return 0;
}

