APIs, concepts, guides, and more
SyncOutputWithMotion.cpp
Attention
See the following Concept pages for a detailed explanation of this sample: Streaming Motion Sync Outputs.
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 <cassert>
#include "rsi.h" // Import our RapidCode Library.
#include "SampleAppsHelper.h" // Import our SampleApp helper functions.
#include <cmath>
#include "SampleApps.h"
using namespace RSI::RapidCode; // Import the RapidCode namespace
int SyncOutputWithMotion::Run()
{
/* CONSTANTS */
// *NOTICE* The following constants must be configured before attempting to run with hardware.
// Axis configuration parameters
const int AXIS_COUNT = 1; // number of axes
const int AXIS_NUMBER = 0; // axis number
const double USER_UNITS = 1048576; // encoder counts per rev (set as appropiate)
// Motion parameters
const double TIME_SLICE = 0.01; // 0.01s = 10ms
const int REVS = 2; // number of revolutions
const int RPS = 1; // revs / sec
const int TOTAL_POINTS = (int)(REVS / TIME_SLICE / RPS); // total number of points
const int EMPTY_CT = -1; // Number of points that remains in the buffer before an e-stop
// To run with hardware, set the USE_HARDWARE flag to true AFTER you have configured the parameters above and taken proper safety precautions.
USE_HARDWARE = false;
/* SAMPLE APP BODY */
// Initizalize the controller from software w/ multiple axes
// Setup the controller for the appropriate hardware configuration.
if (USE_HARDWARE)
{
}
else
{
SampleAppsHelper::SetupControllerForPhantoms(controller, AXIS_COUNT, { AXIS_NUMBER });
}
MultiAxis* multiAxis;
try {
// add an additional axis for the multiaxis supervisor
controller->MotionCountSet(controller->AxisCountGet() + 1);
// create the multiaxis using the ID of the first free axis (0 indexed)
multiAxis = controller->MultiAxisGet(controller->MotionCountGet() - 1);
// populate the multiaxis
Axis* axis = controller->AxisGet(AXIS_NUMBER);
axis->UserUnitsSet(USER_UNITS);
axis->EStopAbort();
axis->ClearFaults();
axis->PositionSet(0);
multiAxis->AxisAdd(axis);
// populate the positions and times
std::vector<double> positions, times;
for (int i = 0; i < TOTAL_POINTS; i += AXIS_COUNT)
{
positions.push_back(i * TIME_SLICE * RPS);
times.push_back(TIME_SLICE);
}
// prepare the controller (and drive)
multiAxis->Abort();
multiAxis->ClearFaults();
assert(multiAxis->StateGet() == RSIState::RSIStateIDLE);
multiAxis->AmpEnableSet(true);
// set up the inputs
// ensure the digital out is set low
output0->Set(0);
// The motion element ID at which to set the output
int outPutEnableID = TOTAL_POINTS / 2;
// enable streaming output
multiAxis->StreamingOutputsEnableSet(true);
// Enable the streaming output (The following are functionally equivalent)
//multiAxis->StreamingOutputAdd(output0->MaskGet(), 0, output0->AddressGet(), outPutEnableID);
multiAxis->StreamingOutputAdd(output0, true, outPutEnableID);
// Disable the output (The following are functionally equivalent)
//multiAxis->StreamingOutputAdd(0, output0->MaskGet(), output0->AddressGet(), outPutEnableID);
//multiAxis->StreamingOutputAdd(output0, false, outPutEnableID);
multiAxis->MovePT(RSIMotionType::RSIMotionTypePT, &positions[0], &times[0], TOTAL_POINTS, EMPTY_CT, false, true);
printf("Motion started. Waiting to complete.\n");
multiAxis->MotionDoneWait();
printf("Motion Complete. The outputs should have been set\n");
multiAxis->StreamingOutputsClear(); // cleanup for next run
multiAxis->EStopAbort();
}
catch (RsiError const& err) {
printf("\n%s\n", err.text);
return -1;
}
controller->Delete(); // Delete the controller as the program exits to ensure memory is deallocated in the correct order.
return 0;
}
void UserUnitsSet(double countsPerUserUnit)
Sets the number of counts per User Unit.
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.
Represents a single axis of motion control. This class provides an interface for commanding motion,...
Definition rsi.h:5863
void Set(bool state)
Set the state of a Digital Output.
static IOPoint * CreateDigitalOutput(Axis *axis, RSIMotorDedicatedOut motorDedicatedOutNumber)
Create a Digital Output from an Axis' Dedicated Output bits.
Represents one specific point: Digital Output, Digital Input, Analog Output, or Analog Input....
Definition rsi.h:11550
static MotionController * CreateFromSoftware()
Initialize and start the RMP EtherCAT controller.
Represents the RMP soft motion controller. This class provides an interface to general controller con...
Definition rsi.h:800
Axis * AxisGet(int32_t index)
Get the Axis class used in this MultiAxis class.
void AxisAdd(Axis *axis)
Add an Axis to a MultiAxis group.
Represents multiple axes of motion control, allows you to map two or more Axis objects together for e...
Definition rsi.h:10795
void ClearFaults()
Clear all faults for an Axis or MultiAxis.
void Abort()
Abort an axis.
int32_t MotionDoneWait()
Waits for a move to complete.
void StreamingOutputAdd(int32_t onMask, int32_t offMask, uint64_t address)
RSIState StateGet()
Get the Axis or MultiAxis state.
void StreamingOutputsClear()
Clear the contents of the Streaming Output List.
void StreamingOutputsEnableSet(bool enable)
Sets whether Streaming Output is enabled (true) or disabled (false).
int32_t AmpEnableSet(bool enable, int32_t ampActiveTimeoutMilliseconds=AmpEnableTimeoutMillisecondsDefault, bool overrideRestrictedState=false)
Enable all amplifiers.
void MovePT(RSIMotionType type, const double *const position, const double *const time, int32_t pointCount, int32_t emptyCount, bool retain, bool final)
A move commanded by a list of position and time points.
void EStopAbort()
E-Stop, then abort an axis.
Represents the error details thrown as an exception by all RapidCode classes. This class contains an ...
Definition rsi.h:111
@ RSIMotionTypePT
Position-Time algorithm with continuous position but discontinuous velocity. Best for short time inte...
Definition rsienums.h:1039
@ RSIActionNONE
None - do not perform any action.
Definition rsienums.h:1116
@ RSIStateIDLE
Idle, ready for anything.
Definition rsienums.h:880
static void SetupControllerForHardware(MotionController *controller)
Sets up the controller for hardware use by resetting it and starting the network.
static void CheckErrors(RapidCodeObject *rsiObject)
Checks for errors in the given RapidCodeObject and throws an exception if any non-warning errors are ...
static void SetupControllerForPhantoms(MotionController *controller, int axisCount, std::vector< int > axisNums)
Sets up the controller for phantom axes, including configuring specified axes as phantom.