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, S-Curve 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()
{
// Print a start message to indicate that the sample app has started
const std::string SAMPLE_APP_NAME = "Motion: Point-to-Point";
/* 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;
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);
// 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 SampleAppsHelper.h depending on the configuration
SampleAppsHelper::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 SampleAppsHelper::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
// 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:5549
Axis * AxisGet(int32_t axisNumber)
AxisGet returns a pointer to an Axis object and initializes its internals.
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:796
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 Cleanup(MotionController *controller)
Cleanup the controller and restore the object counts to the original values.
static MotionController::CreationParameters GetCreationParameters()
Returns a MotionController::CreationParameters object with user-defined parameters.
static void SetupController(MotionController *controller, int numAxes=0)
Setup the controller with user defined axis counts and configuration.
CreationParameters for MotionController::Create.
Definition rsi.h:857