代码流程编辑
{
def main():
loop_counter = 0
vehicleControl1 = vehicleControlAPI(0, 0, 0) # 控制初始化
socketServer = SocketServer()
socketServer.socket_connect()
while True:
dataState, apiList = socketServer.socket_launch()
if dataState:
if apiList != None:
if loop_counter % sceneInfoOutputGap == 1:
print("\n\nInfo begin:")
apiList.showAllState()
print("gear mode: ", vehicleControl1.gear)
if dataState and loop_counter == 0:
socketServer.socket_respond()
elif dataState and apiList != None and apiList.messageState() and loop_counter != 0:
roadLine = RoadLine()
roadLineList = roadLine.trajvisualization(apiList.trajListAPI())
vehicleControl1.__pathVisualizationSet__(roadLineList[1], roadLineList[2],
roadLineList[1], roadLineList[2])
control_dict_demo = algorithm(apiList, vehicleControl1)
control_dict_demo += "|end"
socketServer.socket_send(control_dict_demo)
loop_counter += 1
}
初始化
控制指令初始化
socketServer = SocketServer() 创建socketServer 类对象
socketServer.socket_connect() 建立连接
{
def socket_connect(self):
# 建立websocket连接
try:
x = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
x.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
x.bind(("127.0.0.1", 5061))
x.listen(10)
self.__conn, address = x.accept()
except socket.error as msg:
print(msg)
sys.exit(1)
return self.__conn, address
}
创建了socketServer类。类有属性conn
循环0
dataState, apiList = socketServer.socket_launch()
{
def socket_launch(self):
result = self.__conn.recv(4096 * 500)
dataList = result.split(b"|end") # 使用|end作为分隔符
data = dataList[0]
data_json = json.loads(data)
apiList = None
dataState = False
if data != None:
dataState = True
print(data_json)
if data_json['SimCarMsg'] != None:
apiList = APIList(data_json['SimCarMsg'])
return dataState, apiList
}
如果成果收到数据,dataState为True,apiList = APIList(data_json[‘SimCarMsg’])
if dataState and loop_counter == 0:
socketServer.socket_respond()
第一轮通信返回空包
def socket_respond(self):
self.__conn.send(bytes('{"code":2,"UserInfo":null,"SimCarMsg":null, "messager":""}|end',
encoding="utf-8"))
loop_counter +=1
此时进入循环1
dataState, apiList = socketServer.socket_launch()
接收数据包,dataState为True,apiList = APIList(data_json[‘SimCarMsg’])
{
if dataState:
if apiList != None:
if loop_counter % sceneInfoOutputGap == 1:
print("\n\nInfo begin:")
apiList.showAllState()
print("gear mode: ", vehicleControl1.gear)
}
这里为什么不改成dataState和apiList 的判断
在config里sceneInfoOutputGap = 2,是obstacle信息输出间隔
循环零不通过此处。
def showAllState(self):
print("DataGnssAPI", self.DataGnssAPI())
print("DataMainVehilceAPI", self.DataMainVehilceAPI())
print("ObstacleEntryListAPI", self.ObstacleEntryListAPI())
print("RoadLineListAPI", self.RoadLineListAPI())
print("TrafficLightListAPI", self.TrafficLightListAPI())
print("trajListAPI", self.trajListAPI())
第一轮开始,发送带有数据的包
elif dataState and apiList != None and apiList.messageState() and loop_counter != 0:
roadLine = RoadLine()
roadLineList = roadLine.trajvisualization(apiList.trajListAPI())
vehicleControl1.__pathVisualizationSet__(roadLineList[1], roadLineList[2],
roadLineList[1], roadLineList[2])
control_dict_demo = algorithm(apiList, vehicleControl1)
control_dict_demo += "|end"
socketServer.socket_send(control_dict_demo)
def trajListAPI(self):
return self.__trajList
self.__trajList = data['Trajectory']
apiList.tragListAPI的内容
'Trajectory': {'trajectorySize': 20, 'trajectory': [{'P': {'x': 0.0, 'y': 100.0, 'z': 0.0}, 'V': None}, {'P': {'x': -0.09, 'y': 98.97, 'z': 0.0}, 'V': None}, {'P': {'x': -0.19, 'y': 97.96, 'z': 0.0}, 'V': None}, {'P': {'x': -0.28, 'y': 96.98, 'z': 0.0}, 'V': None}, {'P': {'x': -0.37, 'y': 95.99, 'z': 0.0}, 'V': None}, {'P': {'x': -0.46, 'y': 94.98, 'z': 0.0}, 'V': None}, {'P': {'x': -0.55, 'y': 93.99, 'z': 0.0}, 'V': None}, {'P': {'x': -0.64, 'y': 93.02, 'z': 0.0}, 'V': None}, {'P': {'x': -0.73, 'y': 92.0, 'z': 0.0}, 'V': None}, {'P': {'x': -0.82, 'y': 91.0, 'z': 0.0}, 'V': None}, {'P': {'x': -0.92, 'y': 90.01, 'z': 0.0}, 'V': None}, {'P': {'x': -1.01, 'y': 89.02, 'z': 0.0}, 'V': None}, {'P': {'x': -1.1, 'y': 88.02, 'z': 0.0}, 'V': None}, {'P': {'x': -1.19, 'y': 87.02, 'z': 0.0}, 'V': None}, {'P': {'x': -1.28, 'y': 86.03, 'z': 0.0}, 'V': None}, {'P': {'x': -1.37, 'y': 85.04, 'z': 0.0}, 'V': None}, {'P': {'x': -1.46, 'y': 84.06, 'z': 0.0}, 'V': None}, {'P': {'x': -1.55, 'y': 83.05, 'z': 0.0}, 'V': None}, {'P': {'x': -7.9, 'y': 30.53, 'z': 0.0}, 'V': None}, {'P': {'x': -8.0, 'y': 30.0, 'z': 0.0}, 'V': None}]}
class RoadLine:
def __init__(self):
self.RoadLineLen = 0
self.RoadLineList = []
def trajvisualization(self, RoadLineData):
self.RoadLineLen = len(RoadLineData) # 道路参考线列表长度
print("Roadlinedata", RoadLineData)
roadList_xList = [] # 所有道路参考线点x坐标
roadList_yList = [] # 所有道路参考线点y坐标
roadList_zList = [] # 所有道路参考线点z坐标
for i in range(self.RoadLineLen):
roadList_x = []
roadList_y = []
roadList_z = []
self.RoadLineList.append(RoadLineData[i])
for j in range(len(RoadLineData[i]['PointPath'])):
roadList_x.append(RoadLineData[i]['PointPath'][j]['x'])
roadList_y.append(RoadLineData[i]['PointPath'][j]['y'])
roadList_z.append(RoadLineData[i]['PointPath'][j]['z'])
roadList_xList.append(roadList_x)
roadList_yList.append(roadList_y)
roadList_zList.append(roadList_z)
print("roadList_xList",roadList_xList)
print("roadList_yList",roadList_yList)
print("roadList_zList",roadList_zList)
return [roadList_xList, roadList_yList, roadList_zList]
def trajvisualization(self, Trajectory):
trajLen = Trajectory['trajectorySize']
print("Trajectory", Trajectory)
trajList_xList = [] # 所有道路参考线点x坐标
trajList_yList = [] # 所有道路参考线点y坐标
trajList_zList = [] # 所有道路参考线点z坐标
for i in range(trajLen):
trajList_xList.append(Trajectory['trajectory'][i]['P']['x'])
trajList_yList.append(Trajectory['trajectory'][i]['P']['y'])
trajList_zList.append(Trajectory['trajectory'][i]['P']['z'])
print("trajList_xList", trajList_xList)
print("trajList_yList", trajList_yList)
print("trajList_zList", trajList_zList)
return [trajLen, trajList_xList, trajList_yList, trajList_zList]
def __trajListAppend(self, singleData):
self.RoadLineList.append(singleData)
def __trajClear(self):
self.RoadLineList.clear()
roadLine是类对象
roadLineList 返回了车道线 的xyz坐标
对于 vehicleControl1.__pathVisualizationSet__(roadLineList[1], roadLineList[2],
roadLineList[1], roadLineList[2])
def __pathVisualizationSet__(self, pathlistx, pathlisty, trajlistx, trajlisty):
self.pathlistx = pathlistx
self.pathlisty = pathlisty
self.timeCurveListx = trajlistx
self.timeCurveListy = trajlisty
这一步向demo里面输入了车道线/轨迹线数据
算法
control_dict_demo = algorithm(apiList, vehicleControl1)
def algorithm(apiList, vehicleControl1):
control_velocity = 0
control_steer = 0
# 自动驾驶模式
# pos_x, pos_y, pos_yaw, pose_v = LqrController.lqrControl(4, [curPose['posX']], [curPose['posY']])
# for i in range(len(pos_x)): # 判断路径列表中与当前位置最近的元素
# if i < len(pos_x) - 1: # 防止溢出
# if math.sqrt(math.pow(pos_x[i] - curPose['posX'] ,2) +
# math.pow(pos_y[i] - curPose['posY'], 2)) < \
# math.sqrt(math.pow(pos_x[i + 1] - curPose['posX'] , 2) +
# math.pow((pos_y[i + 1] -curPose['posY']), 2)):
# control_steer = pos_yaw[i]
# control_brake = pose_v[i]
# else: # 最后一位
# control_steer = pos_yaw[-1]
# control_brake = pose_v[-1]
# vehicleControl1.__steeringSet__(control_steer, yaw=curPose['oriZ'])
# vehicleControl1.__throttleSet__(control_brake, speed=math.sqrt(
# math.pow(curPose['posX'], 2) + math.pow(curPose['posY'], 2)))
# vehicleControl1.__brakeSet__(control_brake, speed=math.sqrt(
# math.pow(curPose['posX'], 2) + math.pow(curPose['posY'], 2)))
# 手操模式
vehicleControl1.__keyboardControl__()
control_dict_demo = json_encoder(vehicleControl1)
control_dict_demo = json.dumps(control_dict_demo)
return control_dict_demo
先分析手操模式
json.encoder 将vehicleControl 里的数据填入control_dict_demo 中,是字典
json.dumps 用于将 Python 对象,此处为demo字典 编码成 JSON 字符串
自动驾驶模式
pos_x, pos_y, pos_yaw, pose_v = LqrController.lqrControl(4, [curPose['posX']], [curPose['posY']])
根据当前位置算出路径的位置,转向,速度
找到路径中的最近点
转向= 预期角度和当前角度之差
油门/刹车=预期速度和当前速度之差(当前速度为何是两点之间的位移)
所以我们只需要传入油门刹车转向
车辆切出
算法控制的数据包
{
"E:\work_Miao\python project\pythonProject\.venv\Scripts\python.exe" "E:\work_Miao\python project\pythonProject\autodrive_demo_user\main.py"
E:\work_Miao\python project\pythonProject\autodrive_demo_user
{'code': 1, 'UserInfo': {'sceneName': 'scene_128', 'userName': 'scene112'}, 'SimCarMsg': {'Simdata': {'timestamp': 1725715589, 'queue': []}, 'VehicleControl': None, 'Trajectory': None, 'DataGnss': None, 'DataMainVehilce': None, 'VehicleSignalLight': None, 'ObstacleEntryList': None, 'TrafficLightList': None, 'RoadLineList': None, 'MapInfo': {'path': 'C:/MyLab/Scene/a6ea65fb-4090-418e-9c51-191b4f75e468/SmartCar_Data/StreamingAssets/mapfile', 'route': 'route', 'tpoints': 'tpoint', 'rd': 'rd', 'TileDataList': []}, 'GradingInfo': None, 'DashboardMsgTY': None, 'DashboardMsgXY': None}, 'messager': ''}
{'code': 3, 'UserInfo': None, 'SimCarMsg': {'Simdata': {'timestamp': 1725715590, 'queue': [{'timestamp': 1725715589, 'data': '(0,100,0)'}]}, 'VehicleControl': {'throttle': 0.0, 'brake': 0.0, 'steering': 0.0, 'handbrake': False, 'isManualGear': False, 'gear': 3, 'movetostart': 0, 'movetoend': 0}, 'Trajectory': {'trajectorySize': 20, 'trajectory': [{'P': {'x': 0.0, 'y': 100.0, 'z': 0.0}, 'V': None}, {'P': {'x': -0.09, 'y': 98.97, 'z': 0.0}, 'V': None}, {'P': {'x': -0.19, 'y': 97.96, 'z': 0.0}, 'V': None}, {'P': {'x': -0.28, 'y': 96.98, 'z': 0.0}, 'V': None}, {'P': {'x': -0.37, 'y': 95.99, 'z': 0.0}, 'V': None}, {'P': {'x': -0.46, 'y': 94.98, 'z': 0.0}, 'V': None}, {'P': {'x': -0.55, 'y': 93.99, 'z': 0.0}, 'V': None}, {'P': {'x': -0.64, 'y': 93.02, 'z': 0.0}, 'V': None}, {'P': {'x': -0.73, 'y': 92.0, 'z': 0.0}, 'V': None}, {'P': {'x': -0.82, 'y': 91.0, 'z': 0.0}, 'V': None}, {'P': {'x': -0.92, 'y': 90.01, 'z': 0.0}, 'V': None}, {'P': {'x': -1.01, 'y': 89.02, 'z': 0.0}, 'V': None}, {'P': {'x': -1.1, 'y': 88.02, 'z': 0.0}, 'V': None}, {'P': {'x': -1.19, 'y': 87.02, 'z': 0.0}, 'V': None}, {'P': {'x': -1.28, 'y': 86.03, 'z': 0.0}, 'V': None}, {'P': {'x': -1.37, 'y': 85.04, 'z': 0.0}, 'V': None}, {'P': {'x': -1.46, 'y': 84.06, 'z': 0.0}, 'V': None}, {'P': {'x': -1.55, 'y': 83.05, 'z': 0.0}, 'V': None}, {'P': {'x': -7.9, 'y': 30.53, 'z': 0.0}, 'V': None}, {'P': {'x': -8.0, 'y': 30.0, 'z': 0.0}, 'V': None}]}, 'DataGnss': {'poseGnss': {'posX': 0.0, 'posY': 99.833, 'posZ': -0.007, 'velX': 16.67, 'velY': 0.0, 'velZ': -0.24, 'oriX': 0.0, 'oriY': -0.002, 'oriZ': 90.0}}, 'DataMainVehilce': {'mainVehicleId': 0, 'speed': 16.67, 'gear': 3, 'throttle': 0.0, 'brake': 0.0, 'steering': 0.0, 'length': 4.814, 'width': 2.18, 'height': 1.908}, 'VehicleSignalLight': {'Signal_Light_RightBlinker': 0, 'Signal_Light_LeftBlinker': 0, 'Signal_Light_DoubleFlash': 0, 'Signal_Light_BrakeLight': 0, 'Signal_Light_FrontLight': 0, 'Signal_Light_HighBeam': 0, 'Signal_Light_BackDrive': 0}, 'ObstacleEntryList': [{'id': -1, 'viewId': -1, 'type': 7, 'posX': 0.0, 'posY': 43.6, 'posZ': 0.0, 'oriX': 0.0, 'oriY': 0.0, 'oriZ': 270.0, 'velX': 0.0, 'velY': 0.0, 'velZ': 0.0, 'length': 0.26, 'width': 0.26, 'height': 0.45, 'RedundantValue': ''}], 'TrafficLightList': [], 'RoadLineList': [], 'MapInfo': {'path': 'C:/MyLab/Scene/a6ea65fb-4090-418e-9c51-191b4f75e468/SmartCar_Data/StreamingAssets/mapfile', 'route': 'route', 'tpoints': 'tpoint', 'rd': 'rd', 'TileDataList': []}, 'GradingInfo': {'sceneid': 1, 'scenetype': '128', 'targetpoint': {'x': -8.0, 'y': 30.0, 'z': 0.0}, 'usetime': 0.02, 'totaltime': 100.0}, 'DashboardMsgTY': {'x': [1.0, 2.0, 3.0, 4.0], 'y': [1.0, 2.0, 6.0, 5.0]}, 'DashboardMsgXY': {'x': [1.0, 2.0, 3.0], 'y': [1.0, 2.0, 3.0]}}, 'messager': ''}
}