vrep-python 控制方法


python控制程序 .py 文件控制vrep常常包括以下步驟:

1.引入模塊

import vrep

2.建立連接,先關閉上一次連接

print('Program started')

# Close potential connections
vrep.simxFinish(-1)

clientID = vrep.simxStart('127.0.0.1', 19997, True, True, 5000, 5)
print("Connection success")

if clientID!=-1:
  print ('Connected to remote API server')

3.開始仿真

# Start simulation
vrep.simxStartSimulation(clientID, vrep.simx_opmode_blocking)
print("Simulation start")

根據需要,設置py文件和vrep環境時間一致:

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

相應的,在主程序部分,往往是循環里頭:

# make step forward

vrep.simxSynchronousTrigger(clientID);

4.主程序部分

5.結束仿真

##### Stop simulation
vrep.simxStopSimulation(clientID, vrep.simx_opmode_blocking)
errorCode = vrep.simxSetIntegerSignal(clientID, 'ICECUBE_0', 1, vrep.simx_opmode_blocking)
time.sleep(0.5)

6.斷開連接

##### Close the connection to V-REP
vrep.simxFinish(clientID)
print('Program end')

 

主程序部分:

a.vision sensor傳感器有關

##### obtain the vision sensor handle 

errorCode,visionSensorHandle = vrep.simxGetObjectHandle(clientID,'Cam',vrep.simx_opmode_oneshot_wait)

##### Get the image of vision sensor
errprCode,resolution,image = vrep.simxGetVisionSensorImage(clientID,visionSensorHandle,0,vrep.simx_opmode_streaming)
time.sleep(0.1)
errprCode,resolution,image = vrep.simxGetVisionSensorImage(clientID,visionSensorHandle,0,vrep.simx_opmode_buffer)

##### Process the image to the format (64,64,3)
sensorImage = []
sensorImage = np.array(image,dtype = np.uint8)
sensorImage.resize([resolution[0],resolution[1],3])

##### Use matplotlib.imshow to show the image
mpl.imshow(sensorImage,origin='lower')

 

b.force sensor傳感器有關:

##### simx_opmode_streaming initialization, no values are read at this time
errorCode,state,forceVector,torqueVector=vrep.simxReadForceSensor(clientID,forceSensorHandle,vrep.simx_opmode_streaming)

##### simx_opmode_buffer to obtain forceVector and torqueVector
errorCode,state,forceVector,torqueVector=vrep.simxReadForceSensor(clientID,forceSensorHandle,vrep.simx_opmode_buffer)

# Output the force of XYZ
print(forceVector)
# Output the torque of XYZ
print(torqueVector)

 

c..py文件與vrep之間傳送信號:

##### Sent the signal of movement
vrep.simxPauseCommunication(clientID, 1)

###### obtain signal from vrep

errorCode, signal = vrep.simxGetIntegerSignal(clientID, 'ICECUBE_0', vrep.simx_opmode_blocking)

errorCode = vrep.simxSetIntegerParameter(clientID, vrep.sim_intparam_current_page, 0, vrep.simx_opmode_blocking)

###### send signal to vrep

errorCode = vrep.simxSetIntegerParameter(clientID, vrep.sim_intparam_current_page, 1, vrep.simx_opmode_blocking)

vrep.simxSetFloatSignal(clientID, 'ICECUBE_' + str(i), targetPosition[i - 1], vrep.simx_opmode_oneshot)

#把暫停打開,可以傳輸信號了

vrep.simxPauseCommunication(clientID, 0)

 

d. .py調用vrep的腳本函數:

res,retInts,retFloats,retStrings,retBuffer=vrep.simxCallScriptFunction(clientID,"remoteApiCommandServer",vrep.sim_scripttype_childscript,'executeCode_function',[],[],[code],emptyBuff,vrep.simx_opmode_blocking)
if res==vrep.simx_return_ok:
print ('Code execution returned: ',retStrings[0])
else:
print ('Remote function call failed')

 

e. 跟object物體有關:

# Retrieve some handles:
res,robotHandle=vrep.simxGetObjectHandle(clientID,'IRB4600#',vrep.simx_opmode_oneshot_wait)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM