CCCscp_f.go 4.0 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("obj length", len(data.Objs))
  67. fmt.Println("Framenum", Framenum)
  68. for _, obj := range data.Objs {
  69. fmt.Println("ID", obj.Id, "Type", obj.Type)
  70. if math.Abs(float64(obj.X)) >= 80 || math.Abs(float64(obj.Y)) >= 30 {
  71. continue
  72. }
  73. if _, ok := ObjectSlice[obj.Id]; !ok {
  74. ObjectSlice[obj.Id] = [][]float32{{}, {}, {}, {}, {}}
  75. }
  76. //absspeed := math.Sqrt(math.Pow(float64(obj.Vxabs), 2)+math.Pow(float64(obj.Vyabs), 2)) * 3.6
  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. // 计算自车与目标的最小角度差(范围归到0到180度)
  80. diffh := (float64(obj.Heading - float32(param.YawOfCicvLocation)))
  81. if diffh < -180.0 {
  82. diffh = 360.0 + diffh
  83. } else if diffh > 180.0 {
  84. diffh = 360.0 - diffh
  85. } else {
  86. diffh = math.Abs(diffh)
  87. }
  88. //fmt.Println(diffh)
  89. ObjectSlice[obj.Id][2] = append(ObjectSlice[obj.Id][2], float32(diffh))
  90. ObjectSlice[obj.Id][3] = append(ObjectSlice[obj.Id][3], float32(Framenum))
  91. ObjectSlice[obj.Id][4] = append(ObjectSlice[obj.Id][4], float32(obj.Type))
  92. //ObjectSlice[obj.Id][3] = append(ObjectSlice[obj.Id][3], float32(absspeed))
  93. // 滑动窗口(保持切片大小为200,大于等于200则持续丢弃第一个,整体向前移动一位)
  94. if len(ObjectSlice[obj.Id][0]) >= 200 {
  95. ObjectSlice[obj.Id][0] = ObjectSlice[obj.Id][0][1:]
  96. ObjectSlice[obj.Id][1] = ObjectSlice[obj.Id][1][1:]
  97. ObjectSlice[obj.Id][2] = ObjectSlice[obj.Id][2][1:]
  98. ObjectSlice[obj.Id][3] = ObjectSlice[obj.Id][3][1:]
  99. ObjectSlice[obj.Id][4] = ObjectSlice[obj.Id][4][1:]
  100. }
  101. }
  102. }
  103. func listener() {
  104. // create a node and connect to the master
  105. n, err := goroslib.NewNode(goroslib.NodeConf{
  106. Name: "goroslib_sub",
  107. MasterAddress: "127.0.0.1:11311",
  108. })
  109. if err != nil {
  110. panic(err)
  111. }
  112. defer n.Close()
  113. // create a subscriber
  114. subCicvLocation, err := goroslib.NewSubscriber(goroslib.SubscriberConf{
  115. Node: n,
  116. Topic: "/cicv_location",
  117. Callback: CallbackCicvLocation,
  118. })
  119. if err != nil {
  120. panic(err)
  121. }
  122. defer subCicvLocation.Close()
  123. // create a subscriber
  124. subTpperception, err := goroslib.NewSubscriber(goroslib.SubscriberConf{
  125. Node: n,
  126. Topic: "tpperception",
  127. Callback: CallbackTpperception,
  128. })
  129. if err != nil {
  130. panic(err)
  131. }
  132. defer subTpperception.Close()
  133. // wait for CTRL-C
  134. c := make(chan os.Signal, 1)
  135. signal.Notify(c, os.Interrupt)
  136. <-c
  137. }