# The process of implementing a discrete model in C++/Arduino

I have developed an embedded electronics system centered around the Arduino Due. In my application, I am controlling the force of the end effector of a rigid object using a DC motor. The feedback force sensor has a lot of dead-time and I am unable to get the performance I expect with a standard PID. I am now looking into model predictive control, however at first I will try to use a Smith predictor. To do this, we need to develop an internal model describing the system we have. I have never done this and I need some guidance on formulating a discrete model and implementing it in C++/Arduino.

So far, I developed a continuous-time model based off of input-output data using System Identification Toolbox in MATLAB. Then I performed appropriate discretization (zero-order hold) to get a discrete model, $H(z)$, in the z-domain. To implement this in C++/Arduino I believe I am supposed to perform the inverse z-transform to get $h[n]$. $h[n]$ is then written in a interrupt service routine (ISR) to get the model output.

For instance, my continuous model is:

$$H = \frac{1}{5s+10}$$

Using zero-order hold at 100 Hz, the discrete model is:

$$H_{zoh} = \frac{0.00198}{z-0.9802}$$

Sample time: 0.01 seconds

Using the z-transform table, the inverse $h[n]$ is:

$$h[n] = 0.00198 \cdot \mathcal{H}(n-1) \cdot 0.9802^{(n-1)}$$

So far, is what I have been doing on the right track? If so, how would I implement this in an ISR?

• The problem is you have three fairly different types of questions here: is your control system proposal sound, how one implements a discrete time controller on a small MCU, and specifically what that looks like in Arduino-style code. Commented Jun 6, 2016 at 17:37
• @ChrisStratton Out of the three problems you interpreted, "How one implements a discrete time model* on a small MCU" is the most appropriate. I understand how to implement in Arduino-style code as well as verifying if the control system is viable. Commented Jun 6, 2016 at 18:06
• I don't get it, why inverse? If you want implement the function you just have to use recursive formula tha you'll find for FIR filter, you have 1st order. Commented Jun 6, 2016 at 18:07

You can write your system as $H(z)=\dfrac{B(z)}{A(z)}$ the use a recursive formula for IIR filter where you have A and B coefficients. Before you have to change the form to have denominator like $A(z) = 1+a1\cdot z^{-1}+ a2\cdot z^{-2}+..$ by multiplying your form