produce_window.go 79 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282
  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. type Weather struct {
  22. WeatherID []int
  23. temperature float64
  24. humidity float64
  25. }
  26. type Point struct {
  27. x, y float64
  28. }
  29. type Location struct {
  30. Latitude float64
  31. Longitude float64
  32. }
  33. var (
  34. LeftCurveFlag bool = false
  35. RightCurveFlag bool = false //用来判断车辆是左转还是右转
  36. //定义了车间四个边界点,将车间抽象成一个四边形
  37. LocationCount = 0
  38. vertices = []Point{
  39. {x: 456128.413, y: 4397847.78},
  40. {x: 456288.257, y: 4397953.51},
  41. {x: 456359.022, y: 4397822.84},
  42. {x: 456191.065, y: 4397733.3},
  43. }
  44. //定义园区部门T字路口的经纬度坐标值
  45. point3 = Location{39.73040966605621, 116.48995329696209}
  46. point4 = Location{39.73083727413453, 116.49079780188244}
  47. point5 = Location{39.72976753711939, 116.49043130389033}
  48. point6 = Location{39.73012466515933, 116.49128381717591}
  49. point7 = Location{39.729251498328246, 116.49077484625299}
  50. point8 = Location{39.72964529630643, 116.49164592200161}
  51. pointlist = []Location{point3, point4, point5, point6, point7, point8}
  52. cicvLocationTime = time.Now()
  53. // -----------------------------共享变量
  54. //cicv_location
  55. //NumOfCicvLocation = 0
  56. SpeedSlice = []float64{}
  57. AccelXSlice = []float64{}
  58. JunctionFlag = false
  59. //Yowslice = make([]float64, 0)
  60. //AngleSlice = make([][]float64, 0)
  61. // /tpperception
  62. countSideBySide int = 0
  63. Frame float32 = 0.0
  64. ObjDicOfTpperception = make(map[uint32][][]float32)
  65. objTypeDicOfTpperception = make(map[uint32]uint8)
  66. objSpeedDicOfTpperception = make(map[uint32]float64)
  67. PreCloseTargetSlice = []uint32{}
  68. // /pji_control_pub
  69. numCountPjiControlCommandOfPjControlPub int
  70. egoSteeringCmdOfPjControlPub []float64
  71. egoThrottleCmdOfPjControlPub []float64
  72. // /data_read
  73. numCountDataReadOfDataRead int
  74. egoSteeringRealOfDataRead []float64
  75. egoThrottleRealOfDataRead []float64
  76. GearPosSlice = []int16{}
  77. // --------------------------------------------------
  78. shareVars = new(sync.Map)
  79. saveTimeWindowMutex sync.Mutex // 保存时间窗口需要锁,防止数据竟态
  80. latestTimeWindowEnd = util.GetTimeCustom(time.Now())
  81. triggerInterval = 3.0 // 每个触发器3秒触发一次
  82. )
  83. // 负责监听所有主题并修改时间窗口
  84. func ProduceWindow() {
  85. c_log.GlobalLogger.Info("订阅者 goroutine,启动。")
  86. var err error
  87. subscribers := make([]*goroslib.Subscriber, masterConfig.AllTopicsNumber)
  88. subscribersTimes := make([]time.Time, masterConfig.AllTopicsNumber)
  89. subscribersTimeMutexes := make([]sync.Mutex, masterConfig.AllTopicsNumber)
  90. subscribersMutexes := make([]sync.Mutex, masterConfig.AllTopicsNumber)
  91. for i, topic := range masterConfig.AllTopics {
  92. for {
  93. // 定时器,区别于订阅者
  94. if topic == pjisuv_ticker.TickerTopic {
  95. // 1 把所有触发器函数执行一遍,触发器内部创建额外的定时任务goroutine
  96. for _, f := range masterConfig.RuleOfCicvTicker {
  97. f(shareVars)
  98. }
  99. // 2 创建goroutine接收定时任务触发器返回的Label和Time,执行触发逻辑
  100. go func() {
  101. for {
  102. time.Sleep(time.Duration(1)) // 降低循环频率
  103. select {
  104. case tickInfo := <-pjisuv_ticker.TickerChan:
  105. faultLabel := tickInfo.FaultLabel
  106. faultHappenTime := tickInfo.FaultHappenTime
  107. lastTimeWindow := commonEntity.GetLastTimeWindow() // 获取最后一个时间窗口
  108. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  109. default:
  110. }
  111. }
  112. }()
  113. }
  114. create := false // 判断是否创建成功,用于打印日志
  115. // 1
  116. if topic == masterConfig.TopicOfAmrPose && (len(masterConfig.RuleOfAmrPose1) > 0 || len(masterConfig.RuleOfAmrPose3) > 0) {
  117. subscribers[i], err = goroslib.NewSubscriber(goroslib.SubscriberConf{
  118. Node: commonConfig.RosNode,
  119. Topic: topic,
  120. Callback: func(data *visualization_msgs.MarkerArray) {
  121. subscribersTimeMutexes[i].Lock()
  122. if time.Since(subscribersTimes[i]).Seconds() > triggerInterval {
  123. subscribersMutexes[i].Lock()
  124. faultHappenTime := util.GetNowTimeCustom() // 获取当前故障发生时间
  125. lastTimeWindow := commonEntity.GetLastTimeWindow() // 获取最后一个时间窗口
  126. faultLabel := ""
  127. if len(masterConfig.RuleOfAmrPose1) > 0 {
  128. for _, f := range masterConfig.RuleOfAmrPose1 {
  129. faultLabel = f(data)
  130. if faultLabel != "" {
  131. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  132. subscribersTimes[i] = time.Now()
  133. goto TriggerSuccess
  134. }
  135. }
  136. }
  137. if len(masterConfig.RuleOfAmrPose3) > 0 {
  138. for _, f := range masterConfig.RuleOfAmrPose3 {
  139. faultLabel = f(shareVars, data)
  140. if faultLabel != "" {
  141. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  142. subscribersTimes[i] = time.Now()
  143. goto TriggerSuccess
  144. }
  145. }
  146. }
  147. TriggerSuccess:
  148. subscribersMutexes[i].Unlock()
  149. }
  150. subscribersTimeMutexes[i].Unlock()
  151. },
  152. })
  153. if err == nil {
  154. create = true
  155. }
  156. }
  157. // 2
  158. if topic == masterConfig.TopicOfBoundingBoxesFast &&
  159. (len(masterConfig.RuleOfBoundingBoxesFast1) > 0 ||
  160. len(masterConfig.RuleOfBoundingBoxesFast3) > 0) {
  161. subscribers[i], err = goroslib.NewSubscriber(goroslib.SubscriberConf{
  162. Node: commonConfig.RosNode,
  163. Topic: topic,
  164. Callback: func(data *pjisuv_msgs.BoundingBoxArray) {
  165. subscribersTimeMutexes[i].Lock()
  166. if time.Since(subscribersTimes[i]).Seconds() > triggerInterval {
  167. subscribersMutexes[i].Lock()
  168. faultHappenTime := util.GetNowTimeCustom() // 获取当前故障发生时间
  169. lastTimeWindow := commonEntity.GetLastTimeWindow() // 获取最后一个时间窗口
  170. faultLabel := ""
  171. if len(masterConfig.RuleOfBoundingBoxesFast1) > 0 {
  172. for _, f := range masterConfig.RuleOfBoundingBoxesFast1 {
  173. faultLabel = f(data)
  174. if faultLabel != "" {
  175. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  176. subscribersTimes[i] = time.Now()
  177. goto TriggerSuccess
  178. }
  179. }
  180. }
  181. if len(masterConfig.RuleOfBoundingBoxesFast3) > 0 {
  182. for _, f := range masterConfig.RuleOfBoundingBoxesFast3 {
  183. faultLabel = f(shareVars, data)
  184. if faultLabel != "" {
  185. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  186. subscribersTimes[i] = time.Now()
  187. goto TriggerSuccess
  188. }
  189. }
  190. }
  191. TriggerSuccess:
  192. subscribersMutexes[i].Unlock()
  193. }
  194. subscribersTimeMutexes[i].Unlock()
  195. },
  196. })
  197. if err == nil {
  198. create = true
  199. }
  200. }
  201. // 3
  202. if topic == masterConfig.TopicOfCameraFault &&
  203. (len(masterConfig.RuleOfCameraFault1) > 0 ||
  204. len(masterConfig.RuleOfCameraFault3) > 0) {
  205. subscribers[i], err = goroslib.NewSubscriber(goroslib.SubscriberConf{
  206. Node: commonConfig.RosNode,
  207. Topic: topic,
  208. Callback: func(data *pjisuv_msgs.FaultVec) {
  209. subscribersTimeMutexes[i].Lock()
  210. if time.Since(subscribersTimes[i]).Seconds() > triggerInterval {
  211. subscribersMutexes[i].Lock()
  212. faultHappenTime := util.GetNowTimeCustom() // 获取当前故障发生时间
  213. lastTimeWindow := commonEntity.GetLastTimeWindow() // 获取最后一个时间窗口
  214. faultLabel := ""
  215. if len(masterConfig.RuleOfCameraFault1) > 0 {
  216. for _, f := range masterConfig.RuleOfCameraFault1 {
  217. faultLabel = f(data)
  218. if faultLabel != "" {
  219. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  220. subscribersTimes[i] = time.Now()
  221. goto TriggerSuccess
  222. }
  223. }
  224. }
  225. if len(masterConfig.RuleOfCameraFault3) > 0 {
  226. for _, f := range masterConfig.RuleOfCameraFault3 {
  227. faultLabel = f(shareVars, data)
  228. if faultLabel != "" {
  229. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  230. subscribersTimes[i] = time.Now()
  231. goto TriggerSuccess
  232. }
  233. }
  234. }
  235. TriggerSuccess:
  236. subscribersMutexes[i].Unlock()
  237. }
  238. subscribersTimeMutexes[i].Unlock()
  239. },
  240. })
  241. if err == nil {
  242. create = true
  243. }
  244. }
  245. // 4
  246. if topic == masterConfig.TopicOfCanData &&
  247. (len(masterConfig.RuleOfCanData1) > 0 ||
  248. len(masterConfig.RuleOfCanData3) > 0) {
  249. subscribers[i], err = goroslib.NewSubscriber(goroslib.SubscriberConf{
  250. Node: commonConfig.RosNode,
  251. Topic: topic,
  252. Callback: func(data *pjisuv_msgs.Frame) {
  253. subscribersTimeMutexes[i].Lock()
  254. if time.Since(subscribersTimes[i]).Seconds() > triggerInterval {
  255. subscribersMutexes[i].Lock()
  256. faultHappenTime := util.GetNowTimeCustom() // 获取当前故障发生时间
  257. lastTimeWindow := commonEntity.GetLastTimeWindow() // 获取最后一个时间窗口
  258. faultLabel := ""
  259. if len(masterConfig.RuleOfCanData1) > 0 {
  260. for _, f := range masterConfig.RuleOfCanData1 {
  261. faultLabel = f(data)
  262. if faultLabel != "" {
  263. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  264. subscribersTimes[i] = time.Now()
  265. goto TriggerSuccess
  266. }
  267. }
  268. }
  269. if len(masterConfig.RuleOfCanData3) > 0 {
  270. for _, f := range masterConfig.RuleOfCanData3 {
  271. faultLabel = f(shareVars, data)
  272. if faultLabel != "" {
  273. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  274. subscribersTimes[i] = time.Now()
  275. goto TriggerSuccess
  276. }
  277. }
  278. }
  279. TriggerSuccess:
  280. subscribersMutexes[i].Unlock()
  281. }
  282. subscribersTimeMutexes[i].Unlock()
  283. },
  284. })
  285. if err == nil {
  286. create = true
  287. }
  288. }
  289. // 5
  290. if topic == masterConfig.TopicOfCh128x1LslidarPointCloud &&
  291. (len(masterConfig.RuleOfCh128x1LslidarPointCloud1) > 0 ||
  292. len(masterConfig.RuleOfCh128x1LslidarPointCloud3) > 0) {
  293. subscribers[i], err = goroslib.NewSubscriber(goroslib.SubscriberConf{
  294. Node: commonConfig.RosNode,
  295. Topic: topic,
  296. Callback: func(data *sensor_msgs.PointCloud2) {
  297. subscribersTimeMutexes[i].Lock()
  298. if time.Since(subscribersTimes[i]).Seconds() > triggerInterval {
  299. subscribersMutexes[i].Lock()
  300. faultHappenTime := util.GetNowTimeCustom() // 获取当前故障发生时间
  301. lastTimeWindow := commonEntity.GetLastTimeWindow() // 获取最后一个时间窗口
  302. faultLabel := ""
  303. if len(masterConfig.RuleOfCh128x1LslidarPointCloud1) > 0 {
  304. for _, f := range masterConfig.RuleOfCh128x1LslidarPointCloud1 {
  305. faultLabel = f(data)
  306. if faultLabel != "" {
  307. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  308. subscribersTimes[i] = time.Now()
  309. goto TriggerSuccess
  310. }
  311. }
  312. }
  313. if len(masterConfig.RuleOfCh128x1LslidarPointCloud3) > 0 {
  314. for _, f := range masterConfig.RuleOfCh128x1LslidarPointCloud3 {
  315. faultLabel = f(shareVars, data)
  316. if faultLabel != "" {
  317. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  318. subscribersTimes[i] = time.Now()
  319. goto TriggerSuccess
  320. }
  321. }
  322. }
  323. TriggerSuccess:
  324. subscribersMutexes[i].Unlock()
  325. }
  326. subscribersTimeMutexes[i].Unlock()
  327. },
  328. })
  329. if err == nil {
  330. create = true
  331. }
  332. }
  333. // 6
  334. if topic == masterConfig.TopicOfCh64wLLslidarPointCloud &&
  335. (len(masterConfig.RuleOfCh64wLLslidarPointCloud1) > 0 ||
  336. len(masterConfig.RuleOfCh64wLLslidarPointCloud3) > 0) {
  337. subscribers[i], err = goroslib.NewSubscriber(goroslib.SubscriberConf{
  338. Node: commonConfig.RosNode,
  339. Topic: topic,
  340. Callback: func(data *sensor_msgs.PointCloud2) {
  341. subscribersTimeMutexes[i].Lock()
  342. if time.Since(subscribersTimes[i]).Seconds() > triggerInterval {
  343. subscribersMutexes[i].Lock()
  344. faultHappenTime := util.GetNowTimeCustom() // 获取当前故障发生时间
  345. lastTimeWindow := commonEntity.GetLastTimeWindow() // 获取最后一个时间窗口
  346. faultLabel := ""
  347. if len(masterConfig.RuleOfCh64wLLslidarPointCloud1) > 0 {
  348. for _, f := range masterConfig.RuleOfCh64wLLslidarPointCloud1 {
  349. faultLabel = f(data)
  350. if faultLabel != "" {
  351. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  352. subscribersTimes[i] = time.Now()
  353. goto TriggerSuccess
  354. }
  355. }
  356. }
  357. if len(masterConfig.RuleOfCh64wLLslidarPointCloud3) > 0 {
  358. for _, f := range masterConfig.RuleOfCh64wLLslidarPointCloud3 {
  359. faultLabel = f(shareVars, data)
  360. if faultLabel != "" {
  361. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  362. subscribersTimes[i] = time.Now()
  363. goto TriggerSuccess
  364. }
  365. }
  366. }
  367. TriggerSuccess:
  368. subscribersMutexes[i].Unlock()
  369. }
  370. subscribersTimeMutexes[i].Unlock()
  371. },
  372. })
  373. if err == nil {
  374. create = true
  375. }
  376. }
  377. // 7
  378. if topic == masterConfig.TopicOfCh64wLScan &&
  379. (len(masterConfig.RuleOfCh64wLScan1) > 0 ||
  380. len(masterConfig.RuleOfCh64wLScan3) > 0) {
  381. subscribers[i], err = goroslib.NewSubscriber(goroslib.SubscriberConf{
  382. Node: commonConfig.RosNode,
  383. Topic: topic,
  384. Callback: func(data *sensor_msgs.LaserScan) {
  385. subscribersTimeMutexes[i].Lock()
  386. if time.Since(subscribersTimes[i]).Seconds() > triggerInterval {
  387. subscribersMutexes[i].Lock()
  388. faultHappenTime := util.GetNowTimeCustom() // 获取当前故障发生时间
  389. lastTimeWindow := commonEntity.GetLastTimeWindow() // 获取最后一个时间窗口
  390. faultLabel := ""
  391. if len(masterConfig.RuleOfCh64wLScan1) > 0 {
  392. for _, f := range masterConfig.RuleOfCh64wLScan1 {
  393. faultLabel = f(data)
  394. if faultLabel != "" {
  395. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  396. subscribersTimes[i] = time.Now()
  397. goto TriggerSuccess
  398. }
  399. }
  400. }
  401. if len(masterConfig.RuleOfCh64wLScan3) > 0 {
  402. for _, f := range masterConfig.RuleOfCh64wLScan3 {
  403. faultLabel = f(shareVars, data)
  404. if faultLabel != "" {
  405. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  406. subscribersTimes[i] = time.Now()
  407. goto TriggerSuccess
  408. }
  409. }
  410. }
  411. TriggerSuccess:
  412. subscribersMutexes[i].Unlock()
  413. }
  414. subscribersTimeMutexes[i].Unlock()
  415. },
  416. })
  417. if err == nil {
  418. create = true
  419. }
  420. }
  421. // 8
  422. if topic == masterConfig.TopicOfCh64wRLslidarPointCloud &&
  423. (len(masterConfig.RuleOfCh64wRLslidarPointCloud1) > 0 ||
  424. len(masterConfig.RuleOfCh64wRLslidarPointCloud3) > 0) {
  425. subscribers[i], err = goroslib.NewSubscriber(goroslib.SubscriberConf{
  426. Node: commonConfig.RosNode,
  427. Topic: topic,
  428. Callback: func(data *sensor_msgs.PointCloud2) {
  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.RuleOfCh64wRLslidarPointCloud1) > 0 {
  436. for _, f := range masterConfig.RuleOfCh64wRLslidarPointCloud1 {
  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.RuleOfCh64wRLslidarPointCloud3) > 0 {
  446. for _, f := range masterConfig.RuleOfCh64wRLslidarPointCloud3 {
  447. faultLabel = f(shareVars, data)
  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. if err == nil {
  462. create = true
  463. }
  464. }
  465. // 9
  466. if topic == masterConfig.TopicOfCh64wRScan &&
  467. (len(masterConfig.RuleOfCh64wRScan1) > 0 ||
  468. len(masterConfig.RuleOfCh64wRScan3) > 0) {
  469. subscribers[i], err = goroslib.NewSubscriber(goroslib.SubscriberConf{
  470. Node: commonConfig.RosNode,
  471. Topic: topic,
  472. Callback: func(data *sensor_msgs.LaserScan) {
  473. subscribersTimeMutexes[i].Lock()
  474. if time.Since(subscribersTimes[i]).Seconds() > triggerInterval {
  475. subscribersMutexes[i].Lock()
  476. faultHappenTime := util.GetNowTimeCustom() // 获取当前故障发生时间
  477. lastTimeWindow := commonEntity.GetLastTimeWindow() // 获取最后一个时间窗口
  478. faultLabel := ""
  479. if len(masterConfig.RuleOfCh64wRScan1) > 0 {
  480. for _, f := range masterConfig.RuleOfCh64wRScan1 {
  481. faultLabel = f(data)
  482. if faultLabel != "" {
  483. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  484. subscribersTimes[i] = time.Now()
  485. goto TriggerSuccess
  486. }
  487. }
  488. }
  489. if len(masterConfig.RuleOfCh64wRScan3) > 0 {
  490. for _, f := range masterConfig.RuleOfCh64wRScan3 {
  491. faultLabel = f(shareVars, data)
  492. if faultLabel != "" {
  493. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  494. subscribersTimes[i] = time.Now()
  495. goto TriggerSuccess
  496. }
  497. }
  498. }
  499. TriggerSuccess:
  500. subscribersMutexes[i].Unlock()
  501. }
  502. subscribersTimeMutexes[i].Unlock()
  503. },
  504. })
  505. if err == nil {
  506. create = true
  507. }
  508. }
  509. // 10
  510. if topic == masterConfig.TopicOfCicvLidarclusterMovingObjects &&
  511. (len(masterConfig.RuleOfCicvLidarclusterMovingObjects1) > 0 ||
  512. len(masterConfig.RuleOfCicvLidarclusterMovingObjects3) > 0) {
  513. subscribers[i], err = goroslib.NewSubscriber(goroslib.SubscriberConf{
  514. Node: commonConfig.RosNode,
  515. Topic: topic,
  516. Callback: func(data *pjisuv_msgs.PerceptionCicvMovingObjects) {
  517. subscribersTimeMutexes[i].Lock()
  518. if time.Since(subscribersTimes[i]).Seconds() > triggerInterval {
  519. subscribersMutexes[i].Lock()
  520. faultHappenTime := util.GetNowTimeCustom() // 获取当前故障发生时间
  521. lastTimeWindow := commonEntity.GetLastTimeWindow() // 获取最后一个时间窗口
  522. faultLabel := ""
  523. if len(masterConfig.RuleOfCicvLidarclusterMovingObjects1) > 0 {
  524. for _, f := range masterConfig.RuleOfCicvLidarclusterMovingObjects1 {
  525. faultLabel = f(data)
  526. if faultLabel != "" {
  527. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  528. subscribersTimes[i] = time.Now()
  529. goto TriggerSuccess
  530. }
  531. }
  532. }
  533. if len(masterConfig.RuleOfCicvLidarclusterMovingObjects3) > 0 {
  534. for _, f := range masterConfig.RuleOfCicvLidarclusterMovingObjects3 {
  535. faultLabel = f(shareVars, data)
  536. if faultLabel != "" {
  537. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  538. subscribersTimes[i] = time.Now()
  539. goto TriggerSuccess
  540. }
  541. }
  542. }
  543. TriggerSuccess:
  544. subscribersMutexes[i].Unlock()
  545. }
  546. subscribersTimeMutexes[i].Unlock()
  547. },
  548. })
  549. if err == nil {
  550. create = true
  551. }
  552. }
  553. // 11 有共享变量的订阅者必须被创建
  554. if topic == masterConfig.TopicOfCicvAmrTrajectory {
  555. subscribers[i], err = goroslib.NewSubscriber(goroslib.SubscriberConf{
  556. Node: commonConfig.RosNode,
  557. Topic: topic,
  558. Callback: func(data *pjisuv_msgs.Trajectory) {
  559. subscribersTimeMutexes[i].Lock()
  560. if time.Since(subscribersTimes[i]).Seconds() > triggerInterval {
  561. subscribersMutexes[i].Lock()
  562. faultHappenTime := util.GetNowTimeCustom() // 获取当前故障发生时间
  563. lastTimeWindow := commonEntity.GetLastTimeWindow() // 获取最后一个时间窗口
  564. faultLabel := ""
  565. if len(masterConfig.RuleOfCicvAmrTrajectory1) > 0 {
  566. for _, f := range masterConfig.RuleOfCicvAmrTrajectory1 {
  567. faultLabel = f(data)
  568. if faultLabel != "" {
  569. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  570. subscribersTimes[i] = time.Now()
  571. goto TriggerSuccess
  572. }
  573. }
  574. }
  575. if len(masterConfig.RuleOfCicvAmrTrajectory3) > 0 {
  576. for _, f := range masterConfig.RuleOfCicvAmrTrajectory3 {
  577. faultLabel = f(shareVars, data)
  578. if faultLabel != "" {
  579. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  580. subscribersTimes[i] = time.Now()
  581. goto TriggerSuccess
  582. }
  583. }
  584. }
  585. TriggerSuccess:
  586. subscribersMutexes[i].Unlock()
  587. }
  588. subscribersTimeMutexes[i].Unlock()
  589. // 更新共享变量
  590. Trajectorypoints := data.Trajectoryinfo.Trajectorypoints
  591. if len(Trajectorypoints) > 2 {
  592. StartHeading := Trajectorypoints[0].Heading
  593. EndHeading := Trajectorypoints[len(Trajectorypoints)-1].Heading
  594. diffHeading := StartHeading - EndHeading
  595. //fmt.Println(diffHeading)
  596. if diffHeading < -1.0 && diffHeading > -3.0 {
  597. LeftCurveFlag = true
  598. //fmt.Println(diffHeading)
  599. } else {
  600. LeftCurveFlag = false
  601. }
  602. if diffHeading > 1.0 && diffHeading < 3.0 {
  603. RightCurveFlag = true
  604. //fmt.Println(diffHeading)
  605. } else {
  606. RightCurveFlag = false
  607. }
  608. } else {
  609. LeftCurveFlag = false
  610. RightCurveFlag = false
  611. }
  612. shareVars.Store("LeftCurveFlag", LeftCurveFlag)
  613. shareVars.Store("RightCurveFlag", RightCurveFlag)
  614. currentCurvateres := make([]float64, 0)
  615. for _, point := range data.Trajectoryinfo.Trajectorypoints {
  616. currentCurvateres = append(currentCurvateres, math.Abs(float64(point.Curvature)))
  617. }
  618. shareVars.Store("LastCurvaturesOfCicvAmrTrajectory", currentCurvateres)
  619. shareVars.Store("DecisionType", data.Trajectoryinfo.DecisionType)
  620. },
  621. })
  622. if err == nil {
  623. create = true
  624. }
  625. }
  626. // 12 有共享变量的订阅者必须被创建
  627. if topic == masterConfig.TopicOfCicvLocation {
  628. LocationCount++
  629. subscribers[i], err = goroslib.NewSubscriber(goroslib.SubscriberConf{
  630. Node: commonConfig.RosNode,
  631. Topic: topic,
  632. Callback: func(data *pjisuv_msgs.PerceptionLocalization) {
  633. subscribersTimeMutexes[i].Lock()
  634. if time.Since(subscribersTimes[i]).Seconds() > triggerInterval {
  635. subscribersMutexes[i].Lock()
  636. faultHappenTime := util.GetNowTimeCustom() // 获取当前故障发生时间
  637. lastTimeWindow := commonEntity.GetLastTimeWindow() // 获取最后一个时间窗口
  638. faultLabel := ""
  639. if len(masterConfig.RuleOfCicvLocation1) > 0 {
  640. for _, f := range masterConfig.RuleOfCicvLocation1 {
  641. faultLabel = f(data)
  642. if faultLabel != "" {
  643. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  644. subscribersTimes[i] = time.Now()
  645. goto TriggerSuccess
  646. }
  647. }
  648. }
  649. if len(masterConfig.RuleOfCicvLocation3) > 0 {
  650. for _, f := range masterConfig.RuleOfCicvLocation3 {
  651. faultLabel = f(shareVars, data)
  652. if faultLabel != "" {
  653. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  654. subscribersTimes[i] = time.Now()
  655. goto TriggerSuccess
  656. }
  657. }
  658. }
  659. TriggerSuccess:
  660. subscribersMutexes[i].Unlock()
  661. }
  662. subscribersTimeMutexes[i].Unlock()
  663. // 更新共享变量
  664. if LocationCount%10 == 0 {
  665. enterflag := IfEnter(pointlist, 12.0, data.Latitude, data.Longitude)
  666. shareVars.Store("EnterJunctionFlag", enterflag)
  667. LocationCount = 0
  668. }
  669. AbsSpeed := math.Sqrt(math.Pow(data.VelocityX, 2) + math.Pow(data.VelocityY, 2))
  670. AccelXSlice = append(AccelXSlice, data.AccelX)
  671. shareVars.Store("AbsSpeed", AbsSpeed)
  672. shareVars.Store("AccelXSlice", AccelXSlice)
  673. shareVars.Store("VelocityXOfCicvLocation", data.VelocityX)
  674. shareVars.Store("VelocityYOfCicvLocation", data.VelocityY)
  675. shareVars.Store("VelocityZOfCicvLocation", data.VelocityZ)
  676. shareVars.Store("YawOfCicvLocation", data.Yaw)
  677. shareVars.Store("AngularVelocityZOfCicvLocation", data.AngularVelocityZ)
  678. shareVars.Store("PositionXOfCicvLocation", data.PositionX)
  679. shareVars.Store("PositionYOfCicvLocation", data.PositionY)
  680. shareVars.Store("Latitude", data.Latitude)
  681. shareVars.Store("Longitude", data.Longitude)
  682. //SpeedSlice 用于自车频繁起停触发器-FrequentStartsAndStops
  683. SpeedSlice = append(SpeedSlice, AbsSpeed)
  684. if len(SpeedSlice) > 6000 {
  685. SpeedSlice = SpeedSlice[1:]
  686. }
  687. shareVars.Store("SpeedSlice", SpeedSlice)
  688. /*用于 陡坡-DescendingSteepHill/ClimbingSteepHill 侧翻预警-RolloverWarning 触发器
  689. if NumOfCicvLocation%10==0{
  690. AngleSlice[0] = append(AngleSlice[0], data.Qx)
  691. AngleSlice[1] = append(AngleSlice[1], data.Qy)
  692. AngleSlice[2] = append(AngleSlice[2], data.Qz)
  693. AngleSlice[3] = append(AngleSlice[3], data.Qw)
  694. shareVars.Store("AngleSlice", AngleSlice)
  695. NumOfCicvLocation=1
  696. }
  697. NumOfCicvLocation++
  698. */
  699. /*用于 掉头TurnAround触发器
  700. Yowslice = append(Yowslice, data.Yaw)
  701. if len(Yowslice) >= 600 {
  702. Yowslice = Yowslice[1:]
  703. }
  704. shareVars.Store("Yowslice", Yowslice)
  705. */
  706. // 用于判断是否在车间内
  707. if time.Since(cicvLocationTime).Seconds() > 1 {
  708. p := Point{x: data.PositionX, y: data.PositionY}
  709. OutsideWorkshopFlag := isPointInPolygon(p, vertices) //在车间返回0,不在车间返回1
  710. shareVars.Store("OutsideWorkshopFlag", OutsideWorkshopFlag)
  711. cicvLocationTime = time.Now()
  712. }
  713. },
  714. })
  715. if err == nil {
  716. create = true
  717. }
  718. }
  719. // 13
  720. if topic == masterConfig.TopicOfCloudClusters &&
  721. (len(masterConfig.RuleOfCloudClusters1) > 0 ||
  722. len(masterConfig.RuleOfCloudClusters3) > 0) {
  723. subscribers[i], err = goroslib.NewSubscriber(goroslib.SubscriberConf{
  724. Node: commonConfig.RosNode,
  725. Topic: topic,
  726. Callback: func(data *pjisuv_msgs.AutowareCloudClusterArray) {
  727. subscribersTimeMutexes[i].Lock()
  728. if time.Since(subscribersTimes[i]).Seconds() > triggerInterval {
  729. subscribersMutexes[i].Lock()
  730. faultHappenTime := util.GetNowTimeCustom() // 获取当前故障发生时间
  731. lastTimeWindow := commonEntity.GetLastTimeWindow() // 获取最后一个时间窗口
  732. faultLabel := ""
  733. if len(masterConfig.RuleOfCloudClusters1) > 0 {
  734. for _, f := range masterConfig.RuleOfCloudClusters1 {
  735. faultLabel = f(data)
  736. if faultLabel != "" {
  737. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  738. subscribersTimes[i] = time.Now()
  739. goto TriggerSuccess
  740. }
  741. }
  742. }
  743. if len(masterConfig.RuleOfCloudClusters3) > 0 {
  744. for _, f := range masterConfig.RuleOfCloudClusters3 {
  745. faultLabel = f(shareVars, data)
  746. if faultLabel != "" {
  747. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  748. subscribersTimes[i] = time.Now()
  749. goto TriggerSuccess
  750. }
  751. }
  752. }
  753. TriggerSuccess:
  754. subscribersMutexes[i].Unlock()
  755. }
  756. subscribersTimeMutexes[i].Unlock()
  757. },
  758. })
  759. if err == nil {
  760. create = true
  761. }
  762. }
  763. // 14
  764. if topic == masterConfig.TopicOfHeartbeatInfo &&
  765. (len(masterConfig.RuleOfHeartbeatInfo1) > 0 ||
  766. len(masterConfig.RuleOfHeartbeatInfo3) > 0) {
  767. subscribers[i], err = goroslib.NewSubscriber(goroslib.SubscriberConf{
  768. Node: commonConfig.RosNode,
  769. Topic: topic,
  770. Callback: func(data *pjisuv_msgs.HeartBeatInfo) {
  771. subscribersTimeMutexes[i].Lock()
  772. if time.Since(subscribersTimes[i]).Seconds() > triggerInterval {
  773. subscribersMutexes[i].Lock()
  774. faultHappenTime := util.GetNowTimeCustom() // 获取当前故障发生时间
  775. lastTimeWindow := commonEntity.GetLastTimeWindow() // 获取最后一个时间窗口
  776. faultLabel := ""
  777. if len(masterConfig.RuleOfHeartbeatInfo1) > 0 {
  778. for _, f := range masterConfig.RuleOfHeartbeatInfo1 {
  779. faultLabel = f(data)
  780. if faultLabel != "" {
  781. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  782. subscribersTimes[i] = time.Now()
  783. goto TriggerSuccess
  784. }
  785. }
  786. }
  787. if len(masterConfig.RuleOfHeartbeatInfo3) > 0 {
  788. for _, f := range masterConfig.RuleOfHeartbeatInfo3 {
  789. faultLabel = f(shareVars, data)
  790. if faultLabel != "" {
  791. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  792. subscribersTimes[i] = time.Now()
  793. goto TriggerSuccess
  794. }
  795. }
  796. }
  797. TriggerSuccess:
  798. subscribersMutexes[i].Unlock()
  799. }
  800. subscribersTimeMutexes[i].Unlock()
  801. },
  802. })
  803. if err == nil {
  804. create = true
  805. }
  806. }
  807. // 15
  808. if topic == masterConfig.TopicOfLidarPretreatmentCost &&
  809. (len(masterConfig.RuleOfLidarPretreatmentCost1) > 0 ||
  810. len(masterConfig.RuleOfLidarPretreatmentCost3) > 0) {
  811. subscribers[i], err = goroslib.NewSubscriber(goroslib.SubscriberConf{
  812. Node: commonConfig.RosNode,
  813. Topic: topic,
  814. Callback: func(data *geometry_msgs.Vector3Stamped) {
  815. subscribersTimeMutexes[i].Lock()
  816. if time.Since(subscribersTimes[i]).Seconds() > triggerInterval {
  817. subscribersMutexes[i].Lock()
  818. faultHappenTime := util.GetNowTimeCustom() // 获取当前故障发生时间
  819. lastTimeWindow := commonEntity.GetLastTimeWindow() // 获取最后一个时间窗口
  820. faultLabel := ""
  821. if len(masterConfig.RuleOfLidarPretreatmentCost1) > 0 {
  822. for _, f := range masterConfig.RuleOfLidarPretreatmentCost1 {
  823. faultLabel = f(data)
  824. if faultLabel != "" {
  825. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  826. subscribersTimes[i] = time.Now()
  827. goto TriggerSuccess
  828. }
  829. }
  830. }
  831. if len(masterConfig.RuleOfLidarPretreatmentCost3) > 0 {
  832. for _, f := range masterConfig.RuleOfLidarPretreatmentCost3 {
  833. faultLabel = f(shareVars, data)
  834. if faultLabel != "" {
  835. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  836. subscribersTimes[i] = time.Now()
  837. goto TriggerSuccess
  838. }
  839. }
  840. }
  841. TriggerSuccess:
  842. subscribersMutexes[i].Unlock()
  843. }
  844. subscribersTimeMutexes[i].Unlock()
  845. },
  846. })
  847. if err == nil {
  848. create = true
  849. }
  850. }
  851. // 16
  852. if topic == masterConfig.TopicOfLidarPretreatmentOdometry &&
  853. (len(masterConfig.RuleOfLidarPretreatmentOdometry1) > 0 ||
  854. len(masterConfig.RuleOfLidarPretreatmentOdometry3) > 0) {
  855. subscribers[i], err = goroslib.NewSubscriber(goroslib.SubscriberConf{
  856. Node: commonConfig.RosNode,
  857. Topic: topic,
  858. Callback: func(data *nav_msgs.Odometry) {
  859. subscribersTimeMutexes[i].Lock()
  860. if time.Since(subscribersTimes[i]).Seconds() > triggerInterval {
  861. subscribersMutexes[i].Lock()
  862. faultHappenTime := util.GetNowTimeCustom() // 获取当前故障发生时间
  863. lastTimeWindow := commonEntity.GetLastTimeWindow() // 获取最后一个时间窗口
  864. faultLabel := ""
  865. if len(masterConfig.RuleOfLidarPretreatmentOdometry1) > 0 {
  866. for _, f := range masterConfig.RuleOfLidarPretreatmentOdometry1 {
  867. faultLabel = f(data)
  868. if faultLabel != "" {
  869. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  870. subscribersTimes[i] = time.Now()
  871. goto TriggerSuccess
  872. }
  873. }
  874. }
  875. if len(masterConfig.RuleOfLidarPretreatmentOdometry3) > 0 {
  876. for _, f := range masterConfig.RuleOfLidarPretreatmentOdometry3 {
  877. faultLabel = f(shareVars, data)
  878. if faultLabel != "" {
  879. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  880. subscribersTimes[i] = time.Now()
  881. goto TriggerSuccess
  882. }
  883. }
  884. }
  885. TriggerSuccess:
  886. subscribersMutexes[i].Unlock()
  887. }
  888. subscribersTimeMutexes[i].Unlock()
  889. },
  890. })
  891. if err == nil {
  892. create = true
  893. }
  894. }
  895. // 17
  896. if topic == masterConfig.TopicOfLidarRoi &&
  897. (len(masterConfig.RuleOfLidarRoi1) > 0 ||
  898. len(masterConfig.RuleOfLidarRoi3) > 0) {
  899. subscribers[i], err = goroslib.NewSubscriber(goroslib.SubscriberConf{
  900. Node: commonConfig.RosNode,
  901. Topic: topic,
  902. Callback: func(data *geometry_msgs.PolygonStamped) {
  903. subscribersTimeMutexes[i].Lock()
  904. if time.Since(subscribersTimes[i]).Seconds() > triggerInterval {
  905. subscribersMutexes[i].Lock()
  906. faultHappenTime := util.GetNowTimeCustom() // 获取当前故障发生时间
  907. lastTimeWindow := commonEntity.GetLastTimeWindow() // 获取最后一个时间窗口
  908. faultLabel := ""
  909. if len(masterConfig.RuleOfLidarRoi1) > 0 {
  910. for _, f := range masterConfig.RuleOfLidarRoi1 {
  911. faultLabel = f(data)
  912. if faultLabel != "" {
  913. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  914. subscribersTimes[i] = time.Now()
  915. goto TriggerSuccess
  916. }
  917. }
  918. }
  919. if len(masterConfig.RuleOfLidarRoi3) > 0 {
  920. for _, f := range masterConfig.RuleOfLidarRoi3 {
  921. faultLabel = f(shareVars, data)
  922. if faultLabel != "" {
  923. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  924. subscribersTimes[i] = time.Now()
  925. goto TriggerSuccess
  926. }
  927. }
  928. }
  929. TriggerSuccess:
  930. subscribersMutexes[i].Unlock()
  931. }
  932. subscribersTimeMutexes[i].Unlock()
  933. },
  934. })
  935. if err == nil {
  936. create = true
  937. }
  938. }
  939. // 18
  940. if topic == masterConfig.TopicOfLine1 &&
  941. (len(masterConfig.RuleOfLine11) > 0 ||
  942. len(masterConfig.RuleOfLine13) > 0) {
  943. subscribers[i], err = goroslib.NewSubscriber(goroslib.SubscriberConf{
  944. Node: commonConfig.RosNode,
  945. Topic: topic,
  946. Callback: func(data *nav_msgs.Path) {
  947. subscribersTimeMutexes[i].Lock()
  948. if time.Since(subscribersTimes[i]).Seconds() > triggerInterval {
  949. subscribersMutexes[i].Lock()
  950. faultHappenTime := util.GetNowTimeCustom() // 获取当前故障发生时间
  951. lastTimeWindow := commonEntity.GetLastTimeWindow() // 获取最后一个时间窗口
  952. faultLabel := ""
  953. if len(masterConfig.RuleOfLine11) > 0 {
  954. for _, f := range masterConfig.RuleOfLine11 {
  955. faultLabel = f(data)
  956. if faultLabel != "" {
  957. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  958. subscribersTimes[i] = time.Now()
  959. goto TriggerSuccess
  960. }
  961. }
  962. }
  963. if len(masterConfig.RuleOfLine13) > 0 {
  964. for _, f := range masterConfig.RuleOfLine13 {
  965. faultLabel = f(shareVars, data)
  966. if faultLabel != "" {
  967. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  968. subscribersTimes[i] = time.Now()
  969. goto TriggerSuccess
  970. }
  971. }
  972. }
  973. TriggerSuccess:
  974. subscribersMutexes[i].Unlock()
  975. }
  976. subscribersTimeMutexes[i].Unlock()
  977. },
  978. })
  979. if err == nil {
  980. create = true
  981. }
  982. }
  983. // 19
  984. if topic == masterConfig.TopicOfLine2 &&
  985. (len(masterConfig.RuleOfLine21) > 0 ||
  986. len(masterConfig.RuleOfLine23) > 0) {
  987. subscribers[i], err = goroslib.NewSubscriber(goroslib.SubscriberConf{
  988. Node: commonConfig.RosNode,
  989. Topic: topic,
  990. Callback: func(data *nav_msgs.Path) {
  991. subscribersTimeMutexes[i].Lock()
  992. if time.Since(subscribersTimes[i]).Seconds() > triggerInterval {
  993. subscribersMutexes[i].Lock()
  994. faultHappenTime := util.GetNowTimeCustom() // 获取当前故障发生时间
  995. lastTimeWindow := commonEntity.GetLastTimeWindow() // 获取最后一个时间窗口
  996. faultLabel := ""
  997. if len(masterConfig.RuleOfLine21) > 0 {
  998. for _, f := range masterConfig.RuleOfLine21 {
  999. faultLabel = f(data)
  1000. if faultLabel != "" {
  1001. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  1002. subscribersTimes[i] = time.Now()
  1003. goto TriggerSuccess
  1004. }
  1005. }
  1006. }
  1007. if len(masterConfig.RuleOfLine23) > 0 {
  1008. for _, f := range masterConfig.RuleOfLine23 {
  1009. faultLabel = f(shareVars, data)
  1010. if faultLabel != "" {
  1011. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  1012. subscribersTimes[i] = time.Now()
  1013. goto TriggerSuccess
  1014. }
  1015. }
  1016. }
  1017. TriggerSuccess:
  1018. subscribersMutexes[i].Unlock()
  1019. }
  1020. subscribersTimeMutexes[i].Unlock()
  1021. },
  1022. })
  1023. if err == nil {
  1024. create = true
  1025. }
  1026. }
  1027. // 20
  1028. if topic == masterConfig.TopicOfMapPolygon &&
  1029. (len(masterConfig.RuleOfMapPolygon1) > 0 ||
  1030. len(masterConfig.RuleOfMapPolygon3) > 0) {
  1031. subscribers[i], err = goroslib.NewSubscriber(goroslib.SubscriberConf{
  1032. Node: commonConfig.RosNode,
  1033. Topic: topic,
  1034. Callback: func(data *pjisuv_msgs.PolygonStamped) {
  1035. subscribersTimeMutexes[i].Lock()
  1036. if time.Since(subscribersTimes[i]).Seconds() > triggerInterval {
  1037. subscribersMutexes[i].Lock()
  1038. faultHappenTime := util.GetNowTimeCustom() // 获取当前故障发生时间
  1039. lastTimeWindow := commonEntity.GetLastTimeWindow() // 获取最后一个时间窗口
  1040. faultLabel := ""
  1041. if len(masterConfig.RuleOfMapPolygon1) > 0 {
  1042. for _, f := range masterConfig.RuleOfMapPolygon1 {
  1043. faultLabel = f(data)
  1044. if faultLabel != "" {
  1045. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  1046. subscribersTimes[i] = time.Now()
  1047. goto TriggerSuccess
  1048. }
  1049. }
  1050. }
  1051. if len(masterConfig.RuleOfMapPolygon3) > 0 {
  1052. for _, f := range masterConfig.RuleOfMapPolygon3 {
  1053. faultLabel = f(shareVars, data)
  1054. if faultLabel != "" {
  1055. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  1056. subscribersTimes[i] = time.Now()
  1057. goto TriggerSuccess
  1058. }
  1059. }
  1060. }
  1061. TriggerSuccess:
  1062. subscribersMutexes[i].Unlock()
  1063. }
  1064. subscribersTimeMutexes[i].Unlock()
  1065. },
  1066. })
  1067. if err == nil {
  1068. create = true
  1069. }
  1070. }
  1071. // 21
  1072. if topic == masterConfig.TopicOfObstacleDisplay &&
  1073. (len(masterConfig.RuleOfObstacleDisplay1) > 0 ||
  1074. len(masterConfig.RuleOfObstacleDisplay3) > 0) {
  1075. subscribers[i], err = goroslib.NewSubscriber(goroslib.SubscriberConf{
  1076. Node: commonConfig.RosNode,
  1077. Topic: topic,
  1078. Callback: func(data *visualization_msgs.MarkerArray) {
  1079. subscribersTimeMutexes[i].Lock()
  1080. if time.Since(subscribersTimes[i]).Seconds() > triggerInterval {
  1081. subscribersMutexes[i].Lock()
  1082. faultHappenTime := util.GetNowTimeCustom() // 获取当前故障发生时间
  1083. lastTimeWindow := commonEntity.GetLastTimeWindow() // 获取最后一个时间窗口
  1084. faultLabel := ""
  1085. if len(masterConfig.RuleOfObstacleDisplay1) > 0 {
  1086. for _, f := range masterConfig.RuleOfObstacleDisplay1 {
  1087. faultLabel = f(data)
  1088. if faultLabel != "" {
  1089. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  1090. subscribersTimes[i] = time.Now()
  1091. goto TriggerSuccess
  1092. }
  1093. }
  1094. }
  1095. if len(masterConfig.RuleOfObstacleDisplay3) > 0 {
  1096. for _, f := range masterConfig.RuleOfObstacleDisplay3 {
  1097. faultLabel = f(shareVars, data)
  1098. if faultLabel != "" {
  1099. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  1100. subscribersTimes[i] = time.Now()
  1101. goto TriggerSuccess
  1102. }
  1103. }
  1104. }
  1105. TriggerSuccess:
  1106. subscribersMutexes[i].Unlock()
  1107. }
  1108. subscribersTimeMutexes[i].Unlock()
  1109. },
  1110. })
  1111. if err == nil {
  1112. create = true
  1113. }
  1114. }
  1115. // 22 有共享变量的订阅者必须被创建
  1116. if topic == masterConfig.TopicOfPjControlPub {
  1117. subscribers[i], err = goroslib.NewSubscriber(goroslib.SubscriberConf{
  1118. Node: commonConfig.RosNode,
  1119. Topic: topic,
  1120. Callback: func(data *pjisuv_msgs.CommonVehicleCmd) {
  1121. subscribersTimeMutexes[i].Lock()
  1122. if time.Since(subscribersTimes[i]).Seconds() > triggerInterval {
  1123. subscribersMutexes[i].Lock()
  1124. faultHappenTime := util.GetNowTimeCustom() // 获取当前故障发生时间
  1125. lastTimeWindow := commonEntity.GetLastTimeWindow() // 获取最后一个时间窗口
  1126. faultLabel := ""
  1127. if len(masterConfig.RuleOfPjControlPub1) > 0 {
  1128. for _, f := range masterConfig.RuleOfPjControlPub1 {
  1129. faultLabel = f(data)
  1130. if faultLabel != "" {
  1131. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  1132. subscribersTimes[i] = time.Now()
  1133. goto TriggerSuccess
  1134. }
  1135. }
  1136. }
  1137. if len(masterConfig.RuleOfPjControlPub3) > 0 {
  1138. for _, f := range masterConfig.RuleOfPjControlPub3 {
  1139. faultLabel = f(shareVars, data)
  1140. if faultLabel != "" {
  1141. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  1142. subscribersTimes[i] = time.Now()
  1143. goto TriggerSuccess
  1144. }
  1145. }
  1146. }
  1147. TriggerSuccess:
  1148. subscribersMutexes[i].Unlock()
  1149. }
  1150. subscribersTimeMutexes[i].Unlock()
  1151. // 更新共享变量
  1152. numCountPjiControlCommandOfPjControlPub++
  1153. if numCountPjiControlCommandOfPjControlPub == 10 {
  1154. egoSteeringCmdOfPjControlPub = append(egoSteeringCmdOfPjControlPub, data.ICPVCmdStrAngle)
  1155. egoThrottleCmdOfPjControlPub = append(egoThrottleCmdOfPjControlPub, data.ICPVCmdAccPelPosAct)
  1156. numCountPjiControlCommandOfPjControlPub = 0
  1157. }
  1158. shareVars.Store("NumCountPjiControlCommandOfPjControlPub", numCountPjiControlCommandOfPjControlPub)
  1159. shareVars.Store("EgoSteeringCmdOfPjControlPub", egoSteeringCmdOfPjControlPub)
  1160. shareVars.Store("EgoThrottleCmdOfPjControlPub", egoThrottleCmdOfPjControlPub)
  1161. },
  1162. })
  1163. if err == nil {
  1164. create = true
  1165. }
  1166. }
  1167. // 23
  1168. if topic == masterConfig.TopicOfPointsCluster &&
  1169. (len(masterConfig.RuleOfPointsCluster1) > 0 ||
  1170. len(masterConfig.RuleOfPointsCluster3) > 0) {
  1171. subscribers[i], err = goroslib.NewSubscriber(goroslib.SubscriberConf{
  1172. Node: commonConfig.RosNode,
  1173. Topic: topic,
  1174. Callback: func(data *sensor_msgs.PointCloud2) {
  1175. subscribersTimeMutexes[i].Lock()
  1176. if time.Since(subscribersTimes[i]).Seconds() > triggerInterval {
  1177. subscribersMutexes[i].Lock()
  1178. faultHappenTime := util.GetNowTimeCustom() // 获取当前故障发生时间
  1179. lastTimeWindow := commonEntity.GetLastTimeWindow() // 获取最后一个时间窗口
  1180. faultLabel := ""
  1181. if len(masterConfig.RuleOfPointsCluster1) > 0 {
  1182. for _, f := range masterConfig.RuleOfPointsCluster1 {
  1183. faultLabel = f(data)
  1184. if faultLabel != "" {
  1185. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  1186. subscribersTimes[i] = time.Now()
  1187. goto TriggerSuccess
  1188. }
  1189. }
  1190. }
  1191. if len(masterConfig.RuleOfPointsCluster3) > 0 {
  1192. for _, f := range masterConfig.RuleOfPointsCluster3 {
  1193. faultLabel = f(shareVars, data)
  1194. if faultLabel != "" {
  1195. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  1196. subscribersTimes[i] = time.Now()
  1197. goto TriggerSuccess
  1198. }
  1199. }
  1200. }
  1201. TriggerSuccess:
  1202. subscribersMutexes[i].Unlock()
  1203. }
  1204. subscribersTimeMutexes[i].Unlock()
  1205. },
  1206. })
  1207. if err == nil {
  1208. create = true
  1209. }
  1210. }
  1211. // 24
  1212. if topic == masterConfig.TopicOfPointsConcat &&
  1213. (len(masterConfig.RuleOfPointsConcat1) > 0 ||
  1214. len(masterConfig.RuleOfPointsConcat3) > 0) {
  1215. subscribers[i], err = goroslib.NewSubscriber(goroslib.SubscriberConf{
  1216. Node: commonConfig.RosNode,
  1217. Topic: topic,
  1218. Callback: func(data *sensor_msgs.PointCloud2) {
  1219. subscribersTimeMutexes[i].Lock()
  1220. if time.Since(subscribersTimes[i]).Seconds() > triggerInterval {
  1221. subscribersMutexes[i].Lock()
  1222. faultHappenTime := util.GetNowTimeCustom() // 获取当前故障发生时间
  1223. lastTimeWindow := commonEntity.GetLastTimeWindow() // 获取最后一个时间窗口
  1224. faultLabel := ""
  1225. if len(masterConfig.RuleOfPointsConcat1) > 0 {
  1226. for _, f := range masterConfig.RuleOfPointsConcat1 {
  1227. faultLabel = f(data)
  1228. if faultLabel != "" {
  1229. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  1230. subscribersTimes[i] = time.Now()
  1231. goto TriggerSuccess
  1232. }
  1233. }
  1234. }
  1235. if len(masterConfig.RuleOfPointsConcat3) > 0 {
  1236. for _, f := range masterConfig.RuleOfPointsConcat3 {
  1237. faultLabel = f(shareVars, data)
  1238. if faultLabel != "" {
  1239. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  1240. subscribersTimes[i] = time.Now()
  1241. goto TriggerSuccess
  1242. }
  1243. }
  1244. }
  1245. TriggerSuccess:
  1246. subscribersMutexes[i].Unlock()
  1247. }
  1248. subscribersTimeMutexes[i].Unlock()
  1249. },
  1250. })
  1251. if err == nil {
  1252. create = true
  1253. }
  1254. }
  1255. // 25
  1256. if topic == masterConfig.TopicOfReferenceDisplay &&
  1257. (len(masterConfig.RuleOfReferenceDisplay1) > 0 ||
  1258. len(masterConfig.RuleOfReferenceDisplay3) > 0) {
  1259. subscribers[i], err = goroslib.NewSubscriber(goroslib.SubscriberConf{
  1260. Node: commonConfig.RosNode,
  1261. Topic: topic,
  1262. Callback: func(data *nav_msgs.Path) {
  1263. subscribersTimeMutexes[i].Lock()
  1264. if time.Since(subscribersTimes[i]).Seconds() > triggerInterval {
  1265. subscribersMutexes[i].Lock()
  1266. faultHappenTime := util.GetNowTimeCustom() // 获取当前故障发生时间
  1267. lastTimeWindow := commonEntity.GetLastTimeWindow() // 获取最后一个时间窗口
  1268. faultLabel := ""
  1269. if len(masterConfig.RuleOfReferenceDisplay1) > 0 {
  1270. for _, f := range masterConfig.RuleOfReferenceDisplay1 {
  1271. faultLabel = f(data)
  1272. if faultLabel != "" {
  1273. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  1274. subscribersTimes[i] = time.Now()
  1275. goto TriggerSuccess
  1276. }
  1277. }
  1278. }
  1279. if len(masterConfig.RuleOfReferenceDisplay3) > 0 {
  1280. for _, f := range masterConfig.RuleOfReferenceDisplay3 {
  1281. faultLabel = f(shareVars, data)
  1282. if faultLabel != "" {
  1283. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  1284. subscribersTimes[i] = time.Now()
  1285. goto TriggerSuccess
  1286. }
  1287. }
  1288. }
  1289. TriggerSuccess:
  1290. subscribersMutexes[i].Unlock()
  1291. }
  1292. subscribersTimeMutexes[i].Unlock()
  1293. },
  1294. })
  1295. if err == nil {
  1296. create = true
  1297. }
  1298. }
  1299. // 26
  1300. if topic == masterConfig.TopicOfReferenceTrajectory &&
  1301. (len(masterConfig.RuleOfReferenceTrajectory1) > 0 ||
  1302. len(masterConfig.RuleOfReferenceTrajectory3) > 0) {
  1303. subscribers[i], err = goroslib.NewSubscriber(goroslib.SubscriberConf{
  1304. Node: commonConfig.RosNode,
  1305. Topic: topic,
  1306. Callback: func(data *pjisuv_msgs.Trajectory) {
  1307. subscribersTimeMutexes[i].Lock()
  1308. if time.Since(subscribersTimes[i]).Seconds() > triggerInterval {
  1309. subscribersMutexes[i].Lock()
  1310. faultHappenTime := util.GetNowTimeCustom() // 获取当前故障发生时间
  1311. lastTimeWindow := commonEntity.GetLastTimeWindow() // 获取最后一个时间窗口
  1312. faultLabel := ""
  1313. if len(masterConfig.RuleOfReferenceTrajectory1) > 0 {
  1314. for _, f := range masterConfig.RuleOfReferenceTrajectory1 {
  1315. faultLabel = f(data)
  1316. if faultLabel != "" {
  1317. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  1318. subscribersTimes[i] = time.Now()
  1319. goto TriggerSuccess
  1320. }
  1321. }
  1322. }
  1323. if len(masterConfig.RuleOfReferenceTrajectory3) > 0 {
  1324. for _, f := range masterConfig.RuleOfReferenceTrajectory3 {
  1325. faultLabel = f(shareVars, data)
  1326. if faultLabel != "" {
  1327. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  1328. subscribersTimes[i] = time.Now()
  1329. goto TriggerSuccess
  1330. }
  1331. }
  1332. }
  1333. TriggerSuccess:
  1334. subscribersMutexes[i].Unlock()
  1335. }
  1336. subscribersTimeMutexes[i].Unlock()
  1337. },
  1338. })
  1339. if err == nil {
  1340. create = true
  1341. }
  1342. }
  1343. // 27
  1344. if topic == masterConfig.TopicOfRoiPoints &&
  1345. (len(masterConfig.RuleOfRoiPoints1) > 0 ||
  1346. len(masterConfig.RuleOfRoiPoints3) > 0) {
  1347. subscribers[i], err = goroslib.NewSubscriber(goroslib.SubscriberConf{
  1348. Node: commonConfig.RosNode,
  1349. Topic: topic,
  1350. Callback: func(data *sensor_msgs.PointCloud2) {
  1351. subscribersTimeMutexes[i].Lock()
  1352. if time.Since(subscribersTimes[i]).Seconds() > triggerInterval {
  1353. subscribersMutexes[i].Lock()
  1354. faultHappenTime := util.GetNowTimeCustom() // 获取当前故障发生时间
  1355. lastTimeWindow := commonEntity.GetLastTimeWindow() // 获取最后一个时间窗口
  1356. faultLabel := ""
  1357. if len(masterConfig.RuleOfRoiPoints1) > 0 {
  1358. for _, f := range masterConfig.RuleOfRoiPoints1 {
  1359. faultLabel = f(data)
  1360. if faultLabel != "" {
  1361. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  1362. subscribersTimes[i] = time.Now()
  1363. goto TriggerSuccess
  1364. }
  1365. }
  1366. }
  1367. if len(masterConfig.RuleOfRoiPoints3) > 0 {
  1368. for _, f := range masterConfig.RuleOfRoiPoints3 {
  1369. faultLabel = f(shareVars, data)
  1370. if faultLabel != "" {
  1371. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  1372. subscribersTimes[i] = time.Now()
  1373. goto TriggerSuccess
  1374. }
  1375. }
  1376. }
  1377. TriggerSuccess:
  1378. subscribersMutexes[i].Unlock()
  1379. }
  1380. subscribersTimeMutexes[i].Unlock()
  1381. },
  1382. })
  1383. if err == nil {
  1384. create = true
  1385. }
  1386. }
  1387. // 28
  1388. if topic == masterConfig.TopicOfRoiPolygon &&
  1389. (len(masterConfig.RuleOfRoiPolygon1) > 0 ||
  1390. len(masterConfig.RuleOfRoiPolygon3) > 0) {
  1391. subscribers[i], err = goroslib.NewSubscriber(goroslib.SubscriberConf{
  1392. Node: commonConfig.RosNode,
  1393. Topic: topic,
  1394. Callback: func(data *nav_msgs.Path) {
  1395. subscribersTimeMutexes[i].Lock()
  1396. if time.Since(subscribersTimes[i]).Seconds() > triggerInterval {
  1397. subscribersMutexes[i].Lock()
  1398. faultHappenTime := util.GetNowTimeCustom() // 获取当前故障发生时间
  1399. lastTimeWindow := commonEntity.GetLastTimeWindow() // 获取最后一个时间窗口
  1400. faultLabel := ""
  1401. if len(masterConfig.RuleOfRoiPolygon1) > 0 {
  1402. for _, f := range masterConfig.RuleOfRoiPolygon1 {
  1403. faultLabel = f(data)
  1404. if faultLabel != "" {
  1405. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  1406. subscribersTimes[i] = time.Now()
  1407. goto TriggerSuccess
  1408. }
  1409. }
  1410. }
  1411. if len(masterConfig.RuleOfRoiPolygon3) > 0 {
  1412. for _, f := range masterConfig.RuleOfRoiPolygon3 {
  1413. faultLabel = f(shareVars, data)
  1414. if faultLabel != "" {
  1415. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  1416. subscribersTimes[i] = time.Now()
  1417. goto TriggerSuccess
  1418. }
  1419. }
  1420. }
  1421. TriggerSuccess:
  1422. subscribersMutexes[i].Unlock()
  1423. }
  1424. subscribersTimeMutexes[i].Unlock()
  1425. },
  1426. })
  1427. if err == nil {
  1428. create = true
  1429. }
  1430. }
  1431. // 29
  1432. if topic == masterConfig.TopicOfTf &&
  1433. (len(masterConfig.RuleOfTf1) > 0 ||
  1434. len(masterConfig.RuleOfTf3) > 0) {
  1435. subscribers[i], err = goroslib.NewSubscriber(goroslib.SubscriberConf{
  1436. Node: commonConfig.RosNode,
  1437. Topic: topic,
  1438. Callback: func(data *tf2_msgs.TFMessage) {
  1439. subscribersTimeMutexes[i].Lock()
  1440. if time.Since(subscribersTimes[i]).Seconds() > triggerInterval {
  1441. subscribersMutexes[i].Lock()
  1442. faultHappenTime := util.GetNowTimeCustom() // 获取当前故障发生时间
  1443. lastTimeWindow := commonEntity.GetLastTimeWindow() // 获取最后一个时间窗口
  1444. faultLabel := ""
  1445. if len(masterConfig.RuleOfTf1) > 0 {
  1446. for _, f := range masterConfig.RuleOfTf1 {
  1447. faultLabel = f(data)
  1448. if faultLabel != "" {
  1449. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  1450. subscribersTimes[i] = time.Now()
  1451. goto TriggerSuccess
  1452. }
  1453. }
  1454. }
  1455. if len(masterConfig.RuleOfTf3) > 0 {
  1456. for _, f := range masterConfig.RuleOfTf3 {
  1457. faultLabel = f(shareVars, data)
  1458. if faultLabel != "" {
  1459. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  1460. subscribersTimes[i] = time.Now()
  1461. goto TriggerSuccess
  1462. }
  1463. }
  1464. }
  1465. TriggerSuccess:
  1466. subscribersMutexes[i].Unlock()
  1467. }
  1468. subscribersTimeMutexes[i].Unlock()
  1469. },
  1470. })
  1471. if err == nil {
  1472. create = true
  1473. }
  1474. }
  1475. // 30 有共享变量的订阅者必须被创建
  1476. if topic == masterConfig.TopicOfTpperception {
  1477. subscribers[i], err = goroslib.NewSubscriber(goroslib.SubscriberConf{
  1478. Node: commonConfig.RosNode,
  1479. Topic: topic,
  1480. Callback: func(data *pjisuv_msgs.PerceptionObjects) {
  1481. subscribersTimeMutexes[i].Lock()
  1482. if time.Since(subscribersTimes[i]).Seconds() > triggerInterval {
  1483. subscribersMutexes[i].Lock()
  1484. faultHappenTime := util.GetNowTimeCustom() // 获取当前故障发生时间
  1485. lastTimeWindow := commonEntity.GetLastTimeWindow() // 获取最后一个时间窗口
  1486. faultLabel := ""
  1487. if len(masterConfig.RuleOfTpperception1) > 0 {
  1488. for _, f := range masterConfig.RuleOfTpperception1 {
  1489. faultLabel = f(data)
  1490. if faultLabel != "" {
  1491. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  1492. subscribersTimes[i] = time.Now()
  1493. goto TriggerSuccess
  1494. }
  1495. }
  1496. }
  1497. if len(masterConfig.RuleOfTpperception3) > 0 {
  1498. for _, f := range masterConfig.RuleOfTpperception3 {
  1499. faultLabel = f(shareVars, data)
  1500. if faultLabel != "" {
  1501. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  1502. subscribersTimes[i] = time.Now()
  1503. goto TriggerSuccess
  1504. }
  1505. }
  1506. }
  1507. TriggerSuccess:
  1508. subscribersMutexes[i].Unlock()
  1509. }
  1510. subscribersTimeMutexes[i].Unlock()
  1511. // 更新共享变量
  1512. for _, obj := range data.Objs {
  1513. YawOfCicvLocation, _ := shareVars.Load("YawOfCicvLocation")
  1514. diffh := (float64(obj.Heading - YawOfCicvLocation.(float32)))
  1515. if diffh < -180.0 {
  1516. diffh = 360.0 + diffh
  1517. } else if diffh > 180.0 {
  1518. diffh = 360.0 - diffh
  1519. } else {
  1520. diffh = math.Abs(diffh)
  1521. }
  1522. if math.Abs(float64(obj.X)) >= 30 || math.Abs(float64(obj.Y)) >= 30 {
  1523. continue
  1524. }
  1525. if _, ok := ObjDicOfTpperception[obj.Id]; !ok {
  1526. ObjDicOfTpperception[obj.Id] = [][]float32{{}, {}, {}, {}, {}, {}, {}, {}}
  1527. }
  1528. ObjDicOfTpperception[obj.Id][0] = append(ObjDicOfTpperception[obj.Id][0], obj.X)
  1529. ObjDicOfTpperception[obj.Id][1] = append(ObjDicOfTpperception[obj.Id][1], obj.Y)
  1530. ObjDicOfTpperception[obj.Id][2] = append(ObjDicOfTpperception[obj.Id][2], obj.Vxrel)
  1531. absspeed := math.Sqrt(math.Pow(float64(obj.Vxabs), 2) + math.Pow(float64(obj.Vyabs), 2))
  1532. ObjDicOfTpperception[obj.Id][3] = append(ObjDicOfTpperception[obj.Id][3], float32(absspeed))
  1533. ObjDicOfTpperception[obj.Id][4] = append(ObjDicOfTpperception[obj.Id][4], obj.Heading)
  1534. ObjDicOfTpperception[obj.Id][5] = append(ObjDicOfTpperception[obj.Id][5], Frame)
  1535. ObjDicOfTpperception[obj.Id][6] = append(ObjDicOfTpperception[obj.Id][6], float32(obj.Type))
  1536. ObjDicOfTpperception[obj.Id][7] = append(ObjDicOfTpperception[obj.Id][7], float32(diffh))
  1537. if len(ObjDicOfTpperception[obj.Id][0]) >= 100 {
  1538. ObjDicOfTpperception[obj.Id][0] = ObjDicOfTpperception[obj.Id][0][1:]
  1539. ObjDicOfTpperception[obj.Id][1] = ObjDicOfTpperception[obj.Id][1][1:]
  1540. ObjDicOfTpperception[obj.Id][2] = ObjDicOfTpperception[obj.Id][2][1:]
  1541. ObjDicOfTpperception[obj.Id][3] = ObjDicOfTpperception[obj.Id][3][1:]
  1542. ObjDicOfTpperception[obj.Id][4] = ObjDicOfTpperception[obj.Id][4][1:]
  1543. ObjDicOfTpperception[obj.Id][5] = ObjDicOfTpperception[obj.Id][5][1:]
  1544. ObjDicOfTpperception[obj.Id][6] = ObjDicOfTpperception[obj.Id][6][1:]
  1545. ObjDicOfTpperception[obj.Id][7] = ObjDicOfTpperception[obj.Id][7][1:]
  1546. }
  1547. objTypeDicOfTpperception[obj.Id] = obj.Type
  1548. objSpeedDicOfTpperception[obj.Id] = math.Pow(math.Pow(float64(obj.Vxabs), 2)+math.Pow(float64(obj.Vyabs), 2), 0.5)
  1549. }
  1550. shareVars.Store("ObjDicOfTpperception", ObjDicOfTpperception)
  1551. shareVars.Store("ObjTypeDicOfTpperception", objTypeDicOfTpperception)
  1552. shareVars.Store("ObjSpeedDicOfTpperception", objSpeedDicOfTpperception)
  1553. shareVars.Store("countSideBySide", countSideBySide)
  1554. Frame++
  1555. },
  1556. })
  1557. if err == nil {
  1558. create = true
  1559. }
  1560. }
  1561. // 31
  1562. if topic == masterConfig.TopicOfTpperceptionVis &&
  1563. (len(masterConfig.RuleOfTpperceptionVis1) > 0 ||
  1564. len(masterConfig.RuleOfTpperceptionVis3) > 0) {
  1565. subscribers[i], err = goroslib.NewSubscriber(goroslib.SubscriberConf{
  1566. Node: commonConfig.RosNode,
  1567. Topic: topic,
  1568. Callback: func(data *visualization_msgs.MarkerArray) {
  1569. subscribersTimeMutexes[i].Lock()
  1570. if time.Since(subscribersTimes[i]).Seconds() > triggerInterval {
  1571. subscribersMutexes[i].Lock()
  1572. faultHappenTime := util.GetNowTimeCustom() // 获取当前故障发生时间
  1573. lastTimeWindow := commonEntity.GetLastTimeWindow() // 获取最后一个时间窗口
  1574. faultLabel := ""
  1575. if len(masterConfig.RuleOfTpperceptionVis1) > 0 {
  1576. for _, f := range masterConfig.RuleOfTpperceptionVis1 {
  1577. faultLabel = f(data)
  1578. if faultLabel != "" {
  1579. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  1580. subscribersTimes[i] = time.Now()
  1581. goto TriggerSuccess
  1582. }
  1583. }
  1584. }
  1585. if len(masterConfig.RuleOfTpperceptionVis3) > 0 {
  1586. for _, f := range masterConfig.RuleOfTpperceptionVis3 {
  1587. faultLabel = f(shareVars, data)
  1588. if faultLabel != "" {
  1589. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  1590. subscribersTimes[i] = time.Now()
  1591. goto TriggerSuccess
  1592. }
  1593. }
  1594. }
  1595. TriggerSuccess:
  1596. subscribersMutexes[i].Unlock()
  1597. }
  1598. subscribersTimeMutexes[i].Unlock()
  1599. },
  1600. })
  1601. if err == nil {
  1602. create = true
  1603. }
  1604. }
  1605. // 32
  1606. if topic == masterConfig.TopicOfTprouteplan &&
  1607. (len(masterConfig.RuleOfTprouteplan1) > 0 ||
  1608. len(masterConfig.RuleOfTprouteplan3) > 0) {
  1609. subscribers[i], err = goroslib.NewSubscriber(goroslib.SubscriberConf{
  1610. Node: commonConfig.RosNode,
  1611. Topic: topic,
  1612. Callback: func(data *pjisuv_msgs.RoutePlan) {
  1613. subscribersTimeMutexes[i].Lock()
  1614. if time.Since(subscribersTimes[i]).Seconds() > triggerInterval {
  1615. subscribersMutexes[i].Lock()
  1616. faultHappenTime := util.GetNowTimeCustom() // 获取当前故障发生时间
  1617. lastTimeWindow := commonEntity.GetLastTimeWindow() // 获取最后一个时间窗口
  1618. faultLabel := ""
  1619. if len(masterConfig.RuleOfTprouteplan1) > 0 {
  1620. for _, f := range masterConfig.RuleOfTprouteplan1 {
  1621. faultLabel = f(data)
  1622. if faultLabel != "" {
  1623. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  1624. subscribersTimes[i] = time.Now()
  1625. goto TriggerSuccess
  1626. }
  1627. }
  1628. }
  1629. if len(masterConfig.RuleOfTprouteplan3) > 0 {
  1630. for _, f := range masterConfig.RuleOfTprouteplan3 {
  1631. faultLabel = f(shareVars, data)
  1632. if faultLabel != "" {
  1633. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  1634. subscribersTimes[i] = time.Now()
  1635. goto TriggerSuccess
  1636. }
  1637. }
  1638. }
  1639. TriggerSuccess:
  1640. subscribersMutexes[i].Unlock()
  1641. }
  1642. subscribersTimeMutexes[i].Unlock()
  1643. },
  1644. })
  1645. if err == nil {
  1646. create = true
  1647. }
  1648. }
  1649. // 33
  1650. if topic == masterConfig.TopicOfTrajectoryDisplay &&
  1651. (len(masterConfig.RuleOfTrajectoryDisplay1) > 0 ||
  1652. len(masterConfig.RuleOfTrajectoryDisplay3) > 0) {
  1653. subscribers[i], err = goroslib.NewSubscriber(goroslib.SubscriberConf{
  1654. Node: commonConfig.RosNode,
  1655. Topic: topic,
  1656. Callback: func(data *nav_msgs.Path) {
  1657. subscribersTimeMutexes[i].Lock()
  1658. if time.Since(subscribersTimes[i]).Seconds() > triggerInterval {
  1659. subscribersMutexes[i].Lock()
  1660. faultHappenTime := util.GetNowTimeCustom() // 获取当前故障发生时间
  1661. lastTimeWindow := commonEntity.GetLastTimeWindow() // 获取最后一个时间窗口
  1662. faultLabel := ""
  1663. if len(masterConfig.RuleOfTrajectoryDisplay1) > 0 {
  1664. for _, f := range masterConfig.RuleOfTrajectoryDisplay1 {
  1665. faultLabel = f(data)
  1666. if faultLabel != "" {
  1667. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  1668. subscribersTimes[i] = time.Now()
  1669. goto TriggerSuccess
  1670. }
  1671. }
  1672. }
  1673. if len(masterConfig.RuleOfTrajectoryDisplay3) > 0 {
  1674. for _, f := range masterConfig.RuleOfTrajectoryDisplay3 {
  1675. faultLabel = f(shareVars, data)
  1676. if faultLabel != "" {
  1677. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  1678. subscribersTimes[i] = time.Now()
  1679. goto TriggerSuccess
  1680. }
  1681. }
  1682. }
  1683. TriggerSuccess:
  1684. subscribersMutexes[i].Unlock()
  1685. }
  1686. subscribersTimeMutexes[i].Unlock()
  1687. },
  1688. })
  1689. if err == nil {
  1690. create = true
  1691. }
  1692. }
  1693. // 34
  1694. if topic == masterConfig.TopicOfUngroundCloudpoints &&
  1695. (len(masterConfig.RuleOfUngroundCloudpoints1) > 0 ||
  1696. len(masterConfig.RuleOfUngroundCloudpoints3) > 0) {
  1697. subscribers[i], err = goroslib.NewSubscriber(goroslib.SubscriberConf{
  1698. Node: commonConfig.RosNode,
  1699. Topic: topic,
  1700. Callback: func(data *sensor_msgs.PointCloud2) {
  1701. subscribersTimeMutexes[i].Lock()
  1702. if time.Since(subscribersTimes[i]).Seconds() > triggerInterval {
  1703. subscribersMutexes[i].Lock()
  1704. faultHappenTime := util.GetNowTimeCustom() // 获取当前故障发生时间
  1705. lastTimeWindow := commonEntity.GetLastTimeWindow() // 获取最后一个时间窗口
  1706. faultLabel := ""
  1707. if len(masterConfig.RuleOfUngroundCloudpoints1) > 0 {
  1708. for _, f := range masterConfig.RuleOfUngroundCloudpoints1 {
  1709. faultLabel = f(data)
  1710. if faultLabel != "" {
  1711. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  1712. subscribersTimes[i] = time.Now()
  1713. goto TriggerSuccess
  1714. }
  1715. }
  1716. }
  1717. if len(masterConfig.RuleOfUngroundCloudpoints3) > 0 {
  1718. for _, f := range masterConfig.RuleOfUngroundCloudpoints3 {
  1719. faultLabel = f(shareVars, data)
  1720. if faultLabel != "" {
  1721. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  1722. subscribersTimes[i] = time.Now()
  1723. goto TriggerSuccess
  1724. }
  1725. }
  1726. }
  1727. TriggerSuccess:
  1728. subscribersMutexes[i].Unlock()
  1729. }
  1730. subscribersTimeMutexes[i].Unlock()
  1731. },
  1732. })
  1733. if err == nil {
  1734. create = true
  1735. }
  1736. }
  1737. // 35
  1738. if topic == masterConfig.TopicOfCameraImage &&
  1739. (len(masterConfig.RuleOfCameraImage1) > 0 ||
  1740. len(masterConfig.RuleOfCameraImage3) > 0) {
  1741. subscribers[i], err = goroslib.NewSubscriber(goroslib.SubscriberConf{
  1742. Node: commonConfig.RosNode,
  1743. Topic: topic,
  1744. Callback: func(data *sensor_msgs.Image) {
  1745. subscribersTimeMutexes[i].Lock()
  1746. if time.Since(subscribersTimes[i]).Seconds() > triggerInterval {
  1747. subscribersMutexes[i].Lock()
  1748. faultHappenTime := util.GetNowTimeCustom() // 获取当前故障发生时间
  1749. lastTimeWindow := commonEntity.GetLastTimeWindow() // 获取最后一个时间窗口
  1750. faultLabel := ""
  1751. if len(masterConfig.RuleOfCameraImage1) > 0 {
  1752. for _, f := range masterConfig.RuleOfCameraImage1 {
  1753. faultLabel = f(data)
  1754. if faultLabel != "" {
  1755. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  1756. subscribersTimes[i] = time.Now()
  1757. goto TriggerSuccess
  1758. }
  1759. }
  1760. }
  1761. if len(masterConfig.RuleOfCameraImage3) > 0 {
  1762. for _, f := range masterConfig.RuleOfCameraImage3 {
  1763. faultLabel = f(shareVars, data)
  1764. if faultLabel != "" {
  1765. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  1766. subscribersTimes[i] = time.Now()
  1767. goto TriggerSuccess
  1768. }
  1769. }
  1770. }
  1771. TriggerSuccess:
  1772. subscribersMutexes[i].Unlock()
  1773. }
  1774. subscribersTimeMutexes[i].Unlock()
  1775. },
  1776. })
  1777. if err == nil {
  1778. create = true
  1779. }
  1780. }
  1781. // 36 有共享变量的订阅者必须被创建
  1782. if topic == masterConfig.TopicOfDataRead {
  1783. subscribers[i], err = goroslib.NewSubscriber(goroslib.SubscriberConf{
  1784. Node: commonConfig.RosNode,
  1785. Topic: topic,
  1786. Callback: func(data *pjisuv_msgs.Retrieval) {
  1787. subscribersTimeMutexes[i].Lock()
  1788. if time.Since(subscribersTimes[i]).Seconds() > triggerInterval {
  1789. subscribersMutexes[i].Lock()
  1790. faultHappenTime := util.GetNowTimeCustom() // 获取当前故障发生时间
  1791. lastTimeWindow := commonEntity.GetLastTimeWindow() // 获取最后一个时间窗口
  1792. faultLabel := ""
  1793. if len(masterConfig.RuleOfDataRead1) > 0 {
  1794. for _, f := range masterConfig.RuleOfDataRead1 {
  1795. faultLabel = f(data)
  1796. if faultLabel != "" {
  1797. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  1798. subscribersTimes[i] = time.Now()
  1799. goto TriggerSuccess
  1800. }
  1801. }
  1802. }
  1803. if len(masterConfig.RuleOfDataRead3) > 0 {
  1804. for _, f := range masterConfig.RuleOfDataRead3 {
  1805. faultLabel = f(shareVars, data)
  1806. if faultLabel != "" {
  1807. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  1808. subscribersTimes[i] = time.Now()
  1809. goto TriggerSuccess
  1810. }
  1811. }
  1812. }
  1813. TriggerSuccess:
  1814. subscribersMutexes[i].Unlock()
  1815. }
  1816. subscribersTimeMutexes[i].Unlock()
  1817. // 更新共享变量
  1818. numCountDataReadOfDataRead++
  1819. if numCountDataReadOfDataRead == 10 {
  1820. egoSteeringRealOfDataRead = append(egoSteeringRealOfDataRead, data.ActStrWhAng)
  1821. egoThrottleRealOfDataRead = append(egoThrottleRealOfDataRead, data.AccPed2)
  1822. numCountDataReadOfDataRead = 0
  1823. }
  1824. GearPosSlice = append(GearPosSlice, data.GearPos)
  1825. shareVars.Store("NumCountDataReadOfDataRead", numCountDataReadOfDataRead)
  1826. shareVars.Store("EgoSteeringRealOfDataRead", egoSteeringRealOfDataRead)
  1827. shareVars.Store("EgoThrottleRealOfDataRead", egoThrottleRealOfDataRead)
  1828. shareVars.Store("ActStrWhAngOfDataRead", data.ActStrWhAng)
  1829. shareVars.Store("GearPosSlice", GearPosSlice)
  1830. },
  1831. })
  1832. if err == nil {
  1833. create = true
  1834. }
  1835. }
  1836. // 37
  1837. if topic == masterConfig.TopicOfPjiGps &&
  1838. (len(masterConfig.RuleOfPjiGps1) > 0 ||
  1839. len(masterConfig.RuleOfPjiGps3) > 0) {
  1840. subscribers[i], err = goroslib.NewSubscriber(goroslib.SubscriberConf{
  1841. Node: commonConfig.RosNode,
  1842. Topic: topic,
  1843. Callback: func(data *pjisuv_msgs.PerceptionLocalization) {
  1844. subscribersTimeMutexes[i].Lock()
  1845. if time.Since(subscribersTimes[i]).Seconds() > triggerInterval {
  1846. subscribersMutexes[i].Lock()
  1847. faultHappenTime := util.GetNowTimeCustom() // 获取当前故障发生时间
  1848. lastTimeWindow := commonEntity.GetLastTimeWindow() // 获取最后一个时间窗口
  1849. faultLabel := ""
  1850. if len(masterConfig.RuleOfPjiGps1) > 0 {
  1851. for _, f := range masterConfig.RuleOfPjiGps1 {
  1852. faultLabel = f(data)
  1853. if faultLabel != "" {
  1854. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  1855. subscribersTimes[i] = time.Now()
  1856. goto TriggerSuccess
  1857. }
  1858. }
  1859. }
  1860. if len(masterConfig.RuleOfPjiGps3) > 0 {
  1861. for _, f := range masterConfig.RuleOfPjiGps3 {
  1862. faultLabel = f(shareVars, data)
  1863. if faultLabel != "" {
  1864. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  1865. subscribersTimes[i] = time.Now()
  1866. goto TriggerSuccess
  1867. }
  1868. }
  1869. }
  1870. TriggerSuccess:
  1871. subscribersMutexes[i].Unlock()
  1872. }
  1873. subscribersTimeMutexes[i].Unlock()
  1874. },
  1875. })
  1876. if err == nil {
  1877. create = true
  1878. }
  1879. }
  1880. // 38
  1881. if topic == masterConfig.TopicOfFaultInfo &&
  1882. (len(masterConfig.RuleOfFaultInfo1) > 0 ||
  1883. len(masterConfig.RuleOfFaultInfo3) > 0) {
  1884. subscribers[i], err = goroslib.NewSubscriber(goroslib.SubscriberConf{
  1885. Node: commonConfig.RosNode,
  1886. Topic: topic,
  1887. Callback: func(data *pjisuv_msgs.FaultVec) {
  1888. subscribersTimeMutexes[i].Lock()
  1889. if time.Since(subscribersTimes[i]).Seconds() > triggerInterval {
  1890. subscribersMutexes[i].Lock()
  1891. faultHappenTime := util.GetNowTimeCustom() // 获取当前故障发生时间
  1892. lastTimeWindow := commonEntity.GetLastTimeWindow() // 获取最后一个时间窗口
  1893. faultLabel := ""
  1894. if len(masterConfig.RuleOfFaultInfo1) > 0 {
  1895. for _, f := range masterConfig.RuleOfFaultInfo1 {
  1896. faultLabel = f(data)
  1897. if faultLabel != "" {
  1898. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  1899. subscribersTimes[i] = time.Now()
  1900. goto TriggerSuccess
  1901. }
  1902. }
  1903. }
  1904. if len(masterConfig.RuleOfFaultInfo3) > 0 {
  1905. for _, f := range masterConfig.RuleOfFaultInfo3 {
  1906. faultLabel = f(shareVars, data)
  1907. if faultLabel != "" {
  1908. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  1909. subscribersTimes[i] = time.Now()
  1910. goto TriggerSuccess
  1911. }
  1912. }
  1913. }
  1914. TriggerSuccess:
  1915. subscribersMutexes[i].Unlock()
  1916. }
  1917. subscribersTimeMutexes[i].Unlock()
  1918. },
  1919. })
  1920. if err == nil {
  1921. create = true
  1922. }
  1923. }
  1924. // 39 有共享变量的订阅者必须被创建
  1925. if topic == masterConfig.TopicOfPjVehicleFdbPub {
  1926. subscribers[i], err = goroslib.NewSubscriber(goroslib.SubscriberConf{
  1927. Node: commonConfig.RosNode,
  1928. Topic: topic,
  1929. Callback: func(data *pjisuv_msgs.VehicleFdb) {
  1930. subscribersTimeMutexes[i].Lock()
  1931. if time.Since(subscribersTimes[i]).Seconds() > triggerInterval {
  1932. subscribersMutexes[i].Lock()
  1933. faultHappenTime := util.GetNowTimeCustom() // 获取当前故障发生时间
  1934. lastTimeWindow := commonEntity.GetLastTimeWindow() // 获取最后一个时间窗口
  1935. faultLabel := ""
  1936. if len(masterConfig.RuleOfPjVehicleFdbPub1) > 0 {
  1937. for _, f := range masterConfig.RuleOfPjVehicleFdbPub1 {
  1938. faultLabel = f(data)
  1939. if faultLabel != "" {
  1940. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  1941. subscribersTimes[i] = time.Now()
  1942. goto TriggerSuccess
  1943. }
  1944. }
  1945. }
  1946. if len(masterConfig.RuleOfPjVehicleFdbPub3) > 0 {
  1947. for _, f := range masterConfig.RuleOfPjVehicleFdbPub3 {
  1948. faultLabel = f(shareVars, data)
  1949. if faultLabel != "" {
  1950. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  1951. subscribersTimes[i] = time.Now()
  1952. goto TriggerSuccess
  1953. }
  1954. }
  1955. }
  1956. TriggerSuccess:
  1957. subscribersMutexes[i].Unlock()
  1958. }
  1959. subscribersTimeMutexes[i].Unlock()
  1960. // 更新共享变量
  1961. shareVars.Store("AutomodeOfPjVehicleFdbPub", data.Automode)
  1962. },
  1963. })
  1964. if err == nil {
  1965. create = true
  1966. }
  1967. }
  1968. // 40 有共享变量的订阅者必须被创建
  1969. if topic == masterConfig.TopicOfEndPointMessage {
  1970. subscribers[i], err = goroslib.NewSubscriber(goroslib.SubscriberConf{
  1971. Node: commonConfig.RosNode,
  1972. Topic: topic,
  1973. Callback: func(data *geometry_msgs.Point) {
  1974. subscribersTimeMutexes[i].Lock()
  1975. if time.Since(subscribersTimes[i]).Seconds() > triggerInterval {
  1976. subscribersMutexes[i].Lock()
  1977. faultHappenTime := util.GetNowTimeCustom() // 获取当前故障发生时间
  1978. lastTimeWindow := commonEntity.GetLastTimeWindow() // 获取最后一个时间窗口
  1979. faultLabel := ""
  1980. if len(masterConfig.RuleOfEndPointMessage1) > 0 {
  1981. for _, f := range masterConfig.RuleOfEndPointMessage1 {
  1982. faultLabel = f(data)
  1983. if faultLabel != "" {
  1984. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  1985. subscribersTimes[i] = time.Now()
  1986. goto TriggerSuccess
  1987. }
  1988. }
  1989. }
  1990. if len(masterConfig.RuleOfEndPointMessage3) > 0 {
  1991. for _, f := range masterConfig.RuleOfEndPointMessage3 {
  1992. faultLabel = f(shareVars, data)
  1993. if faultLabel != "" {
  1994. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  1995. subscribersTimes[i] = time.Now()
  1996. goto TriggerSuccess
  1997. }
  1998. }
  1999. }
  2000. TriggerSuccess:
  2001. subscribersMutexes[i].Unlock()
  2002. }
  2003. subscribersTimeMutexes[i].Unlock()
  2004. // 更新共享变量
  2005. shareVars.Store("EndPointX", data.X)
  2006. shareVars.Store("EndPointY", data.Y)
  2007. },
  2008. })
  2009. if err == nil {
  2010. create = true
  2011. }
  2012. }
  2013. if err != nil {
  2014. c_log.GlobalLogger.Infof("创建订阅者报错,可能由于节点未启动,再次尝试【%v】", err)
  2015. time.Sleep(time.Duration(2) * time.Second)
  2016. continue
  2017. } else {
  2018. if create {
  2019. c_log.GlobalLogger.Infof("创建订阅者订阅话题【%v】", topic)
  2020. }
  2021. break
  2022. }
  2023. }
  2024. }
  2025. c_log.GlobalLogger.Infof("全部订阅者创建完成。")
  2026. select {
  2027. case signal := <-service.ChannelKillWindowProducer:
  2028. if signal == 1 {
  2029. commonConfig.RosNode.Close()
  2030. service.AddKillTimes("3")
  2031. return
  2032. }
  2033. }
  2034. }
  2035. func saveTimeWindow(faultLabel string, faultHappenTime string, lastTimeWindow *commonEntity.TimeWindow) {
  2036. saveTimeWindowMutex.Lock()
  2037. defer saveTimeWindowMutex.Unlock()
  2038. masterTopics, slaveTopics := getTopicsOfNode(faultLabel)
  2039. if lastTimeWindow == nil || util.TimeCustom1GreaterTimeCustom2(faultHappenTime, lastTimeWindow.TimeWindowEnd) { // 如果是不在旧故障窗口内,添加一个新窗口
  2040. exceptBegin := util.TimeCustomChange(faultHappenTime, -commonConfig.PlatformConfig.TaskBeforeTime)
  2041. finalTimeWindowBegin := ""
  2042. if util.TimeCustom1LessEqualThanTimeCustom2(exceptBegin, latestTimeWindowEnd) { // 窗口最早时间不能早于上一个窗口结束时间
  2043. finalTimeWindowBegin = latestTimeWindowEnd
  2044. } else {
  2045. finalTimeWindowBegin = exceptBegin
  2046. }
  2047. latestTimeWindowEnd = util.TimeCustomChange(faultHappenTime, commonConfig.PlatformConfig.TaskAfterTime)
  2048. newTimeWindow := commonEntity.TimeWindow{
  2049. FaultTime: faultHappenTime,
  2050. TimeWindowBegin: finalTimeWindowBegin,
  2051. TimeWindowEnd: latestTimeWindowEnd,
  2052. Length: util.CalculateDifferenceOfTimeCustom(finalTimeWindowBegin, latestTimeWindowEnd),
  2053. Labels: []string{faultLabel},
  2054. MasterTopics: masterTopics,
  2055. SlaveTopics: slaveTopics,
  2056. }
  2057. c_log.GlobalLogger.Infof("不在旧故障窗口内,向生产者队列添加一个新窗口,【Lable】=%v,【FaultTime】=%v,【Length】=%v", newTimeWindow.Labels, newTimeWindow.FaultTime, newTimeWindow.Length)
  2058. commonEntity.AddTimeWindowToTimeWindowProducerQueue(newTimeWindow)
  2059. } else { // 如果在旧故障窗口内
  2060. commonEntity.TimeWindowProducerQueueMutex.RLock()
  2061. defer commonEntity.TimeWindowProducerQueueMutex.RUnlock()
  2062. // 更新故障窗口end时间
  2063. expectEnd := util.TimeCustomChange(faultHappenTime, commonConfig.PlatformConfig.TaskAfterTime) // 窗口期望关闭时间是触发时间加上后置时间
  2064. expectLength := util.CalculateDifferenceOfTimeCustom(lastTimeWindow.TimeWindowBegin, expectEnd)
  2065. if expectLength < commonConfig.PlatformConfig.TaskMaxTime {
  2066. latestTimeWindowEnd = expectEnd
  2067. lastTimeWindow.TimeWindowEnd = latestTimeWindowEnd
  2068. lastTimeWindow.Length = util.CalculateDifferenceOfTimeCustom(lastTimeWindow.TimeWindowBegin, lastTimeWindow.TimeWindowEnd)
  2069. }
  2070. // 更新label
  2071. labels := lastTimeWindow.Labels
  2072. lastTimeWindow.Labels = util.AppendIfNotExists(labels, faultLabel)
  2073. // 更新 topic
  2074. sourceMasterTopics := lastTimeWindow.MasterTopics
  2075. lastTimeWindow.MasterTopics = util.MergeSlice(sourceMasterTopics, masterTopics)
  2076. sourceSlaveTopics := lastTimeWindow.SlaveTopics
  2077. lastTimeWindow.SlaveTopics = util.MergeSlice(sourceSlaveTopics, slaveTopics)
  2078. c_log.GlobalLogger.Infof("在旧故障窗口内,更新生产者队列最新的窗口,【Lable】=%v,【FaultTime】=%v,【Length】=%v", lastTimeWindow.Labels, lastTimeWindow.FaultTime, lastTimeWindow.Length)
  2079. }
  2080. }
  2081. func getTopicsOfNode(faultLabel string) (masterTopics []string, slaveTopics []string) {
  2082. // 获取所有需要采集的topic
  2083. var faultCodeTopics []string
  2084. for _, code := range commonConfig.CloudConfig.Triggers {
  2085. if code.Label == faultLabel {
  2086. faultCodeTopics = code.Topics
  2087. }
  2088. }
  2089. // 根据不同节点采集的topic进行分配采集
  2090. for _, acceptTopic := range faultCodeTopics {
  2091. for _, host := range commonConfig.CloudConfig.Hosts {
  2092. for _, topic := range host.Topics {
  2093. if host.Name == commonConfig.CloudConfig.Hosts[0].Name && acceptTopic == topic {
  2094. masterTopics = append(masterTopics, acceptTopic)
  2095. }
  2096. if host.Name == commonConfig.CloudConfig.Hosts[1].Name && acceptTopic == topic {
  2097. slaveTopics = append(slaveTopics, acceptTopic)
  2098. }
  2099. }
  2100. }
  2101. }
  2102. return masterTopics, slaveTopics
  2103. }
  2104. func isPointInPolygon(p Point, vertices []Point) bool {
  2105. intersections := 0
  2106. for i := 0; i < len(vertices); i++ {
  2107. j := (i + 1) % len(vertices)
  2108. if rayIntersectsSegment(p, vertices[i], vertices[j]) {
  2109. intersections++
  2110. }
  2111. }
  2112. return !(intersections%2 == 1)
  2113. }
  2114. func rayIntersectsSegment(p, p1, p2 Point) bool {
  2115. if p1.y > p2.y {
  2116. p1, p2 = p2, p1
  2117. }
  2118. if p.y == p1.y || p.y == p2.y {
  2119. p.y += 0.0001
  2120. }
  2121. if p.y < p1.y || p.y > p2.y {
  2122. return false
  2123. }
  2124. if p.x > max(p1.x, p2.x) {
  2125. return false
  2126. }
  2127. if p.x < min(p1.x, p2.x) {
  2128. return true
  2129. }
  2130. blueSlope := (p.y-p1.y)*(p2.x-p1.x)/(p2.y-p1.y) + p1.x
  2131. return p.x < blueSlope
  2132. }
  2133. func min(a, b float64) float64 {
  2134. if a < b {
  2135. return a
  2136. }
  2137. return b
  2138. }
  2139. func max(a, b float64) float64 {
  2140. if a > b {
  2141. return a
  2142. }
  2143. return b
  2144. }
  2145. // 判断Ego是否在交叉口,在的话返回true
  2146. func IfEnter(pointlist []Location, radius float64, lat, lon float64) bool {
  2147. // 判断是否进入点列表中的区域
  2148. point1 := Location{Latitude: lat, Longitude: lon}
  2149. for _, point := range pointlist {
  2150. d := distance(point1, point)
  2151. if d <= radius {
  2152. return true
  2153. }
  2154. }
  2155. return false
  2156. }
  2157. // 计算两点之间的距离(米)
  2158. func distance(point1, point2 Location) float64 {
  2159. // 经纬度转弧度
  2160. lat1 := point1.Latitude * math.Pi / 180
  2161. lon1 := point1.Longitude * math.Pi / 180
  2162. lat2 := point2.Latitude * math.Pi / 180
  2163. lon2 := point2.Longitude * math.Pi / 180
  2164. // 计算距离
  2165. dlon := lon2 - lon1
  2166. dlat := lat2 - lat1
  2167. a := math.Sin(dlat/2)*math.Sin(dlat/2) + math.Sin(dlon/2)*math.Sin(dlon/2)*math.Cos(lat1)*math.Cos(lat2)
  2168. c := 2 * math.Atan2(math.Sqrt(a), math.Sqrt(1-a))
  2169. d := 6371000 * c
  2170. return d
  2171. }