Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用python中的TCP/IP协议创建与其他软件的Webots接口_Python_Sockets_Webots - Fatal编程技术网

使用python中的TCP/IP协议创建与其他软件的Webots接口

使用python中的TCP/IP协议创建与其他软件的Webots接口,python,sockets,webots,Python,Sockets,Webots,Webots允许我通过TCP/IP协议将我的机器人与其他软件连接起来。它已经有了一个实现的示例,但是所有的代码都是C语言的。我想用socket实现python 控制器需要检查客户机是否发送了任何命令,因此我在主循环中放置了一个msg=con.recv(1024),但问题是在循环的每次交互中,它都停止等待接收消息。如何使套接字不锁定循环,但仍然能够捕获任何传入消息 这是C服务器实现的链接: 这就是我的问题所在: import socket from controller import Robot

Webots允许我通过TCP/IP协议将我的机器人与其他软件连接起来。它已经有了一个实现的示例,但是所有的代码都是C语言的。我想用socket实现python

控制器需要检查客户机是否发送了任何命令,因此我在主循环中放置了一个
msg=con.recv(1024)
,但问题是在循环的每次交互中,它都停止等待接收消息。如何使套接字不锁定循环,但仍然能够捕获任何传入消息

这是C服务器实现的链接:

这就是我的问题所在:

import socket
from controller import Robot

robot = Robot()

# get the time step of the current world
timestep = 250
max_speed = 6.28
                
# Enable motors    
left_motor = robot.getMotor('left wheel motor')
right_motor = robot.getMotor('right wheel motor')
            
left_motor.setPosition(float('inf'))
left_motor.setVelocity(0.0)
            
right_motor.setPosition(float('inf'))
right_motor.setVelocity(0.0)

right_speed = 0.0
left_speed = 0.0

HOST = '10.0.0.6'             
PORT = 10020           
tcp = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
orig = (HOST, PORT)
tcp.bind(orig)
tcp.listen(1)

con, cliente = tcp.accept()
print ('Connected by ', cliente)

# Main loop:
# - perform simulation steps until Webots is stopping the controller
while robot.step(timestep) != -1:
    msg = con.recv(1024)
    txt = msg.decode('UTF-8')

    if txt[0] == "D":      
    
        if float(txt[2]) < max_speed:
            right_motor.setVelocity(max_speed)
        else:
            print("Very high speed")        
    
        if float(txt[4]) < max_speed:
            left_motor.setVelocity(max_speed)
        else:
            print("Very high speed ")
            
        
print ('Ending client connection ', cliente)
con.close()
导入套接字
从控制器导入机器人
机器人
#获取当前世界的时间步长
时间步长=250
最大速度=6.28
#启用马达
左电机=robot.getMotor(“左轮电机”)
右电机=robot.getMotor('右轮电机')
左电机设置位置(浮动('inf'))
左_电机设定速度(0.0)
右电机设置位置(浮动('inf'))
右_电机设定速度(0.0)
右侧速度=0.0
左前速度=0.0
主机='10.0.0.6'
端口=10020
tcp=socket.socket(socket.AF\u INET,socket.SOCK\u流)
orig=(主机、端口)
tcp.bind(orig)
tcp.listen(1)
con,cliente=tcp.accept()
打印('连接人',客户)
#主回路:
#-执行模拟步骤,直到Webots停止控制器
而机器人。步(时间步)!=-1:
msg=con.recv(1024)
txt=msg.decode('UTF-8')
如果txt[0]=“D”:
如果浮动(txt[2])<最大速度:
右电机设定速度(最大速度)
其他:
打印(“非常高速”)
如果浮动(txt[4])<最大速度:
左电机设定速度(最大速度)
其他:
打印(“非常高速”)
打印('结束客户端连接',客户机)
con.close()
您可以设置超时:

tcp.settimeout(0.1)
然后,在while循环中:

试试看:
msg=s.recv(1024)
除socket.timeout外,e:
持续
(而不是
msg=con.recv(1024)

使用给定的实现,控制器将等待0.1秒以获取数据。如果在0.1s内没有收到任何数据,它将跳过并执行模拟步骤