FindTrafficLight.go 1.9 KB

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