codesysstethercat

CoDeSys - Unable to write to internal variable


I'm currently working on a demo project from LinMot. The problem I ran into is that I'm unable to write the values I get of the fieldbus to the variables I have to work with.

enter image description here

As you can see in the image I'm able to grab the correct values of the fieldbus. Ie line 30: StateVar is the value I expected. However after the bitshift uistate should be 8. You can see the same behaviour in line 32 and 36.

At first I thought that I might be overwriting uistate in a different line but it's the only time I try to write to the variable. Also I can't make changes to the code itself.

Does someone have a clue what's happening here?

Edit:

This is the main:

// **************************************************************************
// Init axis parameters on program first run
// **************************************************************************
IF NOT bFirstRun THEN
    LM_Init_Axis_1();   // Init LMAxis_1 parameters
    
    bFirstRun := TRUE;
END_IF

// **************************************************************************
// Read inputs of a LinMot EtherCAT (-EC) drive
// **************************************************************************
LMAxis_1_Axis.DrvToPlc := FC_LM_ReadEC(LMAxis_1_AdrIN);


// **************************************************************************
// General Code
// **************************************************************************

LMAxis_1_Power(Axis:=LMAxis_1_Axis);    // This function block must run cyclically as it updates the data in the axis struct (tstLM_Axis)
LMAxis_1_Reset(Axis:=LMAxis_1_Axis);
LMAxis_1_Home(Axis:=LMAxis_1_Axis);

LMAxis_1_TorqueLimiting(Axis:=LMAxis_1_Axis);

LMAxis_1_ParaAccess(Axis:=LMAxis_1_Axis);

LM_Example_Axis_1(); // Call example program for init and cyclic positioning


// **************************************************************************
// Write outputs of a LinMot EtherCAT (-EC) drive
// **************************************************************************
FC_LM_WriteEC(LMAxis_1_AdrOUT, LMAxis_1_Axis.PlcToDrv);

In main LM_Example_Axis_1 is called. LM_Example_Axis_1 consists of three parts. Initilisation, error handling and controling a servo drive.

This is the initilisation part of LM_Example_Axis_1:

(* Init *)
InitTrig(CLK:=Init);
IF InitTrig.Q AND NOT LMAxis_1_Axis.Status.Error THEN
    InitState:=1;
    InitDone := FALSE;
END_IF

CASE InitState OF
    0:  InitState := InitState;

    1:  IF NOT LMAxis_1_Power.Status THEN
            LMAxis_1_Power.Enable := TRUE;
        ELSE
            InitState := 2;
        END_IF

    2:  IF NOT LMAxis_1_Axis.Status.Homed THEN
            LMAxis_1_Home.Execute := TRUE;
        ELSE
            LMAxis_1_Home.Execute := FALSE;
            InitState := 3;
        END_IF

    3:  IF LMAxis_1_Axis.Status.Homed AND LMAxis_1_Power.Status THEN
            InitDone := TRUE;
            InitState := 0;
            Init := FALSE;
        END_IF
END_CASE

I get stuck on InitState = 1 and the reason is that LMAxis_1_Power.Status does not return true. The screen shot in the original post is from the class LMAxis_1_Power. To make it more clear here is also the variable declaration part.

enter image description here


Solution

  • So as it turns out @Fred you where correct. After a lot of testing I found the solution.

    I'm indeed overwriting the values of the fieldbus. Because as it turns out the demo project was not natively designed to run on a Raspberry Pi. On the native platform you have to make an active attempt to read of and write to the fieldbus.

    However if you convert the platform to Raspberry Pi Codesys already maps your inputs from and outputs to the fieldbus. So by reading of the fieldbus you overwrite the already gained values.

    tl;dr

    In main remove those lines:

    LMAxis_1_Axis.DrvToPlc := FC_LM_ReadEC(LMAxis_1_AdrIN);
    [...]
    FC_LM_WriteEC(LMAxis_1_AdrOUT, LMAxis_1_Axis.PlcToDrv);
    

    then things work out on the Raspberry Pi.