lowSpdTruckAhead.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package main
  2. import (
  3. "cicv-data-closedloop/pjisuv_msgs"
  4. "fmt"
  5. "math"
  6. )
  7. type Object struct {
  8. ID uint32
  9. StableAge int
  10. LastExistTime float64
  11. }
  12. var objectsStability = make(map[uint32]Object)
  13. func Topic() string {
  14. return "/tpperception"
  15. }
  16. // Label todo 禁止存在下划线_
  17. func Label() string {
  18. return "LowSpdTruckAhead"
  19. }
  20. // speedCheck 目标物速度检测
  21. func speedCheck(obj *pjisuv_msgs.PerceptionObject) bool {
  22. const targetMinSpeed = 2 / 3.6 // m/s
  23. const targetMaxSpeed = 20 / 3.6 // m/s
  24. return targetMinSpeed < obj.Speed && obj.Speed < targetMaxSpeed
  25. }
  26. // typeCheck 目标物类型检测
  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 typeCheck(obj *pjisuv_msgs.PerceptionObject) bool {
  30. const targetType uint8 = 3
  31. return targetType == obj.Type
  32. }
  33. // posCheck 判断目标物位置关系
  34. func posCheck(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. // Rule 检测前方大车低速行驶
  39. func Rule(msg *pjisuv_msgs.PerceptionObjects) string {
  40. defer func() {
  41. if r := recover(); r != nil {
  42. fmt.Println(fmt.Sprintf("Recovered from panic: [%s], [%#v]", Label(), r))
  43. }
  44. }()
  45. const MinStableFrameCount = 5
  46. const MaxTimeBetweenFrame = 0.5
  47. for _, obj := range msg.Objs {
  48. // 判断目标物是否满足筛选条件
  49. if speedCheck(&obj) && posCheck(&obj) && typeCheck(&obj) {
  50. // 目标物初见存档
  51. if prevObjRecord, exists := objectsStability[obj.Id]; exists == false {
  52. objectsStability[obj.Id] = Object{
  53. ID: obj.Id,
  54. StableAge: 1,
  55. LastExistTime: msg.Header.TimeStamp,
  56. }
  57. } else {
  58. currObjRecord := Object{
  59. ID: obj.Id,
  60. StableAge: 1,
  61. LastExistTime: msg.Header.TimeStamp,
  62. }
  63. // 目标物确认/更新/重置
  64. if currObjRecord.LastExistTime-prevObjRecord.LastExistTime <= MaxTimeBetweenFrame { // 目标物消失不超过0.5s
  65. currObjRecord.StableAge = prevObjRecord.StableAge + 1
  66. if currObjRecord.StableAge == MinStableFrameCount {
  67. delete(objectsStability, obj.Id)
  68. return Label()
  69. }
  70. }
  71. objectsStability[obj.Id] = currObjRecord
  72. }
  73. }
  74. }
  75. return ""
  76. }