1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- package main
- import (
- "cicv-data-closedloop/pjisuv_ticker"
- "fmt"
- "math"
- "sync"
- "time"
- )
- //记得在produce_window.go中将AngleSlice的注释解除!!!
- var (
- threshold float64 = 0.06 //道路坡度大于6%就可以被认为是陡坡
- )
- // 定时任务触发器固定的
- func Topic() string {
- return pjisuv_ticker.TickerTopic
- }
- // ******* 禁止存在下划线_
- // 触发器标记
- func Label() string {
- return "ClimbingSteepHill"
- }
- 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
- // 计算欧拉角
- pitch := math.Asin(2 * (w*y - z*x))
- return pitch
- }
- func countChanges(AngleSlice [][]float64) int {
- num := 0
- for i := 0; i < len(AngleSlice[1]); i++ {
- pitch := QuaternionToEuler(AngleSlice[0][i], AngleSlice[1][i], AngleSlice[2][i], AngleSlice[3][i])
- if pitch >= 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 := "ClimbingSteepHill"
- fmt.Println(event_lable)
- pjisuv_ticker.TickerChan <- pjisuv_ticker.TickInfo{FaultLabel: Label(), FaultHappenTime: pjisuv_ticker.GetNowTimeCustom()}
- AngleSlice = make([][]float64, 0)
- shareVars.Store("AngleSlice", AngleSlice)
- }
- }
- }
|