Learn how to use Math Blocks for calculations and data processing.
- Warning
- This is a sample program to assist in the integration of the RMP motion controller with your application. It may not contain all of the logic and safety features that your application requires. We recommend that you wire an external hardware emergency stop (e-stop) button for safety when using our code sample apps. Doing so will help ensure the safety of you and those around you and will prevent potential injury or damage.
The sample apps assume that the system (network, axes, I/O) are configured prior to running the code featured in the sample app. See the Configuration page for more information.
In this page:
📜 Math Blocks: Calculate Acceleration from Velocity
Learn how to use Math Blocks to calculate acceleration from velocity data for motion analysis and control applications.
const int NUM_AXES = 1;
const int AXIS_INDEX = 0;
const double VELOCITY = 1.0;
const double ACCELERATION = 0.123;
int initialMathBlockCount = -1;
int exitCode = -1;
try
{
initialMathBlockCount = controller->MathBlockCountGet();
const int subtractionMathBlockIndex = initialMathBlockCount;
const int previousVelocityMathBlockIndex = initialMathBlockCount + 1;
controller->MathBlockCountSet(initialMathBlockCount + 2);
Axis *axis = controller->AxisGet(AXIS_INDEX);
controller->MemoryDoubleSet(userBufferAddr0, 1.0);
controller->MathBlockConfigSet(subtractionMathBlockIndex, subtractionConfig);
controller->MathBlockConfigSet(previousVelocityMathBlockIndex, previousVelocityConfig);
controller->SampleWait(1);
controller->SampleWait(10);
double calculatedVelocityDelta = controller->MathBlockProcessValueGet(subtractionMathBlockIndex).Double;
double calculatedAcceleration = calculatedVelocityDelta * controller->SampleRateGet() * controller->SampleRateGet() / axis->
UserUnitsGet();
std::cout << "Calculated acceleration from MathBlock: " << calculatedAcceleration << std::endl;
if (std::abs(calculatedAcceleration - ACCELERATION) <= 0.000001)
{
std::cout << "The MathBlock is calculating the Axis' acceleration by subtracting previous velocity from current velocity." << std::endl;
exitCode = 0;
}
else
{
std::cerr << "Error: The calculated acceleration does not match the expected value" << std::endl;
exitCode = -1;
}
}
catch (const std::exception &ex)
{
std::cerr << ex.what() << std::endl;
exitCode = -1;
}
if (initialMathBlockCount != -1) { controller->MathBlockCountSet(initialMathBlockCount); }
controller->Delete();
Source: calculate-acceleration-from-velocity.cpp
📜 Math Blocks: Difference of Position User Limit
Learn how to use Math Blocks to monitor position differences and implement user limits for advanced motion control and safety applications.
const int NUM_AXES = 2;
const int AXIS_X_INDEX = 0;
const int AXIS_Y_INDEX = 1;
const double MAX_POSITION_DIFFERENCE = 0.5;
const int USER_LIMIT_DURATION = 0;
const double RELATIVE_POSITION = 2 * MAX_POSITION_DIFFERENCE;
const double VELOCITY = 1;
const double ACCELERATION = 10;
const double DECELERATION = 10;
const double JERK_PCT = 0;
int initialUserLimitCount = -1;
int initialMathBlockCount = -1;
int exitCode = -1;
try
{
initialUserLimitCount = controller->UserLimitCountGet();
initialMathBlockCount = controller->MathBlockCountGet();
const int multiAxisIndex = controller->AxisCountGet();
controller->MotionCountSet(multiAxisIndex + 1);
const int userLimitIndex = initialUserLimitCount;
controller->UserLimitCountSet(initialUserLimitCount + 1);
const int mathBlockIndex = initialMathBlockCount;
controller->MathBlockCountSet(initialMathBlockCount + 1);
Axis *axisX = controller->AxisGet(AXIS_X_INDEX);
Axis *axisY = controller->AxisGet(AXIS_Y_INDEX);
MultiAxis *multiAxis = controller->MultiAxisGet(multiAxisIndex);
controller->MathBlockConfigSet(mathBlockIndex, mathBlockConfig);
controller->SampleWait(1);
std::cout << "MathBlock configured to subtract the position of the second axis from the position of the first axis." << std::endl;
uint64_t mathBlockProcessValueAddress =
controller->UserLimitConditionSet(
);
controller->UserLimitConfigSet(
USER_LIMIT_DURATION
);
std::cout << "UserLimit configured to trigger when the absolute position difference is greater than " << MAX_POSITION_DIFFERENCE
<< " and abort motion." << std::endl;
std::cout << "Moving the axes to trigger the UserLimit..." << std::endl;
axisX->
MoveRelative(RELATIVE_POSITION, VELOCITY, ACCELERATION, DECELERATION, JERK_PCT);
controller->UserLimitDisable(userLimitIndex);
{
std::cout << "Both axes are in the error state after the UserLimit triggered (This is the intended behavior)." << std::endl;
exitCode = 0;
}
else
{
std::cout << "Error: The axes should be in an error state after the UserLimit triggers, but they are not." << std::endl;
std::cout <<
"First Axis State: " << RSIStateMap.at(axisX->
StateGet()) << std::endl;
std::cout <<
"Second Axis State: " << RSIStateMap.at(axisY->
StateGet()) << std::endl;
exitCode = -1;
}
}
catch (const std::exception &ex)
{
std::cerr << ex.what() << std::endl;
exitCode = -1;
}
if (initialUserLimitCount != -1) { controller->UserLimitCountSet(initialUserLimitCount); }
if (initialMathBlockCount != -1) { controller->MathBlockCountSet(initialMathBlockCount); }
controller->Delete();
Source: difference-of-position-user-limit.cpp