RolloverWarning.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package main
  2. import (
  3. "cicv-data-closedloop/pjisuv_ticker"
  4. "fmt"
  5. "math"
  6. "sync"
  7. "time"
  8. )
  9. //记得在produce_window.go中将AngleSlice的注释解除!!!
  10. var (
  11. threshold float64 = 0.5236 //车辆的横滚角大于 30 度到 45 度时,就存在较高的侧翻风险。
  12. )
  13. // 定时任务触发器固定的
  14. func Topic() string {
  15. return pjisuv_ticker.TickerTopic
  16. }
  17. // ******* 禁止存在下划线_
  18. // 触发器标记
  19. func Label() string {
  20. return "RolloverWarning"
  21. }
  22. func Rule(shareVars *sync.Map) {
  23. defer func() {
  24. if r := recover(); r != nil {
  25. fmt.Println("Recovered from panic:", r)
  26. }
  27. }()
  28. // 1 使用goroutine
  29. go func(shareVars *sync.Map) {
  30. // 2 定义触发器的间隔时间
  31. ticker := time.NewTicker(time.Duration(2) * time.Second)
  32. defer ticker.Stop()
  33. // 3 运行一个无限循环
  34. for {
  35. select {
  36. // 定时器触发时执行的代码
  37. case <-ticker.C:
  38. FinalCallback(shareVars)
  39. }
  40. }
  41. }(shareVars)
  42. }
  43. // QuaternionToEuler 将四元数转换为欧拉角
  44. func QuaternionToEuler(x, y, z, w float64) float64 {
  45. // 归一化四元数
  46. length := math.Sqrt(x*x + y*y + z*z + w*w)
  47. x /= length
  48. y /= length
  49. z /= length
  50. w /= length
  51. // 计算欧拉角
  52. roll := math.Atan2(2*(w*x+y*z), 1-2*(x*x+y*y))
  53. //pitch := math.Asin(2 * (w*y - z*x))
  54. //yaw := math.Atan2(2*(w*z+x*y), 1-2*(y*y+z*z))
  55. return roll
  56. }
  57. func countChanges(AngleSlice [][]float64) int {
  58. num := 0
  59. for i := 0; i < len(AngleSlice[1]); i++ {
  60. roll := QuaternionToEuler(AngleSlice[0][i], AngleSlice[1][i], AngleSlice[2][i], AngleSlice[3][i])
  61. if math.Abs(roll) >= threshold {
  62. num++
  63. }
  64. }
  65. return num
  66. }
  67. func FinalCallback(shareVars *sync.Map) {
  68. OutsideWorkshopFlag, ok := shareVars.Load("OutsideWorkshopFlag")
  69. AngleSlice, ok1 := shareVars.Load("AngleSlice")
  70. if ok && ok1 && OutsideWorkshopFlag.(bool) == true {
  71. count := countChanges(AngleSlice.([][]float64))
  72. if count >= 5 {
  73. event_lable := "RolloverWarning"
  74. fmt.Println(event_lable)
  75. pjisuv_ticker.TickerChan <- pjisuv_ticker.TickInfo{FaultLabel: Label(), FaultHappenTime: pjisuv_ticker.GetNowTimeCustom()}
  76. AngleSlice = make([][]float64, 0)
  77. shareVars.Store("AngleSlice", AngleSlice)
  78. }
  79. }
  80. }