Real-Time Workshop User's Guide | ![]() ![]() |
Accumulators
Simulink recognizes the block diagram shown in Figure 8-8 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 8-8: 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 8-9.
Figure 8-9: Block Reduction Option
With the Block reduction option on, Simulink creates a synthesized block, Sum_sythesized_accumulator. This synthesized block replaces the block diagram of Figure 8-8, resulting in a simple increment calculation.
/* Compute block outputs */ void MdlOutputs(int_T tid) { /* UnadornAccum Block: <Root>/Sum_sythesized_accumulator */ rtB.Sum_sythesized_accumulator++; /* Outport Block: <Root>/Out1 */ rtY.Out1 = rtB.Sum_sythesized_accumulator; }
With Block reduction turned off, the generated code reflects the block diagram more literally, but less efficiently.
/* Compute block outputs */ void MdlOutputs(int_T tid) { /* local block i/o variables */ real_T rtb_Unit_Delay; /* UnitDelay Block: <Root>/Unit Delay */ rtb_Unit_Delay = rtDWork.Unit_Delay_DSTATE; /* Sum Block: <Root>/Sum */ rtB.Sum = rtC_Constant + rtb_Unit_Delay; /* Outport Block: <Root>/Out1 */ rtY.Out1 = rtB.Sum; } /* Perform model update */ void MdlUpdate(int_T tid) { /* UnitDelay Block: <Root>/Unit Delay */ rtDWork.Unit_Delay_DSTATE = rtB.Sum; }
![]() | Look-Up Tables and Polynomials | Use of Data Types | ![]() |