We currently do not have state-space filtering implemented within CDS. However, we can run a custom C-code, and the procedure to add a user C-code is described in the RCG developer's guide (see T080135).
State-space models have certain advantages over ZPK models.
- MIMO vs SISO: they can handle multi-input multi-output unlike ZPK.
- Time domain filtering is straightforward. For ZPK, we normally discretize using bilinear transform, convert to second-order sections, and then do the filtering.
- Numerical stability: In ZPK, if specific poles and zeros are very close to each other, they can lead to cancellations and numerical inaccuracies.
- Physical interpretation: Since the state variables (say, for a pendulum model) have physical significance, it is more intuitive.
- Useful for studying the dynamics (& response to control feedback) of an equivalent Digital Twin model of the actual physical plant. Useful to detect changes in the actual plant.
- Adaptive filtering: It is way easier to tweak the filter coefficients of [A,B,C,D] matrices of the state-space controller.
Christopher Wipf previously wrote a routine to convert discrete state-space Matlab models to a CDS-compatible C function where the [A,BC,D] matrices are stored in the corresponding header files. I modified the code to work for any ZPK or StateSpace models. Users have the option to initialize the filter coefficients. I set up a 2-input 2-output filter model consisting of two separate lowpass filters, each with corner frequencies (0.5 Hz & 10 Hz), and checked the results on two inputs (random Gaussian noise in this case) and compared it with the expected one. They both match well.
Next steps:
- Currently the filter coefficients are specified within the header files. Any change would require model recompilation. But it would be convinient if the [A,B,C,D] matrix values were read from epics records and sent as inputs to the cds C function block. Auto-generation of medm [A,B,C,D] matrices would make life easier.
- Implement & run Regina's double pendulum state-space model within CDS.
- Use this same idea to create a CDS C-code AI library (ex: blocks for neural nets, classic ML classifiers, regressors).
Notes:
# While compiling with custom C-code using cdsFunctionCall block,
- # Add path to CDS C-Code Folder (or add to ~/.bashrc) before building the model
- export CDS_SRC=/opt/rtcds/userapps/cds_user_apps/trunk/cds/common/src
- Make sure to remove any commented-out C-function blocks. These for some reason, are detected in the rtbuild process, although it only throws a warning.