classdef.cpp 170 KB

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