Exemplo n.º 1
0
    def init(self, scene=""):
        # close any open connections
        vrep.simxFinish(-1)
        # Connect to the V-REP continuous server
        self.clientID = vrep.simxStart('127.0.0.1', 19997, True, True, 500, 5)

        if self.clientID == -1:  # connection error
            raise Exception(
                'VrepWorld : Failed connecting to remote API server')

        vrep.simxSynchronous(self.clientID, True)
        print('Connected to remote API server')

        ##### NOT WORKING YET ###
        # simxLoadScene
        # clientID: the client ID. refer to simxStart.
        # scenePathAndName: the scene filename, including the path and extension ("ttt"). The file is relative to the client or server system depending on the options value (see next argument)
        # options: options, bit-coded:
        # bit0 set: the specified file is located on the client side (in that case the function will be blocking since the scene first has to be transferred to the server). Otherwise it is located on the server side
        # operationMode: a remote API function operation mode. Recommended operation mode for this function is simx_opmode_blocking
        if scene != "":
            scenePath = os.path.join(os.path.dirname(__file__), scene)
            print(scenePath)
            err = vrep.simxLoadScene(self.clientID, scenePath, 1,
                                     vrep.simx_opmode_blocking)
            if err != 0:
                raise Exception("VrepRobot could load scene. Error " +
                                str(err))
            print('Scene loaded')
Exemplo n.º 2
0
 def close(self):
     """Clean-up
     """
     from vrep import vrep as vrep
     # stop the simulation:
     vrep.simxStopSimulation(self._clientID, vrep.simx_opmode_oneshot_wait)
     # close the connection to V-REP:
     vrep.simxFinish(self._clientID)
Exemplo n.º 3
0
 def close(self):
     """Clean-up
     """
     from vrep import vrep as vrep
     # stop the simulation:
     vrep.simxStopSimulation(self._clientID, vrep.simx_opmode_oneshot_wait)
     # close the connection to V-REP:
     vrep.simxFinish(self._clientID)
Exemplo n.º 4
0
    def close(self):
        # Before closing the connection to V-REP,
        # make sure that the last command sent out had time to arrive.
        vrep.simxGetPingTime(self.clientID)

        # Now close the connection to V-REP:
        vrep.simxFinish(self.clientID)
        print('connection closed...')
Exemplo n.º 5
0
    def __init__(self):
        """Constructor.
        """
        from vrep import vrep as vrep
        vrep.simxFinish(-1)  # just in case, close all opened connections
        self._clientID = vrep.simxStart('127.0.0.1', 19997, True, True, 5000, 5)  # Connect to V-REP

        # enable the synchronous mode on the client:
        vrep.simxSynchronous(self._clientID, True)

        # start the simulation:
        vrep.simxStartSimulation(self._clientID, vrep.simx_opmode_oneshot_wait)
Exemplo n.º 6
0
    def create_virtual_create(self, hostname):
        """Instantiates a new virtual create for visualization (only a single one is supported!)

        Returns:
            (visualization.VirtualCreate) instance of visualization.VirtualCreate
        """
        from vrep import vrep as vrep
        vrep.simxFinish(-1)  # just in case, close all opened connections
        self._clientID = vrep.simxStart(hostname, 19997, True, True, 5000, 5)  # Connect to V-REP

        from visualization import VirtualCreate
        return VirtualCreate(self._clientID)
Exemplo n.º 7
0
    def start_server(self):
        # Finish any previous open communication
        if self.force_finish_comm:
            vrep.simxFinish(-1)

        # Try connection
        client_id = vrep.simxStart(self.server_ip, self.server_port,
                                   self.wait_connection, self.do_not_reconnect,
                                   self.time_out_ms, self.comm_thread_cycle)

        if client_id == -1:
            # Connection failed
            raise Exception("Failed to connect to server. Server IP: " +
                            self.server_ip + ":" + str(self.server_port))

        return client_id
Exemplo n.º 8
0
    def __init__(self):
        # just in case, close all opened connections
        vrep.simxFinish(-1)

        self.client_id = vrep.simxStart('127.0.0.1', 5000, True, True, 5000, 5)

        if self.client_id != -1:  # check if client connection successful
            print('Connected to remote API server')
        else:
            print('Connection not successful')
            sys.exit('Could not connect')

        # Restart the simulation
        self.reset()

        # Get handles
        self.get_handles()
Exemplo n.º 9
0
    def __init__(self, hostname):
        """Constructor.

        Args:
            hostname (string): IP address for host running the simulation.
        """
        vrep.simxFinish(-1)

        self._clientID = vrep.simxStart(hostname, 19997, True, False, 5000, 5)  # Connect to V-REP
        print("clientId:", self._clientID)

        # query objects
        rc, self._obj = vrep.simxGetObjectHandle(self._clientID, "create_estimate", vrep.simx_opmode_oneshot_wait)
        print("Return code 1:", rc)
        # Use custom GUI
        rc, self._uiHandle = vrep.simxGetUIHandle(self._clientID, "UI", vrep.simx_opmode_oneshot_wait)
        print("Return code 2:", rc)

        vrep.simxGetUIEventButton(self._clientID, self._uiHandle, vrep.simx_opmode_streaming)
        print("RealCreate uiHandle:", self._uiHandle)
Exemplo n.º 10
0
    def __init__(self):
        fourcc = cv2.VideoWriter_fourcc(*'mp4v')
        self.out = cv2.VideoWriter('output.mp4', fourcc, 15.0, (1024, 1024))

        self.DoF = 6
        # self.action_bound = [[-15,15],[-10,110],[-30,30],[-120,120],[-180,180],[-180,180]]
        self.action_bound = [[-180, 180], [-180, 180], [-180, 180],
                             [-180, 180], [-180, 180], [-180, 180]]
        self.action_range = [-5, 5]
        self.action_dim = self.DoF

        self.action_space = gym.spaces.Box(shape=(self.DoF, ), low=-5, high=5)
        self.observation_space = gym.spaces.Box(shape=(3 + 6 + self.DoF * 2, ),
                                                low=-180,
                                                high=180)
        self.action_dim = self.action_space.shape[0]
        self.state_dim = self.observation_space.shape[0]

        p = subprocess.Popen(['ps', '-A'], stdout=subprocess.PIPE)
        out, err = p.communicate()
        for line in out.splitlines():
            if b'vrep' in line:
                pid = int(line.split(None, -1)[0])
                os.kill(pid, signal.SIGKILL)

        self.vrep_root = "/home/ali/Downloads/VREP"
        self.scene_file = "/home/ali/RL_code/env/rozum_model.ttt"
        #
        os.chdir(self.vrep_root)
        os.system("./vrep.sh -s " + self.scene_file + " &")

        vrep.simxFinish(-1)
        time.sleep(1)

        # get the ID of the running simulation
        self.ID = vrep.simxStart('127.0.0.1', 19999, True, False, 5000, 5)
        # check the connection
        if self.ID != -1:
            print("Connected")
        else:
            sys.exit("Error")
        # get handles
        # for camera
        self.cam_handle = self.get_handle('Vision_sensor')
        (code, res,
         im) = vrep.simxGetVisionSensorImage(self.ID, self.cam_handle, 0,
                                             const_v.simx_opmode_streaming)

        self.render_handle = self.get_handle('render')
        (code, res,
         im) = vrep.simxGetVisionSensorImage(self.ID, self.render_handle, 0,
                                             const_v.simx_opmode_streaming)

        # joints
        self.joint_handles = []
        for i in range(self.DoF):
            tmp = self.get_handle("joint%d" % (i))
            self.joint_handles.append(tmp)
            code, angle = vrep.simxGetJointPosition(
                self.ID, tmp, const_v.simx_opmode_streaming)

        # gripper tip
        self.tip_handle = self.get_handle("Tip")
        (code,
         pose) = vrep.simxGetObjectPosition(self.ID, self.tip_handle, -1,
                                            const_v.simx_opmode_streaming)
        self.or_handle = self.get_handle("RG2_baseVisible")
        (code,
         pose) = vrep.simxGetObjectOrientation(self.ID, self.or_handle, -1,
                                               const_v.simx_opmode_streaming)
        # cube
        self.cube_handle = self.get_handle("Cube")
        (code,
         pose) = vrep.simxGetObjectPosition(self.ID, self.cube_handle, -1,
                                            const_v.simx_opmode_streaming)
        (code,
         pose) = vrep.simxGetObjectOrientation(self.ID, self.cube_handle, -1,
                                               const_v.simx_opmode_streaming)
        # get the goal handle
        self.goal_handle = self.get_handle("Goal")
        (code,
         pose) = vrep.simxGetObjectPosition(self.ID, self.goal_handle, -1,
                                            const_v.simx_opmode_streaming)

        # angles' array
        self.angles = self.get_angles()

        # gripper handles (used in closing and opening gripper)
        self.gripper_motor = self.get_handle('RG2_openCloseJoint')
        # task part
        self.task_part = 0

        self.init_angles = self.get_angles()
        self.init_orientation = self.get_orientation(self.or_handle)
        self.init_pose_cube = self.get_position(self.cube_handle)
        # print(self.init_pose_cube)
        self.init_goal_pose = self.get_position(self.goal_handle)
        # print(self.init_goal_pose)
        self.open_gripper()
        self.reset()
        self.tip_position = self.get_position(self.tip_handle)

        self.goal_l = (80, 0, 0)
        self.goal_u = (120, 255, 255)
        self.cube_l = (55, 50, 50)
        self.cube_u = (80, 255, 255)
        self.er_kernel = np.ones((2, 2), np.uint8)
        self.di_kernel = np.ones((2, 22), np.uint8)
        self.task_part = 0
        self.part_1_center = np.array([300.0, 335.0])
        self.part_2_center = np.array([320.0, 290.0])
        self.part_1_area = 0.25
        self.part_2_area = 0.75
Exemplo n.º 11
0
try:
    from vrep import vrep
    from vrep import vrepConst
except:
    print('--------------------------------------------------------------')
    print('"vrep.py" could not be imported. This means very probably that')
    print('either "vrep.py" or the remoteApi library could not be found.')
    print('Make sure both are in the same folder as this file,')
    print('or appropriately adjust the file "vrep.py"')
    print('--------------------------------------------------------------')
    print('')

import time

print('Program started')
vrep.simxFinish(-1)  # just in case, close all opened connections
clientID = vrep.simxStart('127.0.0.1', 19999, True, True, 5000,
                          5)  # Connect to V-REP

if clientID != -1:
    print('Connected to remote API server')
else:
    print('Failed connecting to remote API server')
print('Program ended')
#vrep.sim_object_shape_type
#vrep.simx_opmode_oneshot
#simxGetObjectHandle
errorCode, carro = vrep.simxGetObjectHandle(clientID, 'Pioneer_p3dx',
                                            vrep.simx_opmode_oneshot_wait)
errorCode, left_Motor = vrep.simxGetObjectHandle(clientID,
                                                 'Pioneer_p3dx_leftMotor',
Exemplo n.º 12
0
    def __init__(self):
        self.DoF = 6
        self.action_bound = [-5, 5]
        self.action_dim = self.DoF

        # os.chdir("/Vrep_server")
        # os.system("git pull")
        # os.chdir("/")
        #
        # self.vrep_root = "/V-REP_PRO_EDU_V3_5_0_Linux/"
        # self.scene_file = "/Vrep_server/env/rozum_model.ttt"
        #
        # os.chdir(self.vrep_root)
        # os.system("xvfb-run --auto-servernum --server-num=1 -s \"-screen 0 640x480x24\" ./vrep.sh -h -s " + self.scene_file + " &")

        vrep.simxFinish(-1)
        time.sleep(1)

        # get the ID of the running simulation
        self.ID = vrep.simxStart('127.0.0.1', 19999, True, False, 5000, 5)
        # check the connection
        if self.ID != -1:
            print("Connected")
        else:
            sys.exit("Error")
        # get handles
        # for camera
        self.cam_handle = self.get_handle('Vision_sensor')
        (code, res,
         im) = vrep.simxGetVisionSensorImage(self.ID, self.cam_handle, 0,
                                             const_v.simx_opmode_streaming)

        # joints
        self.joint_handles = []
        for i in range(self.DoF):
            tmp = self.get_handle("joint%d" % (i))
            self.joint_handles.append(tmp)
            code, angle = vrep.simxGetJointPosition(
                self.ID, tmp, const_v.simx_opmode_streaming)

        # gripper tip
        self.tip_handle = self.get_handle("Tip")
        (code,
         pose) = vrep.simxGetObjectPosition(self.ID, self.tip_handle, -1,
                                            const_v.simx_opmode_streaming)
        # cube
        self.cube_handle = self.get_handle("Cube")
        (code,
         pose) = vrep.simxGetObjectPosition(self.ID, self.cube_handle, -1,
                                            const_v.simx_opmode_streaming)
        # get the goal handle
        self.goal_handle = self.get_handle("Goal")
        (code,
         pose) = vrep.simxGetObjectPosition(self.ID, self.goal_handle, -1,
                                            const_v.simx_opmode_streaming)

        # angles' array
        self.angles = self.get_angles()

        # gripper handles (used in closing and opening gripper)
        self.gripper_motor = self.get_handle('RG2_openCloseJoint')
        # task part
        self.task_part = 0

        self.init_angles = self.get_angles()

        self.init_pose_cube = self.get_position(self.cube_handle)
        # print(self.init_pose_cube)
        self.init_goal_pose = self.get_position(self.goal_handle)
        # print(self.init_goal_pose)
        self.open_gripper()
        self.reset()
        self.tip_position = self.get_position(self.tip_handle)

        self.goal_l = (80, 0, 0)
        self.goal_u = (120, 255, 255)
        self.cube_l = (55, 50, 50)
        self.cube_u = (80, 255, 255)
        self.er_kernel = np.ones((2, 2), np.uint8)
        self.di_kernel = np.ones((2, 22), np.uint8)
        self.task_part = 0
        self.part_1_center = np.array([300.0, 335.0])
        self.part_2_center = np.array([320.0, 290.0])
        self.part_1_area = 0.25
        self.part_2_area = 0.75
Exemplo n.º 13
0
 def close_connection(self):
     v.simxFinish(self._id)
Exemplo n.º 14
0
 def finish_server(self):
     # Finish server
     vrep.simxFinish(self.client_id)
Exemplo n.º 15
0
    def disconnect(self):
        self.check_is_connected()

        # Disconnect from server
        vrep.simxFinish(self.id)
        self.is_connected = False
Exemplo n.º 16
0
from threading import Thread

p = subprocess.Popen(['ps', '-A'], stdout=subprocess.PIPE)
out, err = p.communicate()
for line in out.splitlines():
    if b'vrep' in line:
        pid = int(line.split(None, -1)[0])
        os.kill(pid, signal.SIGKILL)

vrep_root = "/home/ali/Downloads/VREP"
scene_file = "/home/ali/PycharmProjects/Multi/multi.ttt"

os.chdir(vrep_root)
os.system("./vrep.sh -s " + scene_file + " &")

vrep.simxFinish(-1)
time.sleep(1)

# get the ID of the running simulation
ID = vrep.simxStart('127.0.0.1', 19999, True, False, 5000, 5)
# check the connection
if ID != -1:
    print("Connected")
else:
    sys.exit("Error")

start = 0.0


class Robot:
    def __init__(self, ID, num):
    def create_connect(self):
        vrep.simxFinish(-1)
        self.clientId = vrep.simxStart('127.0.0.1', 19999, True, True, 5000, 5)

        if self.check_connection():
            self.sensor = Sensor(self.clientId)