vrep-python 控制方法

Posted vanusy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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之间传送信号:

###### 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)

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

 

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)

 

以上是关于vrep-python 控制方法的主要内容,如果未能解决你的问题,请参考以下文章

python_控制台输出带颜色的文字方法

python_控制台输出带颜色的文字方法

python_控制台输出带颜色的文字方法

python_控制台输出带颜色的文字方法

python通过colorama模块在控制台输出彩色文字的方法

python控制台输出带颜色的文字方法