Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5

Calling a Python Script from Another Python ramiptiwhin RoboDK Station

#1
Hi Everyone ,


I have a question regarding the use of Python programs within a RoboDK station. Specifically, I would like to know if it is possible for one Python program to call another within the same station.

In my current setup, I have created two separate Python programs—Program A and Program B—within the station. My intention is to call Program B from within Program A using the RunProgram() function. However, despite implementing this approach, Program B does not seem to execute as expected.

Could you please advise whether this method is supported, or if there is an alternative recommended approach to achieve this functionality?

Thank you very much for your assistance.

Code:
    my_prog = RDK.Item("ProgramB",ITEM_TYPE_PROGRAM_PYTHON)
    print(f"{my_prog}")

    if my_prog.Valid():
        my_prog.RunProgram()
        print("Run Program")
    else:
        print("Not Found")


Best regards
Tyler
#2
The sample code you shared should work.

Do you have more than one program called ProgramB in your station? It may be running the wrong script. If you can share your project file we can better help you.
#3
Dear Albert,

Thank you for your reply.
I am sharing the project file with you for your review.
Please let me know if you have any questions or need further information.


Attached Files
.rdk   robot_call_python.rdk (Size: 2.65 MB / Downloads: 41)
#4
OK I understand better now. This issue happens because you are using the driver and you want to trigger a program call in RoboDK simulation environment.

If you want to run your second program when you are connected to the robot I recommend you to change the "Run Mode" to simulation just to trigger the program (call RunProgram).

Also, I recommend you to check the connection state using ConnectedState instead of always connecting to the robot. You can then force the RunMode to "run on robot" if you have the real robot already connected to the driver. This will help you run this program in simulation mode when you are not connected as well. If you want to run your program on the real robot you can simply manually connect to the robot by pressing "Connect" and the program will then execute the movements to the real robot.

Important: using Connect attempts to connect to the robot and returns immediately. I recommend you to use ConnectSafe instead.
Code:
# Check if we are connected to the real robot to force sending the commands to the real robot:
    status, msg = robot.ConnectedState()
    if status == ROBOTCOM_READY:
        run_mode = RUNMODE_RUN_ROBOT
    else:
        run_mode = RUNMODE_SIMULATE
    RDK.setRunMode(run_mode)
    my_prog = RDK.Item("ProgramB",ITEM_TYPE_PROGRAM_PYTHON)
    print(f"{my_prog}")
    if not my_prog.Valid():
        print("ProgramB NOT FOUND")
        return
    # Set the runmode to simulate so we can trigger the call within RoboDK
    RDK.setRunMode(RUNMODE_SIMULATE)
    my_prog.RunProgram()
    my_prog.WaitFinished() # Optional, will block until the other program finishes
    RDK.setRunMode(run_mode)
I attached your project with these edits.


Attached Files
.rdk   robot_call_python-v2-Albert.rdk (Size: 2.66 MB / Downloads: 39)
#5
Dear Albert,

Thank you very much for your support.
I have successfully confirmed that the program can be executed using the method you provided.

Please let me know if there are any further steps I should take or if additional verification is required.

Best regards,
Tyler
#6
Thank you for your feedback! No actions to take on your end. We'll improve this behavior with the next version of RoboDK.
  




Users browsing this thread:
1 Guest(s)