python如何控制传感器_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)

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值