Click or drag to resize

Working with RAPID data

Overview

The RapidDomain namespace enables access to RAPID data in the robot system. There are numerous FlexPendant SDK classes representing the different RAPID data types. There is also a UserDefined class used to reference RECORD structures in RAPID.

The ValueChanged event enables notification from the controller when persistent RAPID data has changed.

Note Note
A convenient and user-friendly way to enable the end-user of your application to read and write to specific RAPID data is using the standard FlexPendant Program Data view. For more information, see Using standard dialog box to modify data .
Note Note
Using databinding for RAPID data is a quick way of implementing access to RAPID data. For more information, see Data of RAPID data and I/O signals.
Providing the path to the RAPID data

To read or write to RAPID data you must first create a RapidData object. The path to the declaration of the data in the controller is passed as argument. If you do not know the path you need to search for the RAPID data by using the SearchRapidSymbol functionality. For more information, see SearchRapidSymbol method.

Direct access

Direct access requires less memory and is faster, and is therefore recommended if you do not need to use the task and module objects afterwards.

The following example shows how to create a RapidData object that refers to the RAPID data instance “reg1” in the USER module.

C#
RapidData rd = aController.Rapid.GetRapidData("T_ROB1", "USER", "reg1");
VB
Dim Rd As RapidData = Me.AController.Rapid.GetRapidData( "T_ROB1", "USER", "reg1")
Hierarchical access

If you need the task and module objects hierarchical access can be more efficient. GetRapidData exists in the Rapid, Task and Module class.

C#
rd = aController.Rapid.GetTask("T_ROB1").GetModule("USER"). GetRapidData("reg1");
VB
Rd = AController.Rapid.GetTask("T_ROB1").GetModule("USER"). GetRapidData("reg1")
Accessing data declared in a shared module

If your application is to be used with a multi-move system (a system with one controller and several motion tasks/robots), it may happen that the RAPID instance you need to access is declared in a -Shared RAPID module. Such a module can be used by all tasks, T_ROB1, T_ROB2 and so on.

The following example shows how to create aRapidData object that refers to the instance “reg100”, which is declared in a shared module.

C#
RapidData rd = aController.Rapid.GetRapidData("reg100");
VB
Dim Rd As RapidData = Me.AController.Rapid.GetRapidData("reg100")

Another possibility is using the Task object to access the RAPID instance, like this:

Note Note

If GetRapidData is called from Rapid the RAPID data will be found even if the -Shared module is configured to be hidden.

Note Note

If the RAPID data does not exist, the return value is Nothing/null and an ArgumentNullException is thrown. A null check should be performed before trying to use the object.

Creating an object representing the RAPID data value

The RapidData object stores the path to the RAPID data. But this is not enough if you want to access its value (at least not if you want to modify it). To do that you need to create another object, which represents the value of the RAPID data.

In the RapidDomain namespace there are types representing the different RAPID data types. To create the object needed to represent the RAPID data value you use the RapidData property Value and cast it to the corresponding type, for example Num, Bool or Tooldata .

The following code explains how this is done, if you want to access the value of a RAPID data of the RAPID data type Bool:

C#
//declare a variable of data type RapidDomain.Bool
RapidDomain.Bool rapidBool;
RapidDomain.RapidData rd = aController.Rapid.GetRapidData("T_ROB1", "MainModule", "flag");
//test that data type is correct before cast
if (rd.Value is ABB.Robotics.Controllers.RapidDomain.Bool)
{
rapidBool = (ABB.Robotics.Controllers.RapidDomain.Bool)rd.Value;
//assign the value of the RAPID data to a local variable
bool boolValue = rapidBool.Value;
}
VB
declare a variable of data type RapidDomain.Bool
Dim rapidBool As RapidDomain.Bool
Dim rd As RapidData = Me.AController.Rapid.GetRapidData( "T_ROB1", "MainModule", "flag" )
’test that data type is correct before cast
If TypeOf rd.Value Is RapidDomain.Bool Then
rapidBool = DirectCast(rd.Value, RapidDomain.Bool)
’check if the value of the RAPID data is true
If (rapidBool.Value) ThenDo something...
End If
End If

If you only want to read this variable you can use this technique instead of creating a RapidDomain.Bool object:

C#
bool b = Convert.ToBoolean(rd.Value.ToString());
VB
Dim b As Boolean = Convert.ToBoolean(rd.Value.ToString)

The ToolDatatype (representing the RAPID data type toolData) can be created like this:

C#
ToolData aTool;
if (rd.Value is ToolData)
{
aTool = (ToolData) rd.Value;
}
VB
Dim ATool As ToolData
If Rd.Value Is ToolData Then
ATool = DirectCast(Rd.Value, ToolData)
End If
IRapidData.ToString method

All RapidDomain structures representing RAPID data types implement the IRapidData interface. It has a ToString method, which returns the value of the RAPID data in the form of a string. This is a simple example: string boolValue = rapidBool.ToString();

The string is formatted according to the same principle as described in the following section IRapidData.FillFromString method

Here is an example of a more complex data type. The ToolDataTframe property is of the Pose type. Its Trans value is displayed in a label in the format [x, y, z].

C#
this.label1.Text = aTool.Tframe.Trans.ToString();
VB
Me.Label1.Text = ATool.Tframe.Trans.ToString()
IRapidData.FillFromString method

The IRapidData interface also has a FillFromString method, which fills the object with a valid RAPID string representation. The method can always be used when you need to modify RAPID data. Using the method with the RapidDomain.Bool variable used earlier in the chapter will look like this:

rapidBool.FillFromString("True")

Using it for a RapidDomain.Num variable is similar:

rapidNum.FillFromString("10")

String format

The format is constructed recursively. An example is the easiest way of illustrating this.

Example:

The RapidDomain.Pose structure corresponds to the RAPID data type pose, which describes how a coordinate system is displaced and rotated around another coordinate system.

public struct Pose : IRapidData
              {  public Pos trans;  public Orient rot;}

This is an example in RAPID:

VAR pose frame1;
              ...
              frame1.trans := [50, 0, 40];
              frame1.rot := [1, 0, 0, 0];

The frame1 coordinate transformation is assigned a value that corresponds to a displacement in position where X=50 mm, Y=0 mm and Z=40 mm. There is no rotation.

As you see, the RapidDomain.Pose structure consists of two other structures, trans and rot . The trans structure consists of three floats and the and rot structure consists of four doubles. The FillFromString format for a pose object is “[[1.0, 0.0, 0.0, 0.0][10.0, 20.0, 30.0]]”. This piece of code shows how to write a new value to a RAPID pose variable:

C#
if (rd.Value is Pose)
{
Pose rapidPose = (Pose) rd.Value;
rapidPose.FillFromString("[[1.0, 0.5, 0.0, 0.0][10, 15, 10]]");
rd.Value = rapidPose;
}
VB
If TypeOf rd.Value Is Pose Then
Dim rapidPose As Pose = DirectCast(rd.Value, Pose)
rapidPose.FillFromString("[[1.0, 0.0, 0.0, 0.0][10, 20, 30]]")
rd.Value = rapidPose
End If
Note Note

Using the same principle arbitrarily long RAPID data types can be represented.

Note Note

The string format must be carefully observed. If the string argument has the wrong format, a RapidDataFormatException is thrown.

Writing to RAPID data

Writing to RAPID data is only possible using the type cast RapidData value, to which the new value is assigned. To transfer the new value to the RAPID data in the controller you must finally assign the .NET object to the Value property of the RapidData object. The following example uses the rapidBool object created in Creating an object representing the RAPID data value. .

C#
 //Assign new value to .Net variable
rapidBool.Value = false;
// Write to new value to the data in the controller
 rd.Value = rapidBool;
VB
 'Assign new value to .Net variable
 rapidBool.Value = False
'Write the new value to the data in the controller
rd.Value = rapidBool

This was an easy example, as the value to change was a simple bool . Often, however, RAPID uses complex structures. By using the FillFromString method you can assign a new value to any RapidData and write it to the controller.

The string must be formatted according to the principle described in the previous section. The following example shows how to write a new value to the pos structure (x, y, z) of a RAPID tooldata:

C#
Pos aPos = new Pos();
aPos.FillFromString("[2,3,3]");
this.aTool.Tframe.Trans = aPos;
this.rd.Value = this.aTool;
VB
Dim APos As Pos = New Pos
APos.FillFromString("[2,3,3]")
Me.ATool.Tframe.Trans = APos
Me.Rd.Value = Me.ATool
Note Note

The new value is not written to the controller until the last statement is executed.

Letting the user know that RAPID data has changed

In order to be notified that RAPID data has changed you need to add a subscription to the ValueChanged event of the RapidData instance. Note, however, that this only works for persistent RAPID data.

Add subscription

The following code explains how you add a subscription to the ValueChanged event:

C#
this.rd.ValueChanged += rd_ValueChanged;
VB
Addhandler Rd.ValueChanged, AddressOf Rd_ValueChanged
Handle event

Implement the event handler. Remember that controller events use their own threads, and avoid Winforms threading problems by the use of Control.Invoke , which forces the execution from the background thread to the GUI thread.

C#
private void rd_ValueChanged(object sender, DataValueChangedEventArgs e)
{
this.Invoke(new EventHandler (UpdateGUI), sender, e);
}
VB
Private Sub Rd_ValueChanged(ByVal sender As Object, ByVal e As DataValueChangedEventArgs)
Me.Invoke(New EventHandler (AddressOf UpdateGUI), sender, e)
End Sub

For more information to learn more about potential threading conflicts in FlexPendant SDK applications, see Controller events and threads.

Read new value from controlller

Update the user interface with the new value. As the value is not part of the event argument, you must use the RapidDataValue property to retrieve the new value:

C#
private void UpdateGUI(object sender, System.EventArgs e)

{

ToolData tool1= (ToolData)this.rd.Value;

this.label1.Text = tool1.Tframe.Trans.ToString();

}
VB
Private Sub UpdateGUI(ByVal sender As Object, ByVal e As System.EventArgs)

Dim Tool1 As ToolData = DirectCast(Me.Rd.Value, ToolData)

Me.Label1.Text = Tool1.Tframe.Trans.ToString()

End Sub
Note Note

Subscriptions work only for RAPID data declared as PERS.

RapidData disposal

Always dispose of RapidData objects when they are no longer needed. If you want to reuse a RapidData object, you should make sure that you dispose of the current object first.

C#
if (rd != null)

{

rd.Dispose();

rd = null;

}
VB
If Not Rd Is Nothing Then

Rd.Dispose()

Rd = Nothing

End If