APIs, concepts, guides, and more
PathMotion.cpp
1
28#include <iostream>
29#include <fstream>
30
31#include "rsi.h" // Import our RapidCode Library.
32#include "SampleAppsHelper.h" // Import our SampleApp helper functions.
33#include "SampleApps.h"
34
35using namespace RSI::RapidCode; // Import the RapidCode namespace
36
38int PathMotion::Run()
39{
41 /* CONSTANTS */
42 // *NOTICE* The following constants must be configured before attempting to run with hardware.
43
44 // Axis configuration parameters
45 const int AXIS_COUNT = (2);
46 const int AXIS_X = 0;
47 const int AXIS_Y = 1;
48
49 const double USER_UNITS = 1048576;
50
51 // Default file path for recording the path followed by the axes
52 const char* const PATH_MOTION_FILEPATH = "PathMotion.csv";
53
54 // To run with hardware, set the USE_HARDWARE flag to true AFTER you have configured the parameters above and taken proper safety precautions.
55 USE_HARDWARE = false;
56
57 /* SAMPLE APP BODY */
58
59 Axis* axisX;
60 Axis* axisY;
61 MultiAxis* multiAxisXY;
62
63 double running_x = 0;
64 double running_y = 0;
65
66 double line_A[2] = { running_x, running_y };
67 running_x += -0.5;
68 double line_B[2] = { running_x, running_y };
69 running_y += -0.5;
70 double line_C[2] = { running_x, running_y };
71
72 //curve down
73 running_y += (-0.25);
74 double arc_center_A[2] = { running_x,running_y };
75 running_x += (0.25);
76
77 //curve left
78 running_x += -0.25;
79 double arc_center_B[2] = { running_x,running_y };
80 running_y += -0.25;
81
82 //curve up
83 running_y += 0.25;
84 double arc_center_C[2] = { running_x,running_y };
85 running_x += -0.25;
86
87 //curve right
88 running_x += 0.25;
89 double arc_center_D[2] = { running_x,running_y };
90 running_y += 0.25;
91
92 running_x += -0.5;
93 running_y += 0.5;
94 double line_D[2] = { running_x, running_y };
95
96 // Create and initialize RsiController class
99
100 // Setup the controller for the appropriate hardware configuration.
101 if (USE_HARDWARE)
102 {
104 }
105 else
106 {
107 SampleAppsHelper::SetupControllerForPhantoms(controller, AXIS_COUNT, { AXIS_X, AXIS_Y });
108 }
109
110 try
111 {
112 // enable one MotionSupervisor for the MultiAxis
113 controller->MotionCountSet(controller->AxisCountGet() + 1);
114
115 // Get Axis X and Y respectively.
116 Axis* axisX = controller->AxisGet(AXIS_X);
118 axisX->PositionSet(0);
119 axisX->HomeMethodSet(RSIHomeMethod::RSIHomeMethodCURRENT_POSITION);
120 axisX->ErrorLimitActionSet(RSIAction::RSIActionNONE);
124 axisX->UserUnitsSet(USER_UNITS);
125 axisX->Abort();
126
127 Axis* axisY = controller->AxisGet(AXIS_Y);
129 axisY->PositionSet(0);
130 axisY->HomeMethodSet(RSIHomeMethod::RSIHomeMethodCURRENT_POSITION);
131 axisY->ErrorLimitActionSet(RSIAction::RSIActionNONE);
135 axisY->UserUnitsSet(USER_UNITS);
136 axisY->Abort();
137
138 // Initialize a MultiAxis, using the last MotionSupervisor.
139 MultiAxis* multiAxisXY = controller->MultiAxisGet(controller->MotionCountGet() - 1);
141 multiAxisXY->AxisAdd(axisX);
142 multiAxisXY->AxisAdd(axisY);
143
144 // make sure all axes are enabled and ready
145 multiAxisXY->Abort();
146 multiAxisXY->ClearFaults();
147 multiAxisXY->AmpEnableSet(true);
148
149 axisX->Home();
150 axisY->Home();
151
152 multiAxisXY->AmpEnableSet(false);
153
154 // set the trajectory info
155 multiAxisXY->VectorVelocitySet(1.0 * USER_UNITS);
156 multiAxisXY->VectorAccelerationSet(10.0 * USER_UNITS);
157 multiAxisXY->VectorDecelerationSet(10.0 * USER_UNITS);
158
159 // start path list
160 double start_positions[2] = { axisX->CommandPositionGet(),axisY->CommandPositionGet() };
161 multiAxisXY->PathListStart(start_positions);
162
163 multiAxisXY->ClearFaults();
164
165 // turn on blending (smooth corners)
166 multiAxisXY->PathBlendSet(true);
167
168 // Lines and arcs
169 multiAxisXY->PathLineAdd(line_A);
170 multiAxisXY->PathLineAdd(line_B);
171 multiAxisXY->PathLineAdd(line_C);
172 multiAxisXY->PathArcAdd(arc_center_A, -90.0);
173 multiAxisXY->PathArcAdd(arc_center_B, -90.0);
174 multiAxisXY->PathArcAdd(arc_center_C, -90.0);
175 multiAxisXY->PathArcAdd(arc_center_D, -90.0);
176 multiAxisXY->PathLineAdd(line_D);
177
178 // end path list
179 multiAxisXY->PathListEnd();
180
181 // make sure all axes are enabled and ready
182 multiAxisXY->ClearFaults();
183 multiAxisXY->AmpEnableSet(true);
184 // execute the motion
185 multiAxisXY->PathMotionStart();
186
187 // log positions
188 std::ofstream myfile;
189 myfile.open(PATH_MOTION_FILEPATH);
190 myfile << "samples,x,y,\n";
191
192 while (!multiAxisXY->MotionDoneGet()) {
193
194 myfile << controller->SampleCounterGet() << "," << axisX->CommandPositionGet() << "," << axisY->CommandPositionGet() << "\n";
195 controller->SampleWait(10);
196 }
197 // wait for motion to complete
198 multiAxisXY->MotionDoneWait();
199 multiAxisXY->AmpEnableSet(false);
200 myfile.close();
201 printf("\n%s\n", PATH_MOTION_FILEPATH);
202 }
203 catch (RsiError const& err)
204 {
205 printf("\n%s\n", err.text);
206 return -1;
207 }
208 controller->Delete(); // Delete the controller as the program exits to ensure memory is deallocated in the correct order.
209 return 0;
211}
double CommandPositionGet()
Get the current command position.
void AmpDisableActionSet(RSIMotorDisableAction action)
Set the Amp Disable action.
void SoftwareNegLimitActionSet(RSIAction action)
Set the action that will occur when the Software Negative Limit Event triggers.
void UserUnitsSet(double countsPerUserUnit)
Sets the number of counts per User Unit.
void ErrorLimitActionSet(RSIAction action)
Set the action that will occur when the Error Limit Event triggers.
void Home()
Execute the homing routine.
void HomeMethodSet(RSIHomeMethod method)
Set the method to be used for homing.
void PositionSet(double position)
Set the Command and Actual positions.
void SoftwarePosLimitActionSet(RSIAction action)
Set the action that will occur when the Software Positive Limit Event triggers.
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 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.
static MotionController * CreateFromSoftware()
Initialize and start the RMP EtherCAT controller.
void Delete(void)
Delete the MotionController and all its objects.
int32_t SampleCounterGet()
Get the current value of the controller's processor's sample counter.
MultiAxis * MultiAxisGet(int32_t motionSupervisorNumber)
MultiAxisGet returns a pointer to a MultiAxis object and initializes its internals.
int32_t AxisCountGet()
Get the number of axes processing.
int32_t MotionCountGet()
Get the number of Motion Supervisors available in the firmware.
Represents the RMP soft motion controller. This class provides an interface to general controller con...
Definition rsi.h:796
void PathListStart(const double *const startPosition)
Start a line and arc point list for path motion.
void PathMotionStart()
Start the path motion.
void PathArcAdd(const double *const center, double angle)
Add an arc segment to the path.
void VectorDecelerationSet(double deceleration)
Set the vector deceleration.
void PathLineAdd(const double *const position)
Add a line segment to the path.
void PathBlendSet(bool blend)
Set the blending attribute.
void VectorAccelerationSet(double acceleration)
Set the vector acceleration.
void PathListEnd()
End a line and arc point list for path motion.
void AxisAdd(Axis *axis)
Add an Axis to a MultiAxis group.
void VectorVelocitySet(double velocity)
Set the vector velocity.
Represents multiple axes of motion control, allows you to map two or more Axis objects together for e...
Definition rsi.h:10244
void ClearFaults()
Clear all faults for an Axis or MultiAxis.
void AmpEnableSet(bool enable)
Enable all amplifiers.
void Abort()
Abort an axis.
int32_t MotionDoneWait()
Waits for a move to complete.
bool MotionDoneGet()
Check to see if motion is done and settled.
Represents the error details thrown as an exception by all RapidCode classes. This class contains an ...
Definition rsi.h:107
@ RSIMotorDisableActionNONE
No action taken when disabled.
@ RSIActionNONE
None - do not perform any action.
static void SetupControllerForHardware(MotionController *controller)
Sets up the controller for hardware use by resetting it and starting the network.
static void CheckErrors(RapidCodeObject *rsiObject)
Checks for errors in the given RapidCodeObject and throws an exception if any non-warning errors are ...
static void SetupControllerForPhantoms(MotionController *controller, int axisCount, std::vector< int > axisNums)
Sets up the controller for phantom axes, including configuring specified axes as phantom.