FrontVehicleCutOutNear.go 2.7 KB

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