lowSpdTruckAhead.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package main
  2. import (
  3. "cicv-data-closedloop/pjisuv_msgs"
  4. "fmt"
  5. "math"
  6. "sync"
  7. )
  8. type Object struct {
  9. ID uint32
  10. StableAge int
  11. LastExistTime float64
  12. }
  13. var objectsStability = make(map[uint32]Object)
  14. func Topic() string {
  15. return "/tpperception"
  16. }
  17. // 禁止存在下划线_
  18. func Label() string {
  19. return "LowSpdTruckAhead"
  20. }
  21. // objSpeedCheck 目标物速度检测
  22. func objSpeedCheck(obj *pjisuv_msgs.PerceptionObject) bool {
  23. const targetMinSpeed = 2 / 3.6 // m/s
  24. const targetMaxSpeed = 20 / 3.6 // m/s
  25. return targetMinSpeed < obj.Speed && obj.Speed < targetMaxSpeed
  26. }
  27. // objTypeCheck 目标物类型检测
  28. // × 金龙车:CAR_TYPE=0, TRUCK_TYPE=1, PEDESTRIAN_TYPE=2, CYCLIST_TYPE=3, UNKNOWN_TYPE=4, UNKNOWN_MOVABLE_TYPE=5, UNKNOWN_UNMOVABLE_TYPE=6
  29. // √ 多功能车:UNKNOWN TYPE=O, PEDESTRIAN TYPE=1, CAR TYPE=2, TRUCK TYPE=3, Bicycle TYPE=4, Tricycle TYPE=5, Traffic Cone TYPE=6
  30. func objTypeCheck(obj *pjisuv_msgs.PerceptionObject) bool {
  31. const targetType uint8 = 3
  32. return targetType == obj.Type
  33. }
  34. // objPosCheck 判断目标物位置关系
  35. func objPosCheck(obj *pjisuv_msgs.PerceptionObject) bool {
  36. const laneWidth = 3.5 // m
  37. return obj.X > 0 && math.Abs(float64(obj.Y)) < laneWidth*1.5
  38. }
  39. // isEgoStationary 判断主车是否“静止”(速度低于阈值)
  40. func isEgoStationary(shareVars *sync.Map) bool {
  41. const minSpeed = 1 // m/s
  42. VelocityXOfCicvLocation, _ := shareVars.Load("VelocityXOfCicvLocation") // float64
  43. VelocityYOfCicvLocation, _ := shareVars.Load("VelocityYOfCicvLocation")
  44. speed := math.Sqrt(math.Pow(VelocityXOfCicvLocation.(float64), 2) + math.Pow(VelocityYOfCicvLocation.(float64), 2))
  45. return speed > minSpeed
  46. }
  47. // Rule 检测前方大车低速行驶
  48. func Rule(shareVars *sync.Map, msg *pjisuv_msgs.PerceptionObjects) string {
  49. defer func() {
  50. if r := recover(); r != nil {
  51. fmt.Println(fmt.Sprintf("Recovered from panic: [%s], [%#v]", Label(), r))
  52. }
  53. }()
  54. // 判断主车所在位置
  55. outsideWorkshopFlag, _ := shareVars.Load("OutsideWorkshopFlag")
  56. if !outsideWorkshopFlag.(bool) {
  57. return ""
  58. }
  59. // 判断主车行驶速度
  60. if isEgoStationary(shareVars) {
  61. return ""
  62. }
  63. //const Cgcs2000X = 456256.260152
  64. //const Cgcs2000Y = 4397809.886833
  65. const MinStableFrameCount = 5
  66. const MaxTimeBetweenFrame = 0.5
  67. //fmt.Println("\n", time.Unix(int64(msg.Header.TimeStamp), int64(msg.Header.TimeStamp*1e9)%1e9).Format(time.StampNano))
  68. for _, obj := range msg.Objs {
  69. // 判断目标物是否满足筛选条件
  70. if objSpeedCheck(&obj) && objPosCheck(&obj) && objTypeCheck(&obj) {
  71. //fmt.Println(fmt.Sprintf("id: [%d], type: [%d], x/yrel: [%f, %f], x/yabs: [%f, %f], speed: [%f], size: [%f/%f/%f]",
  72. // obj.Id, obj.Type, obj.X, obj.Y, obj.Xabs-Cgcs2000X, obj.Yabs-Cgcs2000Y, obj.Speed, obj.Length, obj.Width, obj.Height))
  73. // 目标物初见存档
  74. if prevObjRecord, exists := objectsStability[obj.Id]; exists == false {
  75. objectsStability[obj.Id] = Object{
  76. ID: obj.Id,
  77. StableAge: 1,
  78. LastExistTime: msg.Header.TimeStamp,
  79. }
  80. //fmt.Println("---------- create ----------")
  81. } else {
  82. currObjRecord := Object{
  83. ID: obj.Id,
  84. StableAge: 1,
  85. LastExistTime: msg.Header.TimeStamp,
  86. }
  87. // 目标物确认/更新/重置
  88. if currObjRecord.LastExistTime-prevObjRecord.LastExistTime <= MaxTimeBetweenFrame { // 目标物消失不超过0.5s
  89. currObjRecord.StableAge = prevObjRecord.StableAge + 1
  90. if currObjRecord.StableAge == MinStableFrameCount {
  91. delete(objectsStability, obj.Id)
  92. //fmt.Println("!!!!!!!!!! found !!!!!!!!!!")
  93. return Label()
  94. } else {
  95. //fmt.Println("---------- update ----------")
  96. }
  97. } else {
  98. //fmt.Println("---------- reset ----------")
  99. }
  100. objectsStability[obj.Id] = currObjRecord
  101. }
  102. }
  103. }
  104. return ""
  105. }