⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 billiard_mobility.pr.c

📁 感知无线电有关的仿真
💻 C
字号:
/* Process model C form file: billiard_mobility.pr.c */
/* Portions of this file copyright 1992-2002 by OPNET Technologies, Inc. */



/* This variable carries the header into the object file */
static const char billiard_mobility_pr_c [] = "MIL_3_Tfile_Hdr_ 81A 30A op_runsim 7 47F08FB4 47F08FB4 1 jujumao Administrator 0 0 none none 0 0 none 0 0 0 0 0 0                                                                                                                                                                                                                                                                                                                                                                                                              ";
#include <string.h>



/* OPNET system definitions */
#include <opnet.h>

#if defined (__cplusplus)
extern "C" {
#endif
FSM_EXT_DECS
#if defined (__cplusplus)
} /* end of 'extern "C"' */
#endif


/* Header Block */

#include <math.h>

#define PI	 3.141592654

/* transition macro */
#define UPDATE ( op_intrpt_type () == OPC_INTRPT_SELF ) 

/* Mobility */
#define Disabled 0
#define Enabled 1

/* End of Header Block */


#if !defined (VOSD_NO_FIN)
#undef	BIN
#undef	BOUT
#define	BIN		FIN_LOCAL_FIELD(last_line_passed) = __LINE__ - _block_origin;
#define	BOUT	BIN
#define	BINIT	FIN_LOCAL_FIELD(last_line_passed) = 0; _block_origin = __LINE__;
#else
#define	BINIT
#endif /* #if !defined (VOSD_NO_FIN) */



/* State variable definitions */
typedef struct
	{
	/* Internal state tracking for FSM */
	FSM_SYS_STATE
	/* State Variables */
	Distribution *	         		one;
	Objid	                  		node_id;
	double	                 		POS_TIMER;
	double	                 		STEP_DIST;
	Objid	                  		process_id;
	double	                 		angle;
	double	                 		abs;
	double	                 		ord;
	Objid	                  		net_id;
	int	                    		MOBILE;
	double	                 		XMAX;
	double	                 		XMIN;
	double	                 		YMAX;
	double	                 		YMIN;
	int	                    		DEBUG;
	int	                    		node_addr;
	} billiard_mobility_state;

#define pr_state_ptr            		((billiard_mobility_state*) SimI_Mod_State_Ptr)
#define one                     		pr_state_ptr->one
#define node_id                 		pr_state_ptr->node_id
#define POS_TIMER               		pr_state_ptr->POS_TIMER
#define STEP_DIST               		pr_state_ptr->STEP_DIST
#define process_id              		pr_state_ptr->process_id
#define angle                   		pr_state_ptr->angle
#define abs                     		pr_state_ptr->abs
#define ord                     		pr_state_ptr->ord
#define net_id                  		pr_state_ptr->net_id
#define MOBILE                  		pr_state_ptr->MOBILE
#define XMAX                    		pr_state_ptr->XMAX
#define XMIN                    		pr_state_ptr->XMIN
#define YMAX                    		pr_state_ptr->YMAX
#define YMIN                    		pr_state_ptr->YMIN
#define DEBUG                   		pr_state_ptr->DEBUG
#define node_addr               		pr_state_ptr->node_addr

/* This macro definition will define a local variable called	*/
/* "op_sv_ptr" in each function containing a FIN statement.	*/
/* This variable points to the state variable data structure,	*/
/* and can be used from a C debugger to display their values.	*/
#undef FIN_PREAMBLE
#define FIN_PREAMBLE	billiard_mobility_state *op_sv_ptr = pr_state_ptr;


/* No Function Block */

enum { _block_origin = __LINE__ };

/* Undefine optional tracing in FIN/FOUT/FRET */
/* The FSM has its own tracing code and the other */
/* functions should not have any tracing.		  */
#undef FIN_TRACING
#define FIN_TRACING

#undef FOUTRET_TRACING
#define FOUTRET_TRACING

#if defined (__cplusplus)
extern "C" {
#endif
	void billiard_mobility (void);
	Compcode billiard_mobility_init (void **);
	void billiard_mobility_diag (void);
	void billiard_mobility_terminate (void);
	void billiard_mobility_svar (void *, const char *, char **);
#if defined (__cplusplus)
} /* end of 'extern "C"' */
#endif




/* Process model interrupt handling procedure */


void
billiard_mobility (void)
	{
	int _block_origin = 0;
	FIN (billiard_mobility ());
	if (1)
		{
		double	altitude,latitude,longitude,rand, prev_angle,x,y,z,x_pos,y_pos,z_pos;
		Distribution * first_interval_dist;
		double  first_interval;


		FSM_ENTER (billiard_mobility)

		FSM_BLOCK_SWITCH
			{
			/*---------------------------------------------------------*/
			/** state (init) enter executives **/
			FSM_STATE_ENTER_FORCED_NOLABEL (0, "init", "billiard_mobility () [init enter execs]")
				{
				// init Objids
				node_id = op_topo_parent (op_id_self());
				op_ima_obj_attr_get(node_id,"wlan_mac.station_address",&node_addr);
				process_id = op_id_self();
				net_id = op_topo_parent(node_id);
				
				// read different attributes
				op_ima_obj_attr_get(op_id_self(), "Mobility", &MOBILE);
				op_ima_obj_attr_get(node_id, "DEBUG", &DEBUG);
				op_ima_obj_attr_get(op_id_self(), "mobil_POS_TIMER", &POS_TIMER);
				op_ima_obj_attr_get(op_id_self(), "mobil_STEP_DIST", &STEP_DIST);
				op_ima_obj_attr_get(op_id_self(), "grid_XMIN", &XMIN);
				op_ima_obj_attr_get(op_id_self(), "grid_XMAX", &XMAX);
				op_ima_obj_attr_get(op_id_self(), "grid_YMIN", &YMIN);
				op_ima_obj_attr_get(op_id_self(), "grid_YMAX", &YMAX);
				
				// initialize distributions for random movement
				one = op_dist_load ("uniform", -1, 1);
				angle = op_dist_outcome (one) * PI;
				first_interval_dist = op_dist_load ("uniform_double", 0.0,POS_TIMER);
				first_interval = op_dist_outcome(first_interval_dist);
				// sechedule first interruption f
				op_intrpt_schedule_self(op_sim_time()+POS_TIMER+first_interval,0);
				
				
				}


			/** state (init) exit executives **/
			FSM_STATE_EXIT_FORCED (0, "init", "billiard_mobility () [init exit execs]")
				{
				}


			/** state (init) transition processing **/
			FSM_TRANSIT_FORCE (1, state1_enter_exec, ;, "default", "", "init", "idle")
				/*---------------------------------------------------------*/



			/** state (idle) enter executives **/
			FSM_STATE_ENTER_UNFORCED (1, state1_enter_exec, "idle", "billiard_mobility () [idle enter execs]")
				{
				}


			/** blocking after enter executives of unforced state. **/
			FSM_EXIT (3,billiard_mobility)


			/** state (idle) exit executives **/
			FSM_STATE_EXIT_UNFORCED (1, "idle", "billiard_mobility () [idle exit execs]")
				{
				}


			/** state (idle) transition processing **/
			FSM_INIT_COND (UPDATE)
			FSM_DFLT_COND
			FSM_TEST_LOGIC ("idle")

			FSM_TRANSIT_SWITCH
				{
				FSM_CASE_TRANSIT (0, 2, state2_enter_exec, ;, "UPDATE", "", "idle", "change_position")
				FSM_CASE_TRANSIT (1, 1, state1_enter_exec, ;, "default", "", "idle", "idle")
				}
				/*---------------------------------------------------------*/



			/** state (change_position) enter executives **/
			FSM_STATE_ENTER_FORCED (2, state2_enter_exec, "change_position", "billiard_mobility () [change_position enter execs]")
				{
				if(MOBILE == Enabled)
				{
				// Get the current position of the node
				op_ima_obj_attr_get(node_id,"x position",&x_pos);
				op_ima_obj_attr_get(node_id,"y position",&y_pos);
				
				// calcul the new x and y position according to the STEP_DIST and the current direction (angle)
				x=x_pos+STEP_DIST*cos(angle);
				y=y_pos+STEP_DIST*sin(angle);
				
				// if the new x position is outside the network grid boundaries
				if ((x<XMIN)||(XMAX<x))
					{
					// if the new y position is also outside the network grid boundaries
					if ((y<YMIN)||(YMAX<y))
						{
						// come back to the previous position (on x and y)
						x=x_pos-STEP_DIST*cos(angle);
						y=y_pos-STEP_DIST*sin(angle);
						// and calculate a random new direction
						angle=op_dist_outcome(one)*PI;	
						}
					// if new y position are still in the network boundaries 
					else 
						{
						// come back to the previous position (only on x)
						x=x_pos-STEP_DIST*cos(angle);
						// and calculate a random new direction
						angle=op_dist_outcome(one)*PI;
						}
					}
				
				// if only the new y position is outside (x and y outside handle before) the network boundaries 
				if ((y<YMIN)||(YMAX<y))
					{
					// come back to the previous position (only on y)
					y=y_pos-STEP_DIST*sin(angle);
					// and compute new direction
					angle=op_dist_outcome(one)*PI;
					}
				
				if(DEBUG > 1) printf("\n{{ node %d }} @ mobility : \n      > previous pos (%f,%f) <--> new position (%f,%f)\n      > direction %f\n",node_addr,x_pos,y_pos,x,y,angle);
				
				// set the new position of the node
				op_ima_obj_attr_set(node_id, "x position",x);
				op_ima_obj_attr_set(node_id, "y position",y);
				
				// schedule the interuption for the next movement (position updating)
				op_intrpt_schedule_self(op_sim_time()+POS_TIMER,0);
				
				}
				}


			/** state (change_position) exit executives **/
			FSM_STATE_EXIT_FORCED (2, "change_position", "billiard_mobility () [change_position exit execs]")
				{
				}


			/** state (change_position) transition processing **/
			FSM_TRANSIT_FORCE (1, state1_enter_exec, ;, "default", "", "change_position", "idle")
				/*---------------------------------------------------------*/



			}


		FSM_EXIT (0,billiard_mobility)
		}
	}

#if defined (__cplusplus)
	extern "C" { 
#endif
	extern VosT_Fun_Status Vos_Catmem_Register (const char * , int , VosT_Void_Null_Proc, VosT_Address *);
	extern VosT_Address Vos_Catmem_Alloc (VosT_Address, size_t);
	extern VosT_Fun_Status Vos_Catmem_Dealloc (VosT_Address);
#if defined (__cplusplus)
	}
#endif


Compcode
billiard_mobility_init (void ** gen_state_pptr)
	{
	int _block_origin = 0;
	static VosT_Address	obtype = OPC_NIL;

	FIN (billiard_mobility_init (gen_state_pptr))

	if (obtype == OPC_NIL)
		{
		/* Initialize memory management */
		if (Vos_Catmem_Register ("proc state vars (billiard_mobility)",
			sizeof (billiard_mobility_state), Vos_Vnop, &obtype) == VOSC_FAILURE)
			{
			FRET (OPC_COMPCODE_FAILURE)
			}
		}

	*gen_state_pptr = Vos_Catmem_Alloc (obtype, 1);
	if (*gen_state_pptr == OPC_NIL)
		{
		FRET (OPC_COMPCODE_FAILURE)
		}
	else
		{
		/* Initialize FSM handling */
		((billiard_mobility_state *)(*gen_state_pptr))->current_block = 0;

		FRET (OPC_COMPCODE_SUCCESS)
		}
	}



void
billiard_mobility_diag (void)
	{
	/* No Diagnostic Block */
	}




void
billiard_mobility_terminate (void)
	{
	int _block_origin = __LINE__;

	FIN (billiard_mobility_terminate (void))

	Vos_Catmem_Dealloc (pr_state_ptr);

	FOUT;
	}


/* Undefine shortcuts to state variables to avoid */
/* syntax error in direct access to fields of */
/* local variable prs_ptr in billiard_mobility_svar function. */
#undef one
#undef node_id
#undef POS_TIMER
#undef STEP_DIST
#undef process_id
#undef angle
#undef abs
#undef ord
#undef net_id
#undef MOBILE
#undef XMAX
#undef XMIN
#undef YMAX
#undef YMIN
#undef DEBUG
#undef node_addr



void
billiard_mobility_svar (void * gen_ptr, const char * var_name, char ** var_p_ptr)
	{
	billiard_mobility_state		*prs_ptr;

	FIN (billiard_mobility_svar (gen_ptr, var_name, var_p_ptr))

	if (var_name == OPC_NIL)
		{
		*var_p_ptr = (char *)OPC_NIL;
		FOUT;
		}
	prs_ptr = (billiard_mobility_state *)gen_ptr;

	if (strcmp ("one" , var_name) == 0)
		{
		*var_p_ptr = (char *) (&prs_ptr->one);
		FOUT;
		}
	if (strcmp ("node_id" , var_name) == 0)
		{
		*var_p_ptr = (char *) (&prs_ptr->node_id);
		FOUT;
		}
	if (strcmp ("POS_TIMER" , var_name) == 0)
		{
		*var_p_ptr = (char *) (&prs_ptr->POS_TIMER);
		FOUT;
		}
	if (strcmp ("STEP_DIST" , var_name) == 0)
		{
		*var_p_ptr = (char *) (&prs_ptr->STEP_DIST);
		FOUT;
		}
	if (strcmp ("process_id" , var_name) == 0)
		{
		*var_p_ptr = (char *) (&prs_ptr->process_id);
		FOUT;
		}
	if (strcmp ("angle" , var_name) == 0)
		{
		*var_p_ptr = (char *) (&prs_ptr->angle);
		FOUT;
		}
	if (strcmp ("abs" , var_name) == 0)
		{
		*var_p_ptr = (char *) (&prs_ptr->abs);
		FOUT;
		}
	if (strcmp ("ord" , var_name) == 0)
		{
		*var_p_ptr = (char *) (&prs_ptr->ord);
		FOUT;
		}
	if (strcmp ("net_id" , var_name) == 0)
		{
		*var_p_ptr = (char *) (&prs_ptr->net_id);
		FOUT;
		}
	if (strcmp ("MOBILE" , var_name) == 0)
		{
		*var_p_ptr = (char *) (&prs_ptr->MOBILE);
		FOUT;
		}
	if (strcmp ("XMAX" , var_name) == 0)
		{
		*var_p_ptr = (char *) (&prs_ptr->XMAX);
		FOUT;
		}
	if (strcmp ("XMIN" , var_name) == 0)
		{
		*var_p_ptr = (char *) (&prs_ptr->XMIN);
		FOUT;
		}
	if (strcmp ("YMAX" , var_name) == 0)
		{
		*var_p_ptr = (char *) (&prs_ptr->YMAX);
		FOUT;
		}
	if (strcmp ("YMIN" , var_name) == 0)
		{
		*var_p_ptr = (char *) (&prs_ptr->YMIN);
		FOUT;
		}
	if (strcmp ("DEBUG" , var_name) == 0)
		{
		*var_p_ptr = (char *) (&prs_ptr->DEBUG);
		FOUT;
		}
	if (strcmp ("node_addr" , var_name) == 0)
		{
		*var_p_ptr = (char *) (&prs_ptr->node_addr);
		FOUT;
		}
	*var_p_ptr = (char *)OPC_NIL;

	FOUT;
	}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -