EnterTjunction.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package main
  2. import (
  3. "cicv-data-closedloop/pjisuv_msgs"
  4. "fmt"
  5. "math"
  6. )
  7. func Topic() string {
  8. return "/cicv_location"
  9. }
  10. // Label todo 禁止存在下划线_
  11. func Label() string {
  12. return "EnterTjunction"
  13. }
  14. type Point struct {
  15. Latitude float64
  16. Longitude float64
  17. }
  18. var (
  19. count2 int = 0
  20. //定义园区部门T字路口的经纬度坐标值
  21. point3 = Point{39.73040966605621, 116.48995329696209}
  22. point4 = Point{39.73083727413453, 116.49079780188244}
  23. point5 = Point{39.72976753711939, 116.49043130389033}
  24. point6 = Point{39.73012466515933, 116.49128381717591}
  25. point7 = Point{39.729251498328246, 116.49077484625299}
  26. point8 = Point{39.72964529630643, 116.49164592200161}
  27. pointlist = []Point{point3, point4, point5, point6, point7, point8}
  28. )
  29. func Rule(data *pjisuv_msgs.PerceptionLocalization) string {
  30. defer func() {
  31. if r := recover(); r != nil {
  32. fmt.Println("Recovered from panic:", r)
  33. }
  34. }()
  35. if count2%10 == 0 {
  36. enterflag := IfEnter(pointlist, 12.0, data.Latitude, data.Longitude)
  37. if enterflag {
  38. //eventLabel := "EnterTjunction"
  39. //fmt.Println(eventLabel)
  40. return "EnterTjunction"
  41. }
  42. }
  43. count2++
  44. return ""
  45. }
  46. func IfEnter(pointlist []Point, radius float64, lat, lon float64) bool {
  47. // 判断是否进入点列表中的区域
  48. point1 := Point{Latitude: lat, Longitude: lon}
  49. for _, point := range pointlist {
  50. d := distance(point1, point)
  51. if d <= radius {
  52. return true
  53. }
  54. }
  55. return false
  56. }
  57. // 计算两点之间的距离(米)
  58. func distance(point1, point2 Point) float64 {
  59. // 经纬度转弧度
  60. lat1 := point1.Latitude * math.Pi / 180
  61. lon1 := point1.Longitude * math.Pi / 180
  62. lat2 := point2.Latitude * math.Pi / 180
  63. lon2 := point2.Longitude * math.Pi / 180
  64. // 计算距离
  65. dlon := lon2 - lon1
  66. dlat := lat2 - lat1
  67. a := math.Sin(dlat/2)*math.Sin(dlat/2) + math.Sin(dlon/2)*math.Sin(dlon/2)*math.Cos(lat1)*math.Cos(lat2)
  68. c := 2 * math.Atan2(math.Sqrt(a), math.Sqrt(1-a))
  69. d := 6371000 * c
  70. return d
  71. }