# Combining MTK standard library StateSpace with other Blocks

**URL:** <https://discourse.julialang.org/t/combining-mtk-standard-library-statespace-with-other-blocks/127322>\
**Category:** General Usage\
**Tags:** modelingtoolkit, mtk, controlsystemsmtk\
**Created:** [March 24, 2025, 9:37pm UTC](https://discourse.julialang.org/t/combining-mtk-standard-library-statespace-with-other-blocks/127322 "2025-03-24T21:37:03Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![BambOoxX](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bambooxx/32/22179_2.png) [@BambOoxX](https://discourse.julialang.org/u/BambOoxX)\
**Post date:** [March 24, 2025, 9:37pm UTC](https://discourse.julialang.org/t/combining-mtk-standard-library-statespace-with-other-blocks/127322/1 "2025-03-24T21:37:03Z")

</div>

I’m currently trying to integrate a linear state-space model inside a ModelingToolkit model.  
For this purpose I used the [`ODESystem`](https://github.com/JuliaControl/ControlSystemsMTK.jl/blob/758ea1f1e444c427fa5ae1e9903e7b7598720bed/src/ode_system.jl#L8-L43) specialization provided by [ControlSystemsMTK](https://github.com/JuliaControl/ControlSystemsMTK.jl), which itself uses the [`StateSpace`](https://github.com/SciML/ModelingToolkitStandardLibrary.jl/blob/a3bdab308353063c9ca2eddbf2820f8e56c0ef5e/src/Blocks/continuous.jl#L520-L583) block provided by [MTK-SL](https://github.com/SciML/ModelingToolkitStandardLibrary.jl).

However, once the `ODESystem` is formed, its “interfaces” are `RealInput`s / `RealOutput`s, which feel a bit abstract to me.

Are there any examples around showing how to combine such systems with e.g. `Flange`, `MechanicalPort`, `Pin` or else, to give them proper “physical” interfaces and make connections with other blocks easier ?

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [March 25, 2025, 6:27am UTC](https://discourse.julialang.org/t/combining-mtk-standard-library-statespace-with-other-blocks/127322/2 "2025-03-25T06:27:11Z")

</div>

The `StateSpace` block has inputs and outputs in the “signal domain”, that is, a generic domain not tied to any physical domain. To use these signals in a physical doman, one uses interface components like sensors (physical → signal), and special input components (signal → physical), such as `Voltage, Torque, Force` etc. These input components take a signal input and outputs the corresponding physical quantity.

Here’s an example, where the interface components are `SpeedSensor`, `Voltage`, and `Torque`

> **[DC Motor with Speed Controller · ModelingToolkitStandardLibrary.jl](https://docs.sciml.ai/ModelingToolkitStandardLibrary/stable/tutorials/dc_motor_pi/)**
>
> Documentation for ModelingToolkitStandardLibrary.jl.

---

<div class="post-metadata">

**Author:** ![BambOoxX](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bambooxx/32/22179_2.png) [@BambOoxX](https://discourse.julialang.org/u/BambOoxX)\
**Post date:** [March 26, 2025, 12:45pm UTC](https://discourse.julialang.org/t/combining-mtk-standard-library-statespace-with-other-blocks/127322/3 "2025-03-26T12:45:19Z")

</div>

Thanks ! I figured theses sensor blocks would be of use, I’ll try ASAP !

---

<div class="post-metadata">

**Author:** ![BambOoxX](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bambooxx/32/22179_2.png) [@BambOoxX](https://discourse.julialang.org/u/BambOoxX)\
**Post date:** [December 29, 2025, 1:47am UTC](https://discourse.julialang.org/t/combining-mtk-standard-library-statespace-with-other-blocks/127322/4 "2025-12-29T01:47:19Z")

</div>

Hi. I’m coming back at this (long delayed, due to more pressing matters) issue. Looking again at your answer, I’m wondering if it is the right approach given the following additional context :

I’m trying to combine a state-space model of a mechanical dynamical system with mechanical components of the standard library.

To achieve that

1. I assembled the state space model using the capabilities of `ControlSystemsMTK`, which provides me with an `ODESystem`. This system has its inputs (`RealInput` vector matching forces) and outputs (`RealOutput` vector matching displacements).
2. As some of the forces are actually redundant relationships and should be imposed at 0, I added some equations to reflect that using basically `f_n ~ 0` for each redundant force.
3. Assuming I have the same input and output nodes of my finite element model, I figured I should have `Flanges` to which I would connect the other mechanical blocks. However, as you pointed, I should make the proper conversions since I only have “signals”.

That is where I am actually blocked.

I tried the following with no luck so far :

- manually create `Flange`s and connect their `s` port to the outputs and `f` port to the inputs
- connect these `Flange`s to the the external mechanical block flange, say a spring.

or

- add `ForceSensor`s and `DisplacementSensor`s to this model
- connect their flanges together (since they are on the same node),
- connect the `ForceSensor` `output` to the force input of the state-space model
- connect the `PositionSensor` `output` to the position output of the state-space model
- connect their flanges together (since they are on the same node), along with the external mechanical block flange.

Do you have any suggestions about this ? I am short on ideas… Thanks.

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [December 29, 2025, 5:56am UTC](https://discourse.julialang.org/t/combining-mtk-standard-library-statespace-with-other-blocks/127322/5 "2025-12-29T05:56:12Z")

</div>

An alternative approach could be to directly incorporate the linear equations in some custom component, bypassing the `StateSpace` component.

I was once working on something similar to what I think you are doing  
[https://github.com/JuliaComputing/Multibody.jl/compare/main...sb#diff-7b57a9d4675844bf244fd005347a0ccad38a154cb12ebbe7c8d9c2bc5846ac58](https://github.com/JuliaComputing/Multibody.jl/compare/main...sb#diff-7b57a9d4675844bf244fd005347a0ccad38a154cb12ebbe7c8d9c2bc5846ac58)  
I haven’t yet pursued it to completion though so it’s not usable, but perhaps you can pick up some idea from there.

---

<div class="post-metadata">

**Author:** ![BambOoxX](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bambooxx/32/22179_2.png) [@BambOoxX](https://discourse.julialang.org/u/BambOoxX)\
**Post date:** [January 9, 2026, 2:42pm UTC](https://discourse.julialang.org/t/combining-mtk-standard-library-statespace-with-other-blocks/127322/6 "2026-01-09T14:42:18Z")

</div>

That is great ! It’s very similar to what I’m trying to do. Many thanks !
