- Attention
- See the following Concept pages for a detailed explanation of this sample: Real-Time Tasks.
- 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.
#include "rttaskglobals.h"
#include <cstdlib>
#include <ctime>
#include <iostream>
RSI_TASK(Initialize)
{
data->counter = 0;
data->average = 0;
std::srand(std::time(nullptr));
}
RSI_TASK(Increment)
{
data->counter += 1;
}
RSI_TASK(RandomWalk)
{
int random = std::rand() % 2;
double step = random ? 0.05 : -0.025;
data->average += step;
data->counter += 1;
RTAxisGet(0)->MoveSCurve(data->average);
}
RSI_TASK(CalculateTarget)
{
constexpr int NODE_INDEX = 0;
constexpr int ANALOG_INDEX = 0;
constexpr int ANALOG_MAX = 65536;
constexpr int ANALOG_ORIGIN = 42800;
auto networkNode = RTNetworkNodeGet(NODE_INDEX);
int32_t analogInVal = networkNode->AnalogInGet(ANALOG_INDEX);
int32_t shiftedVal = analogInVal - ANALOG_ORIGIN;
int32_t modVal = (shiftedVal + ANALOG_MAX) % ANALOG_MAX;
data->targetPosition = double(modVal) / ANALOG_MAX;
}
RSI_TASK(FollowTarget)
{
constexpr int AXIS_INDEX = 1;
constexpr double TOLERANCE = 0.02;
auto axis = RTAxisGet(AXIS_INDEX);
if (abs(axis->ActualPositionGet() - data->targetPosition) > TOLERANCE)
{
axis->MoveSCurve(data->targetPosition);
}
}
The RealTimeTasks namespace.