📄 dsr_waypoint_mobility.pr.c
字号:
/* Process model C form file: dsr_waypoint_mobility.pr.c */
/* Portions of this file copyright 1992-2001 by OPNET Technologies, Inc. */
/* This variable carries the header into the object file */
static const char dsr_waypoint_mobility_pr_c [] = "MIL_3_Tfile_Hdr_ 80C 30A op_runsim 7 3C8784B0 3C8784B0 1 jballah Jason@Ballah 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 */
///////////////////////////////////////////////////////////////
// WAYPOINT MOBILITY HEADER BLOCK
//
// Declaration of every constant, type, library, global
// variables... used by the billard nobility process
///////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////
//////////////////////// INCLUDE //////////////////////////////
///////////////////////////////////////////////////////////////
#include <math.h>
///////////////////////////////////////////////////////////////
///////////////// CONSTANTS DEFINITION ////////////////////////
///////////////////////////////////////////////////////////////
#define PI 3.141592654
///////////////////////////////////////////////////////////////
///////////////// TRANSITION CODES ////////////////////////////
///////////////////////////////////////////////////////////////
#define MOVE_CODE 0
#define START_PAUSE_CODE 1
#define END_PAUSE_CODE 2
#define STAT_CODE 3
///////////////////////////////////////////////////////////////
///////////////// TRANSITION MACROS////////////////////////////
///////////////////////////////////////////////////////////////
#define MOVE (op_intrpt_type () == OPC_INTRPT_SELF && op_intrpt_code() == MOVE_CODE)
#define START_PAUSE (PAUSE == 1)
#define KEEP_MOVING (PAUSE == 0)
#define END_PAUSE (op_intrpt_type () == OPC_INTRPT_SELF && op_intrpt_code() == END_PAUSE_CODE)
#define STATS (op_intrpt_type () == OPC_INTRPT_SELF && op_intrpt_code() == STAT_CODE)
#define FIX (MOBILE == 0)
#define MOBIL (MOBILE == 1)
///////////////////////////////////////////////////////////////
///////////////// MOBILITY STATUS /////////////////////////////
///////////////////////////////////////////////////////////////
#define Disabled 0
#define Enabled 1
#define N_MAX 55
#define TRACE_LEV_1 1
///////////////////////////////////////////////////////////////
///////////////// GLOBAL VARIABLES ////////////////////////////
///////////////////////////////////////////////////////////////
double avg_speed;
typedef struct{
double x;
double y;
}Position;
/* 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;
Objid process_id;
Objid net_id;
int MOBILE;
double XMAX;
double XMIN;
double YMAX;
double YMIN;
int DEBUG;
int node_addr;
double TRANSMISSION_RANGE;
int num_nodes;
int my_avg_speed;
double distance_to_cover;
double PAUSE_TIME;
double MVT_STEP;
double SPEED_LIMIT;
double direction_angle;
double step_range;
double DISTANCE_LIMIT;
Distribution * uniform_double_x;
Distribution * uniform_double_y;
int PAUSE;
double x_target;
double y_target;
Distribution * my_avg_speed_dist;
} dsr_waypoint_mobility_state;
#define pr_state_ptr ((dsr_waypoint_mobility_state*) SimI_Mod_State_Ptr)
#define one pr_state_ptr->one
#define node_id pr_state_ptr->node_id
#define process_id pr_state_ptr->process_id
#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
#define TRANSMISSION_RANGE pr_state_ptr->TRANSMISSION_RANGE
#define num_nodes pr_state_ptr->num_nodes
#define my_avg_speed pr_state_ptr->my_avg_speed
#define distance_to_cover pr_state_ptr->distance_to_cover
#define PAUSE_TIME pr_state_ptr->PAUSE_TIME
#define MVT_STEP pr_state_ptr->MVT_STEP
#define SPEED_LIMIT pr_state_ptr->SPEED_LIMIT
#define direction_angle pr_state_ptr->direction_angle
#define step_range pr_state_ptr->step_range
#define DISTANCE_LIMIT pr_state_ptr->DISTANCE_LIMIT
#define uniform_double_x pr_state_ptr->uniform_double_x
#define uniform_double_y pr_state_ptr->uniform_double_y
#define PAUSE pr_state_ptr->PAUSE
#define x_target pr_state_ptr->x_target
#define y_target pr_state_ptr->y_target
#define my_avg_speed_dist pr_state_ptr->my_avg_speed_dist
/* 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 dsr_waypoint_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 dsr_waypoint_mobility (void);
Compcode dsr_waypoint_mobility_init (void **);
void dsr_waypoint_mobility_diag (void);
void dsr_waypoint_mobility_terminate (void);
void dsr_waypoint_mobility_svar (void *, const char *, char **);
#if defined (__cplusplus)
} /* end of 'extern "C"' */
#endif
/* Process model interrupt handling procedure */
void
dsr_waypoint_mobility (void)
{
int _block_origin = 0;
FIN (dsr_waypoint_mobility ());
if (1)
{
Objid params_comp_attr_objid;
Objid params_attr_objid;
double rand, prev_angle,x,y,z,x_pos,y_pos,z_pos;
double first_interval;
char msg[64];
int i;
double pause_time;
FSM_ENTER (dsr_waypoint_mobility)
FSM_BLOCK_SWITCH
{
/*---------------------------------------------------------*/
/** state (init) enter executives **/
FSM_STATE_ENTER_FORCED_NOLABEL (0, "init", "dsr_waypoint_mobility () [init enter execs]")
{
///////////////////////////////////////////////////////////////
// WAYPOINT MOBILITY INIT STATE
//
// Initialize the waypoint mobility process model
///////////////////////////////////////////////////////////////
// init Objids
process_id = op_id_self();
node_id = op_topo_parent (process_id);
net_id = op_topo_parent(node_id);
// Load Mobility parameters
op_ima_sim_attr_get(OPC_IMA_INTEGER, "MOBILITY" , &MOBILE);
op_ima_sim_attr_get(OPC_IMA_DOUBLE, "PAUSE_TIME" , &PAUSE_TIME);
op_ima_sim_attr_get(OPC_IMA_DOUBLE, "MVT_STEP" , &MVT_STEP);
op_ima_sim_attr_get(OPC_IMA_DOUBLE, "SPEED_LIMIT", &SPEED_LIMIT);
op_ima_sim_attr_get(OPC_IMA_DOUBLE, "X_MIN" , &XMIN);
op_ima_sim_attr_get(OPC_IMA_DOUBLE, "X_MAX" , &XMAX);
op_ima_sim_attr_get(OPC_IMA_DOUBLE, "Y_MIN" , &YMIN);
op_ima_sim_attr_get(OPC_IMA_DOUBLE, "Y_MAX" , &YMAX);
op_ima_sim_attr_get(OPC_IMA_DOUBLE,"Wireless LAN Range (meters)",&TRANSMISSION_RANGE);
/* Read the number of nodes in the network */
num_nodes = op_topo_object_count (OPC_OBJTYPE_NDMOB);
if(num_nodes > N_MAX)
{
op_sim_end("Network contains more than N_MAX nodes","Change N_MAX value in all files and restart all over again","","");
}
// Pick a random init position
uniform_double_x = op_dist_load ("uniform", XMIN, XMAX);
x_pos = op_dist_outcome(uniform_double_x);
uniform_double_y = op_dist_load ("uniform", YMIN, YMAX);
y_pos = op_dist_outcome(uniform_double_y);
// Set the current position of the node
op_ima_obj_attr_set(node_id,"x position",x_pos);
op_ima_obj_attr_set(node_id,"y position",y_pos);
// Compute DISTANCE_LIMIT
DISTANCE_LIMIT = sqrt((XMAX - XMIN)*(XMAX - XMIN)+(YMAX - YMIN)*(YMAX - YMIN));
// initialize distributions for random movement
one = op_dist_load ("uniform", -1, 1);
// Begin
// If MOBILITY attribute is set to Enabled, then node is
// really entitled to travel through the network.
// In this case, transit to "Init_Mvt" state.
// Init avg speed
avg_speed = op_dist_uniform(SPEED_LIMIT);
// Schedule the end of the initial pause time
op_intrpt_schedule_self(op_sim_time()+PAUSE_TIME,END_PAUSE_CODE);
}
/** state (init) exit executives **/
FSM_STATE_EXIT_FORCED (0, "init", "dsr_waypoint_mobility () [init exit execs]")
{
}
/** state (init) transition processing **/
FSM_TRANSIT_FORCE (3, state3_enter_exec, ;, "default", "", "init", "idle")
/*---------------------------------------------------------*/
/** state (Init_Mvt) enter executives **/
FSM_STATE_ENTER_FORCED (1, state1_enter_exec, "Init_Mvt", "dsr_waypoint_mobility () [Init_Mvt enter execs]")
{
/* Initiate the movement.
/* 1. Determine the avg speed of the current node.
/* 2. Determine the length of the distance, which the node
/* has to cover in his next run.
/* 3. Given these two values, plus the movement step in time,
/* then compute the distance to cover during each simulation
/* step (state variable "step_range")
/* 4. Determine a direction to follow.
*/
op_prg_odb_bkpt("Mobile_Init");
// Reset the PAUSE flag
PAUSE = 0;
// init the distribution which will chose a uniformly distributed
// average speed between 0 and SPEED_LIMIT meters per second
my_avg_speed_dist=op_dist_load("uniform_int",0,SPEED_LIMIT);
my_avg_speed=(int)op_dist_outcome(my_avg_speed_dist);
//printf("My average speed is %d\n",my_avg_speed);
// Pick a target position
x_target = x = op_dist_outcome(uniform_double_x);
y_target = y = op_dist_outcome(uniform_double_y);
// Read the current position
op_ima_obj_attr_get(node_id,"x position",&x_pos);
op_ima_obj_attr_get(node_id,"y position",&y_pos);
// Compute the distance to cover for the next mvt
distance_to_cover = sqrt((x-x_pos)*(x-x_pos) + (y-y_pos)*(y-y_pos));
sprintf(msg,"Distance to cover = %f",distance_to_cover);
// Step range
step_range = MVT_STEP*my_avg_speed;
// Angle
if(x != x_pos)
{
direction_angle = atan((y-y_pos)/(x-x_pos));///op_dist_outcome (one) * PI;
if(direction_angle < 0)
direction_angle = (direction_angle*(-1));
if(x_target > x_pos && y_target > y_pos)
direction_angle = direction_angle;
else if(x_target > x_pos && y_target < y_pos)
direction_angle = (direction_angle*(-1));
else if(x_target < x_pos && y_target > y_pos)
{
direction_angle = PI - direction_angle;
}
else if(x_target < x_pos && y_target < y_pos)
{
direction_angle = direction_angle - PI;
}
}
else if(y != y_pos)
direction_angle = (y>y_pos?0.5*PI:-0.5*PI);
else
direction_angle = 0;
sprintf(msg,"direction angle = %f*PI", direction_angle/PI);
}
/** state (Init_Mvt) exit executives **/
FSM_STATE_EXIT_FORCED (1, "Init_Mvt", "dsr_waypoint_mobility () [Init_Mvt exit execs]")
{
}
/** state (Init_Mvt) transition processing **/
FSM_TRANSIT_FORCE (2, state2_enter_exec, ;, "default", "", "Init_Mvt", "Move")
/*---------------------------------------------------------*/
/** state (Move) enter executives **/
FSM_STATE_ENTER_FORCED (2, state2_enter_exec, "Move", "dsr_waypoint_mobility () [Move enter execs]")
{
op_prg_odb_bkpt("Mobile_Move");
/* Move a step_range ahead and decrease the lenght of distance_to_cover*/
// Get the current position of the node
op_ima_obj_attr_get(node_id,"x position",&x_pos);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -