APIs, concepts, guides, and more
PointToPoint.cpp
Attention
See the following Concept pages for a detailed explanation of this sample: Final Velocity Motion, Absolute Motion, Relative Motion, SCurve Motion, Velocity Motion.
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 <cmath>
#include "SampleAppsHelper.h" // Import our helper functions.
#include "rsi.h" // Import our RapidCode Library.
using namespace RSI::RapidCode; // Import the RapidCode namespace
// Helper function to wait for the axis to reach velocity when performing a velocity move or move with final velocity
static void HelperAtVelocityWait(MotionController *controller, Axis *axis, const int timeout)
{
// The number of samples to wait before checking if the axis is at velocity
const int WAIT_SAMPLES = 100;
int samplesWaited = 0;
while (axis->StatusBitGet(RSIEventType::RSIEventTypeMOTION_AT_VELOCITY) == 0)
{
controller->SampleWait(WAIT_SAMPLES);
samplesWaited += WAIT_SAMPLES;
if (samplesWaited > timeout)
{
throw std::runtime_error("Timeout waiting for motion to reach velocity");
}
}
}
int main()
{
const std::string SAMPLE_APP_NAME = "Motion: Point-to-Point";
// Print a start message to indicate that the sample app has started
// *NOTICE* The following constants must be configured before attempting to run with hardware.
// Motion Parameters
const double POSITION_0 = 0;
const double POSITION_1 = 0.5;
const double VELOCITY = 1;
const double ACCELERATION = 10;
const double DECELERATION = 10;
const double JERK_PERCENT = 50;
const double FINAL_VELOCITY = 0.5;
/* RAPIDCODE INITIALIZATION */
// Create the controller
MotionController *controller = MotionController::Create(&params);
int exitCode = -1; // Set the exit code to an error value.
try // Ensure that the controller is deleted if an error occurs.
{
// Prepare the controller and axes as defined in SampleAppsHelper.h
/* SAMPLE APP BODY */
/* Configure the controller object counts */
// Get the axes
// Specify a timeout for waiting for the motion to complete
const int32_t TIMEOUT = 10000;
// Clear faults and enable the motor
axis->ClearFaults();
axis->AmpEnableSet(true);
/* Absolute (Trapezoidal) Motion */
std::cout << "Absolute (Trapezoidal) Motion:" << std::endl;
std::cout << "Moving to position: " << POSITION_1 << std::endl;
axis->MoveTrapezoidal(POSITION_1, VELOCITY, ACCELERATION, DECELERATION);
axis->MotionDoneWait(TIMEOUT);
std::cout << "Motion Complete" << std::endl;
std::cout << "Moving back to position: " << POSITION_0 << std::endl;
axis->MoveTrapezoidal(POSITION_0, VELOCITY, ACCELERATION, DECELERATION);
axis->MotionDoneWait(TIMEOUT);
std::cout << "Motion Complete\n" << std::endl;
/* Relative Motion */
std::cout << "Relative Motion:" << std::endl;
std::cout << "Moving to position: " << POSITION_1 << std::endl;
axis->MoveRelative(POSITION_1 - POSITION_0, VELOCITY, ACCELERATION, DECELERATION, JERK_PERCENT);
axis->MotionDoneWait(TIMEOUT);
std::cout << "Motion Complete" << std::endl;
std::cout << "Moving back to position: " << POSITION_0 << std::endl;
axis->MoveRelative(POSITION_0 - POSITION_1, VELOCITY, ACCELERATION, DECELERATION, JERK_PERCENT);
axis->MotionDoneWait(TIMEOUT);
std::cout << "Motion Complete\n" << std::endl;
/* SCurve Motion */
std::cout << "SCurve Motion:" << std::endl;
std::cout << "Moving to position: " << POSITION_1 << std::endl;
axis->MoveSCurve(POSITION_1);
axis->MotionDoneWait(TIMEOUT);
std::cout << "Motion Complete" << std::endl;
std::cout << "Moving back to position: " << POSITION_0 << std::endl;
axis->MoveSCurve(POSITION_0);
axis->MotionDoneWait(TIMEOUT);
std::cout << "Motion Complete\n" << std::endl;
/* SCurve Motion with Final Velocity */
std::cout << "SCurve Motion with Final Velocity:" << std::endl;
std::cout << "Moving to position: " << POSITION_1 << std::endl;
std::cout << "Final Velocity: " << FINAL_VELOCITY << std::endl;
axis->MoveSCurve(POSITION_1, VELOCITY, ACCELERATION, DECELERATION, JERK_PERCENT, FINAL_VELOCITY);
HelperAtVelocityWait(controller, axis, TIMEOUT);
std::cout << "Motion Complete" << std::endl;
std::cout << "Moving back to position: " << POSITION_0 << std::endl;
std::cout << "Final Velocity: " << 0 << std::endl;
axis->MoveSCurve(POSITION_0, VELOCITY, ACCELERATION, DECELERATION, JERK_PERCENT, 0);
axis->MotionDoneWait(TIMEOUT);
std::cout << "Motion Complete\n" << std::endl;
/* Velocity Move */
std::cout << "Velocity Move:" << std::endl;
std::cout << "Accelerating to velocity: " << VELOCITY << std::endl;
axis->MoveVelocity(VELOCITY, ACCELERATION);
HelperAtVelocityWait(controller, axis, TIMEOUT);
std::cout << "Motion Complete" << std::endl;
std::cout << "Decelerating to velocity: " << 0 << std::endl;
axis->MoveVelocity(0, DECELERATION);
HelperAtVelocityWait(controller, axis, TIMEOUT);
std::cout << "Motion Complete" << std::endl;
// Return to the original position
axis->MoveTrapezoidal(POSITION_0, VELOCITY, ACCELERATION, DECELERATION);
axis->MotionDoneWait(TIMEOUT);
// Disable the motor
axis->AmpEnableSet(false);
exitCode = 0; // Set the exit code to success.
}
catch (const std::exception &ex)
{
std::cerr << ex.what() << std::endl;
exitCode = -1;
}
// Delete the controller as the program exits to ensure memory is deallocated in the correct order
controller->Delete();
// Print a message to indicate the sample app has finished and if it was successful or not
SampleAppsHelper::PrintFooter(SAMPLE_APP_NAME, exitCode);
return exitCode;
}
void MoveVelocity(double velocity)
void MoveTrapezoidal(double position, double vel, double accel, double decel)
Point-to-point trapezoidal move.
void MoveRelative(double relativePosition, double vel, double accel, double decel, double jerkPct)
Command a relative point-to-point S-Curve motion.
void MoveSCurve(double position, double vel, double accel, double decel, double jerkPct)
Command a point-to-point S-Curve motion.
Represents a single axis of motion control. This class provides an interface for commanding motion,...
Definition rsi.h:5518
Axis * AxisGet(int32_t axisNumber)
AxisGet returns a pointer to an Axis object and initializes its internals.
void MotionCountSet(int32_t motionCount)
Set the number of processed Motion Supervisors in the controller.
void SampleWait(uint32_t samples)
Wait for controller firmware to execute samples.
void Delete(void)
Delete the MotionController and all its objects.
Represents the RMP soft motion controller. This class provides an interface to general controller con...
Definition rsi.h:795
void ClearFaults()
Clear all faults for an Axis or MultiAxis.
void AmpEnableSet(bool enable)
Enable all amplifiers.
int32_t MotionDoneWait()
Waits for a move to complete.
bool StatusBitGet(RSIEventType bitMask)
Return the state of a status bit.
static void PrintFooter(std::string sampleAppName, int exitCode)
Print a message to indicate the sample app has finished and if it was successful or not.
static void CheckErrors(RapidCodeObject *rsiObject)
Checks for errors in the given RapidCodeObject and throws an exception if any non-warning errors are ...
static void PrintHeader(std::string sampleAppName)
Print a start message to indicate that the sample app has started.
static void SetupController(MotionController *controller)
Setup the controller with user defined axis counts and configuration.
static MotionController::CreationParameters GetCreationParameters()
Returns a MotionController::CreationParameters object with user-defined parameters.
CreationParameters for MotionController::Create.
Definition rsi.h:856
static constexpr int AXIS_COUNT
Number of axes to configure on the controller.
static constexpr int AXIS_X_INDEX
Index of the first axis to use in the sample apps.