FindTrafficLight.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package main
  2. import (
  3. "cicv-data-closedloop/pjisuv_msgs"
  4. "math"
  5. )
  6. func Topic() string {
  7. return "/cicv_location"
  8. }
  9. // Label todo 禁止存在下划线_
  10. func Label() string {
  11. return "FindTrafficLight"
  12. }
  13. type Point struct {
  14. Latitude float64
  15. Longitude float64
  16. }
  17. var (
  18. count1 int = 0
  19. //定义园区4个信号灯的坐标
  20. point2 = Point{39.72975930689718, 116.48861102824081}
  21. point3 = Point{39.7288805296616, 116.48812315228867}
  22. point4 = Point{39.73061430369551, 116.49225103553502}
  23. point5 = Point{39.73077491578002, 116.49060085035634}
  24. pointlist = []Point{point2, point3, point4, point5}
  25. )
  26. func Rule(data *pjisuv_msgs.PerceptionLocalization) string {
  27. if count1%10 == 0 {
  28. enterflag := IfEnter(pointlist, 15.0, data.Latitude, data.Longitude)
  29. if enterflag {
  30. //eventLabel := "FindTrafficLight"
  31. //fmt.Println(eventLabel)
  32. return "FindTrafficLight"
  33. }
  34. }
  35. count1++
  36. return ""
  37. }
  38. func IfEnter(pointlist []Point, radius float64, lat, lon float64) bool {
  39. // 判断是否进入点列表中的区域
  40. point1 := Point{Latitude: lat, Longitude: lon}
  41. for _, point := range pointlist {
  42. d := distance(point1, point)
  43. if d <= radius {
  44. return true
  45. }
  46. }
  47. return false
  48. }
  49. // 计算两点之间的距离(米)
  50. func distance(point1, point2 Point) float64 {
  51. // 经纬度转弧度
  52. lat1 := point1.Latitude * math.Pi / 180
  53. lon1 := point1.Longitude * math.Pi / 180
  54. lat2 := point2.Latitude * math.Pi / 180
  55. lon2 := point2.Longitude * math.Pi / 180
  56. // 计算距离
  57. dlon := lon2 - lon1
  58. dlat := lat2 - lat1
  59. a := math.Sin(dlat/2)*math.Sin(dlat/2) + math.Sin(dlon/2)*math.Sin(dlon/2)*math.Cos(lat1)*math.Cos(lat2)
  60. c := 2 * math.Atan2(math.Sqrt(a), math.Sqrt(1-a))
  61. d := 6371000 * c
  62. return d
  63. }