EnterTjunction.go 2.0 KB

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