Flow Control

Several functions in the API buffer provide simple flow control for the API buffer, so that a part of the API buffer can be executed only if a particular condition is satisfied.

The available flow control functions are listed below:

These flow control functions can be nested up to 100 levels.

Flow Control Example

The following code will execute motion based on the I/O output state as described below:

  • If bit 0.0 is on, the first axis will execute motion.

  • Otherwise, if bit 0.1 is on, the second axis will execute motion.

  • Otherwise, the third axis will execute motion.

  • The motion will be in the positive direction if bit 0.2 is on and in the negative direction if bit 0.2 is off.

wmx3Api::Motion::PosCommand pos;
wmx3Api::ApiBufferCondition cond;

wmxlib_ApiBuffer->StartRecordBufferChannel(0);

pos.profile.type = ProfileType::Trapezoidal;
pos.profile.velocity = 10000;
pos.profile.acc = 10000;
pos.profile.dec = 10000;

//Set the IF branch
cond.bufferConditionType = ApiBufferConditionType::IOOutput;
cond.arg.ioOutput.byteAddress = 0;
cond.arg.ioOutput.bitAddress = 0;
wmxlib_ApiBuffer->FlowIf(&cond);

//Set the nested IF branch
cond.arg.ioOutput.byteAddress = 0;
cond.arg.ioOutput.bitAddress = 2;
wmxlib_ApiBuffer->FlowIf(&cond);

//Add position command
pos.axis = 0;
pos.target = 10000;
wmxlib_CoreMotion->motion->StartMov(&pos);

//Set the nested ELSE branch
wmxlib_ApiBuffer->FlowElse();

//Add position command
pos.axis = 0;
pos.target = -10000;
wmxlib_CoreMotion->motion->StartMov(&pos);

//Set the nested END IF
wmxlib_ApiBuffer->FlowEndIf();

//Set the ELSE IF branch
cond.arg.ioOutput.byteAddress = 0;
cond.arg.ioOutput.bitAddress = 1;
wmxlib_ApiBuffer->FlowElseIf(&cond);

//Set the nested IF branch
cond.arg.ioOutput.byteAddress = 0;
cond.arg.ioOutput.bitAddress = 2;
wmxlib_ApiBuffer->FlowIf(&cond);

//Add position command
pos.axis = 1;
pos.target = 10000;
wmxlib_CoreMotion->motion->StartMov(&pos);

//Set the nested ELSE branch
wmxlib_ApiBuffer->FlowElse();

//Add position command
pos.axis = 1;
pos.target = -10000;
wmxlib_CoreMotion->motion->StartMov(&pos);

//Set the nested END IF
wmxlib_ApiBuffer->FlowEndIf();

//Set the ELSE branch
wmxlib_ApiBuffer->FlowElse();

//Set the nested IF branch
cond.arg.ioOutput.byteAddress = 0;
cond.arg.ioOutput.bitAddress = 2;
wmxlib_ApiBuffer->FlowIf(&cond);

//Add position command
pos.axis = 2;
pos.target = 10000;
wmxlib_CoreMotion->motion->StartMov(&pos);

//Set the nested ELSE branch
wmxlib_ApiBuffer->FlowElse();

//Add position command
pos.axis = 2;
pos.target = -10000;
wmxlib_CoreMotion->motion->StartMov(&pos);

//Set the nested END IF
wmxlib_ApiBuffer->FlowEndIf();

//Set the END IF
wmxlib_ApiBuffer->FlowEndIf();

wmxlib_ApiBuffer->EndRecordBufferChannel();

wmxlib_ApiBuffer->Execute(0);

Flow Control With Multi-Condition Example

The following code will execute motion based on the I/O output state as described below:

  • If bit 0.0 or bit 0.1 is on while bit 0.2 is on, the first axis will execute motion.

  • Otherwise, if both bit 0.0 and bit 0.1 is on, the second axis will execute motion.

  • Otherwise, the third axis will execute motion.

Please refer to ApiBufferMultiCondition for usage description.

wmx3Api::Motion::PosCommand pos;
wmx3Api::ApiBufferMultiCondition multicond;
wmx3Api::ApiBufferCondition cond;

int root, child1, child2, leaf1, leaf2;

wmxlib_ApiBuffer->StartRecordBufferChannel(0);

pos.profile.type = ProfileType::Trapezoidal;
pos.profile.velocity = 10000;
pos.profile.acc = 10000;
pos.profile.dec = 10000;

//Set the IF branch
root = multicond.AddGateNode(ApiBufferConditionNodeType::And);
child1 = multicond.AddGateNode(ApiBufferConditionNodeType::Or, root);

cond.bufferConditionType = ApiBufferConditionType::IOOutput;
cond.arg.ioOutput.byteAddress = 0;
cond.arg.ioOutput.bitAddress = 0;
leaf1 = multicond.AddLeafNode(&cond, child1);

cond.arg.ioOutput.bitAddress = 1;
leaf2 = multicond.AddLeafNode(&cond, child1);

cond.arg.ioOutput.bitAddress = 2;
child2 = multicond.AddLeafNode(&cond, root);

wmxlib_ApiBuffer->FlowIf(&multicond);

//Add position command
pos.axis = 0;
pos.target = 10000;
wmxlib_CoreMotion->motion->StartMov(&pos);

//Set the ELSE IF branch
multicond.clear();

cond.arg.ioOutput.bitAddress = 0;
leaf1 = multicond.AddLeafNode(&cond);

cond.arg.ioOutput.bitAddress = 1;
leaf2 = multicond.AddLeafNode(&cond);

root = multicond.AddGateNode(ApiBufferConditionNodeType::And);
multicond.ConnectParentChild(root, leaf1);
multicond.ConnectParentChild(root, leaf2);

wmxlib_ApiBuffer->FlowElseIf(&multicond);

//Add position command
pos.axis = 1;
pos.target = 10000;
wmxlib_CoreMotion->motion->StartMov(&pos);

//Set the ELSE branch
wmxlib_ApiBuffer->FlowElse();

//Add position command
pos.axis = 2;
pos.target = 10000;
wmxlib_CoreMotion->motion->StartMov(&pos);

//Set the END IF
wmxlib_ApiBuffer->FlowEndIf();

wmxlib_ApiBuffer->EndRecordBufferChannel();

wmxlib_ApiBuffer->Execute(0);

The diagram below illustrates the multi-condition tree defined in the code.

../_images/WMXDOC_FUNC_APIBUFFER_FLOW_image0.png