produce_window.go 80 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316
  1. package service
  2. import (
  3. commonConfig "cicv-data-closedloop/aarch64/jili/common/config"
  4. "cicv-data-closedloop/aarch64/jili/common/service"
  5. masterConfig "cicv-data-closedloop/aarch64/jili/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.(float64)
  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. for _, infoVec := range data.InfoVec {
  1899. c_log.GlobalLogger.Info("接收到故障码:", infoVec.ErrorCode)
  1900. }
  1901. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  1902. subscribersTimes[i] = time.Now()
  1903. goto TriggerSuccess
  1904. }
  1905. }
  1906. }
  1907. if len(masterConfig.RuleOfFaultInfo3) > 0 {
  1908. for _, f := range masterConfig.RuleOfFaultInfo3 {
  1909. faultLabel = f(shareVars, data)
  1910. if faultLabel != "" {
  1911. for _, infoVec := range data.InfoVec {
  1912. c_log.GlobalLogger.Info("接收到故障码:", infoVec.ErrorCode)
  1913. }
  1914. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  1915. subscribersTimes[i] = time.Now()
  1916. goto TriggerSuccess
  1917. }
  1918. }
  1919. }
  1920. TriggerSuccess:
  1921. subscribersMutexes[i].Unlock()
  1922. }
  1923. subscribersTimeMutexes[i].Unlock()
  1924. },
  1925. })
  1926. if err == nil {
  1927. create = true
  1928. }
  1929. }
  1930. // 39 有共享变量的订阅者必须被创建
  1931. if topic == masterConfig.TopicOfPjVehicleFdbPub {
  1932. subscribers[i], err = goroslib.NewSubscriber(goroslib.SubscriberConf{
  1933. Node: commonConfig.RosNode,
  1934. Topic: topic,
  1935. Callback: func(data *pjisuv_msgs.VehicleFdb) {
  1936. subscribersTimeMutexes[i].Lock()
  1937. if time.Since(subscribersTimes[i]).Seconds() > triggerInterval {
  1938. subscribersMutexes[i].Lock()
  1939. faultHappenTime := util.GetNowTimeCustom() // 获取当前故障发生时间
  1940. lastTimeWindow := commonEntity.GetLastTimeWindow() // 获取最后一个时间窗口
  1941. faultLabel := ""
  1942. if len(masterConfig.RuleOfPjVehicleFdbPub1) > 0 {
  1943. for _, f := range masterConfig.RuleOfPjVehicleFdbPub1 {
  1944. faultLabel = f(data)
  1945. if faultLabel != "" {
  1946. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  1947. subscribersTimes[i] = time.Now()
  1948. goto TriggerSuccess
  1949. }
  1950. }
  1951. }
  1952. if len(masterConfig.RuleOfPjVehicleFdbPub3) > 0 {
  1953. for _, f := range masterConfig.RuleOfPjVehicleFdbPub3 {
  1954. faultLabel = f(shareVars, data)
  1955. if faultLabel != "" {
  1956. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  1957. subscribersTimes[i] = time.Now()
  1958. goto TriggerSuccess
  1959. }
  1960. }
  1961. }
  1962. TriggerSuccess:
  1963. subscribersMutexes[i].Unlock()
  1964. }
  1965. subscribersTimeMutexes[i].Unlock()
  1966. // 更新共享变量
  1967. shareVars.Store("AutomodeOfPjVehicleFdbPub", data.Automode)
  1968. },
  1969. })
  1970. if err == nil {
  1971. create = true
  1972. }
  1973. }
  1974. // 40 有共享变量的订阅者必须被创建
  1975. if topic == masterConfig.TopicOfEndPointMessage {
  1976. subscribers[i], err = goroslib.NewSubscriber(goroslib.SubscriberConf{
  1977. Node: commonConfig.RosNode,
  1978. Topic: topic,
  1979. Callback: func(data *geometry_msgs.Point) {
  1980. subscribersTimeMutexes[i].Lock()
  1981. if time.Since(subscribersTimes[i]).Seconds() > triggerInterval {
  1982. subscribersMutexes[i].Lock()
  1983. faultHappenTime := util.GetNowTimeCustom() // 获取当前故障发生时间
  1984. lastTimeWindow := commonEntity.GetLastTimeWindow() // 获取最后一个时间窗口
  1985. faultLabel := ""
  1986. if len(masterConfig.RuleOfEndPointMessage1) > 0 {
  1987. for _, f := range masterConfig.RuleOfEndPointMessage1 {
  1988. faultLabel = f(data)
  1989. if faultLabel != "" {
  1990. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  1991. subscribersTimes[i] = time.Now()
  1992. goto TriggerSuccess
  1993. }
  1994. }
  1995. }
  1996. if len(masterConfig.RuleOfEndPointMessage3) > 0 {
  1997. for _, f := range masterConfig.RuleOfEndPointMessage3 {
  1998. faultLabel = f(shareVars, data)
  1999. if faultLabel != "" {
  2000. saveTimeWindow(faultLabel, faultHappenTime, lastTimeWindow)
  2001. subscribersTimes[i] = time.Now()
  2002. goto TriggerSuccess
  2003. }
  2004. }
  2005. }
  2006. TriggerSuccess:
  2007. subscribersMutexes[i].Unlock()
  2008. }
  2009. subscribersTimeMutexes[i].Unlock()
  2010. // 更新共享变量
  2011. shareVars.Store("EndPointX", data.X)
  2012. shareVars.Store("EndPointY", data.Y)
  2013. },
  2014. })
  2015. if err == nil {
  2016. create = true
  2017. }
  2018. }
  2019. if err != nil {
  2020. c_log.GlobalLogger.Infof("创建订阅者报错,可能由于节点未启动,再次尝试【%v】", err)
  2021. time.Sleep(time.Duration(2) * time.Second)
  2022. continue
  2023. } else {
  2024. if create {
  2025. c_log.GlobalLogger.Infof("创建订阅者订阅话题【%v】", topic)
  2026. }
  2027. break
  2028. }
  2029. }
  2030. }
  2031. c_log.GlobalLogger.Infof("全部订阅者创建完成。")
  2032. select {
  2033. case signal := <-service.ChannelKillWindowProducer:
  2034. if signal == 1 {
  2035. commonConfig.RosNode.Close()
  2036. service.AddKillTimes("3")
  2037. return
  2038. }
  2039. }
  2040. }
  2041. func saveTimeWindow(faultLabel string, faultHappenTime string, lastTimeWindow *commonEntity.TimeWindow) {
  2042. var taskBeforeTime, taskAfterTime, taskMaxTime int
  2043. {
  2044. //if commonConfig.PlatformConfig.TaskBeforeTime == 0 {
  2045. // taskBeforeTime = 5
  2046. //} else {
  2047. // taskBeforeTime = commonConfig.PlatformConfig.TaskBeforeTime
  2048. //}
  2049. //if commonConfig.PlatformConfig.TaskAfterTime == 0 {
  2050. // taskAfterTime = 4
  2051. //} else {
  2052. // taskAfterTime = commonConfig.PlatformConfig.TaskAfterTime
  2053. //}
  2054. //if commonConfig.PlatformConfig.TaskMaxTime == 0 {
  2055. // taskMaxTime = 60
  2056. //} else {
  2057. // taskMaxTime = commonConfig.PlatformConfig.TaskMaxTime
  2058. //}
  2059. // todo 这里基于任务设置taskBeforeTime和taskAfterTime的做法后面需要修改为根据触发器设置
  2060. if len(commonConfig.CurrTask.TaskTriggers) > 0 {
  2061. trigger := commonConfig.CurrTask.TaskTriggers[0]
  2062. taskBeforeTime = trigger.CollectionPreSeconds
  2063. taskAfterTime = trigger.CollectionAfterSeconds
  2064. } else {
  2065. taskBeforeTime = 5
  2066. taskAfterTime = 4
  2067. }
  2068. taskMaxTime = 60
  2069. }
  2070. saveTimeWindowMutex.Lock()
  2071. defer saveTimeWindowMutex.Unlock()
  2072. masterTopics, slaveTopics := getTopicsOfNode(faultLabel)
  2073. if lastTimeWindow == nil || util.TimeCustom1GreaterTimeCustom2(faultHappenTime, lastTimeWindow.TimeWindowEnd) { // 如果是不在旧故障窗口内,添加一个新窗口
  2074. exceptBegin := util.TimeCustomChange(faultHappenTime, -taskBeforeTime)
  2075. finalTimeWindowBegin := ""
  2076. if util.TimeCustom1LessEqualThanTimeCustom2(exceptBegin, latestTimeWindowEnd) { // 窗口最早时间不能早于上一个窗口结束时间
  2077. finalTimeWindowBegin = latestTimeWindowEnd
  2078. } else {
  2079. finalTimeWindowBegin = exceptBegin
  2080. }
  2081. latestTimeWindowEnd = util.TimeCustomChange(faultHappenTime, taskAfterTime)
  2082. newTimeWindow := commonEntity.TimeWindow{
  2083. FaultTime: faultHappenTime,
  2084. TimeWindowBegin: finalTimeWindowBegin,
  2085. TimeWindowEnd: latestTimeWindowEnd,
  2086. Length: util.CalculateDifferenceOfTimeCustom(finalTimeWindowBegin, latestTimeWindowEnd),
  2087. Labels: []string{faultLabel},
  2088. MasterTopics: masterTopics,
  2089. SlaveTopics: slaveTopics,
  2090. }
  2091. c_log.GlobalLogger.Infof("不在旧故障窗口内,向生产者队列添加一个新窗口,【Lable】=%v,【FaultTime】=%v,【Length】=%v", newTimeWindow.Labels, newTimeWindow.FaultTime, newTimeWindow.Length)
  2092. commonEntity.AddTimeWindowToTimeWindowProducerQueue(newTimeWindow)
  2093. } else { // 如果在旧故障窗口内
  2094. commonEntity.TimeWindowProducerQueueMutex.RLock()
  2095. defer commonEntity.TimeWindowProducerQueueMutex.RUnlock()
  2096. // 更新故障窗口end时间
  2097. expectEnd := util.TimeCustomChange(faultHappenTime, taskAfterTime) // 窗口期望关闭时间是触发时间加上后置时间
  2098. expectLength := util.CalculateDifferenceOfTimeCustom(lastTimeWindow.TimeWindowBegin, expectEnd)
  2099. if expectLength < taskMaxTime {
  2100. latestTimeWindowEnd = expectEnd
  2101. lastTimeWindow.TimeWindowEnd = latestTimeWindowEnd
  2102. lastTimeWindow.Length = util.CalculateDifferenceOfTimeCustom(lastTimeWindow.TimeWindowBegin, lastTimeWindow.TimeWindowEnd)
  2103. }
  2104. // 更新label
  2105. labels := lastTimeWindow.Labels
  2106. lastTimeWindow.Labels = util.AppendIfNotExists(labels, faultLabel)
  2107. // 更新 topic
  2108. sourceMasterTopics := lastTimeWindow.MasterTopics
  2109. lastTimeWindow.MasterTopics = util.MergeSlice(sourceMasterTopics, masterTopics)
  2110. sourceSlaveTopics := lastTimeWindow.SlaveTopics
  2111. lastTimeWindow.SlaveTopics = util.MergeSlice(sourceSlaveTopics, slaveTopics)
  2112. c_log.GlobalLogger.Infof("在旧故障窗口内,更新生产者队列最新的窗口,【Lable】=%v,【FaultTime】=%v,【Length】=%v", lastTimeWindow.Labels, lastTimeWindow.FaultTime, lastTimeWindow.Length)
  2113. }
  2114. }
  2115. func getTopicsOfNode(faultLabel string) (masterTopics []string, slaveTopics []string) {
  2116. // 获取所有需要采集的topic
  2117. var faultCodeTopics []string
  2118. for _, code := range commonConfig.CloudConfig.Triggers {
  2119. if code.Label == faultLabel {
  2120. faultCodeTopics = code.Topics
  2121. }
  2122. }
  2123. // 根据不同节点采集的topic进行分配采集
  2124. for _, acceptTopic := range faultCodeTopics {
  2125. for _, host := range commonConfig.CloudConfig.Hosts {
  2126. for _, topic := range host.Topics {
  2127. if host.Name == commonConfig.CloudConfig.Hosts[0].Name && acceptTopic == topic {
  2128. masterTopics = append(masterTopics, acceptTopic)
  2129. }
  2130. if host.Name == commonConfig.CloudConfig.Hosts[1].Name && acceptTopic == topic {
  2131. slaveTopics = append(slaveTopics, acceptTopic)
  2132. }
  2133. }
  2134. }
  2135. }
  2136. return masterTopics, slaveTopics
  2137. }
  2138. func isPointInPolygon(p Point, vertices []Point) bool {
  2139. intersections := 0
  2140. for i := 0; i < len(vertices); i++ {
  2141. j := (i + 1) % len(vertices)
  2142. if rayIntersectsSegment(p, vertices[i], vertices[j]) {
  2143. intersections++
  2144. }
  2145. }
  2146. return !(intersections%2 == 1)
  2147. }
  2148. func rayIntersectsSegment(p, p1, p2 Point) bool {
  2149. if p1.y > p2.y {
  2150. p1, p2 = p2, p1
  2151. }
  2152. if p.y == p1.y || p.y == p2.y {
  2153. p.y += 0.0001
  2154. }
  2155. if p.y < p1.y || p.y > p2.y {
  2156. return false
  2157. }
  2158. if p.x > max(p1.x, p2.x) {
  2159. return false
  2160. }
  2161. if p.x < min(p1.x, p2.x) {
  2162. return true
  2163. }
  2164. blueSlope := (p.y-p1.y)*(p2.x-p1.x)/(p2.y-p1.y) + p1.x
  2165. return p.x < blueSlope
  2166. }
  2167. func min(a, b float64) float64 {
  2168. if a < b {
  2169. return a
  2170. }
  2171. return b
  2172. }
  2173. func max(a, b float64) float64 {
  2174. if a > b {
  2175. return a
  2176. }
  2177. return b
  2178. }
  2179. // 判断Ego是否在交叉口,在的话返回true
  2180. func IfEnter(pointlist []Location, radius float64, lat, lon float64) bool {
  2181. // 判断是否进入点列表中的区域
  2182. point1 := Location{Latitude: lat, Longitude: lon}
  2183. for _, point := range pointlist {
  2184. d := distance(point1, point)
  2185. if d <= radius {
  2186. return true
  2187. }
  2188. }
  2189. return false
  2190. }
  2191. // 计算两点之间的距离(米)
  2192. func distance(point1, point2 Location) float64 {
  2193. // 经纬度转弧度
  2194. lat1 := point1.Latitude * math.Pi / 180
  2195. lon1 := point1.Longitude * math.Pi / 180
  2196. lat2 := point2.Latitude * math.Pi / 180
  2197. lon2 := point2.Longitude * math.Pi / 180
  2198. // 计算距离
  2199. dlon := lon2 - lon1
  2200. dlat := lat2 - lat1
  2201. a := math.Sin(dlat/2)*math.Sin(dlat/2) + math.Sin(dlon/2)*math.Sin(dlon/2)*math.Cos(lat1)*math.Cos(lat2)
  2202. c := 2 * math.Atan2(math.Sqrt(a), math.Sqrt(1-a))
  2203. d := 6371000 * c
  2204. return d
  2205. }