APIs, concepts, guides, and more
InputOutput.cs
1
62using RSI.RapidCode.dotNET; // Import our RapidCode Library.
63using NUnit.Framework;
64using System;
65
66#if DOXYGEN
67// needed for docs generation
68using RSI.RapidCode;
69#else
70// needed for project to compile (NetworkNode alias maps to RapidCodeNetworkNode for Doxygen)
71using NetworkNode = RSI.RapidCode.dotNET.RapidCodeNetworkNode;
72#endif
73
75[TestFixture]
76[Category("Software")]
77class InputOutput : SampleAppTestBase
78{
79
80 [Test, Timeout(Constants.MAX_TEST_TIME)]
81 [Ignore("Needs a network with a real node to run, but we want the snippet for docs. At least it's compiled!")]
82 public void GeneralPurposeIO()
83 {
84 Assert.DoesNotThrow(() =>
85 {
87 const int nodeIndex = 0;
88 const int digitalInIndex = 0;
89 const int digitalOutIndex = 0;
90 const int analogIndex = 0;
91
92 // get node
93 NetworkNode node = controller.NetworkNodeGet(nodeIndex);
95
96 // digital io
97 bool dIn = node.DigitalInGet(digitalInIndex);
98 bool dOut = node.DigitalOutGet(digitalOutIndex);
99 node.DigitalOutSet(digitalOutIndex, true);
100
101 // analog io
102 int aIn = node.AnalogInGet(analogIndex);
103 int aOut = node.AnalogOutGet(analogIndex);
104 node.AnalogOutSet(analogIndex, 1234);
106 });
107 }
108
109 [Test, Timeout(Constants.MAX_TEST_TIME)]
110 public void DedicatedIO()
111 {
113 // Retrieve dedicated inputs with generic and specific function.
114 Console.WriteLine("RSIMotorDedicatedInLIMIT_HW_NEG: {0} and {1}",
115 axis.DedicatedInGet(RSIMotorDedicatedIn.RSIMotorDedicatedInLIMIT_HW_NEG),
116 axis.NegativeLimitGet());
117
118 Console.WriteLine("RSIMotorDedicatedInLIMIT_HW_POS: {0} and {1}",
119 axis.DedicatedInGet(RSIMotorDedicatedIn.RSIMotorDedicatedInLIMIT_HW_POS),
120 axis.PositiveLimitGet());
121
122 Console.WriteLine("RSIMotorDedicatedInHOME: {0} and {1}",
123 axis.DedicatedInGet(RSIMotorDedicatedIn.RSIMotorDedicatedInHOME),
124 axis.HomeSwitchGet());
125
126 Console.WriteLine("RSIMotorDedicatedInAMP_FAULT: {0} and {1}",
127 axis.DedicatedInGet(RSIMotorDedicatedIn.RSIMotorDedicatedInAMP_FAULT),
128 axis.AmpFaultGet());
129
130 Console.WriteLine("RSIMotorDedicatedInAMP_ACTIVE: {0} and {1}",
131 axis.DedicatedInGet(RSIMotorDedicatedIn.RSIMotorDedicatedInAMP_ACTIVE),
132 axis.AmpEnableGet());
134 }
135
136 [Test, Timeout(Constants.MAX_TEST_TIME)]
137 public void NetworkInputsAndOutputs()
138 {
140 // Get Input Values
141 int inputCount = controller.NetworkInputCountGet(); // Get number of Network Inputs (PDOs)
142
143 for (int i = 0; i < inputCount; i++)
144 {
145 int size = controller.NetworkInputBitSizeGet(i); // Read Input BitSize
146 int offset = controller.NetworkInputBitOffsetGet(i); // Read Input BitOffset
147 string name = controller.NetworkInputNameGet(i); // Read Input Name
148 UInt64 value = controller.NetworkInputValueGet(i); // Read Input Value
149 }
150
151 // Get Output Values
152 int outputCount = controller.NetworkOutputCountGet(); // Get number of Network Outputs (SDOs)
153
154 for (int i = 0; i < outputCount; i++)
155 {
156 int size = controller.NetworkOutputBitSizeGet(i); // Read Output BitSize
157 int offset = controller.NetworkOutputBitOffsetGet(i); // Read Output BitOffset
158 string name = controller.NetworkOutputNameGet(i); // Read Output Name
159 UInt64 value = controller.NetworkOutputSentValueGet(i); // Read Output Value
160 controller.NetworkOutputOverrideValueSet(i, value);
161 }
163 }
164
165
166 //TODO expand examples once iopoint is expanded.
167 [Test]
168 public void IOPoints()
169 {
172 const int NODE_INDEX = 0; // The EtherCAT Node we will be communicating with
173 //const int INPUT_INDEX = 0; // The PDO Index in that Node
174 const int OUTPUT_INDEX = 0; // The PDO Index in that Node
175
176 IOPoint output0 = IOPoint.CreateDigitalOutput(controller.NetworkNodeGet(NODE_INDEX), OUTPUT_INDEX); // Automatically gets the memory index of a specified node and input index
177
179 output0.Set(false);
180
182 controller.SampleWait(1);
183 Assert.That(output0.Get(), Is.False, "The getter function should return a value equal to false");
185 }
186
187 //TODO expand examples once iopoint is expanded.
188 [Test]
189 public void IOPointUserBuffer()
190 {
193 const int INPUT_INDEX = 0;
194 const int OUTPUT_INDEX = 1; // The PDO Index in that Node
195
196 UInt64 userBufferAddress = controller.AddressGet(RSIControllerAddressType.RSIControllerAddressTypeUSER_BUFFER, 0); // Grabing an memory address to store a simulated IO point.
197
198 IOPoint input0 = IOPoint.CreateDigitalInput(controller, userBufferAddress, INPUT_INDEX); // Automatically gets the memory index of a specified node and input index
199 IOPoint output0 = IOPoint.CreateDigitalOutput(controller, userBufferAddress, OUTPUT_INDEX); // Automatically gets the memory index of a specified node and input index
200
201 //---ACT/ASSERT---
202 output0.Set(false);
203 Assert.That(output0.Get(), Is.False, "The getter function should return a value equal to false");
204 output0.Set(true);
205 Assert.That(output0.Get(), Is.True, "The getter function should return a value equal to true");
206 controller.MemorySet(input0.AddressGet(), 0);
207 Assert.That(input0.Get(), Is.False, "The getter function should return a value equal to false");
208 controller.MemorySet(input0.AddressGet(), 1);
209 Assert.That(input0.Get(), Is.True, "The getter function should return a value equal to true");
211 }
212
213 [Test]
214 public void SingleAxisSyncOutputs()
215 {
217 const int TOTAL_POINTS = 4; // total number of points
218 const int EMPTY_CT = -1; // Number of points that remains in the buffer before an e-stop
219 const int OUTPUT_INDEX = 0; // This is the index of the digital output that will go active when the user limit triggers.
220 const int NODE_INDEX = 0; // The EtherCAT Node we will be communicating with
221
222 double[] positions = { 1.0, 2.0, 3.0, 4.0 }; // These will be the streaming motion 5 positions.
223 double[] times = { 0.5, 0.1, 0.2, 0.4 }; // These will be the streaming motion 5 positions' time.
224 int outputEnableID = 2; // The motion element ID at which to set the output
225 int outputDisableID = 3; // The motion element ID at which to set the output
226
227 // Set up the inputs
228 // IOPoint output0 = IOPoint.CreateDigitalOutput(axis, RSIMotorGeneralIo.RSIMotorGeneralIo16); // Retrieve DOUT 1, Method 1: requires you know the io adress in memory, slightly faster
229 IOPoint output0 = IOPoint.CreateDigitalOutput(controller.NetworkNodeGet(NODE_INDEX), OUTPUT_INDEX); // Retrieve DOUT 1 Method 2: only need to know node index
230 output0.Set(false); // Set the output low
231
232 // Set up Sync Outputs
233 axis.StreamingOutputsEnableSet(true); // Enable streaming output.
234
235 // ENABLE the Sync Output(s)
236 axis.StreamingOutputAdd(output0, true, outputEnableID); // This will turn DOUT1 High when the streaming motion reaches its 3rd motion point.
237 axis.StreamingOutputAdd(output0, false, outputDisableID); // This will turn DOUT1 Low when the streaming motion reaches its 4th motion point.
238
239 // DISABLE the Sync Output(s)
240 // axis.StreamingOutputAdd(output0, false, outPutEnableID);
241
242 axis.MovePT(RSIMotionType.RSIMotionTypePT, positions, times, TOTAL_POINTS, EMPTY_CT, false, true); // Start Streaming Motion
243
244 while (!axis.MotionDoneGet())
245 {
246 if (axis.MotionIdExecutingGet() > outputEnableID && axis.CommandPositionGet() < outputEnableID)
247 {
248 Assert.That(output0.Get(), Is.EqualTo(true), "The output should be triggered");
249 }
250 else
251 {
252 Assert.That(output0.Get(), Is.EqualTo(false), "The output should NOT be triggered");
253 }
254 }
255
256 axis.StreamingOutputsEnableSet(false); // Disable Sync Outputs.
257 axis.AmpEnableSet(false); // Disable the motor.
259 }
260}
static void CheckErrors(RapidCodeObject rsiObject)
Check if the RapidCodeObject has any errors.
Helper Functions for checking logged creation errors, starting the network, etc.
double CommandPositionGet()
Get the current command position.
bool AmpEnableGet()
Get the state of the Amp Enable Output.
bool HomeSwitchGet()
Get the current state of the Home switch input.
bool PositiveLimitGet()
Get the state of the Hardware Positive Limit input.
uint16_t MotionIdExecutingGet()
bool NegativeLimitGet()
Get the state of the Hardware Negative Limit input.
bool AmpFaultGet()
Get the current state of the Amp Fault input.
bool DedicatedInGet(RSIMotorDedicatedIn motorDedicatedInNumber)
Read a digital input.
uint64_t AddressGet()
Get the Host Address for the I/O point.
void Set(bool state)
Set the state of a Digital Output.
static IOPoint * CreateDigitalInput(Axis *axis, RSIMotorDedicatedIn motorDedicatedInNumber)
Create a Digital Input from an Axis' Dedicated Input bits.
static IOPoint * CreateDigitalOutput(Axis *axis, RSIMotorDedicatedOut motorDedicatedOutNumber)
Create a Digital Output from an Axis' Dedicated Output bits.
bool Get()
Get the state of Digital Input or Output.
Represents one specific point: Digital Output, Digital Input, Analog Output, or Analog Input....
Definition rsi.h:11456
NetworkNode * NetworkNodeGet(int32_t nodeNumber)
NetworkNodeGet returns a pointer to a RapidCodeNetworkNode object using its node number and initializ...
void NetworkOutputOverrideValueSet(int32_t index, uint64_t outputValue)
Sets a PDO output directly.
int32_t NetworkInputBitOffsetGet(int32_t index)
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.
uint64_t NetworkInputValueGet(int32_t index)
uint64_t NetworkOutputSentValueGet(int32_t index)
Gets the value sent out over EtherCAT.
void SampleWait(uint32_t samples)
Wait for controller firmware to execute samples.
const char *const NetworkOutputNameGet(int32_t index)
Get the name of a PDO output.
int32_t NetworkOutputBitSizeGet(int32_t index)
Get the size (in bits) of a PDO output.
int32_t NetworkOutputBitOffsetGet(int32_t index)
Get the raw PDO offset for an output.
int32_t NetworkInputCountGet()
Get the number of PDO inputs found on the network.
int32_t NetworkInputBitSizeGet(int32_t index)
Get the size (in bits) of a network input.
const char *const NetworkInputNameGet(int32_t index)
Get the name of a PDO network input.
void DigitalOutSet(int32_t digitalOutNumber, bool state)
Set the state of a digital output.
bool DigitalInGet(int32_t digitalInNumber)
Get the state of a digital input.
int32_t AnalogInGet(int32_t analogChannel)
Get the value of an analog input.
bool DigitalOutGet(int32_t digitalOutNumber)
Get the state of a digital output.
int32_t AnalogOutGet(int32_t analogChannel)
Get the value of an analog output.
void AnalogOutSet(int32_t analogChannel, int32_t analogValue)
Set the value of an analog output.
Represents an EtherCAT node / SubDevice (Drive, I/O Block, etc) on the network. This class provides a...
Definition rsi.h:3853
void StreamingOutputAdd(int32_t onMask, int32_t offMask, uint64_t address)
void StreamingOutputsEnableSet(bool enable)
Sets whether Streaming Output is enabled (true) or disabled (false).
int32_t AmpEnableSet(bool enable, int32_t ampActiveTimeoutMilliseconds=AmpEnableTimeoutMillisecondsDefault, bool overrideRestrictedState=false)
Enable all amplifiers.
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.
bool MotionDoneGet()
Check to see if motion is done and settled.
RSIControllerAddressType
Used to get firmware address used in User Limits, Recorders, etc.
Definition rsienums.h:405
RSIMotionType
PT and PVT streaming motion types.
Definition rsienums.h:1037
RSIMotorDedicatedIn
Dedicated Input bits per motor.
Definition rsienums.h:888