Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to get & set the "Target linked" item of a move instruction?
#1
How do I get the 'Target linked' item of a program's move instruction? The target and move instruction are created in the GUI, and I would like to obtain the linked target item via the API.

I found I can get several parameters of the instruction using 'Instruction()':

Code:
p = RDK.Item('myProgram',ITEM_TYPE_PROGRAM)
ins_name, ins_type, move_type, isjointtarget, pose, joints = p.Instruction(2) # Selecting '2', the 3rd instruction

However, this does not provide the linked target item. Also, output 'pose' appears to be defined with respect to the linked target item's first parent reference frame, but if I can't determine to which target item the instruction is linked, then I can't determine the frame of reference for 'pose' either.

The second part of this question is: how do I change/set the 'Target Linked' item via the API to a different target item? (I.e. not just changing the 'pose' of the instruction, but changing the actual target item). Similar to what is achieved in the GUI's right click on instruction menu shown below:

   

Best regards,

Maarten
#2
Hi Mearten, 

You are on the right path!

Currently, it is not possible to edit the linked Target Item directly through the API.
You will have to replace the instruction with a new one.

Here's a sample code to give you the idea.

Code:
import robolink
RDK = robolink.Robolink()

prog = RDK.ItemUserPick("Select the program to edit", robolink.ITEM_TYPE_PROGRAM)
old_target = RDK.ItemUserPick("Select the target to replace", robolink.ITEM_TYPE_TARGET)
new_target = RDK.ItemUserPick("Select the new target", robolink.ITEM_TYPE_TARGET)

inst_count = prog.InstructionCount()
for i in range(inst_count):
   instruction_dict = prog.setParam(i)
   if instruction_dict['Type'] == robolink.INS_TYPE_MOVE:
       if old_target.Name() in instruction_dict['Name']:
           prog.InstructionSelect(i) # Select the instruction to replace
           prog.MoveJ(new_target) # Add the new instruction (properly use MoveL or MoveJ)
           prog.setParam(i, {'Type': -1}) # Delete the instruction
           i += 1 # Reset the instruction pointer

You can find more information in your documentation
Find useful information about RoboDK and its features by visiting our Online Documentation and by watching tutorials on our YouTube Channel.
#3
Hi Sam,

Thanks for helping out. Isn't your code example a bit risky, since you're assuming the name of the instruction contains the name of the target?

Code:
if old_target.Name() in instruction_dict['Name']:

By default, the name of a joint move instruction indeed includes the name of the target in its form e.g. "MoveJ (targetName)". But a user is free to rename the instruction. Also, a user could rename the target after creating the instruction. Without the name of the target as part of the instruction's name, your code example would miss the link.

(The code would also give a false positive for e.g. "MoveJ (target10)", which contains the name of both "target1" as well as "target10". But this could be solved by a more specific string compare.)

Is there no other indicator to the linked target item in an instruction than the name of the target item as part of the name of the instruction?

Best regards,

Maarten
#4
I just found out that the name of a move instruction in the GUI is automagically updated to match the default name and the (updated if changed) name of the linked target, upon running these next lines in a Python script:

Code:
from robolink import *    # RoboDK API
RDK = Robolink()

p = RDK.Item('myProgram',ITEM_TYPE_PROGRAM)
ins_dict = p.setParam(2) # Get 3rd instruction
print(ins_dict)
p.setParam(2,ins_dict) # Set 3rd instruction to what I just got
ins_dict = p.setParam(2) # Get 3rd instruction again
print(ins_dict)

So I have e.g. a (3rd) instruction and target:
    "MoveJ (target1)" and "target1"
I manually change in the GUI both the instruction name and target name to:
    "MoveJA (target1B)" and "target1C"
Then I run the code above, and the name of the instruction is automatically updated to:
    "MoveJ (target1C)" and "target1C"

Perhaps something underwater gets refreshed? If the naming of instructions is actually enforced by this, it could make retrieving the linked target item from the instruction's name a viable option.
#5
Indeed, the example code I provided is not robust.
It is provided as a workaround until a more convenient way is added to the API.
You will have to adapt it to your needs.

To make it more robust, you can retrieve the desired target pose and compare it to the instruction pose.
If the name and pose matches, most likely you are replacing the correct instruction.

Other ways to approach this:
  • Create two programs with different targets
  • Create your program from scratch through the API using the desired target Item
Find useful information about RoboDK and its features by visiting our Online Documentation and by watching tutorials on our YouTube Channel.
  




Users browsing this thread:
1 Guest(s)