libhybrid
A library for discretized Hybrid Dynamical Systems
libhybrid.c
Go to the documentation of this file.
1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2  * Copyright 2018 - Matteo Ragni, Matteo Cocetti - University of Trento
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20  * SOFTWARE.
21  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
22 
29 #include "libhybrid.h"
30 
31 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) {
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 }
65 
66 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) {
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 }
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
size_t x_size
Definition: libhybrid.h:244
hyb_errorcode
Definition: libhybrid.h:258
hyb_float J_horizon
Definition: libhybrid.h:247
Options structure for the hybrid system.
Definition: libhybrid.h:242
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
#define HYB_GET_OPTS(S)
Definition: libhybrid.h:255
hyb_float Ts
Definition: libhybrid.h:245
HYB_FLOAT_TYPE hyb_float
Definition: libhybrid.h:62