libhybrid
A library for discretized Hybrid Dynamical Systems
Macros | Functions
mex_wrapper.c File Reference
#include "libhybrid.h"
#include "mex.h"

Go to the source code of this file.

Macros

#define HYB_MODEL_SOURCE   "model.c"
 To include the mex wrappe in your project you MUST define MATLAB_WRAPPER. More...
 
#define MATLAB_SYSTEM_IDENTIFICATION
 MATLAB System Identification wrapper and common wrapper are different. More...
 

Functions

void error_message (hyb_errorcode c)
 handler for errors from hyb_main_loop function More...
 
void mexFunction (int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
 Entry point for MATLAB Api. More...
 

Detailed Description

Author
Matteo Ragni, Matteo Cocetti
Date
10 Jan 2018

Definition in file mex_wrapper.c.

Macro Definition Documentation

#define HYB_MODEL_SOURCE   "model.c"

To include the mex wrappe in your project you MUST define MATLAB_WRAPPER.

Name of the model to wrap

Please provide the name of source of the model to wrap. It will be included in the wrapper for compilation. The model should include at least:

  • a flow map function
  • a jump map function
  • a jump set function
  • an output function
  • an hyb_opts structure with the name "options", and all the information set.

Definition at line 49 of file mex_wrapper.c.

#define MATLAB_SYSTEM_IDENTIFICATION

MATLAB System Identification wrapper and common wrapper are different.

The System Identification Wrapper and the common wrapper are different. The normal wrapper returns for each step, and it does not matter if the step is a either flow step or jump step. In the System Identification wrapper the function returns only in case of flow step. If there is more than one jump step it is not reported to the MATLAB engine. This forces the user to carefully decide which Jump Horizon to use. Reaching an horizon forces the raise of an error. This wrapper separation allows to keep always the flow time state synchronized with the System Identification sim time. This avoid the insertion of a delay \( e^{-T_s s} \) for each jump in the flow dynamic. For the System identification wrapper, simply define MATLAB_SYSTEM_IDENTIFICATION. If MATLAB_SYSTEM_IDENTIFICATION is not defined, the common wrapper is generated.

Definition at line 70 of file mex_wrapper.c.

Function Documentation

void error_message ( hyb_errorcode  c)
inline

handler for errors from hyb_main_loop function

This function handles the error code from the hyb_main_loop function. Any returning value different from HYB_SUCCESS raises a mexErrMsgIdAndTxt with a convenient error message.

Parameters
cthe error code from the hyb_main_loop function

Definition at line 85 of file mex_wrapper.c.

85  {
86  switch(ret) {
87  case HYB_SUCCESS:
88  return;
89  break;
90  case HYB_EMALLOC:
91  mexErrMsgIdAndTxt("LIBHYBRID:Model:MallocError",
92  "Allocation error in the discretization");
93  break;
94  case HYB_NULLPTR:
95  mexErrMsgIdAndTxt("LIBHYBRID:Model:NullPointer",
96  "Null pointer passed to the integrator");
97  break;
98  case HYB_TLIMIT:
99  mexErrMsgIdAndTxt("LIBHYBRID:Model:tLimit",
100  "Reached time horizon for hybrid model");
101  break;
102  case HYB_JLIMIT:
103  mexErrMsgIdAndTxt("LIBHYBRID:Model:jLimit",
104  "Reached jump horizon for hybrid model");
105  break;
106  case HYB_NOJUMP:
107  mexErrMsgIdAndTxt("LIBHYBRID:Model:InvalidSet",
108  "Invalid set conditions provided");
109  break;
110  default:
111  mexErrMsgIdAndTxt("LIBHYBRID:Model:GenericError",
112  "An unknown error was raised");
113  break;
114  }
115 }
void mexFunction ( int  nlhs,
mxArray *  plhs[],
int  nrhs,
const mxArray *  prhs[] 
)

Entry point for MATLAB Api.

The compilation and linking of a mex function generates a shared object that is dynamically loaded in MATLAB. The entry point for such library is the function mexFunction that is always called with the following arguments:

Parameters
nlhsnumber of elements in the pointer for the output (lhs = left hand side)
plhspointer for data in the left hand side
nrhsnumber of elements in the pointer for the input arguments (rhs = right hand side)
prhspointer for input data for the function

Definition at line 129 of file mex_wrapper.c.

130  {
131  /* Declaration of input and output arguments. */
132  double *x, *u, **p, *xp, *y, *sim_time;
133  int i, np;
134  size_t nu, nx;
135 
136  if (nrhs < 3) {
137  mexErrMsgIdAndTxt("LIBHYBRID:Model:InvalidSyntax",
138  "At least 4 inputs expected (t, u, x, p).");
139  }
140 
141  np = nrhs - 3;
142 
143  /* Determine number of inputs and states. */
144  nx = mxGetNumberOfElements(prhs[1]); /* Number of states. */
145  nu = mxGetNumberOfElements(prhs[2]); /* Number of inputs. */
146 
147  if (nx != options.x_size) {
148  mexErrMsgIdAndTxt("LIBHYBRID:Model:InvalidSyntax",
149  "The dimension of input 2 should be %d", options.x_size);
150  }
151 
152  /* Obtain double data pointers from mxArrays. */
153  sim_time = mxGetPr(prhs[0]); /* States at time t. */
154  x = mxGetPr(prhs[1]); /* States at time t. */
155  u = mxGetPr(prhs[2]); /* Inputs at time t. */
156 
157  p = mxCalloc(np, sizeof(double*));
158  for (i = 0; i < np; i++)
159  p[i] = mxGetPr(prhs[3+i]); /* Parameter arrays. */
160 
161  /* Create matrix for the return arguments. */
162  plhs[0] = mxCreateDoubleMatrix(nx, 1, mxREAL);
163  plhs[1] = mxCreateDoubleMatrix(options.y_size, 1, mxREAL);
164  xp = mxGetPr(plhs[0]); /* State derivative values. */
165  y = mxGetPr(plhs[1]); /* Output values. */
166 
167  #ifdef MATLAB_SYSTEM_IDENTIFICATION
168  hyb_bool is_step = hyb_true;
169  while (is_step) {
170  hyb_errorcode ret = hyb_main_loop(&options, y, xp, sim_time[0], x, u, (const double**) p);
171  is_step = (xp[1] == x[1] ? hyb_false : hyb_true);
172  error_message(ret);
173  }
174  #else
175  hyb_errorcode ret = hyb_main_loop(&options, y, xp, sim_time[0], x, u, (const double**) p);
176  error_message(ret);
177  #endif
178  mxFree(p);
179 }
hyb_errorcode
Definition: libhybrid.h:258
hyb_errorcode hyb_main_loop(hyb_opts *opts, hyb_float *y, hyb_float *xp, hyb_float tau, const hyb_float *x, const hyb_float *u, const hyb_float **p)
Hybrid system main loop.
Definition: libhybrid.c:31
hyb_bool
libhybrid boolean type is actually an enum
Definition: libhybrid.h:73
void error_message(hyb_errorcode c)
handler for errors from hyb_main_loop function
Definition: mex_wrapper.c:85