FindTrafficLight.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package main
  2. import (
  3. "cicv-data-closedloop/pjisuv_msgs"
  4. "cicv-data-closedloop/pjisuv_param"
  5. "math"
  6. )
  7. func Topic() string {
  8. return "/cicv_location"
  9. }
  10. // Label 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, param pjisuv_param.PjisuvParam) string {
  28. if count1%10 == 0 {
  29. enterflag := IfEnter(pointlist, 15.0, data.Latitude, data.Longitude)
  30. if enterflag {
  31. //eventLabel := "FindTrafficLight"
  32. //fmt.Println(eventLabel)
  33. return "FindTrafficLight"
  34. }
  35. }
  36. count1++
  37. return ""
  38. }
  39. func IfEnter(pointlist []Point, radius float64, lat, lon float64) bool {
  40. // 判断是否进入点列表中的区域
  41. point1 := Point{Latitude: lat, Longitude: lon}
  42. for _, point := range pointlist {
  43. d := distance(point1, point)
  44. if d <= radius {
  45. return true
  46. }
  47. }
  48. return false
  49. }
  50. // 计算两点之间的距离(米)
  51. func distance(point1, point2 Point) float64 {
  52. // 经纬度转弧度
  53. lat1 := point1.Latitude * math.Pi / 180
  54. lon1 := point1.Longitude * math.Pi / 180
  55. lat2 := point2.Latitude * math.Pi / 180
  56. lon2 := point2.Longitude * math.Pi / 180
  57. // 计算距离
  58. dlon := lon2 - lon1
  59. dlat := lat2 - lat1
  60. a := math.Sin(dlat/2)*math.Sin(dlat/2) + math.Sin(dlon/2)*math.Sin(dlon/2)*math.Cos(lat1)*math.Cos(lat2)
  61. c := 2 * math.Atan2(math.Sqrt(a), math.Sqrt(1-a))
  62. d := 6371000 * c
  63. return d
  64. }