FindTrafficLightP4.go 1.8 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. //point = Point{39.72975930689718, 116.48861102824081}
  23. //point3 = Point{39.7288805296616, 116.48812315228867}
  24. //point4 = Point{39.73061430369551, 116.49225103553502}
  25. point = 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(point, 25.0, data.Latitude, data.Longitude)
  36. AbsSpeed, _ := shareVars.Load("AbsSpeed")
  37. if enterflag && AbsSpeed.(float64) > 1 {
  38. //eventLabel := "FindTrafficLight"
  39. //fmt.Println(eventLabel)
  40. return "FindTrafficLightP1"
  41. }
  42. }
  43. count1++
  44. return ""
  45. }
  46. func IfEnter(point Point, radius float64, lat, lon float64) bool {
  47. // 判断是否进入点列表中的区域
  48. point1 := Point{Latitude: lat, Longitude: lon}
  49. d := distance(point1, point)
  50. if d <= radius {
  51. return true
  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. }