学习新软件的最快捷的方法,就是找例程,与自己需要接近,能跑通、并且理解例程,做好了这一点,速度就会很快。下面我给大家提供了V-rep机器人仿真软件的一些例子(python远程控制),帮助大家学习如何用python远程控制v-rep端。
下面先以巡线小车为例详细讲解
循迹小车仿真的代码我存到网盘里了,大家可以免费下载~ 而且还包含避障小车的代码哟~
链接:https://pan.baidu.com/s/168-E330894ddO6q0xYCJEQ
提取码:fie6
1、准备工作
1.软件下载
V-rep官方下载
Python3下载(本文用的是Spyder)
2.准备以下文件,并将其与你的python程序和Vrep文件(.ttt文件)放到同一目录下
- remoteApi.dll
- vrep.py
- vrepConst.py
2、V-rep模型建立
模型的建立需要自己入门学习,V-rep帮助文档:https://www.coppeliarobotics.com/helpFiles/
后面会提供本例的源代码
3、python端程序详解
解释中相应的语句与函数会在注释中进行解释,部分函数用法参考python远程控制文档:legacy remote API
1,导入所需要用的库
import vrep
import sys
import time
2,python端与V-rep建立连接
vrep.simxFinish(-1) # 闭所有与VREP之前的连接
clientID = vrep.simxStart('127.0.0.1',19997,True,True,5000,5) # 参数解释可查看API文档
if clientID!=-1: #检查连接是否成功
print ('Connected to remote API server')
else:
print ('Connection not successful')
sys.exit('Could not connect')
3,获取车轮电机的操纵
errorCode,left_motor_handle=vrep.simxGetObjectHandle(clientID,"left_joint",vrep.simx_opmode_oneshot_wait) # 获取操纵的函数,参数与返回值参考API文档
print('left:',left_motor_handle) # 返回int数值,不为0时获取成功
errorCode,right_motor_handle=vrep.simxGetObjectHandle(clientID,"right_joint",vrep.simx_opmode_oneshot_wait)
print('right:',right_motor_handle)
4,获取视觉传感器的操纵列表
sensor_h=[] #handles list
sensor_val=[] #Sensor value list
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) # 获取每个传感器读取得到的数值,sensorreadingvalue是一个二维数组,其他参数和返回值参考API
sensor_val.append(1.0) #添加1.0来填充列表上的传感器值。在while循环中,它会覆盖这些值
time.sleep(1)
t = time.time() #节省时间
5,开始循迹
while (1): #Cycle which doesn't end
#It is writing down sensor handles and reading values
summa = 0 #It is zeroing the sum
andur = 0 #and the sensor values
for x in range(0,6):
errorCode,detectionstate, sensorreadingvalue=vrep.simxReadVisionSensor(clientID,sensor_h[x],vrep.simx_opmode_buffer)
#Reading sensor values
print('errorCode:',errorCode) # 返回值为0 函数运行良好
sensor_val[x]=sensorreadingvalue[1][0] #It is overwriting the sensor values
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) #calculating delay time
print ("viivitus on: ", viivitus)
t = time.time() #Taking new time moment
代码讲解完了,大家仔细看代码很容易看懂,有不懂的函数可以到API文档中查看,讲的更详细哦~