Multi-threaded connection using simxStart

Typically: "How do I... ", "How can I... " questions
Post Reply
hskim
Posts: 2
Joined: 14 May 2013, 09:43

Multi-threaded connection using simxStart

Post by hskim » 30 May 2013, 14:25

Dear all,

I'm try to control the joints in separate threads with Python under Windows 7 32bit. But I cannot get connected to V-REP successfully.

Here my code goes,

Code: Select all

import threading
import time
import sys
import traceback

sys.path.insert(0, 'C:\\Program Files\\V-REP3\\V-REP_PRO_EDU\\programming\\Python')
import vrep

class Joint(threading.Thread):
    def __init__(self, joint_name, port):
        threading.Thread.__init__(self)
        self.client_id = -1
        self.port = port
        self.joint_name = joint_name

    def run(self):
        try:
            # connect to V-REP server
            self.client_id = vrep.simxStart("127.0.0.1", self.port, False, True, 5000, 5)
            if self.client_id == -1:
                raise Exception('Failed to connect V-REP remote API server.')
            
            # get handle
            res, p = vrep.simxGetObjectHandle(self.client_id, self.joint_name.encode(), vrep.simx_opmode_oneshot_wait)
            if res == vrep.simx_error_noerror:
                print('[Joint %s] handle= %s' % (self.joint_name, p))
            else:
                raise Exception('Error in getting joint handles.')

        except Exception as e:
            print('[Joint %s] %s' % (self.joint_name, e.args[0]))
            traceback.print_exc()

        finally:
            # disconnect with V-REP server
            vrep.simxFinish(self.client_id)


if __name__ == "__main__":
    joints = [Joint("joint1", 19999), Joint("joint2", 19998)]
    for joint in joints:
        joint.start()
    time.sleep(1)
    for joint in joints:
        joint.join()
    print("Done")
Joint1 always cannot get connected to V-REP, it get stuck in simxStart(...).

I have set the content of remoteApiConnections.txt to

// This file defines all the continuous remote API server services (started at remote API plugin initialization)
//
// Each remote API server service requires following 3 entries:
//
// portIndex@_port = xxxx // where xxxx is the desired port number (below 20000 are preferred for server services starting at V-REP start-up)
// portIndex@_debug = xxxx // where xxxx is true or false
// portIndex@_syncSimTrigger = xxxx // where xxxx is true or false. When true, then the service will be pre-enabled for synchronous operation. Only one service at a time can be pre-enabled for synchronous operation!
//
// In above strings, @ can be any number starting with 1. If more than one server service is required, then numbers need to be consecutive and starting with 1
//
// As an example:
//
portIndex1_port = 19999
portIndex1_debug = false
portIndex1_syncSimTrigger = true
portIndex2_port = 19998
portIndex2_debug = false
portIndex2_syncSimTrigger = false


And I also checked the connection of port 19998 and 19999, surprisingly port 19999 is not working, I suppose that if these 2 ports are both working, the V-REP connection would be OK, but how can I set the both ports connected?

Best Regards.

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

Re: Multi-threaded connection using simxStart

Post by coppelia » 30 May 2013, 17:03

Hello,

Try putting a small delay between the joint.start() commands (e.g. time.sleep(0.1)): a small bug allows that the library gets initialized twice if simxStart is called a second time soon after the first time. This will be fixed in next release.

Cheers

hskim
Posts: 2
Joined: 14 May 2013, 09:43

Re: Multi-threaded connection using simxStart

Post by hskim » 17 Jun 2013, 12:51

coppelia wrote:Hello,

Try putting a small delay between the joint.start() commands (e.g. time.sleep(0.1)): a small bug allows that the library gets initialized twice if simxStart is called a second time soon after the first time. This will be fixed in next release.

Cheers
Thanks! This solves my problem!

npd15393
Posts: 3
Joined: 22 Jul 2018, 15:07

Re: Multi-threaded connection using simxStart

Post by npd15393 » 12 Aug 2018, 23:41

Hello,
So I'm using two ports for each of the mobile robots to be controlled. Each robot has an independent go-to-goal controller that is executed. When I try to read the current position of the robot using getObjectPosition I get all zeros. Adding a delay between thread starts doesn't help. Please let me know what am I missing.

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

Re: Multi-threaded connection using simxStart

Post by coppelia » 17 Aug 2018, 13:19

Hello,

can you show use how you are reading the position?

Cheers

Post Reply