produce_window.go 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891
  1. package service
  2. import (
  3. commonConfig "cicv-data-closedloop/aarch64/pjisuv/common/config"
  4. "cicv-data-closedloop/aarch64/pjisuv/common/service"
  5. masterConfig "cicv-data-closedloop/aarch64/pjisuv/master/config"
  6. "cicv-data-closedloop/common/config/c_log"
  7. commonEntity "cicv-data-closedloop/common/entity"
  8. "cicv-data-closedloop/common/util"
  9. "cicv-data-closedloop/pjisuv_msgs"
  10. "cicv-data-closedloop/pjisuv_param"
  11. "github.com/bluenviron/goroslib/v2"
  12. "github.com/bluenviron/goroslib/v2/pkg/msgs/geometry_msgs"
  13. "github.com/bluenviron/goroslib/v2/pkg/msgs/nav_msgs"
  14. "github.com/bluenviron/goroslib/v2/pkg/msgs/sensor_msgs"
  15. "github.com/bluenviron/goroslib/v2/pkg/msgs/tf2_msgs"
  16. "github.com/bluenviron/goroslib/v2/pkg/msgs/visualization_msgs"
  17. "math"
  18. "sync"
  19. "time"
  20. )
  21. // 所有共享变量
  22. var (
  23. pjisuvParam = pjisuv_param.PjisuvParam{
  24. ObjDicOfTpperception: make(map[uint32][]float32),
  25. ObjTypeDicOfTpperception: make(map[uint32]uint8),
  26. ObjSpeedDicOfTpperception: make(map[uint32]float64),
  27. }
  28. // 保存时间窗口需要锁,防止数据竟态
  29. saveTimeWindowMutex sync.Mutex
  30. // /cicv_location
  31. mutexOfCicvLocation sync.RWMutex
  32. // /tpperception
  33. mutexOfTpperception sync.RWMutex
  34. // /pj_control_pub
  35. mutexOfPjControlPub sync.RWMutex
  36. // /data_read
  37. mutexOfDataRead sync.RWMutex
  38. // /pj_vehicle_fdb_pub
  39. mutexOfCicvAmrTrajectory sync.RWMutex
  40. latestTimeWindowEnd = util.GetTimeCustom(time.Now())
  41. // 每个触发器5秒触发一次
  42. triggerInterval = 3.0
  43. )
  44. // 负责监听所有主题并修改时间窗口
  45. func ProduceWindow() {
  46. c_log.GlobalLogger.Info("订阅者 goroutine,启动。")
  47. var err error
  48. subscribers := make([]*goroslib.Subscriber, len(commonConfig.SubscribeTopics))
  49. subscribersTimes := make([]time.Time, len(commonConfig.SubscribeTopics))
  50. subscribersTimeMutexes := make([]sync.Mutex, len(commonConfig.SubscribeTopics))
  51. subscribersMutexes := make([]sync.Mutex, len(commonConfig.SubscribeTopics))
  52. for i, topic := range commonConfig.SubscribeTopics {
  53. for {
  54. // 增加了可扩展性
  55. if topic == masterConfig.TopicOfCicvExtend {
  56. go func() {
  57. for {
  58. time.Sleep(time.Duration(3500) * time.Millisecond)
  59. for _, f := range masterConfig.RuleOfCicvExtend {
  60. label := f(pjisuvParam)
  61. if label != "" {
  62. saveTimeWindow(label, util.GetNowTimeCustom(), commonEntity.GetLastTimeWindow())
  63. subscribersTimes[i] = time.Now()
  64. break
  65. }
  66. }
  67. }
  68. }()
  69. }
  70. // 其他常规监听器
  71. c_log.GlobalLogger.Info("创建订阅者订阅话题:" + topic)
  72. // 1
  73. if topic == masterConfig.TopicOfAmrPose && (len(masterConfig.RuleOfAmrPose1) > 0 || len(masterConfig.RuleOfAmrPose2) > 0) {
  74. subscribers[i], err = goroslib.NewSubscriber(goroslib.SubscriberConf{
  75. Node: commonConfig.RosNode,
  76. Topic: topic,
  77. Callback: func(data *visualization_msgs.MarkerArray) {
  78. subscribersTimeMutexes[i].Lock()
  79. if time.Since(subscribersTimes[i]).Seconds() > triggerInterval {
  80. subscribersMutexes[i].Lock()
  81. faultHappenTime := util.GetNowTimeCustom() // 获取当前故障发生时间
  82. lastTimeWindow := commonEntity.GetLastTimeWindow() // 获取最后一个时间窗口
  83. faultLabel := ""
  84. if len(masterConfig.RuleOfAmrPose1) > 0 {
  85. for _, f := range masterConfig.RuleOfAmrPose1 {
  86. faultLabel = f(data)
  87. if faultLabel != "" {
  88. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  89. subscribersTimes[i] = time.Now()
  90. goto TriggerSuccess
  91. }
  92. }
  93. }
  94. if len(masterConfig.RuleOfAmrPose2) > 0 {
  95. for _, f := range masterConfig.RuleOfAmrPose2 {
  96. faultLabel = f(data, &pjisuvParam)
  97. if faultLabel != "" {
  98. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  99. subscribersTimes[i] = time.Now()
  100. goto TriggerSuccess
  101. }
  102. }
  103. }
  104. TriggerSuccess:
  105. subscribersMutexes[i].Unlock()
  106. }
  107. subscribersTimeMutexes[i].Unlock()
  108. },
  109. })
  110. }
  111. // 2
  112. if topic == masterConfig.TopicOfBoundingBoxesFast && (len(masterConfig.RuleOfBoundingBoxesFast1) > 0 || len(masterConfig.RuleOfBoundingBoxesFast2) > 0) {
  113. subscribers[i], err = goroslib.NewSubscriber(goroslib.SubscriberConf{
  114. Node: commonConfig.RosNode,
  115. Topic: topic,
  116. Callback: func(data *pjisuv_msgs.BoundingBoxArray) {
  117. subscribersTimeMutexes[i].Lock()
  118. if time.Since(subscribersTimes[i]).Seconds() > triggerInterval {
  119. subscribersMutexes[i].Lock()
  120. faultHappenTime := util.GetNowTimeCustom() // 获取当前故障发生时间
  121. lastTimeWindow := commonEntity.GetLastTimeWindow() // 获取最后一个时间窗口
  122. faultLabel := ""
  123. if len(masterConfig.RuleOfBoundingBoxesFast1) > 0 {
  124. for _, f := range masterConfig.RuleOfBoundingBoxesFast1 {
  125. faultLabel = f(data)
  126. if faultLabel != "" {
  127. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  128. subscribersTimes[i] = time.Now()
  129. goto TriggerSuccess
  130. }
  131. }
  132. }
  133. if len(masterConfig.RuleOfBoundingBoxesFast2) > 0 {
  134. for _, f := range masterConfig.RuleOfBoundingBoxesFast2 {
  135. faultLabel = f(data, &pjisuvParam)
  136. if faultLabel != "" {
  137. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  138. subscribersTimes[i] = time.Now()
  139. goto TriggerSuccess
  140. }
  141. }
  142. }
  143. TriggerSuccess:
  144. subscribersMutexes[i].Unlock()
  145. }
  146. subscribersTimeMutexes[i].Unlock()
  147. },
  148. })
  149. }
  150. // 3
  151. if topic == masterConfig.TopicOfCameraFault && (len(masterConfig.RuleOfCameraFault1) > 0 || len(masterConfig.RuleOfCameraFault2) > 0) {
  152. subscribers[i], err = goroslib.NewSubscriber(goroslib.SubscriberConf{
  153. Node: commonConfig.RosNode,
  154. Topic: topic,
  155. Callback: func(data *pjisuv_msgs.FaultVec) {
  156. subscribersTimeMutexes[i].Lock()
  157. if time.Since(subscribersTimes[i]).Seconds() > triggerInterval {
  158. subscribersMutexes[i].Lock()
  159. faultHappenTime := util.GetNowTimeCustom() // 获取当前故障发生时间
  160. lastTimeWindow := commonEntity.GetLastTimeWindow() // 获取最后一个时间窗口
  161. faultLabel := ""
  162. if len(masterConfig.RuleOfCameraFault1) > 0 {
  163. for _, f := range masterConfig.RuleOfCameraFault1 {
  164. faultLabel = f(data)
  165. if faultLabel != "" {
  166. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  167. subscribersTimes[i] = time.Now()
  168. goto TriggerSuccess
  169. }
  170. }
  171. }
  172. if len(masterConfig.RuleOfCameraFault2) > 0 {
  173. for _, f := range masterConfig.RuleOfCameraFault2 {
  174. faultLabel = f(data, &pjisuvParam)
  175. if faultLabel != "" {
  176. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  177. subscribersTimes[i] = time.Now()
  178. goto TriggerSuccess
  179. }
  180. }
  181. }
  182. TriggerSuccess:
  183. subscribersMutexes[i].Unlock()
  184. }
  185. subscribersTimeMutexes[i].Unlock()
  186. },
  187. })
  188. }
  189. // 4
  190. if topic == masterConfig.TopicOfCanData && (len(masterConfig.RuleOfCanData1) > 0 || len(masterConfig.RuleOfCanData2) > 0) {
  191. subscribers[i], err = goroslib.NewSubscriber(goroslib.SubscriberConf{
  192. Node: commonConfig.RosNode,
  193. Topic: topic,
  194. Callback: func(data *pjisuv_msgs.Frame) {
  195. subscribersTimeMutexes[i].Lock()
  196. if time.Since(subscribersTimes[i]).Seconds() > triggerInterval {
  197. subscribersMutexes[i].Lock()
  198. faultHappenTime := util.GetNowTimeCustom() // 获取当前故障发生时间
  199. lastTimeWindow := commonEntity.GetLastTimeWindow() // 获取最后一个时间窗口
  200. faultLabel := ""
  201. if len(masterConfig.RuleOfCanData1) > 0 {
  202. for _, f := range masterConfig.RuleOfCanData1 {
  203. faultLabel = f(data)
  204. if faultLabel != "" {
  205. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  206. subscribersTimes[i] = time.Now()
  207. goto TriggerSuccess
  208. }
  209. }
  210. }
  211. if len(masterConfig.RuleOfCanData2) > 0 {
  212. for _, f := range masterConfig.RuleOfCanData2 {
  213. faultLabel = f(data, &pjisuvParam)
  214. if faultLabel != "" {
  215. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  216. subscribersTimes[i] = time.Now()
  217. goto TriggerSuccess
  218. }
  219. }
  220. }
  221. TriggerSuccess:
  222. subscribersMutexes[i].Unlock()
  223. }
  224. subscribersTimeMutexes[i].Unlock()
  225. },
  226. })
  227. }
  228. // 5
  229. if topic == masterConfig.TopicOfCh128x1LslidarPointCloud && (len(masterConfig.RuleOfCh128x1LslidarPointCloud1) > 0 || len(masterConfig.RuleOfCh128x1LslidarPointCloud2) > 0) {
  230. subscribers[i], err = goroslib.NewSubscriber(goroslib.SubscriberConf{
  231. Node: commonConfig.RosNode,
  232. Topic: topic,
  233. Callback: func(data *sensor_msgs.PointCloud2) {
  234. subscribersTimeMutexes[i].Lock()
  235. if time.Since(subscribersTimes[i]).Seconds() > triggerInterval {
  236. subscribersMutexes[i].Lock()
  237. faultHappenTime := util.GetNowTimeCustom() // 获取当前故障发生时间
  238. lastTimeWindow := commonEntity.GetLastTimeWindow() // 获取最后一个时间窗口
  239. faultLabel := ""
  240. if len(masterConfig.RuleOfCh128x1LslidarPointCloud1) > 0 {
  241. for _, f := range masterConfig.RuleOfCh128x1LslidarPointCloud1 {
  242. faultLabel = f(data)
  243. if faultLabel != "" {
  244. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  245. subscribersTimes[i] = time.Now()
  246. goto TriggerSuccess
  247. }
  248. }
  249. }
  250. if len(masterConfig.RuleOfCh128x1LslidarPointCloud2) > 0 {
  251. for _, f := range masterConfig.RuleOfCh128x1LslidarPointCloud2 {
  252. faultLabel = f(data, &pjisuvParam)
  253. if faultLabel != "" {
  254. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  255. subscribersTimes[i] = time.Now()
  256. goto TriggerSuccess
  257. }
  258. }
  259. }
  260. TriggerSuccess:
  261. subscribersMutexes[i].Unlock()
  262. }
  263. subscribersTimeMutexes[i].Unlock()
  264. },
  265. })
  266. }
  267. // 6
  268. if topic == masterConfig.TopicOfCh64wLLslidarPointCloud && (len(masterConfig.RuleOfCh64wLLslidarPointCloud1) > 0 || len(masterConfig.RuleOfCh64wLLslidarPointCloud2) > 1) {
  269. subscribers[i], err = goroslib.NewSubscriber(goroslib.SubscriberConf{
  270. Node: commonConfig.RosNode,
  271. Topic: topic,
  272. Callback: func(data *sensor_msgs.PointCloud2) {
  273. subscribersTimeMutexes[i].Lock()
  274. if time.Since(subscribersTimes[i]).Seconds() > triggerInterval {
  275. subscribersMutexes[i].Lock()
  276. faultHappenTime := util.GetNowTimeCustom() // 获取当前故障发生时间
  277. lastTimeWindow := commonEntity.GetLastTimeWindow() // 获取最后一个时间窗口
  278. faultLabel := ""
  279. if len(masterConfig.RuleOfCh64wLLslidarPointCloud1) > 0 {
  280. for _, f := range masterConfig.RuleOfCh64wLLslidarPointCloud1 {
  281. faultLabel = f(data)
  282. if faultLabel != "" {
  283. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  284. subscribersTimes[i] = time.Now()
  285. goto TriggerSuccess
  286. }
  287. }
  288. }
  289. if len(masterConfig.RuleOfCh64wLLslidarPointCloud2) > 0 {
  290. for _, f := range masterConfig.RuleOfCh64wLLslidarPointCloud2 {
  291. faultLabel = f(data, &pjisuvParam)
  292. if faultLabel != "" {
  293. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  294. subscribersTimes[i] = time.Now()
  295. goto TriggerSuccess
  296. }
  297. }
  298. }
  299. TriggerSuccess:
  300. subscribersMutexes[i].Unlock()
  301. }
  302. subscribersTimeMutexes[i].Unlock()
  303. },
  304. })
  305. }
  306. // 7
  307. if topic == masterConfig.TopicOfCh64wLScan && (len(masterConfig.RuleOfCh64wLScan1) > 0 || len(masterConfig.RuleOfCh64wLScan2) > 0) {
  308. subscribers[i], err = goroslib.NewSubscriber(goroslib.SubscriberConf{
  309. Node: commonConfig.RosNode,
  310. Topic: topic,
  311. Callback: func(data *sensor_msgs.LaserScan) {
  312. subscribersTimeMutexes[i].Lock()
  313. if time.Since(subscribersTimes[i]).Seconds() > triggerInterval {
  314. subscribersMutexes[i].Lock()
  315. faultHappenTime := util.GetNowTimeCustom() // 获取当前故障发生时间
  316. lastTimeWindow := commonEntity.GetLastTimeWindow() // 获取最后一个时间窗口
  317. faultLabel := ""
  318. if len(masterConfig.RuleOfCh64wLScan1) > 0 {
  319. for _, f := range masterConfig.RuleOfCh64wLScan1 {
  320. faultLabel = f(data)
  321. if faultLabel != "" {
  322. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  323. subscribersTimes[i] = time.Now()
  324. goto TriggerSuccess
  325. }
  326. }
  327. }
  328. if len(masterConfig.RuleOfCh64wLScan2) > 0 {
  329. for _, f := range masterConfig.RuleOfCh64wLScan2 {
  330. faultLabel = f(data, &pjisuvParam)
  331. if faultLabel != "" {
  332. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  333. subscribersTimes[i] = time.Now()
  334. goto TriggerSuccess
  335. }
  336. }
  337. }
  338. TriggerSuccess:
  339. subscribersMutexes[i].Unlock()
  340. }
  341. subscribersTimeMutexes[i].Unlock()
  342. },
  343. })
  344. }
  345. // 8
  346. if topic == masterConfig.TopicOfCh64wRLslidarPointCloud && (len(masterConfig.RuleOfCh64wRLslidarPointCloud1) > 0 || len(masterConfig.RuleOfCh64wRLslidarPointCloud2) > 0) {
  347. subscribers[i], err = goroslib.NewSubscriber(goroslib.SubscriberConf{
  348. Node: commonConfig.RosNode,
  349. Topic: topic,
  350. Callback: func(data *sensor_msgs.PointCloud2) {
  351. subscribersTimeMutexes[i].Lock()
  352. if time.Since(subscribersTimes[i]).Seconds() > triggerInterval {
  353. subscribersMutexes[i].Lock()
  354. faultHappenTime := util.GetNowTimeCustom() // 获取当前故障发生时间
  355. lastTimeWindow := commonEntity.GetLastTimeWindow() // 获取最后一个时间窗口
  356. faultLabel := ""
  357. if len(masterConfig.RuleOfCh64wRLslidarPointCloud1) > 0 {
  358. for _, f := range masterConfig.RuleOfCh64wRLslidarPointCloud1 {
  359. faultLabel = f(data)
  360. if faultLabel != "" {
  361. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  362. subscribersTimes[i] = time.Now()
  363. goto TriggerSuccess
  364. }
  365. }
  366. }
  367. if len(masterConfig.RuleOfCh64wRLslidarPointCloud2) > 0 {
  368. for _, f := range masterConfig.RuleOfCh64wRLslidarPointCloud2 {
  369. faultLabel = f(data, &pjisuvParam)
  370. if faultLabel != "" {
  371. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  372. subscribersTimes[i] = time.Now()
  373. goto TriggerSuccess
  374. }
  375. }
  376. }
  377. TriggerSuccess:
  378. subscribersMutexes[i].Unlock()
  379. }
  380. subscribersTimeMutexes[i].Unlock()
  381. },
  382. })
  383. }
  384. // 9
  385. if topic == masterConfig.TopicOfCh64wRScan && (len(masterConfig.RuleOfCh64wRScan1) > 0 || len(masterConfig.RuleOfCh64wRScan2) > 0) {
  386. subscribers[i], err = goroslib.NewSubscriber(goroslib.SubscriberConf{
  387. Node: commonConfig.RosNode,
  388. Topic: topic,
  389. Callback: func(data *sensor_msgs.LaserScan) {
  390. subscribersTimeMutexes[i].Lock()
  391. if time.Since(subscribersTimes[i]).Seconds() > triggerInterval {
  392. subscribersMutexes[i].Lock()
  393. faultHappenTime := util.GetNowTimeCustom() // 获取当前故障发生时间
  394. lastTimeWindow := commonEntity.GetLastTimeWindow() // 获取最后一个时间窗口
  395. faultLabel := ""
  396. if len(masterConfig.RuleOfCh64wRScan1) > 0 {
  397. for _, f := range masterConfig.RuleOfCh64wRScan1 {
  398. faultLabel = f(data)
  399. if faultLabel != "" {
  400. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  401. subscribersTimes[i] = time.Now()
  402. goto TriggerSuccess
  403. }
  404. }
  405. }
  406. if len(masterConfig.RuleOfCh64wRScan2) > 0 {
  407. for _, f := range masterConfig.RuleOfCh64wRScan2 {
  408. faultLabel = f(data, &pjisuvParam)
  409. if faultLabel != "" {
  410. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  411. subscribersTimes[i] = time.Now()
  412. goto TriggerSuccess
  413. }
  414. }
  415. }
  416. TriggerSuccess:
  417. subscribersMutexes[i].Unlock()
  418. }
  419. subscribersTimeMutexes[i].Unlock()
  420. },
  421. })
  422. }
  423. // 10
  424. if topic == masterConfig.TopicOfCicvLidarclusterMovingObjects && (len(masterConfig.RuleOfCicvLidarclusterMovingObjects1) > 0 || len(masterConfig.RuleOfCicvLidarclusterMovingObjects2) > 0) {
  425. subscribers[i], err = goroslib.NewSubscriber(goroslib.SubscriberConf{
  426. Node: commonConfig.RosNode,
  427. Topic: topic,
  428. Callback: func(data *pjisuv_msgs.PerceptionCicvMovingObjects) {
  429. subscribersTimeMutexes[i].Lock()
  430. if time.Since(subscribersTimes[i]).Seconds() > triggerInterval {
  431. subscribersMutexes[i].Lock()
  432. faultHappenTime := util.GetNowTimeCustom() // 获取当前故障发生时间
  433. lastTimeWindow := commonEntity.GetLastTimeWindow() // 获取最后一个时间窗口
  434. faultLabel := ""
  435. if len(masterConfig.RuleOfCicvLidarclusterMovingObjects1) > 0 {
  436. for _, f := range masterConfig.RuleOfCicvLidarclusterMovingObjects1 {
  437. faultLabel = f(data)
  438. if faultLabel != "" {
  439. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  440. subscribersTimes[i] = time.Now()
  441. goto TriggerSuccess
  442. }
  443. }
  444. }
  445. if len(masterConfig.RuleOfCicvLidarclusterMovingObjects2) > 0 {
  446. for _, f := range masterConfig.RuleOfCicvLidarclusterMovingObjects2 {
  447. faultLabel = f(data, &pjisuvParam)
  448. if faultLabel != "" {
  449. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  450. subscribersTimes[i] = time.Now()
  451. goto TriggerSuccess
  452. }
  453. }
  454. }
  455. TriggerSuccess:
  456. subscribersMutexes[i].Unlock()
  457. }
  458. subscribersTimeMutexes[i].Unlock()
  459. },
  460. })
  461. }
  462. // 11 有共享变量的订阅者必须被创建
  463. if topic == masterConfig.TopicOfCicvAmrTrajectory {
  464. subscribers[i], err = goroslib.NewSubscriber(goroslib.SubscriberConf{
  465. Node: commonConfig.RosNode,
  466. Topic: topic,
  467. Callback: func(data *pjisuv_msgs.Trajectory) {
  468. subscribersTimeMutexes[i].Lock()
  469. if time.Since(subscribersTimes[i]).Seconds() > triggerInterval {
  470. subscribersMutexes[i].Lock()
  471. faultHappenTime := util.GetNowTimeCustom() // 获取当前故障发生时间
  472. lastTimeWindow := commonEntity.GetLastTimeWindow() // 获取最后一个时间窗口
  473. faultLabel := ""
  474. if len(masterConfig.RuleOfCicvAmrTrajectory1) > 0 {
  475. for _, f := range masterConfig.RuleOfCicvAmrTrajectory1 {
  476. faultLabel = f(data)
  477. if faultLabel != "" {
  478. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  479. subscribersTimes[i] = time.Now()
  480. goto TriggerSuccess
  481. }
  482. }
  483. }
  484. if len(masterConfig.RuleOfCicvAmrTrajectory2) > 0 {
  485. for _, f := range masterConfig.RuleOfCicvAmrTrajectory2 {
  486. faultLabel = f(data, &pjisuvParam)
  487. if faultLabel != "" {
  488. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  489. subscribersTimes[i] = time.Now()
  490. goto TriggerSuccess
  491. }
  492. }
  493. }
  494. TriggerSuccess:
  495. subscribersMutexes[i].Unlock()
  496. }
  497. subscribersTimeMutexes[i].Unlock()
  498. // 触发后更新共享变量
  499. mutexOfCicvAmrTrajectory.RLock()
  500. {
  501. var currentCurvateres []float64
  502. for _, point := range data.Trajectoryinfo.Trajectorypoints {
  503. currentCurvateres = append(currentCurvateres, math.Abs(float64(point.Curvature)))
  504. }
  505. pjisuvParam.LastCurvaturesOfCicvAmrTrajectory = currentCurvateres
  506. pjisuvParam.DecisionType = data.Trajectoryinfo.DecisionType
  507. }
  508. mutexOfCicvAmrTrajectory.RUnlock()
  509. },
  510. })
  511. }
  512. // 12 有共享变量的订阅者必须被创建
  513. if topic == masterConfig.TopicOfCicvLocation {
  514. subscribers[i], err = goroslib.NewSubscriber(goroslib.SubscriberConf{
  515. Node: commonConfig.RosNode,
  516. Topic: topic,
  517. Callback: func(data *pjisuv_msgs.PerceptionLocalization) {
  518. // 更新共享变量
  519. mutexOfCicvLocation.RLock()
  520. {
  521. pjisuvParam.VelocityXOfCicvLocation = data.VelocityX
  522. pjisuvParam.VelocityYOfCicvLocation = data.VelocityY
  523. pjisuvParam.VelocityZOfCicvLocation = data.VelocityZ
  524. pjisuvParam.YawOfCicvLocation = data.Yaw
  525. pjisuvParam.AngularVelocityZOfCicvLocation = data.AngularVelocityZ
  526. pjisuvParam.PositionXOfCicvLocation = data.PositionX
  527. pjisuvParam.PositionYOfCicvLocation = data.PositionY
  528. }
  529. mutexOfCicvLocation.RUnlock()
  530. subscribersTimeMutexes[i].Lock()
  531. if time.Since(subscribersTimes[i]).Seconds() > triggerInterval {
  532. subscribersMutexes[i].Lock()
  533. faultHappenTime := util.GetNowTimeCustom() // 获取当前故障发生时间
  534. lastTimeWindow := commonEntity.GetLastTimeWindow() // 获取最后一个时间窗口
  535. faultLabel := ""
  536. if len(masterConfig.RuleOfCicvLocation1) > 0 {
  537. for _, f := range masterConfig.RuleOfCicvLocation1 {
  538. faultLabel = f(data)
  539. if faultLabel != "" {
  540. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  541. subscribersTimes[i] = time.Now()
  542. goto TriggerSuccess
  543. }
  544. }
  545. }
  546. if len(masterConfig.RuleOfCicvLocation2) > 0 {
  547. for _, f := range masterConfig.RuleOfCicvLocation2 {
  548. faultLabel = f(data, &pjisuvParam)
  549. if faultLabel != "" {
  550. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  551. subscribersTimes[i] = time.Now()
  552. goto TriggerSuccess
  553. }
  554. }
  555. }
  556. TriggerSuccess:
  557. subscribersMutexes[i].Unlock()
  558. }
  559. subscribersTimeMutexes[i].Unlock()
  560. },
  561. })
  562. }
  563. // 13
  564. if topic == masterConfig.TopicOfCloudClusters && (len(masterConfig.RuleOfCloudClusters1) > 0 || len(masterConfig.RuleOfCloudClusters2) > 0) {
  565. subscribers[i], err = goroslib.NewSubscriber(goroslib.SubscriberConf{
  566. Node: commonConfig.RosNode,
  567. Topic: topic,
  568. Callback: func(data *pjisuv_msgs.AutowareCloudClusterArray) {
  569. subscribersTimeMutexes[i].Lock()
  570. if time.Since(subscribersTimes[i]).Seconds() > triggerInterval {
  571. subscribersMutexes[i].Lock()
  572. faultHappenTime := util.GetNowTimeCustom() // 获取当前故障发生时间
  573. lastTimeWindow := commonEntity.GetLastTimeWindow() // 获取最后一个时间窗口
  574. faultLabel := ""
  575. if len(masterConfig.RuleOfCloudClusters1) > 0 {
  576. for _, f := range masterConfig.RuleOfCloudClusters1 {
  577. faultLabel = f(data)
  578. if faultLabel != "" {
  579. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  580. subscribersTimes[i] = time.Now()
  581. goto TriggerSuccess
  582. }
  583. }
  584. }
  585. if len(masterConfig.RuleOfCloudClusters2) > 0 {
  586. for _, f := range masterConfig.RuleOfCloudClusters2 {
  587. faultLabel = f(data, &pjisuvParam)
  588. if faultLabel != "" {
  589. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  590. subscribersTimes[i] = time.Now()
  591. goto TriggerSuccess
  592. }
  593. }
  594. }
  595. TriggerSuccess:
  596. subscribersMutexes[i].Unlock()
  597. }
  598. subscribersTimeMutexes[i].Unlock()
  599. },
  600. })
  601. }
  602. // 14
  603. if topic == masterConfig.TopicOfHeartbeatInfo && (len(masterConfig.RuleOfHeartbeatInfo1) > 0 || len(masterConfig.RuleOfHeartbeatInfo2) > 0) {
  604. subscribers[i], err = goroslib.NewSubscriber(goroslib.SubscriberConf{
  605. Node: commonConfig.RosNode,
  606. Topic: topic,
  607. Callback: func(data *pjisuv_msgs.HeartBeatInfo) {
  608. subscribersTimeMutexes[i].Lock()
  609. if time.Since(subscribersTimes[i]).Seconds() > triggerInterval {
  610. subscribersMutexes[i].Lock()
  611. faultHappenTime := util.GetNowTimeCustom() // 获取当前故障发生时间
  612. lastTimeWindow := commonEntity.GetLastTimeWindow() // 获取最后一个时间窗口
  613. faultLabel := ""
  614. if len(masterConfig.RuleOfHeartbeatInfo1) > 0 {
  615. for _, f := range masterConfig.RuleOfHeartbeatInfo1 {
  616. faultLabel = f(data)
  617. if faultLabel != "" {
  618. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  619. subscribersTimes[i] = time.Now()
  620. goto TriggerSuccess
  621. }
  622. }
  623. }
  624. if len(masterConfig.RuleOfHeartbeatInfo2) > 0 {
  625. for _, f := range masterConfig.RuleOfHeartbeatInfo2 {
  626. faultLabel = f(data, &pjisuvParam)
  627. if faultLabel != "" {
  628. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  629. subscribersTimes[i] = time.Now()
  630. goto TriggerSuccess
  631. }
  632. }
  633. }
  634. TriggerSuccess:
  635. subscribersMutexes[i].Unlock()
  636. }
  637. subscribersTimeMutexes[i].Unlock()
  638. },
  639. })
  640. }
  641. // 15
  642. if topic == masterConfig.TopicOfLidarPretreatmentCost && (len(masterConfig.RuleOfLidarPretreatmentCost1) > 0 || len(masterConfig.RuleOfLidarPretreatmentCost2) > 0) {
  643. subscribers[i], err = goroslib.NewSubscriber(goroslib.SubscriberConf{
  644. Node: commonConfig.RosNode,
  645. Topic: topic,
  646. Callback: func(data *geometry_msgs.Vector3Stamped) {
  647. subscribersTimeMutexes[i].Lock()
  648. if time.Since(subscribersTimes[i]).Seconds() > triggerInterval {
  649. subscribersMutexes[i].Lock()
  650. faultHappenTime := util.GetNowTimeCustom() // 获取当前故障发生时间
  651. lastTimeWindow := commonEntity.GetLastTimeWindow() // 获取最后一个时间窗口
  652. faultLabel := ""
  653. if len(masterConfig.RuleOfLidarPretreatmentCost1) > 0 {
  654. for _, f := range masterConfig.RuleOfLidarPretreatmentCost1 {
  655. faultLabel = f(data)
  656. if faultLabel != "" {
  657. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  658. subscribersTimes[i] = time.Now()
  659. goto TriggerSuccess
  660. }
  661. }
  662. }
  663. if len(masterConfig.RuleOfLidarPretreatmentCost2) > 0 {
  664. for _, f := range masterConfig.RuleOfLidarPretreatmentCost2 {
  665. faultLabel = f(data, &pjisuvParam)
  666. if faultLabel != "" {
  667. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  668. subscribersTimes[i] = time.Now()
  669. goto TriggerSuccess
  670. }
  671. }
  672. }
  673. TriggerSuccess:
  674. subscribersMutexes[i].Unlock()
  675. }
  676. subscribersTimeMutexes[i].Unlock()
  677. },
  678. })
  679. }
  680. // 16
  681. if topic == masterConfig.TopicOfLidarPretreatmentOdometry && (len(masterConfig.RuleOfLidarPretreatmentOdometry1) > 0 || len(masterConfig.RuleOfLidarPretreatmentOdometry2) > 0) {
  682. subscribers[i], err = goroslib.NewSubscriber(goroslib.SubscriberConf{
  683. Node: commonConfig.RosNode,
  684. Topic: topic,
  685. Callback: func(data *nav_msgs.Odometry) {
  686. subscribersTimeMutexes[i].Lock()
  687. if time.Since(subscribersTimes[i]).Seconds() > triggerInterval {
  688. subscribersMutexes[i].Lock()
  689. faultHappenTime := util.GetNowTimeCustom() // 获取当前故障发生时间
  690. lastTimeWindow := commonEntity.GetLastTimeWindow() // 获取最后一个时间窗口
  691. faultLabel := ""
  692. if len(masterConfig.RuleOfLidarPretreatmentOdometry1) > 0 {
  693. for _, f := range masterConfig.RuleOfLidarPretreatmentOdometry1 {
  694. faultLabel = f(data)
  695. if faultLabel != "" {
  696. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  697. subscribersTimes[i] = time.Now()
  698. goto TriggerSuccess
  699. }
  700. }
  701. }
  702. if len(masterConfig.RuleOfLidarPretreatmentOdometry2) > 0 {
  703. for _, f := range masterConfig.RuleOfLidarPretreatmentOdometry2 {
  704. faultLabel = f(data, &pjisuvParam)
  705. if faultLabel != "" {
  706. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  707. subscribersTimes[i] = time.Now()
  708. goto TriggerSuccess
  709. }
  710. }
  711. }
  712. TriggerSuccess:
  713. subscribersMutexes[i].Unlock()
  714. }
  715. subscribersTimeMutexes[i].Unlock()
  716. },
  717. })
  718. }
  719. // 17
  720. if topic == masterConfig.TopicOfLidarRoi && (len(masterConfig.RuleOfLidarRoi1) > 0 || len(masterConfig.RuleOfLidarRoi2) > 0) {
  721. subscribers[i], err = goroslib.NewSubscriber(goroslib.SubscriberConf{
  722. Node: commonConfig.RosNode,
  723. Topic: topic,
  724. Callback: func(data *geometry_msgs.PolygonStamped) {
  725. subscribersTimeMutexes[i].Lock()
  726. if time.Since(subscribersTimes[i]).Seconds() > triggerInterval {
  727. subscribersMutexes[i].Lock()
  728. faultHappenTime := util.GetNowTimeCustom() // 获取当前故障发生时间
  729. lastTimeWindow := commonEntity.GetLastTimeWindow() // 获取最后一个时间窗口
  730. faultLabel := ""
  731. if len(masterConfig.RuleOfLidarRoi1) > 0 {
  732. for _, f := range masterConfig.RuleOfLidarRoi1 {
  733. faultLabel = f(data)
  734. if faultLabel != "" {
  735. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  736. subscribersTimes[i] = time.Now()
  737. goto TriggerSuccess
  738. }
  739. }
  740. }
  741. if len(masterConfig.RuleOfLidarRoi2) > 0 {
  742. for _, f := range masterConfig.RuleOfLidarRoi2 {
  743. faultLabel = f(data, &pjisuvParam)
  744. if faultLabel != "" {
  745. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  746. subscribersTimes[i] = time.Now()
  747. goto TriggerSuccess
  748. }
  749. }
  750. }
  751. TriggerSuccess:
  752. subscribersMutexes[i].Unlock()
  753. }
  754. subscribersTimeMutexes[i].Unlock()
  755. },
  756. })
  757. }
  758. // 18
  759. if topic == masterConfig.TopicOfLine1 && (len(masterConfig.RuleOfLine11) > 0 || len(masterConfig.RuleOfLine12) > 0) {
  760. subscribers[i], err = goroslib.NewSubscriber(goroslib.SubscriberConf{
  761. Node: commonConfig.RosNode,
  762. Topic: topic,
  763. Callback: func(data *nav_msgs.Path) {
  764. subscribersTimeMutexes[i].Lock()
  765. if time.Since(subscribersTimes[i]).Seconds() > triggerInterval {
  766. subscribersMutexes[i].Lock()
  767. faultHappenTime := util.GetNowTimeCustom() // 获取当前故障发生时间
  768. lastTimeWindow := commonEntity.GetLastTimeWindow() // 获取最后一个时间窗口
  769. faultLabel := ""
  770. if len(masterConfig.RuleOfLine11) > 0 {
  771. for _, f := range masterConfig.RuleOfLine11 {
  772. faultLabel = f(data)
  773. if faultLabel != "" {
  774. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  775. subscribersTimes[i] = time.Now()
  776. goto TriggerSuccess
  777. }
  778. }
  779. }
  780. if len(masterConfig.RuleOfLine12) > 0 {
  781. for _, f := range masterConfig.RuleOfLine12 {
  782. faultLabel = f(data, &pjisuvParam)
  783. if faultLabel != "" {
  784. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  785. subscribersTimes[i] = time.Now()
  786. goto TriggerSuccess
  787. }
  788. }
  789. }
  790. TriggerSuccess:
  791. subscribersMutexes[i].Unlock()
  792. }
  793. subscribersTimeMutexes[i].Unlock()
  794. },
  795. })
  796. }
  797. // 19
  798. if topic == masterConfig.TopicOfLine2 && (len(masterConfig.RuleOfLine21) > 0 || len(masterConfig.RuleOfLine22) > 0) {
  799. subscribers[i], err = goroslib.NewSubscriber(goroslib.SubscriberConf{
  800. Node: commonConfig.RosNode,
  801. Topic: topic,
  802. Callback: func(data *nav_msgs.Path) {
  803. subscribersTimeMutexes[i].Lock()
  804. if time.Since(subscribersTimes[i]).Seconds() > triggerInterval {
  805. subscribersMutexes[i].Lock()
  806. faultHappenTime := util.GetNowTimeCustom() // 获取当前故障发生时间
  807. lastTimeWindow := commonEntity.GetLastTimeWindow() // 获取最后一个时间窗口
  808. faultLabel := ""
  809. if len(masterConfig.RuleOfLine21) > 0 {
  810. for _, f := range masterConfig.RuleOfLine21 {
  811. faultLabel = f(data)
  812. if faultLabel != "" {
  813. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  814. subscribersTimes[i] = time.Now()
  815. goto TriggerSuccess
  816. }
  817. }
  818. }
  819. if len(masterConfig.RuleOfLine22) > 0 {
  820. for _, f := range masterConfig.RuleOfLine22 {
  821. faultLabel = f(data, &pjisuvParam)
  822. if faultLabel != "" {
  823. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  824. subscribersTimes[i] = time.Now()
  825. goto TriggerSuccess
  826. }
  827. }
  828. }
  829. TriggerSuccess:
  830. subscribersMutexes[i].Unlock()
  831. }
  832. subscribersTimeMutexes[i].Unlock()
  833. },
  834. })
  835. }
  836. // 20
  837. if topic == masterConfig.TopicOfMapPolygon && (len(masterConfig.RuleOfMapPolygon1) > 0 || len(masterConfig.RuleOfMapPolygon2) > 0) {
  838. subscribers[i], err = goroslib.NewSubscriber(goroslib.SubscriberConf{
  839. Node: commonConfig.RosNode,
  840. Topic: topic,
  841. Callback: func(data *pjisuv_msgs.PolygonStamped) {
  842. subscribersTimeMutexes[i].Lock()
  843. if time.Since(subscribersTimes[i]).Seconds() > triggerInterval {
  844. subscribersMutexes[i].Lock()
  845. faultHappenTime := util.GetNowTimeCustom() // 获取当前故障发生时间
  846. lastTimeWindow := commonEntity.GetLastTimeWindow() // 获取最后一个时间窗口
  847. faultLabel := ""
  848. if len(masterConfig.RuleOfMapPolygon1) > 0 {
  849. for _, f := range masterConfig.RuleOfMapPolygon1 {
  850. faultLabel = f(data)
  851. if faultLabel != "" {
  852. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  853. subscribersTimes[i] = time.Now()
  854. goto TriggerSuccess
  855. }
  856. }
  857. }
  858. if len(masterConfig.RuleOfMapPolygon2) > 0 {
  859. for _, f := range masterConfig.RuleOfMapPolygon2 {
  860. faultLabel = f(data, &pjisuvParam)
  861. if faultLabel != "" {
  862. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  863. subscribersTimes[i] = time.Now()
  864. goto TriggerSuccess
  865. }
  866. }
  867. }
  868. TriggerSuccess:
  869. subscribersMutexes[i].Unlock()
  870. }
  871. subscribersTimeMutexes[i].Unlock()
  872. },
  873. })
  874. }
  875. // 21
  876. if topic == masterConfig.TopicOfObstacleDisplay && (len(masterConfig.RuleOfObstacleDisplay1) > 0 || len(masterConfig.RuleOfObstacleDisplay2) > 0) {
  877. subscribers[i], err = goroslib.NewSubscriber(goroslib.SubscriberConf{
  878. Node: commonConfig.RosNode,
  879. Topic: topic,
  880. Callback: func(data *visualization_msgs.MarkerArray) {
  881. subscribersTimeMutexes[i].Lock()
  882. if time.Since(subscribersTimes[i]).Seconds() > triggerInterval {
  883. subscribersMutexes[i].Lock()
  884. faultHappenTime := util.GetNowTimeCustom() // 获取当前故障发生时间
  885. lastTimeWindow := commonEntity.GetLastTimeWindow() // 获取最后一个时间窗口
  886. faultLabel := ""
  887. if len(masterConfig.RuleOfObstacleDisplay1) > 0 {
  888. for _, f := range masterConfig.RuleOfObstacleDisplay1 {
  889. faultLabel = f(data)
  890. if faultLabel != "" {
  891. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  892. subscribersTimes[i] = time.Now()
  893. goto TriggerSuccess
  894. }
  895. }
  896. }
  897. if len(masterConfig.RuleOfObstacleDisplay2) > 0 {
  898. for _, f := range masterConfig.RuleOfObstacleDisplay2 {
  899. faultLabel = f(data, &pjisuvParam)
  900. if faultLabel != "" {
  901. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  902. subscribersTimes[i] = time.Now()
  903. goto TriggerSuccess
  904. }
  905. }
  906. }
  907. TriggerSuccess:
  908. subscribersMutexes[i].Unlock()
  909. }
  910. subscribersTimeMutexes[i].Unlock()
  911. },
  912. })
  913. }
  914. // 22 有共享变量的订阅者必须被创建
  915. if topic == masterConfig.TopicOfPjControlPub {
  916. subscribers[i], err = goroslib.NewSubscriber(goroslib.SubscriberConf{
  917. Node: commonConfig.RosNode,
  918. Topic: topic,
  919. Callback: func(data *pjisuv_msgs.CommonVehicleCmd) {
  920. // 更新共享变量
  921. mutexOfPjControlPub.RLock()
  922. {
  923. pjisuvParam.NumCountPjiControlCommandOfPjControlPub++
  924. if pjisuvParam.NumCountPjiControlCommandOfPjControlPub == 10 {
  925. pjisuvParam.EgoSteeringCmdOfPjControlPub = append(pjisuvParam.EgoSteeringCmdOfPjControlPub, data.ICPVCmdStrAngle)
  926. pjisuvParam.EgoThrottleCmdOfPjControlPub = append(pjisuvParam.EgoThrottleCmdOfPjControlPub, data.ICPVCmdAccPelPosAct)
  927. pjisuvParam.NumCountPjiControlCommandOfPjControlPub = 0
  928. }
  929. }
  930. mutexOfPjControlPub.RUnlock()
  931. subscribersTimeMutexes[i].Lock()
  932. if time.Since(subscribersTimes[i]).Seconds() > triggerInterval {
  933. subscribersMutexes[i].Lock()
  934. faultHappenTime := util.GetNowTimeCustom() // 获取当前故障发生时间
  935. lastTimeWindow := commonEntity.GetLastTimeWindow() // 获取最后一个时间窗口
  936. faultLabel := ""
  937. if len(masterConfig.RuleOfPjControlPub1) > 0 {
  938. for _, f := range masterConfig.RuleOfPjControlPub1 {
  939. faultLabel = f(data)
  940. if faultLabel != "" {
  941. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  942. subscribersTimes[i] = time.Now()
  943. goto TriggerSuccess
  944. }
  945. }
  946. }
  947. if len(masterConfig.RuleOfPjControlPub2) > 0 {
  948. for _, f := range masterConfig.RuleOfPjControlPub2 {
  949. faultLabel = f(data, &pjisuvParam)
  950. if faultLabel != "" {
  951. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  952. subscribersTimes[i] = time.Now()
  953. goto TriggerSuccess
  954. }
  955. }
  956. }
  957. TriggerSuccess:
  958. subscribersMutexes[i].Unlock()
  959. }
  960. subscribersTimeMutexes[i].Unlock()
  961. },
  962. })
  963. }
  964. // 23
  965. if topic == masterConfig.TopicOfPointsCluster && (len(masterConfig.RuleOfPointsCluster1) > 0 || len(masterConfig.RuleOfPointsCluster2) > 0) {
  966. subscribers[i], err = goroslib.NewSubscriber(goroslib.SubscriberConf{
  967. Node: commonConfig.RosNode,
  968. Topic: topic,
  969. Callback: func(data *sensor_msgs.PointCloud2) {
  970. subscribersTimeMutexes[i].Lock()
  971. if time.Since(subscribersTimes[i]).Seconds() > triggerInterval {
  972. subscribersMutexes[i].Lock()
  973. faultHappenTime := util.GetNowTimeCustom() // 获取当前故障发生时间
  974. lastTimeWindow := commonEntity.GetLastTimeWindow() // 获取最后一个时间窗口
  975. faultLabel := ""
  976. if len(masterConfig.RuleOfPointsCluster1) > 0 {
  977. for _, f := range masterConfig.RuleOfPointsCluster1 {
  978. faultLabel = f(data)
  979. if faultLabel != "" {
  980. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  981. subscribersTimes[i] = time.Now()
  982. goto TriggerSuccess
  983. }
  984. }
  985. }
  986. if len(masterConfig.RuleOfPointsCluster2) > 0 {
  987. for _, f := range masterConfig.RuleOfPointsCluster2 {
  988. faultLabel = f(data, &pjisuvParam)
  989. if faultLabel != "" {
  990. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  991. subscribersTimes[i] = time.Now()
  992. goto TriggerSuccess
  993. }
  994. }
  995. }
  996. TriggerSuccess:
  997. subscribersMutexes[i].Unlock()
  998. }
  999. subscribersTimeMutexes[i].Unlock()
  1000. },
  1001. })
  1002. }
  1003. // 24
  1004. if topic == masterConfig.TopicOfPointsConcat && (len(masterConfig.RuleOfPointsConcat1) > 0 || len(masterConfig.RuleOfPointsConcat2) > 0) {
  1005. subscribers[i], err = goroslib.NewSubscriber(goroslib.SubscriberConf{
  1006. Node: commonConfig.RosNode,
  1007. Topic: topic,
  1008. Callback: func(data *sensor_msgs.PointCloud2) {
  1009. subscribersTimeMutexes[i].Lock()
  1010. if time.Since(subscribersTimes[i]).Seconds() > triggerInterval {
  1011. subscribersMutexes[i].Lock()
  1012. faultHappenTime := util.GetNowTimeCustom() // 获取当前故障发生时间
  1013. lastTimeWindow := commonEntity.GetLastTimeWindow() // 获取最后一个时间窗口
  1014. faultLabel := ""
  1015. if len(masterConfig.RuleOfPointsConcat1) > 0 {
  1016. for _, f := range masterConfig.RuleOfPointsConcat1 {
  1017. faultLabel = f(data)
  1018. if faultLabel != "" {
  1019. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  1020. subscribersTimes[i] = time.Now()
  1021. goto TriggerSuccess
  1022. }
  1023. }
  1024. }
  1025. if len(masterConfig.RuleOfPointsConcat2) > 0 {
  1026. for _, f := range masterConfig.RuleOfPointsConcat2 {
  1027. faultLabel = f(data, &pjisuvParam)
  1028. if faultLabel != "" {
  1029. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  1030. subscribersTimes[i] = time.Now()
  1031. goto TriggerSuccess
  1032. }
  1033. }
  1034. }
  1035. TriggerSuccess:
  1036. subscribersMutexes[i].Unlock()
  1037. }
  1038. subscribersTimeMutexes[i].Unlock()
  1039. },
  1040. })
  1041. }
  1042. // 25
  1043. if topic == masterConfig.TopicOfReferenceDisplay && (len(masterConfig.RuleOfReferenceDisplay1) > 0 || len(masterConfig.RuleOfReferenceDisplay2) > 0) {
  1044. subscribers[i], err = goroslib.NewSubscriber(goroslib.SubscriberConf{
  1045. Node: commonConfig.RosNode,
  1046. Topic: topic,
  1047. Callback: func(data *nav_msgs.Path) {
  1048. subscribersTimeMutexes[i].Lock()
  1049. if time.Since(subscribersTimes[i]).Seconds() > triggerInterval {
  1050. subscribersMutexes[i].Lock()
  1051. faultHappenTime := util.GetNowTimeCustom() // 获取当前故障发生时间
  1052. lastTimeWindow := commonEntity.GetLastTimeWindow() // 获取最后一个时间窗口
  1053. faultLabel := ""
  1054. if len(masterConfig.RuleOfReferenceDisplay1) > 0 {
  1055. for _, f := range masterConfig.RuleOfReferenceDisplay1 {
  1056. faultLabel = f(data)
  1057. if faultLabel != "" {
  1058. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  1059. subscribersTimes[i] = time.Now()
  1060. goto TriggerSuccess
  1061. }
  1062. }
  1063. }
  1064. if len(masterConfig.RuleOfReferenceDisplay2) > 0 {
  1065. for _, f := range masterConfig.RuleOfReferenceDisplay2 {
  1066. faultLabel = f(data, &pjisuvParam)
  1067. if faultLabel != "" {
  1068. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  1069. subscribersTimes[i] = time.Now()
  1070. goto TriggerSuccess
  1071. }
  1072. }
  1073. }
  1074. TriggerSuccess:
  1075. subscribersMutexes[i].Unlock()
  1076. }
  1077. subscribersTimeMutexes[i].Unlock()
  1078. },
  1079. })
  1080. }
  1081. // 26
  1082. if topic == masterConfig.TopicOfReferenceTrajectory && (len(masterConfig.RuleOfReferenceTrajectory1) > 0 || len(masterConfig.RuleOfReferenceTrajectory2) > 0) {
  1083. subscribers[i], err = goroslib.NewSubscriber(goroslib.SubscriberConf{
  1084. Node: commonConfig.RosNode,
  1085. Topic: topic,
  1086. Callback: func(data *pjisuv_msgs.Trajectory) {
  1087. subscribersTimeMutexes[i].Lock()
  1088. if time.Since(subscribersTimes[i]).Seconds() > triggerInterval {
  1089. subscribersMutexes[i].Lock()
  1090. faultHappenTime := util.GetNowTimeCustom() // 获取当前故障发生时间
  1091. lastTimeWindow := commonEntity.GetLastTimeWindow() // 获取最后一个时间窗口
  1092. faultLabel := ""
  1093. if len(masterConfig.RuleOfReferenceTrajectory1) > 0 {
  1094. for _, f := range masterConfig.RuleOfReferenceTrajectory1 {
  1095. faultLabel = f(data)
  1096. if faultLabel != "" {
  1097. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  1098. subscribersTimes[i] = time.Now()
  1099. goto TriggerSuccess
  1100. }
  1101. }
  1102. }
  1103. if len(masterConfig.RuleOfReferenceTrajectory2) > 0 {
  1104. for _, f := range masterConfig.RuleOfReferenceTrajectory2 {
  1105. faultLabel = f(data, &pjisuvParam)
  1106. if faultLabel != "" {
  1107. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  1108. subscribersTimes[i] = time.Now()
  1109. goto TriggerSuccess
  1110. }
  1111. }
  1112. }
  1113. TriggerSuccess:
  1114. subscribersMutexes[i].Unlock()
  1115. }
  1116. subscribersTimeMutexes[i].Unlock()
  1117. },
  1118. })
  1119. }
  1120. // 27
  1121. if topic == masterConfig.TopicOfRoiPoints && (len(masterConfig.RuleOfRoiPoints1) > 0 || len(masterConfig.RuleOfRoiPoints2) > 0) {
  1122. subscribers[i], err = goroslib.NewSubscriber(goroslib.SubscriberConf{
  1123. Node: commonConfig.RosNode,
  1124. Topic: topic,
  1125. Callback: func(data *sensor_msgs.PointCloud2) {
  1126. subscribersTimeMutexes[i].Lock()
  1127. if time.Since(subscribersTimes[i]).Seconds() > triggerInterval {
  1128. subscribersMutexes[i].Lock()
  1129. faultHappenTime := util.GetNowTimeCustom() // 获取当前故障发生时间
  1130. lastTimeWindow := commonEntity.GetLastTimeWindow() // 获取最后一个时间窗口
  1131. faultLabel := ""
  1132. if len(masterConfig.RuleOfRoiPoints1) > 0 {
  1133. for _, f := range masterConfig.RuleOfRoiPoints1 {
  1134. faultLabel = f(data)
  1135. if faultLabel != "" {
  1136. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  1137. subscribersTimes[i] = time.Now()
  1138. goto TriggerSuccess
  1139. }
  1140. }
  1141. }
  1142. if len(masterConfig.RuleOfRoiPoints2) > 0 {
  1143. for _, f := range masterConfig.RuleOfRoiPoints2 {
  1144. faultLabel = f(data, &pjisuvParam)
  1145. if faultLabel != "" {
  1146. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  1147. subscribersTimes[i] = time.Now()
  1148. goto TriggerSuccess
  1149. }
  1150. }
  1151. }
  1152. TriggerSuccess:
  1153. subscribersMutexes[i].Unlock()
  1154. }
  1155. subscribersTimeMutexes[i].Unlock()
  1156. },
  1157. })
  1158. }
  1159. // 28
  1160. if topic == masterConfig.TopicOfRoiPolygon && (len(masterConfig.RuleOfRoiPolygon1) > 0 || len(masterConfig.RuleOfRoiPolygon2) > 0) {
  1161. subscribers[i], err = goroslib.NewSubscriber(goroslib.SubscriberConf{
  1162. Node: commonConfig.RosNode,
  1163. Topic: topic,
  1164. Callback: func(data *nav_msgs.Path) {
  1165. subscribersTimeMutexes[i].Lock()
  1166. if time.Since(subscribersTimes[i]).Seconds() > triggerInterval {
  1167. subscribersMutexes[i].Lock()
  1168. faultHappenTime := util.GetNowTimeCustom() // 获取当前故障发生时间
  1169. lastTimeWindow := commonEntity.GetLastTimeWindow() // 获取最后一个时间窗口
  1170. faultLabel := ""
  1171. if len(masterConfig.RuleOfRoiPolygon1) > 0 {
  1172. for _, f := range masterConfig.RuleOfRoiPolygon1 {
  1173. faultLabel = f(data)
  1174. if faultLabel != "" {
  1175. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  1176. subscribersTimes[i] = time.Now()
  1177. goto TriggerSuccess
  1178. }
  1179. }
  1180. }
  1181. if len(masterConfig.RuleOfRoiPolygon2) > 0 {
  1182. for _, f := range masterConfig.RuleOfRoiPolygon2 {
  1183. faultLabel = f(data, &pjisuvParam)
  1184. if faultLabel != "" {
  1185. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  1186. subscribersTimes[i] = time.Now()
  1187. goto TriggerSuccess
  1188. }
  1189. }
  1190. }
  1191. TriggerSuccess:
  1192. subscribersMutexes[i].Unlock()
  1193. }
  1194. subscribersTimeMutexes[i].Unlock()
  1195. },
  1196. })
  1197. }
  1198. // 29
  1199. if topic == masterConfig.TopicOfTf && (len(masterConfig.RuleOfTf1) > 0 || len(masterConfig.RuleOfTf2) > 0) {
  1200. subscribers[i], err = goroslib.NewSubscriber(goroslib.SubscriberConf{
  1201. Node: commonConfig.RosNode,
  1202. Topic: topic,
  1203. Callback: func(data *tf2_msgs.TFMessage) {
  1204. subscribersTimeMutexes[i].Lock()
  1205. if time.Since(subscribersTimes[i]).Seconds() > triggerInterval {
  1206. subscribersMutexes[i].Lock()
  1207. faultHappenTime := util.GetNowTimeCustom() // 获取当前故障发生时间
  1208. lastTimeWindow := commonEntity.GetLastTimeWindow() // 获取最后一个时间窗口
  1209. faultLabel := ""
  1210. if len(masterConfig.RuleOfTf1) > 0 {
  1211. for _, f := range masterConfig.RuleOfTf1 {
  1212. faultLabel = f(data)
  1213. if faultLabel != "" {
  1214. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  1215. subscribersTimes[i] = time.Now()
  1216. goto TriggerSuccess
  1217. }
  1218. }
  1219. }
  1220. if len(masterConfig.RuleOfTf2) > 0 {
  1221. for _, f := range masterConfig.RuleOfTf2 {
  1222. faultLabel = f(data, &pjisuvParam)
  1223. if faultLabel != "" {
  1224. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  1225. subscribersTimes[i] = time.Now()
  1226. goto TriggerSuccess
  1227. }
  1228. }
  1229. }
  1230. TriggerSuccess:
  1231. subscribersMutexes[i].Unlock()
  1232. }
  1233. subscribersTimeMutexes[i].Unlock()
  1234. },
  1235. })
  1236. }
  1237. // 30 有共享变量的订阅者必须被创建
  1238. if topic == masterConfig.TopicOfTpperception {
  1239. subscribers[i], err = goroslib.NewSubscriber(goroslib.SubscriberConf{
  1240. Node: commonConfig.RosNode,
  1241. Topic: topic,
  1242. Callback: func(data *pjisuv_msgs.PerceptionObjects) {
  1243. // 更新共享变量
  1244. mutexOfTpperception.RLock()
  1245. {
  1246. for _, obj := range data.Objs {
  1247. if obj.X <= 5 || math.Abs(float64(obj.Y)) >= 10 {
  1248. continue
  1249. }
  1250. // 检查 ObjDicOfTpperception 是否为 nil,如果是,则初始化它
  1251. if pjisuvParam.ObjDicOfTpperception == nil {
  1252. pjisuvParam.ObjDicOfTpperception = make(map[uint32][]float32)
  1253. }
  1254. if _, ok := pjisuvParam.ObjDicOfTpperception[obj.Id]; !ok {
  1255. pjisuvParam.ObjDicOfTpperception[obj.Id] = []float32{}
  1256. }
  1257. pjisuvParam.ObjDicOfTpperception[obj.Id] = append(pjisuvParam.ObjDicOfTpperception[obj.Id], obj.Y)
  1258. }
  1259. }
  1260. mutexOfTpperception.RUnlock()
  1261. subscribersTimeMutexes[i].Lock()
  1262. if time.Since(subscribersTimes[i]).Seconds() > triggerInterval {
  1263. subscribersMutexes[i].Lock()
  1264. faultHappenTime := util.GetNowTimeCustom() // 获取当前故障发生时间
  1265. lastTimeWindow := commonEntity.GetLastTimeWindow() // 获取最后一个时间窗口
  1266. faultLabel := ""
  1267. if len(masterConfig.RuleOfTpperception1) > 0 {
  1268. for _, f := range masterConfig.RuleOfTpperception1 {
  1269. faultLabel = f(data)
  1270. if faultLabel != "" {
  1271. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  1272. subscribersTimes[i] = time.Now()
  1273. goto TriggerSuccess
  1274. }
  1275. }
  1276. }
  1277. if len(masterConfig.RuleOfTpperception2) > 0 {
  1278. for _, f := range masterConfig.RuleOfTpperception2 {
  1279. faultLabel = f(data, &pjisuvParam)
  1280. if faultLabel != "" {
  1281. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  1282. subscribersTimes[i] = time.Now()
  1283. goto TriggerSuccess
  1284. }
  1285. }
  1286. }
  1287. TriggerSuccess:
  1288. subscribersMutexes[i].Unlock()
  1289. }
  1290. subscribersTimeMutexes[i].Unlock()
  1291. // -------- 触发后更新共享变量
  1292. mutexOfTpperception.RLock()
  1293. {
  1294. for _, obj := range data.Objs {
  1295. pjisuvParam.ObjTypeDicOfTpperception[obj.Id] = obj.Type
  1296. pjisuvParam.ObjSpeedDicOfTpperception[obj.Id] = math.Pow(math.Pow(float64(obj.Vxabs), 2)+math.Pow(float64(obj.Vyabs), 2), 0.5)
  1297. }
  1298. }
  1299. mutexOfTpperception.RUnlock()
  1300. },
  1301. })
  1302. }
  1303. // 31
  1304. if topic == masterConfig.TopicOfTpperceptionVis && (len(masterConfig.RuleOfTpperceptionVis1) > 0 || len(masterConfig.RuleOfTpperceptionVis2) > 0) {
  1305. subscribers[i], err = goroslib.NewSubscriber(goroslib.SubscriberConf{
  1306. Node: commonConfig.RosNode,
  1307. Topic: topic,
  1308. Callback: func(data *visualization_msgs.MarkerArray) {
  1309. subscribersTimeMutexes[i].Lock()
  1310. if time.Since(subscribersTimes[i]).Seconds() > triggerInterval {
  1311. subscribersMutexes[i].Lock()
  1312. faultHappenTime := util.GetNowTimeCustom() // 获取当前故障发生时间
  1313. lastTimeWindow := commonEntity.GetLastTimeWindow() // 获取最后一个时间窗口
  1314. faultLabel := ""
  1315. if len(masterConfig.RuleOfTpperceptionVis1) > 0 {
  1316. for _, f := range masterConfig.RuleOfTpperceptionVis1 {
  1317. faultLabel = f(data)
  1318. if faultLabel != "" {
  1319. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  1320. subscribersTimes[i] = time.Now()
  1321. goto TriggerSuccess
  1322. }
  1323. }
  1324. }
  1325. if len(masterConfig.RuleOfTpperceptionVis2) > 0 {
  1326. for _, f := range masterConfig.RuleOfTpperceptionVis2 {
  1327. faultLabel = f(data, &pjisuvParam)
  1328. if faultLabel != "" {
  1329. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  1330. subscribersTimes[i] = time.Now()
  1331. goto TriggerSuccess
  1332. }
  1333. }
  1334. }
  1335. TriggerSuccess:
  1336. subscribersMutexes[i].Unlock()
  1337. }
  1338. subscribersTimeMutexes[i].Unlock()
  1339. },
  1340. })
  1341. }
  1342. // 32
  1343. if topic == masterConfig.TopicOfTprouteplan && (len(masterConfig.RuleOfTprouteplan1) > 0 || len(masterConfig.RuleOfTprouteplan2) > 0) {
  1344. subscribers[i], err = goroslib.NewSubscriber(goroslib.SubscriberConf{
  1345. Node: commonConfig.RosNode,
  1346. Topic: topic,
  1347. Callback: func(data *pjisuv_msgs.RoutePlan) {
  1348. subscribersTimeMutexes[i].Lock()
  1349. if time.Since(subscribersTimes[i]).Seconds() > triggerInterval {
  1350. subscribersMutexes[i].Lock()
  1351. faultHappenTime := util.GetNowTimeCustom() // 获取当前故障发生时间
  1352. lastTimeWindow := commonEntity.GetLastTimeWindow() // 获取最后一个时间窗口
  1353. faultLabel := ""
  1354. if len(masterConfig.RuleOfTprouteplan1) > 0 {
  1355. for _, f := range masterConfig.RuleOfTprouteplan1 {
  1356. faultLabel = f(data)
  1357. if faultLabel != "" {
  1358. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  1359. subscribersTimes[i] = time.Now()
  1360. goto TriggerSuccess
  1361. }
  1362. }
  1363. }
  1364. if len(masterConfig.RuleOfTprouteplan2) > 0 {
  1365. for _, f := range masterConfig.RuleOfTprouteplan2 {
  1366. faultLabel = f(data, &pjisuvParam)
  1367. if faultLabel != "" {
  1368. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  1369. subscribersTimes[i] = time.Now()
  1370. goto TriggerSuccess
  1371. }
  1372. }
  1373. }
  1374. TriggerSuccess:
  1375. subscribersMutexes[i].Unlock()
  1376. }
  1377. subscribersTimeMutexes[i].Unlock()
  1378. },
  1379. })
  1380. }
  1381. // 33
  1382. if topic == masterConfig.TopicOfTrajectoryDisplay && (len(masterConfig.RuleOfTrajectoryDisplay1) > 0 || len(masterConfig.RuleOfTrajectoryDisplay2) > 0) {
  1383. subscribers[i], err = goroslib.NewSubscriber(goroslib.SubscriberConf{
  1384. Node: commonConfig.RosNode,
  1385. Topic: topic,
  1386. Callback: func(data *nav_msgs.Path) {
  1387. subscribersTimeMutexes[i].Lock()
  1388. if time.Since(subscribersTimes[i]).Seconds() > triggerInterval {
  1389. subscribersMutexes[i].Lock()
  1390. faultHappenTime := util.GetNowTimeCustom() // 获取当前故障发生时间
  1391. lastTimeWindow := commonEntity.GetLastTimeWindow() // 获取最后一个时间窗口
  1392. faultLabel := ""
  1393. if len(masterConfig.RuleOfTrajectoryDisplay1) > 0 {
  1394. for _, f := range masterConfig.RuleOfTrajectoryDisplay1 {
  1395. faultLabel = f(data)
  1396. if faultLabel != "" {
  1397. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  1398. subscribersTimes[i] = time.Now()
  1399. goto TriggerSuccess
  1400. }
  1401. }
  1402. }
  1403. if len(masterConfig.RuleOfTrajectoryDisplay2) > 0 {
  1404. for _, f := range masterConfig.RuleOfTrajectoryDisplay2 {
  1405. faultLabel = f(data, &pjisuvParam)
  1406. if faultLabel != "" {
  1407. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  1408. subscribersTimes[i] = time.Now()
  1409. goto TriggerSuccess
  1410. }
  1411. }
  1412. }
  1413. TriggerSuccess:
  1414. subscribersMutexes[i].Unlock()
  1415. }
  1416. subscribersTimeMutexes[i].Unlock()
  1417. },
  1418. })
  1419. }
  1420. // 34
  1421. if topic == masterConfig.TopicOfUngroundCloudpoints && (len(masterConfig.RuleOfUngroundCloudpoints1) > 0 || len(masterConfig.RuleOfUngroundCloudpoints2) > 0) {
  1422. subscribers[i], err = goroslib.NewSubscriber(goroslib.SubscriberConf{
  1423. Node: commonConfig.RosNode,
  1424. Topic: topic,
  1425. Callback: func(data *sensor_msgs.PointCloud2) {
  1426. subscribersTimeMutexes[i].Lock()
  1427. if time.Since(subscribersTimes[i]).Seconds() > triggerInterval {
  1428. subscribersMutexes[i].Lock()
  1429. faultHappenTime := util.GetNowTimeCustom() // 获取当前故障发生时间
  1430. lastTimeWindow := commonEntity.GetLastTimeWindow() // 获取最后一个时间窗口
  1431. faultLabel := ""
  1432. if len(masterConfig.RuleOfUngroundCloudpoints1) > 0 {
  1433. for _, f := range masterConfig.RuleOfUngroundCloudpoints1 {
  1434. faultLabel = f(data)
  1435. if faultLabel != "" {
  1436. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  1437. subscribersTimes[i] = time.Now()
  1438. goto TriggerSuccess
  1439. }
  1440. }
  1441. }
  1442. if len(masterConfig.RuleOfUngroundCloudpoints2) > 0 {
  1443. for _, f := range masterConfig.RuleOfUngroundCloudpoints2 {
  1444. faultLabel = f(data, &pjisuvParam)
  1445. if faultLabel != "" {
  1446. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  1447. subscribersTimes[i] = time.Now()
  1448. goto TriggerSuccess
  1449. }
  1450. }
  1451. }
  1452. TriggerSuccess:
  1453. subscribersMutexes[i].Unlock()
  1454. }
  1455. subscribersTimeMutexes[i].Unlock()
  1456. },
  1457. })
  1458. }
  1459. // 35
  1460. if topic == masterConfig.TopicOfCameraImage && (len(masterConfig.RuleOfCameraImage1) > 0 || len(masterConfig.RuleOfCameraImage2) > 0) {
  1461. subscribers[i], err = goroslib.NewSubscriber(goroslib.SubscriberConf{
  1462. Node: commonConfig.RosNode,
  1463. Topic: topic,
  1464. Callback: func(data *sensor_msgs.Image) {
  1465. subscribersTimeMutexes[i].Lock()
  1466. if time.Since(subscribersTimes[i]).Seconds() > triggerInterval {
  1467. subscribersMutexes[i].Lock()
  1468. faultHappenTime := util.GetNowTimeCustom() // 获取当前故障发生时间
  1469. lastTimeWindow := commonEntity.GetLastTimeWindow() // 获取最后一个时间窗口
  1470. faultLabel := ""
  1471. if len(masterConfig.RuleOfCameraImage1) > 0 {
  1472. for _, f := range masterConfig.RuleOfCameraImage1 {
  1473. faultLabel = f(data)
  1474. if faultLabel != "" {
  1475. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  1476. subscribersTimes[i] = time.Now()
  1477. goto TriggerSuccess
  1478. }
  1479. }
  1480. }
  1481. if len(masterConfig.RuleOfCameraImage2) > 0 {
  1482. for _, f := range masterConfig.RuleOfCameraImage2 {
  1483. faultLabel = f(data, &pjisuvParam)
  1484. if faultLabel != "" {
  1485. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  1486. subscribersTimes[i] = time.Now()
  1487. goto TriggerSuccess
  1488. }
  1489. }
  1490. }
  1491. TriggerSuccess:
  1492. subscribersMutexes[i].Unlock()
  1493. }
  1494. subscribersTimeMutexes[i].Unlock()
  1495. },
  1496. })
  1497. }
  1498. // 36 有共享变量的订阅者必须被创建
  1499. if topic == masterConfig.TopicOfDataRead {
  1500. subscribers[i], err = goroslib.NewSubscriber(goroslib.SubscriberConf{
  1501. Node: commonConfig.RosNode,
  1502. Topic: topic,
  1503. Callback: func(data *pjisuv_msgs.Retrieval) {
  1504. // 更新共享变量
  1505. mutexOfDataRead.RLock()
  1506. {
  1507. pjisuvParam.NumCountDataReadOfDataRead++
  1508. if pjisuvParam.NumCountDataReadOfDataRead == 10 {
  1509. pjisuvParam.EgoSteeringRealOfDataRead = append(pjisuvParam.EgoSteeringRealOfDataRead, data.ActStrWhAng)
  1510. pjisuvParam.EgoThrottleRealOfDataRead = append(pjisuvParam.EgoThrottleRealOfDataRead, data.AccPed2)
  1511. pjisuvParam.NumCountDataReadOfDataRead = 0
  1512. }
  1513. pjisuvParam.StrgAngleRealValueOfDataRead = data.ActStrWhAng
  1514. }
  1515. mutexOfDataRead.RUnlock()
  1516. subscribersTimeMutexes[i].Lock()
  1517. if time.Since(subscribersTimes[i]).Seconds() > triggerInterval {
  1518. subscribersMutexes[i].Lock()
  1519. faultHappenTime := util.GetNowTimeCustom() // 获取当前故障发生时间
  1520. lastTimeWindow := commonEntity.GetLastTimeWindow() // 获取最后一个时间窗口
  1521. faultLabel := ""
  1522. if len(masterConfig.RuleOfDataRead1) > 0 {
  1523. for _, f := range masterConfig.RuleOfDataRead1 {
  1524. faultLabel = f(data)
  1525. if faultLabel != "" {
  1526. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  1527. subscribersTimes[i] = time.Now()
  1528. goto TriggerSuccess
  1529. }
  1530. }
  1531. }
  1532. if len(masterConfig.RuleOfDataRead2) > 0 {
  1533. for _, f := range masterConfig.RuleOfDataRead2 {
  1534. faultLabel = f(data, &pjisuvParam)
  1535. if faultLabel != "" {
  1536. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  1537. subscribersTimes[i] = time.Now()
  1538. goto TriggerSuccess
  1539. }
  1540. }
  1541. }
  1542. TriggerSuccess:
  1543. subscribersMutexes[i].Unlock()
  1544. }
  1545. subscribersTimeMutexes[i].Unlock()
  1546. },
  1547. })
  1548. }
  1549. // 37
  1550. if topic == masterConfig.TopicOfPjiGps && (len(masterConfig.RuleOfPjiGps1) > 0 || len(masterConfig.RuleOfPjiGps2) > 0) {
  1551. subscribers[i], err = goroslib.NewSubscriber(goroslib.SubscriberConf{
  1552. Node: commonConfig.RosNode,
  1553. Topic: topic,
  1554. Callback: func(data *pjisuv_msgs.PerceptionLocalization) {
  1555. subscribersTimeMutexes[i].Lock()
  1556. if time.Since(subscribersTimes[i]).Seconds() > triggerInterval {
  1557. subscribersMutexes[i].Lock()
  1558. faultHappenTime := util.GetNowTimeCustom() // 获取当前故障发生时间
  1559. lastTimeWindow := commonEntity.GetLastTimeWindow() // 获取最后一个时间窗口
  1560. faultLabel := ""
  1561. if len(masterConfig.RuleOfPjiGps1) > 0 {
  1562. for _, f := range masterConfig.RuleOfPjiGps1 {
  1563. faultLabel = f(data)
  1564. if faultLabel != "" {
  1565. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  1566. subscribersTimes[i] = time.Now()
  1567. goto TriggerSuccess
  1568. }
  1569. }
  1570. }
  1571. if len(masterConfig.RuleOfPjiGps2) > 0 {
  1572. for _, f := range masterConfig.RuleOfPjiGps2 {
  1573. faultLabel = f(data, &pjisuvParam)
  1574. if faultLabel != "" {
  1575. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  1576. subscribersTimes[i] = time.Now()
  1577. goto TriggerSuccess
  1578. }
  1579. }
  1580. }
  1581. TriggerSuccess:
  1582. subscribersMutexes[i].Unlock()
  1583. }
  1584. subscribersTimeMutexes[i].Unlock()
  1585. },
  1586. })
  1587. }
  1588. // 39
  1589. if topic == masterConfig.TopicOfPjVehicleFdbPub && (len(masterConfig.RuleOfPjVehicleFdbPub1) > 0 || len(masterConfig.RuleOfPjVehicleFdbPub2) > 0) {
  1590. subscribers[i], err = goroslib.NewSubscriber(goroslib.SubscriberConf{
  1591. Node: commonConfig.RosNode,
  1592. Topic: topic,
  1593. Callback: func(data *pjisuv_msgs.VehicleFdb) {
  1594. subscribersTimeMutexes[i].Lock()
  1595. if time.Since(subscribersTimes[i]).Seconds() > triggerInterval {
  1596. subscribersMutexes[i].Lock()
  1597. faultHappenTime := util.GetNowTimeCustom() // 获取当前故障发生时间
  1598. lastTimeWindow := commonEntity.GetLastTimeWindow() // 获取最后一个时间窗口
  1599. faultLabel := ""
  1600. if len(masterConfig.RuleOfPjVehicleFdbPub1) > 0 {
  1601. for _, f := range masterConfig.RuleOfPjVehicleFdbPub1 {
  1602. faultLabel = f(data)
  1603. if faultLabel != "" {
  1604. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  1605. subscribersTimes[i] = time.Now()
  1606. goto TriggerSuccess
  1607. }
  1608. }
  1609. }
  1610. if len(masterConfig.RuleOfPjVehicleFdbPub2) > 0 {
  1611. for _, f := range masterConfig.RuleOfPjVehicleFdbPub2 {
  1612. faultLabel = f(data, &pjisuvParam)
  1613. if faultLabel != "" {
  1614. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  1615. subscribersTimes[i] = time.Now()
  1616. goto TriggerSuccess
  1617. }
  1618. }
  1619. }
  1620. TriggerSuccess:
  1621. subscribersMutexes[i].Unlock()
  1622. }
  1623. subscribersTimeMutexes[i].Unlock()
  1624. },
  1625. })
  1626. }
  1627. if err != nil {
  1628. c_log.GlobalLogger.Info("创建订阅者报错,可能由于节点未启动,再次尝试:", err)
  1629. time.Sleep(time.Duration(2) * time.Second)
  1630. continue
  1631. } else {
  1632. break
  1633. }
  1634. }
  1635. }
  1636. select {
  1637. case signal := <-service.ChannelKillWindowProducer:
  1638. if signal == 1 {
  1639. commonConfig.RosNode.Close()
  1640. service.AddKillTimes("3")
  1641. return
  1642. }
  1643. }
  1644. }
  1645. func saveTimeWindow(faultLabel string, faultHappenTime string, lastTimeWindow *commonEntity.TimeWindow) {
  1646. saveTimeWindowMutex.Lock()
  1647. defer saveTimeWindowMutex.Unlock()
  1648. masterTopics, slaveTopics := getTopicsOfNode(faultLabel)
  1649. if lastTimeWindow == nil || util.TimeCustom1GreaterTimeCustom2(faultHappenTime, lastTimeWindow.TimeWindowEnd) { // 如果是不在旧故障窗口内,添加一个新窗口
  1650. exceptBegin := util.TimeCustomChange(faultHappenTime, -commonConfig.PlatformConfig.TaskBeforeTime)
  1651. finalTimeWindowBegin := ""
  1652. if util.TimeCustom1LessEqualThanTimeCustom2(exceptBegin, latestTimeWindowEnd) { // 窗口最早时间不能早于上一个窗口结束时间
  1653. finalTimeWindowBegin = latestTimeWindowEnd
  1654. } else {
  1655. finalTimeWindowBegin = exceptBegin
  1656. }
  1657. latestTimeWindowEnd = util.TimeCustomChange(faultHappenTime, commonConfig.PlatformConfig.TaskAfterTime)
  1658. newTimeWindow := commonEntity.TimeWindow{
  1659. FaultTime: faultHappenTime,
  1660. TimeWindowBegin: finalTimeWindowBegin,
  1661. TimeWindowEnd: latestTimeWindowEnd,
  1662. Length: util.CalculateDifferenceOfTimeCustom(finalTimeWindowBegin, latestTimeWindowEnd),
  1663. Labels: []string{faultLabel},
  1664. MasterTopics: masterTopics,
  1665. SlaveTopics: slaveTopics,
  1666. }
  1667. c_log.GlobalLogger.Infof("不在旧故障窗口内,向生产者队列添加一个新窗口,【Lable】=%v,【FaultTime】=%v,【Length】=%v", newTimeWindow.Labels, newTimeWindow.FaultTime, newTimeWindow.Length)
  1668. commonEntity.AddTimeWindowToTimeWindowProducerQueue(newTimeWindow)
  1669. } else { // 如果在旧故障窗口内
  1670. commonEntity.TimeWindowProducerQueueMutex.RLock()
  1671. defer commonEntity.TimeWindowProducerQueueMutex.RUnlock()
  1672. // 更新故障窗口end时间
  1673. expectEnd := util.TimeCustomChange(faultHappenTime, commonConfig.PlatformConfig.TaskAfterTime) // 窗口期望关闭时间是触发时间加上后置时间
  1674. expectLength := util.CalculateDifferenceOfTimeCustom(lastTimeWindow.TimeWindowBegin, expectEnd)
  1675. if expectLength < commonConfig.PlatformConfig.TaskMaxTime {
  1676. latestTimeWindowEnd = expectEnd
  1677. lastTimeWindow.TimeWindowEnd = latestTimeWindowEnd
  1678. lastTimeWindow.Length = util.CalculateDifferenceOfTimeCustom(lastTimeWindow.TimeWindowBegin, lastTimeWindow.TimeWindowEnd)
  1679. }
  1680. // 更新label
  1681. labels := lastTimeWindow.Labels
  1682. lastTimeWindow.Labels = util.AppendIfNotExists(labels, faultLabel)
  1683. // 更新 topic
  1684. sourceMasterTopics := lastTimeWindow.MasterTopics
  1685. lastTimeWindow.MasterTopics = util.MergeSlice(sourceMasterTopics, masterTopics)
  1686. sourceSlaveTopics := lastTimeWindow.SlaveTopics
  1687. lastTimeWindow.SlaveTopics = util.MergeSlice(sourceSlaveTopics, slaveTopics)
  1688. c_log.GlobalLogger.Infof("在旧故障窗口内,更新生产者队列最新的窗口,【Lable】=%v,【FaultTime】=%v,【Length】=%v", lastTimeWindow.Labels, lastTimeWindow.FaultTime, lastTimeWindow.Length)
  1689. }
  1690. }
  1691. func getTopicsOfNode(faultLabel string) (masterTopics []string, slaveTopics []string) {
  1692. // 获取所有需要采集的topic
  1693. var faultCodeTopics []string
  1694. for _, code := range commonConfig.CloudConfig.Triggers {
  1695. if code.Label == faultLabel {
  1696. faultCodeTopics = code.Topics
  1697. }
  1698. }
  1699. // 根据不同节点采集的topic进行分配采集
  1700. for _, acceptTopic := range faultCodeTopics {
  1701. for _, host := range commonConfig.CloudConfig.Hosts {
  1702. for _, topic := range host.Topics {
  1703. if host.Name == commonConfig.CloudConfig.Hosts[0].Name && acceptTopic == topic {
  1704. masterTopics = append(masterTopics, acceptTopic)
  1705. }
  1706. if host.Name == commonConfig.CloudConfig.Hosts[1].Name && acceptTopic == topic {
  1707. slaveTopics = append(slaveTopics, acceptTopic)
  1708. }
  1709. }
  1710. }
  1711. }
  1712. return masterTopics, slaveTopics
  1713. }