【车路协同】通信方法
局域网UDP发送字符串条件:代码:#!/usr/bin/env python# -*- coding:UTF-8 -*-from socket import *from time import ctimeimport timeHOST = '127.0.0.1'PORT = 21567BUFSIZE = 1024ADDR = (HOST, PORT)udpSerSock = socket(AF_I
·
【局域网wifi+ROS】
网络配置
主机
ifconifg 查看本机ip
bashrc设置
#声明主机 就是ifconfig的主机ip+11311
export ROS_MASTER_URI=http://192.168.1.110:11311
#声明本机 就是ifconfig的本机ip
export ROS_IP=192.168.1.110
#声明各个设备ip
sudo gedit /etc/hosts
末尾加上
192.168.1.101 computer
192.168.1.100 qm-GL552VW
从机1
ifconifg 查看本机ip
bashrc设置
#声明主机
export ROS_MASTER_URI=http://192.168.1.110:11311
#声明本机
export ROS_IP=192.168.1.110
#声明各个设备ip
sudo gedit /etc/hosts
末尾加上
192.168.1.101 computer
192.168.1.100 qm-GL552VW
从机2
等等
启动ROS
主机直接roscore
从机不用动,直接rostopic查看话题
【 UDP 】
1 广播,同一个路由器下所有人都可以听到我的声音,我只复杂说出去,爱听不听
2 多播,同一个路由器下某一组人可以听到我的声音,我只复杂说出去,这个组的人爱听不听
buf最大可以有6000个左右 大概几kb
1 局域网UDP发送字符串(车端 发字符串给 路端)
硬件条件:
- 一个路由器,插电。(无需连接其他东西)
- 所有设备连接它
- 可以一个连wif-5G,一个连wifi-2.4G
代码:
路端(作为服务器,接受车端(客户端)的感知信息)
#!/usr/bin/env python
# -*- coding:UTF-8 -*-
from socket import *
import time
# 设置好ip和端口
HOST = '127.0.0.1'
PORT = 21567
BUFSIZE = 1024
ADDR = (HOST, PORT)
#初始化upd
udpSerSock = socket(AF_INET, SOCK_DGRAM)
udpSerSock.bind(('', PORT))
print('road_server_ini: ')
#循环等待消息
while True:
time.sleep(1)
data, addr = udpSerSock.recvfrom(BUFSIZE)
data.decode('utf-8')
# 时间戳转年月日秒
timeStamp = time.time()
timeArray = time.localtime(timeStamp)
normal_time = time.strftime("%Y--%m--%d %H:%M:%S", timeArray)
print('received: address:' ,addr, ' data: ',data,' time ',normal_time)
udpSerSock.close()
车端
from socket import *
import time
HOST = '<broadcast>'
PORT = 21567
BUFSIZE = 1024
ADDR = (HOST, PORT)
udpCliSock = socket(AF_INET, SOCK_DGRAM)
udpCliSock.bind(('', 0))
udpCliSock.setsockopt(SOL_SOCKET, SO_BROADCAST, 1)
while True:
time.sleep(1)
data = 'i am client'
data=data.encode('utf-8')
if not data:
break
print("sending:" , data)
udpCliSock.sendto(data, ADDR)
## data,ADDR = udpCliSock.recvfrom(BUFSIZE)
## if not data:
## break
## print data
udpCliSock.close()
2 发送一帧图像
客户端(发)
import socket
import numpy as np
import cv2 as cv
addr = ("127.0.0.1", 65534)
buf = 512
width = 640
height = 480
cap = cv.VideoCapture(0)
cap.set(3, width)
cap.set(4, height)
code = 'start'
code = ('start' + (buf - len(code)) * 'a').encode('utf-8')
if __name__ == '__main__':
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
while(cap.isOpened()):
ret, frame = cap.read()
if ret:
s.sendto(code, addr)
data = frame.tostring()
for i in range(0, len(data), buf):
s.sendto(data[i:i+buf], addr)
# cv.imshow('send', frame)
# if cv.waitKey(1) & 0xFF == ord('q'):
# break
else:
break
服务端(收)
import socket
import numpy as np
import cv2 as cv
addr = ("127.0.0.1", 65534)
buf = 512
width = 640
height = 480
code = b'start'
num_of_chunks = width * height * 3 / buf
if __name__ == '__main__':
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind(addr)
while True:
chunks = []
start = False
while len(chunks) < num_of_chunks:
chunk, _ = s.recvfrom(buf)
if start:
chunks.append(chunk)
elif chunk.startswith(code):
start = True
byte_frame = b''.join(chunks)
frame = np.frombuffer(
byte_frame, dtype=np.uint8).reshape(height, width, 3)
cv.imshow('recv', frame)
if cv.waitKey(1) & 0xFF == ord('q'):
break
s.close()
cv.destroyAllWindows()
3 全功能版本
一个服务器,多个客户端,服务器同时接受多个客户端信息,且不干扰
多线程 UDP 服务端
import socket
import threading
import numpy as np
import cv2
import time
class my_rec:
def __init__(self):
self.ip=""
self.port1=65534
self.addr1=(self.ip,self.port1)
self.port2 = 65535
self.addr2 = (self.ip, self.port2)
self.s1 = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.s2 = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.s1.bind(self.addr1)
self.s2.bind(self.addr2)
print("UDP server OK")
def send_once(self,s,str,addr):
data = str.encode('utf-8')
self.s1.sendto(data, addr)
def rec_once(self,s):
data,addr=s.recvfrom(60000)
data=data.decode('utf-8')
print(str(s),' ',data)
def s1_work(self):
while 1:
self.rec_once(s=self.s1)
def s2_work(self):
while 1:
self.rec_once(s=self.s2)
def run(self):
# t1 = threading.Thread(target=run,args=('t1',)) # target是要执行的函数名(不是函数),args是函数对应的参数,以元组的形式存在
t1 = threading.Thread(target=self.s1_work)
t2 = threading.Thread(target=self.s2_work)
t1.start()
print('t1.start')
t2.start()
print('t2.start')
def main():
t=my_rec()
t.run()
if __name__ == '__main__':
main()
一个客户端
import socket
import numpy as np
import cv2
import time
class my_send:
def __init__(self):
self.ip="192.168.0.183"
self.port=65534
self.addr=(self.ip,self.port)
self.s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
def send(self,str):
data=str.encode('utf-8')
self.s.sendto(data,self.addr)
def main():
t=my_send()
while 1:
t.send("woshi")
time.sleep(1)
if __name__ == '__main__':
main()
更多推荐
已为社区贡献1条内容
所有评论(0)