UserLimit.cs
using RSI.RapidCode.dotNET; // Import our RapidCode Library.
using NUnit.Framework;
using System;
namespace SampleAppsCS
{
[TestFixture]
[Category("SoftwareSample")]
public class UserLimit : StaticMemoryTestBase
{
//[Test, Timeout(Constants.MAX_TEST_TIME)]
[Test]
public void UserLimitDigitalInputOneCondition()
{
//@[UserLimitDigitalInputOneCondition]
// Constants
const int INPUT_INDEX = 0; // This is the index of the digital input you will use to trigger the user limit.
const int OUTPUT_INDEX = 1; // This is the index of the digital output that will go active when the user limit triggers.
controller.AxisCountSet(1); // The number of user limits must not be greater than the number of axis and must be set before the number of user limits
controller.UserLimitCountSet(1); // Set the amount of UserLimits that you want to use. (every connected axis will automatically get 1 user limit)
controller.InterruptEnableSet(true); // Enable User Limit Interrupts. (When a user limit input comparison is met, and interrupt is triggered)
UInt64 userBufferAddress = controller.AddressGet(RSIControllerAddressType.RSIControllerAddressTypeUSER_BUFFER, 0); // Grabing an memory address to store a simulated IO point.
//Instead of using physical IO we will use simulated IO using a memory address as simulated IO. See the IO sample apps to see how to use a physical IO instead.
IOPoint input0 = IOPoint.CreateDigitalInput(controller, userBufferAddress, INPUT_INDEX); // Create a simulated IOpoint using userBuffer memory.
IOPoint output0 = IOPoint.CreateDigitalOutput(controller, userBufferAddress, OUTPUT_INDEX); // Create IOPoint object. Can be used to automatically get the address of a specified node and input index
SampleAppsCS.HelperFunctions.CheckErrors(input0);
SampleAppsCS.HelperFunctions.CheckErrors(output0);
//-------- PARAMETERS FOR UserLimitConditionSet --------
int userLimitNumber = 0; // Specify which user limit to use.
int condition = 0; // Specify which condition to use (0 or 1) ("0" to compare 1 input || "1" to compare 2 inputs)
RSIUserLimitLogic logic = RSIUserLimitLogic.RSIUserLimitLogicEQ; // Logic for input value comparison.
ulong inputAddress = input0.AddressGet(); // Use IOPoint or manually set using controller.NetworkInputAddressGet(INPUT_INDEX); for Beckhoff 10 was the index of 1st input. (To check your IO indexes go to RapidSetup -.
uint test = (uint)input0.MaskGet();
uint inputMask = (uint)input0.MaskGet(); // Get the appropriate mask from your IOpoint. OR use 0x00010000 for AKD General IO, 1 for Beckhoff IO Terminals
uint limtValue = (uint)input0.MaskGet(); // Get the appropriate mask from your IOpoint. OR use 0x00010000 for AKD General IO, 1 for Beckhoff IO Terminals
// [1] Configure the input's trigger condition.
controller.UserLimitConditionSet(userLimitNumber,// (User Limit Index) - Specify which user limit to use
condition, // (Condition Number) - Specify how many inputs you want to compare.
logic, // (Comparison Logic) - Specify the how the input value(s) will be compared
inputAddress, // (Input Address) - Specify the address of the input that will be compared.
inputMask, // (Input Mask) - Specify the bits in an address which need to be used when comparing inputs.
limtValue); // (Limit Value) - Specify the value to be compared with.
//-------- PARAMETERS FOR UserLimitConfigSet --------
RSIUserLimitTriggerType triggerType = RSIUserLimitTriggerType.RSIUserLimitTriggerTypeSINGLE_CONDITION;
RSIAction action = RSIAction.RSIActionNONE;
int axisNumber = 0;
int duration = 0;
// [2] Configure and Enable the user limit.
controller.UserLimitConfigSet(userLimitNumber,// (User Limit Index) - Specify which user limit to use.
triggerType, // (Trigger Type) - Specify how your condition should be evalutated.
action, // (User Limit Action) - Specify the action you want to cause on the axis when the user limit triggers.
axisNumber, // (Current Axis) - Specify the axis that the action (defined above) will occur on.
duration); // (Output Timer) - Specify the time delay before the action is executed after the User Limit has triggered.
//-------- PARAMETERS FOR UserLimitOutputSet --------
uint andMask = (uint)output0.MaskGet(); // Get the appropriate mask from your IOpoint. OR use 0x00010000 for AKD General IO, 1 for Beckhoff IO Terminals
uint orMask = (uint)output0.MaskGet(); // Get the appropriate mask from your IOpoint. OR use 0x00010000 for AKD General IO, 1 for Beckhoff IO Terminals
ulong outputAddress = output0.AddressGet(); //Alternatively set manually using: controller.NetworkOutputAddressGet(OUTPUT_INDEX);
bool enableOutput = true;
// [3] Configure what the output will be. (Call this method after UserLimitConfigSet if you want an output to be triggered) (The output will only be triggered if the input conditions are TRUE.)
controller.UserLimitOutputSet(userLimitNumber,// (User Limit Index) - Specify which user limit to use.
andMask, // (Logic AND Mask) - Specify the value that the digital output will be AND-ed with.
orMask, // (Logic OR Mask) - Specify the value that the digital output will be OR-ed with.
outputAddress, // (Output Address) - Specify the digital output address.
enableOutput); // (Enable Output Set) - If TRUE, the output AND-ing and OR-ing will be executed when the User Limit triggers.
while (controller.InterruptWait(250) != RSIEventType.RSIEventTypeUSER_LIMIT) // Loop will execute every 250 ms
{
//---ASSERT---
Assert.False(output0.Get(), "We expect the output to NOT be triggered");
//---TRIGGER---
controller.MemorySet(input0.AddressGet(), 1);
}
//---ASSERT---
Assert.True(output0.Get(), "We expect the output to be triggered");
controller.UserLimitDisable(userLimitNumber); // Disable User Limit.
output0.Set(false); // Disable User Limit.
//@[UserLimitDigitalInputOneCondition]
}
[Test]
public void UserLimitDigitalInputTwoCondition()
{
//@[UserLimitDigitalInputTwoCondition]
// Constants
const int INPUT_INDEX0 = 0; // This is the index of the digital input you will use to trigger the user limit.
const int INPUT_INDEX1 = 1; // This is the index of the digital input you will use to trigger the user limit.
const int OUTPUT_INDEX = 2; // This is the index of the digital output that will go active when the user limit triggers.
controller.AxisCountSet(1); // The number of user limits must not be greater than the number of axis and must be set before the number of user limits
controller.UserLimitCountSet(1); // Set the amount of UserLimits that you want to use. (every connected axis will automatically get 1 user limit)
controller.InterruptEnableSet(true); // Enable User Limit Interrupts. (When a user limit input comparison is met, and interrupt is triggered)
//-------- PARAMETERS FOR 1st and 2nd UserLimitConditionSet --------
int userLimitNumber = 0; // Specify which user limit to use.
RSIUserLimitLogic logic = RSIUserLimitLogic.RSIUserLimitLogicEQ; // Logic for input value comparison.
UInt64 userBufferAddress = controller.AddressGet(RSIControllerAddressType.RSIControllerAddressTypeUSER_BUFFER, 0); // Grabing an memory address to store a simulated IO point.
//Instead of using physical IO we will use simulated IO using a memory address as simulated IO. See the IO sample apps to see how to use a physical IO instead.
IOPoint input0 = IOPoint.CreateDigitalInput(controller, userBufferAddress, INPUT_INDEX0); // Create IOPoint object. Can be used to automatically get the address of a specified node and input index
IOPoint input1 = IOPoint.CreateDigitalInput(controller, userBufferAddress, INPUT_INDEX1); // Create IOPoint object. Can be used to automatically get the address of a specified node and input index
IOPoint output0 = IOPoint.CreateDigitalOutput(controller, userBufferAddress, OUTPUT_INDEX); // Create IOPoint object. Can be used to automatically get the address of a specified node and input index
SampleAppsCS.HelperFunctions.CheckErrors(input0);
SampleAppsCS.HelperFunctions.CheckErrors(input1);
SampleAppsCS.HelperFunctions.CheckErrors(output0);
int condition0 = 0; // Specify which condition to use (0 or 1) ("0" to compare 1 input || "1" to compare 2 inputs)
ulong input0Address = input0.AddressGet(); // 10 was the index of my 1st input. (To check your IO indexes go to RapidSetup -.
uint input0Mask = (uint)input0.MaskGet();
uint limitValue0 = (uint)input0.MaskGet();
int condition1 = 1; // Specify which condition to use (0 or 1) ("0" to compare 1 input || "1" to compare 2 inputs)
ulong input1Address = input1.AddressGet(); // 11 was the index of my 2nd input. (To check your IO indexes go to RapidSetup -.
uint input1Mask = (uint)input1.MaskGet();
uint limitValue1 = (uint)input1.MaskGet();
// [1] Configure the 1st input's trigger condition. (condition 1)
controller.UserLimitConditionSet(userLimitNumber, // (User Limit Index) - Specify which user limit to use
condition0, // (Condition Number) - Specify how many inputs you want to compare.
logic, // (Comparison Logic) - Specify the how the input value(s) will be compared
input0Address, // (Input Address) - Specify the address of the input that will be compared.
input0Mask, // (Input Mask) - Specify the bits in an address which need to be used when comparing inputs.
limitValue0); // (Limit Value) - Specify the value to be compared with.
// [2] Configure the 2nd input's trigger condition. (condition 2)
controller.UserLimitConditionSet(userLimitNumber, // (User Limit Index) - Specify which user limit to use
condition1, // (Condition Number) - Specify how many inputs you want to compare.
logic, // (Comparison Logic) - Specify the how the input value(s) will be compared
input1Address, // (Input Address) - Specify the address of the input that will be compared.
input1Mask, // (Input Mask) - Specify the bits in an address which need to be used when comparing inputs.
limitValue1); // (Limit Value) - Specify the value to be compared with.
//-------- PARAMETERS FOR UserLimitConfigSet --------
RSIUserLimitTriggerType triggerType = RSIUserLimitTriggerType.RSIUserLimitTriggerTypeCONDITION_AND;
RSIAction action = RSIAction.RSIActionNONE;
int axis = 0;
int duration = 0;
// [3] Configure and Enable the user limit.
controller.UserLimitConfigSet(userLimitNumber, // (User Limit Index) - Specify which user limit to use.
triggerType, // (Trigger Type) - Specify how your condition should be evalutated.
action, // (User Limit Action) - Specify the action you want to cause on the axis when the user limit triggers.
axis, // (Current Axis) - Specify the axis that the action (defined above) will occur on.
duration); // (Output Timer) - Specify the time delay before the action is executed after the User Limit has triggered.
//-------- PARAMETERS FOR UserLimitOutputSet --------
uint andMask = (uint)output0.MaskGet();
uint orMask = (uint)output0.MaskGet();
ulong outputAddress = output0.AddressGet();
bool enableOutput = true;
// [4] Configure what the output will be. (Call this method after UserLimitConfigSet if you want an output to be triggered) (The output will only be triggered if the input conditions are TRUE.)
controller.UserLimitOutputSet(userLimitNumber, // (User Limit Index) - Specify which user limit to use.
andMask, // (Logic AND Mask) - Specify the value that the digital output will be AND-ed with.
orMask, // (Logic OR Mask) - Specify the value that the digital output will be OR-ed with.
outputAddress, // (Output Address) - Specify the digital output address.
enableOutput); // (Enable Output Set) - If TRUE, the output AND-ing and OR-ing will be executed when the User Limit triggers.
while (controller.InterruptWait(250) != RSIEventType.RSIEventTypeUSER_LIMIT) // Wait for user limit to trigger. Loop will execute every 250 ms
{
//---ASSERT---
Assert.False(output0.Get(), "We expect the output to NOT be triggered");
controller.MemorySet(input0.AddressGet(), 1); // Set bit 0 high.
Assert.False(output0.Get(), "We expect the output to NOT be triggered");
controller.MemorySet(input0.AddressGet(), 2); // Set bit 1 high.
Assert.False(output0.Get(), "We expect the output to NOT be triggered");
//---TRIGGER---
controller.MemorySet(input0.AddressGet(), 3); // Set bits 0 and 1 high.
}
//---ASSERT---
Assert.True(output0.Get(), "We expect the output to be triggered");
//@[UserLimitDigitalInputTwoCondition]
}
[Test]
public void UserLimitDigitalInputEStopStorePosition()
{
//@[UserLimitDigitalInputEStopStorePosition]
// Constants
const int AXIS_INDEX = 0; // This is the index of the axis you will use to command motion.
const int INPUT_INDEX = 0;
const int AXIS_COUNT = 1; // Axes on the network.
const int USER_LIMIT = 0; // Specify which user limit to use.
const int CONDITION = 0; // Specify which condition to use. (0 or 1) ("0" to compare 1 input || "1" to compare 2 inputs)
const RSIUserLimitLogic LOGIC = RSIUserLimitLogic.RSIUserLimitLogicEQ; // Logic for input value comparison.
const int LIMIT_VALUE = 1; // The value to be compared which needs to be set here.
const RSIUserLimitTriggerType TRIGGER_TYPE = RSIUserLimitTriggerType.RSIUserLimitTriggerTypeSINGLE_CONDITION; // Choose the how the condition (s) should be evaluated.
const RSIAction ACTION = RSIAction.RSIActionE_STOP_ABORT; // Choose the action you want to cause when the User Limit triggers.
const double DURATION = 0.125; // Enter the time delay before the action is executed after the User Limit has triggered.
const int USER_DATA_INDEX = 0;
// Some Necessary Pre User Limit Configuration
controller.AxisCountSet(1);
controller.UserLimitCountSet(1); // Set the amount of UserLimits that you want to use.
controller.InterruptEnableSet(true); // Enable User Limit Interrupts. (When a user limit input comparison is met, and interrupt is triggered)
UInt64 userBufferAddress = controller.AddressGet(RSIControllerAddressType.RSIControllerAddressTypeUSER_BUFFER, 0); // Grabing an memory address to store a simulated IO point.
//Instead of using physical IO we will use simulated IO using a memory address as simulated IO. See the IO sample apps to see how to use a physical IO instead.
IOPoint input0 = IOPoint.CreateDigitalInput(controller, userBufferAddress, INPUT_INDEX); // Create IOPoint object. Can be used to automatically get the address of a specified node and input index
axis = CreateAndReadyAxis(Constants.AXIS_NUMBER); // Initialize your axis object.
axis.MoveVelocity(10.0, 20.0); // Command a velocity move (Velocity=1.0, Acceleration=10.0).
// USER LIMIT CONDITION
controller.UserLimitConditionSet(USER_LIMIT, CONDITION, LOGIC, input0.AddressGet(), (uint)input0.MaskGet(), LIMIT_VALUE); // Set your User Limit Condition (1st step to setting up your user limit)
// USER LIMIT CONFIGURATION
controller.UserLimitConfigSet(USER_LIMIT, TRIGGER_TYPE, ACTION, AXIS_INDEX, DURATION); // Set your User Limit Configuration. (2nd step to setting up your user limit)
// USER LIMIT USER DATA SET
controller.UserLimitInterruptUserDataAddressSet(USER_LIMIT, // Specify the user limit you want to add User Data for.
USER_DATA_INDEX, // Specify what user data index you would like to use. (must be a value from 0 to 4)
axis.AddressGet(RSIAxisAddressType.RSIAxisAddressTypeCOMMAND_POSITION)); // Specify the address of the data value you want to store in your User Data so that you can retrieve it later after the UserLimit limit triggers.
// WAIT FOR DIGITAL INPUT TO TRIGGER USER LIMIT EVENT.
while (controller.InterruptWait(250) != RSIEventType.RSIEventTypeUSER_LIMIT) // Wait until your user limit triggers.
{
//---TRIGGER---
controller.MemorySet(input0.AddressGet(), 1);
}
int triggeredUserLimit = controller.InterruptSourceNumberGet() - AXIS_COUNT; // Check that the correct user limit has triggered. (an extra user limit is allocated for each axis)
UInt64 data = controller.InterruptUserDataGet(USER_DATA_INDEX); // Get the data stored in the user data you configured.
double interruptPosition = BitConverter.ToDouble(BitConverter.GetBytes(data), 0); // Convert from raw 64-bit memory bytes into 64-bit double.
Console.WriteLine("TRIGGERED - User Limit Interrupt Position = " + interruptPosition / axis.UserUnitsGet()); // Get the position of the axis when it user limit event triggered.
controller.UserLimitDisable(USER_LIMIT);
//@[UserLimitDigitalInputEStopStorePosition]
}
[Test]
public void UserLimitFeedRate()
{
//@[UserLimitFeedRate]
// Constants
const int USER_LIMIT = 0; // Specify which user limit to use.
const int USER_LIMIT_COUNT = 1;
const int AXIS_COUNT = 1;
const int CONDITION = 0; // Specify which condition to use. (0 or 1) ("0" to compare 1 input || "1" to compare 2 inputs)
const RSIUserLimitLogic LOGIC = RSIUserLimitLogic.RSIUserLimitLogicGT; // Logic for input value comparison.
const double POSITION_TRIGGER_VALUE = 5; // The value to be compared which needs to be set here.
const RSIUserLimitTriggerType TRIGGER_TYPE = RSIUserLimitTriggerType.RSIUserLimitTriggerTypeSINGLE_CONDITION; // Choose the how the condition (s) should be evaluated.
const RSIAction ACTION = RSIAction.RSIActionNONE; // Choose the action you want to cause when the User Limit triggers.
const int DURATION = 0; // Enter the time delay before the action is executed after the User Limit has triggered.
const double DEFAULT_FEED_RATE = 1.0;
const double DESIRED_FEED_RATE = 2.0;
// Other Global Variables
ulong feedRateAddress;
// Some Necessary Pre User Limit Configuration
controller.AxisCountSet(AXIS_COUNT);// The number of user limits must not be greater than the number of axis and must be set before the number of user limits
controller.UserLimitCountSet(USER_LIMIT_COUNT); // Set the amount of UserLimits that you want to use.
axis = CreateAndReadyAxis(Constants.AXIS_NUMBER); // Initialize your axis object.
axis.FeedRateSet(DEFAULT_FEED_RATE); // Restore FeedRate to default value.
// USER LIMIT CONDITION
// We will use command position because we are working with a phantom axis. Actual position can be used for real axis.
controller.UserLimitConditionSet(USER_LIMIT, CONDITION, LOGIC, axis.AddressGet(RSIAxisAddressType.RSIAxisAddressTypeCOMMAND_POSITION), POSITION_TRIGGER_VALUE); // Set your User Limit Condition (1st step to setting up your user limit)
// USER LIMIT CONFIGURATION
controller.UserLimitConfigSet(USER_LIMIT, TRIGGER_TYPE, ACTION, axis.NumberGet(), DURATION); // Set your User Limit Configuration. (2nd step to setting up your user limit)
// USER LIMIT OUTPUT
feedRateAddress = axis.AddressGet(RSIAxisAddressType.RSIAxisAddressTypeTARGET_FEEDRATE);
controller.UserLimitOutputSet(USER_LIMIT, DESIRED_FEED_RATE, feedRateAddress, true);
//---ASSERT---
Assert.AreEqual(DEFAULT_FEED_RATE, axis.FeedRateGet(), "We expect the feedrate to be default");
axis.MoveSCurve(POSITION_TRIGGER_VALUE + 1);
axis.MotionDoneWait();
Assert.AreEqual(DESIRED_FEED_RATE, axis.FeedRateGet(), "We expect the feedrate to be changed");
//@[UserLimitFeedRate]
}
[Test]
public void UserLimitPositionOneCondition()
{
//@[UserLimitPositionOneCondition]
// Constants
const int AXIS_COUNT = 1;
const int USER_LIMIT_COUNT = 1;
const double POSITION = 5.0; // Specify the position to travel to.
const int OUTPUT_INDEX = 1; // This is the index of the digital output that will go active when the user limit triggers.
// Some Necessary Pre User Limit Configuration
controller.AxisCountSet(AXIS_COUNT);// The number of user limits must not be greater than the number of axis and must be set before the number of user limits
controller.UserLimitCountSet(USER_LIMIT_COUNT); // Set the amount of UserLimits that you want to use. (every connected axis will automatically get 1 user limit)
controller.InterruptEnableSet(true); // Enable User Limit Interrupts. (When a user limit input comparison is met, and interrupt is triggered)
UInt64 userBufferAddress = controller.AddressGet(RSIControllerAddressType.RSIControllerAddressTypeUSER_BUFFER, 0); // Grabing an memory address to store a simulated IO point.
IOPoint output0 = IOPoint.CreateDigitalOutput(controller, userBufferAddress, OUTPUT_INDEX); // Create IOPoint object. Can be used to automatically get the address of a specified node and input index
axis = CreateAndReadyAxis(Constants.AXIS_NUMBER); // Initialize your axis object.
//-------- PARAMETERS FOR UserLimitConditionSet --------
int userLimit = 0; // Specify which user limit to use.
int condition = 0; // Specify which condition to use (0 or 1) ("0" to compare 1 input || "1" to compare 2 inputs)
double limitValue = POSITION; // The limit value will be in counts so we multiply our desired position by USER_UNITS
controller.UserLimitConditionSet(userLimit, // (User Limit Index) - Specify which user limit to use
condition, // (Condition Number) - Specify how many inputs you want to compare.
RSIUserLimitLogic.RSIUserLimitLogicGT, // (Comparison Logic) - Specify the how the input value(s) will be compared
axis.AddressGet(RSIAxisAddressType.RSIAxisAddressTypeCOMMAND_POSITION),// (Input Address) - Specify the address of the input that will be compared. // FOR A REAL AXIS USE ACTUAL POSITION
limitValue); // (Limit Value) - Specify the value to be compared with.
//-------- PARAMETERS FOR UserLimitConfigSet --------
RSIUserLimitTriggerType triggerType = RSIUserLimitTriggerType.RSIUserLimitTriggerTypeSINGLE_CONDITION;
RSIAction action = RSIAction.RSIActionABORT; // Abort move when user limit triggers.
int duration = 0;
// [2] Configure and Enable the user limit.
controller.UserLimitConfigSet(userLimit, // (User Limit Index) - Specify which user limit to use.
triggerType, // (Trigger Type) - Specify how your condition should be evalutated.
action, // (User Limit Action) - Specify the action you want to cause on the axis when the user limit triggers.
axis.NumberGet(), // (Current Axis) - Specify the axis that the action (defined above) will occur on.
duration); // (Output Timer) - Specify the time delay before the action is executed after the User Limit has triggered.
//-------- PARAMETERS FOR UserLimitOutputSet --------
uint andMask = (uint)output0.MaskGet(); // Get the appropriate mask from your IOpoint. OR use 0x00010000 for AKD General IO, 1 for Beckhoff IO Terminals
uint orMask = (uint)output0.MaskGet(); ; // Get the appropriate mask from your IOpoint. OR use 0x00010000 for AKD General IO, 1 for Beckhoff IO Terminals
ulong outputAddress = output0.AddressGet(); //Alternatively set manually using: controller.NetworkOutputAddressGet(OUTPUT_INDEX);
bool enableOutput = true;
// [3] Configure what the output will be. (Call this method after UserLimitConfigSet if you want an output to be triggered) (The output will only be triggered if the input conditions are TRUE.)
controller.UserLimitOutputSet(userLimit, // (User Limit Index) - Specify which user limit to use.
andMask, // (Logic AND Mask) - Specify the value that the digital output will be AND-ed with.
orMask, // (Logic OR Mask) - Specify the value that the digital output will be OR-ed with.
outputAddress, // (Output Address) - Specify the digital output address.
enableOutput); // (Enable Output Set) - If TRUE, the output AND-ing and OR-ing will be executed when the User Limit triggers.
// WAIT FOR DIGITAL INPUT TO TRIGGER USER LIMIT EVENT.
while (controller.InterruptWait(250) != RSIEventType.RSIEventTypeUSER_LIMIT) // Wait until your user limit triggers.
{
Assert.False(output0.Get(), "We expect the output to NOT be triggered");
//---TRIGGER---
axis.MoveSCurve(POSITION + 1); // Command simple trapezoidal motion.
}
Assert.True(output0.Get(), "We expect the output to be triggered");
axis.AmpEnableSet(false); // Disable the motor.
controller.UserLimitDisable(userLimit); // Disable User Limit.
output0.Set(false); // Set output low so program can run again
//@[UserLimitPositionOneCondition]
}
}
}