Achieving Desired Control Frequency for a Robotic Arm

Typically: "How do I... ", "How can I... " questions
Post Reply
zorro
Posts: 56
Joined: 06 Nov 2023, 09:31

Achieving Desired Control Frequency for a Robotic Arm

Post by zorro »

Hello!

How can I achieve control of a robotic arm at a specific control frequency? I intend to use 0.005 seconds as the control frequency. Does this mean that I can only achieve this by adjusting the simulation time step (dt) to 0.005 seconds and then using step mode?

Adjusting the simulation dt to 0.005 seconds indeed allows achieving the desired control frequency. However, changing the simulation dt from 0.05 seconds to 0.005 seconds has made the simulation run slower, thus prolonging the training time.

coppelia
Site Admin
Posts: 10647
Joined: 14 Dec 2012, 00:25

Re: Achieving Desired Control Frequency for a Robotic Arm

Post by coppelia »

Hello,

your approach is correct, but you probably could also use joint callback functions or a single dynamics callback function (those functions are called at the same frequency as the dynamics time step, i.e. 5ms by default) and keep the original simulation time step at 50ms. In that case however, communication via the ZeroMQ remote API might be a bit more challenging though (since CoppeliaSim, in that case, cannot actively call an external client to fetch the control values). One could use ZeroMQ functions directly.

Cheers

zorro
Posts: 56
Joined: 06 Nov 2023, 09:31

Re: Achieving Desired Control Frequency for a Robotic Arm

Post by zorro »

Thank you for your reply!

I am very interested in the approach you mentioned using the dynamics callback function. Of course, this method seems to be more advanced, and there are many aspects that I don't fully understand. Therefore, I would appreciate a more detailed explanation. Currently, my understanding of the approach you provided is as follows:

1. The simulation `dt` is still set to 50ms, and the dynamics `dt` remains at the default value of 5ms. I will continue using the ZeroMQ remote API for remote calculations (for now, I still prefer to use the Python remote client because my code is quite a mess...).
2. I will create a new script and initialize the `simZMQ` client in `sysCall_init()`.
3. In the script, I will use the `sysCall_dyn()` function. Inside this function, I will pack the data I need (currently the robot arm TCP pose matrix, end-effector force sensor data, and TCP speed data) using `sim.packTable()`, then send it to the client via `simZMQ`, and wait for the client's calculation result.
4. In the Python remote client, I will use `pyzmq` to create a client that continuously receives simulation data, performs control-related calculations, and then sends the desired pose back to the simulation. I may use `time.sleep()` to ensure the control cycle.
5. Once `sysCall_dyn()` in CoppeliaSim receives the pose data, it will control the robotic arm.

Of course, this is just my basic understanding, and I would appreciate your clarification. It would be even better if there is a minimal demo to achieve the above functionality.

Thanks again!

coppelia
Site Admin
Posts: 10647
Joined: 14 Dec 2012, 00:25

Re: Achieving Desired Control Frequency for a Robotic Arm

Post by coppelia »

That looks right to me. In general you have 2 possibilities, in summary:
  • Use the ZeroMQ remote API with an external client (where CoppeliaSim acts as the server). Run in stepping mode, and have the simulation dt same as the dyn. dt. Speed would suffer, but one can always improve that aspect by handling sensors and other calculations every n-th simulation step (e.g. also by modifying the main script)
  • Use an external client that acts as server to CoppeliaSim, providing the control values matching some simulation state. Communication would be based on ZeroMQ directly (instead of ZeroMQ remote API), that would happen from within a Dynamics callback function (called twice in each dyn. step (before and after stepping the physics engine)).
For the second path, see also the ZeroMQ demo scene in scenes/messaging/zeromqDemo-lua.ttt

Cheers

zorro
Posts: 56
Joined: 06 Nov 2023, 09:31

Re: Achieving Desired Control Frequency for a Robotic Arm

Post by zorro »

Thanks!

I tried starting with `sysCall_dyn` and simply used `print('test')`, but I encountered some issues:
1. It seems that output can only be printed when using the MuJoCo engine.
2. It seems that output can only be printed when using Lua scripts, as there is no output with Python scripts.

My code is simple:
lua:

Code: Select all

-- lua
function sysCall_init()
    sim = require('sim')
end

function sysCall_dyn(inData)
    print('test')
    return inData
end
python:

Code: Select all

# python
def sysCall_init():
    sim = require('sim')

def sysCall_dyn(inData):
    print('dyn')
    return inData

coppelia
Site Admin
Posts: 10647
Joined: 14 Dec 2012, 00:25

Re: Achieving Desired Control Frequency for a Robotic Arm

Post by coppelia »

Make sure that your scene contains some dynamic items, otherwise the callback function won't be called (except with Mujoco). Also, when using Python, you need to explicitly enable that callback, see here for differences between Lua and Python:

Code: Select all

#luaExec dynCallback = true        -- enable dyn callback

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

def sysCall_dyn(inData):
    print('dyn')
    return inData
But keep in mind that using Python for that callback wight drastically slow down things.

Cheers

Post Reply