Array position offset

I need to offset the y coordinate 0,.2,-.4,0,.2,-4… So on the first pass I have no offset, second pass needs an offset .2in and on the third pass -.4in lastly on the fourth pass I need 0 offset so on and so forth. I want to learn how to use an array in this manner. Any ideas???

Hi RichPorras,

You can try the following:

MODULE Module1
! Current pass. Starting value = 1
PERS num pass:=1;
! Offset array
CONST num Y_OFFSETS{3}:=[0,0.2,-0.4];
! Target to modify
PERS robtarget myRobtarget:=[[100,200,300],[1,0,0,0],[0,0,0,0],[9E+09,9E+09,9E+09,9E+09,9E+09,9E+09]];

PROC main()
VAR num offset;

WHILE TRUE DO
! Get offset from array based on current pass
offset:=Y_OFFSETS{3};
IF ((pass MOD Dim(Y_OFFSETS,1)) > 0) offset:=Y_OFFSETS{pass MOD (Dim(Y_OFFSETS,1))};

! Apply offset to target
myRobtarget.trans.y:=myRobtarget.trans.y+offset;

! Next pass
Incr pass;
ENDWHILE
ENDPROC
ENDMODULE

If you can, try to avoid changing your reference target by getting a new offset target from the reference target. And Rapid arrays use one-based indexing so you have to adjust for it when calculating the array index when using MOD to cycle around the array.

MODULE Sandbox

    ! Current pass. Starting value = 1
    VAR num pass:=1;

    ! Offset array
    CONST num Y_OFFSETS{3}:=[0,0.2,-0.4];

    ! Reference target. A constant to avoid modifying it due to rounding errors or programming errors, etc.
    CONST robtarget reference_robtarget:=[[100,200,300],[1,0,0,0],[0,0,0,0],[9E+09,9E+09,9E+09,9E+09,9E+09,9E+09]];

    ! Offset target
    VAR robtarget offset_robtarget;

    PROC SandboxMain()

        VAR num y_offsets_index;
        VAR num y_offsets_length;

        y_offsets_length:=dim(Y_OFFSETS,1);

        WHILE TRUE DO

            ! Calculate the array index. (One-based indexing, so subtract 1 from pass, then add 1 after MOD.)
            y_offsets_index:=((pass-1) MOD y_offsets_length)+1;

            ! Calculate the offset from the reference
            offset_robtarget:=offs(reference_robtarget,0,Y_OFFSETS{y_offsets_index},0);

            ! Next pass
            Incr pass;

        ENDWHILE

    ENDPROC

ENDMODULE

Thank you very much!