CarFollowingTooCloseAtNight.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package main
  2. import (
  3. "awesomeProject/entity"
  4. "awesomeProject/pjisuv_msgs"
  5. "fmt"
  6. "github.com/bluenviron/goroslib/v2"
  7. "math"
  8. "os"
  9. "os/signal"
  10. "time"
  11. )
  12. var (
  13. param entity.PjisuvParam
  14. StartTime int64
  15. count1 int64
  16. ObjectSlice = make(map[uint32][][]float32)
  17. )
  18. func Ifatnight() bool {
  19. // 获取当前时间
  20. now := time.Now()
  21. later := now.Add(0 * time.Hour)
  22. // 获取当前小时
  23. hour := later.Hour()
  24. // 判断当前时间是白天还是夜晚
  25. if hour >= 0 && hour < 5 || hour >= 20 && hour <= 23 {
  26. return true
  27. } else {
  28. return false
  29. }
  30. }
  31. func IfObstaclesNearby() bool {
  32. for _, obj := range ObjectSlice {
  33. if obj[0][len(obj[0])-1] <= 13 && obj[0][len(obj[0])-1] >= 3 && (math.Abs(float64(obj[1][len(obj[1])-1]))) <= 6 {
  34. return true
  35. }
  36. }
  37. return false
  38. }
  39. func CallbackCicvLocation(data *pjisuv_msgs.PerceptionLocalization) {
  40. //print(param.AutomodeOfPjVehicleFdbPub)
  41. if count1%10 == 0 && Ifatnight() {
  42. AbsSpeed := math.Sqrt(math.Pow(data.VelocityX, 2) + math.Pow(data.VelocityY, 2))
  43. flag := IfObstaclesNearby()
  44. //fmt.Println(ObjectSlice)
  45. if AbsSpeed >= 1 && flag {
  46. // 如果之前没有记录开始时间,记录当前时间
  47. if StartTime == 0 {
  48. StartTime = time.Now().Unix()
  49. }
  50. // 判断是否持续超过一分钟
  51. if time.Now().Unix()-StartTime > 3 {
  52. event_label := "CarFollowingTooCloseAtNight"
  53. fmt.Println(event_label)
  54. }
  55. } else {
  56. // 如果速度大于 0.1,重置开始时间和停止标志
  57. StartTime = 0
  58. }
  59. count1 = 1
  60. } else {
  61. count1++
  62. }
  63. }
  64. func main() {
  65. go listener()
  66. select {}
  67. //time.Sleep(10000)
  68. }
  69. func CallbackTpperception(data *pjisuv_msgs.PerceptionObjects) {
  70. for _, obj := range data.Objs {
  71. if obj.X <= -2 || math.Abs(float64(obj.Y)) >= 90 {
  72. continue
  73. }
  74. if _, ok := ObjectSlice[obj.Id]; !ok {
  75. ObjectSlice[obj.Id] = [][]float32{{}, {}}
  76. }
  77. ObjectSlice[obj.Id][0] = append(ObjectSlice[obj.Id][0], obj.X)
  78. ObjectSlice[obj.Id][1] = append(ObjectSlice[obj.Id][1], obj.Y)
  79. }
  80. }
  81. func listener() {
  82. // create a node and connect to the master
  83. n, err := goroslib.NewNode(goroslib.NodeConf{
  84. Name: "goroslib_sub",
  85. MasterAddress: "127.0.0.1:11311",
  86. })
  87. if err != nil {
  88. panic(err)
  89. }
  90. defer n.Close()
  91. // create a subscriber
  92. subCicvLocation, err := goroslib.NewSubscriber(goroslib.SubscriberConf{
  93. Node: n,
  94. Topic: "/cicv_location",
  95. Callback: CallbackCicvLocation,
  96. })
  97. if err != nil {
  98. panic(err)
  99. }
  100. defer subCicvLocation.Close()
  101. // create a subscriber
  102. subTpperception, err := goroslib.NewSubscriber(goroslib.SubscriberConf{
  103. Node: n,
  104. Topic: "/tpperception",
  105. Callback: CallbackTpperception,
  106. })
  107. if err != nil {
  108. panic(err)
  109. }
  110. defer subTpperception.Close()
  111. // wait for CTRL-C
  112. c := make(chan os.Signal, 1)
  113. signal.Notify(c, os.Interrupt)
  114. <-c
  115. }