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 const std::string SAMPLE_APP_NAME = "Motion: Point-to-Point";
40
41 // Print a start message to indicate that the sample app has started
42 SampleAppsHelper::PrintHeader(SAMPLE_APP_NAME);
43
44 // *NOTICE* The following constants must be configured before attempting to run with hardware.
46 // Motion Parameters
47 const double POSITION_0 = 0;
48 const double POSITION_1 = 0.5;
49 const double VELOCITY = 1;
50 const double ACCELERATION = 10;
51 const double DECELERATION = 10;
52 const double JERK_PERCENT = 50;
53 const double FINAL_VELOCITY = 0.5;
55
56 /* RAPIDCODE INITIALIZATION */
57
58 // Create the controller
60 MotionController *controller = MotionController::Create(&params);
61
62 int exitCode = -1; // Set the exit code to an error value.
63 try // Ensure that the controller is deleted if an error occurs.
64 {
66
67 // Prepare the controller and axes as defined in SampleAppsHelper.h
69
70 /* SAMPLE APP BODY */
71
72 /* Configure the controller object counts */
74
75 // Get the axes
76 Axis *axis = controller->AxisGet(SampleAppsConfig::AXIS_X_INDEX);
78
79 // Specify a timeout for waiting for the motion to complete
80 const int32_t TIMEOUT = 10000;
81
82 // Clear faults and enable the motor
83 axis->ClearFaults();
84 axis->AmpEnableSet(true);
85
86 /* Absolute (Trapezoidal) Motion */
88 std::cout << "Absolute (Trapezoidal) Motion:" << std::endl;
89 std::cout << "Moving to position: " << POSITION_1 << std::endl;
90 axis->MoveTrapezoidal(POSITION_1, VELOCITY, ACCELERATION, DECELERATION);
91 axis->MotionDoneWait(TIMEOUT);
92 std::cout << "Motion Complete" << std::endl;
93
94 std::cout << "Moving back to position: " << POSITION_0 << std::endl;
95 axis->MoveTrapezoidal(POSITION_0, VELOCITY, ACCELERATION, DECELERATION);
96 axis->MotionDoneWait(TIMEOUT);
97 std::cout << "Motion Complete\n" << std::endl;
99
100 /* Relative Motion */
102 std::cout << "Relative Motion:" << std::endl;
103 std::cout << "Moving to position: " << POSITION_1 << std::endl;
104 axis->MoveRelative(POSITION_1 - POSITION_0, VELOCITY, ACCELERATION, DECELERATION, JERK_PERCENT);
105 axis->MotionDoneWait(TIMEOUT);
106 std::cout << "Motion Complete" << std::endl;
107
108 std::cout << "Moving back to position: " << POSITION_0 << std::endl;
109 axis->MoveRelative(POSITION_0 - POSITION_1, VELOCITY, ACCELERATION, DECELERATION, JERK_PERCENT);
110 axis->MotionDoneWait(TIMEOUT);
111 std::cout << "Motion Complete\n" << std::endl;
113
114 /* SCurve Motion */
116 std::cout << "SCurve Motion:" << std::endl;
117 std::cout << "Moving to position: " << POSITION_1 << std::endl;
118 axis->MoveSCurve(POSITION_1);
119 axis->MotionDoneWait(TIMEOUT);
120 std::cout << "Motion Complete" << std::endl;
121
122 std::cout << "Moving back to position: " << POSITION_0 << std::endl;
123 axis->MoveSCurve(POSITION_0);
124 axis->MotionDoneWait(TIMEOUT);
125 std::cout << "Motion Complete\n" << std::endl;
127
128 /* SCurve Motion with Final Velocity */
130 std::cout << "SCurve Motion with Final Velocity:" << std::endl;
131 std::cout << "Moving to position: " << POSITION_1 << std::endl;
132 std::cout << "Final Velocity: " << FINAL_VELOCITY << std::endl;
133 axis->MoveSCurve(POSITION_1, VELOCITY, ACCELERATION, DECELERATION, JERK_PERCENT, FINAL_VELOCITY);
134 HelperAtVelocityWait(controller, axis, TIMEOUT);
135 std::cout << "Motion Complete" << std::endl;
136
137 std::cout << "Moving back to position: " << POSITION_0 << std::endl;
138 std::cout << "Final Velocity: " << 0 << std::endl;
139 axis->MoveSCurve(POSITION_0, VELOCITY, ACCELERATION, DECELERATION, JERK_PERCENT, 0);
140 axis->MotionDoneWait(TIMEOUT);
141 std::cout << "Motion Complete\n" << std::endl;
143
144 /* Velocity Move */
146 std::cout << "Velocity Move:" << std::endl;
147 std::cout << "Accelerating to velocity: " << VELOCITY << std::endl;
148 axis->MoveVelocity(VELOCITY, ACCELERATION);
149 HelperAtVelocityWait(controller, axis, TIMEOUT);
150 std::cout << "Motion Complete" << std::endl;
151
152 std::cout << "Decelerating to velocity: " << 0 << std::endl;
153 axis->MoveVelocity(0, DECELERATION);
154 HelperAtVelocityWait(controller, axis, TIMEOUT);
155 std::cout << "Motion Complete" << std::endl;
157
158 // Return to the original position
159 axis->MoveTrapezoidal(POSITION_0, VELOCITY, ACCELERATION, DECELERATION);
160 axis->MotionDoneWait(TIMEOUT);
161
162 // Disable the motor
163 axis->AmpEnableSet(false);
164
165 exitCode = 0; // Set the exit code to success.
166 }
167 catch (const std::exception &ex)
168 {
169 std::cerr << ex.what() << std::endl;
170 exitCode = -1;
171 }
172 // Delete the controller as the program exits to ensure memory is deallocated in the correct order
173 controller->Delete();
174
175 // Print a message to indicate the sample app has finished and if it was successful or not
176 SampleAppsHelper::PrintFooter(SAMPLE_APP_NAME, exitCode);
177
178 return exitCode;
179}
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.
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: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.