cameradecision.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. import proto.cameraobjectarray_pb2 as cameraobjectarray_pb2
  2. import proto.decitionspeedlimit_pb2 as decitionspeedlimit_pb2
  3. import proto.gpsimu_pb2 as gpsimu_pb2 # 2024.10.14
  4. import math
  5. from typing import List
  6. from datetime import datetime, timedelta
  7. import time
  8. class Point2D:
  9. def __init__(self, x, y,hdg):
  10. self.mx = x
  11. self.my = y
  12. self.mhdg = hdg
  13. while self.mhdg < 0:
  14. self.mhdg = self.mhdg + 2.0* math.pi
  15. while self.mhdg >= 2.0*math.pi:
  16. self.mhdg = self.mhdg - 2.0* math.pi
  17. def __str__(self):
  18. return f"Point2D({self.mx}, {self.my})"
  19. class CameraDecision:
  20. def __init__(self):
  21. # 初始化代码...
  22. self.mendacc = -0.7 #抵达终点时的减速度为-0.7
  23. self.mmaxwheel = 430 #最大方向盘角度
  24. self.mdefaultacc = 1.0 #加速时的acc
  25. self.mspeed = 10.0 #运行速度10km/h
  26. self.mstopdistoobs = 6.0 #在距离障碍物多远停住
  27. self.mstopdisacc = -1.0 #有障碍物时的减速度
  28. self.mvehwidth = 2.3 #从车宽
  29. pass
  30. def CalcDecision(self,xobjarray : cameraobjectarray_pb2.cameraobjectarray,xgpsimu: gpsimu_pb2.gpsimu): # 2024.10.14
  31. realspeed = 3.6 * math.sqrt(math.pow(xgpsimu.vn,2) + math.pow(xgpsimu.ve,2)) #获取当前车速 # 2024.10.14
  32. nobjsize = len(xobjarray.obj)
  33. for pobj in xobjarray.obj:
  34. print(f"type: {pobj.type} x : {pobj.x} y: {pobj.y}"); #获取摄像头感知结果的方法,其它数据可以查看proto文件用pobj.{名称}获取
  35. acc = 0.0 #加速度数值,m/s2 >0 加速 <0 制动
  36. wheel = 0.0 #方向盘转角,>0 左转 <0 右转 -430----430
  37. speed = 0.0 #车速
  38. leftLamp = False #左转向灯
  39. rightLamp = False #右转向灯
  40. ### 在此编写代码给出决策数值
  41. ### 在此编写代码给出决策数值
  42. ### 在此编写代码给出决策数值
  43. xdecisiion = decitionspeedlimit_pb2.decitionspeedlimit()
  44. xdecisiion.wheelAngle = wheel
  45. xdecisiion.accelerator = acc
  46. xdecisiion.brake = 0
  47. xdecisiion.speed = speed
  48. xdecisiion.leftLamp = leftLamp
  49. xdecisiion.rightLamp = rightLamp
  50. if acc < 0:
  51. xdecisiion.brake = acc
  52. xdecisiion.torque = 0
  53. else:
  54. xdecisiion.brake = 0
  55. fVehWeight = 1800
  56. # fg = 9.8
  57. fRollForce = 50
  58. fRatio = 2.5
  59. fNeed = fRollForce + fVehWeight*acc
  60. xdecisiion.torque = fNeed/fRatio
  61. return xdecisiion
  62. def is_point_in_rotated_rectangle(self,x, y, x1, y1, yaw, l, w):
  63. # 将长方形的左下角坐标转换到原点
  64. x_rel = x - x1
  65. y_rel = y - y1
  66. # 计算旋转矩阵(逆时针旋转)
  67. # | cos(yaw) -sin(yaw) |
  68. # | sin(yaw) cos(yaw) |
  69. cos_yaw = math.cos(yaw)
  70. sin_yaw = math.sin(yaw)
  71. # 应用旋转矩阵到相对坐标
  72. x_rotated = x_rel * cos_yaw + y_rel * sin_yaw
  73. y_rotated = -x_rel * sin_yaw + y_rel * cos_yaw
  74. # 判断点是否在旋转后的长方形内
  75. # 长方形的边界在旋转后的坐标系中是 [-l/2, l/2] x [-w/2, w/2]
  76. if -l/2 <= x_rotated <= l/2 and -w/2 <= y_rotated <= w/2:
  77. return True
  78. else:
  79. return False
  80. def GaussProj(self,lon,lat):
  81. iPI = 0.0174532925199433
  82. ZoneWide = 6
  83. a = 6378245.0
  84. f = 1.0 / 298.3
  85. ProjNo = int(lon / ZoneWide)
  86. longitude0 = ProjNo * ZoneWide + ZoneWide / 2
  87. longitude0 = longitude0 * iPI
  88. latitude0 = 0
  89. longitude1 = lon * iPI #经度转换为弧度
  90. latitude1 = lat * iPI #//纬度转换为弧度
  91. e2 = 2 * f - f * f
  92. ee = e2 * (1.0 - e2)
  93. NN = a / math.sqrt(1.0 - e2 * math.sin(latitude1)*math.sin(latitude1))
  94. T = math.tan(latitude1)*math.tan(latitude1)
  95. C = ee * math.cos(latitude1)*math.cos(latitude1)
  96. A = (longitude1 - longitude0)*math.cos(latitude1)
  97. M = a * ((1 - e2 / 4 - 3 * e2*e2 / 64 - 5 * e2*e2*e2 / 256)*latitude1 - (3 * e2 / 8 + 3 * e2*e2 / 32 + 45 * e2*e2*e2 / 1024)*math.sin(2 * latitude1)+ (15 * e2*e2 / 256 + 45 * e2*e2*e2 / 1024)*math.sin(4 * latitude1) - (35 * e2*e2*e2 / 3072)*math.sin(6 * latitude1))
  98. xval = NN * (A + (1 - T + C)*A*A*A / 6 + (5 - 18 * T + T * T + 72 * C - 58 * ee)*A*A*A*A*A / 120)
  99. yval = M + NN * math.tan(latitude1)*(A*A / 2 + (5 - T + 9 * C + 4 * C*C)*A*A*A*A / 24 + (61 - 58 * T + T * T + 600 * C - 330 * ee)*A*A*A*A*A*A / 720)
  100. X0 = 1000000 * (ProjNo + 1) + 500000
  101. Y0 = 0
  102. xval = xval + X0; yval = yval + Y0;
  103. X = xval
  104. Y = yval
  105. return X,Y
  106. def GaussProjInvCal(self,X,Y):
  107. iPI = 0.0174532925199433 #3.1415926535898/180.0;
  108. a = 6378245.0
  109. f = 1.0 / 298.3 # //54年北京坐标系参数
  110. #////a=6378140.0; f=1/298.257; //80年西安坐标系参数
  111. ZoneWide = 6 # ////6度带宽
  112. ProjNo = int(X / 1000000) # //查找带号
  113. longitude0 = (ProjNo - 1) * ZoneWide + ZoneWide / 2
  114. longitude0 = longitude0 * iPI # //中央经线
  115. X0 = ProjNo * 1000000 + 500000
  116. Y0 = 0
  117. xval = X - X0; yval = Y - Y0 #//带内大地坐标
  118. e2 = 2 * f - f * f
  119. e1 = (1.0 - math.sqrt(1 - e2)) / (1.0 + math.sqrt(1 - e2))
  120. ee = e2 / (1 - e2)
  121. M = yval
  122. u = M / (a*(1 - e2 / 4 - 3 * e2*e2 / 64 - 5 * e2*e2*e2 / 256))
  123. fai = u + (3 * e1 / 2 - 27 * e1*e1*e1 / 32)*math.sin(2 * u) + (21 * e1*e1 / 16 - 55 * e1*e1*e1*e1 / 32)*math.sin(4 * u)+ (151 * e1*e1*e1 / 96)*math.sin(6 * u) + (1097 * e1*e1*e1*e1 / 512)*math.sin(8 * u)
  124. C = ee * math.cos(fai)*math.cos(fai)
  125. T = math.tan(fai)*math.tan(fai)
  126. NN = a / math.sqrt(1.0 - e2 * math.sin(fai)*math.sin(fai))
  127. R = a * (1 - e2) / math.sqrt((1 - e2 * math.sin(fai)*math.sin(fai))*(1 - e2 * math.sin(fai)*math.sin(fai))*(1 - e2 * math.sin(fai)*math.sin(fai)))
  128. D = xval / NN
  129. #//计算经度(Longitude) 纬度(Latitude)
  130. longitude1 = longitude0 + (D - (1 + 2 * T + C)*D*D*D / 6 + (5 - 2 * C + 28 * T - 3 * C*C + 8 * ee + 24 * T*T)*D*D*D*D*D / 120) / math.cos(fai)
  131. latitude1 = fai - (NN*math.tan(fai) / R)*(D*D / 2 - (5 + 3 * T + 10 * C - 4 * C*C - 9 * ee)*D*D*D*D / 24 + (61 + 90 * T + 298 * C + 45 * T*T - 256 * ee - 3 * C*C)*D*D*D*D*D*D / 720)
  132. #//转换为度 DD
  133. longitude = longitude1 / iPI
  134. latitude = latitude1 / iPI
  135. return longitude,latitude