Instability with ROS2 service

Typically: "How do I... ", "How can I... " questions
Post Reply
juhel
Posts: 29
Joined: 22 Nov 2020, 23:10

Instability with ROS2 service

Post by juhel »

Hi,

I've downloaded the service_server.ttt example (https://github.com/CoppeliaRobotics/sim ... server.ttt)to have an example of ROS2 service throw a LUA script.
When I run this scene, I can see a new service :

Code: Select all

koala:~$ ros2 service list
/add_two_ints

and in a terminal, I can call this service :

Code: Select all

koala:~$ ros2 service call /add_two_ints example_interfaces/srv/AddTwoInts "{a: 10, b: 5}"
requester: making request: example_interfaces.srv.AddTwoInts_Request(a=10, b=5)

response:
example_interfaces.srv.AddTwoInts_Response(sum=15)
It works!

Now, I've translated this LUA code in Python :

Code: Select all

# Ensure that this script is added as a threaded child script in CoppeliaSim
# Set the script's interpreter to Python

def sysCall_init():
    sim = require('sim')
    simROS2 = require('simROS2')
    global srv
    sim.addLog(sim.verbosity_msgs, 'Thread started')
    # Create the ROS2 service
    srv = simROS2.createService('/add_two_ints', 'example_interfaces/srv/AddTwoInts', srv_callback)

def sysCall_thread():
    while not sim.getSimulationStopping():
        # Keep the thread alive
        sim.wait(0.01)  # Wait for 10ms before the next iteration

def srv_callback(req):
    sim.addLog(sim.verbosity_msgs, f'Service called: {req}')
    # Create the response
    resp = simROS2.createInterface('example_interfaces/srv/AddTwoIntsResponse')
    resp['sum'] = req['a'] + req['b']
    return resp

def sysCall_cleanup():
    # Shutdown the ROS2 service
    simROS2.shutdownService(srv)
    sim.addLog(sim.verbosity_msgs, 'Thread finished')
and now, the first time I call the service, it works but for the following calls, sometimes, I receive this response :
[url]koala:~$ ros2 service call /add_two_ints example_interfaces/srv/AddTwoInts "{a: 10, b: 5}"
requester: making request: example_interfaces.srv.AddTwoInts_Request(a=10, b=5)

response:
example_interfaces.srv.AddTwoInts_Response(sum=0)
[/url]
The sum equals 0, not 15!!! Why? When it appends, the

Code: Select all

[/script:info] Service called: {'a': 10.0, 'b': 5.0}
is not written. So, why sometimes the

Code: Select all

srv_callback
function is not automatically called?

Related question : what should I do to write a (preferably python) ROS2 service (not a topic) to retrieve ONE image from a camera? Should I have to define a new service? How?

Regards,

Philippe
Post Reply