APIs, concepts, guides, and more
point-to-point.cpp
Note
See Motion: Point-to-Point 📜 for a detailed explanation of this sample code.
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.
/* This sample app demonstrates how to perform point-to-point motion including
absolute moves, relative moves, S-curve motion, and velocity motion.
*/
#include <cmath>
#include "helpers.h" // import our helper functions.
#include "config.h" // import our configuration.
#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;
{
controller->SampleWait(WAIT_SAMPLES);
samplesWaited += WAIT_SAMPLES;
if (samplesWaited > timeout)
{
throw std::runtime_error("Timeout waiting for motion to reach velocity");
}
}
}
int main()
{
// print a start message to indicate that the sample app has started
const std::string SAMPLE_APP_NAME = "📜 Motion: Point-to-Point";
Helpers::PrintHeader(SAMPLE_APP_NAME);
/* CONSTANTS */
// *NOTICE* The following constants must be configured before attempting to run with hardware.
const int NUM_AXES = 1; // the number of axes to configure
const int AXIS_INDEX = 0; // the index of the axis to configure
// motion Parameters
const double POSITION_0 = 0; // specify the start position
const double POSITION_1 = 0.5; // specify the position to move to
const double VELOCITY = 1; // specify your velocity - units: UserUnits/Sec
const double ACCELERATION = 10; // specify your acceleration - units: UserUnits/Sec^2
const double DECELERATION = 10; // specify your deceleration - units: UserUnits/Sec^2
const double JERK_PERCENT = 50; // specify your jerk percent (0.0 to 100.0)
const double FINAL_VELOCITY = 0.5; // specify your final velocity - units: UserUnits/Sec
/* RAPIDCODE INITIALIZATION */
// create the controller
// set the exit code to an error value.
int exitCode = -1;
try // Ensure that the controller is deleted if an error occurs.
{
// prepare the controller as defined in config.h depending on the configuration
Helpers::CheckErrors(controller);
Config::SetupController(controller, NUM_AXES);
/* SAMPLE APP BODY */
/* SET CONTROLLER OBJECT COUNTS HERE*/
// normally you would set the number of axes here, but for samples that is handled in the Config::SetupController function
// controller->AxisCountSet(NUM_AXES);
// get the axes
Axis *axis = controller->AxisGet(AXIS_INDEX);
// 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;
}
// clean up the controller and any other objects as needed
Config::Cleanup(controller);
// 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
Helpers::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)
Represents a single axis of motion control. This class provides an interface for commanding motion,...
Definition rsi.h:5863
static MotionController * Create(CreationParameters *creationParameters)
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
void ClearFaults()
Clear all faults for an Axis or MultiAxis.
int32_t MotionDoneWait()
Waits for a move to complete.
bool StatusBitGet(RSIEventType bitMask)
Return the state of a status bit.
int32_t AmpEnableSet(bool enable, int32_t ampActiveTimeoutMilliseconds=AmpEnableTimeoutMillisecondsDefault, bool overrideRestrictedState=false)
Enable all amplifiers.
void SetupController(MotionController *controller, int numAxes=0)
Setup the controller and check if the network is in the correct state for the configuration.
Definition config.h:134
MotionController::CreationParameters GetCreationParameters()
Returns a MotionController::CreationParameters object with user-defined parameters.
Definition config.h:68
void Cleanup(MotionController *controller)
[SetupController]
Definition config.h:193
void CheckErrors(RapidCodeObject *rsiObject, const std::source_location &location=std::source_location::current())
Checks for errors in the given RapidCodeObject and throws an exception if any non-warning errors are ...
Definition helpers.h:32
void PrintHeader(std::string sampleAppName)
[NetworkShutdown]
Definition helpers.h:146
void PrintFooter(std::string sampleAppName, int exitCode)
[PrintHeader]
Definition helpers.h:162
CreationParameters for MotionController::Create.
Definition rsi.h:866