feminkk
feminkk

Reputation: 1225

How to fix number of steps (not time) in Matlab simulink

This may be a simple question.But I couldn't find a way to go around this.I am using an s function block to input a wave signal to my algorithm.wave signal is being read from file of about 2000 points

First i started with simulation time equal to 50.Then it read only 50 points.I checked the 'tout' variable. It was 0,1,2,....50

Then i increased simulation time to 100.still result is same.only 50 points read.But tout is 0,2,4,6..50

I tried up to 10000.Whatever i do it reads 50 values only with wide time step like 0,200,400,600 etc. Is it a problem with my s-function or simulink settings? Here is the c s-function file

    /* Give S-function a name */
#define S_FUNCTION_NAME  Readip
#define S_FUNCTION_LEVEL 2

/* Include SimStruct definition and file I/O functions */
#include "simstruc.h"
#include <stdio.h>
#include <stdlib.h>

static FILE* file2;
/* Called at the beginning of the simulation */
    static void mdlInitializeSizes(SimStruct *S)

{

        ssSetNumSFcnParams(S, 0); 


   if (ssGetNumSFcnParams(S) != ssGetSFcnParamsCount(S)) {
        return;
    }
    if (!ssSetNumOutputPorts(S, 3)) return;
    ssSetNumContStates(S, 0);
    ssSetNumDiscStates(S, 0);
    ssSetOutputPortWidth(S, 0, 1);
    ssSetOutputPortDataType(S,0,DYNAMICALLY_SIZED);
    ssSetOutputPortOptimOpts(S, 0, SS_REUSABLE_AND_LOCAL);
    ssSetOutputPortWidth(S, 1, 1);
    ssSetOutputPortDataType(S,1,DYNAMICALLY_SIZED);
    ssSetOutputPortOptimOpts(S, 1, SS_REUSABLE_AND_LOCAL);
    ssSetOutputPortWidth(S, 2, 1);
    ssSetOutputPortDataType(S,2,DYNAMICALLY_SIZED);
    ssSetOutputPortOptimOpts(S, 2, SS_REUSABLE_AND_LOCAL);


       ssSetNumPWork(S,1);
        ssSetNumSampleTimes(S, 1);
    }


    /* Set sample times for the block */
    static void mdlInitializeSampleTimes(SimStruct *S)
    {
        ssSetSampleTime(S, 0, CONTINUOUS_SAMPLE_TIME);
        ssSetOffsetTime(S, 0, 0.0);
    }



        #define MDL_START  /* Change to #undef to remove function */
        #if defined(MDL_START) 
          /* Function: mdlStart ========

    ===============================================
       * Abstract:
       *    This function is called once at start of model execution. If you
       *    have states that shou

ld be initialized once, this is the place
   *    to do it.
   */
  static void mdlStart(SimStruct *S)
  {
      /*at start of model execution, open the file and store the pointer
       *in the pwork vector */
      void** pwork = ssGetPWork(S);
      FILE *datafile;

      datafile = fopen("table.data","r");
      pwork[0] =  datafile;

      }
    #endif /*  MDL_START */

/* Function: mdlOutputs =======================================================
 * Abstract:
 *    In this function, you compute the outputs of your S-function
 *    block. Generally outputs are placed in the output vector, ssGetY(S).
 */
static void mdlOutputs(SimStruct *S, int_T tid)
{
    //get pointer to the block's output signal
    real_T       *y1 = ssGetOutputPortSignal(S,0);
    real_T       *y2 = ssGetOutputPortSignal(S,1);
    real_T       *y3 = ssGetOutputPortSignal(S,2);
    char a[10];
    char b[10];
    char c[10];
    /*get pointer to array of pointers, where the first element is the address
     *of the open file */
    void** pwork = ssGetPWork(S);

/*read a floating point number and then the comma delimiter
 *store the result in y*/

fscanf(pwork[0],"%s    %s    %s",&a,&b,&c);
*y1=atof(a);
*y2=atof(b);
    *y3=atof(c);

}



/* Function: mdlTerminate =====================================================
 * Abstract:
 *    In this function, you should perform any actions that are necessary
 *    at the termination of a simulation.  For example, if memory was
 *    allocated in mdlStart, this is the place to free it.
 */
static void mdlTerminate(SimStruct *S)
{
    //close the file
    void** pwork = ssGetPWork(S);
      FILE *datafile;

      datafile = pwork[0];
      fclose(datafile);

}



/*=============================*
 * Required S-function trailer *
 *=============================*/

    #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

Upvotes: 0

Views: 10079

Answers (2)

Craig
Craig

Reputation: 991

You need to use the fixed step discrete solver. Set Periodic sample time constraint to 'Unconstrined' Enter the Fixed-step size and Simulation Stop time so that you get 2000 steps.

For example 1s/step and 2000s stop time, or 0.1s/step and 200s stop time.

Upvotes: 1

grungetta
grungetta

Reputation: 1097

This appears to just be a settings issue. If you do not specify a step-size for your solver and your blocks do not indicate sample time, Simulink will choose a default of Simulation Time / 50. Simply open the Model Configuration Parameters dialog and click on solvers in the left-hand pane. If you are using a fixed-step solver, you can explicitly set the step size. If you are using a variable-step solver, you can specify the max/min step sizes.

Additionally, if you are looking to specify a discrete sample period specifically for your S-function, then you may want to look in to this link to be sure that you are implementing mdlInitializeSampleTimes as desired.

Upvotes: 3

Related Questions