MATLAB: Is the measured latency of the A/D block from the xPC Target 2.7.2 (R14SP2) library in the model greater than the sample time

a/dd/adelayeinputlatencymultiorderportsampleSimulink Real-Timesortedtime

My Simulink model contains multi channel inputs and the latency that I observe is higher than the sample time. I see out of order values on some of my input ports.

Best Answer

The reason you observe an unexpected amount of latency in your model is because the blocks are not being executed in the order you expect.
1. Turn on the "Sorted order" display from "Format -> Block displays -> Sorted order" and make sure that the execution order is set correctly. For example, if a D/A Write Block is being executed before an A/D Read Block, this can introduce a full sampletime of delay in addition to the execution delay so the delay will be the range exectime+T to exectime+2T.
a. Open up the Block Parameters and click on the "Enable input/output port" checkbox.
Doing this with the connections in place connects the E-port on the driver block to the E-port on the driven block. The rising edge can occur any time between two sample times and the output will always occur on the sample time. The minimum occurs if the edge occurred just before the model runs and the maximum occurs if the edge is just after the model runs. Look for the minimum time. That should be on the order of 10-15 microseconds and the maximum will be:
SampleTime + (10 to 15) microseconds
The D/A Write Block only has the direct feedthrough property for the E input, not for the data inputs. The E-port does not have any data associated with it, but you may have to route it through the model in order to force the desired block update order. This allows the user to force either order by the way the E inputs and outputs are strung together but it leaves the order unconstrained if the E input is not used. Always check the sorted order of the blocks and add or manipulate dependencies until you get the order you need.
2. Use the attached model:
delaytest.mdl
This model was implemented using Simulink 6.3 (R14SP3).
To run a test, feed a square wave into the inputs on the ADADIO and connect the selected lines to the 4 output ports. With a square wave input, the sample time hits will vary in relation to the square wave rise or fall by up to the sample time. The phase is random.
Connect an external digital multitrace scope with trace 1 connected to the input waveform, and traces 2 and 3 connected to outputs 1 and 4, you should see what you expect for delays.
An edge of the input may occur either just before or just after a sample time hit or anywhere in between. With all 8 channels being read and all 4 outputs being written, on a 800 Mhz PIII target machine, you should get a TET of 54 microseconds and a minimum delay from input to output of 41 microseconds. The maximum delay is this minimum delay plus the sampletime or 141 microseconds with a sampletime of 100 microseconds. You should be seeing a delay of the execution time of the model as a minimum up to execution time plus sampletime. This is perfectly normal and expected.
If the input waveform is not a square wave, then the delay appears to be a bit more ambiguous but is in fact exactly the same. There is a range of delays that is equal to the sampletime.