plccodesysst

The axis object cannot be used after being put into an array?


Codesys v3.5 sp20+(32bit)

I hope to power up the specified axis through the following code,It worked.

The following code is called cyclically,It's called by a task every 4 milliseconds.

PROGRAM PowerupTest
VAR
    MC_Power_0: MC_Power;
    Axises:ARRAY[0..5] OF SM3_Drive_ETC_DS402_CyclicSync.AXIS_REF_ETC_DS402_CS;
END_VAR
MC_Power_0(
    Axis:=Axis0, 
    Enable:=1 , 
    bRegulatorOn:=1 , 
    bDriveStart:=1 , 
    Status=> ,
    bRegulatorRealState=> , 
    bDriveStartRealState=> , 
    Busy=> , 
    Error=> , 
    ErrorID=> 
);

Now, to change it a little bit, I save Axis0 into an array first, and then pass the elements of the array into the MC_Power function block.
It doesn't work properly anymore.

Axises[0]:=Axis0;

MC_Power_0(
    Axis:=Axises[0],   //Changed here
    Enable:=1 , 
    bRegulatorOn:=1 , 
    bDriveStart:=1 , 
    Status=> ,
    bRegulatorRealState=> , 
    bDriveStartRealState=> , 
    Busy=> , 
    Error=> , 
    ErrorID=> 
);

Could you tell me the possible reason? I hope I can access all axes as an array.

Thank you.

=========================

I made the following changes, but it still doesn't work.

PROGRAM PowerupTest
VAR
    MC_Power_0: MC_Power;
    Axises:ARRAY[0..5] OF SM3_Drive_ETC_DS402_CyclicSync.AXIS_REF_ETC_DS402_CS;
    IsInitialized:BOOL:=false;
END_VAR
IF NOT IsInitialized THEN
    Axises[0]:=Axis0;
    IsInitialized:=TRUE;
    RETURN;
END_IF



MC_Power_0(
    Axis:=Axises[0], 
    Enable:=1 , 
    bRegulatorOn:=1 , 
    bDriveStart:=1 , 
    Status=> ,
    bRegulatorRealState=> , 
    bDriveStartRealState=> , 
    Busy=> , 
    Error=> GVL.LastError, 
    ErrorID=> GVL.LastErrorId
);

Solution

  • Problem solved, I should use a pointer to save the reference to the axis object

    PROGRAM PowerupTest
    VAR
        MC_Power_0: MC_Power;
        Axises:ARRAY[0..5] OF POINTER TO SM3_Drive_ETC_DS402_CyclicSync.AXIS_REF_ETC_DS402_CS;
        IsInitialized:BOOL:=false;
    END_VAR
    
    
    Axises[0]:=ADR(Axis0);
    
    MC_Power_0(
        Axis:=Axises[0]^, 
        Enable:=1 , 
        bRegulatorOn:=1 , 
        bDriveStart:=1 , 
        Status=> ,
        bRegulatorRealState=> , 
        bDriveStartRealState=> , 
        Busy=> , 
        Error=> , 
        ErrorID=> 
    );