AuLongStop.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package main
  2. import (
  3. "awesomeProject/entity"
  4. "awesomeProject/pjisuv_msgs"
  5. "fmt"
  6. "github.com/bluenviron/goroslib/v2"
  7. "os"
  8. "os/signal"
  9. "time"
  10. )
  11. var (
  12. param entity.PjisuvParam
  13. StartTime int64
  14. IsStopped bool
  15. )
  16. func CallbackCicvLocation(data *pjisuv_msgs.PerceptionLocalization) {
  17. //print(param.AutomodeOfPjVehicleFdbPub)
  18. if param.AutomodeOfPjVehicleFdbPub == 1 {
  19. if data.VelocityX < 0.5 {
  20. // 如果之前没有记录开始时间,记录当前时间
  21. if StartTime == 0 {
  22. StartTime = time.Now().Unix()
  23. }
  24. // 判断是否持续超过一分钟
  25. if time.Now().Unix()-StartTime > 10 {
  26. if !IsStopped {
  27. event_label := "AuLongStop"
  28. fmt.Println(event_label)
  29. IsStopped = true
  30. }
  31. }
  32. } else {
  33. // 如果速度大于 0.1,重置开始时间和停止标志
  34. StartTime = 0
  35. //is_stopped = false
  36. }
  37. }
  38. }
  39. func CallbackPjVehicleFdbPub(data *pjisuv_msgs.VehicleFdb) {
  40. param.AutomodeOfPjVehicleFdbPub = data.Automode
  41. }
  42. func main() {
  43. go listener()
  44. select {}
  45. //time.Sleep(10000)
  46. }
  47. func listener() {
  48. // create a node and connect to the master
  49. n, err := goroslib.NewNode(goroslib.NodeConf{
  50. Name: "goroslib_sub",
  51. MasterAddress: "127.0.0.1:11311",
  52. })
  53. if err != nil {
  54. panic(err)
  55. }
  56. defer n.Close()
  57. // create a subscriber
  58. subCicvLocation, err := goroslib.NewSubscriber(goroslib.SubscriberConf{
  59. Node: n,
  60. Topic: "/cicv_location",
  61. Callback: CallbackCicvLocation,
  62. })
  63. if err != nil {
  64. panic(err)
  65. }
  66. defer subCicvLocation.Close()
  67. // create a subscriber
  68. subTpperception, err := goroslib.NewSubscriber(goroslib.SubscriberConf{
  69. Node: n,
  70. Topic: "pj_vehicle_fdb_pub",
  71. Callback: CallbackPjVehicleFdbPub,
  72. })
  73. if err != nil {
  74. panic(err)
  75. }
  76. defer subTpperception.Close()
  77. // wait for CTRL-C
  78. c := make(chan os.Signal, 1)
  79. signal.Notify(c, os.Interrupt)
  80. <-c
  81. }