CCCscp_f.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. Framenum int64 = 0
  14. param entity.PjisuvParam
  15. ObjectSlice = make(map[uint32][][]float32)
  16. )
  17. func main() {
  18. ticker := time.NewTicker(5 * time.Second)
  19. defer ticker.Stop()
  20. go listener()
  21. for {
  22. select {
  23. case <-ticker.C:
  24. FinalCallback()
  25. }
  26. }
  27. }
  28. func isCross(ObjectList [][]float32) bool {
  29. for i := 0; i < len(ObjectList[0]); i++ {
  30. Type := ObjectList[4][0]
  31. xi := ObjectList[0][i]
  32. yi := ObjectList[1][i]
  33. diff_hi := ObjectList[2][i]
  34. if xi >= 0 && yi >= 2 && diff_hi > 0 && Type != 100.0 { //简化条件
  35. //if xi >= 0 && yi >= 2 && diff_hi <= 120 && diff_hi >= 60 && Type != 1.0 {//理论条件
  36. for j := 0; j < len(ObjectList[0])-i-1; j++ {
  37. xj := ObjectList[0][j]
  38. yj := ObjectList[1][j]
  39. diff_hj := ObjectList[2][j]
  40. if xj >= 0 && yj <= -2 && diff_hj > 0 { //简化条件
  41. //if xj >= 0 && yj <= -2 && diff_hj <= 120 && diff_hj >= 60 {//理论条件
  42. return true
  43. }
  44. }
  45. }
  46. }
  47. return false
  48. }
  49. func FinalCallback() {
  50. for _, objValue := range ObjectSlice {
  51. //fmt.Println(objValue)
  52. //fmt.Println("------------------------------------------------")
  53. if len(objValue[0]) <= 10 || !isCross(objValue) {
  54. continue
  55. }
  56. event_lable := "CCCscp_f"
  57. fmt.Println(event_lable)
  58. ObjectSlice = make(map[uint32][][]float32)
  59. }
  60. }
  61. func CallbackCicvLocation(data *pjisuv_msgs.PerceptionLocalization) {
  62. param.YawOfCicvLocation = data.Yaw
  63. }
  64. func CallbackTpperception(data *pjisuv_msgs.PerceptionObjects) {
  65. Framenum += 1
  66. //fmt.Println(len(data.Objs))
  67. for _, obj := range data.Objs {
  68. //fmt.Println("ID", obj.Id, "Type", obj.Type)
  69. if math.Abs(float64(obj.X)) >= 80 || math.Abs(float64(obj.Y)) >= 30 {
  70. continue
  71. }
  72. if _, ok := ObjectSlice[obj.Id]; !ok {
  73. ObjectSlice[obj.Id] = [][]float32{{}, {}, {}, {}, {}}
  74. }
  75. //absspeed := math.Sqrt(math.Pow(float64(obj.Vxabs), 2)+math.Pow(float64(obj.Vyabs), 2)) * 3.6
  76. ObjectSlice[obj.Id][0] = append(ObjectSlice[obj.Id][0], obj.X)
  77. ObjectSlice[obj.Id][1] = append(ObjectSlice[obj.Id][1], obj.Y)
  78. diffh := (float64(obj.Heading - float32(param.YawOfCicvLocation)))
  79. if diffh < -180.0 {
  80. diffh = 360.0 + diffh
  81. } else if diffh > 180.0 {
  82. diffh = 360.0 - diffh
  83. } else {
  84. diffh = math.Abs(diffh)
  85. }
  86. //fmt.Println(diffh)
  87. ObjectSlice[obj.Id][2] = append(ObjectSlice[obj.Id][2], float32(diffh))
  88. ObjectSlice[obj.Id][3] = append(ObjectSlice[obj.Id][3], float32(Framenum))
  89. ObjectSlice[obj.Id][4] = append(ObjectSlice[obj.Id][4], float32(obj.Type))
  90. //ObjectSlice[obj.Id][3] = append(ObjectSlice[obj.Id][3], float32(absspeed))
  91. if len(ObjectSlice[obj.Id][0]) >= 200 {
  92. ObjectSlice[obj.Id][0] = ObjectSlice[obj.Id][0][1:]
  93. ObjectSlice[obj.Id][1] = ObjectSlice[obj.Id][1][1:]
  94. ObjectSlice[obj.Id][2] = ObjectSlice[obj.Id][2][1:]
  95. ObjectSlice[obj.Id][3] = ObjectSlice[obj.Id][3][1:]
  96. ObjectSlice[obj.Id][4] = ObjectSlice[obj.Id][4][1:]
  97. }
  98. }
  99. }
  100. func listener() {
  101. // create a node and connect to the master
  102. n, err := goroslib.NewNode(goroslib.NodeConf{
  103. Name: "goroslib_sub",
  104. MasterAddress: "127.0.0.1:11311",
  105. })
  106. if err != nil {
  107. panic(err)
  108. }
  109. defer n.Close()
  110. // create a subscriber
  111. subCicvLocation, err := goroslib.NewSubscriber(goroslib.SubscriberConf{
  112. Node: n,
  113. Topic: "/cicv_location",
  114. Callback: CallbackCicvLocation,
  115. })
  116. if err != nil {
  117. panic(err)
  118. }
  119. defer subCicvLocation.Close()
  120. // create a subscriber
  121. subTpperception, err := goroslib.NewSubscriber(goroslib.SubscriberConf{
  122. Node: n,
  123. Topic: "tpperception",
  124. Callback: CallbackTpperception,
  125. })
  126. if err != nil {
  127. panic(err)
  128. }
  129. defer subTpperception.Close()
  130. // wait for CTRL-C
  131. c := make(chan os.Signal, 1)
  132. signal.Notify(c, os.Interrupt)
  133. <-c
  134. }