RobotStudio event

Read data from array of robtargets

Hello,

I have an array of robtargets (Targets{6}). I am trying to read the position data from the targets and send those via Sockets.
Here is a snippet of my code:
CONST robtarget Targets{6}:=[[.......]]
VAR num k:=1;

WHILE k>=1 DO
            MoveJ Targets{k},v100,fine,ToolDataTest\WObj:=wobj0;
            sString:=ValToStr(Targets{k});
            SocketSend client_socket\Str:="\0A"+"*"+sString;
            WaitTime 2;
            k:=k+1;
ENDWHILE

The problem I am having is first, it says the string is too large, because it is trying to read all the robtargets as 1 string. What I am wanting is for it to send the values for whatever value "k" is. So if the value is Targets{3}, it will send the declared values of Targets{3} only. If I use CPOS or CROBTT, it will read the actual robot values, but I want to read what the commanded values are.
Any ideas?

Thanks for any help,
SM

Comments

  • Be aware of that the string has a max length of 80 characters
    Per Svensson
    Robotics and Vision Specialist
    Consat Engineering
  • Besides the max length of a string, i do not know the consequence of sending "\0" through sockets. In some languages this act as string-terminator.

    Another problem is your "while"-loop, it will never reach a stop-condition. Initial "k" is 1 and every iteration it will be increased with 1. So a better construction is the use of "Dim".

    FOR k FROM 1 TO Dim(Targets,1) DO
    ....
    ENDFOR
    

  • SteveMob
    SteveMob
    edited June 2016
    Thanks for the reply guys!

    John, like I said, this was only a snippet of my code. I had the "k" reset once it reached the end of the array. But I like the way you did it better, so I will change my code to use the "Dim" and FOR loop.

    In the case that anyone else ever tries to do this, what I did was just grab the individual elements of the target, and packed those separately into raw bytes:

    sCommTrans:="Commanded:"+" Position:"+ValToStr(Targets{k}.trans)+",";
    PackRawBytes sCommTrans,message,(RawBytesLen(message)+1)\ASCII;
    sCommQuat:=" Quaternions:"+ValToStr(Targets{k}.rot)+",";
    PackRawBytes sCommQuat,message,(RawBytesLen(message)+1)\ASCII;
    nComm_anglex:=EulerZYX(\X,Targets{k}.rot);
    nComm_angley:=EulerZYX(\Y,Targets{k}.rot);
    nComm_anglez:=EulerZYX(\Z,Targets{k}.rot);
    sComm_Rotationx:=NumToStr(nComm_anglex,6);
    sComm_Rotationy:=NumToStr(nComm_angley,6);
    sComm_Rotationz:=NumToStr(nComm_anglez,6);
    sCommEuler:=" Euler:"+"["+sComm_Rotationx+","+sComm_Rotationy+","+sComm_Rotationz+"]"+",";
    PackRawBytes sCommEuler,message,(RawBytesLen(message)+1)\ASCII;
    SocketSend client_socket\RawData:=message;

    Thanks for the help everybody!

    SM