APIs, concepts, guides, and more
VelocitySetByAnalogInputValue.cs
1
27using RSI.RapidCode.dotNET; // Import our RapidCode Library.
28using System;
29
30#if DOXYGEN // RSI internal documentation use only
31using RSI.RapidCode;
32#endif
33
35class VelocitySetByAnalogInputValue
36 {
37 static void Main(string[] args)
38 {
40 // GLOBAL VARIABLES
41 double analogMaxValue = 65536; // The analog inputs used are 16 bit wide.
42 double currentVelocity = 0; // Used to update velocity based on analog input value.
43 double analogCurrentValue = 0; // Used to store current analog input value.
44 double analogValuePrecentage = 0; // analogCurrentValue/anlogMaxValue.
45 double velocityAbsoluteSum = 0; // Absolute sum of min and max velocities.
46
47 // CONSTANTS
48 const int AXIS_NUMBER = 0; // Specify which axis/motor to control.
49 const int NODE_NUMBER = 0; // Specify which IO Terminal/Node to control. 0 for RSI AKD DemoBench
50 const int ANALOG_INPUT_0 = 0; // Specify which Analog Input to read.
51 const int MIN_VEL = -10; // Specify Min Velocity.
52 const int MAX_VEL = 10; // Specify Max Velocity.
53 const int ACC = 100; // Specify Acceleration/Deceleration value.
54 const int USER_UNITS = 1048576; // Specify your counts per unit / user units. (the motor used in this sample app has 1048576 encoder pulses per revolution)
55
56 // Initialize RapidCode Objects
57 MotionController controller = MotionController.CreateFromSoftware(/*@"C:\RSI\X.X.X\"*/); // Insert the path location of the RMP.rta (usually the RapidSetup folder)
58 HelperFunctions.CheckErrors(controller); // [Helper Function] Check that the controller has been initialized correctly.
59 HelperFunctions.StartTheNetwork(controller); // [Helper Function] Initialize the network.
60
61 Axis axis = controller.AxisGet(AXIS_NUMBER); // Initialize the axis.
62 HelperFunctions.CheckErrors(axis); // [Helper Function] Check that the axis has been initialized correctly.
63
64 IO ioNode = controller.IOGet(NODE_NUMBER); // Initialize the axis.
65 HelperFunctions.CheckErrors(ioNode); // [Helper Function] Check that the axis has been initialized correctly.
66
67 try
68 {
69 // CONSOLE OUT
70 Console.WriteLine("Velocity Move Initialized.");
71 Console.WriteLine("Max Speed = " + MAX_VEL);
72 Console.WriteLine("Min Speed = " + MIN_VEL);
73 Console.WriteLine("\nPress SPACEBAR to exit.");
74
75 // READY AXIS
76 axis.UserUnitsSet(USER_UNITS); // Specify the counts per Unit.
77 axis.ErrorLimitTriggerValueSet(1); // Specify the position error limit trigger. (Learn more about this on our support page)
78 axis.PositionSet(0); // Make sure motor starts at position 0 everytime.
79 axis.DefaultAccelerationSet(ACC); // Set Acceleration.
80 axis.DefaultDecelerationSet(ACC); // Set Deceleration.
81 axis.Abort(); // If there is any motion happening, abort it.
82 axis.ClearFaults(); // Clear faults.
83 axis.AmpEnableSet(true); // Enable the motor.
84
85 do
86 {
87 while (!Console.KeyAvailable)
88 {
89 velocityAbsoluteSum = Math.Abs(MIN_VEL) + Math.Abs(MAX_VEL);
90 analogCurrentValue = (double)ioNode.AnalogInGet(ANALOG_INPUT_0); // Get analog in value.
91 analogValuePrecentage = analogCurrentValue / analogMaxValue; // Get percentage of analog voltage.
92
93 /*
94 * REPRESENTATION OF ANALOG INPUT VALUE BASED ON DIGITAL OUTPUT VOLTAGE
95 *
96 * AI Value --> 0 ............ 32,769 ............ 65,536
97 * DO Voltage --> 0 ........8 9 10 -10 -9 -8...... 0
98 */
99
100 if (analogValuePrecentage * 100 > 99 || analogValuePrecentage * 100 < 1) // If the Analog Input value is close to 0 or 65,536 then velocity is ZERO.
101 currentVelocity = 0;
102
103 else if (analogValuePrecentage * 100 < 50) // If the Analog Input value is less than 50% of 65,536 then velocity varies from 0 to 10.
104 currentVelocity = velocityAbsoluteSum * analogValuePrecentage;
105
106 else // If the Analog Input value is greater than 50% of 65,536 then velocity varies from 0 to -10.
107 currentVelocity = -velocityAbsoluteSum + (velocityAbsoluteSum * analogValuePrecentage);
108
109 axis.MoveVelocity(currentVelocity); // Update Velocity.
110
111 }
112 } while (Console.ReadKey(true).Key != ConsoleKey.Spacebar); // If the Spacebar key is pressed, exit.
113
114 axis.Abort(); // Abort Motion.
115 }
116 catch (Exception e)
117 {
118 Console.WriteLine(e.Message); // If there are any exceptions/issues this will be printed out.
119 }
121 }
122 }
static void CheckErrors(RapidCodeObject rsiObject)
Check if the RapidCodeObject has any errors.
static void StartTheNetwork(MotionController controller)
Start the controller communication/network.
Helper Functions for checking logged creation errors, starting the network, etc.
void UserUnitsSet(double countsPerUserUnit)
Sets the number of counts per User Unit.
void ErrorLimitTriggerValueSet(double triggerValue)
Set the Position Error Limit trigger value.
void MoveVelocity(double velocity)
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:5513
void DefaultAccelerationSet(double acceleration)
Set the default acceleration in UserUnits.
void DefaultDecelerationSet(double deceleration)
Set the default deceleration in UserUnits.
int32_t AnalogInGet(int32_t analogChannel)
Get the value of an analog input.
The IO object provides an interface to the inputs and outputs of a network node.
Definition rsi.h:11420
Axis * AxisGet(int32_t axisNumber)
AxisGet returns a pointer to an Axis object and initializes its internals.
static MotionController * CreateFromSoftware()
Initialize and start the RMP EtherCAT controller.
IO * IOGet(int32_t nodeNumber)
IOGet returns a pointer to an IO object using its node number and initializes its internals.
Represents the RMP soft motion controller. This class provides an interface to general controller con...
Definition rsi.h:794
void ClearFaults()
Clear all faults for an Axis or MultiAxis.
void AmpEnableSet(bool enable)
Enable all amplifiers.
void Abort()
Abort an axis.