FindTrafficLight.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. // todo 禁止存在下划线_
  12. func Label() string {
  13. return "FindTrafficLight"
  14. }
  15. type Point struct {
  16. Latitude float64
  17. Longitude float64
  18. }
  19. var (
  20. count1 int = 0
  21. //定义园区4个信号灯的坐标
  22. point2 = Point{39.72975930689718, 116.48861102824081}
  23. point3 = Point{39.7288805296616, 116.48812315228867}
  24. point4 = Point{39.73061430369551, 116.49225103553502}
  25. point5 = Point{39.73077491578002, 116.49060085035634}
  26. pointlist = []Point{point2, point3, point4, point5}
  27. )
  28. func Rule(shareVars *sync.Map, data *pjisuv_msgs.PerceptionLocalization) string {
  29. defer func() {
  30. if r := recover(); r != nil {
  31. fmt.Println("Recovered from panic:", r)
  32. }
  33. }()
  34. if count1%10 == 0 {
  35. enterflag := IfEnter(pointlist, 25.0, data.Latitude, data.Longitude)
  36. velocityXOfCicvLocation, ok := shareVars.Load("VelocityXOfCicvLocation")
  37. if ok {
  38. if enterflag && velocityXOfCicvLocation.(float64) >= 1 {
  39. return Label()
  40. }
  41. }
  42. }
  43. count1++
  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. }