def vrepInterface(port): global angles_handler angles_handler = np.zeros(12) global angles_error angles_error = np.zeros(12) global clientID clientID = 0 global inital_name print('Program started') vrep.simxFinish(-1) # just in case, close all opened connections ID = vrep.simxStart('127.0.0.1', port, True, True, 5000, 5) # Connect to V-REP print(ID) if ID != -1: print('Connected to remote API server') # Now try to retrieve data in a blocking fashion (i.e. a service call): res, objs = vrep.simxGetObjects(ID, vrep.sim_handle_all, vrep.simx_opmode_blocking) if res == vrep.simx_return_ok: print('Number of objects in the scene: ', len(objs)) else: print('Remote API function call returned with error code: ', res) else: print('DIDNOT CONNECT!!!') intial_name = [ 'ab3', 'bc3', 'cd3', 'ab4', 'bc4', 'cd4', 'ab1', 'bc1', 'cd1', 'ab2', 'bc2', 'cd2' ] for i in range(angles_handler.shape[0]): angles_error[i], angles_handler[i] = vrep.simxGetObjectHandle( clientID, intial_name[i], vrep.simx_opmode_blocking) return ID
def init(): global robot global blockHandleArray, connectionTime print('Program started') vrep.simxFinish(-1) # just in case, close all opened connections int_portNb = 19999 # define port_nr clientID = vrep.simxStart('127.0.0.1', int_portNb, True, True, 5000, 5) # connect to server if clientID != -1: print('Connected to remote API server') res,objs = vrep.simxGetObjects(clientID,vrep.sim_handle_all,vrep.simx_opmode_oneshot_wait) # get all objects in the scene if res == vrep.simx_return_ok: # Remote function call succeeded print('Number of objects in the scene: ',len(objs))# print number of object in the scene ret_lm, leftMotorHandle = vrep.simxGetObjectHandle(clientID, 'Pioneer_p3dx_leftMotor', vrep.simx_opmode_oneshot_wait) ret_rm, rightMotorHandle = vrep.simxGetObjectHandle(clientID, 'Pioneer_p3dx_rightMotor', vrep.simx_opmode_oneshot_wait) ret_pr, pioneerRobotHandle = vrep.simxGetObjectHandle(clientID, 'Pioneer_p3dx', vrep.simx_opmode_oneshot_wait) ret_sl, ultraSonicSensorLeft = vrep.simxGetObjectHandle(clientID, 'Pioneer_p3dx_ultrasonicSensor4',vrep.simx_opmode_oneshot_wait) ret_sr, ultraSonicSensorRight = vrep.simxGetObjectHandle(clientID, 'Pioneer_p3dx_ultrasonicSensor5',vrep.simx_opmode_oneshot_wait) blockHandleArray = [] for i_block in range(12): blockName = 'ConcretBlock#'+str(i_block) retCode, handle = vrep.simxGetObjectHandle(clientID, blockName, vrep.simx_opmode_oneshot_wait) assert retCode==0, retCode if i_block>6: rand_loc = [random.random()*6-1.5, random.random()*7-2.5, 0.0537] # x[-1.5,4.5] y[-2.5,-4.5] vrep.simxSetObjectPosition(clientID, handle, -1, rand_loc, vrep.simx_opmode_oneshot_wait) retCode,position = vrep.simxGetObjectPosition(clientID, handle, -1, vrep.simx_opmode_oneshot_wait) assert retCode==0, retCode blockHandleArray.append([handle,i_block,position]) robot = EasyDict(clientID=clientID, leftMotorHandle=leftMotorHandle, rightMotorHandle=rightMotorHandle, pioneerRobotHandle=pioneerRobotHandle, ultraSonicSensorLeft=ultraSonicSensorLeft, ultraSonicSensorRight=ultraSonicSensorRight, energySensor=None) connectionTime = vrep.simxGetLastCmdTime(robot.clientID) return robot else: print('Remote API function call returned with error code: ',res) vrep.simxFinish(clientID) # close all opened connections else: print('Failed connecting to remote API server') print('Program finished') return {}
def main(): vrep.simxFinish(-1) clientID = vrep.simxStart('127.0.0.1', 19997, True, True, 5000, 5) if clientID == -1: print('Failed connecting to remote API server') return print('Connected to remote API server') # Now try to retrieve data in a blocking fashion (i.e. a service call): res, objs = vrep.simxGetObjects(clientID, vrep.sim_handle_all, vrep.simx_opmode_blocking) if res == vrep.simx_return_ok: print('Number of objects in the scene:', len(objs)) else: print('Remote API function call returned with error code:', res) sleep(2) # Now retrieve streaming data (i.e. in a non-blocking fashion): vrep.simxGetIntegerParameter( clientID, vrep.sim_intparam_mouse_x, vrep.simx_opmode_streaming) # Initialize streaming err, Cub1_handle = vrep.simxGetObjectHandle(clientID, "Cub1", vrep.simx_opmode_oneshot_wait) err, nozzle_handle = vrep.simxGetObjectHandle( clientID, "nozzle", vrep.simx_opmode_oneshot_wait) # M path # m_path(30, clientID, Cub1_handle) #walk_to(0, 0, 0.5, clientID, Cub1_handle) # walk_with(1, 0, 0, clientID, Cub1_handle) #walk_with(-0.3, -0.5, 0, clientID, Cub1_handle) walk_with(-0.3, -0.3, 0, clientID, Cub1_handle) walk_with(0.3, 0.3, 0, clientID, Cub1_handle) # Now send some data to V-REP in a non-blocking fashion: vrep.simxAddStatusbarMessage(clientID, 'Hello V-REP!', vrep.simx_opmode_oneshot) # Before closing the connection to V-REP, make sure that the last command sent out had time to arrive. You can guarantee this with (for example): vrep.simxGetPingTime(clientID) # Now close the connection to V-REP: vrep.simxFinish(clientID)
def __init__(self, config={}, port_num=19997): print ('Program started') vrep.simxFinish(-1) # just in case, close all opened connections self.clientID = vrep.simxStart('127.0.0.1', port_num, True, True, 5000, 5) # Connect to V-REP if self.clientID != -1: print ('Connected to remote API server') vrep.simxStartSimulation( self.clientID, vrep.simx_opmode_oneshot_wait) vrep.simxSynchronous(self.clientID, True) # Now try to retrieve data in a blocking fashion (i.e. a service # call): res, objs = vrep.simxGetObjects( self.clientID, vrep.sim_handle_all, vrep.simx_opmode_blocking) if res == vrep.simx_return_ok: print ('Number of objects in the scene: ', len(objs)) else: print ('Remote API function call returned with error code: ', res) print("connected through port number: {}".format(port_num)) # used to connect multiple clients in synchronous mode http://www.coppeliarobotics.com/helpFiles/en/remoteApiModusOperandi.htm return_code, iteration = vrep.simxGetIntegerSignal(self.clientID, "iteration", vrep.simx_opmode_streaming) time.sleep(2)
print('or appropriately adjust the file "vrep.py"') print('--------------------------------------------------------------') print('') import time sim_boolparam_vision_sensor_handling_enabled = 10 print('Program started') vrep.simxFinish(-1) # just in case, close all opened connections clientID = vrep.simxStart('127.0.0.1', 20001, True, True, 5000, 5) # Connect to V-REP if clientID != -1: print('Connected to remote API server') # Now try to retrieve data in a blocking fashion (i.e. a service call): res, objs = vrep.simxGetObjects(clientID, vrep.sim_handle_all, vrep.simx_opmode_blocking) if res == vrep.simx_return_ok: print('Number of objects in the scene: ', len(objs)) else: print('Remote API function call returned with error code: ', res) time.sleep(2) LASTSENSOR = 0 # Initializate r, LeftSensor_handle = vrep.simxGetObjectHandle(clientID, "LeftSensor#", vrep.simx_opmode_blocking) r, MiddleSensor_handle = vrep.simxGetObjectHandle( clientID, "MiddleSensor#", vrep.simx_opmode_blocking) r, RightSensor_handle = vrep.simxGetObjectHandle(clientID, "RightSensor#",
plantPosition = [[2, 2, 0.1], [-2, -2, 0.1]] if dist2Plant < 0.8: if currentPlantPosition[0] > 0: PosIdx = 1 else: PosIdx = 0 vrep.simxSetObjectPosition(clientID, indoorPlantHandle, -1, plantPosition[PosIdx], vrep.simx_opmode_oneshot) print 'Program started' vrep.simxFinish(-1) # just in case, close all opened connections int_portNb = 19999 # define port_nr clientID = vrep.simxStart( '127.0.0.1', int_portNb, True, True, 5000, 5) # connect to server if clientID != -1: print 'Connected to remote API server' res,objs = vrep.simxGetObjects(clientID,vrep.sim_handle_all,vrep.simx_opmode_oneshot_wait) # get all objects in the scene if res == vrep.simx_return_ok: # Remote function call succeeded print 'Number of objects in the scene: ',len(objs)# print number of object in the scene ret_lm, leftMotorHandle = vrep.simxGetObjectHandle(clientID, 'Pioneer_p3dx_leftMotor', vrep.simx_opmode_oneshot_wait) ret_rm, rightMotorHandle = vrep.simxGetObjectHandle(clientID, 'Pioneer_p3dx_rightMotor', vrep.simx_opmode_oneshot_wait) ret_pr, pioneerRobotHandle = vrep.simxGetObjectHandle(clientID, 'Pioneer_p3dx', vrep.simx_opmode_oneshot_wait) ret_pl, indoorPlantHandle = vrep.simxGetObjectHandle(clientID, 'indoorPlant', vrep.simx_opmode_oneshot_wait) ret_sl, ultraSonicSensorStraightLeft = vrep.simxGetObjectHandle(clientID, 'Pioneer_p3dx_ultrasonicSensor' + str(4),vrep.simx_opmode_oneshot_wait) ret_sr, ultraSonicSensorStraightRight = vrep.simxGetObjectHandle(clientID, 'Pioneer_p3dx_ultrasonicSensor' + str(5),vrep.simx_opmode_oneshot_wait) ret_sr, ultraSonicSensorAngleLeft = vrep.simxGetObjectHandle(clientID, 'Pioneer_p3dx_ultrasonicSensor' + str(3),vrep.simx_opmode_oneshot_wait) ret_sr, ultraSonicSensorAngleRight = vrep.simxGetObjectHandle(clientID, 'Pioneer_p3dx_ultrasonicSensor' + str(6),vrep.simx_opmode_oneshot_wait) ret_sr, ultraSonicSensorLeft = vrep.simxGetObjectHandle(clientID, 'Pioneer_p3dx_ultrasonicSensor' + str(1),vrep.simx_opmode_oneshot_wait) ret_sr, ultraSonicSensorRight = vrep.simxGetObjectHandle(clientID, 'Pioneer_p3dx_ultrasonicSensor' + str(8),vrep.simx_opmode_oneshot_wait) ret_sr, ultraSonicSensorBackLeft = vrep.simxGetObjectHandle(clientID, 'Pioneer_p3dx_ultrasonicSensor' + str(14),vrep.simx_opmode_oneshot_wait) ret_sr, ultraSonicSensorBackRight = vrep.simxGetObjectHandle(clientID, 'Pioneer_p3dx_ultrasonicSensor' + str(10),vrep.simx_opmode_oneshot_wait)
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') # Now try to retrieve data in a blocking fashion (i.e. a service call): res,objs=vrep.simxGetObjects(clientID,vrep.sim_handle_all,vrep.simx_opmode_blocking) if res==vrep.simx_return_ok: print ('Number of objects in the scene: ',len(objs)) else: print ('Remote API function call returned with error code: ',res) time.sleep(2) # Now retrieve streaming data (i.e. in a non-blocking fashion): startTime=time.time() vrep.simxGetIntegerParameter(clientID,vrep.sim_intparam_mouse_x,vrep.simx_opmode_streaming) # Initialize streaming while time.time()-startTime < 5: returnCode,data=vrep.simxGetIntegerParameter(clientID,vrep.sim_intparam_mouse_x,vrep.simx_opmode_buffer) # Try to retrieve the streamed data if returnCode==vrep.simx_return_ok: # After initialization of streaming, it will take a few ms before the first value arrives, so check the return code print ('Mouse position x: ',data) # Mouse position x is actualized when the cursor is over V-REP's window time.sleep(0.005)
import sys import time import math vrep.simxFinish(-1) clientID = vrep.simxStart('127.0.0.1', 19999, True, True, 5000, 5) if clientID != -1: print("Connected to remote server") else: print('Connection not successful') sys.exit('Could not connect') print( vrep.simxGetObjects(clientID, vrep.sim_handle_all, vrep.simx_opmode_oneshot_wait)) errorCode, angle1 = vrep.simxGetObjectHandle(clientID, 'LBR4p_joint1', vrep.simx_opmode_blocking) errorCode, angle2 = vrep.simxGetObjectHandle(clientID, 'LBR4p_joint2', vrep.simx_opmode_blocking) errorCode, angle3 = vrep.simxGetObjectHandle(clientID, 'LBR4p_joint4', vrep.simx_opmode_blocking) errorCode, angle4 = vrep.simxGetObjectHandle(clientID, 'LBR4p_joint5', vrep.simx_opmode_blocking) errorCode, angle5 = vrep.simxGetObjectHandle(clientID, 'LBR4p_joint6', vrep.simx_opmode_blocking) if errorCode == -1: print('Can not find left or right motor') sys.exit()
portID = 19997 # connect to V-rep clientID=vrep.simxStart('127.0.0.1',portID,True,True,5000,5) if clientID!=-1: print('Connected to remote API server via portID {}'.format(portID)) else: print('Failed connecting to remote API server') vrep.simxFinish(clientID) exit(1) #(re)start the simulation vrep.simxStartSimulation(clientID,vrep.simx_opmode_oneshot_wait) # get handle/pointer to all objects in the V-rep scene err,objs=vrep.simxGetObjects(clientID,vrep.sim_handle_all,vrep.simx_opmode_oneshot_wait) printErr(err, 'Number of objects in the scene: {}'.format(len(objs)), 'Remote API function call returned with error code: {}'.format(err)) # show message in status bar below the V-REP main window vrep.simxAddStatusbarMessage (clientID,"Connect from Python Client",vrep.simx_opmode_oneshot_wait) # get handles for the 4 wheel motors err,frontleftMotorHandle=vrep.simxGetObjectHandle(clientID,"rollingJoint_fl",vrep.simx_opmode_oneshot_wait) printErr(err, 'Get handle for front left motor: {}'.format(frontleftMotorHandle), 'Error by getting handle for front left motor: {}'.format(err)) err,rearleftMotorHandle=vrep.simxGetObjectHandle(clientID,"rollingJoint_rl",vrep.simx_opmode_oneshot_wait) printErr(err, 'Get handle for rear left motor: {}'.format(rearleftMotorHandle), 'Error by getting handle for rear left motor: {}'.format(err)) err,rearrightMotorHandle=vrep.simxGetObjectHandle(clientID,"rollingJoint_rr",vrep.simx_opmode_oneshot_wait) printErr(err, 'Get handle for rear right motor: {}'.format(rearrightMotorHandle), 'Error by getting handle for rear right motor: {}'.format(err))
def get_object_handles(self): rc, arr_Handles = vrep.simxGetObjects(self.client_id, vrep.sim_object_joint_type, blocking) return arr_Handles
def getObjects(self, objectType, operationMode = vrep.simx_opmode_oneshot_wait): return vrep.simxGetObjects(self.clientID, objectType, operationMode)
def get_all_objects(self): res, objs = vrep.simxGetObjects(self._id, vrep.sim_handle_all, vrep.simx_opmode_oneshot_wait) self.error_check(res) return objs
# www.coppeliarobotics.com # # ------------------------------------------------------------------- # This file is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. # # You are free to use/modify/distribute this file for whatever purpose! # ------------------------------------------------------------------- # # This file was automatically created for V-REP release V3.0.1 on January 20th 2013 # Make sure to have the server side running in V-REP! # Start the server from a child script with following command: # simExtRemoteApiStart(19999) -- starts a remote API server service on port 19999 import vrep print 'Program started' if vrep.simxStart('127.0.0.1',19999,True,True,5000)!=-1: print 'Connected to remote API server' res,objs=vrep.simxGetObjects(vrep.sim_handle_all,vrep.simx_opmode_oneshot_wait) if res==vrep.simx_error_noerror: print 'Number of objects in the scene: ',len(objs) else: print 'Remote API function call returned with error code: ',res vrep.simxFinish() else: print 'Failed connecting to remote API server' print 'Program ended'