0

I've made a Matlab program where it detects if 2 circles intersect each other and outputs the coordinates of the intersections. Now, I'm trying to convert the code into vhdl for FPGA implementation.

One of the functions within my code where there is still an error in the HDL Workflow Advisor is:

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)

The errors:

  1. Variable 'p1'. Variable-Size data is not supported.
  2. Variable 'p2'. Variable-Size data is not supported.
  3. Variable 'theta'. Variable-Size data is not supported.

With a little test file to simulate the incoming data:

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

P1 = [0,3];
P2 = [5,3];
f2=angle2Points(P1,P2

Within the Workflow Advisor I receive the: Variable-Size data is not supported - error on line 1.

I understand this is because statically-typed languages like C must be able to determine variable properties at compile time, while within Matlab it happens dynamically.

I'd like some help with this simple function on how to correctly rewrite the code to make it hdl ready.

Thanks in advance

user3488736
  • 109
  • 1
  • 3
  • 13

1 Answers1

0

Coder is complaining because you have both a variable number of inputs as well as variable size inputs (P1 can be either 2x2 or 1x2). What you need to do is write your function so that it requires both p1 and p2 and that they are a known size (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

You will also need to update all the functions that call this function so that they are compatible.

As a side note, you don't have to pre-allocate variables unless you are putting data into them in a loop structure. If you're simply assigning the entire variable at once (i.e. dp above), you don't need any pre-allocation.

Suever
  • 64,497
  • 14
  • 82
  • 101
  • Well, in an other function I initialize the variables by giving them default values. But still the same error occurs – user3488736 Mar 07 '16 at 17:33
  • @user3488736 How did you do that? MATLAB has no concept of default values. – Suever Mar 07 '16 at 17:33
  • @user3488736 Please put that code in your question not in your comments...I can't read it – Suever Mar 07 '16 at 17:48
  • @user3488736 you're still using `varargin` as the input sizes are not consistent. As I mention after my code, consider **requiring** two inputs both [1 x 2] arrays. Then coder should ahve no issues. Update all calling functions to pass two inputs. – Suever Mar 07 '16 at 17:53
  • Yes indeed, I will apply this tonight and let you know. Thanks for your patience and help! – user3488736 Mar 07 '16 at 17:56
  • Function 'exist' is not supported for code generation. Consider adding coder.extrinsic('exist') at the top of the function to bypass code generation. Also adding at the top of the function doesn't work. – user3488736 Mar 07 '16 at 18:14
  • @user3488736 yup that's what I figured. Answer is updated – Suever Mar 07 '16 at 18:43
  • But even if I do as you recommend, Coder gives error on line 1 for p1,p2 and theta. How should I write my function so that it requires both p1 and p2 and that they're a size (1 x 2). I don't really understand – user3488736 Mar 07 '16 at 19:34
  • @user3488736 can you update your question with the code that you've written as well as a full error message. – Suever Mar 07 '16 at 19:34
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/105601/discussion-between-suever-and-user3488736). – Suever Mar 07 '16 at 20:18