Question

I'm developing an embedded application for motor control and I have doubts on what to to with some calculations that transform ADC numeric values into signed physical values (Amps).

Should I make the calculations in the interrupt handler or should I just use the handler to set a global flag and make the calculations in the main loop when the flag has been set?

Thank you in advance.

Was it helpful?

Solution

There is no one-size-fits-all answer for this. It depends upon your timing requirements, existing interrupt load, etc.

If it's a fairly simple multiply or something, then I would just do it in the interrupt and move on.

Just make sure you know what you are doing and do not spend more time in your interrupt than you should.

OTHER TIPS

The less work you can do in your interrupt handler, the better. Right now it might not be a big deal for your application, but there will come a time when your system load is higher and you won't want to hold off other higher-priority tasks to handle interrupts. You might want to look into semaphores and proper multithreading rather than just setting global flags.

It depends on what you want to do with the ADC values, and how fast you need to do it. If you need to take some time-critical action based on the ADC values, you should deal with that in the interrupt. However, if this is the case, to save yourself the expense of first calculating the "real" analog value from the ADC counts, you should just express your critical thresholds in terms of the ADC counts.

For example, if your ADC outputs 8 counts per volt, and you need to fire an emergency stop if the ADC reports greater than 1.5 volts, you write your interrupt code to fire the E-stop at 12 counts. You could then still pass the ADC counts off to the main loop for translation into user-friendly units for user interface purposes, etc.

The general principle is that to guarantee you can meet your deadlines, you structure your application and any settings to make it as easy as possible to meet those deadlines. If something doesn't have a hard deadline, put it in the main loop (again, in general).

One thing to think about is data consistency. If you are getting several related values from the ADC (position, voltage, current, etc..) then you may want to do something to ensure you are working with a consistent set. This may mean that a background update is better than an update in the ISR. Even consistency of a single reading for a given pass of the algorithm may be important. Consider this background code.

  delta = data-lastData;
  //ISR could update data here.
  lastData = data;
  something = K1* data + K2 * delta;  //this may be wrong now
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top