LongDownhill.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package main
  2. import (
  3. "cicv-data-closedloop/pjisuv_msgs"
  4. "fmt"
  5. "math"
  6. "sync"
  7. )
  8. var (
  9. threshold = -0.05
  10. count1 = 0
  11. UphillPointSlice = [][]float64{{}, {}}
  12. CountNoneUphill = 0
  13. )
  14. type Point struct {
  15. X float64
  16. Y float64
  17. }
  18. func Topic() string {
  19. return "/cicv_location"
  20. }
  21. // 禁止存在下划线_
  22. func Label() string {
  23. return "LongDownhill"
  24. }
  25. // QuaternionToEuler 将四元数转换为欧拉角
  26. func QuaternionToEuler(x, y, z, w float64) float64 {
  27. // 归一化四元数
  28. length := math.Sqrt(x*x + y*y + z*z + w*w)
  29. x /= length
  30. y /= length
  31. z /= length
  32. w /= length
  33. // 计算欧拉角
  34. pitch := math.Asin(2 * (w*y - z*x))
  35. if math.IsNaN(pitch) {
  36. return 0.0
  37. }
  38. return pitch
  39. }
  40. // 计算两点之间的距离(米)
  41. func distance(point1, point2 Point) float64 {
  42. d := math.Sqrt((point2.X-point1.X)*(point2.X-point1.X) + (point2.Y-point1.Y)*(point2.Y-point1.Y))
  43. return d
  44. }
  45. func Rule(shareVars *sync.Map, data *pjisuv_msgs.PerceptionLocalization) string {
  46. defer func() {
  47. if r := recover(); r != nil {
  48. fmt.Println("Recovered from panic:", r)
  49. }
  50. }()
  51. OutsideWorkshopFlag, _ := shareVars.Load("OutsideWorkshopFlag")
  52. OutsideWorkshopFlag1 := OutsideWorkshopFlag.(bool)
  53. if OutsideWorkshopFlag1 == true {
  54. if count1%10 == 0 {
  55. pitch := QuaternionToEuler(data.Qx, data.Qy, data.Qz, data.Qw)
  56. //fmt.Println(pitch)
  57. if pitch < threshold {
  58. UphillPointSlice[0] = append(UphillPointSlice[0], data.PositionX)
  59. UphillPointSlice[1] = append(UphillPointSlice[1], data.PositionY)
  60. if len(UphillPointSlice[0]) > 250 {
  61. P1 := Point{UphillPointSlice[0][0], UphillPointSlice[1][0]}
  62. P2 := Point{UphillPointSlice[0][len(UphillPointSlice[0])-1], UphillPointSlice[1][len(UphillPointSlice[1])-1]}
  63. distance := distance(P1, P2)
  64. if distance >= 100 {
  65. event_lable := "LongUphill"
  66. fmt.Println(event_lable)
  67. CountNoneUphill = 0
  68. UphillPointSlice = [][]float64{{}, {}}
  69. return Label()
  70. }
  71. }
  72. } else {
  73. CountNoneUphill++
  74. if CountNoneUphill == 3 {
  75. if len(UphillPointSlice[0]) > 1 {
  76. P1 := Point{UphillPointSlice[0][0], UphillPointSlice[1][0]}
  77. P2 := Point{UphillPointSlice[0][len(UphillPointSlice[0])-1], UphillPointSlice[1][len(UphillPointSlice[1])-1]}
  78. distance := distance(P1, P2)
  79. if distance >= 100 {
  80. event_lable := "LongUphill"
  81. fmt.Println(event_lable)
  82. return Label()
  83. }
  84. UphillPointSlice = [][]float64{{}, {}}
  85. }
  86. CountNoneUphill = 0
  87. }
  88. }
  89. count1 = 1
  90. }
  91. count1++
  92. }
  93. return ""
  94. }