Rewriting scene tutorial from Lua to Python

Typically: "How do I... ", "How can I... " questions
Post Reply
ulisesfm
Posts: 2
Joined: 27 Feb 2025, 14:04

Rewriting scene tutorial from Lua to Python

Post by ulisesfm »

Hello,

I am trying to rewrite a scene from kinematics tutorials called ikPathGeneration, but instead of using Lua like the example, I want to do it in Python.

I am doing this just for learning purposes.

The example uses a UR10 robot, with 6 pairs joint-link. There's a tip dummy and a target dummy.

I simply rewrote the same code using Python, but I am not able to generate a path and the robot does not move. No errors appear in the sandbox below, but when I print the path generated, it is empty.

Could someone tell me what am I doing different from the example that returns no errors but does not generate a path?

Code: Select all

#python

def sysCall_init():
    sim = require('sim')
    simIK = require('simIK')

    self.simBase = sim.getObject('.')
    self.simTip = sim.getObject('./tip')
    self.simTarget = sim.getObject('/target')
    
    # Prepare an IK group using simIK.addElementFromScene

    self.ikEnv = simIK.createEnvironment()
    self.ikGroup = simIK.createGroup(self.ikEnv)
    
    self.ikElement, self.simToIkMap, self.ikToSimMap = simIK.addElementFromScene(
        self.ikEnv, self.ikGroup, self.simBase, self.simTip, self.simTarget, simIK.constraint_pose
    )

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)

def sysCall_thread():
    simJoints = []
    for i in range(6):
        simJoints.append(sim.getObject('./joint', {'index': i}))
    sim.step()
    
    dynModel = sim.isDynamicallyEnabled(simJoints[0])
    
    ikTip = self.simToIkMap[self.simTip]
    ikJoints = []
    for joint in simJoints:
        ikJoints.append(self.simToIkMap[joint])
        
    # Generate a path with 300 configurations:
    path = simIK.generatePath(self.ikEnv, self.ikGroup, ikJoints, ikTip, 300)
    
    simIK.eraseEnvironment(self.ikEnv)
    
    # Hop through the path configurations:
    while True:
        hopThroughConfigs(path, simJoints, False, dynModel)
        hopThroughConfigs(path, simJoints, True, dynModel)
fferri
Posts: 1334
Joined: 09 Sep 2013, 19:28

Re: Rewriting scene tutorial from Lua to Python

Post by fferri »

Your script looks fine.

Try printing some intermediate values to debug.
ulisesfm
Posts: 2
Joined: 27 Feb 2025, 14:04

Re: Rewriting scene tutorial from Lua to Python

Post by ulisesfm »

Thank you for your answer. I've finally fixed it by simply upgrading CoppeliaSim to the latest version (4.9.0). I was previously using 4.6.0 and was having several problems related to trying to implement Python child scripts into existing examples written in Lua.

Looking into the CoppeliaSim version info document, I saw that recent updates include some bug fixes, and somehow it works fine now. I couldn't point out the exact problem that v4.6.0 had with the code, but it's clear that it does not happen in the latest version anymore.

Thanks anyway!
Post Reply