使用HDL Workflow Advisor将matlab代码转换为vhdl

时间:2016-03-07 16:33:14

标签: matlab simulink hdl-coder

我制作了一个Matlab程序,它可以检测到2个圆相互交叉并输出交叉点的坐标。现在,我正在尝试将代码转换为vhdl以用于FPGA实现。

我的代码中HDL Workflow Advisor中仍然存在错误的一个功能是:

function theta = angle2Points(p1,p2)
%ANGLE2POINTS Compute horizontal angle between 2 points
%
%   ALPHA = angle2Points(P1, P2),
%   Pi are either [1*2] arrays, or [N*2] arrays, in this case ALPHA is a 
%   [N*1] array. The angle computed is the horizontal angle of the line 
%   (P1 P2)
%   Result is always given in radians, between 0 and 2*pi.
%
%   See Also:
%   points2d, angles2d, angle3points, normalizeAngle, vectorAngle
%
%
% ---------

dp = zeros(1,2);
% angle of line (P2 P1), between 0 and 2*pi.
dp = p2 - (size(p2, 1) * p1)
theta = mod(atan2(dp(:,2), dp(:,1)) + 2*pi, 2*pi)

错误:

  1. 变量'p1'。不支持可变大小的数据。
  2. 变量'p2'。不支持可变大小的数据。
  3. 变量'theta'。不支持可变大小的数据。
  4. 使用一个小测试文件来模拟传入的数据:

    % P = [x,y]
    P1 = [0,3];
    P2 = [5,10];
    f=angle2Points(P1,P2);
    
    P1 = [0,3];
    P2 = [5,3];
    f2=angle2Points(P1,P2
    

    在Workflow Advisor中,我收到:不支持可变大小的数据 - 第1行出错。

    我理解这是因为像C这样的静态类型语言必须能够在编译时确定变量属性,而在Matlab中它是动态发生的。

    我想要一些关于如何正确重写代码以使其准备就绪的简单函数的帮助。

    提前致谢

1 个答案:

答案 0 :(得分:0)

Coder抱怨是因为你有一个可变的数字输入以及变量大小输入(P1可以是2x2或1x2)。您需要做的是编写您的函数,使其 {/ strong> p1p2并且它们是已知大小(1 x 2)。

function theta = angle2Points(p1, p2)    
    % angle of line (P2 P1), between 0 and 2*pi.
    dp = p2 - (size(p2, 1) * p1)
    theta = mod(atan2(dp(:,2), dp(:,1)) + 2*pi, 2*pi)
end

您还需要更新调用此函数的所有函数,以便它们兼容。

作为旁注,除非将数据放入循环结构中,否则不必预先分配变量。如果您只是一次性分配整个变量(即上面的dp),则不需要任何预分配。