Array in Simulink

Discussion in 'Programmer's Corner' started by lph65724, Jul 2, 2008.

  1. lph65724

    Thread Starter Member

    Jul 2, 2008
    14
    0
    Hi all,

    I am working on project using simulink, but I can't access the elements in the array in the user-defined block the way I do in m-file. Is there anyway I can access the elements in an array in Simulink? Thank you
     
  2. roddefig

    Active Member

    Apr 29, 2008
    149
    0
    You'll probably want to represent your data as a frame. Can you give some more detail on what you're trying to do?
     
  3. lph65724

    Thread Starter Member

    Jul 2, 2008
    14
    0
    Thank you for the reply

    I am trying to control a robot arm. I can move the arm already, but I wanna make the arm go to wherever when I just enter the coordinates. So far, I have written a piece of code in the user-defined block to do the inverse kinematic calculation, but the possible angles are stored in an array, so I need to be able to read all possible angles in order to determine which one is valid.
     
  4. roddefig

    Active Member

    Apr 29, 2008
    149
    0
    Ahh, ok. Take a look at the "Selector" block in the Simulink library under "Signal Routing". You can use this block to manipulate data in and out of the array.
     
  5. lph65724

    Thread Starter Member

    Jul 2, 2008
    14
    0
    Thank you for the input
    It seems that I have to specify the size of the array in the selector block, but the # of the possible angles could be 0, 1, 12, or even infinite, so the # of possible angles is different each time the coordinates are changed. Therefore, there is no way for me to know the size of the array in advance. Is there other ways to do it? Thank you
     
  6. roddefig

    Active Member

    Apr 29, 2008
    149
    0
    I would modify your function to output the possible angles one at a time. I'm not sure if there's a good way to handle an arbitrarily sized array in Simulink. Why do you need to use Simulink? It sounds like an m-function would be the best way to handle your data.
     
  7. lph65724

    Thread Starter Member

    Jul 2, 2008
    14
    0
    I don't think I can do that. I have determine which is valid first because some calculated angles may exceed the robot's limits. Also, it must be efficient, too. I don't know what else I can do about it

    Because I am using a data acquisiton board to read the position of the arm and to send signals to the arm, so I can only use real-time windows target in Simulink to do it
     
    Last edited: Jul 4, 2008
  8. Dave

    Retired Moderator

    Nov 17, 2003
    6,960
    144
    Have you considered using an Embedded Matlab Function (available under "User-Defined Functions")?

    This implements a standard Matlab M-file function through which you can pass arguments to and retrieve arguments from. The M-code can interrogate the array.

    Without knowing a little about your system/model I cannot suggest much more.

    Dave
     
  9. lph65724

    Thread Starter Member

    Jul 2, 2008
    14
    0
    Hi Dave,
    Thank you for your suggestions. I am actually using Embeded Matlab Function already, but it doesn't support the subscript the way we usually use for arrays in M-file. I have got rid of this problem by using fsolve command instead of solve since my system is nonlinear. However, since I am not familiar with fsolve, I still have some errors that I need to fix. Do you know to use fsolve in Embedded Matlab Function? Any advise will be greatly appreciated Thank you
     
  10. Dave

    Retired Moderator

    Nov 17, 2003
    6,960
    144
    I have used fsolve before. What are the errors? Also it would be helpful to see your code.

    Dave
     
  11. lph65724

    Thread Starter Member

    Jul 2, 2008
    14
    0
    Hi Dave,

    The following is my code

    function [th1, th2]= fcn(targetx, targety)
    % This block supports an embeddable subset of the MATLAB language.
    % See the help menu for details.

    %[theta1, theta2] = fsolve(@(theta) [25 * cos(theta(1)) + 15 * sin(theta(1) + theta(2)) - 29; 25 * sin(theta(1)) + 15 * sin(theta(1) + theta(2)) - 20],[0;180]);

    eml.extrinsic('fsolve');

    theta = complex (zeros(1,2,'single'));
    theta = fsolve(@(theta) [25 * cos(theta(1)) + 15 * sin(theta(1) + theta(2)) - 40; 25 * sin(theta(1)) + 15 * sin(theta(1) + theta(2)) - 0],[-5;-5])

    th1 = theta1;
    th2 = theta2;

    And the error messages

    1st one
    Message: Assertin Error, Source: Unknown, Reported by: Stateflow, Summary: 0
    2nd one
    Message: Build Error, Source: inversekinematics, Reported by: Stateflow, Summary: Error in ' inversekinematics/Embedded MATLAB Function/ SFunction ' while executing C MEX S-function ' sf_sfun', (mdlSetInputPortDimensionInfo), at time 0. MATLAB error message:-->Stateflow Assertion Error : (SLSF Diagnostic)0.

    Please take a look. Thank you so much
     
  12. Dave

    Retired Moderator

    Nov 17, 2003
    6,960
    144
    lph65724, let me have a look at the code in Matlab tomorrow (I don't have access to Matlab right now). I assume the over-writing the variable theta with the complex and fsolve functions is a coding error? Otherwise variables theta1 and theta2 are never created.

    Dave
     
  13. Dave

    Retired Moderator

    Nov 17, 2003
    6,960
    144
    I've had a play around with your code. If you correct the syntax errors I pointed about above for theta/theta1/theta2 then the code runs in Matlab. I cannot replicate your errors and can find nothing of use on the Matlab KB database.

    The problem I encounter was when you port the code to an Embedded Matlab function in Simulink the fsolve function is not supported - there is a parsing error (or possibly a restriction) - so you cannot go this route for fsolve.

    Can I suggest you try the following: Use two concurrent Matlab Fcn blocks (under User-Defined Functions > Matlab Fcn). Your Matlab function will be the complex and fsolve function statement in your earlier code, and replace the variable theta with u (this is the Matlab Fcn block convention); return the output dimension as theta1 and theta2 for each of the complex and fsolve functions respectively.

    Does this overcome the issue?

    Dave
     
  14. lph65724

    Thread Starter Member

    Jul 2, 2008
    14
    0
    Hi Dave,
    Thank you so much for your help!
    I can't try anything yet because my computer is down. I will try it when I get my computer back and let you know asap. Thank you so much


    Po
     
  15. Dave

    Retired Moderator

    Nov 17, 2003
    6,960
    144
    No problem.

    Dave
     
Loading...