Real-Time Workshop | ![]() ![]() |
Accumulators
Simulink recognizes the block diagram shown in Figure 9-13 as an accumulator. An accumulator construct -- comprising a Constant block, a Sum block, and feedback through a Unit Delay block -- is recognized anywhere across a block diagram, or within subsystems at lower levels.
Figure 9-13: An Accumulator Algorithm
By using the Block reduction option, you can significantly optimize code generated from an accumulator. Turn this option on in the Advanced page of the Simulink Simulation parameters dialog, as shown in Figure 9-14.
Figure 9-14: Block Reduction Option
With the Block reduction option on, Simulink creates a synthesized block, Sum_synth_accum. This synthesized block replaces the block diagram of Figure 9-13, resulting in a simple increment calculation.
void MdlOutputs(int_T tid) { /* UnadornAccum Block: <Root>/Sum_synth_accum */ rtB.Sum_synth_accum++; /* Outport Block: <Root>/Out1 */ rtY.Out1 = rtB.Sum_synth_accum; }
With Block reduction turned off, the generated code reflects the block diagram more literally, but less efficiently.
void MdlOutputs(int_T tid) { /* Expression for <Root>/Sum incorporates: */ /* Constant Block: <Root>/Constant */ /* UnitDelay Block: <Root>/Unit Delay */ /* Sum Block: <Root>/Sum */ rtB.Sum = 1.0 + rtDWork.Unit_Delay_DSTATE; /* Outport Block: <Root>/Out1 */ rtY.Out1 = rtB.Sum; }
![]() | Look-Up Tables and Polynomials | Use of Data Types | ![]() |