pcdshub / lcls-twincat-motion

TwinCAT 3 Motion Control Utilities - PLC Motion Library for all PCDS Applications
https://pcdshub.github.io/lcls-twincat-motion
Other
35 stars 19 forks source link

ENH: FB_StateSetupHelper #184

Closed ZLLentz closed 1 year ago

ZLLentz commented 1 year ago

Description

Create FB_StateSetupHelper, a tool to make it less tedious and less error prone to set up large arrays of position states. In short, this lets you set defaults for your state fields and then override them on on a state-by-state basis.

Example usage from readme:

VAR
    bStateSetup: FB_StateSetupHelper;
    stDefault: ST_PositionState := (
        fDelta := 0.5,
        fVelocity := 10,
        bMoveOk := TRUE,
        bValid := TRUE
    );
    astStates1: ARRAY[1..GeneralConstants.MAX_STATES] OF ST_PositionState;
    astStates2: ARRAY[1..GeneralConstants.MAX_STATES] OF ST_PositionState;
    astStates2: ARRAY[1..GeneralConstants.MAX_STATES] OF ST_PositionState;
END_VAR

fbStateSetup(stPositionState:=stDefault, bSetDefault:=TRUE);

fbStateSetup(stPositionState:=astStates1[1], sName:='OUT', fPosition:=10);
fbStateSetup(stPositionState:=astStates1[2], sName:='YAG', fPosition:=20);
fbStateSetup(stPositionState:=astStates1[3], sName:='TT', fPosition:=30);

fbStateSetup(stPositionState:=astStates2[1], sName:='OUT', fPosition:=-30);
fbStateSetup(stPositionState:=astStates2[2], sName:='YAG', fPosition:=35);
fbStateSetup(stPositionState:=astStates2[3], sName:='TT', fPosition:=70);

fbStateSetup(stPositionState:=astStates3[1], sName:='OUT', fPosition:=0.4, fDelta:=0.1);
fbStateSetup(stPositionState:=astStates3[2], sName:='YAG', fPosition:=2.3, fDelta:=0.1);
fbStateSetup(stPositionState:=astStates3[3], sName:='TT', fPosition:=5.6, fDelta:=0.1;

Motivation and Context

Setting up position states is tedious and error-prone. closes #176

How Has This Been Tested?

I added unit tests to cover the new features.

Where Has This Been Documented?

I included a docstring and updated the readme.

Pre-merge checklist

ZLLentz commented 1 year ago

I'm very happy with the API I settled on here following the discussions on the linked issue. This remains a draft because the code is completely untested.

ZLLentz commented 1 year ago

A bit of a snag here: method calls in TwinCAT3 cannot have default arguments. Next week I'll find some other way to make this intuitive to use.

ZLLentz commented 1 year ago

I'm going to merge this and move on to the next item, which is making a tag so we can try this in TMO today.