RsTask Example
This is an example of RsTask.
Compiling the Code
Note
Change the path to your path to DefaultProcessTemplate.xml. It should be somewhere in your RobotStudio installation directory.
Note
You need to add System.Collections as a reference for this example to work.
Note
You need a running VC for this example to work.
Example
Project.UndoContext.BeginUndoStep("RsTask");
try
{
//NOTE: This example requires a stataion with and IRB_140, a positioner and a running VC.
Station station = Station.ActiveStation;
// Get the active task
RsTask myTask = station.ActiveTask;
// Create a PathProcedure, add it to the task and set it as active.
RsPathProcedure myPath = new RsPathProcedure(myTask.GetValidRapidName("myPath", "_", 1));
myTask.PathProcedures.Add(myPath);
myTask.ActivePathProcedure = myPath;
// Create a workobject, add it to the task and set it to active.
RsWorkObject myWobj = new RsWorkObject();
myWobj.Name = myTask.GetValidRapidName("myWobj", "_", 1);
myTask.DataDeclarations.Add(myWobj);
myTask.ActiveWorkObject = myWobj;
// Create RsRobTarget.
RsRobTarget myTarget = new RsRobTarget();
myTarget.Name = myTask.GetValidRapidName("myTarget", "_", 10);
// Create a corresponding RsTarget.
RsTarget myRsTarget = new RsTarget(myWobj, myTarget);
myRsTarget.Name = myTarget.Name;
// Add it to the task.
myTask.DataDeclarations.Add(myTarget);
myTask.Targets.Add(myRsTarget);
// Create a move instruction and add it to the PathProcedure.
RsMoveInstruction myMove = new RsMoveInstruction(myTask, "Move", "Default", MotionType.Linear, myWobj.Name, myTarget.Name, myTask.ActiveTool.Name);
myPath.Instructions.Add(myMove);
// Set myPath as entrypoint.
myTask.EntryPoint = myPath.Name;
// Make sure that all the objects in the task are included in simulation.
myTask.Simulate = true;
// Make sure that the objects in the task are visible in the graphics.
myTask.Visible = true;
// Sync the RobTaget to the VC.
await myTask.SyncDataAsync("Module1/" + myTarget.Name, SyncDirection.ToController, new List<SyncLogMessage>());
// Sync the path to the VC.
if (await myTask.SyncPathProcedureAsync("Module1/" + myPath.Name, SyncDirection.ToController, new List<SyncLogMessage>()))
{
Logger.AddMessage(new LogMessage("SyncPathProcedure of path " + myPath.Name + " succeded!"));
}
else
{
Logger.AddMessage(new LogMessage("SyncPathProcedure of path " + myPath.Name + " falied!"));
}
// Sync the complete task to the VC.
if (await myTask.SyncToControllerAsync(new List<SyncLogMessage>(), null))
{
Logger.AddMessage(new LogMessage("SyncToController succeded!"));
}
else
{
Logger.AddMessage(new LogMessage("SyncToController falied!"));
}
// Move along the path.
if (await myPath.MoveAlongAsync(null))
{
Logger.AddMessage(new LogMessage("The MoveAlong path command failed!"));
}
// Jump back to the home position.
if (!myTask.JumpHome())
{
Logger.AddMessage(new LogMessage("The JumpHome command failed!"));
}
// Find all RobTargets in the task.
RsDataDeclaration[] datadecls = myTask.FindDataDeclarationsByType(typeof(RsRobTarget));
Logger.AddMessage(new LogMessage("There are '" + datadecls.Length + "' in the task"));
// Find the first RobTarget in the task.
RsRobTarget robTrg = (RsRobTarget)myTask.FindFirstDataDeclarationByType(typeof(RsRobTarget));
Logger.AddMessage(new LogMessage("The first robtarget in the task is '" + robTrg.Name + "'"));
// Find the first RsTarget in the workobject myWobj.
RsTarget rsTrg = myTask.FindFirstTargetByWorkObject(myWobj);
Logger.AddMessage(new LogMessage("The first RsTarget in WorkObject '" + myWobj.Name + "' is '" + rsTrg.Name + "'"));
// Find RsTargets matching myWobj and myTarget.
RsTarget[] rsTargets = myTask.FindTargets(myWobj, myTarget);
Logger.AddMessage(new LogMessage("There are '" + rsTargets.Length + "' matching wobj: " + myWobj.Name + " and robTarget: " + myTarget.Name));
// Find RsTargets matching the RobTarget myTarget.
rsTargets = myTask.FindTargetsByRobTarget(myTarget);
Logger.AddMessage(new LogMessage("There are '" + rsTargets.Length + "' matching robTarget: " + myTarget.Name));
// Find RsTargets matching the workobject MyWobj.
rsTargets = myTask.FindTargetsByWorkObject(myWobj);
Logger.AddMessage(new LogMessage("There are '" + rsTargets.Length + "' matching wobj: " + myWobj.Name));
// Activate the first mechanical unit.
station.Irc5Controllers[0].MechanicalUnits[0].ActivationMode = MechanicalUnitActivationMode.Active;
// Get the JointTypes of the external axes in the task.
JointType[] myJT = myTask.GetExternalAxisJointTypes();
// Get the joint values of the external axes in the task.
double[] eaValues = myTask.GetExternalAxisJointValues();
// Set joint number 4 of the external axis values to 1
eaValues[3] = 1;
if (!myTask.SetExternalAxisJointValues(eaValues))
{
Logger.AddMessage(new LogMessage("Failed to set ExtAxis Values!"));
}
// Import process definitions from an xml file.
// NOTE: Change this to a valid path! DefaultProcessTemplate.xml should be in your RobotStudio installation directory.
myTask.ImportProcessDefinitions(@"C:\Program Files\ABB Industrial IT\Robotics IT\RobotStudio 5.12\ProcessTemplates\DefaultProcessTemplate.xml");
// Output active path.
Logger.AddMessage(new LogMessage("The ActivePathProcedure is:" + myTask.ActivePathProcedure.Name));
// Output active process definition.
Logger.AddMessage(new LogMessage("The ActiveProcessDefinition is:" + myTask.ActiveProcessDefinition.Name));
// Output active tool.
Logger.AddMessage(new LogMessage("The ActiveTool is:" + myTask.ActiveTool.Name));
// Output active workobject.
Logger.AddMessage(new LogMessage("The ActiveWorkObject is:" + myTask.ActiveWorkObject.Name));
// Output the instruction descriptions in the task.
Logger.AddMessage(new LogMessage("The Task: '" + myTask.Name + "' contains the following InstructionDescriptions:"));
foreach (RsInstructionDescription descr in myTask.InstructionDescriptions)
{
Logger.AddMessage(new LogMessage(descr.Name));
}
// Output the process definitions in the task.
Logger.AddMessage(new LogMessage("The Task: '" + myTask.Name + "' contains the following ProcessDefinitions:"));
foreach (RsProcessDefinition def in myTask.ProcessDefinitions)
{
Logger.AddMessage(new LogMessage(def.Name));
}
// Output the frame of the task.
Logger.AddMessage(new LogMessage("The Frame of the Task is: "
+ "X: " + myTask.Frame.x
+ "; Y: " + myTask.Frame.y
+ "; Z: " + myTask.Frame.z
));
// Output the mechanism of the task.
Logger.AddMessage(new LogMessage("The mechanism of the task id '" + myTask.Mechanism.Name + "'"));
}
catch
{
Project.UndoContext.CancelUndoStep(CancelUndoStepType.Rollback);
throw;
}
finally
{
Project.UndoContext.EndUndoStep();
}