Simulink中的循环缓冲区

时间:2013-06-19 14:34:07

标签: code-generation simulink

我想在纯Simulink模型中实现一个非常大的(10 ^ 6元素 - 固定大小)循环缓冲区(没有其他工具箱,没有S函数)。

在某些方面,我需要阅读一些元素(在任何地方,不仅仅是开始或结束)。

我不能使用以下解决方案:

  1. “队列阻止”或“缓冲区阻止”(我没有可用的信号处理工具箱)
  2. “离散延迟”(我需要一个休眠缓冲区,不会在模型中放置10 ^ 6个延迟)
  3. “Sim Events”(我需要从此模型生成代码
  4. 我还没试过“S-Function”,我正在寻找替代解决方案。

    您知道进一步的方法吗?

2 个答案:

答案 0 :(得分:4)

可以使用MATLAB Fcn块创建一个简单的循环缓冲区:

function y = CircularBuffer(u, N)
%#codegen
% Function to implement a simple N element circular buffer

% Define the internal buffer variable as persistent so
% so that it will maintain its values from one time step
% to the next.
persistent buffer writeIdx

% Initialize the buffer the first time through
if isempty(buffer)
    % Initialize the buffer
    buffer = zeros(N,1);
    writeIdx = 1;
else
    buffer(writeIdx) = u;
    if (writeIdx == N)
        writeIdx = 1;
    else
        writeIdx = writeIdx+1;
    end
end

% Output the buffer
y = buffer;

这完全支持代码生成(并且不执行memcpy)。

如果需要,您可以轻松更改缓冲区的数据类型,但如果您想对输入和输出信号采用不同的采样率(与信号处理模块组中的缓冲区一样),那么您需要恢复为使用S函数。

答案 1 :(得分:2)

首先,让我赞美你对纯Simulink的追求!但这很可能,Mathworks代码生成器无法处理此用例。它非常草率,并创建整个队列的缓冲副本。这是一个例子:

Top Level Model Read Operation Write Operation

然后,看看一些代码。糟糕!

/* Model output function */
static void queue_output(int_T tid)
{
  {
    real_T rtb_MathFunction;

    /* DataStoreRead: '<S1>/Data Store Read' */
    memcpy((void *)(&queue_B.DataStoreRead[0]), (void *)(&queue_DWork.A[0]),
           100000U * sizeof(uint16_T));

    /* Outport: '<Root>/Out1' incorporates:
     *  DataStoreRead: '<S1>/Data Store Read1'
     *  Selector: '<S1>/Selector'
     */
    queue_Y.Out1 = queue_B.DataStoreRead[(int32_T)queue_DWork.idx];

    /* If: '<Root>/If' incorporates:
     *  ActionPort: '<S2>/Action Port'
     *  Constant: '<Root>/Constant'
     *  SubSystem: '<Root>/WriteToQueue'
     */
    if (queue_P.Constant_Value > 0.0) {
      /* DataStoreRead: '<S2>/Data Store Read3' */
      memcpy((void *)(&queue_B.Assignment[0]), (void *)(&queue_DWork.A[0]),
             100000U * sizeof(uint16_T));

      /* Math: '<S2>/Math Function' incorporates:
       *  Constant: '<S2>/Constant1'
       *  Constant: '<S3>/FixPt Constant'
       *  DataStoreRead: '<S2>/Data Store Read2'
       *  Sum: '<S3>/FixPt Sum1'
       */
      rtb_MathFunction = rt_mod_snf(queue_DWork.idx +
        queue_P.FixPtConstant_Value, queue_P.Constant1_Value);

      /* Assignment: '<S2>/Assignment' incorporates:
       *  Constant: '<S2>/Constant'
       */
      queue_B.Assignment[(int32_T)rtb_MathFunction] = queue_P.Constant_Value_h;

      /* DataStoreWrite: '<S2>/Data Store Write' */
      memcpy((void *)(&queue_DWork.A[0]), (void *)(&queue_B.Assignment[0]),
             100000U * sizeof(uint16_T));

      /* DataStoreWrite: '<S2>/Data Store Write1' */
      queue_DWork.idx = rtb_MathFunction;
    }
  }

Memcpy 10000 uint16的每个循环!在Mathworks以健壮的方式解决此问题之前,这里是initial attempt that requires hard coding indices,S函数是唯一的方法。

相关问题