-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibhybrid.h
302 lines (282 loc) · 11.5 KB
/
libhybrid.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Copyright 2018 - Matteo Ragni, Matteo Cocetti - University of Trento
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#ifndef LIBHYBRID_H_
#define LIBHYBRID_H_
/**
* @mainpage
* @author Matteo Ragni, Matteo Cocetti
* @date 10 Jan 2018
*
* This library implements an hybrid system in the form:
*
* \f{align}
* \dot{t}(\tau) = & 1 & \text{for } (t, j, x, u) \in C \\
* \dot{j}(\tau) = & 0 & \\
* \dot{x}(\tau) = & f(t, j, x, u, p) & \\
* \f}
* \f{align}
* t^+(\tau) = & t & \text{for } (t, j, x, u) \in D \\
* j^+(\tau) = & j + 1 & \\
* x^+(\tau) = & g(t, j, x, u, p) & \\
* \f}
* \f{align}
* y = & h(x, u, p)
* \f}
* where:
* * \f$ f \f$ is the flow map;
* * \f$ g \f$ is the jump map;
* * \f$ h \f$ is the output map;
* * \f$ C \f$ is the flow set;
* * \f$ D \f$ is the jump set.
* * \f$ p \f$ are the function parameters.
* * \f$ \tau \f$ is an engine time for the integration of \f$ t \f$ and \f$ j \f$.
*
* The flow map is discretized with a Runge Kutta 4 step. For the evolution of
* the system, both \f$ t \f$ and \f$ j \f$ are limited by horizons.
*/
#ifndef HYB_FLOAT_TYPE
#define HYB_FLOAT_TYPE double /**< Precision defaut is double */
#endif
typedef HYB_FLOAT_TYPE hyb_float; /**< Precision typedef, used in library */
#define RK4_FLOAT_TYPE HYB_FLOAT_TYPE /**< Forcing consistent types */
#include "librk4/librk4.h"
/**
* @brief libhybrid boolean type is actually an enum
*
* This enum is the returned value for the Jump Set function and the
* Flow Set function.
*/
typedef enum hyb_bool {
hyb_false = 0, /**< False value */
hyb_true /**< True value */
} hyb_bool;
/**
* @brief 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:
* @code
* f(t, x, u, p1, [p2, p3])
* @endcode
* then, in the C code, p1, p2 and p3 may be accessed in p as:
* @code
* rk4_float p1, p2, p3;
* p1 = p[0][0];
* p2 = p[1][0];
* p3 = p[1][1];
* @endcode
* @param xdot an array in which the result will be stored
* @param t is the current time
* @param j is the discrete time
* @param x a constant array with the current state
* @param u a constant array with current input
* @param p a pointer to arrays of parameters. This allows the compatibility with the
* MATLAB System Identification Toolbox
*/
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);
/**
* @brief Callback definition for the Jump 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:
* @code
* f(t, x, u, p1, [p2, p3])
* @endcode
* then, in the C code, p1, p2 and p3 may be accessed in p as:
* @code
* rk4_float p1, p2, p3;
* p1 = p[0][0];
* p2 = p[1][0];
* p3 = p[1][1];
* @endcode
* @param xp an array in which the result will be stored
* @param t is the current time
* @param j is the discrete time
* @param x a constant array with the current state
* @param u a constant array with current input
* @param p a pointer to arrays of parameters. This allows the compatibility with the
* MATLAB System Identification Toolbox
*/
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);
/**
* @brief Callback definition for the Output Map
*
* 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:
* @code
* f(t, x, u, p1, [p2, p3])
* @endcode
* then, in the C code, p1, p2 and p3 may be accessed in p as:
* @code
* rk4_float p1, p2, p3;
* p1 = p[0][0];
* p2 = p[1][0];
* p3 = p[1][1];
* @endcode
* @param xp an array in which the result will be stored
* @param t is the current time
* @param j is the discrete time
* @param x a constant array with the current state
* @param u a constant array with current input
* @param p a pointer to arrays of parameters. This allows the compatibility with the
* MATLAB System Identification Toolbox
*/
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);
/**
* @brief 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:
* @code
* f(t, x, u, p1, [p2, p3])
* @endcode
* then, in the C code, p1, p2 and p3 may be accessed in p as:
* @code
* rk4_float p1, p2, p3;
* p1 = p[0][0];
* p2 = p[1][0];
* p3 = p[1][1];
* @endcode
* @param t is the current time
* @param j is the discrete time
* @param x a constant array with the current state
* @param u a constant array with current input
* @param p a pointer to arrays of parameters. This allows the compatibility with the
* MATLAB System Identification Toolbox
*/
typedef hyb_bool (*hyb_jump_set)(hyb_float t, hyb_float j, const hyb_float *x, const hyb_float *u, const hyb_float **p);
/**
* @brief 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:
* @code
* f(t, x, u, p1, [p2, p3])
* @endcode
* then, in the C code, p1, p2 and p3 may be accessed in p as:
* @code
* rk4_float p1, p2, p3;
* p1 = p[0][0];
* p2 = p[1][0];
* p3 = p[1][1];
* @endcode
* @param t is the current time
* @param j is the discrete time
* @param x a constant array with the current state
* @param u a constant array with current input
* @param p a pointer to arrays of parameters. This allows the compatibility with the
* MATLAB System Identification Toolbox
*/
typedef hyb_bool (*hyb_flow_set)(hyb_float t, hyb_float j, const hyb_float *x, const hyb_float *u, const hyb_float **p);
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);
/**
* @brief Jump logic implementation
*
* There are two possible jump logic implementation:
* 1. Precedence on the jump map (default)
* 2. Precedence on the flow map
*/
#ifndef HYB_JUMP_LOGIC
#define HYB_JUMP_LOGIC 1
#endif
/**
* @brief Options structure for the hybrid system
*/
typedef struct hyb_opts {
size_t y_size; /**< Output size */
size_t x_size; /**< State size */
hyb_float Ts; /**< Time step */
hyb_float T_horizon; /**< Maximum continuous time horizon */
hyb_float J_horizon; /**< Maximum discrete step horizon */
hyb_flow_map F; /**< Flow map function pointer */
hyb_jump_map J; /**< Jump map function pointer */
hyb_out_map Y; /**< Output map function pointer */
hyb_jump_set D; /**< Jump set function pointer */
hyb_flow_set C; /**< Flow set function pointer */
} hyb_opts;
#define HYB_GET_OPTS(S) ((hyb_opts *)S) /**< Converts the void pointer in option struct pointer. For internal use only */
#define HYB_SEND_OPTS(S) ((void *)S) /**< Converts the option struct in void pointer. For internal use only */
typedef enum hyb_errorcode {
HYB_SUCCESS = 0, /**< 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 */
} hyb_errorcode;
/**
* @brief 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
* @param opts pointer to an option structure
* @param y vector that will contain the next output. It must be already allocated
* @param xp vector that will contain the next state (already integrated in case of flowing step). It must be already allocated
* @param tau integration engine time
* @param x current state
* @param u current input
* @param p parameter vector
* @return an exit codes, as described in hyb_errorcode
*/
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);
/**
* @brief 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.
* @param dx output of the callback
* @param tau hybrid time step, is probably different with respect to the evolution time
* @param x the current state, contains time and jump state
* @param u the current control
* @param p the parameter vector of vectors
* @param vopts a void pointer for user space, is used for passing by the options struct
*/
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);
#endif /* LIBHYBRID_H_ */