libhybrid
A library for discretized Hybrid Dynamical Systems
Data Structures | Macros | Typedefs | Enumerations | Functions
libhybrid.h File Reference
#include "librk4.h"

Go to the source code of this file.

Data Structures

struct  hyb_opts
 Options structure for the hybrid system. More...
 

Macros

#define HYB_FLOAT_TYPE   double
 
#define RK4_FLOAT_TYPE   HYB_FLOAT_TYPE
 
#define HYB_JUMP_LOGIC   1
 Jump logic implementation. More...
 
#define HYB_GET_OPTS(S)   ((hyb_opts *)S)
 
#define HYB_SEND_OPTS(S)   ((void *)S)
 

Typedefs

typedef HYB_FLOAT_TYPE hyb_float
 
typedef enum hyb_bool hyb_bool
 libhybrid boolean type is actually an enum More...
 
typedef void(* hyb_flow_map) (hyb_float *xdot, hyb_float t, hyb_float j, const hyb_float *x, const hyb_float *u, const hyb_float **p)
 Callback definition for the Flow Map. More...
 
typedef void(* hyb_jump_map) (hyb_float *xp, hyb_float t, hyb_float j, const hyb_float *x, const hyb_float *u, const hyb_float **p)
 Callback definition for the Jump Map. More...
 
typedef hyb_bool(* hyb_jump_set) (hyb_float t, hyb_float j, const hyb_float *x, const hyb_float *u, const hyb_float **p)
 Callback definition for the Jump Set. More...
 
typedef hyb_bool(* hyb_flow_set) (hyb_float t, hyb_float j, const hyb_float *x, const hyb_float *u, const hyb_float **p)
 Callback definition for the Flow Set. More...
 
typedef void(* hyb_out_map) (hyb_float *y, hyb_float t, hyb_float j, const hyb_float *x, const hyb_float *u, const hyb_float **p)
 
typedef struct hyb_opts hyb_opts
 Options structure for the hybrid system. More...
 
typedef enum hyb_errorcode hyb_errorcode
 

Enumerations

enum  hyb_bool { hyb_false = 0, hyb_true }
 libhybrid boolean type is actually an enum More...
 
enum  hyb_errorcode {
  HYB_SUCCESS = 0, HYB_EMALLOC, HYB_NULLPTR, HYB_GENERIC,
  HYB_TLIMIT, HYB_JLIMIT, HYB_NOJUMP
}
 

Functions

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. More...
 
void hyb_flow_map_wrapper (hyb_float *dx, hyb_float tau, const hyb_float *x, const hyb_float *u, const hyb_float **p, void *vopts)
 Internal callback for discretization step. More...
 

Data Structure Documentation

struct hyb_opts

Options structure for the hybrid system.

Definition at line 242 of file libhybrid.h.

Data Fields
hyb_flow_set C

Flow set function pointer

hyb_jump_set D

Jump set function pointer

hyb_flow_map F

Flow map function pointer

hyb_jump_map J

Jump map function pointer

hyb_float J_horizon

Maximum discrete step horizon

hyb_float T_horizon

Maximum continuous time horizon

hyb_float Ts

Time step

size_t x_size

State size

hyb_out_map Y

Output map function pointer

size_t y_size

Output size

Macro Definition Documentation

#define HYB_FLOAT_TYPE   double

Precision defaut is double

Definition at line 60 of file libhybrid.h.

#define HYB_GET_OPTS (   S)    ((hyb_opts *)S)

Converts the void pointer in option struct pointer. For internal use only

Definition at line 255 of file libhybrid.h.

#define HYB_JUMP_LOGIC   1

Jump logic implementation.

There are two possible jump logic implementation:

  1. Precedence on the jump map (default)
  2. Precedence on the flow map

Definition at line 236 of file libhybrid.h.

#define HYB_SEND_OPTS (   S)    ((void *)S)

Converts the option struct in void pointer. For internal use only

Definition at line 256 of file libhybrid.h.

#define RK4_FLOAT_TYPE   HYB_FLOAT_TYPE

Forcing consistent types

Definition at line 64 of file libhybrid.h.

Typedef Documentation

typedef enum hyb_bool hyb_bool

libhybrid boolean type is actually an enum

This enum is the returned value for the Jump Set function and the Flow Set function.

Precision typedef, used in library

Definition at line 62 of file libhybrid.h.

typedef void(* hyb_flow_map) (hyb_float *xdot, hyb_float t, hyb_float j, const hyb_float *x, const hyb_float *u, const hyb_float **p)

Callback definition for the Flow Map.

The callback stores the output in the first pointer (xdot). The function does not need to allocate nor free the output vector, but may overflow if exceedes the dimension that is declared in the hyb_opts structure. Please notice that this function will be discretized in a Runge Kutta 4 integrator: librk4.h. When accessing an element of the parameter, please remember that the implementation keeps in mind a MATLAB-like interface. If the parameters (in MATLAB) are passed like:

1 f(t, x, u, p1, [p2, p3])

then, in the C code, p1, p2 and p3 may be accessed in p as:

1 rk4_float p1, p2, p3;
2 p1 = p[0][0];
3 p2 = p[1][0];
4 p3 = p[1][1];
Parameters
xdotan array in which the result will be stored
tis the current time
jis the discrete time
xa constant array with the current state
ua constant array with current input
pa pointer to arrays of parameters. This allows the compatibility with the MATLAB System Identification Toolbox

Definition at line 107 of file libhybrid.h.

typedef hyb_bool(* hyb_flow_set) (hyb_float t, hyb_float j, const hyb_float *x, const hyb_float *u, const hyb_float **p)

Callback definition for the Flow Set.

The callback should return hyb_true if the combination of state, input and parameters enters the flow set. When accessing an element of the parameter, please remember that the implementation keeps in mind a MATLAB-like interface. If the parameters (in MATLAB) are passed like:

1 f(t, x, u, p1, [p2, p3])

then, in the C code, p1, p2 and p3 may be accessed in p as:

1 rk4_float p1, p2, p3;
2 p1 = p[0][0];
3 p2 = p[1][0];
4 p3 = p[1][1];
Parameters
tis the current time
jis the discrete time
xa constant array with the current state
ua constant array with current input
pa pointer to arrays of parameters. This allows the compatibility with the MATLAB System Identification Toolbox

Definition at line 223 of file libhybrid.h.

typedef void(* hyb_jump_map)(hyb_float *xp, hyb_float t, hyb_float j, const hyb_float *x, const hyb_float *u, const hyb_float **p)

Callback definition for the Jump Map.

Callback definition for the Output Map.

The callback stores the output in the first pointer (xp). The function does not need to allocate nor free the output vector, but may overflow if exceedes the dimension that is declared in the hyb_opts structure. When accessing an element of the parameter, please remember that the implementation keeps in mind a MATLAB-like interface. If the parameters (in MATLAB) are passed like:

1 f(t, x, u, p1, [p2, p3])

then, in the C code, p1, p2 and p3 may be accessed in p as:

1 rk4_float p1, p2, p3;
2 p1 = p[0][0];
3 p2 = p[1][0];
4 p3 = p[1][1];
Parameters
xpan array in which the result will be stored
tis the current time
jis the discrete time
xa constant array with the current state
ua constant array with current input
pa pointer to arrays of parameters. This allows the compatibility with the MATLAB System Identification Toolbox

The callback stores the output in the first pointer (y). The function does not need to allocate nor free the input vector, but may overflow if exceedes the dimension that is declared in the hyb_opts structure. When accessing an element of the parameter, please remember that the implementation keeps in mind a MATLAB-like interface. If the parameters (in MATLAB) are passed like:

1 f(t, x, u, p1, [p2, p3])

then, in the C code, p1, p2 and p3 may be accessed in p as:

1 rk4_float p1, p2, p3;
2 p1 = p[0][0];
3 p2 = p[1][0];
4 p3 = p[1][1];
Parameters
xpan array in which the result will be stored
tis the current time
jis the discrete time
xa constant array with the current state
ua constant array with current input
pa pointer to arrays of parameters. This allows the compatibility with the MATLAB System Identification Toolbox

Definition at line 137 of file libhybrid.h.

typedef hyb_bool(* hyb_jump_set) (hyb_float t, hyb_float j, const hyb_float *x, const hyb_float *u, const hyb_float **p)

Callback definition for the Jump Set.

The callback should return hyb_true if the combination of state, input and parameters enters the jump set. When accessing an element of the parameter, please remember that the implementation keeps in mind a MATLAB-like interface. If the parameters (in MATLAB) are passed like:

1 f(t, x, u, p1, [p2, p3])

then, in the C code, p1, p2 and p3 may be accessed in p as:

1 rk4_float p1, p2, p3;
2 p1 = p[0][0];
3 p2 = p[1][0];
4 p3 = p[1][1];
Parameters
tis the current time
jis the discrete time
xa constant array with the current state
ua constant array with current input
pa pointer to arrays of parameters. This allows the compatibility with the MATLAB System Identification Toolbox

Definition at line 195 of file libhybrid.h.

typedef struct hyb_opts hyb_opts

Options structure for the hybrid system.

typedef void(* hyb_out_map) (hyb_float *y, hyb_float t, hyb_float j, const hyb_float *x, const hyb_float *u, const hyb_float **p)

Definition at line 226 of file libhybrid.h.

Enumeration Type Documentation

enum hyb_bool

libhybrid boolean type is actually an enum

This enum is the returned value for the Jump Set function and the Flow Set function.

Enumerator
hyb_false 

False value

hyb_true 

True value

Definition at line 73 of file libhybrid.h.

73  {
74  hyb_false = 0,
75  hyb_true
76 } hyb_bool;
hyb_bool
libhybrid boolean type is actually an enum
Definition: libhybrid.h:73
Enumerator
HYB_SUCCESS 

Step seems good!

HYB_EMALLOC 

During the step there was an allocation error

HYB_NULLPTR 

Reveived a null pointer

HYB_GENERIC 

Unknown error generated

HYB_TLIMIT 

Reached time limit

HYB_JLIMIT 

Reached step limit

HYB_NOJUMP 

Invalid jump condition (Both c = 0 and d = 0). Not implemented

Definition at line 258 of file libhybrid.h.

Function Documentation

void hyb_flow_map_wrapper ( hyb_float dx,
hyb_float  tau,
const hyb_float x,
const hyb_float u,
const hyb_float **  p,
void *  vopts 
)

Internal callback for discretization step.

Warning
This is for internal use only, do not use directly. This callback has been implemented in order to respond to rk4_ode requirements.
Parameters
dxoutput of the callback
tauhybrid time step, is probably different with respect to the evolution time
xthe current state, contains time and jump state
uthe current control
pthe parameter vector of vectors
voptsa void pointer for user space, is used for passing by the options struct

Definition at line 66 of file libhybrid.c.

66  {
67  dx[0] = 1.0;
68  dx[1] = 0.0;
69  HYB_GET_OPTS(vopts)->F(dx + 2, x[0], x[1], x + 2, u, p);
70 }
#define HYB_GET_OPTS(S)
Definition: libhybrid.h:255
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.

The main loop of the hybrid system performs the following operations:

  1. Check if stop criteria is reached
  2. Check if the jump conditions are respected.
    • if jump is requsted, it enters a loop of update until no jump should be performed
    • else the execution continues
  3. The
    Parameters
    optspointer to an option structure
    yvector that will contain the next output. It must be already allocated
    xpvector that will contain the next state (already integrated in case of flowing step). It must be already allocated
    tauintegration engine time
    xcurrent state
    ucurrent input
    pparameter vector
    Returns
    an exit codes, as described in hyb_errorcode

Definition at line 31 of file libhybrid.c.

31  {
32 
33  opts->Y(y, x[0], x[1], x + 2, u, p);
34 
35  if (x[0] >= opts->T_horizon)
36  return HYB_TLIMIT;
37  if (x[1] >= opts->J_horizon)
38  return HYB_JLIMIT;
39 
40  hyb_bool d = opts->D(x[0], x[1], x + 2, u, p);
41  #if HYB_JUMP_LOGIC == 2
42  hyb_bool c = opts->C(x[0], x[1], x + 2, u, p);
43  #endif
44 
45  hyb_bool it_jumps;
46  #if HYB_JUMP_LOGIC == 1
47  it_jumps = d;
48  #elif HYB_JUMP_LOGIC == 2
49  it_jumps = d && !c;
50  #endif
51 
52  if (it_jumps) {
53  xp[0] = x[0];
54  xp[1] = x[1] + 1.0;
55  opts->J(xp + 2, x[0], x[1], x + 2, u, p);
56  } else {
57  rk4_opts rk4_o = { opts->Ts, (opts->x_size + 2), hyb_flow_map_wrapper };
58  rk4_errorcode rk4_ret = rk4(&rk4_o, xp, tau, x, u, p, HYB_SEND_OPTS(opts));
59  if (rk4_ret) {
60  return (hyb_errorcode)rk4_ret;
61  }
62  }
63  return HYB_SUCCESS;
64 }
size_t x_size
Definition: libhybrid.h:244
hyb_errorcode
Definition: libhybrid.h:258
hyb_float J_horizon
Definition: libhybrid.h:247
hyb_jump_map J
Definition: libhybrid.h:249
hyb_jump_set D
Definition: libhybrid.h:251
hyb_bool
libhybrid boolean type is actually an enum
Definition: libhybrid.h:73
hyb_float T_horizon
Definition: libhybrid.h:246
hyb_out_map Y
Definition: libhybrid.h:250
#define HYB_SEND_OPTS(S)
Definition: libhybrid.h:256
hyb_flow_set C
Definition: libhybrid.h:252
void hyb_flow_map_wrapper(hyb_float *dx, hyb_float tau, const hyb_float *x, const hyb_float *u, const hyb_float **p, void *vopts)
Internal callback for discretization step.
Definition: libhybrid.c:66
hyb_float Ts
Definition: libhybrid.h:245