UR5 with RG2 don't move while using IK mode and Python API.

Typically: "How do I... ", "How can I... " questions
Post Reply
DannyW
Posts: 2
Joined: 01 Dec 2025, 05:45

UR5 with RG2 don't move while using IK mode and Python API.

Post by DannyW »

I attached a force sensor and a RG2 at the end of UR5,
want to use IK mode to control the RG2 moving above Shape0.
After clicking to start simulating and running Python code,
the UR5 had no move, then there was an error message showed :
[Connectivity >> ZMQ remote API server@addOnScript:error] ?: script execution was terminated externally.

I'm sure it's not the problem of Python API because I successfully ran ikPathGeneration.ttt using Python API.
How can I fix this problem?
Looking forward your help.

This is my scene file, https://drive.google.com/file/d/1qK3tBF ... drive_link

This is my Python code.

Code: Select all

#python
import time
from coppeliasim_zmqremoteapi_client import RemoteAPIClient
import numpy as np
import math
import ctypes


def hopThroughConfigs(path, joints, reverse, dynModel):
    lb = sim.setStepping(True)
    total_configs = len(path) // 6  # each configuration holds 6 values
    if not reverse:
        s = 0
        g = total_configs
        incr = 1
    else:
        s = total_configs - 1
        g = -1
        incr = -1
    for i in range(s, g, incr):
        for j in range(len(joints)):
            if dynModel:
                sim.setJointTargetPosition(joints[j], path[i*6 + j])
            else:
                sim.setJointPosition(joints[j], path[i*6 + j])
        sim.step()
    sim.setStepping(lb)
    
    
if __name__ == "__main__":
    # Verify and load the DLL
    dll_path = "C:\Program Files\CoppeliaRobotics\CoppeliaSimEdu\coppeliaSim.dll"
    try:
        ctypes.CDLL(dll_path)
        print("DLL loaded successfully")
    except Exception as e:
        print(f"Error loading DLL: {e}")
        exit()

    # Connect to CoppeliaSim
    client = RemoteAPIClient(port=23000)
    sim = client.require('sim')
    simIK = client.require('simIK')

    # 获取对象句柄
    simBase = sim.getObject('/UR5')
    simTip = sim.getObject('/UR5/tip')
    simGoal = sim.getObject('/target')
    print(simBase, simTip, simGoal)
    
    # 获取6个关节的句柄
    simJoints = []
    for i in range(6):
        joint_handle = sim.getObject('/UR5/joint', {'index': i})
        simJoints.append(joint_handle)
    
    # 跳过第一步仿真以确保命令反映实际情况
    sim.step()
    
    # 检查是否为动力学模型
    dynModel = sim.isDynamicallyEnabled(simJoints[0])
    
    # 创建IK环境
    ikEnv = simIK.createEnvironment()
    ikGroup = simIK.createGroup(ikEnv)
    
    # 从场景添加IK元素
    ikElement, simToIkMap, ikToSimMap = simIK.addElementFromScene(
        ikEnv, ikGroup, simBase, simTip, simGoal, simIK.constraint_pose
    )
    
    # 获取IK环境中的对象句柄
    ikTip = simToIkMap[simTip]
    ikJoints = [simToIkMap[joint] for joint in simJoints]
    
    # 生成路径
    path = simIK.generatePath(ikEnv, ikGroup, ikJoints, ikTip, 300)
    
    # 清理IK环境
    simIK.eraseEnvironment(ikEnv)
    
    print('Move to target position....')
    hopThroughConfigs(path, simJoints, False, dynModel)
    hopThroughConfigs(path, simJoints, True, dynModel)
DannyW
Posts: 2
Joined: 01 Dec 2025, 05:45

Re: UR5 with RG2 don't move while using IK mode and Python API.

Post by DannyW »

Problem solved by referring to this post:
https://forum.coppeliarobotics.com/view ... hp?t=10963

I added a slight angle to the initial position of each joint, then UR5 moved successfully.
coppelia
Site Admin
Posts: 10801
Joined: 14 Dec 2012, 00:25

Re: UR5 with RG2 don't move while using IK mode and Python API.

Post by coppelia »

Hello,

do you have any error message?
It seems you are mixing the legacy remote API (in your script attached to UR5) with the ZeroMQ remote API (your Python script). Just remove the script in your scene.

Then it is fine to handle IK on the Python side, but it would be better if you'd handle it on the CoppeliaSim side (and simply enable/disable it, and simply move the target around from your external Python script).

If you click [Menu bar > Tools > visualize IK worlds], do you see the IK task in the scene?

[EDIT: oops, you already solve your problem. Good!]

Cheers
Post Reply