APIs, concepts, guides, and more
mathblock-difference-of-position-userlimit.cs
/* This sample app demonstrates configuring a MathBlock to calculate the difference of position between two axes
and using that MathBlock process value in a UserLimit to trigger an abort action when the difference exceeds a threshold.
This sample app requires at least 2 Axis so that positions can be read.
*/
using RSI.RapidCode; // RSI.RapidCode.dotNET;
Console.WriteLine("📜 MathBlock: Difference of Position UserLimit");
// set sample config params
const int INDEX_ZERO = 0;
const RSIDataType DOUBLE = RSIDataType.RSIDataTypeDOUBLE;
const RSIMathBlockOperation SUBTRACT = RSIMathBlockOperation.RSIMathBlockOperationSUBTRACT;
// get rmp controller
try
{
Helpers.CheckErrors(controller);
// set mathblock & userlimit counts before any RapidCodeObject get/create other than the controller
controller.MathBlockCountSet(1);
controller.UserLimitCountSet(1);
// get & configure both axes
Axis axis0 = controller.AxisGet(Constants.AXIS_0_INDEX);
axis0.ErrorLimitActionSet(RSIAction.RSIActionNONE);
axis0.Abort();
axis0.ClearFaults();
controller.SampleWait(10);
axis0.PositionSet(0);
Axis axis1 = controller.AxisGet(Constants.AXIS_1_INDEX);
axis1.ErrorLimitActionSet(RSIAction.RSIActionNONE);
axis1.Abort();
axis1.ClearFaults();
controller.SampleWait(10);
axis1.PositionSet(0);
// set sample addresses
ulong axis0PositionAddr = axis0.AddressGet(RSIAxisAddressType.RSIAxisAddressTypeCOMMAND_POSITION);
ulong axis1PositionAddr = axis1.AddressGet(RSIAxisAddressType.RSIAxisAddressTypeCOMMAND_POSITION);
ulong mbProcessValueAddr = controller.AddressGet(RSIControllerAddressType.RSIControllerAddressTypeMATHBLOCK_PROCESS_VALUE, INDEX_ZERO);
// MathBlock config & set (mb.value = axis0.position - axis1.position)
var mbConfig = controller.MathBlockConfigGet(INDEX_ZERO);
mbConfig.Operation = SUBTRACT;
mbConfig.InputAddress0 = axis0PositionAddr; // input0: axis0 command position
mbConfig.InputAddress1 = axis1PositionAddr; // input1: axis1 command position
mbConfig.InputDataType0 = DOUBLE;
mbConfig.InputDataType1 = DOUBLE;
mbConfig.ProcessDataType = DOUBLE;
controller.MathBlockConfigSet(INDEX_ZERO, mbConfig);
// wait 1 sample for MathBlock config to take effect
controller.SampleWait(1);
// set UserLimit condition to trigger when absolute position difference exceeds threshold
number: INDEX_ZERO,
conditionNumber: 0,
logic: RSIUserLimitLogic.RSIUserLimitLogicABS_GT,
addressOfDouble: mbProcessValueAddr,
limitValueDouble: 5 * Constants.AXIS_0_USER_UNITS); // 5 revolutions (values in firmware are not scaled by user units)
// set UserLimit to abort when condition above is met
controller.UserLimitConfigSet(
number: INDEX_ZERO,
triggerType: RSIUserLimitTriggerType.RSIUserLimitTriggerTypeSINGLE_CONDITION,
action: RSIAction.RSIActionABORT,
actionAxis: Constants.AXIS_0_INDEX,
duration: 0);
// move axis0 to trigger the UserLimit
axis0.AmpEnableSet(true);
axis0.MoveRelative(
relativePosition: 10, // revolutions
vel: 1,
accel: 1,
decel: 1,
jerkPct: 0);
axis0.MotionDoneWait();
// get results
var axis0State = axis0.StateGet();
// print results
Console.WriteLine($"Axis 0 state: {axis0State} (expected: {RSIState.RSIStateERROR})");
// verify userlimit triggered abort action
if (axis0State != RSIState.RSIStateERROR)
throw new Exception("❌ UserLimit did not trigger correctly - both axes should be in error state.");
}
// handle errors as needed
finally
{
controller.UserLimitDisable(INDEX_ZERO); // disable user limit
controller.Delete(); // dispose
}
static void CheckErrors(RapidCodeObject rsiObject)
Checks for errors in the given RapidCodeObject and throws an exception if any non-warning errors are ...
Definition _helpers.cs:15
Helpers class provides static methods for common tasks in RMP applications.
Definition _helpers.cs:5
uint64_t AddressGet(RSIAxisAddressType addressType)
Get the an address for some location on the Axis.
void ErrorLimitActionSet(RSIAction action)
Set the action that will occur when the Error Limit Event triggers.
void PositionSet(double position)
Set the Command and Actual positions.
void MoveRelative(double relativePosition, double vel, double accel, double decel, double jerkPct)
Command a relative point-to-point S-Curve motion.
Represents a single axis of motion control. This class provides an interface for commanding motion,...
Definition rsi.h:5870
Axis * AxisGet(int32_t axisNumber)
AxisGet returns a pointer to an Axis object and initializes its internals.
void UserLimitDisable(int32_t number)
Disable the processing of a User Limit.
void MathBlockCountSet(int32_t mathBlockCount)
Set the number of processed MathBlocks in the MotionController.
void UserLimitConditionSet(int32_t number, int32_t conditionNumber, RSIUserLimitLogic logic, uint64_t addressOfUInt32, uint32_t userLimitMask, uint32_t limitValueUInt32)
Set the conditions for a User Limit with a 32-bit integer trigger value.
static MotionController * Get()
Get an already running RMP EtherCAT controller.
uint64_t AddressGet(RSIControllerAddressType type)
Get the an address for some location on the MotionController.
void SampleWait(uint32_t samples)
Wait for controller firmware to execute samples.
void UserLimitConfigSet(int32_t number, RSIUserLimitTriggerType triggerType, RSIAction action, int32_t actionAxis, double duration, bool singleShot)
Configure a User Limit.
void Delete(void)
Delete the MotionController and all its objects.
void UserLimitCountSet(int32_t userLimitCount)
Set the number of processed UserLimits in the MotionController.
Represents the RMP soft motion controller. This class provides an interface to general controller con...
Definition rsi.h:800
MathBlockConfig MathBlockConfigGet(int32_t mathBlockNumber)
Get a MathBlock configuration.
void MathBlockConfigSet(int32_t mathBlockNumber, MathBlockConfig &config)
Set a MathBlock configuration.
void ClearFaults()
Clear all faults for an Axis or MultiAxis.
void Abort()
Abort an axis.
int32_t MotionDoneWait()
Waits for a move to complete.
RSIState StateGet()
Get the Axis or MultiAxis state.
int32_t AmpEnableSet(bool enable, int32_t ampActiveTimeoutMilliseconds=AmpEnableTimeoutMillisecondsDefault, bool overrideRestrictedState=false)
Enable all amplifiers.
RSIMathBlockOperation
MathBlock operations.
Definition rsienums.h:1431
RSIControllerAddressType
Used to get firmware address used in User Limits, Recorders, etc.
Definition rsienums.h:405
RSIDataType
Data types for User Limits and other triggers.
Definition rsienums.h:659
RSIUserLimitLogic
Logic options for User Limits.
Definition rsienums.h:646
RSIAction
Action to perform on an Axis.
Definition rsienums.h:1115
RSIAxisAddressType
Used to get firmware address used in User Limits, Recorders, etc.
Definition rsienums.h:434
RSIUserLimitTriggerType
Trigger types for UserLimits.
Definition rsienums.h:633