FrontVehicleCutInNear.go 2.8 KB

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