APIs, concepts, guides, and more
PointToPoint.cpp
1
12#include <cmath>
13
14#include "SampleAppsHelper.h" // Import our helper functions.
15#include "rsi.h" // Import our RapidCode Library.
16
17using namespace RSI::RapidCode; // Import the RapidCode namespace
18
19// Helper function to wait for the axis to reach velocity when performing a velocity move or move with final velocity
20static void HelperAtVelocityWait(MotionController *controller, Axis *axis, const int timeout)
21{
22 // The number of samples to wait before checking if the axis is at velocity
23 const int WAIT_SAMPLES = 100;
24
25 int samplesWaited = 0;
26 while (axis->StatusBitGet(RSIEventType::RSIEventTypeMOTION_AT_VELOCITY) == 0)
27 {
28 controller->SampleWait(WAIT_SAMPLES);
29 samplesWaited += WAIT_SAMPLES;
30 if (samplesWaited > timeout)
31 {
32 throw std::runtime_error("Timeout waiting for motion to reach velocity");
33 }
34 }
35}
36
37int main()
38{
39 // Print a start message to indicate that the sample app has started
40 const std::string SAMPLE_APP_NAME = "Motion: Point-to-Point";
41 SampleAppsHelper::PrintHeader(SAMPLE_APP_NAME);
42
43 /* CONSTANTS */
45 // *NOTICE* The following constants must be configured before attempting to run with hardware.
46 const int NUM_AXES = 1; // The number of axes to configure
47 const int AXIS_INDEX = 0; // The index of the axis to configure
48
49 // Motion Parameters
50 const double POSITION_0 = 0;
51 const double POSITION_1 = 0.5;
52 const double VELOCITY = 1;
53 const double ACCELERATION = 10;
54 const double DECELERATION = 10;
55 const double JERK_PERCENT = 50;
56 const double FINAL_VELOCITY = 0.5;
58
59 /* RAPIDCODE INITIALIZATION */
60 // Create the controller
62 MotionController *controller = MotionController::Create(&params);
63
64 // Set the exit code to an error value.
65 int exitCode = -1;
66 try // Ensure that the controller is deleted if an error occurs.
67 {
68 // Prepare the controller as defined in SampleAppsHelper.h depending on the configuration
70 SampleAppsHelper::SetupController(controller, NUM_AXES);
71
72 /* SAMPLE APP BODY */
73
74 /* SET CONTROLLER OBJECT COUNTS HERE*/
75 // Normally you would set the number of axes here, but for samples that is handled in the SampleAppsHelper::SetupController function
76 // controller->AxisCountSet(NUM_AXES);
77
78 // Get the axes
79 Axis *axis = controller->AxisGet(AXIS_INDEX);
81
82 // Specify a timeout for waiting for the motion to complete
83 const int32_t TIMEOUT = 10000;
84
85 // Clear faults and enable the motor
86 axis->ClearFaults();
87 axis->AmpEnableSet(true);
88
89 /* Absolute (Trapezoidal) Motion */
91 std::cout << "Absolute (Trapezoidal) Motion:" << std::endl;
92 std::cout << "Moving to position: " << POSITION_1 << std::endl;
93 axis->MoveTrapezoidal(POSITION_1, VELOCITY, ACCELERATION, DECELERATION);
94 axis->MotionDoneWait(TIMEOUT);
95 std::cout << "Motion Complete" << std::endl;
96
97 std::cout << "Moving back to position: " << POSITION_0 << std::endl;
98 axis->MoveTrapezoidal(POSITION_0, VELOCITY, ACCELERATION, DECELERATION);
99 axis->MotionDoneWait(TIMEOUT);
100 std::cout << "Motion Complete\n" << std::endl;
102
103 /* Relative Motion */
105 std::cout << "Relative Motion:" << std::endl;
106 std::cout << "Moving to position: " << POSITION_1 << std::endl;
107 axis->MoveRelative(POSITION_1 - POSITION_0, VELOCITY, ACCELERATION, DECELERATION, JERK_PERCENT);
108 axis->MotionDoneWait(TIMEOUT);
109 std::cout << "Motion Complete" << std::endl;
110
111 std::cout << "Moving back to position: " << POSITION_0 << std::endl;
112 axis->MoveRelative(POSITION_0 - POSITION_1, VELOCITY, ACCELERATION, DECELERATION, JERK_PERCENT);
113 axis->MotionDoneWait(TIMEOUT);
114 std::cout << "Motion Complete\n" << std::endl;
116
117 /* SCurve Motion */
119 std::cout << "SCurve Motion:" << std::endl;
120 std::cout << "Moving to position: " << POSITION_1 << std::endl;
121 axis->MoveSCurve(POSITION_1);
122 axis->MotionDoneWait(TIMEOUT);
123 std::cout << "Motion Complete" << std::endl;
124
125 std::cout << "Moving back to position: " << POSITION_0 << std::endl;
126 axis->MoveSCurve(POSITION_0);
127 axis->MotionDoneWait(TIMEOUT);
128 std::cout << "Motion Complete\n" << std::endl;
130
131 /* SCurve Motion with Final Velocity */
133 std::cout << "SCurve Motion with Final Velocity:" << std::endl;
134 std::cout << "Moving to position: " << POSITION_1 << std::endl;
135 std::cout << "Final Velocity: " << FINAL_VELOCITY << std::endl;
136 axis->MoveSCurve(POSITION_1, VELOCITY, ACCELERATION, DECELERATION, JERK_PERCENT, FINAL_VELOCITY);
137 HelperAtVelocityWait(controller, axis, TIMEOUT);
138 std::cout << "Motion Complete" << std::endl;
139
140 std::cout << "Moving back to position: " << POSITION_0 << std::endl;
141 std::cout << "Final Velocity: " << 0 << std::endl;
142 axis->MoveSCurve(POSITION_0, VELOCITY, ACCELERATION, DECELERATION, JERK_PERCENT, 0);
143 axis->MotionDoneWait(TIMEOUT);
144 std::cout << "Motion Complete\n" << std::endl;
146
147 /* Velocity Move */
149 std::cout << "Velocity Move:" << std::endl;
150 std::cout << "Accelerating to velocity: " << VELOCITY << std::endl;
151 axis->MoveVelocity(VELOCITY, ACCELERATION);
152 HelperAtVelocityWait(controller, axis, TIMEOUT);
153 std::cout << "Motion Complete" << std::endl;
154
155 std::cout << "Decelerating to velocity: " << 0 << std::endl;
156 axis->MoveVelocity(0, DECELERATION);
157 HelperAtVelocityWait(controller, axis, TIMEOUT);
158 std::cout << "Motion Complete" << std::endl;
160
161 // Return to the original position
162 axis->MoveTrapezoidal(POSITION_0, VELOCITY, ACCELERATION, DECELERATION);
163 axis->MotionDoneWait(TIMEOUT);
164
165 // Disable the motor
166 axis->AmpEnableSet(false);
167
168 exitCode = 0; // Set the exit code to success.
169 }
170 catch (const std::exception &ex)
171 {
172 std::cerr << ex.what() << std::endl;
173 exitCode = -1;
174 }
175 // Clean up the controller and any other objects as needed
176 SampleAppsHelper::Cleanup(controller);
177
178 // Delete the controller as the program exits to ensure memory is deallocated in the correct order
179 controller->Delete();
180
181 // Print a message to indicate the sample app has finished and if it was successful or not
182 SampleAppsHelper::PrintFooter(SAMPLE_APP_NAME, exitCode);
183
184 return exitCode;
185}
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.
static MotionController * Create()
Initialize and start the RMP EtherCAT 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: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