I cant make simIK work in python

Typically: "How do I... ", "How can I... " questions
Post Reply
javl0p
Posts: 25
Joined: 24 Mar 2023, 10:42

I cant make simIK work in python

Post by javl0p »

Hi!

I'm trying to write the well known IK resolution (simIK) in python.

I think everything in the code below is correct. I don't get any error messages and the Ik resolution flag is set to 1 (which means that the IK is succesfully being computed). However, the tip of my kinematic chain does not move at all. What am i doing wrong?

Code: Select all

#python

def setIK(simBase, simTip, simTarget):

    ikEnv = simIK.createEnvironment()
    
    ikGroup = simIK.createGroup(ikEnv)
    simIK.setGroupCalculation(ikEnv, ikGroup, simIK.method_damped_least_squares, 0.1, 99)
    ikElement = simIK.addElementFromScene(ikEnv, ikGroup, simBase, simTip, simTarget, simIK.constraint_pose)
    simIK.setElementPrecision(ikEnv, ikGroup, ikElement[0], self.precision)
    
    self.ikEnv = ikEnv
    self.ikGroup = ikGroup

def solveIK():
    result, flag, precision = simIK.handleGroup(self.ikEnv, self.ikGroup, {'options.syncWorlds': True, 'options.allowError': False})
    
    if result != 1:
        print("Warning! Inverse kinematics could not be solved. Error code: ", flag)
        sim.stopSimulation()


def sysCall_init():
    sim = require('sim')
    simIK = require('simIK')
    
    linear_tolerance = 1e-6
    angular_tolerance = 1e-4
    
    self.precision = [linear_tolerance, angular_tolerance]
    
    simBase = sim.getObject('/base')
    simTip = sim.getObject('./tip')
    simTarget = sim.getObject('/target')

    setIK(simBase, simTip, simTarget)
    
def sysCall_actuation():
    solveIK()

def sysCall_sensing():
    # put your sensing code here
    pass

def sysCall_cleanup():
    # do some clean-up here
    pass

I can't find any example scene that uses the simIk library in python. I'm using Coppelia v4.6.0, by the way.

Thanks!
coppelia
Site Admin
Posts: 10747
Joined: 14 Dec 2012, 00:25

Re: I cant make simIK work in python

Post by coppelia »

Hello,

have a look at the demo script in programming/zmqRemoteApi/clients/python/simpleMovement.py: it gives the target pose high-level IK commands to CoppeliaSim that executes the IK and motion.

If you really must handle low-level IK remotely, then your code looks ok at first glance, except for this line:

Code: Select all

result, flag, precision = simIK.handleGroup(self.ikEnv, self.ikGroup, {'options.syncWorlds': True, 'options.allowError': False})
which should be:

Code: Select all

result, flag, precision = simIK.handleGroup(self.ikEnv, self.ikGroup, {'syncWorlds': True, 'allowError': False})
Cheers
javl0p
Posts: 25
Joined: 24 Mar 2023, 10:42

Re: I cant make simIK work in python

Post by javl0p »

Great!

The problem laid in the syntax of the options regarding simIK.handleGroups() function.

I could only find detailed documentation of the simIK API for lua, since the python sintaxis documentation is really short.

My problem now is that I'm trying to get a callback function for capturing the Jacobian matrix. I think the problem also lies in the options syntaxis but I can't find the proper way to make it work.

Here is what I have:

Code: Select all

def solveIK():
    result, flag, precision = simIK.handleGroup(self.ikEnv, self.ikGroup, {'syncWorlds': True, 'callback': jacobianCallback})

    if result != 1:
        print("Warning! Inverse kinematics could not be solved. Error code: ", flag)
        sim.stopSimulation()

def jacobianCallback(inData):
    print(inData.jacobian)
But will give me this error:
script, line 16, in solveIK
Exception: .../CoppeliaRobotics/CoppeliaSimPro/lua/pythonWrapperV2.lua:557: func: argument 1 must be a int
coppelia
Site Admin
Posts: 10747
Joined: 14 Dec 2012, 00:25

Re: I cant make simIK work in python

Post by coppelia »

Usually you wouldn't have a Python callback, since that is not very efficient.

But right now there is a bug. You'll need to download the newest simIK.lua and ZMQ remote API server.lua which should fix the problem. Also, we will make a new release within a few days.

Cheers
javl0p
Posts: 25
Joined: 24 Mar 2023, 10:42

Re: I cant make simIK work in python

Post by javl0p »

Ok, thanks for the clarification.

Do I have to download the entire v4.7 Coppelia version to have access to such .lua files or is there a more direct way to get them?

Also, since I only have the Coppelia Pro license up to 4.6.0 version, would I be able to update the ZMQ Remote API server and simIK lua files without issues?
coppelia
Site Admin
Posts: 10747
Joined: 14 Dec 2012, 00:25

Re: I cant make simIK work in python

Post by coppelia »

The best is really to upgrade to V4.8, since most things are inter-connected. V4.8 should be out within a few hours.

Cheers
Post Reply