12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- package main
- import (
- "cicv-data-closedloop/pjisuv_ticker"
- "fmt"
- "math"
- "sync"
- "time"
- )
- //记得在produce_window.go中将AngleSlice的注释解除!!!
- var (
- threshold float64 = 0.5236 //车辆的横滚角大于 30 度到 45 度时,就存在较高的侧翻风险。
- )
- // 定时任务触发器固定的
- func Topic() string {
- return pjisuv_ticker.TickerTopic
- }
- // ******* 禁止存在下划线_
- // 触发器标记
- func Label() string {
- return "RolloverWarning"
- }
- func Rule(shareVars *sync.Map) {
- defer func() {
- if r := recover(); r != nil {
- fmt.Println("Recovered from panic:", r)
- }
- }()
- // 1 使用goroutine
- go func(shareVars *sync.Map) {
- // 2 定义触发器的间隔时间
- ticker := time.NewTicker(time.Duration(2) * time.Second)
- defer ticker.Stop()
- // 3 运行一个无限循环
- for {
- select {
- // 定时器触发时执行的代码
- case <-ticker.C:
- FinalCallback(shareVars)
- }
- }
- }(shareVars)
- }
- // QuaternionToEuler 将四元数转换为欧拉角
- func QuaternionToEuler(x, y, z, w float64) float64 {
- // 归一化四元数
- length := math.Sqrt(x*x + y*y + z*z + w*w)
- x /= length
- y /= length
- z /= length
- w /= length
- // 计算欧拉角
- roll := math.Atan2(2*(w*x+y*z), 1-2*(x*x+y*y))
- //pitch := math.Asin(2 * (w*y - z*x))
- //yaw := math.Atan2(2*(w*z+x*y), 1-2*(y*y+z*z))
- return roll
- }
- func countChanges(AngleSlice [][]float64) int {
- num := 0
- for i := 0; i < len(AngleSlice[1]); i++ {
- roll := QuaternionToEuler(AngleSlice[0][i], AngleSlice[1][i], AngleSlice[2][i], AngleSlice[3][i])
- if math.Abs(roll) >= threshold {
- num++
- }
- }
- return num
- }
- func FinalCallback(shareVars *sync.Map) {
- OutsideWorkshopFlag, ok := shareVars.Load("OutsideWorkshopFlag")
- AngleSlice, ok1 := shareVars.Load("AngleSlice")
- if ok && ok1 && OutsideWorkshopFlag.(bool) == true {
- count := countChanges(AngleSlice.([][]float64))
- if count >= 5 {
- event_lable := "RolloverWarning"
- fmt.Println(event_lable)
- pjisuv_ticker.TickerChan <- pjisuv_ticker.TickInfo{FaultLabel: Label(), FaultHappenTime: pjisuv_ticker.GetNowTimeCustom()}
- AngleSlice = make([][]float64, 0)
- shareVars.Store("AngleSlice", AngleSlice)
- }
- }
- }
|