FindTrafficLight.go 1.8 KB

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