produce_window.go 70 KB

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