- Notifications
You must be signed in to change notification settings - Fork441
Discrete-time control of a nonlinear continuous-time system#677
-
I am trying to apply state-feedback control using discrete-time SS controller to a system of two tanks described by differential equations. The process is thus modelled as NonlinearIOSystem without sampling time: The controller is discrete-time SS: The resulting closed-loop system (interconnect) does end with compile error: Is there any working example for such a setup or is it not implemented yet? In the latter case, is these any possible workaround? |
BetaWas this translation helpful?Give feedback.
All reactions
Replies: 4 comments 1 reply
-
I have been working for awhile on how to best do this in a general way in python-control. The challenge is that the controller outputs a zero-order-hold “staircase” function that is incompatible with the way variable-step numerical integrators work. (They jump back and forth). I have not yet figured out a way to force any of scipy’s integrators to pick only the time steps you specify, but I am not sure I have exhausted all options yet. In the meantime one way to simulate such a system is to repeatedly simulate your dynamical system (eg tanks2) over short intervals of This is cumbersome and slow though. What I have been working on is a an alternative that entails simple fixed-step Euler integration, at a short dt for accuracy, of the continuous-time plant. This entails creating a discrete-time model of the plant for a short dt, and a function that can create a I’ll post here if I get something working. |
BetaWas this translation helpful?Give feedback.
All reactions
-
FWIW, here's one approach, in which one calls This is fairly raw; I haven't thought at all about how to apply this to the IOSystem classes. importnumpyasnpfromscipy.integrateimportsolve_ivpimportmatplotlib.pyplotasplt# non-linear DEdefde(t,y,u):return-y-y**3+u# controller; discrete or continuous timedefprop_controller(r,y,k):returnk* (r-y)# desired final timetf_des=0.3# sample perioddt=1/10# number of samplesnsamples=max(5,int(np.ceil(tf_des/dt)))k=5# gainr=5# setpointy0=-1# initial state# discrete-time state (=output)yd=np.empty(nsamples)# discrete-time inputud=np.empty(nsamples)# initializeyd[0]=y0ud[0]=prop_controller(r,yd[0],k)fornsampleinrange(1,nsamples):# construct new DE function for each sample intervalf=lambdat,y:de(t,y,ud[nsample-1])sol=solve_ivp(f, [nsample*dt, (nsample+1)*dt], [yd[nsample-1]])yd[nsample]=sol.y[0,-1]ud[nsample]=prop_controller(r,yd[nsample],k)# continuous-time controlled systemdefcontrol_de(t,y,r,k):returnde(t,y,prop_controller(r,y,k))# continuous-time solutioncont_sol=solve_ivp(lambdat,y:control_de(t,y,r,k), [0,nsamples*dt], [y0])# compare continous-time and discrete-timeplt.clf()plt.stairs(yd,np.arange(nsamples+1)*dt,baseline=None)plt.plot(cont_sol.t,cont_sol.y[0]) |
BetaWas this translation helpful?Give feedback.
All reactions
-
At the end, I modified the solution by@roryyorke: created a helper function that correctly integrates within sampling time and a wrapper for nlsys that evaluates the model in discrete-time. This will give correct results at sampling times and can be used without further modifications in existing code. If a more detailed solution is needed between sampling times, it is easy to integrate the system using the resulting piece-wise constant closed-loop input and solve_ivp with more data points.
|
BetaWas this translation helpful?Give feedback.
All reactions
-
Thanks for posting update. I forgot to post here about this, but I eventually got around to submitting an example of what I wrote above, using a fixed step integrator. Downside is it is less accurate than variable-step, but one upside is that it is easier to also incorporate time delays. here is the example: https://python-control.readthedocs.io/en/0.10.1/simulating_discrete_nonlinear.html |
BetaWas this translation helpful?Give feedback.
All reactions
-
Yes, I studied the example in the docs and played originally with it as well. Discrete-time delays can indeed be modeled using state-space formulation. As you mention, the integration precision was less accurate and I wanted the exact results as I had in Simulink. In addition, this proposed solution requires only one small additional function and the whole code can remain the same - it integrates very well with the rest of the python-control package. |
BetaWas this translation helpful?Give feedback.