RearVehiclesFrequentChangeLane.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. Maxlenobj int32 = 0
  14. param entity.PjisuvParam
  15. ObjectSlice = make(map[uint32][][]float32)
  16. )
  17. func main() {
  18. ticker := time.NewTicker(4 * time.Second)
  19. defer ticker.Stop()
  20. go listener()
  21. for {
  22. select {
  23. case <-ticker.C:
  24. FinalCallback()
  25. }
  26. }
  27. }
  28. func countChanges(slice [][]float32) int {
  29. //fmt.Println(slice)
  30. count := 0
  31. lable1:
  32. for i := 0; i < len(slice[1]); {
  33. xi := slice[0][i]
  34. yi := math.Abs(float64(slice[1][i]))
  35. if (yi >= 1.8 || yi < 0.7) && xi <= -2 && math.Abs(param.AngularVelocityZOfCicvLocation) <= 1.6 {
  36. for j := 0; j < len(slice[1])-i-1; j++ {
  37. xij := slice[0][1+i+j]
  38. yij := math.Abs(float64(slice[1][1+i+j]))
  39. if ((yi >= 1.8 && yij <= 0.7) || (yi < 0.7 && yij >= 1.8)) && xij <= -2 && math.Abs(param.AngularVelocityZOfCicvLocation) <= 1.6 {
  40. count++
  41. fmt.Println("here!!")
  42. i = i + j + 1
  43. continue lable1
  44. }
  45. }
  46. break lable1
  47. } else {
  48. i++
  49. }
  50. }
  51. return count
  52. }
  53. func FinalCallback() {
  54. for _, objValue := range ObjectSlice {
  55. //fmt.Println(objValue)
  56. //fmt.Println("------------------------------------------------")
  57. Maxlenobj = max(Maxlenobj, int32(len(objValue[0])))
  58. if len(objValue[0]) <= 10 || countChanges(objValue) < 2 {
  59. continue
  60. }
  61. event_lable := "RearVehiclesFrequentChangeLane"
  62. fmt.Println(event_lable)
  63. ObjectSlice = make(map[uint32][][]float32)
  64. }
  65. if Maxlenobj >= 250 {
  66. ObjectSlice = make(map[uint32][][]float32)
  67. Maxlenobj = 0
  68. }
  69. }
  70. func CallbackCicvLocation(data *pjisuv_msgs.PerceptionLocalization) {
  71. param.AngularVelocityZOfCicvLocation = data.AngularVelocityZ
  72. }
  73. func CallbackTpperception(data *pjisuv_msgs.PerceptionObjects) {
  74. for _, obj := range data.Objs {
  75. if math.Abs(float64(obj.X)) >= 60 || math.Abs(float64(obj.Y)) >= 10 {
  76. continue
  77. }
  78. if _, ok := ObjectSlice[obj.Id]; !ok {
  79. ObjectSlice[obj.Id] = [][]float32{{}, {}}
  80. }
  81. ObjectSlice[obj.Id][0] = append(ObjectSlice[obj.Id][0], obj.X)
  82. ObjectSlice[obj.Id][1] = append(ObjectSlice[obj.Id][1], obj.Y)
  83. }
  84. }
  85. func listener() {
  86. // create a node and connect to the master
  87. n, err := goroslib.NewNode(goroslib.NodeConf{
  88. Name: "goroslib_sub",
  89. MasterAddress: "127.0.0.1:11311",
  90. })
  91. if err != nil {
  92. panic(err)
  93. }
  94. defer n.Close()
  95. // create a subscriber
  96. subCicvLocation, err := goroslib.NewSubscriber(goroslib.SubscriberConf{
  97. Node: n,
  98. Topic: "/cicv_location",
  99. Callback: CallbackCicvLocation,
  100. })
  101. if err != nil {
  102. panic(err)
  103. }
  104. defer subCicvLocation.Close()
  105. // create a subscriber
  106. subTpperception, err := goroslib.NewSubscriber(goroslib.SubscriberConf{
  107. Node: n,
  108. Topic: "tpperception",
  109. Callback: CallbackTpperception,
  110. })
  111. if err != nil {
  112. panic(err)
  113. }
  114. defer subTpperception.Close()
  115. // wait for CTRL-C
  116. c := make(chan os.Signal, 1)
  117. signal.Notify(c, os.Interrupt)
  118. <-c
  119. }