control_sys/lib.rs
1#![warn(missing_docs)]
2/*!
3# control-sys
4
5**control-sys** is a control system library written in Rust. It implements tools to represent and analyze LTI systems using state-space model.
6
7## Examples
8
9### Continuous state space model
10
11A continuous state space model can be defined with `ContinuousStateSpaceModel`. As an example, a state-space model representing a DC motor can be defined this way. We are using `nalgebra` to represent the matrices:
12
13```rust
14use nalgebra as na;
15use control_sys::model;
16
17// DC motor parameters
18let b = 0.1f64;
19let j = 0.01f64;
20let k = 0.01f64;
21let l = 0.5f64;
22let r = 1.0f64;
23
24let mat_ac = na::dmatrix![
25 -b / j, k / j;
26 -k / l, -r / l;
27 ];
28let mat_bc = na::dmatrix![0.0;
29 1.0 / l];
30let mat_cc = na::dmatrix![1.0, 0.0];
31
32let cont_model = model::ContinuousStateSpaceModel::from_matrices(&mat_ac, &mat_bc, &mat_cc, &na::dmatrix![]);
33```
34
35### Continuous to discrete model conversion
36
37A `DiscreteStateSpaceModel` can be built from a continuous one. You then need to specify the sampling step `ts`:
38
39```rust
40use nalgebra as na;
41use control_sys::model;
42
43// DC motor parameters
44let b = 0.1f64;
45let j = 0.01f64;
46let k = 0.01f64;
47let l = 0.5f64;
48let r = 1.0f64;
49
50let mat_ac = na::dmatrix![
51 -b / j, k / j;
52 -k / l, -r / l;
53 ];
54let mat_bc = na::dmatrix![0.0;
55 1.0 / l];
56let mat_cc = na::dmatrix![1.0, 0.0];
57
58let cont_model = model::ContinuousStateSpaceModel::from_matrices(&mat_ac, &mat_bc, &mat_cc, &na::dmatrix![]);
59
60let discrete_model =
61 model::DiscreteStateSpaceModel::from_continuous_ss_forward_euler(&cont_model, 0.05);
62```
63
64### Step response
65
66You can compute the step response of a system. For a discrete system, the simulation steps are given by the sampling step of the discretization:
67
68```rust
69use nalgebra as na;
70use control_sys::{model, simulator};
71
72// DC motor parameters
73let b = 0.1f64;
74let j = 0.01f64;
75let k = 0.01f64;
76let l = 0.5f64;
77let r = 1.0f64;
78
79let mat_ac = na::dmatrix![
80 -b / j, k / j;
81 -k / l, -r / l;
82 ];
83let mat_bc = na::dmatrix![0.0;
84 1.0 / l];
85let mat_cc = na::dmatrix![1.0, 0.0];
86
87let cont_model = model::ContinuousStateSpaceModel::from_matrices(&mat_ac, &mat_bc, &mat_cc, &na::dmatrix![]);
88
89let discrete_model =
90 model::DiscreteStateSpaceModel::from_continuous_ss_forward_euler(&cont_model, 0.05);
91
92// where model implements the traits `StateSpaceModel` and `Discrete`
93let duration = 10.0; // [s]
94let (response, input, states) = simulator::step_for_discrete_ss(
95 &discrete_model,
96 duration,
97 );
98```
99
100You can also compute the step response for a continuous model. You will need to provide the sampling step and the model will be discretized before computing the step response:
101
102```rust
103use nalgebra as na;
104use control_sys::{model, simulator};
105
106// DC motor parameters
107let b = 0.1f64;
108let j = 0.01f64;
109let k = 0.01f64;
110let l = 0.5f64;
111let r = 1.0f64;
112
113let mat_ac = na::dmatrix![
114 -b / j, k / j;
115 -k / l, -r / l;
116 ];
117let mat_bc = na::dmatrix![0.0;
118 1.0 / l];
119let mat_cc = na::dmatrix![1.0, 0.0];
120
121let cont_model = model::ContinuousStateSpaceModel::from_matrices(&mat_ac, &mat_bc, &mat_cc, &na::dmatrix![]);
122
123// where model is a continuous state space model
124let sampling_dt = 0.05; // [s]
125let duration = 10.0; // [s]
126let (response, input, states) = simulator::step_for_continuous_ss(
127 &cont_model,
128 sampling_dt,
129 duration,
130 );
131```
132
133### Controllability
134
135The controllability of a system can be evaluated using the `is_ss_controllable` method:
136
137```rust
138use nalgebra as na;
139use control_sys::{model, analysis};
140
141let ss_model = model::DiscreteStateSpaceModel::from_matrices(
142 &nalgebra::dmatrix![1.0, -2.0;
143 2.0, 1.0],
144 &nalgebra::dmatrix![1.0;
145 2.0],
146 &nalgebra::dmatrix![],
147 &nalgebra::dmatrix![],
148 0.05,
149 );
150
151let (is_controllable, controllability_matrix) = analysis::is_ss_controllable(&ss_model);
152
153if is_controllable
154{
155 println!("The system is controllable");
156 println!("Its controllability matrix is: {}", controllability_matrix);
157}
158```
159
160
161
162 */
163
164/// Methods to analyze control systems
165pub mod analysis;
166
167/// Structures to represent control systems
168pub mod model;
169
170/// Methods to simulate control systems in time-space
171pub mod simulator;
172
173/// Methods to generate trajectories
174pub mod trajectory;