我需要根据套接字从客户端发送到服务器的命令进行预测。服务器进行预测(TensorFlow神经网络),并将其发送到运行模拟机器人的另一软件。因此,在服务器接收数据的同时进行预测。我创建了一个线程来计算预测,而另一个则发送给机器人软件。我一直在尝试下面的代码,但无法正常工作。
def pioneer():
sensor_val = np.array([])
for x in range(1, 16 + 1):
errorCode, detectionState, detectedPoint, detectedObjectHandle, detectedSurfaceNormalVector = sim.simxReadProximitySensor(
clientID, sensor_h[x - 1], sim.simx_opmode_buffer)
sensor_val = np.append(sensor_val, detectionState) # get list of values
sensor[:, :] = sensor_val.reshape(1, 16)
errorCode = sim.simxSetJointTargetVelocity(clientID, left_motor_handle, pred[:, 0],
sim.simx_opmode_streaming)
errorCode = sim.simxSetJointTargetVelocity(clientID, right_motor_handle, pred[:, 1],
sim.simx_opmode_streaming)
def prediction():
with tf.compat.v1.Session() as sess:
sess.run(init)
loader = tf.compat.v1.train.Saver()
loader.restore(sess, "output_pioneer")
pred = sess.run(Y_hat, feed_dict={X: direction_data})
print(pred)
try:
while True:
print("Aguardando conexao")
con, client = tcp.accept()
print("Conectado por", client)
sim.simxStartSimulation(clientID, sim.simx_opmode_blocking)
t1 = threading.Thread(target=pioneer())
t2 = threading.Thread(target=prediction())
t1.start()
t2.start()
while True:
msg = con.recv(4096)
if not msg: break
direction_data = np.array(pickle.loads(msg)).reshape(1,4)
print(direction_data)
print("Finalizando conexão do cliente", client)
con.close()
sim.simxStopSimulation(clientID, sim.simx_opmode_blocking)
except KeyboardInterrupt:
pass
如果您能帮助我,我将不胜感激。谢谢!