1
\$\begingroup\$

In a computer control system (microcontroller-based), the sampling time of the inputs to the system determine whether to treat the system as a discrete system or continuous. From what I have learnt, when \$t_s \rightarrow 0\$, you can treat the discrete control system as a continuous time system.

enter image description here

My question is, how do I implement what an op-amp integrator and differentiator would do on a microcontroller.

Differentiation - Can I simply calculate the gradient between adjacent sampled values, or is it more complicated?

Integration - Similarly, can I add up all the previous values which have been sampled?

\$\endgroup\$
5
  • 1
    \$\begingroup\$ That is close to correct. For differentiation, you need to divide that difference by the sample period. For integration, you need to multiply by the sample period. If the sample period is sufficiently small, these will be very good approximations. \$\endgroup\$
    – caveman
    Commented Jan 19, 2015 at 4:50
  • \$\begingroup\$ Your "(but continuous time)" qualification is wrong. A digital (micro-controller) system is discreet in both time and value, you can't avoid that. \$\endgroup\$ Commented Jan 19, 2015 at 7:52
  • \$\begingroup\$ Are you trying to make simple digital filters? \$\endgroup\$
    – Andy aka
    Commented Jan 19, 2015 at 8:55
  • \$\begingroup\$ @Andyaka No, I'm trying to understand how you'd program a digital PID controller with small sampling times \$\endgroup\$
    – tgun926
    Commented Jan 19, 2015 at 10:28
  • \$\begingroup\$ @WoutervanOoijen removed it from the title \$\endgroup\$
    – tgun926
    Commented Jan 19, 2015 at 10:28

3 Answers 3

1
\$\begingroup\$

Integration is easy especially if each sample is the same time apart, just keep adding each new sample to the integration value. Note you may need to deal with integral wind-up where your integral gets massive. This is especially true if you are doing a PID controller.

For the differential taking the difference between sample n and sample n-1 is the very first step. You may need to consider noise though, if you have a lot or your system is to sensitive you may wish to filter it.

\$\endgroup\$
2
  • \$\begingroup\$ Any tips on how to deal with the wind-up bit? \$\endgroup\$
    – tgun926
    Commented Jan 19, 2015 at 10:31
  • \$\begingroup\$ There are two approaches that you can use, hard limits and time limits. 1) Hard limit: Have a max and min that the integration value can get to. 2) Time limit: Keep an array of large number of samples and each cycle add them up. 2 is memory intensive but could be 'better'. 1 is simple and I use it to kick a dirastic state change and reset the loop. \$\endgroup\$
    – dan
    Commented Jan 20, 2015 at 7:48
1
\$\begingroup\$

It sounds like what you're trying to do is implementing standard numerical calculations. No need to try and mimic the analog version of it, just apply the standard formulae for both operations. For differentiation $$\frac{f(x+t_0)-f(x)}{t_0}$$ In other words, the difference between the sample divided by the sampling time. For the integration you need to multiply the incoming sample with the sampling time, and add it to the last result of you're integrator, like so: $$y[n] = y[n-1] + t_0*x[n-1]$$ There are more methods of integration and differentiation, but these are pretty standard ones. The lower the sampling time is and the quicker your microcontroller is, the more you approximate continuous-time scenario.

\$\endgroup\$
1
\$\begingroup\$

My question is, how do I implement what an op-amp integrator and differentiator would do on a microcontroller.

An op-amp integrator has a CR time constant and the digital equivalent is this: -

enter image description here

Because the op-amp integrator also inverts, the "sign change" should be built into the digital circuit if you want to be exact.

A true differentiator (mathematically) will amplify noise dramatically in a simple digital differentiator so I'd consider using an integrator (as described above) and subtract its output from the input to obtain "differentiation". For very slow moving inputs, the integrator output will equal the input hence the subtracted output will be zero. For very fast input signals, the integrator output will be very small and the differentiated output will be almost the same magnitude as the input.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.