multiTrafficConeAhead.go 3.5 KB

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