On Mon, 31 Jan 2005 14:51:45 -0500, Olin Lathrop wrote: > William Couture wrote: > > The PID calculations MUST be in the ISR to reduce timing > > jitter as much as possible > > No, only the measurement of the inputs and the writing of the output. The > computation only needs to be done between these and can jitter all it wants > as long as the results are computed in time to write them to the outputs. No, I meant what I wrote. If the PID calculation was done in the background loop, there would be a variable number of instructions between the setting of the DO_IT_NOW flag and the start of the PID update. The effect of this is that, while in the long run the PID was updated at 2Khz, there would be a considerable jitter in the timing, which showed up in the accuracy of position hold. If the PID calculation was done in the ISR, the jitter was much smaller, and the accuracy of the position hold was much better. As for the idea of "read inputs in ISR, set outputs in ISR, do calculations in background" that doesn't always work very well. In our application, phase lag was a serious problem. There can also be a problem with when interrupts occur. Since there are a limited number of timers to play with and we have to keep things on schedule, the "best" solutions are: 1) interrupt is at 2Khz, read current inputs and set outputs based on previous interrupt. Unacceptable due to phase lag. 2) interrupt is at 4Khz, alternating interrupts read inputs and set outputs. Better (though not very good) phase lag, but interrrupt overhead is getting significant. 3) interrupt is a higher multiple of 2Khz, reading inputs and setting outputs on appropriate interrupts. Good resolution on phase lag, but interrupt overhead has changed timing (increased phase lag) with respect to just doing the entire PID in the isr. So, though it does not appeal to you, the entire PID was done in the ISR. If you want to debate it, I'll pass you over to our control systems engineer. Bill -- http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist