# State space feedback

**URL:** https://discourse.julialang.org/t/state-space-feedback/92416
**Category:** General Usage
**Tags:** controlsystems
**Created:** [January 2, 2023, 1:37pm UTC](https://discourse.julialang.org/t/state-space-feedback/92416 "2023-01-02T13:37:20Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![lev96jeff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lev96jeff/32/45533_2.png) [@lev96jeff](https://discourse.julialang.org/u/lev96jeff)
#### Post date: [January 2, 2023, 1:37pm UTC](https://discourse.julialang.org/t/state-space-feedback/92416/1 "2023-01-02T13:37:20Z")

</div>

Hello everyone!

I am new to julia and trying to model with 1 input and 2 output.  
my problem is that i am unable to create a feedback with a pid controller in the forward path, even if i create the feedback the (first output to the first input) the 2nd output got lost.

A =  
-0.06617428571428571 19.852285714285713 4.2 0.0 -0.0033551169590643275 -0.033551169590643276 0.0  
0.0 0.0 0.0 0.0 0.0 0.0 0.0  
0.0 0.0 0.0 0.0 0.0 0.0 0.0  
0.05413766434648106 0.0 0.0 -0.05413766434648106 0.0 0.0 0.0  
0.0 0.0 0.0 0.0 0.0 1.0 0.0  
0.0 0.0 0.0 0.0 0.0 0.0 1.0  
0.0 0.0 0.0 0.0 0.0 -22.22222222222222 -6.666666666666667  
B =  
0.0  
0.0  
0.0  
0.0  
0.0  
0.0  
1.0  
C =  
1.0 0.0 0.0 0.0 0.0 0.0 0.0  
0.0 0.0 0.0 1.0 0.0 0.0 0.0  
D =  
0.0  
0.0

this is system and the pid together.

C=pid(Kp, Ki, Kd; form=:parallel, Tf=0.3, state\_space=true)

back=series(C, Model)

Controlled=feedback(back[1,1])

could anybody help me out, thanks in advance!

---

<div class="post-metadata">

### Author: ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)
#### Post date: [January 2, 2023, 2:00pm UTC](https://discourse.julialang.org/t/state-space-feedback/92416/2 "2023-01-02T14:00:55Z")

</div>

A couple of proposals:

1. It is customary (and much better) to give (Julia) code within a block of three starting back-ticks “`” followed by keywoard “julia”, i.e., “````julia”, and then close the code block by three backticks “```”. That would make part of your code look as follows:

```julia
C=pid(Kp, Ki, Kd; form=:parallel, Tf=0.3, state_space=true)
back=series(C, Model)
Controlled=feedback(back[1,1])

```

1. I’m not an expert on the control package (Fredrik is answering right now, I think). But what is your chosen values for `Kp`, `Ki`, and `Kd`?
2. Remember: with two outputs and one input, you have to choose which output you want integral action for.
3. A `pid` controller is normally not considered “state space feedback”. Still, “pd” control with filtered output is somewhat similar to state space feedback with a state estimator – for the very specific case when the system is a double integrator.
4. Along this line (#4), one could in principle consider LQG control as a particularly tuned PD controller in the multivariable case. If one one augments the system with states being the integral of output deviation and put weight on these in the cost function of LQG control, then this would be some sort of generalized multivariable PID controller.
5. In any way, it is not possible to have more integral actions than inputs.

---

<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: [January 2, 2023, 2:02pm UTC](https://discourse.julialang.org/t/state-space-feedback/92416/3 "2023-01-02T14:02:15Z")

</div>

Hello and welcome to the forum!

Please read [Please read: make it easier to help you](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757) to learn how to quote your code, and try to include code for an example that runs 🙂

When you index `back[1,1]` you get a SISO system, you must somehow account for the second output of `Model` before you call `feedback`, i.e., by making `C` a 1x2 system?

Alternatively, if you want `C` to operate on one of the outputs only, but keep the second output when you form the closed-loop system, you need to use the advanced interface to `feedback` where you specify which inputs and outputs are involved in the feedback, and which are considered external.

```julia
  feedback(sys1::AbstractStateSpace, sys2::AbstractStateSpace;
           U1=:, Y1=:, U2=:, Y2=:, W1=:, Z1=:, W2=Int[], Z2=Int[],
           Wperm=:, Zperm=:, pos_feedback::Bool=false)

  Basic use feedback(sys1, sys2) forms the feedback
  interconnection

             ┌──────────────┐
  ◄──────────┤ sys1 │◄──── Σ ◄──────
      │ │ │ │
      │ └──────────────┘ -1
      │ |
      │ ┌──────────────┐ │
      └─────►│ sys2 ├──────┘
             │ │
             └──────────────┘

  Advanced use feedback also supports more flexible use according
  to the figure below

                ┌──────────────┐
        z1◄─────┤ sys1 │◄──────w1
   ┌─── y1◄─────┤ │◄──────u1 ◄─┐
   │ └──────────────┘ │
   │ α
   │ ┌──────────────┐ │
   └──► u2─────►│ sys2 ├───────►y2──┘
        w2─────►│ ├───────►z2

```

in your case, you would have to provide `Z1 = :` and `Y1=[1]` or `Y1=[2]` depending on which output is fed back to the controller, e.g.,

```julia
feedback(Model, C, Y1=[1])

```

---

<div class="post-metadata">

### Author: ![lev96jeff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lev96jeff/32/45533_2.png) [@lev96jeff](https://discourse.julialang.org/u/lev96jeff)
#### Post date: [January 2, 2023, 3:32pm UTC](https://discourse.julialang.org/t/state-space-feedback/92416/4 "2023-01-02T15:32:54Z")

</div>

Hi!

Thanks for the quick reply!  
I am trying to control the temperature inside a relatively small space (approximately 1m3) with a perltier modul.

My proposed control setup would be the following.

 ![Untitled](https://global.discourse-cdn.com/julialang/original/3X/6/5/65926049f20f7b3b920c8e5a9ede0eb68242c676.png)

I think that your suggestion place the controller in the feedback path instead of the forward path.

Thanks in advance!

---

<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: [January 2, 2023, 3:36pm UTC](https://discourse.julialang.org/t/state-space-feedback/92416/5 "2023-01-02T15:36:24Z")

</div>

You’re right, to place it in the forward path you’d have to use `P*C` as `sys1` (like you correctly did) and `ss(1)` as `sys2`

```julia
feedback(P*C, ss(1), Y1=[1])

```

---

<div class="post-metadata">

### Author: ![lev96jeff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lev96jeff/32/45533_2.png) [@lev96jeff](https://discourse.julialang.org/u/lev96jeff)
#### Post date: [January 3, 2023, 7:44am UTC](https://discourse.julialang.org/t/state-space-feedback/92416/6 "2023-01-03T07:44:01Z")

</div>

Worked,thx!

i have checked, the [juliacontrol.github.io](http://juliacontrol.github.io) site, but i could not fully understand all the feedback function’s arguments. if you are planning to make a new video of this tool, i would definitely watch it, if it contains a more specific usage of this function (E.g. a mimo plant with a mimo feedback).

tbh, the tool all in all is awesome, great job!

---

<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: [January 3, 2023, 8:02am UTC](https://discourse.julialang.org/t/state-space-feedback/92416/7 "2023-01-03T08:02:39Z")

</div>

Thanks for the feedback 🙂  
I do in fact have a number of videos in the pipeline, I hope they’ll be out within a month or so. In the meantime, there is a possibility to make use of named signals and call the function `connect` to form more complicated feedback interconnections. The docs for `connect` are here  
[https://juliacontrol.github.io/RobustAndOptimalControl.jl/dev/api/#RobustAndOptimalControl.connect-Tuple{Any}](https://juliacontrol.github.io/RobustAndOptimalControl.jl/dev/api/#RobustAndOptimalControl.connect-Tuple%7BAny%7D)  
there’s also an example making use of this functionality here  
[https://juliacontrol.github.io/RobustAndOptimalControl.jl/dev/hinf\_connection/](https://juliacontrol.github.io/RobustAndOptimalControl.jl/dev/hinf_connection/)
