Getting Robot Coordinates
This example provides information on getting the current position of the robot. The current position and rotation (in Euler XYZ) of the robot's tool is printed in the logger, as well as the current joint values of the robot. The angles are given in radians, and are here converted to degrees. Everything is done in the event handler, so this example shows you how to add an event handler to your Add-In. Run the Add-In and right click on the robot in the Layout browser and choose Mechanism Joint Jog. From here you can move the robot around, one joint at a time. Each time the robot moves, the logger will show the current tool coordinate, tool rotation and the joint values.
Use this procedure to get the current position:
Get the coordinates of the tool.
Get the rotation of the tool. Convert from radians to degrees.
Get the joint values of the robot.
Print the joint values in the logger.
Solution
Get the coordinates of the tool.
// Get the coordinates of the tool. RsToolData tool = station.ActiveTask.ActiveTool; Vector4 trans = tool.Frame.GlobalMatrix.t; trans.x *= 1000.0; trans.y *= 1000.0; trans.z *= 1000.0;
Get the rotation of the tool. Convert from radians to degrees.
// Get the rotation of the tool. Convert from radians to degrees. double rx = Globals.RadToDeg(tool.Frame.GlobalMatrix.EulerXYZ.x); double ry = Globals.RadToDeg(tool.Frame.GlobalMatrix.EulerXYZ.y); double rz = Globals.RadToDeg(tool.Frame.GlobalMatrix.EulerXYZ.z); Logger.AddMessage(new LogMessage("The tools rotation (EulerXYZ): Rx: " + rx.ToString() + "; Ry: " + ry.ToString() + "; Rz: " + rz.ToString()));
Get the joint values of the robot.
// Get the joint values of the robot. Mechanism mech = station.ActiveTask.Mechanism; double[] jointValues = mech.GetJointValues();
Print the joint values in the logger.
// Print the joint values in the logger. Logger.AddMessage(new LogMessage("The robots joint values:")); int i = 1; foreach (double d in jointValues) { Logger.AddMessage(new LogMessage("Joint " + i++ + ": " + Globals.RadToDeg(d).ToString())); }
Example
This example provides information on getting the robot coordinates.
public void GetRobotCoordinatesExample()
{
Project.UndoContext.BeginUndoStep("RsTestAddIn");
try
{
// This example needs an IRB140 to work!
Station station = Station.ActiveStation;
// Add an event handler.
station.ActiveTask.Mechanism.JointValuesChanged += new EventHandler(MyJointListener);
// Update joint values
double[] newJointValues = new double[] { 0, 0, 0, 0, 0, 0 };
station.ActiveTask.Mechanism.SetJointValues(newJointValues, true);
}
catch
{
Project.UndoContext.CancelUndoStep(CancelUndoStepType.Rollback);
throw;
}
finally
{
Project.UndoContext.EndUndoStep();
}
}
static void MyJointListener(object sender, EventArgs e)
{
Station station = Station.ActiveStation;
Logger.AddMessage(new LogMessage("--------------------------------------------------"));
// Get the coordinates of the tool.
RsToolData tool = station.ActiveTask.ActiveTool;
Vector4 trans = tool.Frame.GlobalMatrix.t;
trans.x *= 1000.0;
trans.y *= 1000.0;
trans.z *= 1000.0;
String xStr = trans.x.ToString();
String yStr = trans.y.ToString();
String zStr = trans.z.ToString();
Logger.AddMessage(new LogMessage("The tools position: X: " + xStr + "; Y: "
+ yStr + "; Z: " + zStr));
// Get the rotation of the tool. Convert from radians to degrees.
double rx = Globals.RadToDeg(tool.Frame.GlobalMatrix.EulerXYZ.x);
double ry = Globals.RadToDeg(tool.Frame.GlobalMatrix.EulerXYZ.y);
double rz = Globals.RadToDeg(tool.Frame.GlobalMatrix.EulerXYZ.z);
Logger.AddMessage(new LogMessage("The tools rotation (EulerXYZ): Rx: "
+ rx.ToString() + "; Ry: " + ry.ToString() + "; Rz: " + rz.ToString()));
// Get the joint values of the robot.
Mechanism mech = station.ActiveTask.Mechanism;
double[] jointValues = mech.GetJointValues();
// Print the joint values in the logger.
Logger.AddMessage(new LogMessage("The robots joint values:"));
int i = 1;
foreach (double d in jointValues)
{
Logger.AddMessage(new LogMessage("Joint " + i++ + ": " +
Globals.RadToDeg(d).ToString()));
}
}
Required Namespaces
ABB.
ABB.