classdef.cpp 179 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510
  1. #include "stdafx.h"
  2. #include <math.h>
  3. #include <algorithm>
  4. #include "classdef.h"
  5. #include "constdef.h"
  6. #include "structdef.h"
  7. #include "locate_algorithm.h"
  8. #include "./system_basic_info/SystemAnalysis.h"
  9. #include "ProcessRemodule.h"
  10. #include "log_process_module.h"
  11. #include "Functions/Functions.h"
  12. #include "algorithm/Fit.h"
  13. #include "mylog/log_module.h"
  14. #pragma warning(disable: 4244)
  15. unsigned int g_nAlgoFailedCounts[ALGO_LOC_TOTAL] = {0};
  16. unsigned int g_nAlgoFailedCycleCounts[ALGO_LOC_TOTAL] = {0};
  17. DWORD g_ullCurTime = 0;
  18. Card::Card( string cardid, int cardtype, double z_offset)
  19. :m_isWarning(false),m_minDistanceToWarningpoint(DBL_MAX),m_warning_threshold(DBL_MAX),m_CardHasBeenInDrivingfaceAlarm(false),
  20. m_DrivingfaceAlarmFlag(-1),shift_id(0),m_indexForRcvOneReader(0)
  21. {
  22. m_warning_point_id = -1;
  23. BOOL bRet = InitializeCriticalSectionAndSpinCount(&m_csCard, MAXCRITICALSECTIONSPINCOUNT);
  24. is_drivingface_Initialization=false;
  25. card_type = cardtype;
  26. card_id = cardid;
  27. this->z_offset = z_offset;
  28. ::GetLocalTime(&deal_time);
  29. ::GetLocalTime(&enter_area_time);
  30. down_time = up_time = enter_reader_time = rec_time = time(NULL);
  31. time_over_time = time_area_over_time = time_area_forbidden = time_over_speed = time_low_power = time(NULL);
  32. last_locate_time = time(NULL);
  33. //坐标初始值为0
  34. //x = y = z = stored_x = stored_y = stored_z = 0;
  35. x = y = z = stored_x = stored_y = stored_z = INVALID_COORDINATE;
  36. output_x = output_y = 0;
  37. a = 0;
  38. t = 0;
  39. init_postion = false;
  40. is_first_location = true;
  41. is_algo_first_location = true;
  42. b_pos_change = false;
  43. is_driving_face_start = 0;
  44. right_x = right_y = right_z = left_x = left_y = left_z = 0;
  45. m_nMoveDirection = 0;
  46. map_id = map_id_old = area_id = reader_id = 0;
  47. map_scale = 1.0;
  48. state = 0;
  49. state_moving = 0;
  50. state_biz = 0;
  51. pos_state = pos_state_old = PDT_INIT;
  52. pos_state_count = 0;
  53. pos_state_confirm_times = 1;
  54. dept_id = group_id = occlevel_id = level_id = worktype_id = vehice_type_id = 0;
  55. power_state = power_state_last = 0;
  56. status_help = status_area_over_time = status_area_forbidden = status_area_over_speed = 0;
  57. status_over_speed = status_over_time = status_power = status_lost = status_call = 0;
  58. sync_num = 0;
  59. isdealed = isreceive = is_pos_changed = is_hist = is_need_cal = false;
  60. is_area_over_time = is_mine_over_time = false;
  61. id = 0;
  62. driver_id = "";
  63. shift_type_id = 1;
  64. reader_tickcount = time_stamp;
  65. time_stamp_last = -1;
  66. p_dists_locate = new _coordinate*[DIST_COUNT];
  67. time_stamp_max = 0;
  68. for(int i = 0; i < DIST_COUNT; i++){
  69. p_dists_locate[i] = NULL;
  70. }
  71. is_registered = true;
  72. is_deal_by_algo = false;
  73. pTofReaderPathMap = NULL;
  74. pTdoaReaderPathMap = NULL;
  75. last_s_locate_reader[0] = last_s_locate_reader[1] = -1;
  76. m_syncNumList.clear();
  77. m_nCalcSyncNum = 0;
  78. m_nSyncNumInList = 0;
  79. last_locate.tt = 0;
  80. last_locate.a = last_locate.antenna_id = last_locate.reader_id = last_locate.t = 0;
  81. last_locate.d = last_locate.d_offset = last_locate.v = 0.0;
  82. last_locate.x = last_locate.y = last_locate.z = INVALID_COORDINATE;
  83. last_x = last_y = last_z = INVALID_COORDINATE;
  84. this->driving_face_cur_shift_start_x = this->driving_face_cur_shift_start_y = this->driving_face_cur_shift_start_z= this->driving_face_cur_shift_x= this->driving_face_cur_shift_y=this->driving_face_cur_shift_z = 0;
  85. locate = nullptr;
  86. m_bUseFilter = false;
  87. m_nFilterType = NO_FILTER;
  88. m_pKalmanFilter = nullptr;
  89. m_nLastLocateT = 0;
  90. origin_locate.x = origin_locate.y = origin_locate.z = origin_locate.v = 0.0;
  91. last_vx = last_vy = 0.0;
  92. v = 0.0;
  93. ins_weight = INS_WEIGHT;
  94. uwb_weight = UWB_WEIGHT;
  95. acce_cur_state = 0;
  96. acce_last_state = 0;
  97. b_long_interval = false;
  98. accelerate_state = accelerate_state_last = ACCELERATE_INIT_STATE; //默认初始的为-10
  99. ins_direction = 0;
  100. direction = 0;
  101. antenna_angle = 0;
  102. m_nLightGroupId = -1; //默认为没控制灯组
  103. b_enter_intersection = false;
  104. is_red_light = false;
  105. light_id = 0;
  106. time_red_light = time(NULL);
  107. m_nOutputPosState = OUTPUT_POS::RIGHT_POS;
  108. m_nIsRailroad = 0;
  109. mileage = 0;
  110. diff_direction_counts = 0;
  111. m_nStream = INIT_STREAM;
  112. for(int i = 0; i < CARD_EVENT_COUNT; i++){
  113. m_event_list[i] = 0;
  114. }
  115. p_reader = nullptr;
  116. _dists.swap(DistQueMap());
  117. vt_his_speed.resize(0);
  118. his_pos.resize(0);
  119. is_ref_pos = false;
  120. is_fit_pos = false;
  121. cur_fit_nums = 0;
  122. have_fit_pos = false;
  123. have_long_fit_pos = false;
  124. count_idle = 0;
  125. count_change_direction = 0;
  126. is_over_interval = false;
  127. bInitalCellPath = false;
  128. curCellReaderName = "";
  129. curCellId = 0;
  130. originCellId = 0;
  131. nIncrease = 0;
  132. change_cell_dir = 0;
  133. nStartLocateCounts = 0;
  134. call_type = 0;
  135. vt_deal_call_reader_id.resize(0);
  136. recv_call_time = time(NULL);
  137. landmark_id = 0;
  138. landmark_dis = 0;
  139. landmark_direction = NODIRECTORY;
  140. isIdling = false;
  141. data_cell_time = 0;
  142. calc_cell_time = 0;
  143. last_cell = nullptr;
  144. ::GetLocalTime(&cellDealTime);
  145. drive_face_location_write_time = 0;
  146. idle_pos_list.resize(0);
  147. fit_b = fit_k = fit_r = cell_a = 0.0;
  148. last_k = 0;
  149. delta_ct = 0;
  150. time_last_tof = time(NULL);
  151. }
  152. Card::Card( void )
  153. :m_isWarning(false),m_minDistanceToWarningpoint(DBL_MAX),m_warning_threshold(DBL_MAX),m_CardHasBeenInDrivingfaceAlarm(false),
  154. m_DrivingfaceAlarmFlag(-1)
  155. {
  156. m_warning_point_id = -1;
  157. }
  158. /*
  159. * 重置算法相关参数的数据
  160. *
  161. * param
  162. * 无
  163. *
  164. * return
  165. * 无
  166. */
  167. void Card::reset()
  168. {
  169. //以下参数与算法相关
  170. //坐标初始值为0
  171. m_nLastLocateT = 0;
  172. time_stamp_last = -1;
  173. b_long_interval = false;
  174. is_first_location = true;
  175. last_locate.sync_num = 0;
  176. is_algo_first_location = true;
  177. while (_dists.size()>0)
  178. {
  179. _dists.pop_front();
  180. }
  181. if (m_syncNumList.size() > 0)
  182. {
  183. m_syncNumList.erase(m_syncNumList.begin(),m_syncNumList.end());
  184. m_syncNumList.resize(0);
  185. }
  186. if (vt_his_speed.size()>0)
  187. {
  188. vt_his_speed.erase(vt_his_speed.begin(),vt_his_speed.end());
  189. vt_his_speed.resize(0);
  190. }
  191. if (his_pos.size() > 0)
  192. {
  193. his_pos.erase(his_pos.begin(),his_pos.end());
  194. his_pos.resize(0);
  195. have_fit_pos = false;
  196. }
  197. if (long_his_pos.size() > 0)
  198. {
  199. long_his_pos.erase(long_his_pos.begin(),long_his_pos.end());
  200. long_his_pos.resize(0);
  201. have_long_fit_pos = false;
  202. count_idle = 0;
  203. }
  204. cur_fit_nums = 0;
  205. count_idle = 0;
  206. locate = nullptr;
  207. //卡尔曼参数重置
  208. if (m_pKalmanFilter!=nullptr)
  209. {
  210. m_pKalmanFilter->m_nCounts = 0;
  211. m_pKalmanFilter->Initial(0.2);
  212. }
  213. //清除历史格子列表
  214. if (his_cell.size() > 0)
  215. {
  216. his_cell.erase(his_cell.begin(),his_cell.end());
  217. }
  218. }
  219. void Card::set_reader(std::shared_ptr<Reader> preader) // 设置卡时间
  220. {
  221. this->rec_time = time(NULL);
  222. if(preader->reader_id != this->reader_id){ // 所在分站没有发生变化
  223. this->p_reader = preader.get();
  224. this->reader_id = preader->reader_id;
  225. this->enter_reader_time = this->rec_time;
  226. this->map_id_old = this->map_id;
  227. this->map_id = preader->map_id;
  228. this->map_scale = preader->map_scale;
  229. }
  230. if(this->pos_state == this->p_reader->pos_state){
  231. if(this->pos_state_count <= pos_state_confirm_times)
  232. {
  233. this->pos_state_count++;
  234. }
  235. }
  236. else // 井上井下位置变化
  237. {
  238. this->pos_state = p_reader->pos_state;
  239. this->pos_state_count = 1;
  240. }
  241. }
  242. /*
  243. * 设置地图集覆盖范围,适用TOF
  244. *
  245. */
  246. void Card::set_reader_path_tof(std::shared_ptr<ReaderPathMap> rpm)
  247. {
  248. this->pTofReaderPathMap = rpm;
  249. }
  250. bool Card::checkStream(double x1,double y1,double x2,double y2,int nStream)
  251. {
  252. //方向一致,返回true,否则返回false
  253. //去除采集刚启动,last_locate的坐标为零而导致判断车辆上下行方向错误的问题
  254. //车辆上下行确定
  255. //3个条件:起点(x1,y1),终点(x2,y2)
  256. //1.x1==x2的情况下,y2>y1为下行
  257. //2.y1==y2的情况下,x1>x2为下行
  258. //3.x2>x1且y2>y1为下行
  259. //其他情况为上行
  260. int stream = 0;
  261. if (((x1 == x2 || abs(x1 - x2) < 1E-2) && y1 < y2 )
  262. ||((y1 == y2 || abs(y1 - y2) < 1E-2) && x1 > x2)
  263. ||(x1 < x2 && y1 < y2))
  264. {
  265. stream = DOWN_STREAM;
  266. }
  267. else
  268. {
  269. stream = UP_STREAM;
  270. }
  271. return nStream == stream;
  272. }
  273. /*
  274. * 设置地图集覆盖范围,适用TDOA
  275. *
  276. */
  277. void Card::set_reader_path_tdoa(std::shared_ptr<TDOAReaderPathMap> trpm)
  278. {
  279. this->pTdoaReaderPathMap = trpm;
  280. }
  281. bool operator==(sync_data& a,sync_data&b){
  282. return a.sync_num == b.sync_num;
  283. }
  284. /*
  285. * 此函数主要判断此坐标是否需要形成json输出给webserver
  286. *
  287. * param
  288. * acce_state 算法中保存的当前加速度计状态
  289. * acce_state_last 算法中保存的上一次加速度计状态
  290. *
  291. * return
  292. * 无
  293. */
  294. void Card::inspect_coordinate(int acce_state)
  295. {
  296. this->isoutput = false;
  297. char log[200] = {0};
  298. //如果是静止或者怠速状态,每次的定位坐标不变化,此时要求采集不再将此数据送往webserver
  299. if (STATE_ACCE_STATIC == acce_state || STATE_ACCE_IDLING == acce_state)
  300. {
  301. this->isoutput = false;
  302. }
  303. else
  304. {
  305. if (this->x == this->output_x && this->y == this->output_y) //last_x,last_y
  306. {
  307. count_idle++;
  308. this->isoutput = false;
  309. }else{
  310. //如果运动状态时,
  311. int nSign = 0;
  312. int nState = 0;
  313. if (CheckStreamUniformity(this->output_x,this->output_y,this->x,this->y,this->m_nStream))
  314. {
  315. double distance = sqrt(pow(this->x - this->output_x,2) + pow(this->y - this->output_y,2)) * this->map_scale;
  316. if(distance > this->map_scale) //this->map_scale)
  317. {
  318. //两点之间的距离是否大于一个0.5米,满足条件,输出json
  319. this->isoutput = true;
  320. this->output_x = this->x;
  321. this->output_y = this->y;
  322. count_idle = 0;
  323. }else{
  324. count_idle++;
  325. }
  326. }
  327. else
  328. {
  329. if( count_change_direction >= MAX_REBOUND_COUNTS){
  330. // 认为掉头,输出给web
  331. this->isoutput = true;
  332. this->output_x = this->x;
  333. this->output_y = this->y;
  334. count_idle = 0;
  335. count_change_direction = 0;
  336. }else{
  337. // 认为是抖动,不输出给web
  338. count_idle++;
  339. }
  340. }
  341. }
  342. }
  343. }
  344. /*
  345. * 检查当前定位坐标和nStream方向是否一致
  346. *
  347. * param
  348. * x1,y1 起点的坐标
  349. * x2,y2 终点的坐标
  350. * nStream 上一次的方向
  351. *
  352. * return
  353. * 如果算出的方向和nStream一致,则返回true;否则返回false
  354. *
  355. */
  356. bool Card::CheckStreamUniformity(double x1,double y1,double x2,double y2,int nStream)
  357. {
  358. return nStream == GetStream(x1,y1,x2,y2);
  359. }
  360. /*
  361. * TOF定位算法
  362. *
  363. * param
  364. * cnt 数据条数
  365. *
  366. * return
  367. * 无
  368. *
  369. */
  370. void Card::algo_tof(int cnt)
  371. {
  372. }
  373. /*
  374. * TOF一维定位算法
  375. *
  376. * param
  377. * cnt 数据条数
  378. *
  379. * return
  380. * 成功返回0,否则返回非零值
  381. *
  382. */
  383. int Card::algo_tof_1d(int cnt)
  384. {
  385. int i = 0, nCount = 0;
  386. bool bFound = false;
  387. //组装数据部分
  388. map<unsigned long long, std::shared_ptr<_coordinate>> tmp_dists_locate;
  389. if (cnt > 1)
  390. {
  391. for(DistMap::iterator it = _dists.front().distmap.begin(); it != _dists.front().distmap.end(); ++it){
  392. bFound = false;
  393. // 判断是否已经加入到列表
  394. for(int j = 0; j < i; j++){
  395. if(tmp_dists_locate.find(it->second->reader_id) != tmp_dists_locate.end()){
  396. //已存在,则退出查找for循环
  397. bFound = true;
  398. break;
  399. }
  400. }
  401. if(i < DIST_COUNT && !bFound){
  402. tmp_dists_locate.insert(make_pair(i,(it->second)));
  403. i++;
  404. }
  405. }
  406. }else{
  407. /*for(DistMap::iterator it = _dists.front().distmap.begin(); it != _dists.front().distmap.end(); ++it,i++){
  408. tmp_dists_locate.insert(make_pair(i,(it->second)));
  409. }*/
  410. return 1;
  411. }
  412. nCount = tmp_dists_locate.size();
  413. if (nCount <= 0)
  414. {
  415. return 1;
  416. }
  417. map<unsigned long long, std::shared_ptr<_coordinate>>::iterator it = tmp_dists_locate.begin();
  418. time_stamp_cal = it->second->t;
  419. double dMaxDistance = 1000.0;
  420. int key = 0;
  421. //修正由于高度所引入的误差
  422. for (map<unsigned long long, std::shared_ptr<_coordinate>>::iterator it_tmp = tmp_dists_locate.begin();it_tmp != tmp_dists_locate.end();++it_tmp)
  423. {
  424. double distance = 0.0;
  425. distance = sqrt(pow(it_tmp->second->d,2) - pow(TOF_HEIGHT_ERROR,2));
  426. it_tmp->second->d = distance;
  427. if (distance < dMaxDistance)
  428. {
  429. key = it_tmp->first;
  430. dMaxDistance = distance;
  431. }
  432. }
  433. TOF_REFER_DATA refer_data;
  434. it = tmp_dists_locate.find(key);
  435. if (it != tmp_dists_locate.end())
  436. {
  437. refer_data.nCardTimeStamp = it->second->t;
  438. refer_data.llDeltaTime = 0;
  439. refer_data.nReaderId = it->second->reader_id;
  440. refer_data.nAntennaIndex = it->second->antenna_id;
  441. refer_data.dDistance = it->second->d;
  442. refer_data.x = it->second->x;
  443. refer_data.y = it->second->y;
  444. refer_data.z = it->second->z;
  445. refer_data.a = it->second->a;
  446. }
  447. std::shared_ptr<POS> p = nullptr;
  448. std::shared_ptr<POS> tmpPos = std::make_shared<POS>();
  449. std::advance(it,1);
  450. i = 1;
  451. bool isExist = false;
  452. int nReaderId = 0; //分站id
  453. int nAntennaIndex = 0; //天线索引
  454. double dist = 0.0; //距离
  455. std::vector<std::shared_ptr<POS>> udm_pos;
  456. udm_pos.resize(0);
  457. for(it = tmp_dists_locate.begin();it != tmp_dists_locate.end();++it,i++){
  458. nReaderId = it->second->reader_id;
  459. nAntennaIndex = it->second->antenna_id;
  460. dist = it->second->d;
  461. //如果为参考分站,则不计算,直接寻找下一个数据
  462. if (nReaderId == refer_data.nReaderId && nAntennaIndex == refer_data.nAntennaIndex && abs(dist - refer_data.dDistance) < ZERO_PRECISION)
  463. {
  464. continue;
  465. }
  466. p = LocateAlgorithm::Pos(pTofReaderPathMap,nReaderId,nAntennaIndex,dist,refer_data);
  467. if(p == nullptr)
  468. {
  469. continue;
  470. }
  471. isExist = true;
  472. udm_pos.push_back(p);
  473. }
  474. if (!isExist )
  475. {
  476. if (p == nullptr)
  477. {
  478. p = std::make_shared<POS>();
  479. }
  480. p->posx = refer_data.x;
  481. p->posy = refer_data.y;
  482. p->posz = refer_data.z;
  483. p->cx = p->posx/map_scale;
  484. p->cy = p->posy/map_scale;
  485. p->cz = p->posz/map_scale;
  486. }else{
  487. //对多解求平均
  488. double posx = 0;
  489. double posy = 0;
  490. double posz = 0;
  491. //选取误差最小的解
  492. double range = 999999.0;
  493. for (std::vector<std::shared_ptr<POS>>::iterator it_pos = udm_pos.begin();it_pos != udm_pos.end();++it_pos)
  494. {
  495. if((*it_pos)->pos_radius < range){
  496. range = (*it_pos)->pos_radius;
  497. posx = (*it_pos)->posx;
  498. posy = (*it_pos)->posy;
  499. posz = (*it_pos)->posz;
  500. }
  501. }
  502. p->posx = posx;
  503. p->posy = posy;
  504. p->posz = posz;
  505. p->cx = p->posx/map_scale;
  506. p->cy = p->posy/map_scale;
  507. p->cz = p->posz/map_scale;
  508. }
  509. if (CheckPositionValid(p))
  510. {
  511. p->delta_time = refer_data.nCardTimeStamp - this->t;
  512. }
  513. //求完解,对解做优化处理
  514. if (!CheckSolutionTof(p))
  515. {
  516. //通过加速度被抛弃,利用参考数据来做圆
  517. return 1;
  518. }
  519. //最后保存
  520. if (CheckPositionValid(p))
  521. {
  522. SaveCardAlgoData(p);
  523. }
  524. return 0;
  525. }
  526. /*
  527. * TDOA算法
  528. *
  529. * param
  530. * cnt 数据条数
  531. *
  532. * return
  533. * 无
  534. *
  535. */
  536. void Card::algo_tdoa(int cnt)
  537. {
  538. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_140);
  539. GetLocalTime(&m_afmData.st);
  540. if(cnt < 2){
  541. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_141);
  542. m_afmData.bStatus = true;
  543. m_afmData.strCardId = this->card_id;
  544. m_afmData.nType = ALGO_FAILED_CONDITION_15;
  545. ALGORITHM_FAILED(ALGO_FAILED_CONDITION_15);
  546. return;
  547. }
  548. //主要处理当相同卡的时间戳的数据中存在同步序号大于5的情况,如果有大于5的数据则丢弃此数据
  549. int k = 0;
  550. unsigned short dst = 0;
  551. unsigned short st = 0;
  552. bool bRet = false;
  553. mp_dists_locate.clear();
  554. // 多个基站的时间同步序号相差大于阈值,不参与计算
  555. for(DistMap::iterator it = _dists.front().distmap.begin(); it != _dists.front().distmap.end(); ++it,k++){
  556. if(k==0){
  557. st = it->second->sync_num;
  558. }else{
  559. dst = (st - (unsigned short)it->second->sync_num);
  560. if ((st > (unsigned short)it->second->sync_num && dst > MAX_SYNCTIME_DIFF_NUM) ||
  561. (st < (unsigned short)it->second->sync_num && dst < MAX_SYNCTIME_DIFF_NUM_CROSS_PERIOD))
  562. {
  563. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_142);
  564. bRet = true;
  565. //此处需要输出计数
  566. break;
  567. }
  568. }
  569. mp_dists_locate.insert(make_pair(it->second->tt, it->second));
  570. }
  571. if(bRet){
  572. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_143);
  573. this->x = this->last_locate.x;
  574. this->y = this->last_locate.y;
  575. this->z = 0;
  576. m_afmData.bStatus = true;
  577. m_afmData.strCardId = this->card_id;
  578. m_afmData.nType = ALGO_FAILED_CONDITION_2;
  579. ALGORITHM_FAILED(ALGO_FAILED_CONDITION_2);
  580. return;
  581. }
  582. std::shared_ptr<ReceiveDataMap> pRdm = std::make_shared<ReceiveDataMap>();
  583. pRdm->clear();
  584. bRet = false;
  585. int maxSyncTimes = 0;
  586. //保存加速度当前状态和上次状态
  587. int acce_state = 0;
  588. int acce_state_last = 0;
  589. int ins_direction = 0;
  590. int card_time_stamp = 0;
  591. map<unsigned long long,std::shared_ptr<_coordinate>>::iterator it_mpdl = mp_dists_locate.begin();
  592. int i = 0;
  593. for(;it_mpdl!=mp_dists_locate.end();++it_mpdl){
  594. if(i==0){
  595. card_time_stamp = it_mpdl->second->t;
  596. maxSyncTimes = it_mpdl->second->sync_num;
  597. acce_state = it_mpdl->second->acce_state;
  598. acce_state_last = it_mpdl->second->acce_state_last;
  599. ins_direction = it_mpdl->second->ins_direction;
  600. }
  601. else{
  602. if(maxSyncTimes < it_mpdl->second->sync_num){
  603. maxSyncTimes = it_mpdl->second->sync_num;
  604. acce_state = it_mpdl->second->acce_state;
  605. acce_state_last = it_mpdl->second->acce_state_last;
  606. ins_direction = it_mpdl->second->ins_direction;
  607. }
  608. }
  609. ReceiveDataMap::iterator prdm_it = pRdm->find(it_mpdl->second->tt);
  610. if(prdm_it == pRdm->end()){
  611. if(it_mpdl->second->tt == LLONG_MAX ){
  612. //如果同步时间戳存在异常值,则不走算法定位,直接返回上一次结果值
  613. bRet = true;
  614. //此处需要输出计数
  615. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_144);
  616. break; // 存在一个异常值就退出算法
  617. // continue; // 将异常值过滤掉,不参与算法,其他正常值照常参与计算
  618. }
  619. //保存信息用于定位
  620. std::shared_ptr<ReceiveData> prd = std::make_shared<ReceiveData>();
  621. prd->reader_id = it_mpdl->second->reader_id;
  622. prd->antenna_id = it_mpdl->second->antenna_id;
  623. prd->rec_time_stamp = it_mpdl->second->tt;
  624. prd->x = it_mpdl->second->x*this->map_scale;
  625. prd->y = it_mpdl->second->y*this->map_scale;
  626. prd->z = it_mpdl->second->z*this->map_scale;
  627. prd->special = it_mpdl->second->special;
  628. if (prd->rec_time_stamp > 0)
  629. {
  630. pRdm->insert(make_pair(prd->rec_time_stamp,prd));
  631. }
  632. }
  633. i++;
  634. }
  635. //存在异常值,直接返回上一次结果值
  636. if(bRet){
  637. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_145);
  638. pRdm->clear();
  639. this->x = this->last_locate.x;
  640. this->y = this->last_locate.y;
  641. this->z = 0;
  642. m_afmData.bStatus = true;
  643. m_afmData.strCardId = this->card_id;
  644. m_afmData.nCardStamp = it_mpdl->first;
  645. m_afmData.nType = ALGO_FAILED_CONDITION_3;
  646. ALGORITHM_FAILED(ALGO_FAILED_CONDITION_3);
  647. return;
  648. }
  649. //time_stamp_last特指卡的计数序号,非分站的
  650. //当卡需要新定位的计数序列号小于上一次成功定位的序列号,则此次不定位,避免定位结果的回退
  651. // 本次序号小于上次序号,存在如下情况
  652. // 1、跨周期,如本次0,上次为65535,另外进入盲区也可能跨周期后回来
  653. // 2、卡重置,从0开始
  654. // 3、序号较连续,新数据先到,旧数据后到,会造成跳动
  655. if (this->time_stamp_last != -1 && this->time_stamp_last > this->time_stamp_cal)
  656. {
  657. if((unsigned short)this->time_stamp_last - (unsigned short)this->time_stamp_cal < MAX_SYNCTIME_DELAY_NUM)
  658. {
  659. // 小于5,认为会影响数据
  660. pRdm->clear();
  661. this->x = this->last_locate.x;
  662. this->y = this->last_locate.y;
  663. this->z = 0;
  664. m_afmData.bStatus = true;
  665. m_afmData.strCardId = this->card_id;
  666. m_afmData.nCardStamp = this->time_stamp_cal;
  667. m_afmData.nType = ALGO_FAILED_CONDITION_4;
  668. ALGORITHM_FAILED(ALGO_FAILED_CONDITION_4);
  669. //此处需要输出计数
  670. return;
  671. }
  672. }
  673. this->time_stamp_last = this->time_stamp_cal;
  674. int nCount = 0;
  675. std::unique_ptr<POS> p = nullptr;
  676. std::unique_ptr<POS> p2 = nullptr;
  677. bool bOutput = false;
  678. this->z = 0;
  679. i = 0;
  680. if(pRdm->size() > 1){
  681. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_147);
  682. this->m_nCalcSyncNum = maxSyncTimes;
  683. //p = LocateAlgorithm::Pos(pRdm, pTdoaReaderPathMap);
  684. //算法逻辑修改:
  685. //1.当开始两分站之间有地图集时,走原来的算法逻辑
  686. //2.当开始两分站之间无地图集时,走两两遍历的算法逻辑
  687. ReceiveDataMap::iterator first = pRdm->begin();
  688. ReceiveDataMap::iterator second = first;
  689. //偏移到第二个元素
  690. std::advance(second,1);
  691. bool bFind = false;
  692. bool bExitPath = false;
  693. //如果两级都能找到才运行继续后续操作,否则,表明没有此路径地图集
  694. TDOAReaderPathMap::iterator rdm_it = pTdoaReaderPathMap->find(first->second->reader_id);
  695. if(rdm_it != pTdoaReaderPathMap->end()){
  696. //表示地图集中存在第一个分站的路径集
  697. bFind = true;
  698. }
  699. if (bFind)
  700. {
  701. //确认第一个和第二个分站之间是否有地图集
  702. ReaderPathMap::iterator rpm_it = pTdoaReaderPathMap->find(first->second->reader_id)->second->find(second->second->reader_id);
  703. if(rpm_it == pTdoaReaderPathMap->find(first->second->reader_id)->second->end()){
  704. bExitPath = false;
  705. }else{
  706. bExitPath = true;
  707. }
  708. }
  709. if (bExitPath)
  710. {
  711. //以第一个基站作为基准分站进行坐标计算
  712. p = LocateAlgorithm::Pos(pRdm, pTdoaReaderPathMap);
  713. }else{
  714. //pRdm内的数据双重遍历进行坐标计算
  715. p = LocateAlgorithm::LocatePos(pRdm, pTdoaReaderPathMap);
  716. if(fabs(p->posx) < 1E-4 && fabs(p->posy) < 1E-4 ){
  717. p->posx = INVALID_COORDINATE;
  718. p->posy = INVALID_COORDINATE;
  719. p->posz = INVALID_COORDINATE;
  720. }
  721. }
  722. this->origin_locate.x = p->posx / (this->map_scale*1.0);
  723. this->origin_locate.y = p->posy / (this->map_scale*1.0);
  724. this->origin_locate.z = p->posz / (this->map_scale*1.0);
  725. double interval_time = 0.2; // 当ReadFileTest时,没有set_reader,给默认值
  726. if(this->p_reader){
  727. interval_time = this->p_reader->reader_interval_time;
  728. }
  729. double deltaT = 0.0;
  730. double cvx = 0;
  731. double cvy = 0;
  732. double cv = 0;
  733. double cx = 0;
  734. double cy = 0;
  735. double cz = 0;
  736. int nSign = 1;
  737. sync_data sdNew;
  738. sync_data sd;
  739. cx = p->posx / (this->map_scale*1.0);
  740. cy = p->posy / (this->map_scale*1.0);
  741. cz = p->posz / (this->map_scale*1.0);
  742. #ifdef ALGORITHM_TYPE_INS
  743. int acce_direction = 0; //加速度计速度方向 0-静止,1-前进,-1-后退
  744. int uwb_direction = 0; //uwb速度方向,同上
  745. bool bUseKalman = false;
  746. bool bOriginLocate = false; //原始定位是否成功
  747. bool bDirectReturn = false; //是否直接返回本次定位结果
  748. #endif
  749. if(p->posx != INVALID_COORDINATE && p->posy != INVALID_COORDINATE){
  750. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_148);
  751. #ifdef ALGORITHM_TYPE_INS
  752. bOriginLocate = true; //原始定位成功
  753. #endif
  754. //定位成功
  755. if(is_algo_first_location){
  756. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_149);
  757. if (m_syncNumList.size() > 0)
  758. {
  759. m_syncNumList.erase(m_syncNumList.begin(),m_syncNumList.end());
  760. m_syncNumList.resize(0);
  761. }
  762. sdNew.sync_num = maxSyncTimes;
  763. sdNew.x = cx;
  764. sdNew.y = cy;
  765. sdNew.vx = 0;
  766. sdNew.vy = 0;
  767. sdNew.update = false;
  768. m_syncNumList.push_back(sdNew);
  769. #ifdef ALGORITHM_TYPE_INS
  770. uwb_direction = 1;
  771. #endif
  772. }
  773. else{
  774. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_150);
  775. //现在的关于同步序号的处理是这样的:
  776. //如果定位成功,就把这次定位成功的同步数据:同步序号,坐标;x,y方向的速度,扔到一个队列里,
  777. //后来定位成功的就会先根据同步序号差用加速度抛一次;
  778. //抛不掉,就用队列里的同步数据(从后往前找),找到第一个与当前同步序号相差大于5的同步数据来进行第二次计算速度以及加速度,
  779. //如果加速度大于5,就不要此次的定位数据,
  780. //如果通过加速度判断就将队列中从头开始到此同步数据的所有元素都丢弃,并插入新的此次同步数据
  781. if (this->b_long_interval)
  782. {
  783. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_151);
  784. //此段代码用于将上一次定位是根据两个时间差是个很大值而定位出的结果
  785. //当后续定位时就和最近的定位结果进行比较
  786. //例如:当上一次同步序号是14321,它定位时比较的同步序号是14200,时间差大于20多秒
  787. //这时我们就将b_long_interval置为true
  788. //当本次定位,同步序号是14326,,这时就需要根据最近的14321进行判断
  789. list<sync_data>::reverse_iterator it = m_syncNumList.rbegin();
  790. sync_data sdl = *it;
  791. //以下计算deltaT还需要考虑卡的同步序号轮回的情况。
  792. if (maxSyncTimes > it->sync_num)
  793. {
  794. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_152);
  795. deltaT = (maxSyncTimes - sdl.sync_num)*interval_time;
  796. }
  797. else
  798. {
  799. deltaT = (maxSyncTimes + 65536 - sdl.sync_num)*interval_time;
  800. }
  801. if (deltaT < 10 && deltaT > 0)
  802. {
  803. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_155);
  804. this->b_long_interval = false;
  805. }
  806. //避免同一个同步序号下存在多个不同卡序号
  807. if (deltaT < 1E-2)
  808. {
  809. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_156);
  810. deltaT = 0.2;
  811. }
  812. cvx = (cx - sdl.x)*this->map_scale/deltaT;
  813. cvy = (cy - sdl.y)*this->map_scale/deltaT;
  814. double avx = (cvx - sdl.vx) / deltaT;
  815. double avy = (cvy - sdl.vy) / deltaT;
  816. double av = sqrt(pow(avx,2) + pow(avy,2));
  817. //车卡的加速度
  818. switch(this->card_type){
  819. case CT_PERSON:
  820. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_157);
  821. if(av > PERSON_ACCELERATE_THRESHOLD){
  822. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_158);
  823. this->x = this->last_locate.x;
  824. this->y = this->last_locate.y;
  825. this->b_long_interval = false;
  826. pRdm->clear();
  827. m_afmData.bStatus = true;
  828. m_afmData.strCardId = this->card_id;
  829. m_afmData.nType = ALGO_FAILED_CONDITION_5;
  830. ALGORITHM_FAILED(ALGO_FAILED_CONDITION_5);
  831. return;
  832. }
  833. break;
  834. case CT_VEHICLE:
  835. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_159);
  836. if(av > VECHILE_ACCELERATE_THRESHOLD){
  837. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_160);
  838. //保留上次结果
  839. this->x = this->last_locate.x;
  840. this->y = this->last_locate.y;
  841. this->b_long_interval = false;
  842. pRdm->clear();
  843. m_afmData.bStatus = true;
  844. m_afmData.strCardId = this->card_id;
  845. m_afmData.nType = ALGO_FAILED_CONDITION_6;
  846. ALGORITHM_FAILED(ALGO_FAILED_CONDITION_6);
  847. return;
  848. }
  849. break;
  850. }
  851. deltaT = 0;
  852. cvx = cvy = 0;
  853. }
  854. //从队列尾部开始查找,找到第一个同步序号与当前计算卡的同步序号相差5个以上的数据
  855. list<sync_data>::reverse_iterator it;
  856. bool bOverflow = false;
  857. for(it = m_syncNumList.rbegin();it!=m_syncNumList.rend();it++){
  858. if(maxSyncTimes - it->sync_num >= 5){
  859. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_161);
  860. sd = *it;
  861. break;
  862. }
  863. else{
  864. if(maxSyncTimes - it->sync_num < 0 && maxSyncTimes < 100){
  865. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_162);
  866. //如果最新同步号小于列表中的同步号则
  867. if(maxSyncTimes + 65536 - it->sync_num >=5 ){//5
  868. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_163);
  869. bOverflow = true;
  870. sd = *it;
  871. break;
  872. }
  873. }else{
  874. continue;
  875. }
  876. }
  877. }
  878. //根据溢出条件来计算deltaT
  879. if(bOverflow){
  880. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_164);
  881. deltaT = (maxSyncTimes + 65536 - sd.sync_num)*interval_time;
  882. }else{
  883. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_165);
  884. deltaT = (maxSyncTimes - sd.sync_num)*interval_time;
  885. }
  886. //速度正负的判断:以x轴,y轴正向运动为正
  887. //如果x相等,则y2 - y1 > 0为正
  888. //其他情况,则x2 - x1 > 0 为正
  889. if(cx == sd.x){
  890. if(cy > sd.y){
  891. nSign = 1;
  892. }else{
  893. nSign = -1;
  894. }
  895. }else{
  896. if(cx > sd.x){
  897. nSign = 1;
  898. }else{
  899. nSign = -1;
  900. }
  901. }
  902. #ifdef ALGORITHM_TYPE_INS
  903. uwb_direction = (nSign == 1)?1:-1; //uwb形式判断不出静止或者怠速
  904. #endif
  905. //使用间隔来修正速度
  906. if(deltaT - 1.0 >= 0){
  907. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_166);
  908. if (deltaT > 10)
  909. {
  910. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_167);
  911. this->b_long_interval = true;
  912. }
  913. //转为m/s
  914. cvx = (cx - sd.x)*this->map_scale/deltaT;
  915. cvy = (cy - sd.y)*this->map_scale/deltaT;
  916. cv = sqrt(pow(cvx,2) + pow(cvy,2));
  917. cv = cv*nSign;
  918. double avx = (cvx - sd.vx) / deltaT;
  919. double avy = (cvy - sd.vy) / deltaT;
  920. double av = sqrt(pow(avx,2) + pow(avy,2));
  921. //车卡的加速度
  922. switch(this->card_type){
  923. case CT_PERSON:
  924. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_168);
  925. if(av > PERSON_ACCELERATE_THRESHOLD){
  926. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_169);
  927. this->x = this->last_locate.x;
  928. this->y = this->last_locate.y;
  929. pRdm->clear();
  930. m_afmData.bStatus = true;
  931. m_afmData.strCardId = this->card_id;
  932. m_afmData.nType = ALGO_FAILED_CONDITION_5;
  933. ALGORITHM_FAILED(ALGO_FAILED_CONDITION_5);
  934. return;
  935. }
  936. break;
  937. case CT_VEHICLE:
  938. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_170);
  939. if(av > VECHILE_ACCELERATE_THRESHOLD){
  940. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_171);
  941. //保留上次结果
  942. this->x = this->last_locate.x;
  943. this->y = this->last_locate.y;
  944. pRdm->clear();
  945. m_afmData.bStatus = true;
  946. m_afmData.strCardId = this->card_id;
  947. m_afmData.nType = ALGO_FAILED_CONDITION_6;
  948. ALGORITHM_FAILED(ALGO_FAILED_CONDITION_6);
  949. return;
  950. }
  951. break;
  952. }
  953. this->last_locate.acceleration = av;
  954. this->last_vx = cvx;
  955. this->last_vy = cvy;
  956. cv = cv*3.6;
  957. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_172);
  958. //速度的限制
  959. if(fabs(cv) > MAX_VECHILE_SPEED){
  960. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_173);
  961. this->x = this->last_locate.x;
  962. this->y = this->last_locate.y;
  963. pRdm->clear();
  964. m_afmData.bStatus = true;
  965. m_afmData.strCardId = this->card_id;
  966. m_afmData.nType = ALGO_FAILED_CONDITION_7;
  967. ALGORITHM_FAILED(ALGO_FAILED_CONDITION_7);
  968. return;
  969. }
  970. //删除第一个元素到tmp(含)之间的所有元素
  971. bool bStartDel = false;
  972. for(list<sync_data>::reverse_iterator tmp = m_syncNumList.rbegin();tmp != m_syncNumList.rend();)
  973. {
  974. if(bStartDel){
  975. tmp = list<sync_data>::reverse_iterator(m_syncNumList.erase((++tmp).base()));
  976. }else{
  977. if(*tmp == sd){
  978. bStartDel = true;
  979. }
  980. ++tmp;
  981. }
  982. }
  983. //更新值为当前值并插入队列
  984. sdNew.sync_num = maxSyncTimes;
  985. this->m_nSyncNumInList = sd.sync_num;
  986. sdNew.vx = cvx;
  987. sdNew.vy = cvy;
  988. sdNew.update = true;
  989. }else{
  990. cv = this->origin_locate.v;
  991. }
  992. }
  993. }
  994. #ifdef ALGORITHM_TYPE_INS
  995. else{
  996. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_174);
  997. uwb_direction = ins_direction;
  998. }
  999. #endif
  1000. #ifdef ALGORITHM_TYPE_INS
  1001. bRet = false;
  1002. //0,1,2分别表示静止(含怠速),运动,运动中刹车
  1003. switch (acce_state)
  1004. {
  1005. case 0:
  1006. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_175);
  1007. acce_direction = 0;
  1008. if(acce_state_last!=0){
  1009. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_176);
  1010. //说明从其他状态变为静止(怠速)状态
  1011. //返回这次定位结果
  1012. if (bOriginLocate)
  1013. {
  1014. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_177);
  1015. bDirectReturn = true; //直接返回本次定位结果
  1016. }
  1017. }else{
  1018. //返回上一次定位结果
  1019. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_178);
  1020. bDirectReturn = false;
  1021. }
  1022. bUseKalman = false;
  1023. //bRet = true;
  1024. break;
  1025. case 1://需要判断状态是否发生了变化
  1026. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_179);
  1027. if (acce_state_last == ACCELERATE_INIT_STATE)
  1028. {
  1029. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_180);
  1030. //第一次获得卡的状态,需要确定首次方向
  1031. acce_direction = uwb_direction; //将UWB定出的速度方向赋值给它
  1032. }else{
  1033. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_181);
  1034. acce_direction = acce_state_last;
  1035. }
  1036. //bRet = false;
  1037. break;
  1038. case 2:
  1039. case 3:
  1040. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_182);
  1041. acce_direction = 0; //此处将速度方向设为静止,也就是0
  1042. //bRet = false;
  1043. break;
  1044. }
  1045. if (acce_state!=acce_last_state)
  1046. {
  1047. // 如果状态发生了改变,则加速度计的权重需要重置
  1048. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_183);
  1049. this->ins_weight = INS_WEIGHT;
  1050. }
  1051. double cweight = 0;
  1052. if(!bRet){
  1053. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_184);
  1054. cweight = this->ins_weight * acce_direction + this->uwb_weight*uwb_direction;
  1055. //如果计算出的权重在合适范围内,就逐渐降低惯导的权限
  1056. //如果计算出的权重超过范围,则重置惯导的权重为90%
  1057. if(cweight>=INS_WEIGHT*-1&&cweight<=INS_WEIGHT){
  1058. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_185);
  1059. cweight = abs(cweight);
  1060. }else{
  1061. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_186);
  1062. cweight = INS_WEIGHT;
  1063. }
  1064. if(cweight*uwb_direction < 0){
  1065. //惯导和uwb定位方向不一致,定位失败
  1066. //增加一次判断,与上一次的进行比对,如果和上一次的一致,则送进卡尔曼滤波
  1067. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_187);
  1068. if(cweight*acce_state_last > 0){
  1069. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_188);
  1070. bUseKalman = true;
  1071. }else{
  1072. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_189);
  1073. bUseKalman = false;
  1074. }
  1075. }else{
  1076. //惯导和uwb定位方向一致,定位成功
  1077. //送进卡尔曼滤波
  1078. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_190);
  1079. bUseKalman = true;
  1080. }
  1081. }
  1082. if (abs(cweight) > 0)
  1083. {
  1084. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_191);
  1085. ins_direction = cweight/abs(cweight) > 0?1:-1;
  1086. }else{
  1087. //如果加速度计的状态权重为零
  1088. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_192);
  1089. ins_direction = uwb_direction;
  1090. }
  1091. this->ins_weight = cweight;
  1092. this->ins_direction = ins_direction;
  1093. this->acce_last_state = acce_last_state;
  1094. //this->accelerate_state_last = this->acce_cur_state = acce_state;
  1095. #endif
  1096. this->accelerate_state_last = this->acce_cur_state = acce_state;
  1097. if(m_nFilterType == FILTER_KALMAN){
  1098. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_193);
  1099. #ifdef ALGORITHM_TYPE_INS
  1100. if(bUseKalman){
  1101. #endif
  1102. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_194);
  1103. //也需要考虑轮回
  1104. //double kalman_detal_t = (maxSyncTimes - this->last_locate.st)*interval_time;
  1105. double kalman_detal_t = 0;
  1106. if (maxSyncTimes - this->last_locate.sync_num < 0 && maxSyncTimes < 100)
  1107. {
  1108. kalman_detal_t = (maxSyncTimes + 65535 - this->last_locate.sync_num)*interval_time;
  1109. }else{
  1110. kalman_detal_t = (maxSyncTimes - this->last_locate.sync_num)*interval_time;
  1111. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_195);
  1112. }
  1113. //通过卡尔曼滤波处理
  1114. if(p->posx == INVALID_COORDINATE && p->posy == INVALID_COORDINATE){
  1115. m_afmData.nCardStamp = m_nCalcSyncNum;
  1116. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_196);
  1117. if(this->m_pKalmanFilter->m_nCounts < 3 || this->m_pKalmanFilter->m_pCar->P(0,0) > 2 || kalman_detal_t > 3){
  1118. //P(0,0):连续时间(大于2s)都定位失败
  1119. //deltaT>3:距离上次成功定位时间间隔为3s
  1120. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_197);
  1121. this->x = this->last_locate.x;
  1122. this->y = this->last_locate.y;
  1123. this->z = 0;
  1124. pRdm->clear();
  1125. m_afmData.bStatus = true;
  1126. m_afmData.strCardId = this->card_id;
  1127. this->m_pKalmanFilter->m_bFlag = false;
  1128. m_afmData.nType = ALGO_FAILED_CONDITION_8;
  1129. ALGORITHM_FAILED(ALGO_FAILED_CONDITION_8);
  1130. return;
  1131. }
  1132. if(this->m_pKalmanFilter->m_nCounts >= 3){
  1133. //只有三次以上才允许使用kalman滤波以下的函数
  1134. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_198);
  1135. this->m_pKalmanFilter->Predict(kalman_detal_t);
  1136. this->z = 0;
  1137. sdNew.update = true;
  1138. }
  1139. }else{
  1140. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_199);
  1141. this->m_pKalmanFilter->m_bFlag = true;
  1142. this->m_pKalmanFilter->m_nCounts++;
  1143. this->m_pKalmanFilter->m_pCar->z(0,0) = cx * this->map_scale;
  1144. this->m_pKalmanFilter->m_pCar->z(1,0) = cvx;
  1145. this->m_pKalmanFilter->m_pCar->z(2,0) = cy * this->map_scale;
  1146. this->m_pKalmanFilter->m_pCar->z(3,0) = cvy;
  1147. if(this->m_pKalmanFilter->m_nCounts == 1){
  1148. //第一次直接赋值
  1149. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_200);
  1150. this->m_pKalmanFilter->m_pCar->x = this->m_pKalmanFilter->m_pCar->z;
  1151. }
  1152. if(this->m_pKalmanFilter->m_nCounts == 2){
  1153. //两次处理
  1154. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_201);
  1155. this->m_pKalmanFilter->m_pCar->z(1, 0) = (this->m_pKalmanFilter->m_pCar->z(0, 0) - this->m_pKalmanFilter->m_pCar->x(0, 0))/deltaT;
  1156. this->m_pKalmanFilter->m_pCar->z(3, 0) = (this->m_pKalmanFilter->m_pCar->z(2, 0) - this->m_pKalmanFilter->m_pCar->x(2, 0))/deltaT;
  1157. this->m_pKalmanFilter->m_pCar->x = this->m_pKalmanFilter->m_pCar->z;
  1158. }
  1159. if(this->m_pKalmanFilter->m_nCounts >= 3){
  1160. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_202);
  1161. //只有三次以上才允许使用kalman滤波以下的函数
  1162. //this->m_pKalmanFilter->Predict_Correct(deltaT);
  1163. this->m_pKalmanFilter->Predict_Correct(kalman_detal_t);
  1164. sdNew.update = true;
  1165. if(deltaT!=0){
  1166. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_203);
  1167. this->m_pKalmanFilter->m_pCar->x(1,0) = (this->m_pKalmanFilter->m_pCar->x(0,0) - sd.x*this->map_scale)/deltaT;
  1168. this->m_pKalmanFilter->m_pCar->x(3,0) = (this->m_pKalmanFilter->m_pCar->x(2,0) - sd.y*this->map_scale)/deltaT;
  1169. }
  1170. }
  1171. }
  1172. /*if(p->nFirstReader == -1 && p->nSecondReader == -1){
  1173. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_204);
  1174. p->nFirstReader = last_s_locate_reader[0];
  1175. p->nSecondReader = last_s_locate_reader[1];
  1176. }*/
  1177. //增加地图集的判定,判断定位结果是否在地图集上
  1178. //如果不在地图集上,需要再次定位
  1179. //需要带出定位结果的分站信息,
  1180. //利用地图集中分站信息再次定位
  1181. //std::shared_ptr<POS> kalman_p(new POS());
  1182. std::shared_ptr<POS> kalman_p = std::make_shared<POS>();
  1183. kalman_p->nFirstReader = p->nFirstReader;
  1184. kalman_p->nSecondReader = p->nSecondReader;
  1185. kalman_p->posx = this->m_pKalmanFilter->m_pCar->x(0,0);//* this->map_scale
  1186. kalman_p->posy = this->m_pKalmanFilter->m_pCar->x(2,0);//* this->map_scale
  1187. kalman_p->pos_radius = p->pos_radius;
  1188. //二维定位不需要再判定在地图集上了
  1189. if(!LocateAlgorithm::IsOnMap(kalman_p,pTdoaReaderPathMap)){
  1190. //再一次定位到地图集上
  1191. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_205);
  1192. //std::shared_ptr<POS> cp = LocateAlgorithm::Pos(kalman_p,pTdoaReaderPathMap);
  1193. // 定位结果在地图集外,映射到地图集上
  1194. std::shared_ptr<POS> cp = LocateAlgorithm::MappingToPath(kalman_p,pTdoaReaderPathMap);
  1195. if(cp != nullptr){
  1196. if (cp->posx != INVALID_COORDINATE && cp->posy !=INVALID_COORDINATE)
  1197. {
  1198. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_206);
  1199. this->m_pKalmanFilter->m_pCar->x(0,0) = cp->posx;
  1200. this->m_pKalmanFilter->m_pCar->x(2,0) = cp->posy;
  1201. }
  1202. }
  1203. cp.reset();
  1204. }
  1205. this->x = this->m_pKalmanFilter->m_pCar->x(0,0) / this->map_scale;
  1206. this->y = this->m_pKalmanFilter->m_pCar->x(2,0) / this->map_scale;
  1207. if (abs(this->last_locate.x) > 1E-5 || abs(this->last_locate.y) > 1E-5)
  1208. {
  1209. //去除采集刚启动,last_locate的坐标为零而导致判断车辆上下行方向错误的问题
  1210. //车辆上下行确定
  1211. //3个条件:起点(x1,y1),终点(x2,y2)
  1212. //1.x1==x2的情况下,y2>y1为下行
  1213. //2.y1==y2的情况下,x1>x2为下行
  1214. //3.x1>x2且y2>y1为下行
  1215. //其他情况为上行
  1216. if ((this->last_locate.x == this->x && this->y > this->last_locate.y)
  1217. ||(this->last_locate.x > this->x && this->y == this->last_locate.y)
  1218. ||(this->last_locate.x > this->x && this->y > this->last_locate.y))
  1219. {
  1220. this->m_nStream = DOWN_STREAM;
  1221. }
  1222. else
  1223. {
  1224. this->m_nStream = UP_STREAM;
  1225. }
  1226. }
  1227. nSign = 1;
  1228. if(this->m_pKalmanFilter->m_pCar->x(1,0) == 0){
  1229. if(this->m_pKalmanFilter->m_pCar->x(3,0)>0){
  1230. nSign = 1;
  1231. }
  1232. }else{
  1233. if(this->m_pKalmanFilter->m_pCar->x(1,0) > 0){
  1234. nSign = 1;
  1235. }else{
  1236. nSign = -1;
  1237. }
  1238. }
  1239. this->m_nMoveDirection = nSign;
  1240. algo_calc_offset();
  1241. this->last_locate.x = this->x;
  1242. this->last_locate.y = this->y;
  1243. this->last_locate.z = this->z;
  1244. this->last_locate.sync_num = maxSyncTimes;
  1245. /*last_s_locate_reader[0] = p->nFirstReader;
  1246. last_s_locate_reader[1] = p->nSecondReader;*/
  1247. this->m_pKalmanFilter->m_pCar->t = this->m_nLastLocateT = maxSyncTimes;
  1248. //速度的计算采用求平均的方式
  1249. double speed = sqrt(pow(this->m_pKalmanFilter->m_pCar->x(1,0),2) + pow(this->m_pKalmanFilter->m_pCar->x(3,0),2));
  1250. speed *=3.6; //转为km/h
  1251. if (vt_his_speed.size()==3)//10
  1252. {
  1253. vt_his_speed.pop_front();
  1254. }
  1255. vt_his_speed.push_back(speed);
  1256. int total = 0;
  1257. double sum_speed = 0;
  1258. for (list<double>::iterator it = vt_his_speed.begin();it != vt_his_speed.end();++it)
  1259. {
  1260. if (*it > 0)
  1261. {
  1262. sum_speed += *it;
  1263. total++;
  1264. }
  1265. }
  1266. double av = 0;
  1267. if (total>0)
  1268. {
  1269. av = sum_speed / total;
  1270. }
  1271. if (this->acce_cur_state == STATE_ACCE_STATIC)
  1272. {
  1273. this->v = 0;
  1274. }
  1275. else
  1276. {
  1277. this->v = av*nSign;
  1278. }
  1279. cvx = this->m_pKalmanFilter->m_pCar->x(1,0);
  1280. cvy = this->m_pKalmanFilter->m_pCar->x(3,0);
  1281. this->last_locate.v = this->v;
  1282. this->origin_locate.v = cv;
  1283. //如果是第一次定位,则下一次即为非第一次定位了
  1284. if (this->is_algo_first_location)
  1285. {
  1286. this->is_algo_first_location = false;
  1287. }
  1288. ALGORITHM_FAILED(ALGO_LOC_SUCCESSED);
  1289. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_207);
  1290. if(sdNew.update){
  1291. sdNew.sync_num = maxSyncTimes;
  1292. sdNew.x = this->x;
  1293. sdNew.y = this->y;
  1294. sdNew.vx = cvx;
  1295. sdNew.vy = cvy;
  1296. m_syncNumList.push_back(sdNew);
  1297. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_208);
  1298. }
  1299. //校验机会3次,3次内跳回去就丢弃此次计算结果
  1300. if (this->x != INVALID_COORDINATE && this->y != INVALID_COORDINATE)
  1301. {
  1302. int nDirection = 0;
  1303. if (!algo_is_same_direction(this->x,this->y,0))
  1304. {
  1305. this->x = this->last_locate.x;
  1306. this->y = this->last_locate.y;
  1307. }
  1308. }
  1309. #ifdef ALGORITHM_TYPE_INS
  1310. }else{
  1311. //这组数据的处理方法是:
  1312. //如果第一次成功定位,但状态是静止,就取第一次成功定位值(这是为了处理当第一次成功定位,但状态是静止的,此时取上一次定位值为零的问题),
  1313. //后续如果不管定位成功还是失败,只要状态是静止的,就输出上一次成功定位值,并更新同步序号。
  1314. //取上次结果
  1315. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_209);
  1316. if (bDirectReturn)
  1317. {
  1318. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_210);
  1319. if(this->map_scale > 0){
  1320. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_211);
  1321. this->last_locate.x = this->x = p->posx / (this->map_scale*1.0);
  1322. this->last_locate.y = this->y = p->posy / (this->map_scale*1.0);
  1323. this->last_locate.z = this->z = p->posz / (this->map_scale*1.0);
  1324. this->last_locate.v = this->v = cv;
  1325. }
  1326. }else{
  1327. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_212);
  1328. this->x = this->last_locate.x;
  1329. this->y = this->last_locate.y;
  1330. this->z = -5;//this->last_locate.z
  1331. this->v = this->last_locate.v;
  1332. }
  1333. this->m_nLastLocateT = this->m_nCalcSyncNum = this->last_locate.st = this->sync_num = maxSyncTimes;
  1334. if(sdNew.update){
  1335. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_213);
  1336. sdNew.sync_num = maxSyncTimes;
  1337. sdNew.x = this->x;
  1338. sdNew.y = this->y;
  1339. sdNew.vx = cvx;
  1340. sdNew.vy = cvy;
  1341. m_syncNumList.push_back(sdNew);
  1342. this->m_nSyncNumInList = maxSyncTimes;
  1343. }
  1344. this->is_deal_by_algo = true;
  1345. }
  1346. #endif
  1347. }else{
  1348. //最新通过算法算出的结果
  1349. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_214);
  1350. if(p->posx == INVALID_COORDINATE || p->posy == INVALID_COORDINATE){
  1351. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_215);
  1352. this->x = this->last_locate.x;
  1353. this->y = this->last_locate.y;
  1354. this->z = this->last_locate.z;
  1355. }else{
  1356. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_216);
  1357. if(this->map_scale > 0){
  1358. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_216);
  1359. this->x = p->posx / (this->map_scale*1.0);
  1360. this->y = p->posy / (this->map_scale*1.0);
  1361. this->z = p->posz / (this->map_scale*1.0);
  1362. }
  1363. double speed = (fabs(this->v) + fabs(cv))/2;
  1364. this->v = speed*nSign;
  1365. this->last_locate.x = this->x;
  1366. this->last_locate.y = this->y;
  1367. this->last_locate.z = this->z;
  1368. this->last_locate.v = this->v;
  1369. this->m_nLastLocateT = this->last_locate.sync_num = maxSyncTimes;
  1370. }
  1371. this->a = 0;
  1372. }
  1373. }
  1374. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_217);
  1375. pRdm->clear();
  1376. }
  1377. int Card::algo_tdoa_1d(int cnt)
  1378. {
  1379. int ret = 0;
  1380. //1.数据有效性判断
  1381. ret = CheckDistData(cnt);
  1382. if(ret){
  1383. return ret;
  1384. }
  1385. //2.组装数据
  1386. std::shared_ptr<ReceiveDataMap> pRdm = std::make_shared<ReceiveDataMap>();
  1387. pRdm->clear();
  1388. ret = AssembleDistData(pRdm);
  1389. if(ret){
  1390. return ret;
  1391. }
  1392. //3.算法定位
  1393. //保存结果解
  1394. std::shared_ptr<POS> pos = std::make_shared<POS>();
  1395. //保存所有可能解
  1396. std::vector<std::shared_ptr<POS>> udm_pos;
  1397. udm_pos.resize(0);
  1398. //根据分站数据计算所有可能解,并将所有可能解保存到udm_pos中
  1399. ret = LocateAlgorithm::CalcTdoaPosition(pRdm,pTdoaReaderPathMap,udm_pos);
  1400. bool bIsBack = false;
  1401. if (ret == 0)
  1402. {
  1403. //4.从多解中筛选出一个解,存在两种可能:
  1404. //a.可能无解返回非0,
  1405. //b.可能有解,但解不正确,比如解的位置在4727,-100,但选出的解是4727,-200
  1406. ret = ChooseOneSolution(pRdm, udm_pos, pos);
  1407. //无论正确与否,保存原始值
  1408. SaveOriginDataBeforeFilter(pos);
  1409. if (ret == 0)
  1410. {
  1411. //5.如果有解,则对唯一解做合法性验证
  1412. //主要验证条件为:加速度和速度
  1413. ret = CheckSolution(pos);
  1414. if (ret == 0)
  1415. {
  1416. //对通过加速度验证的解进行是否回退的判断
  1417. if (IsRebound(pos))
  1418. {
  1419. if (count_change_direction < MAX_REBOUND_COUNTS)
  1420. {
  1421. //CalcPositionBySpeed(pos,last_locate.v);
  1422. //if (pos->posx != INVALID_COORDINATE && pos->posy != INVALID_COORDINATE)
  1423. if(CheckPositionValid(pos))
  1424. {
  1425. pos->cx = pos->posx / (map_scale*1.0);
  1426. pos->cy = pos->posy / (map_scale*1.0);
  1427. pos->update = true;
  1428. pos->is_fit = true;
  1429. count_change_direction++;
  1430. }else{
  1431. //这里可能需要加逻辑处理,避免一次回跳太多
  1432. count_change_direction = 0;
  1433. bIsBack = true;
  1434. }
  1435. }
  1436. else
  1437. {
  1438. count_change_direction = 0;
  1439. }
  1440. }
  1441. }else{
  1442. //加速度校验失败
  1443. //加速度或速度抛弃的结果
  1444. if (!IsRebound(pos))
  1445. {
  1446. //如果不是回退,说明了是往前大跳
  1447. //加速追
  1448. }else{
  1449. //说明是往回大跳
  1450. //减速走
  1451. bIsBack = true;
  1452. }
  1453. }
  1454. }else{
  1455. //选解失败
  1456. return 1;
  1457. }
  1458. }
  1459. if (acce_cur_state == ACCELERATE_STATE::STATE_ACCE_MOTION && !bIsBack)
  1460. {
  1461. //6.卡尔曼滤波,两种情况:
  1462. //a.解通过了合法性验证,b.无解,但在2s内
  1463. //则使用卡尔曼滤波进行滤波处理
  1464. if (FILTER_KALMAN == m_nFilterType)
  1465. {
  1466. ret = KalmanFilterProcess(pos);
  1467. }
  1468. }
  1469. bool bRet = LocateAlgorithm::IsOnMap(pos,pTdoaReaderPathMap);
  1470. if (bRet)
  1471. {
  1472. //7.数据保存
  1473. ret = SaveCardAlgoData(pos);
  1474. }
  1475. return ret;
  1476. }
  1477. int Card::algo_tdoa_2d(int cnt)
  1478. {
  1479. int ret = 0;
  1480. //1.数据有效性判断
  1481. ret = CheckDistData(cnt);
  1482. if(ret) return ret;
  1483. //2.组装数据
  1484. std::shared_ptr<ReceiveDataMap> pRdm = std::make_shared<ReceiveDataMap>();
  1485. pRdm->clear();
  1486. ret = AssembleDistData(pRdm);
  1487. if(ret) return ret;
  1488. //3.算法定位
  1489. std::shared_ptr<POS> pos = std::make_shared<POS>();
  1490. pos = LocateAlgorithm::TdoaLocate2d(pRdm);
  1491. if (pos == nullptr)
  1492. {
  1493. return 0;
  1494. }else{
  1495. pos->cx = pos->posx / (map_scale*1.0);
  1496. pos->cy = pos->posy / (map_scale*1.0);
  1497. pos->cz = pos->posz / (map_scale*1.0);
  1498. isoutput = true;
  1499. }
  1500. //5.唯一解合法性验证
  1501. ret = CheckSolution(pos);
  1502. if(ret) {
  1503. return ret;
  1504. }
  1505. //保存原始值
  1506. SaveOriginDataBeforeFilter(pos);
  1507. //6.滤波
  1508. if (FILTER_KALMAN == m_nFilterType)
  1509. {
  1510. ret = KalmanFilterProcess(pos);
  1511. }
  1512. if (ret)
  1513. {
  1514. return ret;
  1515. }
  1516. //7.数据保存
  1517. ret = SaveCardAlgoData(pos);
  1518. if (ret)
  1519. {
  1520. return ret;
  1521. }
  1522. return ret;
  1523. }
  1524. int Card::algo_tdoa_3d(int cnt)
  1525. {
  1526. if (cnt < 3)
  1527. {
  1528. return 1;
  1529. }
  1530. int sync_num = 0;
  1531. int k = 0;
  1532. bool ret = false;
  1533. mp_dists_locate.clear();
  1534. for (DistMap::iterator it = _dists.front().distmap.begin();it!=_dists.front().distmap.end();++it,k++)
  1535. {
  1536. if (k==0)
  1537. {
  1538. sync_num = it->second->sync_num;
  1539. }
  1540. else
  1541. {
  1542. int diff = abs(sync_num - it->second->sync_num);
  1543. if (diff >= 5)
  1544. {
  1545. ret = true;
  1546. break;
  1547. }
  1548. }
  1549. if (it->second->tt == LLONG_MAX)
  1550. {
  1551. ret = true;
  1552. break;
  1553. }
  1554. mp_dists_locate.insert(make_pair(it->second->tt,it->second));
  1555. }
  1556. if (ret)
  1557. {
  1558. return 1;
  1559. }
  1560. //构造数据
  1561. int i = 0;
  1562. int max_sync_time = 0;
  1563. std::shared_ptr<ReceiveDataMap> pRdm = std::make_shared<ReceiveDataMap>();
  1564. pRdm->clear();
  1565. for (map<unsigned long long ,std::shared_ptr<_coordinate>>::iterator it = mp_dists_locate.begin();it!=mp_dists_locate.end();++it)
  1566. {
  1567. int card_time_stamp = 0;
  1568. if (i==0)
  1569. {
  1570. max_sync_time = it->second->sync_num;
  1571. card_time_stamp = it->second->t;
  1572. }else{
  1573. if (max_sync_time < it->second->sync_num)
  1574. {
  1575. max_sync_time = it->second->sync_num;
  1576. card_time_stamp = it->second->t;
  1577. }
  1578. }
  1579. ReceiveDataMap::iterator rdm_it = pRdm->find(it->second->tt);
  1580. if (rdm_it == pRdm->end())
  1581. {
  1582. //保存信息用于定位
  1583. std::shared_ptr<ReceiveData> prd = std::make_shared<ReceiveData>();
  1584. prd->reader_id = it->second->reader_id;
  1585. prd->antenna_id = it->second->antenna_id;
  1586. prd->rec_time_stamp = it->second->tt;
  1587. prd->x = it->second->x*this->map_scale;
  1588. prd->y = it->second->y*this->map_scale;
  1589. prd->z = it->second->z*this->map_scale;
  1590. prd->special = it->second->special;
  1591. if (prd->rec_time_stamp > 0)
  1592. {
  1593. pRdm->insert(make_pair(prd->rec_time_stamp,prd));
  1594. }
  1595. }
  1596. i++;
  1597. }
  1598. if (pRdm->size() >2)
  1599. {
  1600. m_nCalcSyncNum = max_sync_time;
  1601. }
  1602. return 0;
  1603. }
  1604. /*
  1605. * 根据算法计算新位置的坐标以及卡的上一次坐标,计算车辆偏移坐标
  1606. *
  1607. * param
  1608. * 无
  1609. *
  1610. * return
  1611. * 无locatepos
  1612. *
  1613. */
  1614. void Card::algo_calc_offset()
  1615. {
  1616. double offset = CHAMBER_WIDTH / 4;
  1617. // 根据方向判断,如何靠右行驶
  1618. if(abs(this->x - this->last_locate.x) <= 1E-4)
  1619. {
  1620. this->right_y = this->left_y = this->y;
  1621. if(this->y >= this->last_locate.y)
  1622. {
  1623. this->right_x = this->x - offset;
  1624. this->left_x = this->x + offset;
  1625. }
  1626. else
  1627. {
  1628. this->right_x = this->x + offset;
  1629. this->left_x = this->x - offset;
  1630. }
  1631. }
  1632. else
  1633. {
  1634. if(abs(this->y - this->last_locate.y) <= 1E-4)
  1635. {
  1636. this->right_x = this->left_x = this->x;
  1637. if(this->x > this->last_locate.x)
  1638. {
  1639. this->right_y = this->y + offset;
  1640. this->left_y = this->y - offset;
  1641. }
  1642. else
  1643. {
  1644. this->right_y = this->y - offset;
  1645. this->left_y = this->y + offset;
  1646. }
  1647. }
  1648. else
  1649. {
  1650. double k = -1.0 / (this->y - this->last_locate.y)/(this->x - this->last_locate.x);
  1651. double m = this->y - k * this->x;
  1652. double a = 1 + pow(k, 2);
  1653. double b = 2 * (k * m - this->y * k - this->x);
  1654. double c = pow(m, 2) + pow(this->x, 2) + pow(this->y, 2) - pow(offset, 2) - 2 * m * this->y;
  1655. if(this->last_locate.x < this->x && this->last_locate.y < this->y)
  1656. {
  1657. this->right_x = -0.5 * (b + sqrt(pow(b, 2) - 4 * a * c)) / a;
  1658. this->left_x = - 0.5 * (b - sqrt(pow(b, 2) - 4 * a * c)) / a;
  1659. }
  1660. else if(this->last_locate.x < this->x && this->last_locate.y > this->y)
  1661. {
  1662. this->right_x = -0.5 * (b - sqrt(pow(b, 2) - 4 * a * c)) / a;
  1663. this->left_x = - 0.5 * (b + sqrt(pow(b, 2) - 4 * a * c)) / a;
  1664. }
  1665. else if(this->last_locate.x > this->x && this->last_locate.y < this->y)
  1666. {
  1667. this->right_x = -0.5 * (b + sqrt(pow(b, 2) - 4 * a * c)) / a;
  1668. this->left_x = - 0.5 * (b - sqrt(pow(b, 2) - 4 * a * c)) / a;
  1669. }
  1670. else if(this->last_locate.x > this->x && this->last_locate.y > this->y)
  1671. {
  1672. this->right_x = -0.5 * (b - sqrt(pow(b, 2) - 4 * a * c)) / a;
  1673. this->left_x = - 0.5 * (b + sqrt(pow(b, 2) - 4 * a * c)) / a;
  1674. }
  1675. this->right_y = this->right_x * k + m;
  1676. this->left_y = this->left_x * k + m;
  1677. }
  1678. }
  1679. }
  1680. double Card::x_offset_after()
  1681. {
  1682. if(this->is_hist)
  1683. {
  1684. return this->x;
  1685. }
  1686. return (OUTPUT_POS::LEFT_POS == m_nOutputPosState) ? this->left_x : this->right_x;
  1687. }
  1688. double Card::y_offset_after()
  1689. {
  1690. if(this->is_hist)
  1691. {
  1692. return this->y;
  1693. }
  1694. return (OUTPUT_POS::LEFT_POS == m_nOutputPosState) ? this->left_y : this->right_y;
  1695. }
  1696. double Card::z_offset_after()
  1697. {
  1698. return this->z;
  1699. }
  1700. bool Card::algo_is_same_direction(double x,double y,double z)
  1701. {
  1702. int sign = 0;
  1703. if(x == last_locate.x){
  1704. if(y > last_locate.y){
  1705. sign = 1;
  1706. }else{
  1707. sign = -1;
  1708. }
  1709. }else{
  1710. if(x > last_locate.x){
  1711. sign = 1;
  1712. }else{
  1713. sign = -1;
  1714. }
  1715. }
  1716. if (sign!=m_nMoveDirection)
  1717. {
  1718. diff_direction_counts++;
  1719. }else{
  1720. diff_direction_counts = 0;
  1721. }
  1722. if (diff_direction_counts > 0 && diff_direction_counts <3)
  1723. {
  1724. return true;
  1725. }else{
  1726. diff_direction_counts = 0;
  1727. return false;
  1728. }
  1729. }
  1730. bool Card::is_pos_state_changed() // 考勤
  1731. {
  1732. bool ret = false;
  1733. if(pos_state != pos_state_old)
  1734. {
  1735. if((PDT_INIT == pos_state_old && PDT_UP == pos_state ) || (PDT_INIT == pos_state && PDT_UP == pos_state_old) )
  1736. {
  1737. pos_state = pos_state_old = PDT_UP;
  1738. pos_state_count = pos_state_confirm_times;
  1739. ret = false;
  1740. }
  1741. else if(pos_state_count >= pos_state_confirm_times)
  1742. {
  1743. pos_state_old = pos_state;
  1744. ret = true;
  1745. }
  1746. }
  1747. return ret ;
  1748. //return (this->last_area_type_id != this->cur_area_type_id && 0 == (this->cur_area_type_id & this->last_area_type_id));
  1749. }
  1750. /*
  1751. * 将数据加入到队列中,但相同卡的接受时间戳的数据满足一定条件时,将相应的数据丢入算法进行定位计算,
  1752. * 计算完后删除相应的数据,释放内存
  1753. *
  1754. * param
  1755. * dist 定位数据
  1756. *
  1757. * return
  1758. * 无
  1759. */
  1760. void Card::add_dist(_coordinate* dist)
  1761. {
  1762. EnterCriticalSection(&m_csCard);
  1763. string s = concat(dist->reader_id, dist->antenna_id);
  1764. int idx = FindDistMap(dist->t);
  1765. if(-1 == idx){
  1766. DistQueMapItem dq;
  1767. dq.cardstamp = dist->t;
  1768. _dists.push_back(dq);
  1769. }else{
  1770. }
  1771. //如果_dists.size()的大小始终为1,错误原因可能是卡在数据库中不存在
  1772. if(_dists.size() >= MAX_DIST_CACHE){ // 超过缓存数量限制
  1773. // 计算并删除第一个
  1774. get_coordinate();
  1775. remove_dist_head();
  1776. }
  1777. if (this->x != this->last_x || this->y != this->last_y)
  1778. {
  1779. this->b_pos_change = true;
  1780. }else{
  1781. this->b_pos_change = false;
  1782. }
  1783. LeaveCriticalSection(&m_csCard);
  1784. }
  1785. void Card::add_dist(std::shared_ptr<_coordinate> dist)
  1786. {
  1787. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_134);
  1788. EnterCriticalSection(&m_csCard);
  1789. string s = concat(dist->reader_id, dist->antenna_id);
  1790. int idx = FindDistMap(dist->t);
  1791. if(-1 == idx){
  1792. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_135);
  1793. DistQueMapItem dq;
  1794. dq.cardstamp = dist->t;
  1795. dq.distmap[s] = dist;
  1796. _dists.push_back(dq);
  1797. }else{
  1798. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_136);
  1799. _dists[idx].distmap[s] = dist;
  1800. }
  1801. if(_dists.size() >= MAX_DIST_CACHE){ // 超过缓存数量限制
  1802. // 计算并删除第一个
  1803. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_137);
  1804. get_coordinate(); //调用计算过程
  1805. _dists.pop_front();
  1806. }
  1807. if (this->x != this->last_x || this->y != this->last_y)
  1808. {
  1809. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_138);
  1810. this->b_pos_change = true;
  1811. }else{
  1812. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_139);
  1813. this->b_pos_change = false;
  1814. }
  1815. LeaveCriticalSection(&m_csCard);
  1816. }
  1817. time_t Card::get_working_time()
  1818. {
  1819. //return (card_type == CT_VEHICLE) ? leave_park_time : down_time;
  1820. return down_time;
  1821. }
  1822. int Card::get_effictive_dist_count( int offset /*= 0*/ )
  1823. {
  1824. if(0 == _dists.size()) return 0;
  1825. time_stamp_cal = _dists.front().cardstamp;
  1826. return (_dists.front().distmap.size());
  1827. }
  1828. /*
  1829. * Card析构类
  1830. */
  1831. Card::~Card(void)
  1832. {
  1833. if(init_postion){
  1834. for(int i = DIST_COUNT - 1;i >= 0;i--){
  1835. if(p_dists_locate[i] != NULL){
  1836. delete p_dists_locate[i];
  1837. p_dists_locate[i] = NULL;
  1838. }
  1839. }
  1840. if(p_dists_locate){
  1841. delete[] p_dists_locate;
  1842. p_dists_locate = NULL;
  1843. }
  1844. }
  1845. _dists.clear();
  1846. DeleteCriticalSection(&m_csCard);
  1847. }
  1848. double Card::get_speed()
  1849. {
  1850. return ceil(fabs(v));
  1851. }
  1852. /*
  1853. * 采用TOF或者TDOA算法进行定位计算
  1854. *
  1855. * param
  1856. * cnt ------ _dists数据条数
  1857. *
  1858. * return
  1859. * 无返回值
  1860. *
  1861. */
  1862. void Card::get_coordinate(int cnt)
  1863. {
  1864. int ret = 0;
  1865. switch(ranging_type){
  1866. case LOCATEDATATYPE::LDT_TDOA:
  1867. //ret = algo_tdoa_1d(cnt);
  1868. ret = DiscreteLocate(cnt);
  1869. break;
  1870. case LOCATEDATATYPE::LDT_TOF:
  1871. ret = algo_tof_1d(cnt);
  1872. break;
  1873. default:
  1874. ;
  1875. }
  1876. if (ret == 0)
  1877. {
  1878. m_indexForRcvOneReader = 0;
  1879. inspect_coordinate(this->acce_cur_state);
  1880. }
  1881. else if(DIST_COUNT_LESS_THAN_TWO == ret)
  1882. {
  1883. m_indexForRcvOneReader++;
  1884. }
  1885. else
  1886. {
  1887. m_indexForRcvOneReader=0;
  1888. }
  1889. if (m_indexForRcvOneReader >= INDEXFORRECONEREADER)
  1890. {
  1891. this->v = 0;
  1892. }
  1893. if(_isnan(this->x) || _isnan(this->y) || _isnan(this->z)){
  1894. this->x = this->last_x;
  1895. this->y = this->last_y;
  1896. this->z = this->last_z;
  1897. this->output_x = this->x;
  1898. this->output_y = this->y;
  1899. }
  1900. if ((isNeedWrited || nStartLocateCounts<6) && m_nCalcSyncNum > 0 && ret == 0)
  1901. {
  1902. std::mutex mu;
  1903. std::lock_guard<mutex> lg(mu);
  1904. std::string flag1 = is_fit_pos?"Y":"N";
  1905. int nSource1 = 1;
  1906. char chLog1[300] = {0};
  1907. sprintf_s(chLog1,"cardid: %s, sync_num: %d, ct: %d, ox: %f, oy: %f, x: %f,y: %f, readerName: %s, cellId : %d, originId: %d, oReaderName: %s, cv: %f, source: %d, fit_k : %f, fit_b: %f, fit_r: %f",
  1908. card_id.c_str(),
  1909. m_nCalcSyncNum,
  1910. time_stamp_cal,
  1911. origin_locate.x,
  1912. origin_locate.y,
  1913. x,
  1914. y,
  1915. curCellReaderName.c_str(),
  1916. curCellId,
  1917. originCellId,
  1918. originCellReaderName.c_str(),
  1919. get_speed(),
  1920. nSource1,
  1921. fit_k,
  1922. fit_b,
  1923. fit_r
  1924. );
  1925. std::string strLog1 = "";
  1926. strLog1 = chLog1;
  1927. Log::write_log(FILE_TYPE::KALMAN_S,strLog1,true);
  1928. isNeedWrited = false;
  1929. }
  1930. }
  1931. void Card::get_coordinate()
  1932. {
  1933. get_coordinate(get_effictive_dist_count());
  1934. }
  1935. /*
  1936. * 根据状态值获得状态文本描述
  1937. *
  1938. * param
  1939. * 无
  1940. *
  1941. * return
  1942. * 状态文本描述
  1943. *
  1944. */
  1945. std::string Card::get_state_text()
  1946. {
  1947. string ret = "";
  1948. this->state= 0;
  1949. this->state_biz = 0;
  1950. if(status_help == STATUS_ERROR){
  1951. //state += STATUS_HELP;
  1952. state_biz += STATUS_HELP;
  1953. ret += "呼救,";
  1954. }
  1955. if(status_area_over_time == STATUS_ERROR){
  1956. //state += STATUS_AREA_OVER_TIME;
  1957. state_biz += STATUS_AREA_OVER_TIME;
  1958. ret += "区域超时,";
  1959. }else if(status_area_over_time == STATUS_ERROR){
  1960. //state += STATUS_OVER_TIME;
  1961. state_biz += STATUS_OVER_TIME;
  1962. ret += "超时,";
  1963. }
  1964. if(status_area_over_speed == STATUS_ERROR){
  1965. //state += STATUS_AREA_OVER_SPEED;
  1966. state_biz += STATUS_AREA_OVER_SPEED;
  1967. ret += "区域超速,";
  1968. }else if(status_over_speed == STATUS_ERROR){
  1969. //state += STATUS_OVER_SPEED;
  1970. state_biz += STATUS_OVER_SPEED;
  1971. ret += "超速,";
  1972. }
  1973. if(status_area_forbidden == STATUS_ERROR){
  1974. //state += STATUS_AREA_FORBIDDEN;
  1975. state_biz += STATUS_AREA_FORBIDDEN;
  1976. ret += "进入限制区域,";
  1977. }
  1978. if(status_call == STATUS_ERROR){
  1979. //state += STATUS_CALL;
  1980. state_biz += STATUS_CALL;
  1981. ret += "呼叫,";
  1982. }
  1983. if(STATUS_LOST == status_lost){
  1984. //state += STATUS_LOST;
  1985. state_biz += STATUS_LOST;
  1986. ret += "进入盲区,";
  1987. }
  1988. //if(power_state == STATUS_ERROR){
  1989. // state += STATUS_POWER_LOWER;
  1990. // ret += "电量低,";
  1991. //}else
  1992. if(power_state == STATUS_ERROR_SERIOUS){
  1993. state += STATUS_POWER_LOWER_SERIOUS;
  1994. ret += "电量极低,";
  1995. }
  1996. if(ret.length() > 0){
  1997. ret = ret.substr(0, ret.length() - 1);
  1998. }else{
  1999. ret = "正常";
  2000. }
  2001. return ret;
  2002. }
  2003. std::string Card::get_acc_text()
  2004. {
  2005. string ret = "";
  2006. state_moving = (accelerate_state & 0x01)? STATE_ACCE_MOTION : STATE_ACCE_STATIC;
  2007. if(state_moving == STATE_ACCE_STATIC){
  2008. ret += "静止";
  2009. }else if(state_moving == STATE_ACCE_MOTION){
  2010. if(CT_VEHICLE == card_type && 0 == v)
  2011. {
  2012. state_moving = STATE_ACCE_IDLING;
  2013. ret += "怠速";
  2014. }
  2015. else
  2016. {
  2017. ret += "运动";
  2018. }
  2019. }else if(state_moving == STATE_ACCE_IDLING)
  2020. {
  2021. ret += "怠速";
  2022. }
  2023. return ret;
  2024. }
  2025. std::string Card::concat( int reader_id, int ant_id )
  2026. {
  2027. char s[10];
  2028. sprintf_s(s, "%d-%d", reader_id, ant_id);
  2029. return s;
  2030. }
  2031. /*
  2032. * 滤波功能设置
  2033. *
  2034. * param
  2035. * nType ------ 滤波类型
  2036. *
  2037. * return
  2038. * 无
  2039. */
  2040. void Card::EnableFilter(int nType)
  2041. {
  2042. //如果无滤波类型直接返回
  2043. if(nType == NO_FILTER){
  2044. return;
  2045. }
  2046. //开启滤波功能,设置滤波类型
  2047. m_bUseFilter = TRUE;
  2048. m_nFilterType = nType;
  2049. switch(nType){
  2050. case FILTER_KALMAN:
  2051. //分配卡尔曼滤波类型变量并初始化参数
  2052. if(m_pKalmanFilter == nullptr){
  2053. std::unique_ptr<CKalmanFilter> p(new CKalmanFilter());
  2054. m_pKalmanFilter = std::move(p);
  2055. m_pKalmanFilter->Initial(0.2);
  2056. m_pKalmanFilter->m_bFlag = false;
  2057. }
  2058. break;
  2059. default:
  2060. break;
  2061. }
  2062. }
  2063. void Card::remove_dist_head()
  2064. {
  2065. DistMap tmp = _dists.front().distmap;
  2066. if(tmp.size() > 0 ){
  2067. DistMap::iterator it_mp_dist = tmp.begin();
  2068. for(it_mp_dist;it_mp_dist != tmp.end();){
  2069. it_mp_dist = tmp.erase(it_mp_dist);
  2070. }
  2071. }
  2072. _dists.pop_front();
  2073. }
  2074. Reader::Reader(void)
  2075. {
  2076. reader_id = device_type_id = pos_state = map_id = area_id = temperature = tick_count= 0;
  2077. sync_level = 0xFF;
  2078. reader_state = reader_state_old = STATUS_DEVICE_NORMAL;
  2079. m_nIsSpecial = -1;
  2080. last_send_time = rec_time = reader_time = lost_time = time(NULL);
  2081. sync_rootId = 0;
  2082. map_scale = 1.0;
  2083. reader_x = reader_y = reader_z = reader_angle = reader_interval_time = -9999.0;
  2084. reader_name = ip = "";
  2085. for(int i = 0;i < ANTENNA_COUNT;i++){
  2086. ant[i] = nullptr;
  2087. }
  2088. for(int i = 0;i < ADHOC_COUNT;i++){
  2089. adhoc[i] = nullptr;
  2090. }
  2091. bIsInitCoverage = false;
  2092. init_ctrl_reader_state = false;
  2093. for(int i = 0; i < READER_EVENT_COUNT; i++){
  2094. m_event_list[i] = 0;
  2095. }
  2096. }
  2097. Reader::~Reader(void)
  2098. {
  2099. }
  2100. std::string Reader::get_state_text()
  2101. {
  2102. string ret = "";
  2103. if(reader_state == STATUS_DEVICE_ERROR){
  2104. ret = "故障";
  2105. }else if(reader_state == STATUS_DEVICE_NORMAL){
  2106. ret = "正常";
  2107. }
  2108. return ret;
  2109. }
  2110. Antenna::Antenna(void)
  2111. {
  2112. antenna_angle = 0;
  2113. antenna_id = 0;
  2114. antenna_x = 0;
  2115. antenna_y = 0;
  2116. antenna_z = 0;
  2117. }
  2118. Antenna::~Antenna(void)
  2119. {
  2120. }
  2121. Area::Area(void)
  2122. {
  2123. is_att = 1;
  2124. polygon_count = 0;
  2125. polygon = NULL;
  2126. map_id = area_id = area_type_id = 0 ;
  2127. area_name = area_type_name = path = "";
  2128. over_count_person = over_time_person = under_count_person = under_time_person = 0;
  2129. over_count_vehicle = over_time_vehicle = under_count_vehicle = under_time_vehicle = 0;
  2130. count_person = count_vehicle = count_card = 0;
  2131. is_area_over_time_person = is_area_over_time_vehicle = false;
  2132. count_area_over_time_person = count_area_over_time_vehicle = 0;
  2133. time_over_time_person = time_over_time_vehicle = time(NULL);
  2134. is_area_over_count_person = is_area_over_count_vehicle = false;
  2135. count_area_over_count_person = count_area_over_count_vehicle = 0;
  2136. time_over_count_person = time_over_count_vehicle = time(NULL);
  2137. is_area_forbidden_person = is_area_forbidden_vehicle = false;
  2138. count_area_forbidden_person = count_area_forbidden_vehicle = 0;
  2139. time_forbidden_person = time_forbidden_vehicle = time(NULL);
  2140. over_speed_vehicle = 0;
  2141. area_card_list_person = std::make_shared<CardMap>();
  2142. area_card_list_vehicle = std::make_shared<CardMap>();
  2143. area_card_list_over_speed = std::make_shared<CardMap>();
  2144. for(int i = 0; i < AREA_EVENT_COUNT; i++){
  2145. m_event_list[i] = 0;
  2146. }
  2147. }
  2148. Area::~Area(void)
  2149. {
  2150. if(polygon){
  2151. delete[] polygon;
  2152. polygon = NULL;
  2153. }
  2154. }
  2155. void Area::init_border(string sz_path)
  2156. {
  2157. if(sz_path == ""){
  2158. return ;
  2159. }
  2160. std::vector<std::string> vec = split(sz_path, " ");
  2161. std::vector<std::string>::iterator it = vec.begin();
  2162. if(polygon){
  2163. delete[] polygon;
  2164. polygon = NULL;
  2165. }
  2166. polygon = new _point[vec.size()];
  2167. polygon_count = 0;
  2168. for(; it != vec.end(); ++it){
  2169. std::vector<std::string> subvec = split(it->c_str(), ",");
  2170. _point p;
  2171. p.x = get_vertex(subvec[0]);
  2172. p.y = get_vertex(subvec[1]);
  2173. p.z = 0;
  2174. polygon[polygon_count] = p;
  2175. polygon_count++;
  2176. }
  2177. }
  2178. std::vector<std::string> Area::split( std::string str,std::string pattern )
  2179. {
  2180. std::string::size_type pos;
  2181. std::vector<std::string> result;
  2182. str+=pattern;//扩展字符串以方便操作
  2183. unsigned int size=str.size();
  2184. for(unsigned int i=0; i<size; i++){
  2185. pos=str.find(pattern,i);
  2186. if(pos<size){
  2187. std::string s=str.substr(i,pos-i);
  2188. result.push_back(s);
  2189. i=pos+pattern.size()-1;
  2190. }
  2191. }
  2192. return result;
  2193. }
  2194. double Area::get_vertex( std::string src)
  2195. {
  2196. std::string dest = "";
  2197. for(unsigned int i = 0; i < src.length(); i++){
  2198. if((src[i] >= '0' && src[i]<='9') || src[i]=='-' || src[i] == '.'){
  2199. dest += src[i];
  2200. }
  2201. }
  2202. return atof(dest.c_str());
  2203. }
  2204. bool Area::is_in_polygon( _point p )
  2205. {
  2206. if(polygon == NULL){
  2207. return false;
  2208. }
  2209. int counter = 0;
  2210. int i;
  2211. double xinters;
  2212. _point p1,p2;
  2213. p1 = polygon[0];
  2214. for (int i=1;i<= polygon_count;i++) {
  2215. p2 = polygon[i % polygon_count];
  2216. if (p.y > MIN(p1.y,p2.y)) {
  2217. if (p.y <= MAX(p1.y,p2.y)) {
  2218. if (p.x <= MAX(p1.x,p2.x)) {
  2219. if (p1.y != p2.y) {
  2220. xinters = (p.y-p1.y)*(p2.x-p1.x)/(p2.y-p1.y)+p1.x;
  2221. if (p1.x == p2.x || p.x <= xinters)
  2222. counter++;
  2223. }
  2224. }
  2225. }
  2226. }
  2227. p1 = p2;
  2228. }
  2229. return (counter % 2 == 0) ? false : true;
  2230. }
  2231. int Area::is_special()
  2232. {
  2233. return (area_type_id == AREA_TYPE_NO_COVER) ? 1: 0;
  2234. }
  2235. int Card::FindDistMap( int cardstamp )
  2236. {
  2237. int idx = -1;
  2238. for(int i = _dists.size() - 1; i >= 0; i--){
  2239. if(_dists[i].cardstamp == cardstamp ){
  2240. return i;
  2241. }
  2242. }
  2243. return idx;
  2244. }
  2245. /*
  2246. * 卡尔曼滤波处理模块
  2247. *
  2248. * param
  2249. * pos 定位结果
  2250. *
  2251. * return
  2252. * 处理成功返回0,否则返回非零值
  2253. *
  2254. */
  2255. int Card::KalmanFilterProcess(std::shared_ptr<POS>& pos)
  2256. {
  2257. //卡尔曼的时间差
  2258. double kalman_detal_t = 0;
  2259. //分站同步序号的间隔时间,单位为秒
  2260. double interval_time = 0.2;
  2261. kalman_detal_t = GetDeltaT();
  2262. if (abs(pos->diff_reader_sync_num) < ZERO_PRECISION)
  2263. {
  2264. pos->diff_reader_sync_num = kalman_detal_t;
  2265. }
  2266. //如果定位失败,通过卡尔曼滤波处理
  2267. //if(pos->posx == INVALID_COORDINATE && pos->posy == INVALID_COORDINATE){
  2268. if(!CheckPositionValid(pos)){
  2269. m_afmData.nCardStamp = m_nCalcSyncNum;
  2270. if(this->m_pKalmanFilter->m_nCounts < 3 || this->m_pKalmanFilter->m_pCar->P(0,0) > 2 || kalman_detal_t > 3){
  2271. //if(this->m_pKalmanFilter->m_nCounts < 3 || kalman_detal_t > 3){
  2272. //P(0,0):连续时间(大于2s)都定位失败
  2273. //deltaT>3:距离上次成功定位时间间隔为3s
  2274. //this->x = this->last_locate.x;
  2275. //this->y = this->last_locate.y;
  2276. pos->cx = INVALID_COORDINATE / map_scale;
  2277. pos->cy = INVALID_COORDINATE / map_scale;
  2278. pos->posx = INVALID_COORDINATE;
  2279. pos->posy = INVALID_COORDINATE;
  2280. pos->reason = KALMAN_FILTER_LONG_INTERVAL;
  2281. //this->z = 0;
  2282. m_afmData.bStatus = true;
  2283. m_afmData.strCardId = this->card_id;
  2284. this->m_pKalmanFilter->m_bFlag = false;
  2285. m_afmData.nType = ALGO_FAILED_CONDITION_8;
  2286. ALGORITHM_FAILED(ALGO_FAILED_CONDITION_8);
  2287. return KALMAN_FILTER_LONG_INTERVAL;
  2288. }
  2289. if(this->m_pKalmanFilter->m_nCounts >= 3){
  2290. //只有三次以上才允许使用kalman滤波以下的函数
  2291. this->m_pKalmanFilter->Predict(kalman_detal_t);
  2292. this->z = 0;
  2293. pos->update = true;
  2294. }
  2295. }else{
  2296. if (!is_algo_first_location)
  2297. {
  2298. //避免第一次时加入了两次
  2299. pos->update = true;
  2300. }
  2301. this->m_pKalmanFilter->m_bFlag = true;
  2302. this->m_pKalmanFilter->m_nCounts++;
  2303. this->m_pKalmanFilter->m_pCar->z(0,0) = pos->posx;
  2304. this->m_pKalmanFilter->m_pCar->z(1,0) = pos->cvx;
  2305. this->m_pKalmanFilter->m_pCar->z(2,0) = pos->posy;
  2306. this->m_pKalmanFilter->m_pCar->z(3,0) = pos->cvy;
  2307. if(this->m_pKalmanFilter->m_nCounts == 1){
  2308. //第一次直接赋值
  2309. this->m_pKalmanFilter->m_pCar->x = this->m_pKalmanFilter->m_pCar->z;
  2310. }
  2311. if(this->m_pKalmanFilter->m_nCounts == 2){
  2312. //两次处理
  2313. if (abs(pos->diff_reader_sync_num) > 1E-5)
  2314. {
  2315. this->m_pKalmanFilter->m_pCar->z(1, 0) = (this->m_pKalmanFilter->m_pCar->z(0, 0) - this->m_pKalmanFilter->m_pCar->x(0, 0))/pos->diff_reader_sync_num;
  2316. this->m_pKalmanFilter->m_pCar->z(3, 0) = (this->m_pKalmanFilter->m_pCar->z(2, 0) - this->m_pKalmanFilter->m_pCar->x(2, 0))/pos->diff_reader_sync_num;
  2317. this->m_pKalmanFilter->m_pCar->x = this->m_pKalmanFilter->m_pCar->z;
  2318. }
  2319. }
  2320. if(this->m_pKalmanFilter->m_nCounts >= 3){
  2321. //只有三次以上才允许使用kalman滤波以下的函数
  2322. //this->m_pKalmanFilter->Predict_Correct(deltaT);
  2323. this->m_pKalmanFilter->Predict_Correct(kalman_detal_t);
  2324. if(fabs(pos->diff_reader_sync_num) > ZERO_PRECISION){
  2325. if (abs(pos->ref_x) < ZERO_PRECISION && abs(pos->ref_y) < ZERO_PRECISION)
  2326. {
  2327. }else{
  2328. this->m_pKalmanFilter->m_pCar->x(1,0) = (this->m_pKalmanFilter->m_pCar->x(0,0) - pos->ref_x*this->map_scale)/pos->diff_reader_sync_num;
  2329. this->m_pKalmanFilter->m_pCar->x(3,0) = (this->m_pKalmanFilter->m_pCar->x(2,0) - pos->ref_y*this->map_scale)/pos->diff_reader_sync_num;
  2330. }
  2331. }
  2332. }
  2333. }
  2334. if (pos->update)
  2335. {
  2336. //二维定位不需要如下内容
  2337. //增加地图集的判定,判断定位结果是否在地图集上
  2338. //如果不在地图集上,需要再次定位
  2339. //需要带出定位结果的分站信息,
  2340. //利用地图集中分站信息再次定位
  2341. std::shared_ptr<POS> kalman_p = std::make_shared<POS>();
  2342. kalman_p->posx = this->m_pKalmanFilter->m_pCar->x(0,0);
  2343. kalman_p->posy = this->m_pKalmanFilter->m_pCar->x(2,0);
  2344. ////二维定位不需要再判定在地图集上了
  2345. if(!LocateAlgorithm::IsOnMap(kalman_p,pTdoaReaderPathMap)){
  2346. //再一次定位到地图集上
  2347. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_205);
  2348. std::shared_ptr<POS> cp = LocateAlgorithm::MappingToPath(kalman_p,pTdoaReaderPathMap);
  2349. if(cp != nullptr){
  2350. if (cp->posx != INVALID_COORDINATE && cp->posy !=INVALID_COORDINATE)
  2351. {
  2352. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_206);
  2353. this->m_pKalmanFilter->m_pCar->x(0,0) = cp->posx;
  2354. this->m_pKalmanFilter->m_pCar->x(2,0) = cp->posy;
  2355. }
  2356. }
  2357. cp.reset();
  2358. }
  2359. pos->posx = this->m_pKalmanFilter->m_pCar->x(0,0);
  2360. pos->posy = this->m_pKalmanFilter->m_pCar->x(2,0);
  2361. pos->cvx = this->m_pKalmanFilter->m_pCar->x(1,0);
  2362. pos->cvy = this->m_pKalmanFilter->m_pCar->x(3,0);
  2363. pos->cx = pos->posx / (map_scale*1.0);
  2364. pos->cy = pos->posy / (map_scale*1.0);
  2365. }
  2366. return 0;
  2367. }
  2368. /*
  2369. * 检查溢出
  2370. */
  2371. bool Card::CheckCrossCycle()
  2372. {
  2373. //time_stamp_last特指卡的计数序号,非分站的
  2374. //当卡需要新定位的计数序列号小于上一次成功定位的序列号,则此次不定位,避免定位结果的回退
  2375. if (this->time_stamp_last != 0 && this->time_stamp_last > this->time_stamp_cal)
  2376. {
  2377. string str_last = "";
  2378. string str_cur = "";
  2379. str_last = CFunctions::int2string(this->time_stamp_last);
  2380. str_cur = CFunctions::int2string(this->time_stamp_cal);
  2381. //如何解决卡的ct为2000时,卡重启,从0开始计数的问题?
  2382. if (str_last.length() >= 3 && str_cur.length() <= 2)
  2383. {
  2384. //满足此条件,表示标示卡的计数序号走完了65535的周期
  2385. return true;
  2386. }
  2387. else{
  2388. return false;
  2389. }
  2390. }
  2391. return true;
  2392. }
  2393. /*
  2394. * 组装拟合数据,主要是前REF_POSITION_NUM次的定位数据信息
  2395. *
  2396. * param
  2397. * pos 定位坐标
  2398. *
  2399. * return
  2400. * 成功返回0
  2401. */
  2402. int Card::UpdateFittingData(std::shared_ptr<POS> pos)
  2403. {
  2404. if (cur_ref_totals == REF_POSITION_NUM - 1)
  2405. {
  2406. for (int i = 0;i < cur_ref_totals-1;i++)
  2407. {
  2408. fitting_v[i] = fitting_v[i + 1];
  2409. fitting_ct[i] = fitting_ct[i + 1];
  2410. fitting_x[i] = fitting_x[i + 1];
  2411. fitting_y[i] = fitting_y[i + 1];
  2412. }
  2413. }
  2414. fitting_v[cur_ref_totals] = v;
  2415. fitting_ct[cur_ref_totals] = m_nCalcSyncNum;
  2416. fitting_x[cur_ref_totals] = pos->posx;
  2417. fitting_y[cur_ref_totals] = pos->posy;
  2418. if (cur_ref_totals < REF_POSITION_NUM - 1)
  2419. {
  2420. cur_ref_totals++;
  2421. }
  2422. return 0;
  2423. }
  2424. int Card::CalcFittingData()
  2425. {
  2426. /*if (cur_ref_totals != REF_POSITION_NUM - 1)
  2427. {
  2428. if (Fitting::VAR(fitting_v) < 25)
  2429. {
  2430. double fitting_coe_x[REF_POSITION_NUM] = {0};
  2431. double fitting_coe_y[REF_POSITION_NUM] = {0};
  2432. Fitting::EMatrix(fitting_ct,fitting_x,REF_POSITION_NUM,3,fitting_coe_x);
  2433. Fitting::EMatrix(fitting_ct,fitting_y,REF_POSITION_NUM,3,fitting_coe_y);
  2434. for (size_t i=0;i<FIT_POSITION_NUM;i++)
  2435. {
  2436. fit_new_x[i] = Fitting::polyval(fitting_coe_x,fitting_ct[cur_ref_totals] + i);
  2437. fit_new_y[i] = Fitting::polyval(fitting_coe_y,fitting_ct[cur_ref_totals] + i);
  2438. }
  2439. }
  2440. }*/
  2441. //如果滤波失败或者其他条件失败,
  2442. //则通过直线回归算法拟合出三个解
  2443. vector<double> vx,vy,vt;
  2444. vx.resize(0);
  2445. vy.resize(0);
  2446. vt.resize(0);
  2447. for (list<std::shared_ptr<POS>>::iterator it = his_pos.begin();it!=his_pos.end();++it)
  2448. {
  2449. vt.push_back((*it)->card_count);
  2450. vx.push_back((*it)->posx);
  2451. vy.push_back((*it)->posy);
  2452. }
  2453. //线性回归拟合,从5个历史点中取出3个预测点
  2454. Fit fit_x;
  2455. fit_x.linearFit(vt,vx);
  2456. Fit fit_y;
  2457. fit_y.linearFit(vt,vy);
  2458. for (int i = 0;i < FIT_POSITION_NUM;i++)
  2459. {
  2460. fit_new_x[i] = 0;
  2461. fit_new_y[i] = 0;
  2462. }
  2463. for (int i = 1;i <= FIT_POSITION_NUM;i++)
  2464. {
  2465. fit_new_x[i-1] = fit_x.getY(vt[4] + i);
  2466. fit_new_y[i-1] = fit_y.getY(vt[4] + i);
  2467. }
  2468. have_fit_pos = true;
  2469. return 0;
  2470. }
  2471. /*
  2472. * 计算线性拟合数据,
  2473. * 当满足如下两个条件:
  2474. * a.之前定位成功,
  2475. * b.参考数据小于5,
  2476. * c.连续取拟合数据等于3次了,
  2477. * 则不计算拟合数据
  2478. *
  2479. * param
  2480. * ret 定位状态
  2481. * pos 定位结果
  2482. *
  2483. * return
  2484. * 成功获得拟合数据返回0,否则返回1
  2485. *
  2486. */
  2487. int Card::CalcFittingData(int ret,std::shared_ptr<POS>& pos)
  2488. {
  2489. if (his_pos.size() < FIT_POSITION_NUM || cur_fit_nums >= 3)
  2490. {
  2491. if (cur_fit_nums == 3)
  2492. {
  2493. have_fit_pos = false;
  2494. }
  2495. return 1;
  2496. }
  2497. if (ret == 0 && pos->reason == 0)
  2498. {
  2499. return 2;
  2500. }
  2501. if (cur_fit_nums == 0)
  2502. {
  2503. //如果滤波失败或者其他条件失败,
  2504. //则通过直线回归算法拟合出三个解
  2505. vector<double> vx,vy,vt;
  2506. vx.resize(0);
  2507. vy.resize(0);
  2508. vt.resize(0);
  2509. for (list<std::shared_ptr<POS>>::iterator it = his_pos.begin();it!=his_pos.end();++it)
  2510. {
  2511. vt.push_back((*it)->card_count);
  2512. vx.push_back((*it)->posx);
  2513. vy.push_back((*it)->posy);
  2514. }
  2515. //线性回归拟合,从5个历史点中取出3个预测点
  2516. Fit fit_x;
  2517. fit_x.linearFit(vt,vx);
  2518. Fit fit_y;
  2519. fit_y.linearFit(vt,vy);
  2520. for (int i = 1;i <= FIT_POSITION_NUM;i++)
  2521. {
  2522. fit_new_x[i-1] = fit_x.getY(vt[4] + i);
  2523. fit_new_y[i-1] = fit_y.getY(vt[4] + i);
  2524. }
  2525. }
  2526. int index = cur_fit_nums;
  2527. //判断
  2528. double kx = fit_new_x[index];
  2529. double ky = fit_new_y[index];
  2530. std::shared_ptr<POS> ks = std::make_shared<POS>();
  2531. ks->posx = fit_new_x[index] ;
  2532. ks->posy = fit_new_y[index] ;
  2533. ks->cx = ks->posx / (1.0*map_scale);
  2534. ks->cy = ks->posy / (1.0*map_scale);
  2535. //判断拟合数据是否在地图集上,如果不在返回1
  2536. if (!LocateAlgorithm::IsOnMap(ks,pTdoaReaderPathMap))
  2537. {
  2538. //不在地图集上,则返回
  2539. return 1;
  2540. }
  2541. pos->posx = ks->posx;
  2542. pos->posy = ks->posy;
  2543. pos->cx = ks->cx;
  2544. pos->cy = ks->cy;
  2545. if (fabs(pos->diff_reader_sync_num) > 1E-4)
  2546. {
  2547. pos->cvx = (pos->cx - last_locate.x)/pos->diff_reader_sync_num;
  2548. pos->cvy = (pos->cy - last_locate.y)/pos->diff_reader_sync_num;
  2549. }
  2550. cur_fit_nums++;
  2551. pos->reason = 0;
  2552. pos->is_fit = true;
  2553. have_fit_pos = true;
  2554. return 0;
  2555. }
  2556. /*
  2557. * 通过多项式拟合计算数据,
  2558. * 当满足如下两个条件:
  2559. * a.之前定位成功,
  2560. * b.参考数据小于5,
  2561. * c.连续取拟合数据等于3次了,
  2562. * 则不计算拟合数据
  2563. *
  2564. * param
  2565. * ret 定位状态
  2566. * pos 定位结果
  2567. *
  2568. * return
  2569. * 成功获得拟合数据返回0,否则返回1
  2570. *
  2571. */
  2572. int Card::CalcLongFittingData(int ret,std::shared_ptr<POS>& pos)
  2573. {
  2574. int nums = FIT_POSITION_NUM*4;
  2575. if (long_his_pos.size() < nums || cur_fit_nums >= nums)
  2576. {
  2577. if (cur_fit_nums == nums)
  2578. {
  2579. have_fit_pos = false;
  2580. }
  2581. return 1;
  2582. }
  2583. if (ret == 0 && pos->reason == 0)
  2584. {
  2585. return 2;
  2586. }
  2587. if (cur_fit_nums <= 3)
  2588. {
  2589. //如果滤波失败或者其他条件失败,
  2590. //则通过多项式拟合算法拟合出20个解
  2591. vector<double> vx,vy,vt;
  2592. vx.resize(0);
  2593. vy.resize(0);
  2594. vt.resize(0);
  2595. for (list<std::shared_ptr<POS>>::iterator it = long_his_pos.begin();it!=long_his_pos.end();++it)
  2596. {
  2597. vt.push_back((*it)->card_count);
  2598. vx.push_back((*it)->posx);
  2599. vy.push_back((*it)->posy);
  2600. }
  2601. //多项式拟合,从20个历史点中拟合出20个预测点
  2602. Fit fit_x;
  2603. fit_x.polyfit(vt,vx,2);
  2604. Fit fit_y;
  2605. fit_y.polyfit(vt,vy,2);
  2606. for (int i = 1;i <= nums;i++)
  2607. {
  2608. long_fit_new_x[i-1] = fit_x.getY(vt[nums - 1] + i);
  2609. long_fit_new_y[i-1] = fit_y.getY(vt[nums - 1] + i);
  2610. }
  2611. }
  2612. int index = cur_fit_nums - 3;
  2613. //判断
  2614. double kx = long_fit_new_x[index];
  2615. double ky = long_fit_new_y[index];
  2616. std::shared_ptr<POS> ks = std::make_shared<POS>();
  2617. ks->posx = long_fit_new_x[index] ;
  2618. ks->posy = long_fit_new_y[index] ;
  2619. ks->cx = ks->posx / (1.0*map_scale);
  2620. ks->cy = ks->posy / (1.0*map_scale);
  2621. //判断拟合数据是否在地图集上,如果不在返回1
  2622. if (!LocateAlgorithm::IsOnMap(ks,pTdoaReaderPathMap))
  2623. {
  2624. //不在地图集上,则返回
  2625. return 1;
  2626. }
  2627. pos->posx = ks->posx;
  2628. pos->posy = ks->posy;
  2629. pos->cx = ks->cx;
  2630. pos->cy = ks->cy;
  2631. if (fabs(pos->diff_reader_sync_num) > 1E-4)
  2632. {
  2633. pos->cvx = (pos->cx - last_locate.x)/pos->diff_reader_sync_num;
  2634. pos->cvy = (pos->cy - last_locate.y)/pos->diff_reader_sync_num;
  2635. }
  2636. cur_fit_nums++;
  2637. pos->reason = 0;
  2638. pos->is_fit = true;
  2639. have_fit_pos = true;
  2640. return 0;
  2641. }
  2642. int Card::CalcLongFittingData()
  2643. {
  2644. int nums = FIT_POSITION_NUM*4;
  2645. //如果滤波失败或者其他条件失败,
  2646. //则通过多项式拟合算法拟合出20个解
  2647. vector<double> vx,vy,vt;
  2648. vx.resize(0);
  2649. vy.resize(0);
  2650. vt.resize(0);
  2651. for (list<std::shared_ptr<POS>>::iterator it = long_his_pos.begin();it!=long_his_pos.end();++it)
  2652. {
  2653. vt.push_back((*it)->card_count);
  2654. vx.push_back((*it)->posx);
  2655. vy.push_back((*it)->posy);
  2656. }
  2657. //多项式拟合,从20个历史点中拟合出20个预测点
  2658. Fit fit_x;
  2659. fit_x.polyfit(vt,vx,2);
  2660. Fit fit_y;
  2661. fit_y.polyfit(vt,vy,2);
  2662. for (int i = 0;i < nums;i++)
  2663. {
  2664. long_fit_new_x[i] = 0;
  2665. long_fit_new_y[i] = 0;
  2666. }
  2667. double diff_x = 0 , diff_y = 0;
  2668. double dx[FIT_POSITION_NUM*4] = {0};
  2669. double dy[FIT_POSITION_NUM*4] = {0};
  2670. for (int i = 1;i <= nums;i++)
  2671. {
  2672. long_fit_new_x[i-1] = fit_x.getY(vt[nums - 1] + i);
  2673. dx[i-1] = long_fit_new_x[i-1];
  2674. long_fit_new_y[i-1] = fit_y.getY(vt[nums - 1] + i);
  2675. dy[i-1] = long_fit_new_y[i-1];
  2676. }
  2677. double start_x = 0.0;
  2678. double end_x = 0.0;
  2679. double start_y = 0.0;
  2680. double end_y = 0.0;
  2681. start_x = long_fit_new_x[4];
  2682. start_y = long_fit_new_y[4];
  2683. end_x = long_fit_new_x[5];
  2684. end_y = long_fit_new_y[5];
  2685. double distance = sqrt(pow(start_x - end_x,2)+pow(start_y - end_y,2));
  2686. double percent = 0.5;
  2687. for (int i = 5;i < nums;i++)
  2688. {
  2689. distance *= percent;
  2690. if (abs(long_fit_new_x[i-1] - long_fit_new_x[i]) < 1E-4)
  2691. {
  2692. //表示x相等,在y轴上
  2693. if (long_fit_new_y[i-1] < long_fit_new_y[i])
  2694. {
  2695. //如果下一次拟合值比当前拟合值大
  2696. dy[i] = dy[i-1] + distance;
  2697. }else{
  2698. dy[i] = dy[i-1] - distance;
  2699. }
  2700. }else{
  2701. //计算斜率
  2702. double k = (long_fit_new_y[i-1] - long_fit_new_y[i])/(long_fit_new_x[i-1] - long_fit_new_x[i]);
  2703. //在有斜率的地方
  2704. double arg = atan(k);
  2705. if (long_fit_new_x[i-1] < long_fit_new_x[i] && long_fit_new_y[i-1] < long_fit_new_y[i])
  2706. {
  2707. dx[i] = dx[i-1] + cos(arg)*distance;
  2708. dy[i] = dy[i-1] + sin(arg)*distance;
  2709. }
  2710. else
  2711. {
  2712. dx[i] = dx[i-1] - cos(arg)*distance;
  2713. dy[i] = dy[i-1] - sin(arg)*distance;
  2714. }
  2715. }
  2716. //使用下一个点和第六点的距离取50%
  2717. distance = sqrt(pow(dx[i-1] - end_x,2) + pow(dy[i] - end_y,2));
  2718. }
  2719. for (int i = 0;i < nums;i++)
  2720. {
  2721. long_fit_new_x[i] = dx[i];
  2722. long_fit_new_y[i] = dy[i];
  2723. }
  2724. have_long_fit_pos = true;
  2725. return 0;
  2726. }
  2727. std::shared_ptr<POS> Card::GetPosFromFittingData()
  2728. {
  2729. if ((!have_fit_pos&&!have_long_fit_pos)
  2730. || (have_fit_pos && cur_fit_nums >= 3&&!have_long_fit_pos)
  2731. || (have_fit_pos && have_long_fit_pos && cur_fit_nums >= 20))
  2732. {
  2733. //1.如果5点拟合和20点拟合无数据,则无法获取
  2734. //2.如果有5点拟合,但无20点拟合,获取拟合数据超过3,则无法获取
  2735. //3.如果有5点拟合,且有20点拟合,而且获取拟合数据超过20,则无法获取
  2736. return nullptr;
  2737. }
  2738. std::shared_ptr<POS> p = std::make_shared<POS>();
  2739. //没有拟合20个点的数据
  2740. if (!have_long_fit_pos)
  2741. {
  2742. if (have_fit_pos)
  2743. {
  2744. p->posx = fit_new_x[cur_fit_nums];
  2745. p->posy = fit_new_y[cur_fit_nums];
  2746. p->cx = p->posx / (map_scale*1.0);
  2747. p->cy = p->posy / (map_scale*1.0);
  2748. p->is_fit = true;
  2749. p->reason = 0;
  2750. cur_fit_nums++;
  2751. }
  2752. }else{
  2753. //有拟合20个点的数据
  2754. p->posx = long_fit_new_x[cur_fit_nums];
  2755. p->posy = long_fit_new_y[cur_fit_nums];
  2756. p->cx = p->posx / (map_scale*1.0);
  2757. p->cy = p->posy / (map_scale*1.0);
  2758. p->is_fit = true;
  2759. p->reason = 0;
  2760. cur_fit_nums++;
  2761. }
  2762. if (p->posx == INVALID_COORDINATE && p->posy == INVALID_COORDINATE)
  2763. {
  2764. p = nullptr;
  2765. }
  2766. return p;
  2767. }
  2768. int Card::GetPosFromFittingData(std::shared_ptr<POS>& pos)
  2769. {
  2770. if (have_fit_pos || have_long_fit_pos)
  2771. {
  2772. //如果拟合了数据,则直接取拟合数据
  2773. std::shared_ptr<POS> fit_p = GetPosFromFittingData();
  2774. if (fit_p == nullptr)
  2775. {
  2776. return 1;
  2777. }
  2778. pos->posx = fit_p->posx;
  2779. pos->posy = fit_p->posy;
  2780. pos->cx = fit_p->cx;
  2781. pos->cy = fit_p->cy;
  2782. if (m_nCalcSyncNum - this->last_locate.sync_num < 0 && m_nCalcSyncNum < 100)
  2783. {
  2784. pos->diff_reader_sync_num = (m_nCalcSyncNum + 65536 - this->last_locate.sync_num)*0.2;
  2785. }else{
  2786. pos->diff_reader_sync_num = (m_nCalcSyncNum - this->last_locate.sync_num)*0.2;
  2787. }
  2788. if (!LocateAlgorithm::IsOnMap(pos,pTdoaReaderPathMap))
  2789. {
  2790. pos->reason = 1;
  2791. return 1;
  2792. }else{
  2793. //如果在地图集上,则计算相关参数
  2794. if (fabs(pos->diff_reader_sync_num) > 1E-4)
  2795. {
  2796. pos->cvx = (pos->cx - last_locate.x)/pos->diff_reader_sync_num;
  2797. pos->cvy = (pos->cy - last_locate.y)/pos->diff_reader_sync_num;
  2798. }else{
  2799. pos->cvx = last_vx;
  2800. pos->cvy = last_vy;
  2801. }
  2802. cur_fit_nums++;
  2803. pos->reason = 0;
  2804. pos->is_fit = true;
  2805. }
  2806. }else{
  2807. return 1;
  2808. }
  2809. return 0;
  2810. }
  2811. int Card::CheckSolutionByFit(int ret,std::shared_ptr<POS>& pos)
  2812. {
  2813. int fit_ret = 0;
  2814. //拟合预测解
  2815. is_ref_pos = true;
  2816. std::shared_ptr<POS> fit_pos = GetPosFromFittingData();
  2817. if (fit_pos == nullptr)
  2818. {
  2819. return 1;
  2820. }
  2821. //存在如下情况,解是个错误解
  2822. if (ret && fit_ret == 0)
  2823. {
  2824. //如果加速度和速度失败了或选解选不出来,而且有拟合数据了
  2825. //则直接使用拟合解
  2826. pos->posx = fit_pos->posx;
  2827. pos->posy = fit_pos->posy;
  2828. pos->cx = fit_pos->cx;
  2829. pos->cy = fit_pos->cy;
  2830. pos->cvx = fit_pos->cvx;
  2831. pos->cvy = fit_pos->cvy;
  2832. pos->is_fit = fit_pos->is_fit;
  2833. //如果是拟合出来的点就不能作为参考点
  2834. is_ref_pos = !pos->is_fit;
  2835. }else{
  2836. if (fit_ret == 0)
  2837. {
  2838. //如果有拟合数据
  2839. //如果选出了一个解,需要判断此解和预测值得距离
  2840. double distance = 0.0;
  2841. distance = sqrt(pow(pos->posx - fit_pos->posx,2) + pow(pos->posy - fit_pos->posy,2));
  2842. if (distance > 10)
  2843. {
  2844. //如果定位出的解和拟合解的距离差大于10,则认为拟合解可信
  2845. pos->posx = fit_pos->posx;
  2846. pos->posy = fit_pos->posy;
  2847. pos->cx = fit_pos->cx;
  2848. pos->cy = fit_pos->cy;
  2849. pos->cvx = fit_pos->cvx;
  2850. pos->cvy = fit_pos->cvy;
  2851. pos->is_fit = fit_pos->is_fit;
  2852. //如果是拟合出来的点就不能作为参考点
  2853. is_ref_pos = !pos->is_fit;
  2854. }
  2855. }
  2856. if (fit_ret && ret == 0)
  2857. {
  2858. //表示没有拟合值,但解是可信的,则直接返回0
  2859. return 0;
  2860. }
  2861. }
  2862. return fit_ret;
  2863. }
  2864. int Card::CheckSolutionBySpeed(std::shared_ptr<POS>& pos)
  2865. {
  2866. if (pos->posx == INVALID_COORDINATE && pos->posy == INVALID_COORDINATE)
  2867. {
  2868. return 1;
  2869. }
  2870. //double speed = sqrt(pow(pos->cvx,2) + pow(pos->cvy,2));
  2871. if (m_nLastLocateT != 0)
  2872. {
  2873. //如果第一次定位不做速度的判别
  2874. double speed = sqrt(pow(pos->cvx,2) + pow(pos->cvy,2));
  2875. speed *= 3.6; //转为km/h
  2876. if (speed > MAX_VECHILE_SPEED)
  2877. {
  2878. return 1;
  2879. }
  2880. }
  2881. return 0;
  2882. }
  2883. //根据点的信息获得格子信息
  2884. std::shared_ptr<Cell> Card::Position2Cell(std::shared_ptr<POS> pos)
  2885. {
  2886. if (mpCellPath == nullptr)
  2887. {
  2888. return nullptr;
  2889. }
  2890. std::shared_ptr<Cell> cell = nullptr;
  2891. //获得格子列表
  2892. std::string name1 = CFunctions::getCellName(pos->nFirstReader,pos->nSecondReader);
  2893. std::string name2 = CFunctions::getCellName(pos->nSecondReader,pos->nFirstReader);
  2894. int nIdx = 0;
  2895. CellPathMap::iterator it = mpCellPath->find(name1);
  2896. if (it == mpCellPath->end())
  2897. {
  2898. it = mpCellPath->find(name2);
  2899. if (it == mpCellPath->end())
  2900. {
  2901. return nullptr;
  2902. }else{
  2903. nIdx = 2;
  2904. }
  2905. }else{
  2906. nIdx = 1;
  2907. }
  2908. //其他方式,判断点距离某个分站的距离,使用距离除以间隔即可
  2909. std::list<std::shared_ptr<Cell>>::iterator it_cell = it->second.end();
  2910. for (it_cell = it->second.begin();it_cell != it->second.end();++it_cell)
  2911. {
  2912. //获得cell,判断点是否在cell内部
  2913. point p;
  2914. p.x = pos->posx;
  2915. p.y = pos->posy;
  2916. if (LocateAlgorithm::PointIsInRect(p,(*it_cell)->top,(*it_cell)->left,(*it_cell)->bottom,(*it_cell)->right))
  2917. {
  2918. cell = std::make_shared<Cell>();
  2919. *cell = *(*it_cell);
  2920. switch (nIdx)
  2921. {
  2922. case 1:
  2923. cell->strSubjectReader = name1;
  2924. break;
  2925. case 2:
  2926. cell->strSubjectReader = name2;
  2927. break;
  2928. default:
  2929. break;
  2930. }
  2931. break;
  2932. }
  2933. }
  2934. if (cell)
  2935. {
  2936. if (cell->strSubjectReader == "")
  2937. {
  2938. return nullptr;
  2939. }
  2940. }else{
  2941. return nullptr;
  2942. }
  2943. return cell;
  2944. }
  2945. //把中心点的坐标赋值给pos
  2946. std::shared_ptr<POS> Card::Cell2Position(std::shared_ptr<Cell>& cell)
  2947. {
  2948. if (cell == nullptr)
  2949. {
  2950. return nullptr;
  2951. }
  2952. std::shared_ptr<POS> pos = std::make_shared<POS>();
  2953. //把cell的中心点的坐标给cell
  2954. pos->posx = (cell->top.x + cell->bottom.x)/2;
  2955. pos->posy = (cell->top.y + cell->bottom.y)/2;
  2956. pos->cx = pos->posx/map_scale;
  2957. pos->cy = pos->posy/map_scale;
  2958. pos->update = true;
  2959. pos->is_fit = cell->isFit;
  2960. return pos;
  2961. }
  2962. bool Card::CheckCardCtValid()
  2963. {
  2964. bool bRet = true;
  2965. // 本次序号小于上次序号,存在如下情况
  2966. // 1、跨周期,如本次0,上次为65535,另外进入盲区也可能跨周期后回来
  2967. // 2、卡重置,从0开始,初始状态值为-1
  2968. // 3、序号较连续,新数据先到,旧数据后到,会造成跳动
  2969. if (this->time_stamp_last != -1 && this->time_stamp_last > this->time_stamp_cal)
  2970. {
  2971. if((unsigned short)this->time_stamp_last - (unsigned short)this->time_stamp_cal < MAX_SYNCTIME_DELAY_NUM)
  2972. {
  2973. // 小于5,认为会影响数据
  2974. this->x = this->last_locate.x;
  2975. this->y = this->last_locate.y;
  2976. this->z = 0;
  2977. m_afmData.bStatus = true;
  2978. m_afmData.strCardId = this->card_id;
  2979. m_afmData.nCardStamp = this->time_stamp_cal;
  2980. m_afmData.nType = ALGO_FAILED_CONDITION_4;
  2981. ALGORITHM_FAILED(ALGO_FAILED_CONDITION_4);
  2982. //此处需要输出计数
  2983. return false;
  2984. }
  2985. }
  2986. return bRet;
  2987. }
  2988. double Card::GetFittingDegree(std::list<std::shared_ptr<Cell>> ltCells)
  2989. {
  2990. if (ltCells.size() < 5)
  2991. {
  2992. return 1.0;
  2993. }
  2994. double degree = 0.0;
  2995. double ctArray[5] = {0.0};
  2996. double cidArray[5] = {0};
  2997. //获取5个元素的值
  2998. int i = 4;
  2999. for (std::list<std::shared_ptr<Cell>>::reverse_iterator rit = ltCells.rbegin();rit != ltCells.rend();++rit)
  3000. {
  3001. ctArray[i] = (*rit)->card_stamp_time;
  3002. cidArray[i] = (*rit)->id;
  3003. i--;
  3004. if (i<0)
  3005. {
  3006. break;
  3007. }
  3008. }
  3009. //构造拟合对象
  3010. Fit f;
  3011. f.linearFit(ctArray,cidArray,5,true);
  3012. //获得拟合度
  3013. degree = f.getR();
  3014. return degree;
  3015. }
  3016. int Card::MappingCell2FitLinear(std::shared_ptr<Cell>& cell)
  3017. {
  3018. if (cell == nullptr)
  3019. {
  3020. return 1;
  3021. }
  3022. double ctArray[5] = {0.0};
  3023. double cidArray[5] = {0};
  3024. //获取新5个元素的值
  3025. int i = 4;
  3026. for (std::list<std::shared_ptr<Cell>>::reverse_iterator rit = his_cell.rbegin();rit != his_cell.rend();++rit)
  3027. {
  3028. ctArray[i] = (*rit)->card_stamp_time;
  3029. cidArray[i] = (*rit)->id;
  3030. i--;
  3031. if (i<0)
  3032. {
  3033. break;
  3034. }
  3035. }
  3036. //第五点和前4点拟合的直线方程y = kx + b的k,b
  3037. Fit f;
  3038. f.linearFit(ctArray,cidArray,5,true);
  3039. if (f.getR()!=1)
  3040. {
  3041. double k = f.getSlope();
  3042. double b = f.getIntercept();
  3043. //取得垂直线的k
  3044. /*double k1 = -1.0/k;
  3045. double b1 = cidArray[4] - k1*ctArray[4];*/
  3046. fit_k = k;
  3047. fit_b = b;
  3048. double y = fit_k*ctArray[4] + b;
  3049. /*double x = (b1 - b) /(k + k1);
  3050. double y = k1*x + b1;*/
  3051. //这里的y即为第五点格子id投影到直线上的格子id
  3052. int newCellId = int(y);
  3053. //将新格子id进行更新
  3054. if (newCellId < mpCellPath->find(curCellReaderName)->second.size())
  3055. {
  3056. cell->id = newCellId;
  3057. }else{
  3058. int n = cell->id;
  3059. int b = n;
  3060. n = b;
  3061. }
  3062. }
  3063. return 0;
  3064. }
  3065. int Card::PersonLocation(int cnt)
  3066. {
  3067. int ret = 0;
  3068. std::shared_ptr<Cell> cell = nullptr;
  3069. std::shared_ptr<Cell> tmp_cell = std::make_shared<Cell>();
  3070. is_fit_pos = false;
  3071. //1.数据有效性判断
  3072. ret = CheckDistData(cnt);
  3073. if(ret){
  3074. //time_stamp_cal== 14931 14878
  3075. originCellId = 0;
  3076. originCellReaderName = "";
  3077. return 1;
  3078. }else{
  3079. //2.组装数据
  3080. std::shared_ptr<ReceiveDataMap> pRdm = std::make_shared<ReceiveDataMap>();
  3081. pRdm->clear();
  3082. ret = AssembleDistData(pRdm);
  3083. if(ret){
  3084. return 1;
  3085. }else{
  3086. if (pRdm->size()<=0)
  3087. {
  3088. originCellId = 0;
  3089. originCellReaderName = "";
  3090. return 1;
  3091. }
  3092. std::shared_ptr<POS> pos = std::make_shared<POS>();
  3093. //3.算法定位
  3094. std::vector<std::shared_ptr<POS>> udm_pos;
  3095. udm_pos.resize(0);
  3096. ret = LocateAlgorithm::CalcTdoaPosition(pRdm,pTdoaReaderPathMap,udm_pos);
  3097. //4.从多解中筛选出一个解,存在两种可能:
  3098. //a.可能无解返回非0,
  3099. //b.可能有解,但解不正确,比如解的位置在4727,-100,但选出的解是4727,-200
  3100. ret = ChooseOneSolution(pRdm, udm_pos, pos);
  3101. SaveOriginDataBeforeFilter(pos);
  3102. if (ret)
  3103. {
  3104. //根据上一次数据计算格子数,并根据上一次方向进行偏移
  3105. originCellId = 0;
  3106. originCellReaderName = "";
  3107. return 1;
  3108. }else{
  3109. std::shared_ptr<POS> tmp_pos = std::make_shared<POS>();
  3110. //获取分站信息
  3111. bool bRet = false;
  3112. bRet = LocateAlgorithm::IsOnMap(pos,pTdoaReaderPathMap);
  3113. if (!bRet)
  3114. {
  3115. return 1;
  3116. }
  3117. *tmp_pos = *pos;
  3118. if (ret == 0)
  3119. {
  3120. //人卡数据处理20170705
  3121. //time_stamp_cal== 2635
  3122. //人卡格子为空,不去保存了!
  3123. //by lsp 2017.07.09 @长治
  3124. //Log::write_log(FILE_TYPE::JSON_S, "person before Position2Cell!!!!!!", true);
  3125. cell = Position2Cell(tmp_pos);
  3126. if (cell && cell->id != 0)
  3127. {
  3128. originCellId = cell->id;
  3129. originCellReaderName = cell->strSubjectReader;
  3130. cell->card_stamp_time = time_stamp_cal;
  3131. ::GetLocalTime(&cell->deal_time);
  3132. //保存格子到单元格,用于计算怠速判断
  3133. if (his_cell.size() > 100)
  3134. {
  3135. his_cell.pop_front();
  3136. }
  3137. his_cell.push_back(cell);//his_cell保存车辆怠速
  3138. if (nStartLocateCounts>=1&&his_cell.size() > 1)
  3139. {
  3140. //检查是否超限
  3141. isOverThreshold();//检查大跳
  3142. }
  3143. *tmp_cell = *cell;
  3144. nStartLocateCounts++;//统计原始定位次数
  3145. }else{
  3146. originCellId = 0;
  3147. originCellReaderName = "";
  3148. }
  3149. if (!CheckStartRule())//检查启动规则
  3150. {
  3151. return 1;
  3152. }
  3153. if (last_cell && cell)//cell是本次定位 last_cell 是下次定位
  3154. {
  3155. //20170704只判断怠速
  3156. int nCounts = 0;
  3157. if (cell->strSubjectReader == last_cell->strSubjectReader)
  3158. {
  3159. nCounts = ceil(cell->id - last_cell->id);//cell->id - last_cell->id两次格子的步进 ceil向上取整
  3160. }
  3161. else{
  3162. double distance = sqrt(pow(tmp_pos->cx - this->x,2) + pow(tmp_pos->cy - this->y,2)) * map_scale;
  3163. nCounts = distance / CELL_WIDTH;
  3164. }
  3165. //允许调整按ct号的最大格子数
  3166. int diff_ct = abs(cell->card_stamp_time - last_cell->card_stamp_time);
  3167. int thre_value ;
  3168. if(CT_VEHICLE == this->card_type )
  3169. {
  3170. thre_value = MAX_CELL_FORWARD_JUMP;//各自固定长度
  3171. }
  3172. else if(CT_PERSON == this->card_type )
  3173. {
  3174. thre_value = 3;//各自固定长度
  3175. }
  3176. if (diff_ct > 0)
  3177. {
  3178. if(CT_VEHICLE == this->card_type)
  3179. {
  3180. thre_value = MAX_CELL_FORWARD_JUMP*diff_ct;
  3181. }
  3182. else if(CT_PERSON == this->card_type )
  3183. {
  3184. thre_value = 3*diff_ct;
  3185. }
  3186. }
  3187. //怠速保留 0704实现
  3188. //越界,向前大跳或向后大跳
  3189. //有对应,越界()
  3190. if (nCounts >= thre_value)//步进大于阈值 //两次成功定位的数据处理好后竟然大于阈值 //向前/后大跳
  3191. {
  3192. isIdling = false;
  3193. if (this->nIncrease != 0)//方向 =0是没有方向
  3194. {
  3195. int nCalcStep = 0;
  3196. if (last_cell->nStep < 1)//上个格子距离上上个格子的步进
  3197. {
  3198. nCalcStep = 1;
  3199. }else{
  3200. nCalcStep = last_cell->nStep;//匀速 ----大跳按匀速处理
  3201. }
  3202. cell = GetNextCell(last_cell,this->nIncrease,nCalcStep);//本次的格子
  3203. if (cell)
  3204. {
  3205. cell->isFit = true;
  3206. }
  3207. }
  3208. }else if (!CheckIdleStatus())//检查是否怠速
  3209. {
  3210. isIdling = false;
  3211. //非怠速
  3212. }else{
  3213. //怠速
  3214. isIdling = true;
  3215. acce_cur_state = STATE_ACCE_IDLING;
  3216. //如果怠速了,则直接取上一次的结果
  3217. if (last_cell)
  3218. {
  3219. cell = last_cell;
  3220. }
  3221. }
  3222. }
  3223. }
  3224. }
  3225. }
  3226. }
  3227. if (cell)
  3228. {
  3229. //我要去保存人卡格子了!
  3230. //by lsp 2017.07.09 @长治
  3231. Log::write_log(FILE_TYPE::JSON_S, "I am going to save person cell!!!!!!", true);
  3232. SaveCardAlgoData(cell);
  3233. }else{
  3234. //人卡格子为空,不去保存了!
  3235. //by lsp 2017.07.09 @长治
  3236. Log::write_log(FILE_TYPE::JSON_S, "person cell is null!!!!!!", true);
  3237. }
  3238. return ret;
  3239. }
  3240. //
  3241. //int Card::DiscreteLocate(int cnt)
  3242. //{
  3243. // ULONGLONG nowTickCounts = ::GetTickCount();
  3244. // //如果last_cell存在,那说明不是第一次定位,如果不存在,那么继续往下走。
  3245. // if (last_cell)
  3246. // {
  3247. // SYSTEMTIME curSt;
  3248. // ::GetLocalTime(&curSt);
  3249. //
  3250. // int ret = 0;
  3251. // ret = CFunctions::CompareSystemTime(curSt,last_cell->interval_time);
  3252. // if (ret < 0)
  3253. // {
  3254. // return 1;
  3255. // }
  3256. // }
  3257. //
  3258. // int ret = 0;
  3259. // std::shared_ptr<Cell> cell = nullptr;
  3260. // std::shared_ptr<Cell> tmp_cell = std::make_shared<Cell>();
  3261. // is_fit_pos = false;
  3262. //
  3263. // //1.数据有效性判断
  3264. // ret = CheckDistData(cnt);
  3265. // if(ret){
  3266. // //time_stamp_cal== 14931 14878
  3267. // originCellId = 0;
  3268. // originCellReaderName = "";
  3269. // return 1;
  3270. // }else{
  3271. // //2.组装数据
  3272. // std::shared_ptr<ReceiveDataMap> pRdm = std::make_shared<ReceiveDataMap>();
  3273. // pRdm->clear();
  3274. // ret = AssembleDistData(pRdm);
  3275. // if(ret){
  3276. // return 1;
  3277. // }else{
  3278. // if (pRdm->size()<=0)
  3279. // {
  3280. // originCellId = 0;
  3281. // originCellReaderName = "";
  3282. // return 1;
  3283. // }
  3284. //
  3285. // //time_stamp_cal==12298
  3286. // std::shared_ptr<POS> pos = std::make_shared<POS>();
  3287. // //3.算法定位
  3288. // std::vector<std::shared_ptr<POS>> udm_pos;
  3289. // udm_pos.resize(0);
  3290. // ret = LocateAlgorithm::CalcTdoaPosition(pRdm,pTdoaReaderPathMap,udm_pos);
  3291. // //4.从多解中筛选出一个解,存在两种可能:
  3292. // //a.可能无解返回非0,
  3293. // //b.可能有解,但解不正确,比如解的位置在4727,-100,但选出的解是4727,-200
  3294. // ret = ChooseOneSolution(pRdm, udm_pos, pos);
  3295. //
  3296. // SaveOriginDataBeforeFilter(pos);
  3297. // if (ret)
  3298. // {
  3299. // //根据上一次数据计算格子数,并根据上一次方向进行偏移
  3300. // originCellId = 0;
  3301. // originCellReaderName = "";
  3302. // return 1;
  3303. // }else{
  3304. // std::shared_ptr<POS> tmp_pos = std::make_shared<POS>();
  3305. // //获取分站信息
  3306. // bool bRet = false;
  3307. // bRet = LocateAlgorithm::IsOnMap(pos,pTdoaReaderPathMap);
  3308. // if (!bRet)
  3309. // {
  3310. // return 1;
  3311. // }
  3312. // *tmp_pos = *pos;
  3313. //
  3314. // if (ret == 0)
  3315. // {
  3316. // //车辆数据处理20170705
  3317. // //time_stamp_cal== 5862
  3318. // cell = Position2Cell(tmp_pos);
  3319. // if (cell && cell->id == 0)
  3320. // {
  3321. // return 1; //cellId为0,直接走人
  3322. // }
  3323. // if (cell && cell->id != 0)
  3324. // {
  3325. // originCellId = cell->id;
  3326. // originCellReaderName = cell->strSubjectReader;
  3327. // cell->card_stamp_time = time_stamp_cal;
  3328. // //获取处理格子的系统时间
  3329. // ::GetLocalTime(&cell->deal_time);
  3330. // //保存卡的格子处理时间
  3331. // ::GetLocalTime(&cellDealTime);
  3332. //
  3333. // //保存格子到单元格,用于计算怠速判断
  3334. // if (his_cell.size() > 100)
  3335. // {
  3336. // his_cell.pop_front();
  3337. // }
  3338. // his_cell.push_back(cell);
  3339. //
  3340. // if (nStartLocateCounts>=1&&his_cell.size() > 1)
  3341. // {
  3342. // //检查是否超限
  3343. // isOverThreshold();
  3344. // }
  3345. //
  3346. // *tmp_cell = *cell;
  3347. // nStartLocateCounts++;
  3348. // }else{
  3349. // originCellId = 0;
  3350. // originCellReaderName = "";
  3351. // }
  3352. //
  3353. // if (!CheckStartRule())
  3354. // {
  3355. // return 1;
  3356. // }
  3357. //
  3358. // if (last_cell && cell)
  3359. // {
  3360. // //怠速
  3361. // if (CheckIdleStatus())
  3362. // {
  3363. // //怠速,取上次结果
  3364. // isIdling = true;
  3365. // acce_cur_state = STATE_ACCE_IDLING;
  3366. // //如果怠速了,则直接取上一次的结果
  3367. // if (last_cell)
  3368. // {
  3369. // cell = last_cell;
  3370. // }
  3371. // }
  3372. // else
  3373. // {
  3374. // if (last_cell->id == cell->id)
  3375. // {
  3376. // //如果本次定位的格子和上一次的格子id相同,则向前走一格
  3377. // int nCalcStep = 1;
  3378. // cell = GetNextCell(last_cell,this->nIncrease,nCalcStep);
  3379. // if (cell)
  3380. // {
  3381. // cell->isFit = true;
  3382. // }
  3383. // }else{
  3384. // //非怠速
  3385. // int nCounts = 0;
  3386. // if (cell->strSubjectReader == last_cell->strSubjectReader)
  3387. // {
  3388. // nCounts = ceil(abs(cell->id - last_cell->id));
  3389. // }
  3390. // else{
  3391. // double distance = sqrt(pow(tmp_pos->cx - this->x,2) + pow(tmp_pos->cy - this->y,2)) * map_scale;
  3392. // nCounts = distance / CELL_WIDTH;
  3393. // }
  3394. // //允许调整按ct号的最大格子数
  3395. // int diff_ct = abs(cell->card_stamp_time - last_cell->card_stamp_time);
  3396. // int thre_value = MAX_CELL_FORWARD_JUMP;
  3397. // if (diff_ct > 0)
  3398. // {
  3399. // thre_value = MAX_CELL_FORWARD_JUMP*diff_ct;
  3400. // }
  3401. // //1.大跳,只考虑往前跳
  3402. // if (nCounts >= thre_value)
  3403. // {
  3404. // if (nOverThreCounts < 3)
  3405. // {
  3406. // nOverThreCounts++;
  3407. // isIdling = false;
  3408. // if (this->nIncrease != 0)
  3409. // {
  3410. // int nCalcStep = 0;
  3411. // if (last_cell->nStep < 1)
  3412. // {
  3413. // nCalcStep = 1;
  3414. // }else{
  3415. // nCalcStep = last_cell->nStep;
  3416. // }
  3417. // cell = GetNextCell(last_cell,this->nIncrease,nCalcStep);
  3418. // if (cell)
  3419. // {
  3420. // cell->isFit = true;
  3421. // }
  3422. // }
  3423. // }
  3424. // }
  3425. // else if (isRebound(cell))
  3426. // {
  3427. // //2.回退,5个周期内的都不认
  3428. // //如果5次以内回退
  3429. // if (change_cell_dir < MAX_CHANGE_DIR_COUNTS)
  3430. // {
  3431. // int nCounts = 0;
  3432. // if (last_cell)
  3433. // {
  3434. // //在不知下一时刻如何走的情况下,不做减速,
  3435. // nCounts = last_cell->nStep;
  3436. // //nCounts = ceil(last_cell->nStep/2); //20170703 16:46 做取半减速
  3437. // }
  3438. // if (abs(nCounts) < 1E-2)
  3439. // {
  3440. // nCounts = 1;
  3441. // }
  3442. // if (this->nIncrease != 0)
  3443. // {
  3444. // cell = GetNextCell(last_cell,this->nIncrease,nCounts);
  3445. // if (cell)
  3446. // {
  3447. // cell->isFit = true;
  3448. // }
  3449. // }
  3450. // change_cell_dir++;
  3451. // }else{
  3452. // change_cell_dir = 0;
  3453. // }
  3454. // }else{
  3455. // //3.正常运动
  3456. // }
  3457. // }
  3458. // }
  3459. // //非怠速
  3460. // //先处理回退
  3461. // /*if (isRebound(tmp_pos))
  3462. // {
  3463. // if (this->nIncrease != 0)
  3464. // {
  3465. // int nCalcStep = 0;
  3466. // nCalcStep = ceil(last_cell->nStep / 2);
  3467. // if (nCalcStep < 1)
  3468. // {
  3469. // nCalcStep = 1;
  3470. // }
  3471. // cell = GetNextCell(last_cell,this->nIncrease,nCalcStep);
  3472. // if (cell)
  3473. // {
  3474. // cell->isFit = true;
  3475. // }
  3476. // }
  3477. // }*/
  3478. //
  3479. // ////只判断怠速
  3480. // //int nCounts = 0;
  3481. // //if (cell->strSubjectReader == last_cell->strSubjectReader)
  3482. // //{
  3483. // // nCounts = ceil(abs(cell->id - last_cell->id));
  3484. // //}
  3485. // //else{
  3486. // // double distance = sqrt(pow(tmp_pos->cx - this->x,2) + pow(tmp_pos->cy - this->y,2)) * map_scale;
  3487. // // nCounts = distance / CELL_WIDTH;
  3488. // //}
  3489. // ////允许调整按ct号的最大格子数
  3490. // //int diff_ct = abs(cell->card_stamp_time - last_cell->card_stamp_time);
  3491. // //int thre_value = MAX_CELL_FORWARD_JUMP;
  3492. // //if (diff_ct > 0)
  3493. // //{
  3494. // // thre_value = MAX_CELL_FORWARD_JUMP*diff_ct;
  3495. // //}
  3496. //
  3497. // ////怠速保留 0704实现
  3498. // ////越界,向前大跳或向后大跳
  3499. // //if (nCounts >= thre_value)
  3500. // //{
  3501. // // isIdling = false;
  3502. // // if (this->nIncrease != 0)
  3503. // // {
  3504. // // int nCalcStep = 0;
  3505. // // if (last_cell->nStep < 1)
  3506. // // {
  3507. // // nCalcStep = 1;
  3508. // // }else{
  3509. // // nCalcStep = last_cell->nStep;
  3510. // // }
  3511. // // cell = GetNextCell(last_cell,this->nIncrease,nCalcStep);
  3512. // // if (cell)
  3513. // // {
  3514. // // cell->isFit = true;
  3515. // // }
  3516. // // }
  3517. // //}else if (!CheckIdleStatus())
  3518. // //{
  3519. // // isIdling = false;
  3520. // // //非怠速
  3521. // //}else{
  3522. // // isIdling = true;
  3523. // // acce_cur_state = STATE_ACCE_IDLING;
  3524. // // //如果怠速了,则直接取上一次的结果
  3525. // // if (last_cell)
  3526. // // {
  3527. // // cell = last_cell;
  3528. // // }
  3529. // //}
  3530. // }
  3531. // }
  3532. // }
  3533. // }
  3534. // }
  3535. //
  3536. // if (cell)
  3537. // {
  3538. // SaveCardAlgoData(cell);
  3539. // }
  3540. //
  3541. // return ret;
  3542. //}
  3543. //拟合度实现方案
  3544. //int Card::DiscreteLocate(int cnt)
  3545. //{
  3546. // int ret = 0;
  3547. // std::shared_ptr<Cell> cell = nullptr;
  3548. // std::shared_ptr<Cell> tmp_cell = std::make_shared<Cell>();
  3549. // is_fit_pos = false;
  3550. // std::shared_ptr<POS> pos = nullptr;
  3551. //
  3552. // //1.数据有效性判断
  3553. // ret = CheckDistData(cnt);
  3554. // if(ret){
  3555. // return 1;
  3556. // }else{
  3557. // //2.组装数据
  3558. // std::shared_ptr<ReceiveDataMap> pRdm = std::make_shared<ReceiveDataMap>();
  3559. // pRdm->clear();
  3560. // ret = AssembleDistData(pRdm);
  3561. // if(ret){
  3562. // return 1;
  3563. // }else{
  3564. // if (pRdm->size() <= 0)
  3565. // {
  3566. // return 1;
  3567. // }
  3568. //
  3569. // pos = std::make_shared<POS>();
  3570. // //3.算法定位
  3571. // std::vector<std::shared_ptr<POS>> udm_pos;
  3572. // udm_pos.resize(0);
  3573. // ret = LocateAlgorithm::CalcTdoaPosition(pRdm,pTdoaReaderPathMap,udm_pos);
  3574. // //4.从多解中筛选出一个解,存在两种可能:
  3575. // //a.可能无解返回非0,
  3576. // //b.可能有解,但解不正确,比如解的位置在4727,-100,但选出的解是4727,-200
  3577. // ret = ChooseOneSolution(pRdm, udm_pos, pos);
  3578. // SaveOriginDataBeforeFilter(pos);
  3579. // if (ret)
  3580. // {
  3581. // return 1;
  3582. // }
  3583. //
  3584. // bool bRet = false;
  3585. // bRet = LocateAlgorithm::IsOnMap(pos,pTdoaReaderPathMap);
  3586. // if (!bRet)
  3587. // {
  3588. // return 1;
  3589. // }
  3590. // if (card_id == "0020000001023" && time_stamp_cal == 12885)
  3591. // {
  3592. // int aa = 1;
  3593. // aa = his_cell.size();
  3594. // }
  3595. // if (ret)
  3596. // {
  3597. // if (his_cell.size() < 5)
  3598. // {
  3599. // return 1;
  3600. // }
  3601. // //如果从多解中未选出解,则按上一次格子的步进匀速运动
  3602. // int nStep = 1;
  3603. // if (last_cell->nStep > 1)
  3604. // {
  3605. // nStep = last_cell->nStep;
  3606. // }
  3607. // std::shared_ptr<Cell> cell = GetNextCell(last_cell,nIncrease,nStep,nullptr);
  3608. // if (cell)
  3609. // {
  3610. // cell->originId = cell->id;
  3611. // }
  3612. // }else{
  3613. // std::shared_ptr<POS> tmp_pos = std::make_shared<POS>();
  3614. // //获取分站信息
  3615. // bool bRet = false;
  3616. // *tmp_pos = *pos;
  3617. //
  3618. // if (ret == 0)
  3619. // {
  3620. // //将坐标转为格子信息
  3621. // cell = Position2Cell(tmp_pos);
  3622. // if (cell)
  3623. // {
  3624. // originCellId = cell->id;
  3625. // originCellReaderName = cell->strSubjectReader;
  3626. // cell->card_stamp_time = time_stamp_cal;
  3627. // cell->originId = cell->id;
  3628. // cell->strOriginReaderName = cell->strSubjectReader;
  3629. // nStartLocateCounts++;
  3630. //
  3631. // if (lvCells.size() >= MAX_FIT_DATA_COUNTS)
  3632. // {
  3633. // lvCells.pop_front();
  3634. // }
  3635. // lvCells.push_back(cell);
  3636. //
  3637. // if (!is_algo_first_location)
  3638. // {
  3639. // double degree = 0;
  3640. // degree = GetFittingDegree(lvCells);
  3641. //
  3642. // bool bOverThre = false;
  3643. // bool isDirectOutput = false; //是否直接输出数据
  3644. //
  3645. // if (degree - 0.9 > ZERO_PRECISION)
  3646. // {
  3647. // bOverThre = false;
  3648. // isDirectOutput = true;
  3649. // pos->update = true;
  3650. // } else if ((degree - 0.7) > ZERO_PRECISION && (0.9 - degree) > ZERO_PRECISION)
  3651. // {
  3652. // //向前大跳
  3653. // bOverThre = true;
  3654. // lvCells.pop_back();
  3655. // }else if ((degree - 0.5) > ZERO_PRECISION &&(0.7 - degree) > ZERO_PRECISION)
  3656. // {
  3657. // bOverThre = false;
  3658. // }
  3659. // else if(0.5 - degree > ZERO_PRECISION)
  3660. // {
  3661. // //向后大跳
  3662. // bOverThre = true;
  3663. // lvCells.pop_back();
  3664. // }
  3665. //
  3666. // if (!isDirectOutput)
  3667. // {
  3668. // if (bOverThre)
  3669. // {
  3670. // //根据之前的拟合方程k,b进行补格子
  3671. // int tmpOriginId = 0;
  3672. // std::string tmpOriginReaderName = "";
  3673. // tmpOriginId = cell->originId;
  3674. // tmpOriginReaderName = cell->strOriginReaderName;
  3675. //
  3676. // //根据上一次数据计算格子数,并根据上一次方向进行偏移
  3677. // double y = 0;
  3678. // std::list<std::shared_ptr<Cell>>::iterator it = lvCells.begin();
  3679. //
  3680. // y = fit_k*(this->time_stamp_cal - (*it)->card_stamp_time + 1) + fit_b;
  3681. //
  3682. // int nCellId = 0 ;
  3683. // std::string strReaderName = "";
  3684. // //判断两次格子的id是否在相同分站内,不在走跨分站逻辑
  3685. // if ((*it)->strOriginReaderName != cell->strOriginReaderName)
  3686. // {
  3687. // int nRealId = 0;
  3688. // nCellId = LocateAlgorithm::round(y);
  3689. // //换基站了
  3690. // int first_min = (*it)->minReaderId; //first_cell_temp对应的最小分站
  3691. // int first_max = (*it)->maxReaderId; //first_cell_temp对应的最大分站
  3692. // int cell_min = cell->minReaderId; //cell对应的最小分站
  3693. // int cell_max = cell->maxReaderId; //cell对应的最大分站
  3694. //
  3695. // if(first_min == cell_min){
  3696. // if (nCellId == 0)
  3697. // {
  3698. // nRealId = 1;
  3699. // strReaderName = cell->strSubjectReader;
  3700. // }else if(nCellId > 0){
  3701. // nRealId = nCellId;
  3702. // strReaderName = (*it)->strSubjectReader;
  3703. // }else if(nCellId < 0){
  3704. // nRealId = abs(nCellId);
  3705. // strReaderName = cell->strSubjectReader;
  3706. // }
  3707. // }
  3708. // if(first_min == cell_max){
  3709. // if (nCellId == 0)
  3710. // {
  3711. // nRealId = mpCellPath->find(cell->strSubjectReader)->second.size();
  3712. // strReaderName = cell->strSubjectReader;
  3713. // }else if(nCellId > 0){
  3714. // nRealId = nCellId;
  3715. // strReaderName = (*it)->strSubjectReader;
  3716. // }else if(nCellId < 0){
  3717. // nRealId = mpCellPath->find(cell->strSubjectReader)->second.size() - abs(nCellId);
  3718. // strReaderName = cell->strSubjectReader;
  3719. // }
  3720. // }
  3721. // if(first_max == cell_min){
  3722. // //只存在nCellId>0的情况,但还要区分它是否大于所在分站的总格子数
  3723. // if (nCellId >= mpCellPath->find((*it)->strSubjectReader)->second.size())
  3724. // {
  3725. // nRealId = nCellId - mpCellPath->find(cell->strSubjectReader)->second.size();
  3726. // if (nRealId == 0)
  3727. // {
  3728. // nRealId = 1;
  3729. // }
  3730. // strReaderName = cell->strSubjectReader;
  3731. // }else if(nCellId < mpCellPath->find((*it)->strSubjectReader)->second.size()){
  3732. // nRealId = nCellId;
  3733. // strReaderName = (*it)->strSubjectReader;
  3734. // }
  3735. // }
  3736. // if(first_max == cell_max){
  3737. // if (nCellId >= mpCellPath->find((*it)->strSubjectReader)->second.size())
  3738. // {
  3739. // nRealId = mpCellPath->find(cell->strOriginReaderName)->second.size() - (nCellId - mpCellPath->find((*it)->strSubjectReader)->second.size());
  3740. // strReaderName = cell->strOriginReaderName;
  3741. // }else if(nCellId < mpCellPath->find((*it)->strSubjectReader)->second.size()){
  3742. // nRealId = nCellId;
  3743. // strReaderName = (*it)->strSubjectReader;
  3744. // }
  3745. // }
  3746. // nCellId = nRealId;
  3747. // }else{
  3748. // nCellId = LocateAlgorithm::round(y);
  3749. // strReaderName = cell->strOriginReaderName;
  3750. // }
  3751. // //将新格子的信息除了格子id信息
  3752. // cell = FindCell(strReaderName,nCellId);
  3753. // if (cell)
  3754. // {
  3755. // cell->originId = tmpOriginId;
  3756. // cell->strOriginReaderName = tmpOriginReaderName;
  3757. // }
  3758. // }
  3759. // else
  3760. // {
  3761. // colourfulCloudsChasingTheMoon(cell,his_cell);
  3762. // }
  3763. // }else{
  3764. // //直接输出数据需要放入拟合队列中
  3765. // colourfulCloudsChasingTheMoon(cell,his_cell);
  3766. // }
  3767. // }else{
  3768. // //如果是第一次定位结果,调用以下函数将cell放入到his_cell队列中
  3769. // if (his_cell.size() > 0)
  3770. // {
  3771. // his_cell.erase(his_cell.begin(),his_cell.end());
  3772. // fit_k = fit_b = 0;
  3773. // }
  3774. // colourfulCloudsChasingTheMoon(cell,his_cell);
  3775. // }
  3776. // }else{
  3777. // originCellId = 0;
  3778. // originCellReaderName = "";
  3779. // }
  3780. // }
  3781. // }
  3782. // }
  3783. // }
  3784. //
  3785. // if (cell)
  3786. // {
  3787. // //做加速度处理
  3788. // double dAcce = 0;
  3789. // bool bRet = false;
  3790. // if (abs(fit_k - last_k) < ZERO_PRECISION)
  3791. // {
  3792. // bRet = true;
  3793. // }else{
  3794. // dAcce = abs((fit_k - last_k)/(delta_ct))*1.2;
  3795. // if (dAcce > 10)
  3796. // {
  3797. // bRet = false;
  3798. // }else{
  3799. // bRet = true;
  3800. // }
  3801. // }
  3802. //
  3803. // if (bRet)
  3804. // {
  3805. // SaveCardAlgoData(cell);
  3806. // }
  3807. // //SaveCardAlgoData(cell);
  3808. // }
  3809. //
  3810. // return ret;
  3811. //}
  3812. //只有通过加速度判断丢失数据部分
  3813. int Card::DiscreteLocate(int cnt)
  3814. {
  3815. int ret = 0;
  3816. std::shared_ptr<Cell> cell = nullptr;
  3817. std::shared_ptr<Cell> tmp_cell = std::make_shared<Cell>();
  3818. is_fit_pos = false;
  3819. std::shared_ptr<POS> pos = nullptr;
  3820. //1.数据有效性判断
  3821. ret = CheckDistData(cnt);
  3822. if(ret){
  3823. return 1;
  3824. }else{
  3825. //2.组装数据
  3826. std::shared_ptr<ReceiveDataMap> pRdm = std::make_shared<ReceiveDataMap>();
  3827. pRdm->clear();
  3828. ret = AssembleDistData(pRdm);
  3829. if(ret){
  3830. return 1;
  3831. }else{
  3832. if (pRdm->size() <= 0)
  3833. {
  3834. return 1;
  3835. }
  3836. pos = std::make_shared<POS>();
  3837. //3.算法定位
  3838. std::vector<std::shared_ptr<POS>> udm_pos;
  3839. udm_pos.resize(0);
  3840. ret = LocateAlgorithm::CalcTdoaPosition(pRdm,pTdoaReaderPathMap,udm_pos);
  3841. //4.从多解中筛选出一个解,存在两种可能:
  3842. //a.可能无解返回非0,
  3843. //b.可能有解,但解不正确,比如解的位置在4727,-100,但选出的解是4727,-200
  3844. ret = ChooseOneSolution(pRdm, udm_pos, pos);
  3845. SaveOriginDataBeforeFilter(pos);
  3846. if (ret)
  3847. {
  3848. return 1;
  3849. }
  3850. bool bRet = false;
  3851. bRet = LocateAlgorithm::IsOnMap(pos,pTdoaReaderPathMap);
  3852. if (!bRet)
  3853. {
  3854. return 1;
  3855. }
  3856. if (card_id == "0020000001061" && time_stamp_cal == 11049)
  3857. {
  3858. int aa = 1;
  3859. aa = his_cell.size();
  3860. }
  3861. if (ret)
  3862. {
  3863. if (his_cell.size() < 5)
  3864. {
  3865. return 1;
  3866. }
  3867. //如果从多解中未选出解,则按上一次格子的步进匀速运动
  3868. int nStep = 1;
  3869. if (last_cell->nStep > 1)
  3870. {
  3871. nStep = last_cell->nStep;
  3872. }
  3873. std::shared_ptr<Cell> cell = GetNextCell(last_cell,nIncrease,nStep,nullptr);
  3874. if (cell)
  3875. {
  3876. cell->originId = cell->id;
  3877. }
  3878. }else{
  3879. std::shared_ptr<POS> tmp_pos = std::make_shared<POS>();
  3880. //获取分站信息
  3881. bool bRet = false;
  3882. *tmp_pos = *pos;
  3883. if (ret == 0)
  3884. {
  3885. //将坐标转为格子信息
  3886. cell = Position2Cell(tmp_pos);
  3887. if (cell)
  3888. {
  3889. originCellId = cell->id;
  3890. originCellReaderName = cell->strSubjectReader;
  3891. cell->card_stamp_time = time_stamp_cal;
  3892. cell->originId = cell->id;
  3893. cell->strOriginReaderName = cell->strSubjectReader;
  3894. nStartLocateCounts++;
  3895. if (lvCells.size() >= MAX_FIT_DATA_COUNTS)
  3896. {
  3897. lvCells.pop_front();
  3898. }
  3899. lvCells.push_back(cell);
  3900. colourfulCloudsChasingTheMoon(cell,his_cell);
  3901. }else{
  3902. originCellId = 0;
  3903. originCellReaderName = "";
  3904. }
  3905. }
  3906. }
  3907. }
  3908. }
  3909. if (cell)
  3910. {
  3911. //做加速度处理
  3912. double dAcce = 0;
  3913. bool bRet = false;
  3914. if (abs(fit_k - last_k) < ZERO_PRECISION)
  3915. {
  3916. bRet = true;
  3917. }else{
  3918. dAcce = abs((fit_k - last_k)/(delta_ct))*1.2;
  3919. if (dAcce > 10)
  3920. {
  3921. bRet = false;
  3922. if(his_cell.size() > 0){
  3923. his_cell.pop_back();
  3924. }
  3925. if (lvCells.size())
  3926. {
  3927. lvCells.pop_back();
  3928. }
  3929. return 1;
  3930. }else{
  3931. bRet = true;
  3932. }
  3933. }
  3934. if (bRet)
  3935. {
  3936. SaveCardAlgoData(cell);
  3937. }
  3938. //SaveCardAlgoData(cell);
  3939. }
  3940. return ret;
  3941. }
  3942. std::shared_ptr<Cell> Card::GetNextCell(std::shared_ptr<Cell> lastCell,int increase,int nIndex,std::shared_ptr<Cell> newCell)
  3943. {
  3944. if (lastCell->id == 0)
  3945. {
  3946. return nullptr;
  3947. }
  3948. std::shared_ptr<Cell> cell = std::make_shared<Cell>();
  3949. int nFindId = 0;
  3950. bool bFind = false;
  3951. std::string findReaderName = "";
  3952. std::mutex mu;
  3953. std::lock_guard<mutex> lg(mu);
  3954. if (last_cell && newCell == nullptr)
  3955. {
  3956. //处理500ms定时器的处理
  3957. int nNewCellId = 0;
  3958. findReaderName = curCellReaderName;
  3959. if (findReaderName == "")
  3960. {
  3961. return nullptr;
  3962. }
  3963. CellPathMap::iterator it_cpm = mpCellPath->end();
  3964. switch (increase)
  3965. {
  3966. case 1:
  3967. nNewCellId = lastCell->id + nIndex;
  3968. it_cpm = mpCellPath->find(lastCell->strSubjectReader);
  3969. if (it_cpm == mpCellPath->end())
  3970. {
  3971. return nullptr;
  3972. }
  3973. //if (nNewCellId > mpCellPath->find(lastCell->strSubjectReader)->second.size())
  3974. if (nNewCellId > it_cpm->second.size())
  3975. {
  3976. nFindId = mpCellPath->find(lastCell->strSubjectReader)->second.size();
  3977. cell->isChangePath = false;
  3978. }else{
  3979. nFindId = nNewCellId;
  3980. }
  3981. break;
  3982. case -1:
  3983. nNewCellId = lastCell->id - nIndex;
  3984. if (nNewCellId < 0)
  3985. {
  3986. nFindId = 1;
  3987. cell->isChangePath = false;
  3988. }else{
  3989. nFindId = nNewCellId;
  3990. }
  3991. break;
  3992. default:
  3993. break;
  3994. }
  3995. if (findReaderName == "")
  3996. {
  3997. return nullptr;
  3998. }
  3999. for (std::list<std::shared_ptr<Cell>>::iterator it = mpCellPath->find(findReaderName)->second.begin();it != mpCellPath->find(findReaderName)->second.end();++it)
  4000. {
  4001. if ((*it)->id == nFindId)
  4002. {
  4003. cell->id = (*it)->id;
  4004. cell->top = (*it)->top;
  4005. cell->left = (*it)->left;
  4006. cell->bottom = (*it)->bottom;
  4007. cell->right = (*it)->right;
  4008. cell->isHasBranch = (*it)->isHasBranch;
  4009. cell->maxReaderId = (*it)->maxReaderId;
  4010. cell->minReaderId = (*it)->minReaderId;
  4011. break;
  4012. }
  4013. }
  4014. }else if (last_cell && newCell)
  4015. {
  4016. //处理两种情况,一种是分站内的格子移动,另一种是跨分站格子的移动
  4017. if (last_cell->strSubjectReader == newCell->strSubjectReader)
  4018. {
  4019. cell->isChangePath = false;
  4020. findReaderName = lastCell->strSubjectReader;
  4021. cell->strSubjectReader = findReaderName;
  4022. //分站内的格子移动
  4023. switch (increase)
  4024. {
  4025. case 1:
  4026. if (lastCell->id + nIndex > mpCellPath->find(lastCell->strSubjectReader)->second.size())
  4027. {
  4028. //存在跨分站的问题,先找到路径中的最大分站
  4029. //根据路径集中找到最大分站的列表,
  4030. nFindId = mpCellPath->find(lastCell->strSubjectReader)->second.size();
  4031. }else{
  4032. nFindId = lastCell->id + nIndex;
  4033. }
  4034. break;
  4035. case -1:
  4036. if (lastCell->id - nIndex < 0)
  4037. {
  4038. //nFindId = 1;
  4039. //存在跨分站的问题
  4040. //先找到路径中的最小分站
  4041. //根据路径集中找到最小分站的列表,
  4042. nFindId = 1;
  4043. }else{
  4044. nFindId = lastCell->id - nIndex;
  4045. }
  4046. break;
  4047. default:
  4048. *cell = *newCell;
  4049. break;
  4050. }
  4051. }else{
  4052. //另一种是跨分站格子的移动
  4053. cell->isChangePath = true;
  4054. findReaderName = newCell->strSubjectReader;
  4055. if (findReaderName != "")
  4056. {
  4057. cell->strSubjectReader = findReaderName;
  4058. }
  4059. int oldCellOffset = 0;
  4060. int newCellOffset = 0;
  4061. int CellOffset = 0;
  4062. if (lastCell->maxReaderId == newCell->maxReaderId)
  4063. {
  4064. oldCellOffset = mpCellPath->find(lastCell->strSubjectReader)->second.size() - lastCell->id;
  4065. newCellOffset = mpCellPath->find(newCell->strSubjectReader)->second.size() - newCell->id;
  4066. CellOffset = ceil(sqrt(pow(oldCellOffset,2)+pow(newCellOffset,2)));
  4067. nFindId = mpCellPath->find(newCell->strSubjectReader)->second.size() - abs(CellOffset - oldCellOffset);
  4068. if (nFindId == 0)
  4069. {
  4070. nFindId = mpCellPath->find(newCell->strSubjectReader)->second.size();
  4071. }
  4072. nIncrease = -1;
  4073. }else if (lastCell->maxReaderId == newCell->minReaderId)
  4074. {
  4075. oldCellOffset = mpCellPath->find(lastCell->strSubjectReader)->second.size() - lastCell->id;
  4076. newCellOffset = newCell->id;
  4077. CellOffset = ceil(sqrt(pow(oldCellOffset,2) + pow(newCellOffset,2)));
  4078. nFindId = abs(CellOffset - oldCellOffset);
  4079. if (nFindId == 0)
  4080. {
  4081. nFindId = 1;
  4082. }
  4083. nIncrease = 1;
  4084. }else if (lastCell->minReaderId == newCell->maxReaderId)
  4085. {
  4086. oldCellOffset = lastCell->id;
  4087. newCellOffset = mpCellPath->find(newCell->strSubjectReader)->second.size() - newCell->id;
  4088. CellOffset = ceil(sqrt(pow(oldCellOffset,2)+pow(newCellOffset,2)));
  4089. nFindId = mpCellPath->find(newCell->strSubjectReader)->second.size() - abs(CellOffset - oldCellOffset);
  4090. if (nFindId == 0)
  4091. {
  4092. nFindId = mpCellPath->find(newCell->strSubjectReader)->second.size();
  4093. }
  4094. nIncrease = -1;
  4095. }else if (lastCell->minReaderId == newCell->minReaderId)
  4096. {
  4097. oldCellOffset = lastCell->id;
  4098. newCellOffset = newCell->id;
  4099. CellOffset = ceil(sqrt(pow(oldCellOffset,2)+pow(newCellOffset,2)));
  4100. nFindId = abs(CellOffset - oldCellOffset);
  4101. if (nFindId == 0)
  4102. {
  4103. nFindId = 1;
  4104. }
  4105. nIncrease = 1;
  4106. }else{
  4107. findReaderName = "";
  4108. }
  4109. }
  4110. if (findReaderName == "")
  4111. {
  4112. return nullptr;
  4113. }
  4114. for (std::list<std::shared_ptr<Cell>>::iterator it = mpCellPath->find(findReaderName)->second.begin();it != mpCellPath->find(findReaderName)->second.end();++it)
  4115. {
  4116. if ((*it)->id == nFindId)
  4117. {
  4118. cell->id = (*it)->id;
  4119. cell->top = (*it)->top;
  4120. cell->left = (*it)->left;
  4121. cell->bottom = (*it)->bottom;
  4122. cell->right = (*it)->right;
  4123. cell->isHasBranch = (*it)->isHasBranch;
  4124. cell->maxReaderId = (*it)->maxReaderId;
  4125. cell->minReaderId = (*it)->minReaderId;
  4126. break;
  4127. }
  4128. }
  4129. }
  4130. return cell;
  4131. }
  4132. std::shared_ptr<Cell> Card::GetNextCell(std::shared_ptr<Cell> lastCell,int increase,int nIndex)
  4133. {
  4134. if (lastCell == nullptr || lastCell->id == 0)
  4135. {
  4136. return nullptr;
  4137. }
  4138. std::shared_ptr<Cell> cell = std::make_shared<Cell>();
  4139. int step = 0 , nFindId = 0;
  4140. step = lastCell->id + nIndex*increase;
  4141. std::string strFindReader = "";
  4142. strFindReader = lastCell->strSubjectReader;
  4143. if (strFindReader == "")
  4144. {
  4145. return last_cell;
  4146. }
  4147. int nSize = 0;
  4148. nSize = mpCellPath->find(strFindReader)->second.size();
  4149. if (step > nSize)
  4150. {
  4151. nFindId = nSize;
  4152. }else if (step < 0)
  4153. {
  4154. nFindId = 1;
  4155. }else{
  4156. nFindId = step;
  4157. }
  4158. bool bFind = false;
  4159. for (std::list<std::shared_ptr<Cell>>::iterator it = mpCellPath->find(strFindReader)->second.begin();it != mpCellPath->find(strFindReader)->second.end();++it)
  4160. {
  4161. if ((*it)->id == nFindId)
  4162. {
  4163. cell->id = (*it)->id;
  4164. cell->top = (*it)->top;
  4165. cell->left = (*it)->left;
  4166. cell->bottom = (*it)->bottom;
  4167. cell->right = (*it)->right;
  4168. cell->isHasBranch = (*it)->isHasBranch;
  4169. cell->maxReaderId = (*it)->maxReaderId;
  4170. cell->minReaderId = (*it)->minReaderId;
  4171. cell->interval_time = lastCell->interval_time;
  4172. bFind = true;
  4173. break;
  4174. }
  4175. }
  4176. if (!bFind)
  4177. {
  4178. return nullptr;
  4179. }
  4180. return cell;
  4181. }
  4182. //根据分站名称和格子id查找出格子的其他所有信息
  4183. std::shared_ptr<Cell> Card::FindCell(std::string strReaderName,int cellid)
  4184. {
  4185. std::shared_ptr<Cell> cell = std::make_shared<Cell>();
  4186. if (strReaderName == "" || cellid == 0)
  4187. {
  4188. return nullptr;
  4189. }
  4190. for (std::list<std::shared_ptr<Cell>>::iterator it = mpCellPath->find(strReaderName)->second.begin();it != mpCellPath->find(strReaderName)->second.end();++it)
  4191. {
  4192. if ((*it)->id == cellid)
  4193. {
  4194. //cell = (*it);
  4195. cell->id = (*it)->id;
  4196. cell->top = (*it)->top;
  4197. cell->left = (*it)->left;
  4198. cell->bottom = (*it)->bottom;
  4199. cell->right = (*it)->right;
  4200. cell->strSubjectReader = (*it)->strSubjectReader;
  4201. cell->isHasBranch = (*it)->isHasBranch;
  4202. cell->maxReaderId = (*it)->maxReaderId;
  4203. cell->minReaderId = (*it)->minReaderId;
  4204. cell->realId = cellid;
  4205. break;
  4206. }
  4207. }
  4208. return cell;
  4209. }
  4210. /*
  4211. * 如果怠速返回true,否则返回false
  4212. */
  4213. bool Card::CheckIdleStatus()
  4214. {
  4215. std::mutex mu;
  4216. std::lock_guard<mutex> lg(mu);
  4217. if (his_cell.size() < 5)
  4218. {
  4219. return false;
  4220. }
  4221. int flag = 0;
  4222. int i = 0;
  4223. std::list<std::shared_ptr<Cell>>::reverse_iterator ref = his_cell.rbegin();
  4224. for (std::list<std::shared_ptr<Cell>>::reverse_iterator ref_it = his_cell.rbegin();ref_it != his_cell.rend();++ref_it)
  4225. {
  4226. if ((*ref)->id != (*ref_it)->id)
  4227. {
  4228. flag = 1;
  4229. break;
  4230. }
  4231. i++;
  4232. if (i == 4)
  4233. {
  4234. break;
  4235. }
  4236. }
  4237. if (flag == 0)
  4238. {
  4239. return true;
  4240. }
  4241. //拟合法
  4242. std::vector<int> vt,vc;
  4243. vt.resize(0);
  4244. vc.resize(0);
  4245. //反向取MAX_FIT_DATA_COUNTS个元素
  4246. double val[MAX_FIT_DATA_COUNTS] = {0};
  4247. i = MAX_FIT_DATA_COUNTS - 1;
  4248. for (std::list<std::shared_ptr<Cell>>::reverse_iterator it = his_cell.rbegin();it != his_cell.rend();++it)
  4249. {
  4250. if (!(*it)->isOverThre)//大跳数据不参与怠速判断
  4251. {
  4252. //如果非越界,则参与计算怠速判断,
  4253. val[i] = (*it)->id;
  4254. i--;
  4255. }
  4256. if (i < 0)
  4257. {
  4258. break;
  4259. }
  4260. }
  4261. int nAcs = 0;
  4262. int nDesc = 0;
  4263. //统计方向
  4264. for (int k = 0; k < MAX_FIT_DATA_COUNTS - 1;k++)
  4265. {
  4266. if (val[k] < val[k+1])
  4267. {
  4268. nAcs++;
  4269. }else{
  4270. nDesc++;
  4271. }
  4272. }
  4273. if (nAcs > nDesc)
  4274. {
  4275. nIncrease = 1;
  4276. }else if (nAcs < nDesc)
  4277. {
  4278. nIncrease = -1;
  4279. }else
  4280. {
  4281. //相等的话就保持原来的方向
  4282. }
  4283. i = 0;
  4284. for (; i < MAX_FIT_DATA_COUNTS;i++)
  4285. {
  4286. vt.push_back(i+1);
  4287. vc.push_back(val[i]);
  4288. }
  4289. Fit fit;
  4290. fit.linearFit(vt,vc);
  4291. if (fit.getR() < FIT_RELIABILITY)
  4292. {
  4293. acce_cur_state = ACCELERATE_STATE::STATE_ACCE_IDLING;
  4294. return true;
  4295. }
  4296. return false;
  4297. }
  4298. /*
  4299. * 功能:确定启动规则,如果怠速,则不能运动,否则开始运动
  4300. *
  4301. * param
  4302. * 无
  4303. *
  4304. * return
  4305. * 怠速 返回false
  4306. * 运动 返回true
  4307. */
  4308. bool Card::CheckStartRule()
  4309. {
  4310. //前5次不运动
  4311. if (nStartLocateCounts < 5)
  4312. {
  4313. return false;
  4314. }
  4315. //如果第六次开始还是怠速状态,则不运动
  4316. if (CheckIdleStatus())
  4317. {
  4318. return false;
  4319. }
  4320. return true;
  4321. }
  4322. /*
  4323. * 作用:检查是否大跳
  4324. *
  4325. * param
  4326. * cell 当前定位格子信息
  4327. *
  4328. * return
  4329. * 如果存在大跳,则返回true;否则,返回false
  4330. *
  4331. */
  4332. bool Card::isOverThreshold()
  4333. {
  4334. int cnt = 0;
  4335. /*cnt = abs(cell->card_stamp_time - last_cell->card_stamp_time);
  4336. int diff_cell_id = abs(cell->id - last_cell->id);
  4337. if (diff_cell_id > cnt * MAX_CELL_FORWARD_JUMP)
  4338. {
  4339. cell->isOverThre = true;
  4340. return true;
  4341. }*/
  4342. //判定方式改为反向遍历取相邻两个元素进行判断
  4343. std::list<std::shared_ptr<Cell>>::reverse_iterator it = his_cell.rbegin();
  4344. std::list<std::shared_ptr<Cell>>::reverse_iterator it_last = it;
  4345. std::advance(it_last,1);
  4346. if (it == his_cell.rend() || it_last == his_cell.rend())
  4347. {
  4348. return false;
  4349. }
  4350. cnt = abs((*it)->card_stamp_time - (*it_last)->card_stamp_time);
  4351. int diff_cell_id = abs((*it)->id - (*it_last)->id);
  4352. if (diff_cell_id > cnt * MAX_CELL_FORWARD_JUMP)
  4353. {
  4354. (*it)->isOverThre = true;
  4355. return true;
  4356. }
  4357. return false;
  4358. }
  4359. int Card::SetCellPathMap(std::shared_ptr<CellPathMap> pCellPath)
  4360. {
  4361. this->mpCellPath = pCellPath;
  4362. return 0;
  4363. }
  4364. int Card::ChangeSystemTime(SYSTEMTIME& st,const int condition)
  4365. {
  4366. int nMs = st.wMilliseconds;
  4367. int nSecond = st.wSecond;
  4368. int nMinute = st.wMinute;
  4369. int nHour = st.wHour;
  4370. if (condition == 0)
  4371. {
  4372. if (st.wMilliseconds < 500)
  4373. {
  4374. nMs = 0;
  4375. }else{
  4376. nMs = 500;
  4377. }
  4378. }
  4379. else
  4380. {
  4381. if (st.wMilliseconds == 0)
  4382. {
  4383. nMs = 500;
  4384. }else if(st.wMilliseconds == 500){
  4385. nMs = 0;
  4386. nSecond += 1;
  4387. if (nSecond >= 60)
  4388. {
  4389. nSecond = 0;
  4390. nMinute += 1;
  4391. if (nMinute >= 60)
  4392. {
  4393. nMinute = 0;
  4394. nHour += 1;
  4395. }
  4396. }
  4397. }
  4398. }
  4399. st.wMilliseconds = nMs;
  4400. st.wSecond = nSecond;
  4401. st.wMinute = nMinute;
  4402. st.wHour = nHour;
  4403. return 0;
  4404. }
  4405. int Card::ChangeSystemTime(SYSTEMTIME& st)
  4406. {
  4407. int nMs = st.wMilliseconds;
  4408. int nSecond = st.wSecond;
  4409. int nMinute = st.wMinute;
  4410. int nHour = st.wHour;
  4411. //如果毫秒数大于1000,向秒进一格
  4412. if (st.wMilliseconds + 500 >= 1000)
  4413. {
  4414. nMs = st.wMilliseconds + 500 - 1000;
  4415. //如果秒大于60,向分进一格
  4416. if (st.wSecond + 1 >= 60)
  4417. {
  4418. nSecond = st.wSecond + 1 - 60;
  4419. //如果分大于60,向时进一格
  4420. if (st.wMinute + 1 >= 60)
  4421. {
  4422. nMinute = st.wMinute + 1 - 60;
  4423. //如果时大于24,向天进一格
  4424. if (st.wHour + 1 >= 24)
  4425. {
  4426. nHour = st.wHour + 1 - 24;
  4427. }else{
  4428. nHour = st.wHour + 1;
  4429. }
  4430. }else{
  4431. nMinute = st.wMinute + 1;
  4432. }
  4433. }else{
  4434. nSecond = st.wSecond + 1;
  4435. }
  4436. }else{
  4437. nMs = st.wMilliseconds + 500;
  4438. }
  4439. st.wMilliseconds = nMs;
  4440. st.wSecond = nSecond;
  4441. st.wMinute = nMinute;
  4442. st.wHour = nHour;
  4443. return 0;
  4444. }
  4445. int Card::ChangeSystemTimeBySecond(SYSTEMTIME& st)
  4446. {
  4447. //只考虑到小时
  4448. int nMs = st.wMilliseconds;
  4449. int nSecond = st.wSecond;
  4450. int nMinute = st.wMinute;
  4451. int nHour = st.wHour;
  4452. //毫秒数保持不变
  4453. nMs = st.wMilliseconds;
  4454. //如果秒大于60,向分进一格
  4455. if (st.wSecond + 1 >= 60)
  4456. {
  4457. nSecond = st.wSecond + 1 - 60;
  4458. //如果分大于60,向时进一格
  4459. if (st.wMinute + 1 >= 60)
  4460. {
  4461. nMinute = st.wMinute + 1 - 60;
  4462. //如果时大于24,向天进一格
  4463. if (st.wHour + 1 >= 24)
  4464. {
  4465. nHour = st.wHour + 1 - 24;
  4466. }else{
  4467. nHour = st.wHour + 1;
  4468. }
  4469. }else{
  4470. nMinute = st.wMinute + 1;
  4471. }
  4472. }else{
  4473. nSecond = st.wSecond + 1;
  4474. }
  4475. st.wMilliseconds = nMs;
  4476. st.wSecond = nSecond;
  4477. st.wMinute = nMinute;
  4478. st.wHour = nHour;
  4479. return 0;
  4480. }
  4481. //void Card::handle_data_thread(LPVOID pContext)
  4482. //{
  4483. // CYAServerDlg* pDlg = (CYAServerDlg*)pContext;
  4484. // while (TRUE)
  4485. // {
  4486. // WaitForSingleObject(hEvent,INFINITE);
  4487. // ResetEvent(hEvent);
  4488. // char sql[LENGTH_SQL] = {0};
  4489. // char chTime[30] = {0};
  4490. // char chIntervalTime[30] = {0};
  4491. // sprintf_s(chTime,
  4492. // 30,
  4493. // "%u-%u-%u %u:%u:%u.%u",
  4494. // cellDealTime.wYear,
  4495. // cellDealTime.wMonth,
  4496. // cellDealTime.wDay,
  4497. // cellDealTime.wHour,
  4498. // cellDealTime.wMinute,
  4499. // cellDealTime.wSecond,
  4500. // cellDealTime.wMilliseconds);
  4501. //
  4502. // sprintf_s(chIntervalTime,
  4503. // 30,
  4504. // "%u-%u-%u %u:%u:%u.%u",
  4505. // last_cell->interval_time.wYear,
  4506. // last_cell->interval_time.wMonth,
  4507. // last_cell->interval_time.wDay,
  4508. // last_cell->interval_time.wHour,
  4509. // last_cell->interval_time.wMinute,
  4510. // last_cell->interval_time.wSecond,
  4511. // last_cell->interval_time.wMilliseconds);
  4512. //
  4513. // sprintf_s(sql,
  4514. // "insert ignore into his_cell_location(card_id,cur_time,interval_time,origin_reader_name,origin_cell_id,real_reader_name,real_cell_id,card_stamp_time,isFit,speed,mileage,map_id,area_id) values('%s','%s','%s','%s',%d,'%s',%d,%d,%d,%.4f,%.4f,%d,%d);",
  4515. // card_id.c_str(),
  4516. // chTime,
  4517. // chIntervalTime,
  4518. // originCellReaderName.c_str(),
  4519. // originCellId,
  4520. // curCellReaderName.c_str(),
  4521. // curCellId,
  4522. // time_stamp_last,
  4523. // is_fit_pos,
  4524. // get_speed(),
  4525. // mileage,
  4526. // map_id,
  4527. // area_id);
  4528. //
  4529. // send_sql_message(sql);
  4530. // pDlg->send_json_data(JSON_CMD_VALUE_PUSH,pDlg->get_json_position());
  4531. // }
  4532. //}
  4533. int Card::OutputCmdLog(int n)
  4534. {
  4535. if (card_id != "0020000001047")
  4536. {
  4537. return 0;
  4538. }
  4539. CString strOutput = _T("");
  4540. CString tmp = _T("");
  4541. tmp = this->card_id.c_str();
  4542. strOutput += tmp;
  4543. tmp =_T("");
  4544. tmp.Format(_T(",sync num:%d"),this->m_nLastLocateT);
  4545. strOutput += tmp;
  4546. tmp =_T("");
  4547. tmp.Format(_T(",card num:%d"),this->time_stamp_cal);
  4548. strOutput += tmp;
  4549. tmp =_T("");
  4550. tmp.Format(_T(",sync list size :%d \r\n"),m_syncNumList.size());
  4551. strOutput += tmp;
  4552. TRACE(strOutput);
  4553. tmp = _T("");
  4554. tmp.Format(_T("exit %d \r\n"),n);
  4555. TRACE(tmp);
  4556. return 0;
  4557. }
  4558. void Card::set_speed( double v )
  4559. {
  4560. }
  4561. // 检查dist数据有效性
  4562. int Card::CheckDistData(int cnt)
  4563. {
  4564. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_140);
  4565. GetLocalTime(&m_afmData.st);
  4566. // dist数据少于两条直接退出
  4567. if(cnt < 2){
  4568. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_141);
  4569. m_afmData.bStatus = true;
  4570. m_afmData.strCardId = this->card_id;
  4571. m_afmData.nType = ALGO_FAILED_CONDITION_15;
  4572. ALGORITHM_FAILED(ALGO_FAILED_CONDITION_15);
  4573. return DIST_COUNT_LESS_THAN_TWO;
  4574. }
  4575. //主要处理当相同卡的时间戳的数据中存在同步序号大于5的情况,如果有大于5的数据则丢弃此数据
  4576. int k = 0;
  4577. int dst = 0;
  4578. int st = 0;
  4579. bool bRet = false;
  4580. // 获取最大时间同步值
  4581. for(DistMap::iterator it = _dists.front().distmap.begin(); it != _dists.front().distmap.end(); ++it,k++){
  4582. if(k==0){
  4583. st = it->second->sync_num;
  4584. }else{
  4585. if(st < it->second->sync_num){
  4586. //未考虑跨周期
  4587. st = it->second->sync_num;
  4588. }
  4589. }
  4590. }
  4591. map<unsigned long long,std::shared_ptr<_coordinate>> mp_dists_locate_ex;
  4592. mp_dists_locate_ex.clear();
  4593. bool bExist = false;
  4594. // 筛选掉线性插值异常的数据
  4595. for(DistMap::iterator it = _dists.front().distmap.begin(); it != _dists.front().distmap.end(); ++it){
  4596. //如果同步时间戳存在异常值,则不走算法定位,直接返回上一次结果值
  4597. if(LLONG_MAX == it->second->tt){
  4598. bExist = true;
  4599. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_144);
  4600. }else
  4601. {
  4602. mp_dists_locate_ex.insert(make_pair(it->second->tt,it->second));
  4603. }
  4604. }
  4605. if (bExist)
  4606. {
  4607. //因为存在异常数据,如果抛弃了几条,剩下的数据定位错误会导致后续所有定位成功的都失败
  4608. return DIST_COUNT_LESS_FOR_TIMESTAMP_ERROR;
  4609. }
  4610. if(mp_dists_locate_ex.size() < 2){
  4611. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_143);
  4612. this->x = this->last_locate.x;
  4613. this->y = this->last_locate.y;
  4614. this->z = 0;
  4615. m_afmData.bStatus = true;
  4616. m_afmData.strCardId = this->card_id;
  4617. m_afmData.nType = ALGO_FAILED_CONDITION_2;
  4618. ALGORITHM_FAILED(ALGO_FAILED_CONDITION_2);
  4619. return DIST_COUNT_LESS_FOR_TIMESTAMP_ERROR;
  4620. }
  4621. // 筛选掉同步序号与最大值差5的数据
  4622. mp_dists_locate.clear();
  4623. map<unsigned long long,std::shared_ptr<_coordinate>>::iterator it_mpdl = mp_dists_locate_ex.begin();
  4624. for(;it_mpdl!=mp_dists_locate_ex.end();++it_mpdl){
  4625. if(st - it_mpdl->second->sync_num < 5){
  4626. mp_dists_locate.insert(make_pair(it_mpdl->second->tt, it_mpdl->second));
  4627. }else{
  4628. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_142);
  4629. }
  4630. }
  4631. if(mp_dists_locate.size() < 2){
  4632. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_143);
  4633. this->x = this->last_locate.x;
  4634. this->y = this->last_locate.y;
  4635. this->z = 0;
  4636. m_afmData.bStatus = true;
  4637. m_afmData.strCardId = this->card_id;
  4638. m_afmData.nType = ALGO_FAILED_CONDITION_2;
  4639. ALGORITHM_FAILED(ALGO_FAILED_CONDITION_2);
  4640. return DIST_COUNT_LESS_FOR_SYNC_NUM_DIFFER_FIVE;
  4641. }
  4642. if (!CheckCardCtValid())
  4643. {
  4644. //如果卡的ct存在当前比之前的小,且是未跨周期的情况下,则认为此组数据无效
  4645. return DIST_COUNT_CARD_CUR_CT_LESS_LAST;
  4646. }
  4647. return 0;
  4648. }
  4649. int Card::AssembleDistData(std::shared_ptr<ReceiveDataMap> pRdm)
  4650. {
  4651. int maxSyncTimes = 0;
  4652. //保存加速度当前状态和上次状态
  4653. int acce_state = 0;
  4654. int acce_state_last = 0;
  4655. int ins_direction = 0;
  4656. int card_time_stamp = 0;
  4657. map<unsigned long long,std::shared_ptr<_coordinate>>::iterator it_mpdl = mp_dists_locate.begin();
  4658. int i = 0;
  4659. for(;it_mpdl!=mp_dists_locate.end();++it_mpdl){
  4660. if(i==0){
  4661. card_time_stamp = it_mpdl->second->t;
  4662. maxSyncTimes = it_mpdl->second->sync_num;
  4663. acce_state = it_mpdl->second->acce_state;
  4664. acce_state_last = it_mpdl->second->acce_state_last;
  4665. ins_direction = it_mpdl->second->ins_direction;
  4666. }
  4667. else{
  4668. if(maxSyncTimes < it_mpdl->second->sync_num){
  4669. maxSyncTimes = it_mpdl->second->sync_num;
  4670. acce_state = it_mpdl->second->acce_state;
  4671. acce_state_last = it_mpdl->second->acce_state_last;
  4672. ins_direction = it_mpdl->second->ins_direction;
  4673. }
  4674. }
  4675. ReceiveDataMap::iterator prdm_it = pRdm->find(it_mpdl->second->tt);
  4676. if(prdm_it == pRdm->end()){
  4677. //保存信息用于定位
  4678. std::shared_ptr<ReceiveData> prd = std::make_shared<ReceiveData>();
  4679. prd->reader_id = it_mpdl->second->reader_id;
  4680. prd->antenna_id = it_mpdl->second->antenna_id;
  4681. prd->rec_time_stamp = it_mpdl->second->tt;
  4682. prd->x = it_mpdl->second->x*this->map_scale;
  4683. prd->y = it_mpdl->second->y*this->map_scale;
  4684. prd->z = it_mpdl->second->z*this->map_scale;
  4685. prd->special = it_mpdl->second->special;
  4686. if (prd->rec_time_stamp > 0)
  4687. {
  4688. pRdm->insert(make_pair(prd->rec_time_stamp,prd));
  4689. }
  4690. }
  4691. i++;
  4692. }
  4693. m_nCalcSyncNum = maxSyncTimes;
  4694. acce_cur_state = acce_state;
  4695. return 0;
  4696. }
  4697. int Card::SaveCardAlgoData(std::shared_ptr<POS>& pos)
  4698. {
  4699. int nRet = 0;
  4700. if (ranging_type == LOCATEDATATYPE::LDT_TDOA)
  4701. {
  4702. nRet = SaveTdoaData(pos);
  4703. }else if (ranging_type == LOCATEDATATYPE::LDT_TOF)
  4704. {
  4705. nRet = SaveTofData(pos);
  4706. }
  4707. return nRet;
  4708. }
  4709. int Card::SaveTofData(const std::shared_ptr<POS> pos)
  4710. {
  4711. //if(tmpPos->posx != 0 && tmpPos->posy != 0){
  4712. //if((abs(pos->posx) < ZERO_PRECISION && abs(pos->posy) < ZERO_PRECISION) || pos->posx == INVALID_COORDINATE && pos->posy == INVALID_COORDINATE){
  4713. if(!CheckPositionValid(pos)){
  4714. return 1;
  4715. }else{
  4716. double newPosX = pos->cx;
  4717. double newPosY = pos->cy;
  4718. if (is_algo_first_location)
  4719. {
  4720. is_algo_first_location = false;
  4721. }else{
  4722. this->m_nStream = GetStream(last_locate.x,last_locate.y,pos->cx,pos->cy);
  4723. double d = sqrt(pow(newPosX - this->last_locate.x,2) + pow(newPosY - this->last_locate.y,2))*this->map_scale;
  4724. pos->delta_time = difftime(p_reader->reader_time,time_last_tof);
  4725. this-> v = d / pos->delta_time; //速度计算有问题,因为delta_time时间问题
  4726. }
  4727. time_last_tof = p_reader->reader_time;
  4728. this->x = this->last_locate.x = newPosX;
  4729. this->y = this->last_locate.y = newPosY;
  4730. this->z = 0;
  4731. this->t = this->last_locate.t = time_stamp_cal; //difT
  4732. this->is_deal_by_algo = true;
  4733. }
  4734. return 0;
  4735. }
  4736. int Card::SaveTdoaData(const std::shared_ptr<POS> pos)
  4737. {
  4738. this->x = pos->cx;
  4739. this->y = pos->cy;
  4740. this->z = pos->cz;
  4741. if (STATE_ACCE_IDLING == this->acce_cur_state)
  4742. {
  4743. this->x = this->last_locate.x;
  4744. this->y = this->last_locate.y;
  4745. this->z = this->last_locate.z;
  4746. this->v = 0;
  4747. }else{
  4748. //非怠速状态
  4749. if (!is_algo_first_location)
  4750. {
  4751. //第一定位不计算上下行方向
  4752. if (abs(this->last_locate.x) > 1E-5 || abs(this->last_locate.y) > 1E-5)
  4753. {
  4754. //获得上下行方向
  4755. this->m_nStream = GetStream(this->last_locate.x,this->last_locate.y,this->x,this->y);
  4756. }
  4757. }
  4758. this->algo_calc_offset();
  4759. this->last_locate.x = this->x;
  4760. this->last_locate.y = this->y;
  4761. this->last_locate.z = this->z = 0;
  4762. this->last_locate.sync_num = this->m_nCalcSyncNum;
  4763. this->last_locate.acceleration = pos->av;
  4764. int nSign = 1;
  4765. if(pos->cvx == 0){
  4766. if(pos->cvy > 0){
  4767. nSign = 1;
  4768. }
  4769. }else{
  4770. if(pos->cvx > 0){
  4771. nSign = 1;
  4772. }else{
  4773. nSign = -1;
  4774. }
  4775. }
  4776. this->m_nMoveDirection = nSign;
  4777. if (!is_algo_first_location)
  4778. {
  4779. //第一次定位不计算速度
  4780. //速度的计算采用求平均的方式
  4781. double speed = sqrt(pow(pos->cvx,2) + pow(pos->cvy,2));
  4782. speed *=3.6; //转为km/h
  4783. if (vt_his_speed.size() == 3)
  4784. {
  4785. vt_his_speed.pop_front();
  4786. }
  4787. if (speed > 1E-4)
  4788. {
  4789. vt_his_speed.push_back(speed);
  4790. }
  4791. int total = 0;
  4792. double sum_speed = 0;
  4793. for (list<double>::iterator it = vt_his_speed.begin();it != vt_his_speed.end();++it)
  4794. {
  4795. if (*it > 0)
  4796. {
  4797. sum_speed += *it;
  4798. total++;
  4799. }
  4800. }
  4801. double av = 0;
  4802. if (total > 0)
  4803. {
  4804. av = sum_speed / total;
  4805. }
  4806. if (STATE_ACCE_STATIC == this->acce_cur_state || STATE_ACCE_IDLING == this->acce_cur_state)
  4807. {
  4808. this->v = 0;
  4809. }
  4810. else
  4811. {
  4812. this->v = av*nSign;
  4813. }
  4814. }
  4815. }
  4816. this->last_locate.sync_num = this->m_nCalcSyncNum;
  4817. this->m_pKalmanFilter->m_pCar->t = this->m_nLastLocateT = this->m_nCalcSyncNum;
  4818. this->last_vx = pos->cvx;
  4819. this->last_vy = pos->cvy;
  4820. if (is_algo_first_location)
  4821. {
  4822. is_algo_first_location = false;
  4823. }
  4824. if (FIT_POSITION_NUM*4 == cur_fit_nums)
  4825. {
  4826. this->v = 0;
  4827. }
  4828. this->last_locate.v = this->v;
  4829. if(pos->update){
  4830. sync_data sdNew;
  4831. sdNew.sync_num = this->m_nCalcSyncNum;
  4832. sdNew.x = this->x;
  4833. sdNew.y = this->y;
  4834. sdNew.vx = this->last_vx;
  4835. sdNew.vy = this->last_vy;
  4836. m_syncNumList.push_back(sdNew);
  4837. }
  4838. //保存历史数据
  4839. std::shared_ptr<POS> p = std::make_shared<POS>();
  4840. p->posx = this->x*map_scale;
  4841. p->posy = this->y*map_scale;
  4842. p->card_count = this->time_stamp_cal;
  4843. //拟合点作为参考点
  4844. //拟合数据的计算
  4845. if (his_pos.size() == REF_POSITION_NUM)
  4846. {
  4847. his_pos.pop_front();
  4848. }
  4849. his_pos.push_back(p);
  4850. int nRef = REF_POSITION_NUM * 4;
  4851. if (long_his_pos.size() == nRef)
  4852. {
  4853. long_his_pos.pop_front();
  4854. }
  4855. long_his_pos.push_back(p);
  4856. if(is_ref_pos)
  4857. {
  4858. //保存历史数据
  4859. std::shared_ptr<POS> p = std::make_shared<POS>();
  4860. p->posx = this->x*map_scale;
  4861. p->posy = this->y*map_scale;
  4862. p->card_count = this->time_stamp_cal;
  4863. //拟合数据的计算
  4864. if (REF_POSITION_NUM == his_pos.size())
  4865. {
  4866. his_pos.pop_front();
  4867. }
  4868. his_pos.push_back(p);
  4869. if (REF_POSITION_NUM == his_pos.size())
  4870. {
  4871. if (!have_long_fit_pos)
  4872. {
  4873. CalcFittingData();
  4874. cur_fit_nums = 0;
  4875. }
  4876. }
  4877. int nRef = REF_POSITION_NUM * 4;
  4878. if (long_his_pos.size() == nRef)
  4879. {
  4880. long_his_pos.pop_front();
  4881. }
  4882. long_his_pos.push_back(p);
  4883. if (long_his_pos.size() == nRef)
  4884. {
  4885. CalcLongFittingData();
  4886. cur_fit_nums = 0;
  4887. }
  4888. }
  4889. return 0;
  4890. }
  4891. /*
  4892. * 作用:保存算法计算结果数据
  4893. *
  4894. * param
  4895. * cell 单元格
  4896. *
  4897. * return
  4898. * 返回0
  4899. *
  4900. */
  4901. int Card::SaveCardAlgoData(std::shared_ptr<Cell>& cell)
  4902. {
  4903. //计算卡本次相对于上一次的步进格子数
  4904. int nStep = 0;
  4905. SYSTEMTIME sysit;
  4906. std::string strTime = "";
  4907. if (cell && last_cell)
  4908. {
  4909. if (!cell->isChangePath)
  4910. {
  4911. nStep = abs(cell->id - last_cell->id);
  4912. }else{
  4913. nStep = cell->nStep;
  4914. }
  4915. }
  4916. if (last_cell)
  4917. {
  4918. if (CFunctions::CheckSystemTimeValid(last_cell->interval_time))
  4919. {
  4920. sysit = last_cell->interval_time;
  4921. strTime = CFunctions::systime2string(last_cell->interval_time);
  4922. }
  4923. }
  4924. //格子换算成坐标
  4925. std::shared_ptr<POS> pos = std::make_shared<POS>();
  4926. pos = Cell2Position(cell);
  4927. curCellReaderName = cell->strSubjectReader;
  4928. curCellId = cell->id;
  4929. //更新格子信息
  4930. cell->nStep = nStep;
  4931. cell->dataSource = 1;
  4932. cell->card_stamp_time = time_stamp_cal;
  4933. time_stamp_last = time_stamp_cal;
  4934. if (is_algo_first_location)
  4935. {
  4936. //第一次定位,将定位时间的毫秒数改为500或者0
  4937. ::GetLocalTime(&cell->interval_time);
  4938. if (cell->interval_time.wMilliseconds < 500)
  4939. {
  4940. cell->interval_time.wMilliseconds = 0;
  4941. }
  4942. else
  4943. {
  4944. cell->interval_time.wMilliseconds = 500;
  4945. }
  4946. }else{
  4947. if (card_type == CT_VEHICLE && last_cell && CFunctions::CompareSystemTime(cell->interval_time,last_cell->interval_time) != 0)
  4948. {
  4949. cell->interval_time = last_cell->interval_time;
  4950. ChangeSystemTime(cell->interval_time);
  4951. }
  4952. //下面判断是人卡,并且指向上一个cell的指针存在,并且
  4953. //if (card_type == CT_PERSON)
  4954. //{
  4955. // ::GetLocalTime(&cell->interval_time);
  4956. // ChangeMsTimeToZeroOrFive(cell->interval_time); //ms四舍五入
  4957. // //如果与上一个格子的时刻是一样的,那么这次的cell就没有必要插入了,直接舍弃
  4958. // if( last_cell && CFunctions::CompareSystemTime(cell->interval_time,last_cell->interval_time) == 0){
  4959. // return 1; //这个格子舍弃,直接返回,返回错误值
  4960. // }
  4961. //}
  4962. }
  4963. if (cell)
  4964. {
  4965. if (last_cell == nullptr)
  4966. {
  4967. last_cell = std::make_shared<Cell>();
  4968. }
  4969. ::GetLocalTime(&cell->deal_time);
  4970. *last_cell = *cell;
  4971. locate_cells.push_back(last_cell);
  4972. }
  4973. //std::mutex mu;
  4974. //mu.lock();
  4975. //if (this->nStartLocateCounts == 5)
  4976. //{
  4977. // ::GetLocalTime(&cell->interval_time);
  4978. // if (cell->interval_time.wMilliseconds < 500)
  4979. // {
  4980. // cell->interval_time.wMilliseconds = 0;
  4981. // }
  4982. // else
  4983. // {
  4984. // cell->interval_time.wMilliseconds = 500;
  4985. // }
  4986. //}else if(this->nStartLocateCounts > 5){
  4987. // //刘思平修订的代码Part 1/Sum 1:begin
  4988. // //以下代码区分了人卡和车卡的不同,车卡是固定500ms时隙的,而人卡不会补时隙,也就是说只会将当前时刻的毫秒四舍五入为0ms或者500ms
  4989. // //这个地方要修改,只是针对车卡的
  4990. // //by lsp on 2017.07.09 @山西长治
  4991. // if (card_type == CT_VEHICLE && last_cell && CFunctions::CompareSystemTime(cell->interval_time,last_cell->interval_time) != 0)
  4992. // {
  4993. // cell->interval_time = last_cell->interval_time;
  4994. // ChangeSystemTime(cell->interval_time);
  4995. // }
  4996. // //下面判断是人卡,并且指向上一个cell的指针存在,并且
  4997. // if (card_type == CT_PERSON)
  4998. // {
  4999. // ::GetLocalTime(&cell->interval_time);
  5000. // ChangeMsTimeToZeroOrFive(cell->interval_time); //ms四舍五入
  5001. // //如果与上一个格子的时刻是一样的,那么这次的cell就没有必要插入了,直接舍弃
  5002. // if( last_cell && CFunctions::CompareSystemTime(cell->interval_time,last_cell->interval_time) == 0){
  5003. // mu.unlock();
  5004. // return 1; //这个格子舍弃,直接返回,返回错误值
  5005. // }
  5006. // }
  5007. //}
  5008. //if (cell)
  5009. //{
  5010. // if (last_cell == nullptr)
  5011. // {
  5012. // last_cell = std::make_shared<Cell>();
  5013. // }
  5014. // *last_cell = *cell;
  5015. // locate_cells.push_back(last_cell);
  5016. //}else
  5017. // return 1;
  5018. //mu.unlock();
  5019. //if (!is_algo_first_location)
  5020. //{
  5021. // //确保第一次不会进来算速度
  5022. // if (this->x != INVALID_COORDINATE && this->y != INVALID_COORDINATE)
  5023. // {
  5024. // //pos为本次定位坐标,(x,y)为上一次定位坐标
  5025. // double distance = sqrt(pow(pos->cx - this->x,2) + pow(pos->cy - this->y,2))*map_scale;
  5026. // this->mileage = distance/1000.0;
  5027. // int nCounts = ceil(distance/CELL_WIDTH);
  5028. // this->v = nCounts*CELL_WIDTH/(SEND_CELL_INTERVAL/1000.0);
  5029. // this->v*=3.6;
  5030. // }
  5031. //}
  5032. this->v = fit_k*0.6*3.6;
  5033. this->x = pos->cx;
  5034. this->y = pos->cy;
  5035. this->z = 0;
  5036. if (!is_algo_first_location )
  5037. {
  5038. this->m_nStream = GetStream(this->last_locate.x,this->last_locate.y,this->x,this->y);
  5039. }
  5040. this->last_locate.x = pos->cx;
  5041. this->last_locate.y = pos->cy;
  5042. this->last_locate.z = 0;
  5043. this->last_locate.sync_num = this->m_nCalcSyncNum;
  5044. this->m_nLastLocateT = this->m_nCalcSyncNum;
  5045. this->last_locate.v = this->v;
  5046. if (is_algo_first_location)
  5047. {
  5048. is_algo_first_location = false;
  5049. }
  5050. //反向取MAX_FIT_DATA_COUNTS个元素
  5051. double val[MAX_FIT_DATA_COUNTS] = {0};
  5052. int i = MAX_FIT_DATA_COUNTS - 1;
  5053. for (std::list<std::shared_ptr<Cell>>::reverse_iterator it = his_cell.rbegin();it != his_cell.rend();++it)
  5054. {
  5055. if (!(*it)->isOverThre)//大跳数据不参与怠速判断
  5056. {
  5057. //如果非越界,则参与计算怠速判断,
  5058. val[i] = (*it)->id;
  5059. i--;
  5060. }
  5061. if (i < 0)
  5062. {
  5063. break;
  5064. }
  5065. }
  5066. int nAcs = 0;
  5067. int nDesc = 0;
  5068. //统计方向
  5069. for (int k = 0; k < MAX_FIT_DATA_COUNTS - 1;k++)
  5070. {
  5071. if (val[k] < val[k+1])
  5072. {
  5073. nAcs++;
  5074. }else{
  5075. nDesc++;
  5076. }
  5077. }
  5078. if (nAcs > nDesc)
  5079. {
  5080. nIncrease = 1;
  5081. }else if (nAcs < nDesc)
  5082. {
  5083. nIncrease = -1;
  5084. }else
  5085. {
  5086. //相等的话就保持原来的方向
  5087. }
  5088. if (pos)
  5089. {
  5090. }
  5091. return 0;
  5092. }
  5093. //刘思平修订的代码Part 2/Sum 2:
  5094. //以下函数将当前时刻的毫秒四舍五入为0ms或者500ms
  5095. //by lsp on 2017.07.09 @山西长治
  5096. int Card::ChangeMsTimeToZeroOrFive(SYSTEMTIME& st)
  5097. {
  5098. int nMs = st.wMilliseconds;
  5099. //如果毫秒数大于1000,向秒进一格
  5100. if (nMs < 500)
  5101. {
  5102. nMs = 0;
  5103. }else{
  5104. nMs = 500;
  5105. }
  5106. st.wMilliseconds = nMs;
  5107. return 0;
  5108. }
  5109. //代码到此修改完毕
  5110. //刘思平修订的代码Part 2/Sum 2
  5111. /*
  5112. * 通过加速度或速度阈值来检查p定位结果的正确性
  5113. *
  5114. * param
  5115. * p 定位结果
  5116. *
  5117. * return
  5118. * 如果通过检查返回0,否则返回非零值
  5119. */
  5120. int Card::CheckSolution(std::shared_ptr<POS>& p)
  5121. {
  5122. //定位成功
  5123. double cx = p->cx;
  5124. double cy = p->cy;
  5125. double cz = p->cz;
  5126. double cvx = 0.0,cvy = 0.0,cvz = 0.0;
  5127. double av = 0.0;
  5128. if(is_algo_first_location){
  5129. sync_data sdNew;
  5130. sdNew.sync_num = this->m_nCalcSyncNum;
  5131. sdNew.x = cx;
  5132. sdNew.y = cy;
  5133. sdNew.vx = 0;
  5134. sdNew.vy = 0;
  5135. sdNew.update = false;
  5136. m_syncNumList.push_back(sdNew);
  5137. }else{
  5138. //现在的关于同步序号的处理是这样的:
  5139. //如果定位成功,就把这次定位成功的同步数据:同步序号,坐标;x,y方向的速度,扔到一个队列里,
  5140. //后来定位成功的就会先根据同步序号差用加速度抛一次;
  5141. //抛不掉,就用队列里的同步数据(从后往前找),找到第一个与当前同步序号相差大于5的同步数据来进行第二次计算速度以及加速度,
  5142. //如果加速度大于5,就不要此次的定位数据,
  5143. //如果通过加速度判断就将队列中从头开始到此同步数据的所有元素都丢弃,并插入新的此次同步数据
  5144. double interval_time = 0.2;
  5145. double deltaT = 0;
  5146. sync_data sd;
  5147. if (this->b_long_interval)
  5148. {
  5149. //此段代码用于将上一次定位是根据两个时间差是个很大值而定位出的结果
  5150. //当后续定位时就和最近的定位结果进行比较
  5151. //例如:当上一次同步序号是14321,它定位时比较的同步序号是14200,时间差大于20多秒
  5152. //这时我们就将b_long_interval置为true
  5153. //当本次定位,同步序号是14326,,这时就需要根据最近的14321进行判断
  5154. list<sync_data>::reverse_iterator it = m_syncNumList.rbegin();
  5155. sync_data sd = *it;
  5156. //以下计算deltaT还需要考虑卡的同步序号轮回的情况。
  5157. if (m_nCalcSyncNum > it->sync_num)
  5158. {
  5159. deltaT = (m_nCalcSyncNum - sd.sync_num)*interval_time;
  5160. }
  5161. else
  5162. {
  5163. deltaT = (m_nCalcSyncNum + 65536 - sd.sync_num)*interval_time;
  5164. }
  5165. if (deltaT < 10 && deltaT > 0)
  5166. {
  5167. this->b_long_interval = false;
  5168. }
  5169. //避免同一个同步序号下存在多个不同卡序号
  5170. if (deltaT < 1E-2)
  5171. {
  5172. deltaT = 0.2;
  5173. }
  5174. p->diff_reader_sync_num = deltaT;
  5175. p->ref_x = sd.x;
  5176. p->ref_y = sd.y;
  5177. cvx = abs((cx - sd.x)*this->map_scale/deltaT);
  5178. cvy = abs((cy - sd.y)*this->map_scale/deltaT);
  5179. double avx = (cvx - sd.vx) / deltaT;
  5180. double avy = (cvy - sd.vy) / deltaT;
  5181. av = sqrt(pow(avx,2) + pow(avy,2));
  5182. //车卡的加速度
  5183. switch(this->card_type){
  5184. case CT_PERSON:
  5185. if(av > PERSON_ACCELERATE_THRESHOLD){
  5186. p->reason = CHECK_PERSON_ACCE_OVER_SPEED;
  5187. p->posx = INVALID_COORDINATE;
  5188. p->posy = INVALID_COORDINATE;
  5189. p->posz = INVALID_COORDINATE;
  5190. p->cx = p->posx / map_scale;
  5191. p->cy = p->posy / map_scale;
  5192. p->cz = p->posz / map_scale;
  5193. this->b_long_interval = false;
  5194. m_afmData.bStatus = true;
  5195. m_afmData.strCardId = this->card_id;
  5196. m_afmData.nType = ALGO_FAILED_CONDITION_5;
  5197. ALGORITHM_FAILED(ALGO_FAILED_CONDITION_5);
  5198. return CHECK_PERSON_ACCE_OVER_SPEED;
  5199. }
  5200. break;
  5201. case CT_VEHICLE:
  5202. if(av > VECHILE_ACCELERATE_THRESHOLD){
  5203. //保留上次结果
  5204. p->reason = CHECK_VEHICLE_ACCE_OVER_SPEED;
  5205. p->posx = INVALID_COORDINATE;
  5206. p->posy = INVALID_COORDINATE;
  5207. p->posz = INVALID_COORDINATE;
  5208. p->cx = p->posx / map_scale;
  5209. p->cy = p->posy / map_scale;
  5210. p->cz = p->posz / map_scale;
  5211. this->b_long_interval = false;
  5212. m_afmData.bStatus = true;
  5213. m_afmData.strCardId = this->card_id;
  5214. m_afmData.nType = ALGO_FAILED_CONDITION_6;
  5215. ALGORITHM_FAILED(ALGO_FAILED_CONDITION_6);
  5216. return CHECK_VEHICLE_ACCE_OVER_SPEED;
  5217. }
  5218. break;
  5219. }
  5220. }else{
  5221. //从队列尾部开始查找,找到第一个同步序号与当前计算卡的同步序号相差5个以上的数据
  5222. list<sync_data>::reverse_iterator it;
  5223. bool bOverflow = false;
  5224. bool bfind =false;
  5225. for(it = m_syncNumList.rbegin();it!=m_syncNumList.rend();it++){
  5226. if(m_nCalcSyncNum - it->sync_num >= 5){
  5227. bfind = true;
  5228. sd = *it;
  5229. break;
  5230. }else{
  5231. if(m_nCalcSyncNum - it->sync_num < 0 && m_nCalcSyncNum < 100){
  5232. //如果最新同步号小于列表中的同步号则
  5233. if(m_nCalcSyncNum + 65536 - it->sync_num >=5 ){//5
  5234. bOverflow = true;
  5235. bfind = true;
  5236. sd = *it;
  5237. }
  5238. }else{
  5239. continue;
  5240. }
  5241. }//end else
  5242. }//end for
  5243. //根据溢出条件来计算deltaT
  5244. if(bOverflow){
  5245. deltaT = (m_nCalcSyncNum + 65536 - sd.sync_num)*interval_time;
  5246. }else{
  5247. deltaT = (m_nCalcSyncNum - sd.sync_num)*interval_time;
  5248. }
  5249. p->diff_reader_sync_num = deltaT;
  5250. p->ref_x = sd.x;
  5251. p->ref_y = sd.y;
  5252. //速度正负的判断:以x轴,y轴正向运动为正
  5253. //如果x相等,则y2 - y1 > 0为正
  5254. //其他情况,则x2 - x1 > 0 为正
  5255. int nSign = 1;
  5256. if(cx == sd.x){
  5257. if(cy > sd.y){
  5258. nSign = 1;
  5259. }else{
  5260. nSign = -1;
  5261. }
  5262. }else{
  5263. if(cx > sd.x){
  5264. nSign = 1;
  5265. }else{
  5266. nSign = -1;
  5267. }
  5268. }
  5269. //转为m/s
  5270. cvx = abs((cx - sd.x)*this->map_scale/deltaT);
  5271. cvy = abs((cy - sd.y)*this->map_scale/deltaT);
  5272. double cv = sqrt(pow(cvx,2) + pow(cvy,2));
  5273. cv = cv*nSign;
  5274. double avx = (cvx - sd.vx) / deltaT;
  5275. double avy = (cvy - sd.vy) / deltaT;
  5276. double av = sqrt(pow(avx,2) + pow(avy,2));
  5277. //车卡的加速度
  5278. switch(this->card_type){
  5279. case CT_PERSON:
  5280. if(av > PERSON_ACCELERATE_THRESHOLD){
  5281. p->reason = CHECK_PERSON_ACCE_OVER_SPEED;
  5282. p->posx = INVALID_COORDINATE;
  5283. p->posy = INVALID_COORDINATE;
  5284. p->posz = INVALID_COORDINATE;
  5285. p->cx = p->posx / map_scale;
  5286. p->cy = p->posy / map_scale;
  5287. p->cz = p->posz / map_scale;
  5288. m_afmData.bStatus = true;
  5289. m_afmData.strCardId = this->card_id;
  5290. m_afmData.nType = ALGO_FAILED_CONDITION_5;
  5291. ALGORITHM_FAILED(ALGO_FAILED_CONDITION_5);
  5292. return CHECK_PERSON_ACCE_OVER_SPEED;
  5293. }
  5294. break;
  5295. case CT_VEHICLE:
  5296. if(av > VECHILE_ACCELERATE_THRESHOLD){
  5297. //保留上次结果
  5298. p->reason = CHECK_VEHICLE_ACCE_OVER_SPEED;
  5299. p->posx = INVALID_COORDINATE;
  5300. p->posy = INVALID_COORDINATE;
  5301. p->posz = INVALID_COORDINATE;
  5302. p->cx = p->posx / map_scale;
  5303. p->cy = p->posy / map_scale;
  5304. p->cz = p->posz / map_scale;
  5305. m_afmData.bStatus = true;
  5306. m_afmData.strCardId = this->card_id;
  5307. m_afmData.nType = ALGO_FAILED_CONDITION_6;
  5308. ALGORITHM_FAILED(ALGO_FAILED_CONDITION_6);
  5309. return CHECK_VEHICLE_ACCE_OVER_SPEED;
  5310. }
  5311. break;
  5312. }
  5313. cv = cv*3.6;
  5314. //速度的限制
  5315. if(fabs(cv) > MAX_VECHILE_SPEED){
  5316. p->reason = CHECK_VEHICLE_OVER_SPEED;
  5317. p->posx = INVALID_COORDINATE;
  5318. p->posy = INVALID_COORDINATE;
  5319. p->posz = INVALID_COORDINATE;
  5320. p->cx = p->posx / map_scale;
  5321. p->cy = p->posy / map_scale;
  5322. p->cz = p->posz / map_scale;
  5323. m_afmData.bStatus = true;
  5324. m_afmData.strCardId = this->card_id;
  5325. m_afmData.nType = ALGO_FAILED_CONDITION_7;
  5326. ALGORITHM_FAILED(ALGO_FAILED_CONDITION_7);
  5327. return CHECK_VEHICLE_OVER_SPEED;
  5328. }
  5329. //使用间隔来修正速度
  5330. if(deltaT - 1.0 >= 0){
  5331. if (deltaT > 10 && bfind)
  5332. {
  5333. this->b_long_interval = true;
  5334. }
  5335. //删除第一个元素到tmp(含)之间的所有元素
  5336. bool bStartDel = false;
  5337. for(list<sync_data>::reverse_iterator tmp = m_syncNumList.rbegin();tmp != m_syncNumList.rend();)
  5338. {
  5339. if(bStartDel){
  5340. tmp = list<sync_data>::reverse_iterator(m_syncNumList.erase((++tmp).base()));
  5341. }else{
  5342. if(*tmp == sd){
  5343. bStartDel = true;
  5344. }
  5345. ++tmp;
  5346. }
  5347. }
  5348. }
  5349. if (!bfind)
  5350. {
  5351. cvx = 0;
  5352. cvy = 0;
  5353. cvz = 0;
  5354. av = 0;
  5355. }
  5356. }
  5357. this->m_nSyncNumInList = sd.sync_num;
  5358. p->update = true;
  5359. }
  5360. p->cvx = cvx;
  5361. p->cvy = cvy;
  5362. p->cvz = cvz;
  5363. p->av = av;
  5364. return 0;
  5365. }
  5366. /*
  5367. * 通过趋向性判断解的可靠性,暂通过上下行判断
  5368. *
  5369. * param
  5370. * p 当前定位坐标信息
  5371. *
  5372. * return
  5373. * 通过验证返回0,否则返回错误码
  5374. *
  5375. */
  5376. int Card::CheckSulutionByStream(std::shared_ptr<POS> p)
  5377. {
  5378. if (INIT_STREAM == m_nStream)
  5379. {
  5380. return 0;
  5381. }
  5382. if (abs(this->last_locate.x) > 1E-5 || abs(this->last_locate.y) > 1E-5)
  5383. {
  5384. //去除采集刚启动,last_locate的坐标为零而导致判断车辆上下行方向错误的问题
  5385. //车辆上下行确定
  5386. //3个条件:起点(x1,y1),终点(x2,y2)
  5387. //1.x1==x2的情况下,y2>y1为下行
  5388. //2.y1==y2的情况下,x1>x2为下行
  5389. //3.x1>x2且y2>y1为下行
  5390. //其他情况为上行
  5391. int nStream = 0;
  5392. double cx = p->posx / (map_scale*1.0);
  5393. double cy = p->posy / (map_scale*1.0);
  5394. if ((this->last_locate.x == cx && cy > this->last_locate.y)
  5395. ||(this->last_locate.x > cx && cy == this->last_locate.y)
  5396. ||(this->last_locate.x > cx && cy > this->last_locate.y))
  5397. {
  5398. nStream = DOWN_STREAM;
  5399. }
  5400. else
  5401. {
  5402. nStream = UP_STREAM;
  5403. }
  5404. if (nStream != m_nStream)
  5405. {
  5406. return 1;
  5407. }
  5408. }
  5409. return 0;
  5410. }
  5411. bool Card::IsExistPath(int left,int right)
  5412. {
  5413. TDOAReaderPathMap::iterator it = pTdoaReaderPathMap->find(left);
  5414. if (it == pTdoaReaderPathMap->end())
  5415. {
  5416. return false;
  5417. }
  5418. ReaderPathMap::iterator it_s = it->second->find(right);
  5419. if (it_s == it->second->end())
  5420. {
  5421. return false;
  5422. }
  5423. return true;
  5424. }
  5425. int Card::CopySolution(std::shared_ptr<POS> source,std::shared_ptr<POS>& dest)
  5426. {
  5427. dest->posx = source->posx;
  5428. dest->posy = source->posy;
  5429. dest->cvx = source->cvx;
  5430. dest->cvy = source->cvy;
  5431. return 0;
  5432. }
  5433. /*
  5434. * 判断是否为怠速状态
  5435. *
  5436. * param
  5437. * pos 定位结果
  5438. *
  5439. * return
  5440. * 如果怠速则返回true,否则返回false
  5441. *
  5442. */
  5443. bool Card::IsIdleStatus(std::shared_ptr<POS> pos)
  5444. {
  5445. if (idle_pos_list.size() < REF_POSITION_NUM)
  5446. {
  5447. return false;
  5448. }
  5449. bool bRet = false;
  5450. double acq = 0.3; //精度范围
  5451. _point cp;
  5452. cp.x = cp.y = cp.z = 0;
  5453. int i = 0;
  5454. double ax = 0,ay = 0;
  5455. //怠速队列数据够REF_POSITION_NUM个,计算怠速队列中数据的平均值;不够则直接返回
  5456. //if (idle_pos_list.size() == REF_POSITION_NUM)
  5457. {
  5458. for (std::list<std::shared_ptr<POS>>::iterator it = idle_pos_list.begin(); it != idle_pos_list.end();++it)
  5459. {
  5460. ax += (*it)->posx;
  5461. ay += (*it)->posy;
  5462. }
  5463. ax /= REF_POSITION_NUM;
  5464. ay /= REF_POSITION_NUM;
  5465. }
  5466. i = 0;
  5467. //计算怠速队列中数据是否都在以平均值为中心,半径是1米的范围内;如果都在,则为怠速,否则为非怠速
  5468. for (std::list<std::shared_ptr<POS>>::iterator it = idle_pos_list.begin(); it != idle_pos_list.end();++it)
  5469. {
  5470. double distance = 0;
  5471. distance = sqrt(pow((*it)->posx - ax,2) + pow((*it)->posy - ay,2));
  5472. if (distance < 1)
  5473. {
  5474. i++;
  5475. }else{
  5476. break;
  5477. }
  5478. }
  5479. if (i == 5)
  5480. {
  5481. bRet = true;
  5482. }
  5483. return bRet;
  5484. }
  5485. /*
  5486. * 判断定位结果回退,根据本次的上下行和上一次的上下行方向是否一致
  5487. *
  5488. * param
  5489. * pos 定位结果
  5490. *
  5491. * return
  5492. * 如果回退返回true,否则返回false
  5493. *
  5494. */
  5495. bool Card::IsRebound(std::shared_ptr<POS> pos)
  5496. {
  5497. //如果是第一次定位,则不判断回退
  5498. if (is_algo_first_location)
  5499. {
  5500. return false;
  5501. }
  5502. //如果上一次无方向或者本次定位结果异常,则不判断是否回退
  5503. if (m_nStream == 0 || pos->posx == INVALID_COORDINATE&&pos->posy == INVALID_COORDINATE)
  5504. {
  5505. return false;
  5506. }
  5507. int nStream = 0;
  5508. //上一次定位为起点,本次定位结果为终点,判断上下行方向
  5509. nStream = GetStream(last_locate.x,last_locate.y,pos->cx,pos->cy);
  5510. return nStream == this->m_nStream?false:true;
  5511. }
  5512. /*
  5513. * 检查解的有效性:1.非法值检查;2.指定无效数检查;3.零值检查
  5514. *
  5515. * param
  5516. * pos 定位结果
  5517. *
  5518. * return
  5519. * 如果pos中的posx,posy有效则返回true;否则返回false
  5520. *
  5521. */
  5522. bool Card::CheckPositionValid(const std::shared_ptr<POS> pos)
  5523. {
  5524. if (pos == nullptr)
  5525. {
  5526. return false;
  5527. }else if (_isnan(pos->posx) || _isnan(pos->posy))
  5528. {
  5529. return false;
  5530. }else if (pos->posx == INVALID_COORDINATE || pos->posy == INVALID_COORDINATE)
  5531. {
  5532. return false;
  5533. }else if (abs(pos->posx) < ZERO_PRECISION && abs(pos->posy) < ZERO_PRECISION)
  5534. {
  5535. //如果以后地图有(0,0)坐标的话,就需要删除此判断条件
  5536. return false;
  5537. }
  5538. return true;
  5539. }
  5540. /*
  5541. * 检查tof定位解的有效性,通过速度和加速度方式校验
  5542. *
  5543. * param
  5544. * pos 定位结果
  5545. *
  5546. * return
  5547. * 通过检验返回true,否则返回false;
  5548. *
  5549. */
  5550. bool Card::CheckSolutionTof(std::shared_ptr<POS>& pos)
  5551. {
  5552. if (is_algo_first_location)
  5553. {
  5554. return true;
  5555. }
  5556. bool bRet = false;
  5557. double delta = difftime(p_reader->reader_time,time_last_tof);
  5558. if (delta < ZERO_PRECISION)
  5559. {
  5560. }else{
  5561. double distance = 0;
  5562. distance = sqrt(pow(pos->posx - last_locate.x*map_scale,2) + pow(pos->posy - last_locate.y*map_scale,2));
  5563. double a = 0;
  5564. a = distance / pow(delta,2);
  5565. if (a > 5)
  5566. {
  5567. bRet = false;
  5568. }else{
  5569. pos->cvx = pos->posx - last_locate.x*map_scale / delta;
  5570. pos->cvy = pos->posy - last_locate.y*map_scale / delta;
  5571. bRet = true;
  5572. }
  5573. double v = 0;
  5574. v = distance/delta;
  5575. if (v > 3)
  5576. {
  5577. bRet = false;
  5578. }
  5579. }
  5580. return bRet;
  5581. }
  5582. /*
  5583. * 判断上下行方向
  5584. *
  5585. * param
  5586. * (x1,y1) 起点坐标
  5587. * (x2,y2) 终点坐标
  5588. *
  5589. * return
  5590. * 上行为1,下行为2,无方向为0
  5591. *
  5592. */
  5593. int Card::GetStream(double x1,double y1,double x2,double y2)
  5594. {
  5595. int nStream = 0;
  5596. //车辆上下行确定
  5597. //3个条件:起点(x1,y1),终点(x2,y2)
  5598. //1.x1==x2的情况下,y2>y1为下行
  5599. //2.y1==y2的情况下,x2>x1为下行
  5600. //3.x2>x1且y2>y1为下行
  5601. //其他情况为上行
  5602. if (((x1 == x2 || abs(x1 - x2) < 1E-2) && y1 < y2 )
  5603. ||((y1 == y2 || abs(y1 - y2) < 1E-2) && x2 > x1)
  5604. ||(x1 < x2 && y1 < y2))
  5605. {
  5606. nStream = DOWN_STREAM;
  5607. }
  5608. else
  5609. {
  5610. nStream = UP_STREAM;
  5611. }
  5612. return nStream;
  5613. }
  5614. /*
  5615. * 根据上一次的速度计算本次的新坐标
  5616. *
  5617. * param
  5618. * pos 定位坐标
  5619. * v 上一次的运动速度
  5620. *
  5621. * return
  5622. * 成功返回0,并将计算出的新坐标保存到pos结果中
  5623. *
  5624. */
  5625. int Card::CalcPositionBySpeed(std::shared_ptr<POS>& pos,double v)
  5626. {
  5627. double distance = 0;
  5628. distance = v*pos->diff_reader_sync_num/3.6; //转为m/s
  5629. //如果两次定位的卡的ct号相差1
  5630. if (abs(time_stamp_cal - time_stamp_last) == 1)
  5631. {
  5632. //时间按0.2s计算
  5633. //速度如果按30km/h计算,则位移为1.6米左右
  5634. //速度如果按20km/h计算,则位移为1.1米左右
  5635. //速度如果按10km/h计算,则位移为0.5米左右
  5636. distance = 1;
  5637. }
  5638. //x相等
  5639. if (abs(pos->cx - last_locate.x) < ZERO_PRECISION)
  5640. {
  5641. int nSign = 0;
  5642. /*if (last_locate.y > pos->cy)
  5643. {
  5644. nSign = 1;
  5645. }
  5646. if (last_locate.y < pos->cy)
  5647. {
  5648. nSign = -1;
  5649. }*/
  5650. if (this->m_nStream == DOWN_STREAM)
  5651. {
  5652. //下行
  5653. //pos->cy = nSign*(last_locate.y + distance/map_scale);
  5654. pos->cy = last_locate.y + distance/map_scale;
  5655. }else if(this->m_nStream == UP_STREAM){
  5656. //上行
  5657. //pos->cy = nSign*(last_locate.y - distance/map_scale);
  5658. pos->cy = last_locate.y - distance/map_scale;
  5659. }
  5660. }else{
  5661. //在有斜率的地方
  5662. double arg = atan((pos->cy - last_locate.y)/(pos->cx - last_locate.x));
  5663. double calc_x = 0,calc_y = 0;
  5664. if (this->m_nStream == DOWN_STREAM)
  5665. {
  5666. //下行
  5667. calc_x = last_locate.x + cos(arg)*distance/map_scale;
  5668. calc_y = last_locate.y + sin(arg)*distance/map_scale;
  5669. }else if(this->m_nStream == UP_STREAM){
  5670. //上行
  5671. calc_x = last_locate.x - cos(arg)*distance/map_scale;
  5672. calc_y = last_locate.y - sin(arg)*distance/map_scale;
  5673. }
  5674. pos->cx = calc_x;
  5675. pos->cy = calc_y;
  5676. }
  5677. pos->posx = pos->cx*map_scale;
  5678. pos->posy = pos->cy*map_scale;
  5679. return 0;
  5680. }
  5681. /*
  5682. * 保存原始定位信息
  5683. *
  5684. * param
  5685. * pos 定位结果
  5686. *
  5687. * return
  5688. * 如果成功则为零
  5689. */
  5690. int Card::SaveOriginDataBeforeFilter(std::shared_ptr<POS> pos)
  5691. {
  5692. origin_locate.x = pos->cx;
  5693. origin_locate.y = pos->cy;
  5694. origin_locate.z = pos->cz;
  5695. origin_locate.v = sqrt(pow(pos->cvx,2) + pow(pos->cvy,2))*3.6; //*3.6转为km/h
  5696. origin_locate.sync_num = this->m_nCalcSyncNum;
  5697. return 0;
  5698. }
  5699. /*
  5700. * 从多解中选择合适的解,会有两种结果:1.选出一个合适解;2.选不出合适解
  5701. *
  5702. * param
  5703. * pRdm 分站定位数据集
  5704. * udm_pos 多个可能解
  5705. * pos 保存最终解
  5706. *
  5707. * return
  5708. * 选出合适解,将解保存到pos参数中,并返回0;选不出解,则返回非零错误码
  5709. *
  5710. */
  5711. int Card::ChooseOneSolution(std::shared_ptr<ReceiveDataMap> pRdm, std::vector<std::shared_ptr<POS>> udm_pos, std::shared_ptr<POS>& pos )
  5712. {
  5713. if(0 == udm_pos.size()){ // 没有解
  5714. return SOLUTION_NO_SOLUTION;
  5715. }
  5716. // 只有一个解
  5717. if(1 == udm_pos.size()){
  5718. //判断此位置距离分站的距离是否为4米范围内
  5719. ReceiveDataMap::iterator it_first = pRdm->end();
  5720. ReceiveDataMap::iterator it_second = pRdm->end();
  5721. for (ReceiveDataMap::iterator it = pRdm->begin();it != pRdm->end();++it)
  5722. {
  5723. if (it->second->reader_id == udm_pos.at(0)->nFirstReader)
  5724. {
  5725. it_first = it;
  5726. }
  5727. if (it->second->reader_id == udm_pos.at(0)->nSecondReader)
  5728. {
  5729. it_second = it;
  5730. }
  5731. }
  5732. if(it_first == pRdm->end() || it_second == pRdm->end())
  5733. {
  5734. return SOLUTION_NOT_EXIST_READRE;
  5735. }
  5736. double dist = 0.0;
  5737. if (it_first != pRdm->end())
  5738. {
  5739. dist = sqrt(pow(udm_pos.at(0)->posx - it_first->second->x,2)+pow(udm_pos.at(0)->posy - it_first->second->y,2));
  5740. if (dist < NEAR_READER && it_first->second->special == 0)
  5741. {
  5742. return SOLUTION_NEAR_READER;
  5743. }
  5744. }
  5745. if (it_second != pRdm->end())
  5746. {
  5747. dist = sqrt(pow(udm_pos.at(0)->posx - it_second->second->x,2)+pow(udm_pos.at(0)->posy - it_second->second->y,2));
  5748. if (dist < NEAR_READER && it_second->second->special == 0)
  5749. {
  5750. return SOLUTION_NEAR_READER;
  5751. }
  5752. }
  5753. pos->posx = udm_pos.at(0)->posx;
  5754. pos->posy = udm_pos.at(0)->posy;
  5755. pos->posz = udm_pos.at(0)->posz;
  5756. pos->cx = pos->posx / (this->map_scale*1.0);
  5757. pos->cy = pos->posy / (this->map_scale*1.0);
  5758. pos->cz = pos->posz / (this->map_scale*1.0);
  5759. TRACE(_T("solutions: ct:%d, r1: %d, r2:%d, x:%.4f y:%.4f\r\n"), this->time_stamp_cal, it_first->second->reader_id, it_second->second->reader_id, pos->cx, pos->cy);
  5760. return 0;
  5761. }
  5762. ReceiveDataMap::iterator itf = pRdm->begin();
  5763. int bf = itf->second->reader_id;
  5764. std::advance(itf,1);
  5765. int bs = itf->second->reader_id;
  5766. // 有多个解,含两解或两解以上
  5767. //判断依据:选取无地图集的分站数据来校正所有的解,
  5768. //解可靠的条件如下:1解与无地图集的时间戳距离差最小,2且此距离差不大于10米
  5769. for (ReceiveDataMap::reverse_iterator it_first = pRdm->rbegin();it_first!=pRdm->rend();++it_first)
  5770. {
  5771. ReceiveDataMap::reverse_iterator it_second = it_first;
  5772. std::advance(it_second,1);
  5773. if (it_second == pRdm->rend())
  5774. {
  5775. //找到尾部了还未找到合适解,即认为无解
  5776. pos->reason = 1;
  5777. break;
  5778. }
  5779. //反向查找,还要去掉这两个之间有地图集的数据
  5780. int nfr = it_first->second->reader_id;
  5781. int nfs = it_second->second->reader_id;
  5782. if (bf!=nfs && bs!=nfr)
  5783. {
  5784. if (IsExistPath(nfr,nfs))
  5785. {
  5786. continue;
  5787. }
  5788. }
  5789. int nCounts = 0;
  5790. for (std::vector<std::shared_ptr<POS>>::iterator it_pos = udm_pos.begin();it_pos != udm_pos.end();++it_pos)
  5791. {
  5792. //此点在这两个线段内,才允许使用此判断条件
  5793. _point p,l1,l2;
  5794. p.x = (*it_pos)->posx;
  5795. p.y = (*it_pos)->posy;
  5796. l1.x = it_first->second->x;
  5797. l1.y = it_first->second->y;
  5798. l2.x = it_second->second->x;
  5799. l2.y = it_second->second->y;
  5800. TRACE(_T("solutions: ct:%d, r1: %d, r2:%d, x:%.4f y:%.4f\r\n"), this->time_stamp_cal, it_first->second->reader_id, it_second->second->reader_id, p.x/2.0, p.y/2.0);
  5801. if (!LocateAlgorithm::IsInLine(p,l1,l2))
  5802. {
  5803. nCounts++;
  5804. continue;
  5805. }
  5806. double distance_reader = 0;
  5807. distance_reader = fabs(sqrt(
  5808. pow((*it_pos)->posx - it_first->second->x,2) + pow((*it_pos)->posy - it_first->second->y,2)
  5809. ) -
  5810. sqrt(
  5811. pow((*it_pos)->posx - it_second->second->x,2) + pow((*it_pos)->posy - it_second->second->y,2)
  5812. ));
  5813. long long dt = it_first->second->rec_time_stamp - it_second->second->rec_time_stamp;
  5814. double distance_tt = 0;
  5815. distance_tt = fabs(CFunctions::getDistance(dt,CFunctions::TDOA));
  5816. (*it_pos)->dis_diff = fabs(distance_reader - distance_tt);
  5817. }
  5818. if (nCounts == udm_pos.size())
  5819. {
  5820. continue;
  5821. }
  5822. double dMinDiff = 99999.9;
  5823. std::vector<std::shared_ptr<POS>>::iterator it_find = udm_pos.end();
  5824. for (std::vector<std::shared_ptr<POS>>::iterator it_pos = udm_pos.begin();it_pos != udm_pos.end();++it_pos)
  5825. {
  5826. if ((*it_pos)->dis_diff < dMinDiff)
  5827. {
  5828. _point p,l1,l2;
  5829. p.x = p.y = p.z = l1.x = l1.y = l1.z = l2.x = l2.y = l2.z = 0;
  5830. p.x = (*it_pos)->posx;
  5831. p.y = (*it_pos)->posy;
  5832. for (ReceiveDataMap::iterator it = pRdm->begin();it != pRdm->end();++it)
  5833. {
  5834. if (it->second->reader_id == (*it_pos)->nFirstReader)
  5835. {
  5836. l1.x = it->second->x;
  5837. l1.y = it->second->y;
  5838. }
  5839. if (it->second->reader_id == (*it_pos)->nSecondReader)
  5840. {
  5841. l2.x = it->second->x;
  5842. l2.y = it->second->y;
  5843. }
  5844. }
  5845. if (!LocateAlgorithm::IsInLine(p,l1,l2))
  5846. {
  5847. continue;
  5848. }else{
  5849. if (((*it_pos)->nFirstReader == nfr &&(*it_pos)->nSecondReader == nfs)||
  5850. ((*it_pos)->nFirstReader == nfs &&(*it_pos)->nSecondReader == nfr))
  5851. {
  5852. continue;
  5853. }else{
  5854. dMinDiff = (*it_pos)->dis_diff;
  5855. it_find = it_pos;
  5856. }
  5857. }
  5858. }
  5859. }
  5860. if (it_find != udm_pos.end())
  5861. {
  5862. if ((*it_find)->dis_diff < MIN_DIFFER_DISTANCE)
  5863. {
  5864. //找到即退出
  5865. //pos = *it_find;
  5866. pos->posx = (*it_find)->posx;
  5867. pos->posy = (*it_find)->posy;
  5868. pos->posz = (*it_find)->posz;
  5869. pos->reason = 0;
  5870. break;
  5871. }
  5872. }
  5873. }
  5874. if (pos->reason == 0)
  5875. {
  5876. pos->cx = pos->posx / (this->map_scale*1.0);
  5877. pos->cy = pos->posy / (this->map_scale*1.0);
  5878. pos->cz = pos->posz / (this->map_scale*1.0);
  5879. }
  5880. // 未计算出结果
  5881. if(pos->posx == INVALID_COORDINATE || pos->posy == INVALID_COORDINATE){
  5882. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_148);
  5883. return SOLUTION_NO_SOLUTION;
  5884. }else{
  5885. bool bExistSolution = true;
  5886. //对这唯一解最最后一步筛选,在分站1米内
  5887. //增加这个条件主要是为了解决,当距离最近的分站数据未收到的情况,
  5888. //比如实际数据应该是103,102,101,,但目前收到的数据为102,101,但定位到102附近且在102和101之间
  5889. for (ReceiveDataMap::iterator it = pRdm->begin();it != pRdm->end();++it)
  5890. {
  5891. double distance = 0.0;
  5892. distance = sqrt(pow(it->second->x - pos->posx,2) + pow(it->second->y - pos->posy,2));
  5893. if (distance < 1)
  5894. {
  5895. bExistSolution = false;
  5896. break;
  5897. }
  5898. }
  5899. //无解
  5900. if (!bExistSolution)
  5901. {
  5902. std::shared_ptr<POS> tmp = std::make_shared<POS>();
  5903. pos->posx = tmp->posx;
  5904. pos->posy = tmp->posy;
  5905. pos->cx = 0;
  5906. pos->cy = 0;
  5907. pos->cz = 0;
  5908. return SOLUTION_NO_SOLUTION;
  5909. }
  5910. }
  5911. return 0;
  5912. }
  5913. int Card::GetDeltaT( map<unsigned long long,std::shared_ptr<_coordinate>> dl )
  5914. {
  5915. unsigned long long revTime = 0;
  5916. int rid = 0, sn = 0, ct = 0;
  5917. map<unsigned long long,std::shared_ptr<_coordinate>>::iterator it_mpdl = dl.begin();
  5918. int i = 0;
  5919. for(; it_mpdl != dl.end(); ++it_mpdl){
  5920. if(0 == it_mpdl->second->tt){
  5921. continue;
  5922. }
  5923. if(this->locate && it_mpdl->second->reader_id == this->locate->ReaderId()){
  5924. revTime = it_mpdl->second->tt;
  5925. rid = it_mpdl->second->reader_id;
  5926. sn = it_mpdl->second->sync_num;
  5927. ct = it_mpdl->second->t;
  5928. break;
  5929. }
  5930. if(0 == revTime){
  5931. revTime = it_mpdl->second->tt;
  5932. rid = it_mpdl->second->reader_id;
  5933. sn = it_mpdl->second->sync_num;
  5934. ct = it_mpdl->second->t;
  5935. }
  5936. else if( revTime < it_mpdl->second->tt){ // 跨周期,可能会取到较大值
  5937. revTime = it_mpdl->second->tt;
  5938. rid = it_mpdl->second->reader_id;
  5939. sn = it_mpdl->second->sync_num;
  5940. ct = it_mpdl->second->t;
  5941. }
  5942. }
  5943. std::shared_ptr<nspLocate::LocateRecord> loc = std::make_shared<nspLocate::LocateRecord>(revTime, rid, sn, ct);
  5944. loc->getDeltaT(this->locate);
  5945. this->locate = loc;
  5946. return 0;
  5947. }
  5948. /*
  5949. * 获得tdoa和tof 卡的时间差
  5950. *
  5951. * param
  5952. * 无
  5953. *
  5954. * return
  5955. * 返回值为计算后的时间差
  5956. *
  5957. */
  5958. double Card::GetDeltaT()
  5959. {
  5960. double dt = 0;
  5961. switch (ranging_type)
  5962. {
  5963. case LOCATEDATATYPE::LDT_TOF:
  5964. dt = difftime(p_reader->reader_time,time_last_tof);
  5965. break;
  5966. case LOCATEDATATYPE::LDT_TDOA:
  5967. //存在分站在计数序号为1000掉电,但重新上电会导致此时的计算值偏大
  5968. if (m_nCalcSyncNum - this->last_locate.sync_num < 0 && m_nCalcSyncNum < 100)
  5969. {
  5970. dt = (m_nCalcSyncNum + 65535 - this->last_locate.sync_num)*READER_SYNC_INTERVAL_TIME;
  5971. }else{
  5972. dt = (m_nCalcSyncNum - this->last_locate.sync_num)*READER_SYNC_INTERVAL_TIME;
  5973. }
  5974. if (abs(dt) < ZERO_PRECISION)
  5975. {
  5976. dt = 0.2;
  5977. }
  5978. break;
  5979. default:
  5980. break;
  5981. }
  5982. return dt;
  5983. }
  5984. void Card::PosState( int val )
  5985. {
  5986. pos_state = val;
  5987. pos_state_count = pos_state_confirm_times;
  5988. }
  5989. int const Card::PosState()
  5990. {
  5991. if(pos_state_count >= pos_state_confirm_times )
  5992. {
  5993. return pos_state;
  5994. }
  5995. return pos_state_old;
  5996. }
  5997. void Card::PosStateOld( int val )
  5998. {
  5999. pos_state_old = val;
  6000. }
  6001. // 只显示优先级最高的状态
  6002. int const Card::StateBiz()
  6003. {
  6004. state_biz = 0;
  6005. if(status_help == STATUS_ERROR){
  6006. state_biz = STATUS_HELP;
  6007. return state_biz;
  6008. }
  6009. if(status_lost == STATUS_LOST){
  6010. state_biz = STATUS_LOST;
  6011. return state_biz;
  6012. }
  6013. if(status_area_forbidden == STATUS_ERROR){
  6014. state_biz += STATUS_AREA_FORBIDDEN;
  6015. return state_biz;
  6016. }
  6017. if(status_area_over_speed == STATUS_ERROR){
  6018. state_biz = STATUS_AREA_OVER_SPEED;
  6019. return state_biz;
  6020. }
  6021. if(status_over_speed == STATUS_ERROR){
  6022. state_biz = STATUS_OVER_SPEED;
  6023. return state_biz;
  6024. }
  6025. if(status_area_over_time == STATUS_ERROR){
  6026. state_biz = STATUS_AREA_OVER_TIME;
  6027. return state_biz;
  6028. }
  6029. if(status_area_over_time == STATUS_ERROR){
  6030. state_biz = STATUS_OVER_TIME;
  6031. return state_biz;
  6032. }
  6033. if(status_call == STATUS_ERROR){
  6034. state_biz = STATUS_CALL;
  6035. return state_biz;
  6036. }
  6037. return state_biz;
  6038. }
  6039. void Card::set_reader_path(std::shared_ptr<ReaderPathMap> rpm, std::shared_ptr<TDOAReaderPathMap> trpm)
  6040. {
  6041. this->set_reader_path_tof(rpm);
  6042. this->set_reader_path_tdoa(trpm);
  6043. }
  6044. MapInfo::MapInfo( void )
  6045. {
  6046. }
  6047. MapInfo::~MapInfo( void )
  6048. {
  6049. }
  6050. Dept::Dept( int id, string name )
  6051. {
  6052. dept_id = id;
  6053. dept_name = name;
  6054. }
  6055. OccLevel::OccLevel(int id )
  6056. {
  6057. occlevel_id = id;
  6058. }
  6059. Adhoc::Adhoc()
  6060. {
  6061. adhoc_id = 0;
  6062. x = 0;
  6063. y = 0;
  6064. z = 0;
  6065. idx = 0;
  6066. }
  6067. Adhoc::~Adhoc()
  6068. {
  6069. }
  6070. Light::Light()
  6071. {
  6072. m_nID = m_nMapID = m_nAreaID = m_nLightsGroupID = m_nReaderID = m_nSectionID = m_nPort = m_nState = m_nStream = 0;
  6073. device_type_id = 0;
  6074. m_nOldState = 0;
  6075. m_nShape = 0;
  6076. x = y = z = 0.0;
  6077. m_nIsCtrl = LIGHT_CTRL_STATE::CTRL_STATE_AUTO;
  6078. m_strIP = m_strName = m_strLabel = "";
  6079. rec_time = time(NULL);
  6080. alarm_start_time = time(NULL);
  6081. last_send_time = time(NULL);
  6082. }
  6083. int Light::get_light_state()
  6084. {
  6085. return m_nState;
  6086. }
  6087. Chamber::Chamber()
  6088. {
  6089. m_nID = m_nIsUsed = m_nMapID = m_nState = m_nSectionID = 0;
  6090. m_strPath = m_strName = m_strLabel = "";
  6091. }
  6092. Chamber::~Chamber()
  6093. {
  6094. }
  6095. /*
  6096. * 初始化区域边界
  6097. *
  6098. * param
  6099. * path 边界字符串描述
  6100. *
  6101. * return
  6102. * 成功返回0,失败返回1
  6103. */
  6104. int BaseArea::init_border(string path)
  6105. {
  6106. if(path == "" || path.find(',') == std::string::npos){
  6107. return 1;
  6108. }
  6109. std::vector<std::string> vec = Split(path, " ");
  6110. std::vector<std::string>::iterator it = vec.begin();
  6111. if(m_pPolygon){
  6112. delete[] m_pPolygon;
  6113. m_pPolygon = NULL;
  6114. }
  6115. m_pPolygon = new _point[vec.size()];
  6116. m_nPolygonCount = 0;
  6117. for(; it != vec.end(); ++it){
  6118. std::vector<std::string> subvec = Split(it->c_str(), ",");
  6119. _point p;
  6120. p.x = GetVertex(subvec[0]);
  6121. p.y = GetVertex(subvec[1]);
  6122. p.z = 0;
  6123. m_pPolygon[m_nPolygonCount] = p;
  6124. m_nPolygonCount++;
  6125. }
  6126. return 0;
  6127. }
  6128. bool BaseArea::IsInPolygon(_point p)
  6129. {
  6130. if(m_pPolygon == NULL){
  6131. return false;
  6132. }
  6133. int counter = 0;
  6134. int i;
  6135. double xinters;
  6136. _point p1,p2;
  6137. p1 = m_pPolygon[0];
  6138. for (int i=1;i<= m_nPolygonCount;i++) {
  6139. p2 = m_pPolygon[i % m_nPolygonCount];
  6140. if (p.y > MIN(p1.y,p2.y)) {
  6141. if (p.y <= MAX(p1.y,p2.y)) {
  6142. if (p.x <= MAX(p1.x,p2.x)) {
  6143. if (p1.y != p2.y) {
  6144. xinters = (p.y-p1.y)*(p2.x-p1.x)/(p2.y-p1.y)+p1.x;
  6145. if (p1.x == p2.x || p.x <= xinters)
  6146. counter++;
  6147. }
  6148. }
  6149. }
  6150. }
  6151. p1 = p2;
  6152. }
  6153. return (counter % 2 == 0) ? false : true;
  6154. }
  6155. double BaseArea::GetVertex(std::string src)
  6156. {
  6157. std::string dest = "";
  6158. for(unsigned int i = 0; i < src.length(); i++){
  6159. if((src[i] >= '0' && src[i]<='9') || src[i]=='-' || src[i] == '.'){
  6160. dest += src[i];
  6161. }
  6162. }
  6163. return atof(dest.c_str());
  6164. }
  6165. std::vector<std::string> BaseArea::Split(std::string str,std::string pattern)
  6166. {
  6167. std::string::size_type pos;
  6168. std::vector<std::string> result;
  6169. str+=pattern;//扩展字符串以方便操作
  6170. unsigned int size=str.size();
  6171. for(unsigned int i=0; i<size; i++){
  6172. pos=str.find(pattern,i);
  6173. if(pos<size){
  6174. std::string s=str.substr(i,pos-i);
  6175. result.push_back(s);
  6176. i=pos+pattern.size()-1;
  6177. }
  6178. }
  6179. return result;
  6180. }
  6181. Section::Section()
  6182. {
  6183. m_nID = m_nMapId = m_nState = 0;
  6184. m_strName = m_strLabel = m_strPath = "";
  6185. for(int i = 0; i < SECTION_EVENT_COUNT; i++){
  6186. m_event_list[i] = 0;
  6187. }
  6188. }
  6189. bool Section::is_has_chamber()
  6190. {
  6191. bool bIsHasEmptyChamber = false;
  6192. if (mp_champer_list.size()<=0)
  6193. {
  6194. return bIsHasEmptyChamber;
  6195. }
  6196. ChamberMap::iterator it = mp_champer_list.begin();
  6197. for (;it!=mp_champer_list.end();++it)
  6198. {
  6199. if (it->second->m_nIsUsed == false)
  6200. {
  6201. bIsHasEmptyChamber = true;
  6202. break;
  6203. }
  6204. }
  6205. return bIsHasEmptyChamber;
  6206. }
  6207. int Section::get_section_state()
  6208. {
  6209. int nTotals = 0;
  6210. nTotals = mp_vehicle_list.size();
  6211. switch (nTotals)
  6212. {
  6213. case 0:
  6214. m_nState = SECTION_STATE_NORMAL;
  6215. break;
  6216. case 1:
  6217. m_nState = SECTION_STATE_BUSY;
  6218. break;
  6219. case 2:
  6220. m_nState = SECTION_STATE_CONGESTION;
  6221. break;
  6222. default:
  6223. m_nState = SECTION_STATE_CONGESTION;
  6224. break;
  6225. }
  6226. return m_nState;
  6227. }
  6228. int Section::get_section_vehicle_counts()
  6229. {
  6230. return mp_vehicle_list.size();
  6231. }
  6232. int Section::get_section_staffer_counts()
  6233. {
  6234. return mp_staffer_list.size();
  6235. }
  6236. LightsGroup::LightsGroup()
  6237. {
  6238. m_bIsUsed = false;
  6239. m_nID = m_nState = 0;
  6240. m_strLabel = m_strName = m_strVechileId = "";
  6241. }
  6242. /*
  6243. * 判断红绿灯组中是否有此灯
  6244. *
  6245. * param
  6246. * pLight 灯对象
  6247. *
  6248. * return
  6249. * 存在返回true,不存在返回false
  6250. */
  6251. bool LightsGroup::isExist(std::shared_ptr<Light> pLight)
  6252. {
  6253. bool bRet = false;
  6254. for (LightMap::iterator it = mp_lights_list.begin();it != mp_lights_list.end();++it)
  6255. {
  6256. //如果红绿灯的id,地图id,路段id,都和参数一致,且灯状态可用,即为找到
  6257. if (it->second->m_nID == pLight->m_nID
  6258. && it->second->m_nMapID == pLight->m_nMapID
  6259. && it->second->m_nSectionID == pLight->m_nSectionID
  6260. && it->second->m_nState == 0)
  6261. {
  6262. bRet = true;
  6263. }
  6264. }
  6265. return bRet;
  6266. }
  6267. /*
  6268. * 判断灯组内灯的颜色是否一致
  6269. *
  6270. * param
  6271. * nColor 指定颜色(红色或绿色)
  6272. *
  6273. * return
  6274. * 相同返回true,否则返回false
  6275. *
  6276. */
  6277. bool LightsGroup::isAllLightColor(int nColor)
  6278. {
  6279. bool bRet = true;
  6280. for (LightMap::iterator it = mp_lights_list.begin();it != mp_lights_list.end();++it)
  6281. {
  6282. if (it->second->m_nColor != nColor)
  6283. {
  6284. bRet = false;
  6285. }
  6286. }
  6287. return bRet;
  6288. }
  6289. PatrolTask::PatrolTask()
  6290. {
  6291. patrol_task_id = patrol_path_id = 0;
  6292. card_id = "";
  6293. starffer_id = "";
  6294. start_time = end_time = time(NULL);
  6295. mpPoint.swap(map<unsigned int, std::shared_ptr<PatrolPoint>>());
  6296. cur_point_idx = 1;
  6297. enter_time = leave_time = time(NULL);
  6298. state = stay_state = duration_stay = 0;
  6299. is_in_cur_point = false;
  6300. }
  6301. PatrolTask::~PatrolTask()
  6302. {
  6303. }
  6304. PatrolPoint::PatrolPoint()
  6305. {
  6306. idx = 0;
  6307. patrol_point_id = 0;
  6308. map_id = 0;
  6309. x = y = z = 0;
  6310. ranging = 0; // 距离范围
  6311. duration_last = duration_stay_min = duration_stay_max = duration_ranging = 0;
  6312. }
  6313. PatrolPoint::~PatrolPoint()
  6314. {
  6315. }
  6316. BanShift::BanShift()
  6317. {
  6318. }
  6319. BanShift::BanShift( int id, std::string s, std::string e, int tid )
  6320. {
  6321. shift_id = id;
  6322. start_time = s;
  6323. end_time = e;
  6324. shift_type_id = tid;
  6325. }
  6326. BanShift::~BanShift()
  6327. {
  6328. }
  6329. /*
  6330. // 模仿matlab线性拟合工具,返回y=ax+b,并且输出R:相关系数,0不相关,1很相关
  6331. // 返回值代表:是否怠速静止
  6332. // by lsp on 07.10.2017 @山西长治
  6333. */
  6334. bool Card::cftool(float *data_x, float *data_y, int data_n, std::vector<float> &vResult)
  6335. {
  6336. //如果数组中所有元素相等,那么直接宣布是怠速
  6337. int flag = 0; // flag 表示是否遇到了不相等的元素
  6338. int i;
  6339. for (i = 1; i < data_n; i++) {
  6340. if (data_y[i] != data_y[0]) { // 遇到了不等于x[0]的元素,设置 flag = 1,然后跳出循环
  6341. flag = 1; break;
  6342. }
  6343. }
  6344. if (flag == 1) { // 输出判断结果
  6345. printf("不相等,继续调用线性拟合方法判断是否是怠速\n");
  6346. } else {
  6347. return true;
  6348. }
  6349. float A = 0.0;
  6350. float B = 0.0;
  6351. float C = 0.0;
  6352. float D = 0.0;
  6353. float E = 0.0;
  6354. float F = 0.0;
  6355. for (int i=0; i<data_n; i++)
  6356. {
  6357. A += data_x[i] * data_x[i];
  6358. B += data_x[i];
  6359. C += data_x[i] * data_y[i];
  6360. D += data_y[i];
  6361. }
  6362. // 计算斜率a和截距b
  6363. float a, b, temp = 0;
  6364. if( temp = (data_n*A - B*B) )// 判断分母不为0
  6365. {
  6366. a = (data_n*C - B*D) / temp;
  6367. b = (A*D - B*C) / temp;
  6368. }
  6369. else
  6370. {
  6371. a = 1;
  6372. b = 0;
  6373. }
  6374. // 计算相关系数r
  6375. float Xmean, Ymean;
  6376. Xmean = B / data_n;
  6377. Ymean = D / data_n;
  6378. float tempSumXX = 0.0, tempSumYY = 0.0;
  6379. for (int i=0; i<data_n; i++)
  6380. {
  6381. tempSumXX += (data_x[i] - Xmean) * (data_x[i] - Xmean);
  6382. tempSumYY += (data_y[i] - Ymean) * (data_y[i] - Ymean);
  6383. E += (data_x[i] - Xmean) * (data_y[i] - Ymean);
  6384. }
  6385. F = sqrt(tempSumXX) * sqrt(tempSumYY);
  6386. float r;
  6387. r = E / F;
  6388. vResult.push_back(a);
  6389. vResult.push_back(b);
  6390. vResult.push_back(r*r);
  6391. fit_r = vResult.at(2);
  6392. if(vResult.at(2) < 0.7)
  6393. return true;
  6394. else
  6395. return false;
  6396. }
  6397. /*
  6398. //by lsp on 2017.07.10 @山西长治高河调度室
  6399. //by lsp on 2017.07.10 @山西长治
  6400. //让上帝来决定下一个格子怎么走吧!
  6401. //int返回值:1表示失败,并且返回的cell也会为空
  6402. // 0表示成功,并且会返回正确位置的cell
  6403. //给我一个格子
  6404. */
  6405. int Card::colourfulCloudsChasingTheMoon(std::shared_ptr<Cell> &cell, std::list<std::shared_ptr<Cell>> &myCellList){
  6406. try{
  6407. if (cell && cell->id != 0)
  6408. {
  6409. //入队列,用的algorithm文件中的MAX_FIT_DATA_COUNTS常量,现在定义的是5
  6410. if (myCellList.size() >= MAX_FIT_DATA_COUNTS)
  6411. {
  6412. myCellList.pop_front();
  6413. myCellList.push_back(cell); //格子入栈
  6414. }else{
  6415. //只入栈
  6416. myCellList.push_back(cell); //格子入栈
  6417. return 1;
  6418. }
  6419. float* pX = new float[MAX_FIT_DATA_COUNTS];
  6420. float* pY = new float[MAX_FIT_DATA_COUNTS];
  6421. memset(pX, 0, MAX_FIT_DATA_COUNTS*sizeof(float));
  6422. memset(pY, 0, MAX_FIT_DATA_COUNTS*sizeof(float));
  6423. size_t i = 0;
  6424. if (myCellList.size() == MAX_FIT_DATA_COUNTS){
  6425. //构造his_cell中的x, y队列
  6426. //x用ct值来表示
  6427. //ct存在:cell->card_stamp_time
  6428. float step; //把坐标往前挪
  6429. std::list<std::shared_ptr<Cell>>::iterator first_cell = myCellList.begin(); //后续总是跟第一个点比较距离
  6430. for (std::list<std::shared_ptr<Cell>>::iterator it = myCellList.begin(); it != myCellList.end() ; ++it)
  6431. {
  6432. if(it == first_cell){
  6433. pY[i] = (*it)->id; //第一个值,当然直接入数组,而且后面的值都要向它看齐
  6434. }else{
  6435. if ((*first_cell)->strSubjectReader == (*it)->strSubjectReader){
  6436. pY[i] = (*it)->id; //跟前者在相同基站之间
  6437. }else
  6438. {
  6439. int last_min = (*first_cell)->minReaderId; //last_cell_temp对应的最小分站
  6440. int last_max = (*first_cell)->maxReaderId; //last_cell_temp对应的最大分站
  6441. int cell_min = (*it)->minReaderId; //cell对应的最小分站
  6442. int cell_max = (*it)->maxReaderId; //cell对应的最大分站
  6443. if(last_min == cell_min){
  6444. //pY[i] = pY[i - 1] + pY[i - 1] - 1 + (*it)->id; //换基站了
  6445. pY[i] = 0 - (*it)->id;
  6446. }
  6447. if(last_min == cell_max){
  6448. //pY[i] = pY[i - 1] + pY[i - 1] - 1 + mpCellPath->find((*it)->strSubjectReader)->second.size() - (*it)->id + 1; //换基站了
  6449. int nSize = 0;
  6450. nSize = mpCellPath->find((*it)->strSubjectReader)->second.size();
  6451. int nDiff = (*it)->id - nSize;
  6452. float a = nDiff*1.0;
  6453. pY[i] = a;
  6454. //pY[i] = (*it)->id - mpCellPath->find((*it)->strSubjectReader)->second.size();
  6455. }
  6456. if(last_max == cell_min){
  6457. pY[i] = mpCellPath->find((*first_cell)->strSubjectReader)->second.size() + (*it)->id; //换基站了
  6458. }
  6459. if(last_max == cell_max){
  6460. pY[i] = mpCellPath->find((*first_cell)->strSubjectReader)->second.size() + mpCellPath->find((*it)->strSubjectReader)->second.size() - (*it)->id + 1; //换基站了
  6461. }
  6462. }
  6463. }
  6464. //设置横坐标
  6465. if(i == 0){
  6466. pX[i] = 1;
  6467. step = (*it)->card_stamp_time - 1;
  6468. }else{
  6469. if((*it)->card_stamp_time - step < 0){
  6470. pX[i] = (*it)->card_stamp_time - step + 65535;
  6471. }else{
  6472. pX[i] = (*it)->card_stamp_time - step;
  6473. }
  6474. }
  6475. i++;
  6476. }
  6477. //上面构造完毕,检测其线性相关性
  6478. std::vector<float> vResult;
  6479. vResult.resize(0);
  6480. //不做怠速判断
  6481. bool cft = cftool(pX, pY, MAX_FIT_DATA_COUNTS, vResult);
  6482. cft = false;
  6483. float k = 0.0;
  6484. k = vResult.at(0);
  6485. float b = 0.0;
  6486. b = vResult.at(1);
  6487. last_k = 0;
  6488. last_k = fit_k;
  6489. std::list<std::shared_ptr<Cell>>::reverse_iterator it_cell = myCellList.rbegin();
  6490. int first_ct = 0, second_ct = 0;
  6491. if (it_cell != myCellList.rend())
  6492. {
  6493. first_ct = (*it_cell)->card_stamp_time;
  6494. ++it_cell;
  6495. if (it_cell != myCellList.rend())
  6496. {
  6497. second_ct = (*it_cell)->card_stamp_time;
  6498. }
  6499. }
  6500. delta_ct = first_ct - second_ct;
  6501. fit_k = k;
  6502. fit_b = b;
  6503. float Relation = 0.0;
  6504. Relation = vResult.at(2);
  6505. //如果是怠速,则当前cell的真实id=idlastcell的真实id
  6506. if(cft){
  6507. //如果怠速了,则直接取上一次的结果
  6508. Log::write_log(FILE_TYPE::SQL_S, "be quiet now!!!!original id: " + CFunctions::int2string(cell->id) + " realId: "+ CFunctions::int2string(cell->realId) + " cardid: " + this->card_id, true);
  6509. if (last_cell)
  6510. {
  6511. cell->realId = last_cell->realId; //据说是复制赋值
  6512. }else{
  6513. cell->realId = cell->id; //第一次没有last_cell,直接原始值为实际realId
  6514. }
  6515. }else{
  6516. std::list<std::shared_ptr<Cell>>::reverse_iterator end_cell = myCellList.rbegin();
  6517. //int realId = floor(pY[0] * k + b); //考虑到有可能跨基站,处理下形成realId
  6518. int realId = floor(pX[4] * k + b);
  6519. std::string strReaderName = "";
  6520. std::string tmpOriginReaderName = "";
  6521. int tmpOriginId = 0;
  6522. tmpOriginId = cell->originId;
  6523. tmpOriginReaderName = cell->strOriginReaderName;
  6524. if ((*first_cell)->strSubjectReader == (*end_cell)->strSubjectReader){
  6525. cell->realId = realId; //跟前者在相同基站之间
  6526. strReaderName = (*first_cell)->strSubjectReader;
  6527. }else
  6528. {
  6529. //换基站了
  6530. int first_min = (*first_cell)->minReaderId; //first_cell_temp对应的最小分站
  6531. int first_max = (*first_cell)->maxReaderId; //first_cell_temp对应的最大分站
  6532. int cell_min = (*end_cell)->minReaderId; //cell对应的最小分站
  6533. int cell_max = (*end_cell)->maxReaderId; //cell对应的最大分站
  6534. if(first_min == cell_min){
  6535. //cell->realId = realId - pY[0];
  6536. if (realId == 0)
  6537. {
  6538. cell->realId = 1;
  6539. strReaderName = (*end_cell)->strSubjectReader;
  6540. }else if(realId > 0){
  6541. cell->realId = realId;
  6542. strReaderName = (*first_cell)->strSubjectReader;
  6543. }else{
  6544. cell->realId = abs(realId);
  6545. strReaderName = (*end_cell)->strSubjectReader;
  6546. }
  6547. }
  6548. if(first_min == cell_max){
  6549. if (realId == 0)
  6550. {
  6551. cell->realId = mpCellPath->find((*end_cell)->strSubjectReader)->second.size();
  6552. strReaderName = (*end_cell)->strSubjectReader;
  6553. }else if(realId > 0){
  6554. //cell->realId = mpCellPath->find((*end_cell)->strSubjectReader)->second.size() - (realId - pY[0]) + 1;
  6555. cell->realId = realId;
  6556. strReaderName = (*first_cell)->strSubjectReader;
  6557. }else{
  6558. cell->realId = mpCellPath->find((*end_cell)->strSubjectReader)->second.size() + realId;
  6559. strReaderName = (*end_cell)->strSubjectReader;
  6560. }
  6561. }
  6562. if(first_max == cell_min){
  6563. //cell->realId = realId - (mpCellPath->find((*first_cell)->strSubjectReader)->second.size() - pY[0]);
  6564. //cell->realId = realId - mpCellPath->find((*first_cell)->strSubjectReader)->second.size();
  6565. //strReaderName = (*first_cell)->strSubjectReader;
  6566. if (realId >= mpCellPath->find((*first_cell)->strSubjectReader)->second.size())
  6567. {
  6568. cell->realId = realId - mpCellPath->find((*first_cell)->strSubjectReader)->second.size();
  6569. if (cell->realId == 0)
  6570. {
  6571. cell->realId = 1;
  6572. }
  6573. strReaderName = (*end_cell)->strSubjectReader;
  6574. }else{
  6575. cell->realId = mpCellPath->find((*first_cell)->strSubjectReader)->second.size() - realId;
  6576. strReaderName = (*first_cell)->strSubjectReader;
  6577. }
  6578. }
  6579. if(first_max == cell_max){
  6580. //cell->realId = (mpCellPath->find((*end_cell)->strSubjectReader))->second.size() - (realId - ((mpCellPath->find((*first_cell)->strSubjectReader))->second.size() - pY[0])) + 1;
  6581. //strReaderName = (*end_cell)->strSubjectReader;
  6582. if (realId >= mpCellPath->find((*first_cell)->strSubjectReader)->second.size())
  6583. {
  6584. cell->realId = mpCellPath->find((*end_cell)->strSubjectReader)->second.size() - (realId - mpCellPath->find((*first_cell)->strSubjectReader)->second.size());
  6585. strReaderName = (*end_cell)->strSubjectReader;
  6586. }
  6587. else
  6588. {
  6589. cell->realId = realId;
  6590. strReaderName = (*first_cell)->strSubjectReader;
  6591. }
  6592. }
  6593. }
  6594. cell = FindCell(strReaderName,cell->realId);
  6595. if (cell)
  6596. {
  6597. cell->originId = tmpOriginId;
  6598. cell->strOriginReaderName = tmpOriginReaderName;
  6599. }
  6600. }
  6601. }else{
  6602. cell = nullptr;
  6603. return 1; //1表示失败,并且返回的cell也会为空
  6604. }
  6605. }
  6606. return 0; //表示成功
  6607. }catch (...)
  6608. {
  6609. return 1;
  6610. }
  6611. }
  6612. //int Card::DiscreteLocate(int cnt)
  6613. //{
  6614. // try{
  6615. // ULONGLONG nowTickCounts = ::GetTickCount();
  6616. // //如果last_cell存在,那说明不是第一次定位,如果不存在,那么继续往下走。
  6617. // if (last_cell)
  6618. // {
  6619. // SYSTEMTIME curSt;
  6620. // ::GetLocalTime(&curSt);
  6621. //
  6622. // int ret = 0;
  6623. // ret = CFunctions::CompareSystemTime(curSt,last_cell->interval_time);
  6624. // if (ret < 0)
  6625. // {
  6626. // return 1;
  6627. // }
  6628. // }
  6629. //
  6630. // int ret = 0;
  6631. // std::shared_ptr<Cell> cell = nullptr;
  6632. // //std::shared_ptr<Cell> tmp_cell = std::make_shared<Cell>();
  6633. // is_fit_pos = false;
  6634. //
  6635. // //1.数据有效性判断
  6636. // ret = CheckDistData(cnt);
  6637. // if(ret){
  6638. // originCellId = 0;
  6639. // originCellReaderName = "";
  6640. // return 1;
  6641. // }else{
  6642. // //2.组装数据
  6643. // std::shared_ptr<ReceiveDataMap> pRdm = std::make_shared<ReceiveDataMap>();
  6644. // pRdm->clear();
  6645. // ret = AssembleDistData(pRdm);
  6646. // if(ret){
  6647. // return 1;
  6648. // }else{
  6649. // if (pRdm->size()<=0)
  6650. // {
  6651. // return 1;
  6652. // }
  6653. //
  6654. // std::shared_ptr<POS> pos = std::make_shared<POS>();
  6655. // //3.算法定位
  6656. // std::vector<std::shared_ptr<POS>> udm_pos;
  6657. // udm_pos.resize(0);
  6658. // ret = LocateAlgorithm::CalcTdoaPosition(pRdm,pTdoaReaderPathMap,udm_pos);
  6659. // //4.从多解中筛选出一个解,存在两种可能:
  6660. // //a.可能无解返回非0,
  6661. // //b.可能有解,但解不正确,比如解的位置在4727,-100,但选出的解是4727,-200
  6662. // ret = ChooseOneSolution(pRdm, udm_pos, pos);
  6663. //
  6664. // if (ret)
  6665. // {
  6666. // return 1;
  6667. // }else{
  6668. // std::shared_ptr<POS> tmp_pos = std::make_shared<POS>();
  6669. // //获取分站信息
  6670. // bool bRet = false;
  6671. // *tmp_pos = *pos;
  6672. //
  6673. // if (ret == 0)
  6674. // {
  6675. // //这里的cell->id是originalId
  6676. // cell = Position2Cell(tmp_pos);
  6677. // //从测试数据中读cell,送给彩云追月形成输出
  6678. // //by lsp on 2017.07.15 @北京林萃公寓
  6679. // //his_cell = nullptr;
  6680. // //int oiginalId[10] = {1023, 1016, 998, 988, 979, 974, 966, 960, 949, 938}; //原始cellId
  6681. // //int ct[10] ={9859, 9860, 9863, 9865, 9866, 9867, 9868, 9869, 9870, 9872}; //对应的ct号码
  6682. // //int firstStation[10] = {201, 201, 201, 201, 201, 201, 201, 201, 201, 201};
  6683. // //int endStation[10] = {202, 202, 202, 202, 202, 202, 202, 202, 202, 202};
  6684. // //std::list<std::shared_ptr<Cell>> myCellList;
  6685. // //myCellList.resize(0);
  6686. // //for (int i=0; i < 10; i++)
  6687. // //{
  6688. // // //初始化格子
  6689. // // std::shared_ptr<Cell> cell = std::make_shared<Cell>();
  6690. // // cell->id = oiginalId[i];
  6691. // // cell->card_stamp_time = ct[i];
  6692. // // cell->minReaderId = firstStation[i];
  6693. // // cell->maxReaderId = endStation[i];
  6694. // // cell->strSubjectReader = CFunctions::getCellName(firstStation[i], endStation[i]);
  6695. // // //调用彩云追月来处理
  6696. // // if(colourfulCloudsChasingTheMoon(cell, myCellList) == 0){
  6697. // // //输出结果
  6698. // // Log::write_log(FILE_TYPE::SQL_S, "ct: " + CFunctions::int2string(cell->card_stamp_time) + " station: " + cell->strSubjectReader + " original id: " + CFunctions::int2string(cell->id) + " realId: "+ CFunctions::int2string(cell->realId) + " cardid: " + this->card_id, true);
  6699. // // }
  6700. // //}
  6701. // //Log::write_log(FILE_TYPE::JSON_S, this->card_id + ": have postion2cell, go now!!!!!!!!!!!!!!!", true);
  6702. // //if (cell && cell->id == 0)
  6703. // //{
  6704. // // //问题是下面这一步不可能走过来,只有500ms补的地方才会出现originalId为0
  6705. // // Log::write_log(FILE_TYPE::JSON_S, this->card_id + ":my cell Id is Zero, so I have to go out!!!!!!!!!!!!!!!", true);
  6706. // // return 1; //cellId为0,直接走人
  6707. // //}else{
  6708. // // if(colourfulCloudsChasingTheMoon(cell) && cell != nullptr){
  6709. // // Log::write_log(FILE_TYPE::SQL_S, "original id: " + CFunctions::int2string(cell->id) + " realId: "+ CFunctions::int2string(cell->realId) + " cardid: " + this->card_id, true);
  6710. // // SaveCardAlgoData(cell);
  6711. // // }
  6712. // //}
  6713. // }
  6714. // }
  6715. // }
  6716. // }
  6717. // return ret;
  6718. // }catch (...)
  6719. // {
  6720. // return 1;
  6721. // }
  6722. //}
  6723. LeaderArrange::LeaderArrange(string cardid)
  6724. {
  6725. card_id = cardid;
  6726. starttime = time(NULL);
  6727. endtime = time(NULL);
  6728. }
  6729. LeaderArrange::~ LeaderArrange()
  6730. {
  6731. }
  6732. VehicleTypeAttRule::VehicleTypeAttRule(int vtid, int pre, int back)
  6733. {
  6734. this->vehicle_type_id = vtid;
  6735. this->offset_pre = pre;
  6736. this->offset_back = back;
  6737. }
  6738. VehicleTypeAttRule::~VehicleTypeAttRule()
  6739. {
  6740. this->Clear();
  6741. }
  6742. void VehicleTypeAttRule::Clear()
  6743. {
  6744. this->vecArea.clear();
  6745. }
  6746. void VehicleTypeAttRule::AddArea(int area_id)
  6747. {
  6748. std::vector<int>::iterator itArea = std::find(this->vecArea.begin(), this->vecArea.end(), area_id);
  6749. if(itArea == this->vecArea.end())
  6750. {
  6751. this->vecArea.push_back(area_id);
  6752. }
  6753. }