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

📄 ttnetwork.cpp

📁 一个很棒的网络控制系统仿真软件
💻 CPP
📖 第 1 页 / 共 3 页
字号:
  ssSetNumInputPorts(S, 1);  ssSetInputPortDirectFeedThrough(S, 0, 0);  ssSetInputPortWidth(S, 0, nbrOfNodes);    ssSetNumOutputPorts(S, 2);  ssSetOutputPortWidth(S, 0, nbrOfNodes);  ssSetOutputPortWidth(S, 1, nbrOfNodes);  ssSetNumSampleTimes(S, 1);    ssSetNumRWork(S, 0);  ssSetNumIWork(S, 0);  ssSetNumPWork(S, 0);   ssSetNumModes(S, 0);  ssSetNumNonsampledZCs(S, 1);  // Make sure cleanup is performed even if errors occur  ssSetOptions(S, SS_OPTION_RUNTIME_EXCEPTION_FREE_CODE | 	          SS_OPTION_CALL_TERMINATE_ON_EXIT);}static void mdlInitializeSampleTimes(SimStruct *S){  ssSetSampleTime(S, 0, CONTINUOUS_SAMPLE_TIME);  ssSetOffsetTime(S, 0, FIXED_IN_MINOR_STEP_OFFSET);}#define MDL_STARTstatic void mdlStart(SimStruct *S){  // allocate data  const mxArray *arg;  const int buflen = 256;  char buf[buflen];  int i, tmp;  static mxArray *lhs[1];  static mxArray *rhs[2];  char nwsysp[100];  struct msg *m;  // Create new network struct  //srand(314);  nwsys = new RTnetwork;  ssSetUserData(S, nwsys); // save pointer in UserData  // 1 - Network type  arg = ssGetSFcnParam(S, 0);  if (mxIsDoubleScalar(arg)) {    nwsys->type = (int)(*mxGetPr(arg))-1;  }  if (nwsys->type < 0) {    ssSetErrorStatus(S, "Error in type argument");    return;  }  // printf("type: %d\n", nwsys->type);  // 2 - Network Number  arg = ssGetSFcnParam(S, 1);  if (mxIsDoubleScalar(arg)) {     nwsys->networkNbr = (int) *mxGetPr(arg);  }  // 3 - Number of nodes  arg = ssGetSFcnParam(S, 2);  nwsys->nbrOfNodes = (int) *mxGetPr(arg); // we know it's right  // printf("nbrOfNodes: %d\n", nwsys->nbrOfNodes);  // 4 - Data rate (translated into bytes/s)  arg = ssGetSFcnParam(S, 3);  if (mxIsDoubleScalar(arg)) {     nwsys->datarate = *mxGetPr(arg)/8.0;  }  if (nwsys->datarate < 0.0) {    ssSetErrorStatus(S, "The data rate must be > 0");    return;  }  // printf("datarate: %f\n", nwsys->datarate);  // 5 - Minimum frame size  arg = ssGetSFcnParam(S, 4);  if (mxIsDoubleScalar(arg)) {    nwsys->minsize = (int) *mxGetPr(arg);  }  if (nwsys->minsize < 0) {    ssSetErrorStatus(S, "The minimum frame size must be >= 0");    return;  }  // printf("Preprocessing delay: %f\n", nwsys->predelay);  // 6 - Preprocessing delay  arg = ssGetSFcnParam(S, 5);  if (mxIsDoubleScalar(arg)) {    nwsys->predelay = *mxGetPr(arg);  }  if (nwsys->predelay < 0.0) {    ssSetErrorStatus(S, "The preprocessing delay must be > 0.0");    return;  }  // printf("Preprocessing delay: %f\n", nwsys->predelay);  // 7 - Postprocessing delay  arg = ssGetSFcnParam(S, 6);  if (mxIsDoubleScalar(arg)) {    nwsys->postdelay = *mxGetPr(arg);  }  if (nwsys->postdelay < 0.0) {    ssSetErrorStatus(S, "The postprocessing delay must be > 0.0");    return;  }  // printf("Postprocessing delay: %f\n", nwsys->postdelay);    // 8 - Loss Probability  arg = ssGetSFcnParam(S, 7);  if (mxIsDoubleScalar(arg)) {    nwsys->lossprob = *mxGetPr(arg);  }  if (nwsys->lossprob < -EPS || nwsys->lossprob > 1.0+EPS) {    ssSetErrorStatus(S, "The loss probability must be between 0 and 1");    return;  }  if (nwsys->type == FDMA) {    // 9 - Bandwidth allocations    arg = ssGetSFcnParam(S, 8);    if (mxIsDouble(arg)) {      if (mxGetNumberOfElements(arg) != nwsys->nbrOfNodes) {	ssSetErrorStatus(S, "The number of bandwidth allocations must equal the number of nodes");	return;      }      nwsys->bandwidths = new double[nwsys->nbrOfNodes];      for (i=0; i<nwsys->nbrOfNodes; i++) {	nwsys->bandwidths[i] = mxGetPr(arg)[i];	printf("bandwidth %d: %f\n", i, nwsys->bandwidths[i]);      }    } else {      ssSetErrorStatus(S, "The bandwidth allocations must be a vector of doubles");    }  }  if (nwsys->type == TDMA) {    // 10 - Slotsize (translated into slottime)    arg = ssGetSFcnParam(S, 9);    if (mxIsDoubleScalar(arg)) {      nwsys->slottime = *mxGetPr(arg)/nwsys->datarate;    }    if (nwsys->slottime <= 0.0) {      ssSetErrorStatus(S, "The slot size must be > 0");      return;    }    // printf("slottime: %f\n", nwsys->slottime);    // 11 - Schedule    arg = ssGetSFcnParam(S, 10);    if (mxIsDouble(arg)) {      nwsys->schedsize = mxGetNumberOfElements(arg);    }    if (nwsys->schedsize < 1) {      	ssSetErrorStatus(S, "A schedule must be entered");	return;    }    nwsys->schedule = new int[nwsys->schedsize];    // printf("schedule: ");    for (i=0; i<nwsys->schedsize; i++) {      nwsys->schedule[i] = ((int)mxGetPr(arg)[i])-1;      if (nwsys->schedule[i] < -1 || nwsys->schedule[i] > nwsys->nbrOfNodes-1) {	ssSetErrorStatus(S, "Illegal node (< 0 or > nbrOfNodes) in schedule");	return;      }      // printf("%d ", nwsys->schedule[i]);    }    // printf("\n");  }  if (nwsys->type == SFDSE) {    // 12 - Switch memory size    arg = ssGetSFcnParam(S, 11);    if (mxIsDoubleScalar(arg)) {      nwsys->memsize = (int) *mxGetPr(arg);    }    if (nwsys->memsize <= 0) {      ssSetErrorStatus(S, "The switch memory size must be > 0");      return;    }    nwsys->switchmem = nwsys->memsize;    // 13 - Switch buffer type    arg = ssGetSFcnParam(S, 12);    if (mxIsDoubleScalar(arg)) {      nwsys->buftype = (int)(*mxGetPr(arg))-1;    }    if (nwsys->buftype < 0) {      ssSetErrorStatus(S, "Error in buffer type argument");      return;    }    // 14 - Switch overflow behavior    arg = ssGetSFcnParam(S, 13);    if (mxIsDoubleScalar(arg)) {      nwsys->overflow = (int)(*mxGetPr(arg))-1;    }    if (nwsys->overflow < 0) {      ssSetErrorStatus(S, "Error in buffer type argument");      return;    }  }  /* Write pointer to Simulink block UserData */  mexCallMATLAB(1, lhs, 0, NULL, "gcbh");  sprintf(nwsysp,"%p",nwsys);  rhs[0] = mxCreateDoubleMatrix(1,1,mxREAL);  *mxGetPr(rhs[0]) = *mxGetPr(lhs[0]);  rhs[1] = mxCreateString("UserData");  rhs[2] = mxCreateString(nwsysp);  mexCallMATLAB(0, NULL, 3, rhs, "set_param");  nwsys->inputs = new double[nwsys->nbrOfNodes];  nwsys->oldinputs = new double[nwsys->nbrOfNodes];  nwsys->outputs   = new double[nwsys->nbrOfNodes];  nwsys->sendschedule = new double[nwsys->nbrOfNodes];  for (i=0; i<nwsys->nbrOfNodes; i++) {    nwsys->inputs[i] = 0.0;    nwsys->oldinputs[i] = 0.0;    nwsys->outputs[i] = 0.0;    nwsys->sendschedule[i] = i+1;  }  nwsys->time = 0.0;  nwsys->prevHit = 0.0;  nwsys->nwnodes = new NWnode*[nwsys->nbrOfNodes];  for (i=0; i<nwsys->nbrOfNodes; i++) {    nwsys->nwnodes[i] = new NWnode();    nwsys->nwnodes[i]->switchmem = nwsys->switchmem / nwsys->nbrOfNodes;  }    nwsys->waituntil = 0.0;  nwsys->sending = -1;  // Note! -1 means nobody is sending  nwsys->rrturn = nwsys->nbrOfNodes - 1; // want to start at 0  nwsys->lasttime = -1.0;  nwsys->slotcount = nwsys->schedsize - 1; // want to start at 0  nwsys->currslottime = -nwsys->slottime;  // same here }#define MDL_INITIALIZE_CONDITIONSstatic void mdlInitializeConditions(SimStruct *S){}static void mdlOutputs(SimStruct *S, int_T tid){  int i, run = 0;  double *y;  double input;  InputRealPtrsType u;  struct msg *m;  int waiting;  real_T *s = ssGetOutputPortRealSignal(S,1);  nwsys = (RTnetwork*)ssGetUserData(S);  nwsys->time = ssGetT(S);  for (i=0; i < nwsys->nbrOfNodes; i++) {    // printf("input %d: %f\n", i+1, input);    if (fabs(nwsys->inputs[i]-nwsys->oldinputs[i]) > 0.1) {      // printf("event at input %d\n", i);      nwsys->oldinputs[i] = nwsys->inputs[i];      nwsys->nextHit = runNetwork();      run = 1;    }  }  // not sure if this is really needed...  if (run == 0 && nwsys->time >= nwsys->nextHit) {    nwsys->nextHit = runNetwork();  }  for (i=0; i<nwsys->nbrOfNodes; i++) {    ssGetOutputPortRealSignal(S,0)[i] = nwsys->outputs[i];  }  for (i=0; i<nwsys->nbrOfNodes; i++) {    ssGetOutputPortRealSignal(S,1)[i] = nwsys->sendschedule[i];  }} #define MDL_ZERO_CROSSINGSstatic void mdlZeroCrossings(SimStruct *S){  int i;  nwsys = (RTnetwork*)ssGetUserData(S);  /* Copy inputs and check for events */  for (i=0; i < nwsys->nbrOfNodes; i++) {    if (fabs(*ssGetInputPortRealSignalPtrs(S,0)[i] - nwsys->inputs[i]) > 0.1) {      nwsys->nextHit = ssGetT(S);    }    nwsys->inputs[i] = *ssGetInputPortRealSignalPtrs(S,0)[i];  }  ssGetNonsampledZCs(S)[0] = nwsys->nextHit - ssGetT(S);}static void mdlTerminate(SimStruct *S){  int i;  nwsys = (RTnetwork*) ssGetUserData(S);  if (nwsys == NULL) {    return;  }  if (nwsys->inputs) delete[] nwsys->inputs;  if (nwsys->sendschedule) delete[] nwsys->sendschedule;  if (nwsys->outputs) delete[] nwsys->outputs;  if (nwsys->oldinputs) delete[] nwsys->oldinputs;  if (nwsys->schedule) delete[] nwsys->schedule;  if (nwsys->bandwidths) delete[] nwsys->bandwidths;  delete nwsys;}#ifdef  MATLAB_MEX_FILE    /* Is this file being compiled as a MEX-file? */#include "simulink.c"      /* MEX-file interface mechanism */#else#include "cg_sfun.h"       /* Code generation registration function */#endif#ifdef __cplusplus} // end of extern "C" scope#endif

⌨️ 快捷键说明

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