mrborel
03-04-2009, 11:22
Ho questo codice e mi chiede di passargli un parametro all'interno di simulink.
A quale variabile appartiene tale parametro?Cordiali saluti
#undef S_FUNCTION_NAME
#define S_FUNCTION_NAME rt_clock_hi
#include "simstruc.h" /* Where simulation structure, S, is defined */
#include <windows.h>
#ifdef MATLAB_MEX_FILE
#include "mex.h"
#endif
/* Input Arguments */
#define MAX_ERROR_ARG ssGetArg(S,0)
#define NUMBER_OF_ARGS (1)
#define NSAMPLE_TIMES (1)
/* IWORK Storage Allocation */
#define START_TIME (0)
#define MAX_ERROR (1)
#define NUMBER_OF_IWORKS (2)
/* RWORK Storage Allocation */
#define NUMBER_OF_RWORKS (0)
__int64 errorsum=0;
__int64 ciclo=0;
#define READINGS_PER_SECOND 10// N° reading per second (e.g. 1000 = 1 millisecond)
//mexPrintf("Prova:%i\n",READINGS_PER_SECOND);
typedef union ms_un {
LARGE_INTEGER lin;
__int64 i64;
} ms_un;
// ------
static double ms_cps = 0; // ticks per millisecond
ms_un HiRes_time; // Hi resolution time
// -- Set the high resolution clock
static void Init_HiRes_Clock(void) {
//##### ms_un HiRes_time;
HiRes_time.i64 = 0;
QueryPerformanceFrequency(&HiRes_time.lin);
if(HiRes_time.i64 == 0)
{
ms_cps = -1;
OutputDebugString("Error. No QPF.\n");
_exit(1);
}
else ms_cps = ((double)HiRes_time.i64/READINGS_PER_SECOND);
}
// -- Read the high resolution clock
__int64 HiRes_Clock() {
QueryPerformanceCounter(&HiRes_time.lin);
return (__int64) HiRes_time.i64/(__int64) ms_cps; //#####
}
static void mdlInitializeSizes(S)
SimStruct *S;
{
if (ssGetNumArgs(S) != NUMBER_OF_ARGS) {
#ifdef MATLAB_MEX_FILE
mexErrMsgTxt("Numero errato di parametri passati.\nE' richiesto un solo argomento.\n");
#endif
} else {
/* Set-up size information */
ssSetNumContStates(S, 0);
ssSetNumDiscStates(S, 0);
ssSetNumInputs(S, 0);
ssSetNumOutputs(S, 0);
ssSetDirectFeedThrough(S, 0);
ssSetNumInputArgs(S, NUMBER_OF_ARGS);
ssSetNumSampleTimes(S, NSAMPLE_TIMES);
ssSetNumIWork(S, NUMBER_OF_IWORKS);
ssSetNumRWork(S, NUMBER_OF_RWORKS);
}
}
/* Inizializzazione del tempo di campionamento */
static void mdlInitializeSampleTimes(S)
SimStruct *S;
{
ssSetSampleTimeEvent(S, 0, INHERITED_SAMPLE_TIME);
ssSetOffsetTimeEvent(S, 0, 0.0);
}
static void mdlInitializeConditions(x0, S)
double *x0;
SimStruct *S;
{
unsigned long error;
__int64 initial_time; // Time when simulation starts
Init_HiRes_Clock();
SetPriorityClass(GetCurrentProcess(),HIGH_PRIORITY_CLASS);
SetThreadPriority(GetCurrentThread(),THREAD_PRIORITY_HIGHEST);
error=(unsigned long) (READINGS_PER_SECOND*mxGetPr(MAX_ERROR_ARG)[0]);
ssSetIWorkValue(S, MAX_ERROR, error);
errorsum=0;
ciclo=0;
initial_time=HiRes_Clock();
ssSetIWorkValue(S, START_TIME, initial_time);
}
/* Function to compute outputs */
static void mdlOutputs(y, x, u, S, tid)
double *y;
double *x;
double *u;
SimStruct *S;
int tid;
{
}
/* Function to compute model update */
static void mdlUpdate(x, u, S, tid)
double *x;
double *u;
SimStruct *S;
int tid;
{
__int64 start_time=0;
__int64 target_time=0;
__int64 target_time2=0;
__int64 max_error=0;
__int64 error=0;
start_time = ssGetIWorkValue(S, START_TIME);
target_time2 = (unsigned long) (ssGetT(S)*READINGS_PER_SECOND);
target_time = target_time2+start_time;
ciclo++;
// start_time = ssGetIWorkValue(S, START_TIME);
// target_time = (unsigned long) (ssGetT(S)*READINGS_PER_SECOND)+start_time;
// ciclo++;
// mexPrintf("Prova:%lu\n",ssGetT(S));
error=HiRes_Clock()-target_time;
if (error > 0) // Simulazione in ritardo
{
max_error = ssGetIWorkValue(S, MAX_ERROR);
if (error>max_error)
{
#ifdef MATLAB_MEX_FILE
mexPrintf("Massimo errore superato. Simulazione bloccata.\n");
mexPrintf("Errore assoluto = %lu millisecondi \n",error);
mexPrintf("MAX Errore assoluto = %lu millisecondi \n",max_error);
#endif
ssSetStopRequested(S,1);
}
else
{
#ifdef MATLAB_MEX_FILE
errorsum+=error;
//mexPrintf("Attenzione: Momentanea perdita del real-time! ");
//mexPrintf("Errore = %lu millisecondi\n",error);
#endif
}
}
else // Temporizzazione OK
{
while (HiRes_Clock() < target_time) ;
}
}
/* Function to compute derivatives */
static void mdlDerivatives(dx, x, u, S, tid)
double *dx;
double *x;
double *u;
SimStruct *S;
int tid;
{
}
/* Function to perform housekeeping at execution termination */
static void mdlTerminate(S)
SimStruct *S;
{
double error_sec=(double) ((double)errorsum/READINGS_PER_SECOND)*1000/ciclo;
mexPrintf("Numero cicli = %i \n",ciclo);
mexPrintf("Media errore per ciclo (msec) = %f \n",error_sec);
SetPriorityClass(GetCurrentProcess(),NORMAL_PRIORITY_CLASS);
SetThreadPriority(GetCurrentThread(),THREAD_PRIORITY_NORMAL);
}
#ifdef MATLAB_MEX_FILE /* Is this file being compiled as a MEX-file? */
#include "simulink.c" /* Mex glue */
#else
#include "cg_sfun.h" /* Code generation glue */
#endif
A quale variabile appartiene tale parametro?Cordiali saluti
#undef S_FUNCTION_NAME
#define S_FUNCTION_NAME rt_clock_hi
#include "simstruc.h" /* Where simulation structure, S, is defined */
#include <windows.h>
#ifdef MATLAB_MEX_FILE
#include "mex.h"
#endif
/* Input Arguments */
#define MAX_ERROR_ARG ssGetArg(S,0)
#define NUMBER_OF_ARGS (1)
#define NSAMPLE_TIMES (1)
/* IWORK Storage Allocation */
#define START_TIME (0)
#define MAX_ERROR (1)
#define NUMBER_OF_IWORKS (2)
/* RWORK Storage Allocation */
#define NUMBER_OF_RWORKS (0)
__int64 errorsum=0;
__int64 ciclo=0;
#define READINGS_PER_SECOND 10// N° reading per second (e.g. 1000 = 1 millisecond)
//mexPrintf("Prova:%i\n",READINGS_PER_SECOND);
typedef union ms_un {
LARGE_INTEGER lin;
__int64 i64;
} ms_un;
// ------
static double ms_cps = 0; // ticks per millisecond
ms_un HiRes_time; // Hi resolution time
// -- Set the high resolution clock
static void Init_HiRes_Clock(void) {
//##### ms_un HiRes_time;
HiRes_time.i64 = 0;
QueryPerformanceFrequency(&HiRes_time.lin);
if(HiRes_time.i64 == 0)
{
ms_cps = -1;
OutputDebugString("Error. No QPF.\n");
_exit(1);
}
else ms_cps = ((double)HiRes_time.i64/READINGS_PER_SECOND);
}
// -- Read the high resolution clock
__int64 HiRes_Clock() {
QueryPerformanceCounter(&HiRes_time.lin);
return (__int64) HiRes_time.i64/(__int64) ms_cps; //#####
}
static void mdlInitializeSizes(S)
SimStruct *S;
{
if (ssGetNumArgs(S) != NUMBER_OF_ARGS) {
#ifdef MATLAB_MEX_FILE
mexErrMsgTxt("Numero errato di parametri passati.\nE' richiesto un solo argomento.\n");
#endif
} else {
/* Set-up size information */
ssSetNumContStates(S, 0);
ssSetNumDiscStates(S, 0);
ssSetNumInputs(S, 0);
ssSetNumOutputs(S, 0);
ssSetDirectFeedThrough(S, 0);
ssSetNumInputArgs(S, NUMBER_OF_ARGS);
ssSetNumSampleTimes(S, NSAMPLE_TIMES);
ssSetNumIWork(S, NUMBER_OF_IWORKS);
ssSetNumRWork(S, NUMBER_OF_RWORKS);
}
}
/* Inizializzazione del tempo di campionamento */
static void mdlInitializeSampleTimes(S)
SimStruct *S;
{
ssSetSampleTimeEvent(S, 0, INHERITED_SAMPLE_TIME);
ssSetOffsetTimeEvent(S, 0, 0.0);
}
static void mdlInitializeConditions(x0, S)
double *x0;
SimStruct *S;
{
unsigned long error;
__int64 initial_time; // Time when simulation starts
Init_HiRes_Clock();
SetPriorityClass(GetCurrentProcess(),HIGH_PRIORITY_CLASS);
SetThreadPriority(GetCurrentThread(),THREAD_PRIORITY_HIGHEST);
error=(unsigned long) (READINGS_PER_SECOND*mxGetPr(MAX_ERROR_ARG)[0]);
ssSetIWorkValue(S, MAX_ERROR, error);
errorsum=0;
ciclo=0;
initial_time=HiRes_Clock();
ssSetIWorkValue(S, START_TIME, initial_time);
}
/* Function to compute outputs */
static void mdlOutputs(y, x, u, S, tid)
double *y;
double *x;
double *u;
SimStruct *S;
int tid;
{
}
/* Function to compute model update */
static void mdlUpdate(x, u, S, tid)
double *x;
double *u;
SimStruct *S;
int tid;
{
__int64 start_time=0;
__int64 target_time=0;
__int64 target_time2=0;
__int64 max_error=0;
__int64 error=0;
start_time = ssGetIWorkValue(S, START_TIME);
target_time2 = (unsigned long) (ssGetT(S)*READINGS_PER_SECOND);
target_time = target_time2+start_time;
ciclo++;
// start_time = ssGetIWorkValue(S, START_TIME);
// target_time = (unsigned long) (ssGetT(S)*READINGS_PER_SECOND)+start_time;
// ciclo++;
// mexPrintf("Prova:%lu\n",ssGetT(S));
error=HiRes_Clock()-target_time;
if (error > 0) // Simulazione in ritardo
{
max_error = ssGetIWorkValue(S, MAX_ERROR);
if (error>max_error)
{
#ifdef MATLAB_MEX_FILE
mexPrintf("Massimo errore superato. Simulazione bloccata.\n");
mexPrintf("Errore assoluto = %lu millisecondi \n",error);
mexPrintf("MAX Errore assoluto = %lu millisecondi \n",max_error);
#endif
ssSetStopRequested(S,1);
}
else
{
#ifdef MATLAB_MEX_FILE
errorsum+=error;
//mexPrintf("Attenzione: Momentanea perdita del real-time! ");
//mexPrintf("Errore = %lu millisecondi\n",error);
#endif
}
}
else // Temporizzazione OK
{
while (HiRes_Clock() < target_time) ;
}
}
/* Function to compute derivatives */
static void mdlDerivatives(dx, x, u, S, tid)
double *dx;
double *x;
double *u;
SimStruct *S;
int tid;
{
}
/* Function to perform housekeeping at execution termination */
static void mdlTerminate(S)
SimStruct *S;
{
double error_sec=(double) ((double)errorsum/READINGS_PER_SECOND)*1000/ciclo;
mexPrintf("Numero cicli = %i \n",ciclo);
mexPrintf("Media errore per ciclo (msec) = %f \n",error_sec);
SetPriorityClass(GetCurrentProcess(),NORMAL_PRIORITY_CLASS);
SetThreadPriority(GetCurrentThread(),THREAD_PRIORITY_NORMAL);
}
#ifdef MATLAB_MEX_FILE /* Is this file being compiled as a MEX-file? */
#include "simulink.c" /* Mex glue */
#else
#include "cg_sfun.h" /* Code generation glue */
#endif