Kuka CrossCom documentation - lionpeloux/KUKAVARPROXY GitHub Wiki
Credits goes to :
Ein lernender KUKA Roboter am Beispiel eines inversen Pendels
Other source of information : Paul Chavent
The KUKA server is used to read out and write the required data for the swinging up of the pendulum. The agent has been provided with commands that allow him to start the robot, stop it and turn axis 1 left or right. In addition, the current position, speed and acceleration can be read out of the KUKA class.
Snippet 3 starts the asynchronous reading of variable $OV_PRO
. As soon as the data is available, the KRCReadData
method is called and the value of the variable is transferred to KRCResult
.
private void btReadData_Click(object sender, EventArgs e) {
AsyncCallback callBackKRCReadData = new AsyncCallback(KRCReadData);
object obTest = new object();
KUKARoboter.KRCModel.CrossKrc.KrcVariableCommands.BeginShowVar("$OV_PRO" , callBackKRCReadData, obTest);
}
static void KRCReadData(IAsyncResult KRCResult) {
MessageBox.Show(KUKARoboter.KRCModel.CrossKrc.KrcVariableCommands.EndShowVar(KRCResult));
}
Code Snippet 3 : Reading of variable $OV_PRO
KUKA real-time data is stored in variables in the KUKA runtime environment. These variables can be read out via shared memory. The reading of the data follows via an asynchronous call.
The class CrossKRC
manages the interface to the KRC and initiates the reading and writing of variables in the KRC. In addition, it is possible to read out error numbers that are sent to the KUKA HMI
. There is also a method for transferring files to the basic system and back. The following references are required for the communication.
-
InterOp.KukaForm
(InterOp.KukaForm.dll) (v1.0.3705) -
PrimaryInterOp.Cross3Krc
(PrimaryInterOp.Cross3Krc.dll) (v1.0.3705)
The dynamic link library (dll) are in the KRC\HMI
directory of the tested KRC version.
In the constructor, the connection to the KRC is established via the KRCServiceFactoryClass
. The lVariable
list is used to save the variables that are to be updated cyclically. The list also includes the current value and a sequential number. The cycle time with which the variable list is sent to the superordinate class must be passed to the constructor.
public CrossKRC(fReadKUKA oParent, int CycleTime) {
if (oParent != null)
{
// provide service to KUKA Cross
objServiceFactory = new KrcServiceFactoryClass();
// List for variables for data management
lVariable = new List<Data>();
// Cycle time for updating the data
iCycleTime = CycleTime;
// timer for cyclic update
Timer tCycleTime = new Timer(); // create timer
tCycleTime.Interval = iCycleTime; // Iet the interval
// Generate event handler that is called during timer execution
tCycleTime.Tick += new EventHandler(tCycleTime_Tick);
tCycleTime.Start(); // Timer start
}
}
Code Snippet 4 : Constructor
public void ConnectToCross(string sConnectName)
{
int nMessage;
var sLocalConnectName = sConnectName;
var itfSyncvar = (ICKSyncVar)objServiceFactory.GetService("WBC_KrcLib.SyncVar", sLocalConnectName);
var itfSyncFile = (ICKSyncFile)objServiceFactory.GetService("WBC_KrcLib.SyncFile", sLocalConnectName);
var itfSyncEdit = (ICKSyncEdit)objServiceFactory.GetService("WBC_KrcLib.SyncEdit", sLocalConnectName);
var itfSyncSelect = (ICKSyncSelect)objServiceFactory.GetService("WBC_KrcLib.SyncSelect", sLocalConnectName);
var itfSyncIo = (ICKSyncIo)objServiceFactory.GetService("WBC_KrcLib.SyncIo", sLocalConnectName);
var itfAsyncVar = (ICKAsyncVar)objServiceFactory.GetService("WBC_KrcLib.AsyncVar", sLocalConnectName);
var itfAdviseMessage = (ICKAdviseMessage)objServiceFactory.GetService("WBC_KrcLib.AdviseMessage", sLocalConnectName);
nMessage = 0;
nMessage = nMessage | (int)EMessageType.eMessageTypeInfo | (int)EMessageType.eMessageTypeState | (int)(EMessageType.eMessageTypeQuitt);
nMessage = nMessage | (int)EKMessageType.eMessageTypeEvent | (int)EKMessageType.eMessageTypeState;
nMessage = nMessage | (int)EDialogType.eMessageTypeWait | (int)EDialogType.eMessageTypeDialog;
itfAdviseMessage.Advise(this, nMessage); Debug.Print(" --- Connected to Cross");
}
Code Snippet 5 : Log on services for communication
With the method addVariable
variable names can be passed to the class. These are then stored in a list and logged on for cyclic reading from the KRC. As soon as a value changes for a variable, the method OnSetInfo
(see snippet 6) is called and the current value is noted in the list. A maximum of 55
variables are possible for simultaneous monitoring. If too many variables are logged in, the method OnError
is called and the corresponding error number is returned.
void ICKCallbackVar.OnSetInfo(int Nr, string Value) {
Debug.Print(" --- OnSetInfo");
lVariable[Nr - 1].Value = Value;
}
Code Snippet 6 : Callback for Event SetInfo
// *****************************************************************
// Add variable for cyclic update
// Danger!!! A maximum of 55 variables can be observed
// *****************************************************************
public void addVariable(string sVariable)
{
Debug.Print("--- addVariable " + (lVariable.Count + 1).ToString());
if (lVariable.Count < 55)
{
// enter variable in list
lVariable.Add(new Data(sVariable, "0", "INT", lVariable.Count + 1));
// Feedback on variable change
ICKCallbackVar CallBackVar = this;
// Log in to KRC
itfAsyncVar.SetInfo((ICKCallbackVar)this,lVariable.Count, sVariable, iCycleTime);
}
else
{
Debug.Print("Max. Number of variables (55) reached");
}
}
Code Snippet 7 : Public method for registering variables
The method setVariable
(see snippet 8) can be used to describe a variable from the list with a value. If a variable has been set, successful writing is confirmed with the callback method OnSetVar
.
If the variable is write-protected or the value to be written is invalid, this is reported via the method OnError
.
// *****************************************************************
// Set variable value
// *****************************************************************
public void setVariable(string sVariable, string sValue)
{
// Feedback on variable change
ICKCallbackVar CallBackVar = this;
Data myData = lVariable.Find(delegate(Data d) {return d.Variable == sVariable;});
itfAsyncVar.SetVar(CallBackVar, myData.Number, myData.Variable, sValue);
}
Code Snippet 8 : Method for writing variables