欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

V-rep 和 Python 的同步问题

程序员文章站 2024-02-10 09:19:40
...

我按照以下代码执行程序,目的是测试两边程序到底同步与否。vrep端已经提前运行,并在主函数里面加入simRemoteApi.start(19999)。
python端代码如下,会先运行一下,然后完全暂停,然后再启动后,记录10张图片后停止。vrep端,我在非线程脚本里写了一个循环函数。

# Make sure to have the server side running in CoppeliaSim:
# in a child script of a CoppeliaSim scene, add following command
# to be executed just once, at simulation start:
#
# simRemoteApi.start(19999)
#
# then start simulation, and run this program.
#
# IMPORTANT: for each successful call to simxStart, there
# should be a corresponding call to simxFinish at the end!

try:
    import sim
except:
    print('--------------------------------------------------------------')
    print('"sim.py" could not be imported. This means very probably that')
    print('either "sim.py" or the remoteApi library could not be found.')
    print('Make sure both are in the same folder as this file,')
    print('or appropriately adjust the file "sim.py"')
    print('--------------------------------------------------------------')
    print('')

import time
from epuck import epuck
import math
import matplotlib.pyplot as plt
import numpy as np
import cv2

Max_Speed = 120 * math.pi / 180
print('Program started')
sim.simxFinish(-1)  # just in case, close all opened connections
clientID = sim.simxStart('127.0.0.1', 19999, True, True, 5000, 5)  # Connect to CoppeliaSim
if clientID != -1:
    print('Connected to remote API server')
    robot = epuck(clientID)
# 然后打开同步模式
sim.simxSynchronous(clientID, True)
res = sim.simxStartSimulation(clientID, sim.simx_opmode_oneshot);
sim.simxSynchronousTrigger(clientID)  # 让仿真走一步
res = sim.simxPauseSimulation(clientID, sim.simx_opmode_blocking);
sim.simxSynchronousTrigger(clientID)  # 让仿真走一步
res = sim.simxStartSimulation(clientID, sim.simx_opmode_oneshot);
i = 0;
file_path = ''
while clientID != -1:
    if i < 10:
        i += 1
    else:
        break
    file_path = i.__str__() + '.png'
    # 获取机器人位置
    res, robot.pos = sim.simxGetObjectPosition(clientID, robot.self, -1, sim.simx_opmode_buffer)
    c = 1 - robot.pos[1]
    res = sim.simxSetJointTargetVelocity(clientID, robot.left_joint, c, sim.simx_opmode_oneshot)
    res = sim.simxSetJointTargetVelocity(clientID, robot.right_joint, c, sim.simx_opmode_oneshot)
    # clientID,robot.vision_sensor_handle,0表示RGB数据,第一次用simx_opmode_streaming,第二次之后simx_opmode_buffer
    res, resolution, image = sim.simxGetVisionSensorImage(clientID, robot.vision_sensor_handle, 0,
                                                          sim.simx_opmode_buffer)
    # image processing
    # 注:vrep返回的RGB数据大概是从-128-127的
    image = np.asarray(image)
    image = image.reshape(resolution[0], resolution[1], 3)

    image = np.asarray(image, dtype='float64') / 255
    image[image < 0] += 1
    image *=255
    image = np.fliplr(image)
    image = image.astype(np.uint8)
    # 二值化
    # image[image > 0] = 1
    cv2.imshow('path', image)
    cv2.imwrite(file_path, image)
    cv2.waitKey(1)
    # imshow rgb data [0-1] for floats or [0-255] for integers
    # sim.simxGetPingTime(clientID)  # 使得该仿真步走完
    sim.simxSynchronousTrigger(clientID)  # 进行下一步

sim.simxStopSimulation(clientID, sim.simx_opmode_blocking);
sim.simxFinish(clientID)
print('Program ended')

运行结果是,当python端记录10张图片时,vrep实际循环在20-40不等,具体看startSimulation的模式。

Simulation suspended.
Simulation resumed.
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
simulation stopping...
278
Simulation stopped.

解决方案:在vrep端开启实时模式
V-rep 和 Python 的同步问题
v-rep端测试:

Simulation suspended.
Simulation resumed.
103
104
105
106
107
108
109
110
111
112
113
simulation stopping...
114
Simulation stopped.
相关标签: vrep python