VREP循迹小车
蓝色部分是视觉传感器
黑色是路径
代码
errorCode,left_motor_handle=vrep.simxGetObjectHandle(clientID,"left_joint",vrep.simx_opmode_oneshot_wait)
errorCode,right_motor_handle=vrep.simxGetObjectHandle(clientID,"right_joint",vrep.simx_opmode_oneshot_wait)
sensor_h=[]
sensor_val=[]
for x in range(0,6):
errorCode,sensor_handle=vrep.simxGetObjectHandle(clientID,'line_sensor'+str(x),vrep.simx_opmode_oneshot_wait)
sensor_h.append(sensor_handle)
errorCode,detectionstate, sensorreadingvalue=vrep.simxReadVisionSensor(clientID,sensor_h[x],vrep.simx_opmode_streaming)
sensor_val.append(1.0)
time.sleep(1)
t = time.time()
while (1):
summa = 0
andur = 0
for x in range(0,6):
errorCode,detectionstate, sensorreadingvalue=vrep.simxReadVisionSensor(clientID,sensor_h[x],vrep.simx_opmode_buffer)
sensor_val[x]=sensorreadingvalue[1][0]
print "Positsiooni väärtus kokku45 :",sensor_val[x] ,x
if sensor_val[2]<=0.2 or sensor_val[3]<=0.2:
errorCode=vrep.simxSetJointTargetVelocity(clientID,left_motor_handle,10, vrep.simx_opmode_streaming)
errorCode=vrep.simxSetJointTargetVelocity(clientID,right_motor_handle,10, vrep.simx_opmode_streaming)
if sensor_val[1]<=0.2:
errorCode=vrep.simxSetJointTargetVelocity(clientID,left_motor_handle,13, vrep.simx_opmode_streaming)
errorCode=vrep.simxSetJointTargetVelocity(clientID,right_motor_handle,10, vrep.simx_opmode_streaming)
if sensor_val[0]<=0.2:
errorCode=vrep.simxSetJointTargetVelocity(clientID,left_motor_handle,16, vrep.simx_opmode_streaming)
errorCode=vrep.simxSetJointTargetVelocity(clientID,right_motor_handle,10, vrep.simx_opmode_streaming)
if sensor_val[4]<=0.2:
errorCode=vrep.simxSetJointTargetVelocity(clientID,left_motor_handle,10, vrep.simx_opmode_streaming)
errorCode=vrep.simxSetJointTargetVelocity(clientID,right_motor_handle,13, vrep.simx_opmode_streaming)
if sensor_val[5]<=0.2:
errorCode=vrep.simxSetJointTargetVelocity(clientID,left_motor_handle,10, vrep.simx_opmode_streaming)
errorCode=vrep.simxSetJointTargetVelocity(clientID,right_motor_handle,16, vrep.simx_opmode_streaming)
viivitus = round((time.time()-t),5)
print "viivitus on: ", viivitus
t = time.time()
原理解释: