APIs, concepts, guides, and more
MotionHoldReleasedBySoftwareAddress.cpp
1
22#include <thread>
23#include "rsi.h" // Import our RapidCode Library.
24#include "SampleAppsHelper.h" // Import our SampleApp helper functions.
25#include "SampleApps.h"
26
27using namespace RSI::RapidCode; // Import the RapidCode namespace
28
30int MotionHoldReleasedBySoftwareAddress::Run()
31{
33 /* CONSTANTS */
34 // *NOTICE* The following constants must be configured before attempting to run with hardware.
35
36 // Axis configuration parameters
37 const int AXIS_COUNT = 1;
38 const int AXIS_NUMBER = 0;
39
40 const double USER_UNITS = 1048576;
41
42 // Index for the address in the user buffer to use for the motion hold
43 const int USER_BUFFER_INDEX = 0;
44
45 // Motion parameters
46 const double MOTION_DISTANCE = 0.25;
47
48 const std::chrono::duration HOLD_TIME = std::chrono::milliseconds(2000);
49
50 // To run with hardware, set the USE_HARDWARE flag to true AFTER you have configured the parameters above and taken proper safety precautions.
51 USE_HARDWARE = false;
52
53 // The rmpPath only needs to be set if the project directory is different than the rapid setup directory.
54 // *NOTICE* When setting the rmpPath, be sure to uncomment the rmpPath arguement in MotionController::CreateFromSoftware() in the Run() function.
55 const char rmpPath[] = "C:\\RSI\\X.X.X\\"; // Insert the path location of the RMP.rta (usually the RapidSetup folder)"
56
57 /* SAMPLE APP BODY */
58
59 // Initialize MotionController class.
60 MotionController* controller = MotionController::CreateFromSoftware(/*rmpPath*/); // NOTICE: Uncomment "rmpPath" if project directory is different than rapid setup directory.
61 SampleAppsHelper::CheckErrors(controller); // [Helper Function] Check that the axis has been initialize correctly.
62
63 // Setup the controller for the appropriate hardware configuration.
64 if (USE_HARDWARE)
65 {
67 }
68 else
69 {
70 SampleAppsHelper::SetupControllerForPhantoms(controller, AXIS_COUNT, { AXIS_NUMBER });
71 }
72
73 try
74 {
75 Axis* axis = controller->AxisGet(AXIS_NUMBER); // Initialize Axis Class. (Use RapidSetup Tool to see what is your axis number)
76 SampleAppsHelper::CheckErrors(axis); // [Helper Function] Check that the axis has been initialize correctly.
77
78 // GET AXIS READY
79 axis->UserUnitsSet(USER_UNITS); // Specify the counts per Unit.
80 axis->PositionSet(0); // Make sure motor starts at position 0 every time.
81 axis->ErrorLimitTriggerValueSet(1); // Set the position error trigger value
82 axis->Abort(); // If there is any motion happening, abort it.
83 axis->ClearFaults(); // Clear faults.>
84 axis->AmpEnableSet(true); // Enable the motor.
85
86 // GET AN AVAILABLE MEMORY ADDRESS
87 uint64_t userBufferAddress = controller->AddressGet(RSIControllerAddressType::RSIControllerAddressTypeUSER_BUFFER, USER_BUFFER_INDEX);
88
89 // SET MOTION HOLD ON AVAILABLE SOFTWARE ADDRESS
90 axis->MotionHoldTypeSet(RSIMotionHoldType::RSIMotionHoldTypeCUSTOM); // Use TypeCUSTOM to hold execution based on a particular bit turning ON or OFF.
91 axis->MotionHoldUserAddressSet(userBufferAddress); // Specify the available hostAddress . This address' value will be used to evaluate the motion hold condition.
92 axis->MotionHoldUserMaskSet(0x1); // Specify the bit you want to mask/watch from the MotionHoldUserAddressSet' address value (this evaluates using a logic AND)
93 axis->MotionHoldUserPatternSet(0x1); // Specify the bit value that will release the motion hold. (When this value is met, motion hold will be released)
94
95 // Check the condition to be false at first
96 if (controller->MemoryGet(userBufferAddress) != 0x0) // Check Available host address value is mask to be false (in this case 0x0)
97 {
98 controller->MemorySet(userBufferAddress, 0x0); // if not, mask it to false value/condition (in this case 0x0)
99 }
100
101 // COMMAND MOTION (NO HOLD YET)
102 printf("\nCommanding Motion...\n");
103 axis->MoveRelative(MOTION_DISTANCE); // Command simple relative motion. (This motion will have no HOLD)
104
105 // SET MOTION HOLD
106 printf("\nSetting Motion Hold...\n");
107 axis->MotionAttributeMaskOnSet(RSIMotionAttrMask::RSIMotionAttrMaskHOLD); // Set the HOLD motion attribute mask ON. (This initializes the HOLD on a particular motion)
108
109 // COMMAND MOTION AGAIN (MOTION WILL BE HELD)
110 printf("Commanding Motion...\n");
111 axis->MoveRelative(MOTION_DISTANCE); // Command simple relative motion. (This motion will be HOLD by the condition above)
112 std::this_thread::sleep_for(HOLD_TIME); // Sleep for 3 second before releasing motion hold.
113
114 // RELEASE MOTION HOLD (MOTION WILL START)
115 printf("Releasing Motion Hold...\n");
116 controller->MemorySet(userBufferAddress, 0x1); // Release Motion Hold by specifying the host address value to SET Condition (in this case 0x10000)
117 axis->MotionDoneWait(); // Wait for motion to be done
118 controller->MemorySet(userBufferAddress, 0x0); // Specify host address value back to false value/condition (in this case 0x0)
119
120 // CLEAR MOTION HOLD
121 printf("\nClearing Motion Hold...\n");
122 axis->MotionAttributeMaskOffSet(RSIMotionAttrMask::RSIMotionAttrMaskHOLD); // Set the HOLD motion attribute mask OFF. (This will clear any motion HOLDS that were set on this Axis)
123
124 // COMMAND MOTION AGAIN (NO HOLD)
125 printf("Commanding Motion...\n");
126 axis->MoveRelative(-2 * MOTION_DISTANCE); // This motion will have no HOLD since the previous line has set the motion attribute mask OFF.
127 axis->MotionDoneWait(); // Wait for Motion to be completed.
128
129 // Abort and Clear Faults
130 axis->Abort();
131 axis->ClearFaults();
132 }
133 catch (RsiError const& err)
134 {
135 printf("%s\n", err.text); // If there are any exceptions/issues this will be printed out.
136 return -1;
137 }
138 controller->Delete(); // Delete the controller as the program exits to ensure memory is deallocated in the correct order.
139 return 0;
141}
void UserUnitsSet(double countsPerUserUnit)
Sets the number of counts per User Unit.
void ErrorLimitTriggerValueSet(double triggerValue)
Set the Position Error Limit trigger value.
void PositionSet(double position)
Set the Command and Actual positions.
void MoveRelative(double relativePosition, double vel, double accel, double decel, double jerkPct)
Command a relative 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 MemorySet(uint64_t address, int32_t data)
Write a value to controller memory.
uint64_t AddressGet(RSIControllerAddressType type)
Get the an address for some location on the MotionController.
int32_t MemoryGet(uint64_t address)
Read controller memory.
static MotionController * CreateFromSoftware()
Initialize and start the RMP EtherCAT controller.
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.
void Abort()
Abort an axis.
int32_t MotionDoneWait()
Waits for a move to complete.
void MotionHoldUserMaskSet(int32_t holdMask)
Sets the Motion Hold User bit mask.
void MotionHoldUserPatternSet(int32_t pattern)
Sets the Motion Hold User pattern bit mask.
void MotionAttributeMaskOffSet(RSIMotionAttrMask maskOff)
Turn off a particular motion attribute mask.
void MotionHoldTypeSet(RSIMotionHoldType type)
Set the motion hold type.
void MotionHoldUserAddressSet(uint64_t address)
Sets the Motion Hold User Address.
void MotionAttributeMaskOnSet(RSIMotionAttrMask maskOn)
Turn on a particular motion attribute mask.
Represents the error details thrown as an exception by all RapidCode classes. This class contains an ...
Definition rsi.h:107
@ RSIControllerAddressTypeUSER_BUFFER
First location in User Buffer.
@ RSIMotionAttrMaskHOLD
Prevents the move from executing until a specific trigger condition is met. See MotionHold methods.
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.