Homing.cs
using RSI.RapidCode.dotNET; // Import our RapidCode Library.
using NUnit.Framework;
using System;
namespace SampleAppsCS
{
class Homing : SampleAppTestBase
{
public void MasterBasedHoming()
{
//@[MasterBasedHoming]
axis.HardwareNegLimitActionSet(RSIAction.RSIActionSTOP); // Neg Limit action set to STOP.
axis.HomeMethodSet(RSIHomeMethod.RSIHomeMethodImprovedFALLING_HOME_NEGATIVE_START_POSITIVE_MOMENTUM); // Set the method to be used for homing.
axis.HomeVelocitySet(Constants.VELOCITY); // Set the home velocity.
axis.HomeSlowVelocitySet(Constants.VELOCITY / 10); // Set the slow home velocity. (used for final move, if necessary)
axis.HomeAccelerationSet(Constants.ACCELERATION); // Set the acceleration used for homing.
axis.HomeDecelerationSet(Constants.DECELERATION); // Set the deceleration used for homing.
axis.HomeOffsetSet(0.5); // HomeOffsetSet sets the position offset from the home (zero) position.
axis.Home(); // Execute the homing routine.
if (axis.HomeStateGet() == true) // HomeStateGet returns true if the Axis is homed.
{
Console.WriteLine("Homing successful\n");
}
axis.ClearFaults(); // Clear faults created by homing.
axis.AmpEnableSet(false); // Disable the motor.
//@[MasterBasedHoming]
}
public void HomingWithAKDdrive()
{
//@[HomingWithAKDdrive]
// 1. READY DRIVE
axis.OperationModeSet(RSIOperationMode.RSIOperationModeHOMING_MODE); // Mode of Operation set to Homing Mode.
axis.NetworkNode.AKDASCIICommand("DRV.OPMODE 2"); // Sets the drive operation mode (0 - current | 1 = velocity | 2 = position).
axis.NetworkNode.AKDASCIICommand("HOME.AUTOMOVE 0"); // 0 = Disabled | 1 = Homing starts when drive is enabled.
// Make sure you know your motor's position, velocity, and acceleration units before you send any values.
// 2. SET YOUR LIMIT SWITCHES
axis.NetworkNode.AKDASCIICommand("DIN5.MODE 18"); // Sets the digital input modes. - DI5 is now our Positive Limit Switch.
axis.NetworkNode.AKDASCIICommand("DIN5.INV 1"); // Sets the indicated polarity of a digital input mode. - DI5 is now active when Low.
axis.NetworkNode.AKDASCIICommand("DIN6.MODE 19"); // Sets the digital input modes. - DI6 is now our Negative Limit Switch.
axis.NetworkNode.AKDASCIICommand("DIN6.INV 1"); // Sets the indicated polarity of a digital input mode. - DI6 is now active when Low.
// 3. CONFIGURE DRIVE HOMING PARAMETERS
axis.NetworkNode.AKDASCIICommand("HOME.MODE 1"); // Selects the homing method; active in opmode 2 (position) only. MODE1 = Find limit input
axis.NetworkNode.AKDASCIICommand("HOME.V 20"); // Sets homing velocity; active in opmode 2 (position) only.
axis.NetworkNode.AKDASCIICommand("HOME.ACC 200"); // Sets homing acceleration; active in opmode 2 (position) only.
axis.NetworkNode.AKDASCIICommand("HOME.DEC 200"); // Sets homing deceleration; active in opmode 2 (position) only.
axis.NetworkNode.AKDASCIICommand("HOME.DIR 0"); // Sets homing direction; active in opmode 2 (position) only. (0 = negative | 1 = positive)
axis.NetworkNode.AKDASCIICommand("HOME.P 0"); // Sets home position; active in opmode 2 (position) only.
axis.NetworkNode.AKDASCIICommand("HOME.DIST 0"); // Sets homing distance; active in opmode 2 (position) only.
axis.NetworkNode.AKDASCIICommand("HOME.MAXDIST 0"); // Sets the maximum distance the motor is allowed to move during the homing routine. (Disabled when value = 0)
axis.NetworkNode.AKDASCIICommand("HOME.IPEAK"); // Sets the current limit during homing procedure to a mechanical stop; active in opmode 2 (position) only.
// 4. READY AXIS
axis.Abort(); // Disable axis.
axis.ClearFaults(); // Clear any faults.
axis.AmpEnableSet(true); // Enable the axis.
System.Threading.Thread.Sleep(1000); // Allow time for amp enable
// 5. START HOMING
axis.NetworkNode.AKDASCIICommand("HOME.MOVE"); // Starts a homing procedure; active in opmode 2 (position) only.
Console.WriteLine("HOME.MOVE");
// 6. CHECK IF HOMING IS DONE
UInt16 statusWordValue;
int isHomedvalue = 0;
int axisIndex = axis.NumberGet();
while (isHomedvalue != 1) // When isHomedValue = 1, homing has finished.
{
statusWordValue = axis.NetworkNode.StatusWordGet(axisIndex);
isHomedvalue = statusWordValue >> 12; // Get the 12th bit only. This bit tells us homing is done when it goes HIGH.
}
Console.WriteLine("Axis homed.");
// 7. CLEAN UP
axis.Abort(); // Disable the axis.
axis.OperationModeSet(RSIOperationMode.RSIOperationModeINTERPOLATED_POSITION_MODE); // Mode of Operation Restore
//@[HomingWithAKDdrive]
}
public void HomingWithDS402drive()
{
//@[HomingWithDS402drive]
// Constants
const int AXIS_NUMBER = 0; // Specify which axis/motor to control.
// Homing Offset.
const int offsetIndex = 0x607C;
const int offsetSubindex = 0x0;
const int offsetByteSize = 4;
const int offsetValue = 0;
// Home Method
const int methodIndex = 0x6098;
const int methodSubindex = 0x0;
const int methodByteSize = 1;
const int methodValue = 24;
// Speed To Switch
const int targetSpeedIndex = 0x6099;
const int targetSpeedSubindex = 0x1;
const int targetSpeedByteSize = 4;
const int targetSpeedValue = 2;
// Speed to Zero
const int originSpeedIndex = 0x6099;
const int originSpeedSubindex = 0x2;
const int orignSpeedByteSize = 4;
const int originSpeedValue = 10;
// Homing Acceleration
const int accelerationIndex = 0x609A;
const int accelerationSubindex = 0x0;
const int accelerationByteSize = 4;
const int accelerationValue = 100;
//Desired DS402 Enabled Output.
const int CONTROL_WORD_TO_PREP_HOMING = 15;
const int CONTROL_WORD_TO_START_HOMING = 31;
const int ACCEPTABLE_DELAY_IN_MS = 20;
const int STATUS_WORD_TARGET_REACHED_BIT = 0x400; // Status Bit 10 (0x400) indicates Target Reached (Homing is Complete).
const int STATUS_WORD_HOMING_ATTAINED_BIT = 0x1000; // Status Bit 12 (0x1000) indicates Homing Attained (Homing is Successful).
const int STATUS_WORD_HOMING_ERROR_BIT = 0x2000; // Status Bit 13 (0x2000) indicates Homing Error (Homing ran into a problem).
// Initialize RapidCode Objects
MotionController controller = MotionController.CreateFromSoftware(/*@"C:\RSI\X.X.X\"*/); // Insert the path location of the RMP.rta (usually the RapidSetup folder)
SampleAppsCS.HelperFunctions.CheckErrors(controller); // [Helper Function] Check that the controller has been initialize correctly.
SampleAppsCS.HelperFunctions.StartTheNetwork(controller); // [Helper Function] Initialize the network.
Axis axis = controller.AxisGet(AXIS_NUMBER); // Initialize Axis Class. (Use RapidSetup Tool to see what is your axis number)
SampleAppsCS.HelperFunctions.CheckErrors(axis); // [Helper Function] Check that the axis has been initialize correctly.
int axisControlWordIndex = (int) axis.NetworkIndexGet(RSINetworkIndexType.NetworkIndexTypeCONTROL_WORD_INDEX);
// CONFIGURE (Writing to SDOs)
axis.NetworkNode.ServiceChannelWrite(offsetIndex, offsetSubindex, offsetByteSize, offsetValue); // Home Offset
axis.NetworkNode.ServiceChannelWrite(methodIndex, methodSubindex, methodByteSize, methodValue); // Home Method (Home type)
axis.NetworkNode.ServiceChannelWrite(targetSpeedIndex, targetSpeedSubindex, targetSpeedByteSize, targetSpeedValue); // Home Speed during search for switch
axis.NetworkNode.ServiceChannelWrite(originSpeedIndex, originSpeedSubindex, orignSpeedByteSize, originSpeedValue); // Home Speed during search for zero
axis.NetworkNode.ServiceChannelWrite(accelerationIndex, accelerationSubindex, accelerationByteSize, accelerationValue); // Home acceleration
axis.rsiControl.NetworkOutputOverrideValueSet(axisControlWordIndex, CONTROL_WORD_TO_PREP_HOMING); // Control Word should be 15 before Switching to Homing Mode.
axis.rsiControl.NetworkOutputOverrideSet(axisControlWordIndex, true); // Override Control Word.
controller.SampleWait(ACCEPTABLE_DELAY_IN_MS);
// Delay to give transitions time -or- setup something more complex to ensure drive is in the appropriate state.
axis.OperationModeSet(RSIOperationMode.RSIOperationModeHOMING_MODE);
controller.SampleWait(ACCEPTABLE_DELAY_IN_MS);
// HOME
axis.rsiControl.NetworkOutputOverrideValueSet(axisControlWordIndex, CONTROL_WORD_TO_START_HOMING); // Start Homing.
controller.SampleWait(ACCEPTABLE_DELAY_IN_MS); // Delay to give transitions time -or- setup something more complex to ensure drive is in the appropriate state.
UInt16 statusWordValue; // Takes the axis index. This will return the status word value.
bool cancelHome = false;
statusWordValue = axis.NetworkNode.StatusWordGet(AXIS_NUMBER);
while ((!cancelHome) && ((statusWordValue & STATUS_WORD_TARGET_REACHED_BIT) == 0)) // When Status Word Indicates Target Reached (Success or Fail) or external evaluator (cancelHome)
{
//A timeout that sets cancelHome would be a good idea for this while loop.
statusWordValue = axis.NetworkNode.StatusWordGet(AXIS_NUMBER); //Update.
//A short sleep or wait is appropriate so you can't spamming this call.
}
// 4. EVALUATE HOMING SUCCESS
if ((statusWordValue & STATUS_WORD_HOMING_ATTAINED_BIT) == 1)
{
Console.WriteLine("Axis homed.");
}
else if ((statusWordValue & STATUS_WORD_HOMING_ERROR_BIT) == 1)
{
Console.WriteLine("Error Occured during homing.");
}
//5. CLEAN UP
axis.AmpEnableSet(false); // Disable the axis.
axis.ClearFaults();
axis.rsiControl.NetworkOutputOverrideSet(axisControlWordIndex, false);
// Restore the mode of operation to the control mode you want to run in. For most, this will be RSIOperationModeCYCLIC_SYNCHRONOUS_POSITION_MODE.
axis.OperationModeSet(RSIOperationMode.RSIOperationModeCYCLIC_SYNCHRONOUS_POSITION_MODE);
//@[HomingWithDS402drive]
}
public void CustomHome()
{
//@[CustomHome]
// user defined options
//const RSIMotorDedicatedIn CAPTURE_SOURCE = RSIMotorDedicatedIn.RSIMotorDedicatedInHOME;
const int HOME_LIMIT_NETWORK_INDEX = 99; //You may want to discover this rather than hard code it.
const int HOME_LIMIT_SIG_BIT = 0;
const int LongTime = 15000;
var homeLimitAddress = controller.NetworkInputAddressGet(HOME_LIMIT_NETWORK_INDEX);
axis.HomeLimitCustomConfigSet(homeLimitAddress, HOME_LIMIT_SIG_BIT);
axis.HomeActionSet(RSIAction.RSIActionSTOP);
// Ready Axis
axis.Abort();
axis.ClearFaults();
axis.AmpEnableSet(true);
// commanding a velocity move. This program assumes that the Custom Home will trigger at some point.
axis.MoveVelocity(Constants.VELOCITY, Constants.ACCELERATION);
//wait (sleep) until motion done interrupt occurs
Boolean done = false;
while (!done)
{
RSIEventType eventType = controller.InterruptWait(LongTime);
switch (eventType)
{
case RSIEventType.RSIEventTypeMOTION_DONE:
done = true;
break;
case RSIEventType.RSIEventTypeTIMEOUT:
done = true;
break;
default:
break;
}
}
if ((controller.NetworkInputValueGet(HOME_LIMIT_NETWORK_INDEX) & HOME_LIMIT_SIG_BIT) > 0) //On Home Limit
{
axis.HomeStateSet(true);
}
else
{
//Evaluate why we aren't on custom home.
}
// setup Home Action (the home action will not trigger)
axis.HomeActionSet(RSIAction.RSIActionNONE);
axis.ClearFaults();
axis.AmpEnableSet(false);
//@[CustomHome]
}
}
}