The RMP Motion Controller APIs
SyncOutputWithMotion.cpp
1
17#include <cassert>
18#include "rsi.h" // Import our RapidCode Library.
19#include "HelperFunctionsCpp.h" // Import our SampleApp helper functions.
20#include <cmath>
21#include "SampleApps.h"
22
23using namespace RSI::RapidCode;
24
26int SyncOutputWithMotion::Run()
27{
28 /* CONSTANTS */
29 // *NOTICE* The following constants must be configured before attempting to run with hardware.
30
31 // Axis configuration parameters
32 const int AXIS_COUNT = 1; // number of axes
33 const int AXIS_NUMBER = 0; // axis number
34
35 const double USER_UNITS = 1048576; // encoder counts per rev (set as appropiate)
36
37 // Motion parameters
38 const double TIME_SLICE = 0.01; // 0.01s = 10ms
39 const int REVS = 2; // number of revolutions
40 const int RPS = 1; // revs / sec
41 const int TOTAL_POINTS = (int)(REVS / TIME_SLICE / RPS); // total number of points
42 const int EMPTY_CT = -1; // Number of points that remains in the buffer before an e-stop
43
44 // To run with hardware, set the USE_HARDWARE flag to true AFTER you have configured the parameters above and taken proper safety precautions.
45 USE_HARDWARE = false;
46
47 /* SAMPLE APP BODY */
48
49 // Initizalize the controller from software w/ multiple axes
52
53 // Setup the controller for the appropriate hardware configuration.
54 if (USE_HARDWARE)
55 {
57 }
58 else
59 {
60 HelperFunctionsCpp::SetupControllerForPhantoms(controller, AXIS_COUNT, { AXIS_NUMBER });
61 }
62
63 MultiAxis* multiAxis;
64
65 try {
66 // add an additional axis for the multiaxis supervisor
67 controller->MotionCountSet(controller->AxisCountGet() + 1);
68
69 // create the multiaxis using the ID of the first free axis (0 indexed)
70 multiAxis = controller->MultiAxisGet(controller->MotionCountGet() - 1);
72
73 // populate the multiaxis
74 Axis* axis = controller->AxisGet(AXIS_NUMBER);
76 axis->UserUnitsSet(USER_UNITS);
78 axis->EStopAbort();
79 axis->ClearFaults();
80 axis->PositionSet(0);
81
82 multiAxis->AxisAdd(axis);
83
84 // populate the positions and times
85 std::vector<double> positions, times;
86 for (int i = 0; i < TOTAL_POINTS; i += AXIS_COUNT)
87 {
88 positions.push_back(i * TIME_SLICE * RPS);
89 times.push_back(TIME_SLICE);
90 }
91
92
93 // prepare the controller (and drive)
94 multiAxis->Abort();
95 multiAxis->ClearFaults();
96 assert(multiAxis->StateGet() == RSIState::RSIStateIDLE);
97 multiAxis->AmpEnableSet(true);
98
99
100 // set up the inputs
102 // ensure the digital out is set low
103 output0->Set(0);
104
105 // The motion element ID at which to set the output
106 int outPutEnableID = TOTAL_POINTS / 2;
107
108
109 // enable streaming output
110 multiAxis->StreamingOutputsEnableSet(true);
111
112
113 // Enable the streaming output (The following are functionally equivalent)
114 //multiAxis->StreamingOutputAdd(output0->MaskGet(), 0, output0->AddressGet(), outPutEnableID);
115 multiAxis->StreamingOutputAdd(output0, true, outPutEnableID);
116
117 // Disable the output (The following are functionally equivalent)
118 //multiAxis->StreamingOutputAdd(0, output0->MaskGet(), output0->AddressGet(), outPutEnableID);
119 //multiAxis->StreamingOutputAdd(output0, false, outPutEnableID);
120
121
122 multiAxis->MovePT(RSIMotionType::RSIMotionTypePT, &positions[0], &times[0], TOTAL_POINTS, EMPTY_CT, false, true);
123
124 printf("Motion started. Waiting to complete.\n");
125 multiAxis->MotionDoneWait();
126 printf("Motion Complete. The outputs should have been set\n");
127
128 multiAxis->StreamingOutputsClear(); // cleanup for next run
129
130 multiAxis->EStopAbort();
131 }
132 catch (RsiError const& err) {
133 printf("\n%s\n", err.text);
134 return -1;
135 }
136 controller->Delete(); // Delete the controller as the program exits to ensure memory is deallocated in the correct order.
137 return 0;
138}
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.
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 PositionSet(double position)
Set the Command and Actual positions.
Represents a single axis of motion control. This class provides an interface for commanding motion,...
Definition rsi.h:5664
void Set(bool state)
Set the state of a Digital Output.
static IOPoint * CreateDigitalOutput(Axis *axis, RSIMotorDedicatedOut motorDedicatedOutNumber)
Create a Digital Output from an Axis' Dedicated Output bits.
Represents one specific point: Digital Output, Digital Input, Analog Output, or Analog Input....
Definition rsi.h:11319
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 * CreateFromSoftware()
Initialize and start the RMP EtherCAT controller.
void Delete(void)
Delete the MotionController and all its objects.
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:762
Axis * AxisGet(int32_t index)
Get the Axis class used in this MultiAxis class.
void AxisAdd(Axis *axis)
Add an Axis to a MultiAxis group.
Represents multiple axes of motion control, allows you to map two or more Axis objects together for e...
Definition rsi.h:10564
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 StreamingOutputAdd(int32_t onMask, int32_t offMask, uint64_t address)
RSIState StateGet()
Get the Axis or MultiAxis state.
void StreamingOutputsClear()
Clear the contents of the Streaming Output List.
void StreamingOutputsEnableSet(bool enable)
Sets whether Streaming Output is enabled (true) or disabled (false).
void MovePT(RSIMotionType type, const double *const position, const double *const time, int32_t pointCount, int32_t emptyCount, bool retain, bool final)
A move commanded by a list of position and time points.
void EStopAbort()
E-Stop, then abort an axis.
Represents the error details thrown as an exception by all RapidCode classes. This class contains an ...
Definition rsi.h:105
@ RSIMotionTypePT
simple Position and Time algorithm
@ RSIActionNONE
None - do not perform any action.
@ RSIStateIDLE
Idle, ready for anything.