Пример #1
0
void init() {

  // Initialize TrueTime kernel  
  ttInitKernel(1, 0, prioFP); // nbrOfInputs, nbrOfOutputs, fixed priority
  
  // Create task data (local memory)
  data = new PD_Data;
  data->K = 1.5;
  data->Td = 0.035;
  data->N = 100000.0;
  data->h = 0.010;
  data->ad = data->Td/(data->N*data->h+data->Td);
  data->bd = data->N*data->K*data->Td/(data->N*data->h+data->Td);
  data->yold = 0.0;
  data->Dold = 0.0;
  data->u = 0.0;

  // Controller task
  double deadline = 0.010;
  double prio = 2.0;
  ttCreateTask("pid_task", deadline, prio, ctrl_code, data);
  ttCreateJob("pid_task");

  // Disturbance task (uncomment to add disturbance task)
  // double offset = 0.0002;
  // double period = 0.007;
  // prio = 1.0;
  // ttCreatePeriodicTask("dummy", offset, period, prio, dummy_code);

  // Initialize network
  ttCreateInterruptHandler("msgRcv", prio, msgRcvhandler);
  ttInitNetwork(3, "msgRcv"); // node #3 in the network
  
  ttCreateSemaphore("sem", 0);
}
Пример #2
0
void ttInitKernelMATLAB(int dispatch) {

  switch (dispatch) {
  case EDF:
    ttInitKernel(rtsys->prioEDF);
    break;
  case DM:
    ttInitKernel(rtsys->prioDM);
    break;
  case FP:
    ttInitKernel(rtsys->prioFP);
    break;
  default:
    TT_MEX_ERROR("ttInitKernel: Invalid priority function!");
    return;
  }

}
Пример #3
0
void ttInitKernel(double (*prioFcn)(UserTask*), double contextSwitchOH) {  

  ttInitKernel(prioFcn);

  if (contextSwitchOH < 0.0) {
    TT_MEX_ERROR("ttInitKernel: Invalid context switch overhead!");
    return;
  }

  if (contextSwitchOH > TT_TIME_RESOLUTION) {
    rtsys->contextSwitchTime = contextSwitchOH;
    rtsys->kernelHandler = createCShandler();
  }
}
Пример #4
0
void init()
{
  // Initialize TrueTime kernel
  ttInitKernel(1, 0, prioFP); // nbrOfInputs, nbrOfOutputs, fixed priority

  // Sensor task
  double offset = 0.0;
  double period = 0.010;
  double prio = 1.0;
  ttCreatePeriodicTask("sensor_task", offset, period, prio, sensor_code);

  // Initialize network
  ttCreateInterruptHandler("msgRcv", prio, msgRcvhandler);
  ttInitNetwork(4, "msgRcv"); // node #4 in the network
}
Пример #5
0
void init() {

  // Read the input argument from the block dialogue
  mxArray *initarg = ttGetInitArg();
  if (!mxIsDoubleScalar(initarg)) {
    TT_MEX_ERROR("The init argument must be a number!\n");
    return;
  }
  int implementation = (int)mxGetPr(initarg)[0];

  // Allocate KernelData memory and store pointer in kernel
  KernelData *kd = new KernelData;
  ttSetUserData(kd);

  // Allocate memory for implementation != 2
  TaskData *d = new TaskData;
  kd->d = d;  // Store pointer in KernelData

  // Allocate memory for implementation 2
  double *d2 = new double;
  kd->d2 = d2; // Store pointer in KernelData

  // Allocate memory for implementation 4
  int *hdl_data = new int;
  kd->hdl_data = hdl_data;   // Store pointer in KernelData
  
  // Initialize TrueTime kernel
  ttInitKernel(prioFP);

  // Task attributes
  double starttime = 0.0;
  double period = 0.006;
  double deadline = period;

  // Controller parameters and states
  d->K = 0.96;
  d->Ti = 0.12;
  d->Td = 0.049;
  d->beta = 0.5;
  d->N = 10.0;
  d->h = period;
  d->u = 0.0;
  d->t = 0.0;  // only used for implementation 3
  d->Iold = 0.0;
  d->Dold = 0.0;
  d->yold = 0.0;
  d->rChan = 1;
  d->yChan = 2;
  d->uChan = 1;

  switch (implementation) {
    
  case 1:
    // IMPLEMENTATION 1: using the built-in support for periodic tasks
    
    ttCreatePeriodicTask("pid_task", starttime, period, pid_code1, d);
    
    break;

  case 2:
    // IMPLEMENTATION 2: calling Simulink block within code function
    
    ttCreatePeriodicTask("pid_task", starttime, period, pid_code2, d2);
    break;

  case 3:
    // IMPLEMENTATION 3: sleepUntil and loop back
  
    ttCreateTask("pid_task", deadline, pid_code3, d);
    ttCreateJob("pid_task");
    break;
    
  case 4:
    // IMPLEMENTATION 4: sampling in timer handler, triggers task job
  
    *hdl_data = 2; // y_chan for reading samples
    ttCreateHandler("timer_handler", 1, sampler_code, hdl_data);
    ttCreatePeriodicTimer("timer", starttime, period, "timer_handler");
    ttCreateMailbox("Samples", 10);
    ttCreateTask("pid_task", deadline, pid_code4, d);
    break;

  }
}
Пример #6
0
void mexFunction( int nlhs, mxArray *plhs[],
                  int nrhs, const mxArray *prhs[] )
{
  rtsys = getrtsys(); // Get pointer to rtsys 
  if (rtsys==NULL) {
    return;
  }

  // Check number and type of arguments. 
  if (nrhs < 3 || nrhs > 4) {
    MEX_ERROR("ttInitKernel: Wrong number of input arguments! \nUsage: ttInitKernel(nbrInputs, nbrOutputs, prioFcn) or\n       ttInitKernel(nbrInputs, nbrOutputs, prioFcn, contextSwitchOH)");
    return;
  }
  if (!mxIsDoubleScalar(prhs[0])) {
    MEX_ERROR("ttInitKernel: nbrInputs must be an integer");
    return;
  }
  if (!mxIsDoubleScalar(prhs[1])) {
    MEX_ERROR("ttInitKernel: nbrOutputs must be an integer");
    return;
  }
  if (mxIsChar(prhs[2]) != 1 || mxGetM(prhs[2]) != 1) {
    MEX_ERROR("ttInitKernel: prioFcn must be a string");
    return;
  }
  if (nrhs == 4) {
    if (!mxIsDoubleScalar(prhs[3])) {
      MEX_ERROR("ttInitKernel: contextSwitchOH must be a number");
      return;
    }
  }

  int nbrInp = (int) *mxGetPr(prhs[0]);
  int nbrOutp = (int) *mxGetPr(prhs[1]);
  
  if ( nbrInp < 0 ) {
    MEX_ERROR("ttInitKernel: nbrInputs must be positive or zero");
    return;
  }
  if ( nbrOutp < 0 ) {
    MEX_ERROR("ttInitKernel: nbrOutputs must be positive or zero");
    return;
  }

  char buf[20];
  mxGetString(prhs[2], buf, 20);

  int dispatch;
  if (strcmp(buf, "prioFP") == 0) {
    dispatch = FP;
  } else if (strcmp(buf, "prioRM") == 0) {
    dispatch = RM;
  } else if (strcmp(buf, "prioDM") == 0){
    dispatch = DM;
  } else if (strcmp(buf, "prioEDF") == 0) {
    dispatch = EDF;
  } else {
    printf("ttInitKernel: Unknown priority function '%s', using fixed priority scheduling!\n",buf);
    dispatch = FP; // default 
  }
  
  if (nrhs == 3) {
    ttInitKernel(nbrInp, nbrOutp, dispatch);
  } else {
    double contextSwitchOH = *mxGetPr(prhs[3]);
    ttInitKernel(nbrInp, nbrOutp, dispatch, contextSwitchOH);
  }
}