在對simulink建模的過程中,有時候會遇到sample time出現錯誤的問題,比如下圖是我在使用simulink自帶的Recursive least square Estimator最小二乘估計器去估計質量和坡度的模型截圖。
但是在仿真的時候就會報錯,報錯內容如下:‘Error in port sample time. The "TestAlgorithmModel/Vehicle /Recursive Least Squares Estimator" block must operate at a discrete sample time. Either connect discrete-time signals to the "Regressors" and "Output" input ports of the block or explicitly provide a discrete time in the "Sample Time" setting in the block dialog.’
大致意思就是端口的采樣時間出錯了,必須得使用離散的采樣時間點。但其實我模型的solver(求解器)都已經設置為discrete了,而且我已經把用到了continuous中的各個模組全部替換成 discrete中的模組了(比如積分模塊,用的就是discrete中的 discrete-time integrator)。還是這樣報錯的情況下,只能另想他法了。
最后找到的解決辦法是,在估計器(Estimator)的輸入處加上Zero-Order hold模塊,如下圖所示,並且把Zero-Order hold的仿真時間設置的和離散采樣步長一樣就可以了,然后問題就真的解決了!
下面就來探討一下Zero-Order hold這個模塊的作用如何,網上有結論是說這玩意兒是把輸入信號每過一段時間跟新一次,並且保持到下一次采樣之前,相當於就是把信號離散化處理(也可以是重新修改信號的采樣時間),原話地址:https://zhidao.baidu.com/question/368276493483526644.html。
但是我們要追根溯源,https://ww2.mathworks.cn/help/simulink/slref/zeroorderhold.html,官方文件里說他是將具有連續采樣時間的輸入信號轉換為具有離散采樣時間的輸出信號。和它同樣功能的還有Unit delay 和memory,但是官方解釋感覺不太明白,於是參考:http://www.ece.northwestern.edu/CSEL/local-apps/matlabhelp/toolbox/simulink/slref/zeroorderhold.html,這篇里面解釋的就比較清楚了,Zero-Order Hold blocks between the fast-to-slow transitions.the sample rate of the Zero-Order Hold must be set to that of the slower block. For slow-to-fast transitions, use the Unit Delay block. 也就是說快轉慢用zero-order hold,慢轉快用Unit Delay。