car.cpp 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132
  1. #include "stdafx.h"
  2. #include "car.h"
  3. #include "ProcessRemodule.h"
  4. NAMESPACE_POINT_BEGIN(NAMESPACE_POINT)
  5. base_card::base_card()
  6. :m_id(0)
  7. ,m_ct(-1)
  8. ,mutex(0)
  9. {
  10. base_card_initiate();
  11. }
  12. base_card::base_card(std::shared_ptr<card_interface> &c,int id)
  13. :m_id(id)
  14. ,m_ct(-1)
  15. ,mutex(0)
  16. ,m_card(c)
  17. {
  18. base_card_initiate();
  19. }
  20. void base_card::base_card_initiate()
  21. {
  22. m_sol_p=m_sol;
  23. m_fitk.add_tool(30,5,5);
  24. m_fitk.add_tool(30,5,6);
  25. m_fitk.add_tool(30,10,10);
  26. m_fitk.add_tool(30,10,15);
  27. m_fitk.add_tool(20,5,6);
  28. m_fitk.add_tool(30,4,6);
  29. m_fitk.add_tool(40,4,6);
  30. m_fita.add_tool(15,5,5);
  31. m_begin=m_last=0;
  32. m_filter_man_count = 0;
  33. //site*m_current_site=nullptr;
  34. //record of last fitting data
  35. m_last_fit_valid = false;
  36. m_last_fit_time_sec = 0;
  37. m_last_fit_k = 0;
  38. m_last_fit_kb = 0;
  39. m_last_fit_ka = 0;
  40. m_last_fit_xo = 0;
  41. m_last_fit_yo = 0;
  42. m_last_fit_md_x = 0;
  43. m_last_fit_md_y = 0;
  44. m_last_fit_nearest_time_sec = 0; //latest time_sec within estimation span
  45. m_last_time_sec = 0;
  46. //record of last receiving time comment it out if using timer
  47. m_last_receive_time_sec=0;
  48. m_last_receive_ct=0;
  49. m_last_receive_sit=nullptr;
  50. //parameters
  51. m_fit_differ=4;
  52. m_pos_differ=8;
  53. m_simple_rec_kx = 0;
  54. m_simple_rec_ky = 0;
  55. //turning
  56. reset_turning();
  57. smooth_initial_setting = false;
  58. smooth_speed = 0;
  59. smooth_speed_presentation = 0;
  60. smooth_speed_presentation_cnt = 0;
  61. smooth_last_time_sec = 0;
  62. smooth_line_reset = false;
  63. smooth_halt_condition = 0; //if halting
  64. smooth_halt_count = 0; //halting count
  65. //his
  66. his_reset();
  67. }
  68. int base_card::last_ct()const
  69. {
  70. return m_ct;
  71. }
  72. void base_card::reset_sol()
  73. {
  74. m_sol_p=m_sol;
  75. }
  76. int base_card::sol_count()const
  77. {
  78. return m_sol_p-m_sol;
  79. }
  80. void base_card::on_tof_data(const site*sit,const loc_info&li)
  81. {
  82. if(sit->is_path_empty() || li.m_tof==0)
  83. return ;
  84. if(! m_d.empty() && li.m_loc_time<m_d(0).m_time)
  85. {
  86. //printf("time backwards!!!\n");
  87. return;
  88. }
  89. //estimate_missing_point_by_history(sit, mtc, m_last_receive_ct);
  90. //debug_print_syslog(0,"[sit_scale on_tof_data]%f",sit->m_scale);
  91. double dist_tof=li.m_tof*15.65*2.996*1e-4/sit->m_scale;
  92. //the same ct,then calc
  93. if(last_ct()==li.ct())
  94. {
  95. sit->solving(&m_sol_p,li.ant_id(),dist_tof);
  96. m_d.grow().reset().set_source(m_id,sit->m_id,m_last_li,li);
  97. select_solution(sit);
  98. //计算多次天线之间的测量距离
  99. sit->count_ant_dist(dist_tof, m_last_li.m_tof*15.65*2.996*1e-4/sit->m_scale);
  100. }
  101. else
  102. {
  103. if(sol_count()==2 && li.m_card_ct - m_last_li.m_card_ct ==1)
  104. {
  105. m_d.grow().reset().set_source(m_id,m_last_site->m_id,m_last_li);
  106. select_solution(m_last_site);
  107. }
  108. reset_sol();
  109. sit->solving(&m_sol_p,li.ant_id(),dist_tof);
  110. m_last_li=li;
  111. m_last_site=sit;
  112. }
  113. m_ct=li.ct();
  114. }
  115. void base_card::remove_history()
  116. {
  117. loc_point&b=m_d(0);
  118. //如果队列大小大于120个元素
  119. //或者队列元素大于2个以上,其中第一个元素和第二个元素的时间差大于max_histime(此处为60)秒
  120. if(m_d.size()>120 || (m_d.size()>2 && m_d(1).time_off(b)>max_histime))
  121. {
  122. m_d.skip_if([&b,this](loc_point&p){ return p.time_off(b)>max_histime;});
  123. m_fitk.reset_data();
  124. m_fita.reset_data();
  125. m_cur_fit.reset();
  126. m_begin=m_last=nullptr;
  127. int idx=find_first(0);
  128. if(idx<0)
  129. return;
  130. m_begin=&m_d[idx];
  131. idx=find_last(1);
  132. m_last=&m_d(idx);
  133. double dist=0,dist2=0;
  134. for(int len=min(15,m_d.size()-1),i=len;i>0;i--)
  135. {
  136. if(!m_d(i).cl())
  137. continue;
  138. //implemented by li
  139. int lastID1=find_last(i+1);
  140. int lastID2=-1;
  141. if(lastID1!=-1)
  142. lastID2=find_last(lastID1+1);
  143. if(lastID2!=-1)
  144. {
  145. double t0=m_d(i).m_time/1000., t1=m_d(lastID1).m_time/1000., t2=m_d(lastID2).m_time/1000.;
  146. double d0=m_begin->loc_dist(m_d(i)[0]),
  147. d1=m_begin->loc_dist(m_d(lastID1)[0]),
  148. d2=m_begin->loc_dist(m_d(lastID2)[0]);
  149. double k1=(d1-d0)/(t1-t0), k2=(d2-d1)/(t2-t1);
  150. if(t0-t1<5 && t1-t2<5 && fabs(k2-k1)<0.5)
  151. {
  152. double tbegin = t0-3;
  153. while(tbegin<t1+0.5)
  154. tbegin=tbegin+1;
  155. double tk=(d0-d1)/(t0-t1), tb=d1-tk*t1; //d1+(d0-d1)/(t0-t1)*(t-t1)
  156. for(double ti=tbegin;ti<t0;ti=ti+1)
  157. {
  158. m_fitk.add(ti, tk*ti+tb);
  159. }
  160. }
  161. }
  162. dist=m_begin->loc_dist(m_d(i)[0]);
  163. m_fitk.add(m_d(i).m_time/1000.,dist);
  164. if(i==len)
  165. continue;
  166. m_fita.add(m_d(i).m_time/1000.,dist-dist2);
  167. dist2=dist;
  168. }
  169. save_k();
  170. }
  171. }
  172. void base_card::select_solution(const site*sit)
  173. {
  174. if(! select_solution_impl(sit))
  175. {
  176. m_cur_fit.reset();
  177. }
  178. }
  179. bool base_card::select_solution_impl(const site*sit)
  180. {
  181. if (CT_PERSON == m_card->getCardType())
  182. {
  183. return select_solution_impl_person(sit);
  184. }
  185. remove_history();
  186. if(!solution0(sit))
  187. {
  188. reset_sol();
  189. return false;
  190. }
  191. point pt=select_solution0(sit);
  192. reset_sol();
  193. debug_print_syslog(0,"[lemon_pt]%s,x:%f,y:%f",m_card->cardId().c_str(),pt.x,pt.y);
  194. if(m_d.empty())
  195. {
  196. debug_print_syslog(0,"[m_d.empty() is true] %s,ct: %d",m_card->cardId().c_str(),m_ct);
  197. return false;
  198. }
  199. //将pt点插入到队首位置
  200. m_d(0).set(pt);
  201. if(!m_d(0).empty())
  202. m_d(0).m_dist=sit->dist_direct(pt);
  203. else
  204. {
  205. m_d(0).set_cl(0);
  206. if(m_last) m_d(0).m_dist=0.01*m_last->m_dist>0?1:-1;
  207. }
  208. if(pt.empty())
  209. {
  210. debug_print_syslog(0,"[pt.empty() is true] %s,ct: %d",m_card->cardId().c_str(),m_ct);
  211. return false;
  212. }
  213. //计算分站与计算出点之间距离
  214. m_d(0).m_dist1=sit->dist_direct(pt);
  215. double speed=0;
  216. int stat=1;
  217. int revise_flag = 0;
  218. //只有车卡才平滑
  219. if (m_card->smoothFlag())
  220. {
  221. revise_flag=1;
  222. pt=revise_by_history(pt, sit, m_d(0).m_time, revise_flag);
  223. }
  224. else
  225. {
  226. put_single_loc_point(pt, sit, m_d(0).m_ct, m_d(0));
  227. debug_print_syslog(0,"revise_by_history %s,flag:%d,(%.2f,%.2f)",m_card->cardId().c_str(),revise_flag,pt.x,pt.y);
  228. }
  229. //定位结果是否在路径上
  230. if(!card_path::inst().is_at_path(pt))
  231. {
  232. m_d(0).set_cl(0);
  233. debug_print_syslog(0,"[is_at_path() is false] %s,ct: %d",m_card->cardId().c_str(),m_ct);
  234. return false;
  235. }
  236. if(m_d(0).empty())
  237. {
  238. debug_print_syslog(0,"[m_d(0).empty() is true] %s,ct: %d",m_card->cardId().c_str(),m_ct);
  239. return false;
  240. }
  241. m_last=&m_d(0);
  242. if(m_line.empty() && m_d.size()>=2 && !make_line())
  243. {
  244. debug_print_syslog(0,"[m_line.empty() is true] %s,ct: %d",m_card->cardId().c_str(),m_ct);
  245. return false;
  246. }
  247. //if(!m_line.contain(m_d(0),0.1))
  248. if(!m_line.contain(m_d(0),0.01) || (m_begin && !m_line.contain(m_begin->m_sol[0], 0.01)))
  249. {
  250. m_fitk.reset_data();
  251. m_fita.reset_data();
  252. m_begin=m_last=nullptr;
  253. int i0=find_last(1);
  254. if(i0==-1)
  255. {
  256. debug_print_syslog(0,"[i0==-1] %s,ct: %d",m_card->cardId().c_str(),m_ct);
  257. return false;
  258. }
  259. std::vector<point> path=card_path::inst().find_path(m_d(i0),m_d(0));
  260. m_d.skip(m_d.size()-i0);
  261. if(path.empty())
  262. {
  263. m_line.clear();
  264. debug_print_syslog(0,"[path.empty() is true] %s,ct: %d",m_card->cardId().c_str(),m_ct);
  265. return false;
  266. }
  267. m_line.set(path.back(),m_d(0));
  268. }
  269. if(!m_begin)
  270. {
  271. int idx=find_first(0);
  272. if(idx>=0) m_begin=&m_d[idx];
  273. }
  274. if(!m_last)
  275. {
  276. int idx=find_last(0);
  277. if(idx>=0) m_last=&m_d(idx);
  278. }
  279. if(m_begin && m_d(0).cl())
  280. {
  281. //implemented by li
  282. int lastID1=find_last(1);
  283. int lastID2=-1;
  284. if(lastID1!=-1)
  285. lastID2=find_last(lastID1+1);
  286. if(lastID2!=-1)
  287. {
  288. double t0=m_last->m_time/1000., t1=m_d(lastID1).m_time/1000., t2=m_d(lastID2).m_time/1000.;
  289. double d0=m_begin->loc_dist(*m_last), d1=m_begin->loc_dist(m_d(lastID1)), d2=m_begin->loc_dist(m_d(lastID2));
  290. double k1=(d1-d0)/(t1-t0), k2=(d2-d1)/(t2-t1);
  291. if(t0-t1<5 && t1-t2<5 && fabs(k2-k1)<0.5)
  292. {
  293. double tbegin = t0-3;
  294. while(tbegin<t1+0.5)
  295. tbegin=tbegin+1;
  296. double tk=(d0-d1)/(t0-t1), tb=d1-tk*t1; //d1+(d0-d1)/(t0-t1)*(t-t1)
  297. for(double ti=tbegin;ti<t0;ti=ti+1)
  298. {
  299. m_fitk.add(ti, tk*ti+tb);
  300. }
  301. }
  302. }
  303. m_d(0).m_area=m_fitk.add(m_last->m_time/1000.,
  304. m_begin->loc_dist(*m_last)); //printf("log:%f\n",f.dist(m_d(0)));
  305. if(m_d.size()>1)
  306. {
  307. int pre=find_last(1);
  308. if(pre>0)
  309. {
  310. m_fita.add(m_d(0).m_time/1000.,m_begin->loc_dist(m_d(0))-m_begin->loc_dist(m_d(pre)));
  311. }
  312. }
  313. }
  314. // m_fitk.log();
  315. // m_fita.log();
  316. #ifdef _SMOOTH
  317. //m_d(0).set(pt);
  318. //计算分站到这个点距离
  319. m_d(0).m_dist=sit->dist_direct(pt);
  320. #endif
  321. //取出队首位置的坐标数据更新到业务模块
  322. loc_point lp=m_d(0);
  323. lp.set(pt);
  324. lp.m_speed=speed;
  325. lp.m_stat=stat;
  326. if(revise_flag==0)
  327. {
  328. //将结果更新到业务系统
  329. m_card->push_optimized_data(lp);
  330. //debug_print_syslog(0,"[on_tof_data]cardid: %s,ct: %d,x: %.2f,y: %.2f,pass: %d,speed: %.3f,acc: %.2f",m_card->cardId().c_str(),lp.m_ct,lp.x,lp.y,pass,speed,acc);
  331. }
  332. save_k();
  333. m_d(0).debug_out();
  334. //debug_print_syslog(0,"[lemon_smooth_log]t=%lld,sit=%d,card=%d,ct=%d,cred=%d,tof1=%d,tof2=%d,dist=%f,dir=0,pt=(%f,%f),rsp=%d,acc=%.2f,dist1=%.2f,dist2=%.2f\n",
  335. // lp.m_time, lp.m_sid, lp.m_cid, lp.m_ct, lp.m_cred_level,
  336. // lp.m_tof[0], lp.m_tof[1],lp.m_dist, lp.x,
  337. // lp.y ,(int)(lp.m_rsp[0]+lp.m_rsp[1])/2,lp.m_acc,
  338. // lp.m_dist1,lp.m_dist2
  339. // );
  340. return true;
  341. }
  342. bool base_card::select_solution_impl_person(const site* sit)
  343. {
  344. remove_history();
  345. if(!solution0(sit))
  346. {
  347. reset_sol();
  348. return false;
  349. }
  350. int t_ct = m_ct;
  351. point pt=select_solution0(sit);
  352. reset_sol();
  353. if(m_d.empty())
  354. {
  355. return false;
  356. }
  357. //将pt点插入到队首位置
  358. m_d(0).set(pt);
  359. if(!m_d(0).empty())
  360. m_d(0).m_dist=sit->dist_direct(pt);
  361. else
  362. {
  363. m_d(0).set_cl(0);
  364. if(m_last) m_d(0).m_dist=0.01*m_last->m_dist>0?1:-1;
  365. }
  366. if(pt.empty())
  367. {
  368. return false;
  369. }
  370. //计算分站与计算出点之间距离
  371. int revise_flag = 0;
  372. put_single_loc_point(pt, sit, m_d(0).m_ct, m_d(0));
  373. //定位结果是否在路径上
  374. if(!card_path::inst().is_at_path(pt))
  375. {
  376. m_d(0).set_cl(0);
  377. return false;
  378. }
  379. if(m_d(0).empty())
  380. return false;
  381. m_last=&m_d(0);
  382. if(m_line.empty() && m_d.size()>=2 && !make_line())
  383. return false;
  384. //点不在直线上
  385. if(!m_line.contain(m_d(0),0.01) || (m_begin && !m_line.contain(m_begin->m_sol[0], 0.01)))
  386. {
  387. m_fitk.reset_data();
  388. m_fita.reset_data();
  389. m_begin=m_last=nullptr;
  390. int i0=find_last(1);
  391. if(i0==-1)
  392. return false;
  393. std::vector<point> path=card_path::inst().find_path(m_d(i0),m_d(0));
  394. m_d.skip(m_d.size()-i0);
  395. if(path.empty())
  396. {
  397. m_line.clear();
  398. return false;
  399. }
  400. m_line.set(path.back(),m_d(0));
  401. }
  402. if(!m_begin)
  403. {
  404. int idx=find_first(0);
  405. if(idx>=0) m_begin=&m_d[idx];
  406. }
  407. if(!m_last)
  408. {
  409. int idx=find_last(0);
  410. if(idx>=0) m_last=&m_d(idx);
  411. }
  412. if(m_begin && m_d(0).cl())
  413. {
  414. //implemented by li
  415. int lastID1=find_last(1);
  416. int lastID2=-1;
  417. if(lastID1!=-1)
  418. lastID2=find_last(lastID1+1);
  419. if(lastID2!=-1)
  420. {
  421. double t0=m_last->m_time/1000., t1=m_d(lastID1).m_time/1000., t2=m_d(lastID2).m_time/1000.;
  422. double d0=m_begin->loc_dist(*m_last), d1=m_begin->loc_dist(m_d(lastID1)), d2=m_begin->loc_dist(m_d(lastID2));
  423. double k1=(d1-d0)/(t1-t0), k2=(d2-d1)/(t2-t1);
  424. if(t0-t1<5 && t1-t2<5 && fabs(k2-k1)<0.5)
  425. {
  426. double tbegin = t0-3;
  427. while(tbegin<t1+0.5)
  428. tbegin=tbegin+1;
  429. double tk=(d0-d1)/(t0-t1), tb=d1-tk*t1; //d1+(d0-d1)/(t0-t1)*(t-t1)
  430. for(double ti=tbegin;ti<t0;ti=ti+1)
  431. {
  432. m_fitk.add(ti, tk*ti+tb);
  433. }
  434. }
  435. }
  436. m_d(0).m_area=m_fitk.add(m_last->m_time/1000.,m_begin->loc_dist(*m_last));
  437. if(m_d.size()>1)
  438. {
  439. int pre=find_last(1);
  440. if(pre>0)
  441. {
  442. m_fita.add(m_d(0).m_time/1000.,m_begin->loc_dist(m_d(0))-m_begin->loc_dist(m_d(pre)));
  443. }
  444. }
  445. }
  446. #ifdef _SMOOTH
  447. //计算分站到这个点距离
  448. m_d(0).m_dist=sit->dist_direct(pt);
  449. #endif
  450. //取出队首位置的坐标数据更新到业务模块
  451. loc_point lp=m_d(0);
  452. lp.set(pt);
  453. //在将结果更新到业务之前先判断结果是否正确
  454. //将结果更新到业务系统
  455. m_card->push_optimized_data(lp);
  456. save_k();
  457. return true;
  458. }
  459. void base_card::save_k()
  460. {
  461. m_d(0).m_acc=0;
  462. const fit_result*fk=best_fit_raw(0,4);
  463. if(!fk)
  464. {
  465. m_cur_fit.reset();
  466. return;
  467. }
  468. m_cur_fit=*fk;
  469. fit_result&r=m_cur_fit;
  470. card_fit*fa=&m_fita[0];
  471. if(fa->is_valid() && fa->ke<0.1 && fk->k*fa->k<0)
  472. {
  473. double dk=fa->k*fa->num_point;
  474. r.ka=fa->k;
  475. if((fk->k+dk)*fk->k<0)
  476. r.k=0;
  477. else
  478. r.k+=dk;
  479. double y=fk->k*m_fitk(0).x+fk->kb;
  480. r.kb=y-m_fitk(0).x*r.k;
  481. }
  482. }
  483. int base_card::find_last(int start)
  484. {
  485. for(int i=start,len=m_d.size();i<len;i++)
  486. {
  487. if(m_d(i).cl()>0)
  488. return i;
  489. }
  490. return -1;
  491. }
  492. int base_card::find_first(int start)
  493. {
  494. for(int i=start,len=m_d.size();i<len;i++)
  495. {
  496. if(m_d[i].cl()>0)
  497. return i;
  498. }
  499. return -1;
  500. }
  501. bool base_card::make_line()
  502. {
  503. int i0=-1,i1=-1;
  504. if(-1==(i0=find_last(0)))
  505. return false;
  506. if(-1==(i1=find_last(i0+1)))
  507. return false;
  508. m_line.set(m_d(i0),m_d(i1));
  509. return true;
  510. }
  511. point base_card::select_solution0(const site* sit)
  512. {
  513. loc_point&c=m_d(0);
  514. if(m_d.size()==1)
  515. {
  516. //first point ,only accpet two ants data.
  517. if(c.cl()>0)
  518. {
  519. c[1]=m_sol[1];
  520. return c[0]=m_sol[0];
  521. }
  522. m_d.skip(1);
  523. return point(0,0);
  524. }
  525. //two ants.
  526. if(c.cl()>0 && sol_count()==2 && m_d(0).m_card_type==2)//m_card->smoothFlag() )
  527. {
  528. debug_print_syslog(0,"[1.one ant push data] %s,ct: %d",m_card->cardId().c_str(),m_ct);
  529. c[0]=m_sol[0];
  530. c[1]=m_sol[1];
  531. c.inc_cl(50);
  532. }
  533. else if(filter_by_fit(sit))
  534. {
  535. debug_print_syslog(0,"[2.one ant push data] %s,ct: %d",m_card->cardId().c_str(),m_ct);
  536. c.inc_cl(40);
  537. }
  538. // else if(filter_by_speed(sit))
  539. // {
  540. // c.inc_cl(20);
  541. // }
  542. else if(c.cl()>0 && sol_count()==2)
  543. {
  544. debug_print_syslog(0,"[3.one ant push data] %s,ct: %d",m_card->cardId().c_str(),m_ct);
  545. c[0]=m_sol[0];
  546. c[1]=m_sol[1];
  547. c.inc_cl(10);
  548. }
  549. else //one ant
  550. {
  551. debug_print_syslog(0,"[4.one ant push data] %s,ct: %d",m_card->cardId().c_str(),m_ct);
  552. int last=find_last(1);
  553. if(last>0)
  554. {
  555. loc_point&p=m_d(last);
  556. int cnt=sol_count();
  557. std::array<solpoint,4> res;
  558. //find the shortest dis
  559. for(int i=0;i<cnt;i++)
  560. res[i].set_sol(m_sol[i],m_sol[i].dist(p[0]));
  561. std::sort(&res[0],&res[0]+cnt);
  562. c[1].set(res[1]);
  563. c[0].set(res[0]);
  564. c.inc_cl(5);
  565. }
  566. }
  567. return c[0];
  568. }
  569. fit_result* base_card::best_fit_raw(int num_point,int start,int last)
  570. {
  571. card_fit*fit=nullptr;
  572. start=max(start,0);
  573. last =min(last,m_fitk.tool_size());
  574. if(last==-1)
  575. last=m_fitk.tool_size();
  576. for(int i=start;i<last;i++)
  577. {
  578. if(!m_fitk[i].is_valid())
  579. continue;
  580. if(m_fitk[i].num_point<num_point)
  581. continue;
  582. if(fit==nullptr)
  583. {
  584. fit=&m_fitk[i];
  585. continue;
  586. }
  587. if(fit->ke>m_fitk[i].ke)
  588. {
  589. fit=&m_fitk[i];
  590. }
  591. }
  592. return fit;
  593. }
  594. const fit_result* base_card::best_fit()const
  595. {
  596. if(m_cur_fit.k==0 && m_cur_fit.ke==0)
  597. return nullptr;
  598. return &m_cur_fit;
  599. }
  600. bool base_card::filter_by_speed(const site *sit)
  601. {
  602. static double MAX_SPEED[]={0,8/(3.6*sit->m_scale),40/(3.6*sit->m_scale)};
  603. loc_point&c=m_d(0);
  604. if(c.m_card_type==2||m_begin==nullptr||m_last==nullptr)
  605. return false;
  606. int ctype=c.m_card_type==2?2:1;
  607. loc_point&e=*m_last;
  608. int cnt=sol_count();
  609. std::array<solpoint,4> v;
  610. int vc=0;
  611. double time=(c.m_time-e.m_time)/1000.;
  612. for(int i=0;i<cnt;i++)
  613. {
  614. double dist=e.loc_dist(m_sol[i]);
  615. double speed=dist/time;
  616. if(fabs(speed)>MAX_SPEED[ctype])
  617. continue;
  618. v[vc++].set_sol(m_sol[i],speed);
  619. }
  620. if(vc==0) return true;
  621. if(vc==1)
  622. {
  623. c[0]=v[0];
  624. c[1]=v[0];
  625. return true;
  626. }
  627. std::sort(&v[0],&v[0]+vc,[](const solpoint&l,const solpoint&r){
  628. return fabs(l.m_score)<fabs(r.m_score);
  629. });
  630. while(vc>1)
  631. {
  632. int pre=find_last(1);
  633. pre=find_last(pre+1);
  634. if(pre<0)
  635. break;
  636. double dist=m_d(pre).loc_dist(e);
  637. double time=(e.m_time-m_d(pre).m_time)/1000.;
  638. double speed=dist/time;
  639. int ac=0;
  640. for(int i=0;i<vc;i++)
  641. {
  642. double a=fabs(v[i].m_score-speed)/time;
  643. if(a>2.5)
  644. continue;
  645. v[ac++].m_score=a;
  646. }
  647. vc=ac;
  648. if(vc==0)
  649. return true;
  650. if(vc==1)
  651. {
  652. c[0]=v[0];
  653. c[1]=v[0];
  654. return true;
  655. }
  656. std::sort(&v[0],&v[0]+vc);
  657. if(c.m_card_type==1 && v[0].m_score<0.1)
  658. {
  659. c[0]=v[0];
  660. c[1]=v[0];
  661. return true;
  662. }
  663. break;
  664. }
  665. return false;
  666. }
  667. void base_card::log(const char * str)
  668. {
  669. if (m_card->getCardType()==5||m_card->getCardType()==4)
  670. debug_print_syslog(0,"%s %s",str,m_card->cardId().c_str());
  671. }
  672. bool base_card::filter_by_fit(const site *sit)
  673. {
  674. loc_point&f=*m_begin;
  675. loc_point&c=m_d(0);
  676. fit_result*fit=c.m_card_type==2?best_fit_raw(5):best_fit_raw(4,4);
  677. // fit_result*fit=best_fit_raw();
  678. if(fit==nullptr || m_begin==nullptr || fit->ke>2)
  679. return false;
  680. int cnt=sol_count();
  681. std::array<solpoint,4> v;
  682. for(int i=0;i<cnt;i++)
  683. {
  684. v[i].set_sol(m_sol[i],fabs(fit->testk(c.m_time/1000.,f.dist_direct(m_sol[i]))));
  685. }
  686. std::sort(&v[0],&v[0]+cnt);
  687. double a=2.5;
  688. if(c.m_card_type==2 && fabs(fit->k)<10/(3.6*sit->m_scale) && fabs(fit->k)>1/(3.6*sit->m_scale) && v[1].score()-v[0].score()<20)
  689. {
  690. a=1;
  691. }
  692. if(c.m_card_type==1 && fabs(fit->k)<2/(3.6*sit->m_scale) && fabs(fit->k)>0.5/(3.6*sit->m_scale) && v[1].score()-v[0].score()<10)
  693. {
  694. a=0.3;
  695. }
  696. if(c.m_card_type==2 && fabs(fit->k)<=1/(3.6*sit->m_scale) && v[1].score()-v[0].score()<20)
  697. {
  698. return false;
  699. }
  700. //if(c.m_card_type==1 && fabs(fit->k)<=0.5/(3.6*sit->m_scale) && v[1].score()-v[0].score()<8)
  701. //{
  702. // return false;
  703. //}
  704. if(!filter_by_acc(c,v,a))
  705. {
  706. c.set_cl(0);
  707. log("filter_by_acc");
  708. return true; //false?
  709. }
  710. assert(m_last);
  711. //revise
  712. if(c.m_card_type==1)// && c.cl()>0 && sol_count()==2)
  713. {
  714. if(m_sol[0].dist(v[0])>1)
  715. m_filter_man_count++;
  716. else
  717. m_filter_man_count = 0;
  718. if(m_filter_man_count==3)
  719. {
  720. m_fitk.reset_data();
  721. m_fita.reset_data();
  722. m_cur_fit.reset();
  723. m_begin=m_last=nullptr;
  724. m_filter_man_count = 0;
  725. }
  726. }
  727. c[0]=v[0];
  728. c[1]=v[1];
  729. return true;
  730. }
  731. bool base_card::filter_by_acc(loc_point&c,std::array<solpoint,4>&v,double a)
  732. {
  733. if(!m_last->is_same_site(c))
  734. return true;
  735. double td=m_last->time_off(c);
  736. if(v[0].score()>a*td*td)
  737. return false;
  738. return true;
  739. }
  740. #if 0
  741. bool filter_gt50m()
  742. {
  743. static double max_speed=10.0/1000.;
  744. loc_point&c=m_d(0);
  745. if(!c.b_50m())
  746. return false;
  747. int last=std::min(6,m_d.size()-1);
  748. while(c.m_time-m_d(last).m_time>8000)
  749. --last;
  750. if(last<3)
  751. return false;
  752. int cnt=sol_count();
  753. std::array<point,4> v;
  754. for(int j=1;j<=last;j++)
  755. {
  756. double t=c.m_time-m_d(j).m_time;
  757. if(m_d(j).empty())
  758. continue;
  759. for(int i=0;i<cnt;i++)
  760. {
  761. double v0=m_d(j).dist(m_sol[i])/t;
  762. if(v0>max_speed)
  763. continue;
  764. v[i].y=i;
  765. v[i].x-=1;
  766. if(m_d(j).m_sid!=c.m_sid)//切换过分站之后更靠谱
  767. v[i].x-=3;
  768. if(j==last)
  769. continue;
  770. double v1=m_d(j).dist(m_d(last))/(m_d(j).m_time-m_d(last).m_time);
  771. v[i].x-=10-fabs(v1-v0)*1000;
  772. }
  773. }
  774. std::sort(&v[0],&v[0]+cnt);
  775. if(v[0].x<-3)
  776. {
  777. c[0]=m_sol[(int)v[0].y];
  778. c[1]=m_sol[(int)v[1].y];
  779. return true;
  780. }
  781. return false;
  782. }
  783. #endif
  784. //判断距离最近的两个解
  785. bool base_card::solution0(const site*sit)
  786. {
  787. if(sol_count()!=4)
  788. return true;
  789. int c=0;
  790. std::array<solpoint,4> res;
  791. for(int i=0;i<2;i++)
  792. {
  793. double d=m_sol[i].dist(m_sol[i+2]);
  794. //add by zhuyf 2018/06/27
  795. //将判断条件改为根据两天线间的距离与实际距离的差是否符合指定误差范围
  796. /*double l = 0.0;
  797. double d1 = 0.0, d2 = 0.0;
  798. d1 = m_d(0).m_tof[0]*15.65*2.996*1e-4/sit->m_scale;
  799. d2 = m_d(0).m_tof[1]*15.65*2.996*1e-4/sit->m_scale;
  800. if (d1 < 10.0 || d2 < 10.0)
  801. {
  802. d1 = sqrt(d1*d1 - 1.5*1.5);
  803. d2 = sqrt(d2*d2 - 1.5*1.5);
  804. }
  805. l = fabs(d1 - d2);
  806. bool condition = false;
  807. if (fabs(l - sit->ant_dist()) < 0.2*sit->m_scale)
  808. {
  809. condition = true;
  810. debug_print_syslog(0,"[condition failed push data]cardid: %s,ct: %d",m_card->cardId().c_str(),m_ct);
  811. }*/
  812. if(d<sit->ant_dist()*3)
  813. //if(condition)
  814. {
  815. res[c++].set_sol(m_sol[i].middle(m_sol[i+2]),d);
  816. }
  817. else
  818. {
  819. res[c++].set_sol(m_sol[i]);
  820. res[c++].set_sol(m_sol[i+2]);
  821. }
  822. }
  823. std::sort(&res[0],&res[0]+c);
  824. // printf("dist=%.5f, %.5f\n",res[0].dist,res[1].dist);
  825. for(int i=0;i<c;i++)
  826. m_sol[i]=res[i];
  827. m_sol_p=m_sol+c;
  828. //if(c>=2 && res[0].m_score*3>res[1].m_score)
  829. //{
  830. //}
  831. //else if(c<=2)
  832. //{
  833. m_d(0).inc_cl(10);
  834. //}
  835. return true;
  836. }
  837. point base_card::revise_by_history(point pt, const site*sit, int64_t m_time, int &revise_flag) //m_time - 1000*ct
  838. {
  839. if(m_line.empty() || !m_line.contain(m_d(0),0.1))
  840. {
  841. //m_current_site = sit;
  842. m_last_fit_valid = false;
  843. m_last_fit_time_sec = 0;
  844. m_last_fit_k = 0;
  845. m_last_fit_kb = 0;
  846. m_last_fit_ka = 0;
  847. m_last_fit_xo = 0;
  848. m_last_fit_yo = 0;
  849. m_last_fit_nearest_time_sec = 0;
  850. m_last_time_sec = 0;
  851. //turning
  852. reset_turning();
  853. //create the list
  854. generate_list(pt, sit, false); //generate single point
  855. // smooth the dist----test
  856. #ifdef _CAR_TEST
  857. point smooth_location;
  858. if(!m_if_turning)
  859. {
  860. point tmp;
  861. tmp.set(sit->x,sit->y);
  862. sit->mapping(tmp,smooth_location);
  863. }
  864. else
  865. smooth_location.set(m_turning_pt);
  866. smooth_dist_car(pt, m_time/1000., m_d(0).m_ct, sit, smooth_location,&m_d(0));
  867. #endif
  868. return pt;
  869. }
  870. #ifdef _CAR_TEST
  871. // print missing point
  872. if(m_last_fit_valid && m_last_receive_sit!=nullptr && m_last_receive_time_sec - m_last_fit_time_sec < 20 && fabs(m_last_fit_k) > 0.5)
  873. {
  874. double mt1 = m_last_receive_time_sec;
  875. double mt2 = m_d(0).m_time / 1000.;
  876. double mtc = mt1;
  877. printf("begin print missing point %f ~ %f\n",mt1, mt2);
  878. if(mt2-mt1 > 1.5)
  879. {
  880. mtc = mtc + 1;
  881. while(mt2-mtc > 0.5)
  882. {
  883. if(mtc-mt1>12)
  884. {
  885. break;
  886. }
  887. estimate_missing_point_by_history(m_last_receive_sit, mtc, m_last_receive_ct);
  888. mtc = mtc + 1;
  889. }
  890. }
  891. //printf("end print missing point\n");
  892. }
  893. m_last_receive_time_sec = m_time / 1000.;
  894. m_last_receive_ct = m_d(0).m_ct;
  895. m_last_receive_sit = sit;
  896. // comment out if using timer
  897. #endif
  898. // convert pt to distance
  899. double dist = convert_pt_to_dist(pt, sit);
  900. double m_time_sec = m_time / 1000.; //second
  901. //if(m_time_sec - m_last_fit_nearest_time_sec > 30)m_last_fit_valid = false;
  902. if(m_time_sec - m_last_fit_time_sec > 60)m_last_fit_valid = false;
  903. // update acc
  904. //m_accumulate_acc = m_d(0).m_acc;
  905. // choose data by fit
  906. const fit_result*fit=best_fit();
  907. bool if_change_fit=false;
  908. if(fit!=nullptr && fit->ke<=1 && m_time_sec - m_fitk.x(0) <= 15 && fabs(fit->k) < m_pos_differ)
  909. { //printf("change fit time:%f,%f,%f\n",m_time_sec, fit->d.x(0), m_time_sec - fit->d.x(0));
  910. // put m_acccumulate_acc into consideration
  911. // fit->k - m_last_fit_k < m_accumulate_acc
  912. if(m_last_fit_valid == true && m_last_fit_k * fit->k > -0.6)
  913. //if((sit->dist(pt)<20 ||m_last_fit_k * fit->k > -0.6))
  914. { //if point is too near the sit: do not not judge the backwards
  915. double est1 = estimate_point_by_history(sit, m_last_fit_time_sec);
  916. double est2 = fit->k * (m_time_sec-fit->xo) + fit->kb + fit->yo;
  917. //printf("change fit:1(%f,%f),2(%f,%f),differ:(%f,%f)\n",
  918. // m_last_fit_nearest_time_sec,est1,m_time_sec,est2,m_time_sec-m_last_fit_nearest_time_sec,est2-est1);
  919. //if(fabs(est1-est2)>40)printf("change fit:%f,%f,%f\n",fabs(est1-est2),est1,est2);
  920. if(fabs(est1-est2)< (m_time_sec - m_last_fit_time_sec) * 5) // large jump is not allowed
  921. if_change_fit=true;
  922. }
  923. else if(m_last_fit_valid==false)
  924. if_change_fit=true;
  925. }
  926. if(if_change_fit)
  927. {
  928. m_last_fit = *fit;
  929. m_last_fit_valid = true;
  930. m_last_fit_time_sec = m_fitk.x(0);
  931. m_last_fit_k = fit->k;
  932. m_last_fit_kb = fit->kb;
  933. m_last_fit_ka = fit->ka;
  934. m_last_fit_xo = fit->xo;
  935. m_last_fit_yo = fit->yo;
  936. m_last_fit_nearest_time_sec = m_fitk.x(0);
  937. int fidx=find_first(0);
  938. if(fidx<0)
  939. {
  940. m_last_fit_md_x = 0;
  941. m_last_fit_md_y = 0;
  942. }
  943. else{
  944. loc_point&f=m_d[fidx];
  945. m_last_fit_md_x = f.m_sol[0].x;
  946. m_last_fit_md_y = f.m_sol[0].y;
  947. }
  948. //printf("change line\n");
  949. }
  950. // revise
  951. double estimate_dist = estimate_point_by_history(sit, m_time_sec);
  952. //if(m_last_fit_valid)printf("e:%f,d:%f,fit differ:%f;t-lt:%f\n",
  953. // estimate_dist,dist,estimate_dist - dist,m_time_sec - m_last_fit_time_sec);
  954. if(m_last_fit_valid && m_time_sec - m_last_fit_time_sec < 20)
  955. {
  956. if(fabs(m_last_fit_k) > 0.5 && fabs(estimate_dist-dist)>m_fit_differ)
  957. dist=estimate_dist;
  958. else if(fabs(m_last_fit_k) <= 0.5 && fabs(estimate_dist-dist)>m_fit_differ * 2)
  959. dist=estimate_dist;
  960. else revise_flag = 0;
  961. }
  962. else m_last_fit_nearest_time_sec = m_time_sec;
  963. m_last_time_sec = m_time_sec;
  964. // convert the estimated dist to pt
  965. point mpt = convert_dist_to_pt(dist, sit);
  966. // judging turning
  967. detect_turning(mpt, sit);
  968. //create the list
  969. if(m_last_fit_valid && m_time/1000. - m_last_fit_time_sec < 20 && fabs(m_last_fit_k) > 0.5)
  970. generate_list(mpt, sit, true); //generate the whole list
  971. else
  972. generate_list(mpt, sit, false); //generate single point
  973. //turning map
  974. turning_mapping(mpt, sit);
  975. // smooth the dist----test
  976. #ifdef _CAR_TEST
  977. point smooth_location;
  978. if(!m_if_turning)
  979. {
  980. point tmp;
  981. tmp.set(sit->x,sit->y);
  982. sit->mapping(tmp,smooth_location);
  983. }
  984. else
  985. smooth_location.set(m_turning_pt);
  986. smooth_dist_car(mpt, m_time_sec, m_d(0).m_ct, sit, smooth_location, &m_d(0));
  987. #endif
  988. return mpt;
  989. }
  990. // estimate point dist
  991. double base_card::estimate_point_by_history(const site*sit, double m_time_sec)
  992. {
  993. //m_time, m_sid, m_cid
  994. double estimate_dist = m_last_fit_k * (m_time_sec-m_last_fit_xo) + m_last_fit_kb + m_last_fit_yo;
  995. point pt(m_last_fit_md_x + estimate_dist * m_simple_rec_kx, m_last_fit_md_y + estimate_dist * m_simple_rec_ky);
  996. //printf("cts:%f, ts:%f, k:%f, b:%f, xo:%f, yo:%f, (%f,%f)",
  997. //m_time_sec ,m_last_fit_time_sec,m_last_fit_k,m_last_fit_kb,m_fitk.xo,m_fitk.yo, pt.x, pt.y);
  998. int fidx=find_first(0);
  999. if(fidx>=0)
  1000. {
  1001. loc_point&f=m_d[fidx];
  1002. estimate_dist = f.loc_dist(pt);
  1003. }
  1004. else estimate_dist = 0;
  1005. return estimate_dist;
  1006. }
  1007. // timer up missing point
  1008. void base_card::estimate_missing_point_by_history(const site*sit, double m_time_sec, int m_ct)
  1009. {
  1010. loc_point m_lp;
  1011. m_lp.m_time=(int64_t)(m_time_sec * 1000);
  1012. m_lp.m_sid = sit->m_id;
  1013. m_lp.m_cid = m_d(0).m_cid;
  1014. //printf("cts:%f, ts:%f, k:%f, b:%f, xo:%f, yo:%f\n",
  1015. // m_time_sec ,m_last_fit_time_sec,m_last_fit_k,m_last_fit_kb,m_fitk.xo,m_fitk.yo);
  1016. //m_time, m_sid, m_cid
  1017. double estimate_dist = estimate_point_by_history(sit, m_time_sec);
  1018. // printf("missing, t:%f,d:%f\n",m_time_sec,estimate_dist);
  1019. point m_p = convert_dist_to_pt(estimate_dist, sit);
  1020. turning_mapping(m_p,sit);
  1021. if(!card_path::inst().is_at_path(m_p)) //if point not on the path
  1022. {
  1023. printf("out of path:(%f,%f)\n",m_p.x, m_p.y);
  1024. return;
  1025. }
  1026. #ifdef _CAR_TEST
  1027. point smooth_location;
  1028. if(!m_if_turning)
  1029. {
  1030. point tmp;
  1031. tmp.set(sit->x,sit->y);
  1032. sit->mapping(tmp,smooth_location);
  1033. }
  1034. else
  1035. smooth_location.set(m_turning_pt);
  1036. smooth_dist_car(m_p, m_time_sec, m_ct, sit, smooth_location, &m_lp);
  1037. #endif
  1038. m_lp.set(m_p);
  1039. m_lp.m_dist=sit->dist_direct(m_p);
  1040. m_lp.debug_out();
  1041. m_lp.x=m_lp.m_smooth_x;
  1042. m_lp.y=m_lp.m_smooth_y;
  1043. //push m_lp
  1044. }
  1045. double base_card::convert_pt_to_dist(point &pt, const site*sit)
  1046. {
  1047. double dist=0;
  1048. int fidx=find_first(0);
  1049. if(fidx>=0)
  1050. {
  1051. loc_point&f=m_d[fidx];
  1052. //dist = f.dist(pt) * (pt<f?-1:1);
  1053. dist = f.loc_dist(pt);
  1054. if(dist!=0)
  1055. {
  1056. m_simple_rec_kx = (pt.x - f.m_sol[0].x) / dist;
  1057. m_simple_rec_ky = (pt.y - f.m_sol[0].y) / dist;
  1058. }
  1059. }
  1060. if(fidx<0 || dist==0)
  1061. {
  1062. m_simple_rec_kx = 0;
  1063. m_simple_rec_ky = 0;
  1064. }
  1065. //printf("convert_pt_to_dist:(%f,%f),%f\n",pt.x,pt.y,dist);
  1066. //double dist = sit->dist_direct(pt);
  1067. //if(dist == 0)return 0;
  1068. //m_simple_rec_kx = (pt.x - (*sit).x) / dist;
  1069. //m_simple_rec_ky = (pt.y - (*sit).y) / dist;
  1070. return dist;
  1071. }
  1072. point base_card::convert_dist_to_pt(double dist, const site*sit)
  1073. {
  1074. int fidx=find_first(0);
  1075. if(fidx<0)return point(0, 0);
  1076. loc_point&f=m_d[fidx];
  1077. //printf("convert_dist_to_pt:dist:%f,x:%f,y:%f,reckx:%f,recky:%f,f.x:%f,f.y%f\n",
  1078. //dist,f.x + dist * m_simple_rec_kx,f.y + dist * m_simple_rec_ky,
  1079. //m_simple_rec_kx,m_simple_rec_ky,f.x,f.y);
  1080. return point(f.m_sol[0].x + dist * m_simple_rec_kx, f.m_sol[0].y + dist * m_simple_rec_ky);
  1081. //return point((*sit).x + dist * m_simple_rec_kx, (*sit).y + dist * m_simple_rec_ky);
  1082. }
  1083. void base_card::reset_turning()
  1084. {
  1085. m_if_turning=false;
  1086. m_turning_pt.set(0,0);
  1087. m_turning_ept.set(0,0);
  1088. }
  1089. void base_card::detect_turning(point &mpt, const site*sit)
  1090. {
  1091. if(m_if_turning)return;
  1092. //IMPORTANT: only car-1121 and car-1136 have accurate rav values currently. May delete this sentence in the future.
  1093. //if(m_id!=1121 && m_id!=1136)return;
  1094. double detect_area = 4;
  1095. double detect_angle = 15; //15
  1096. double detect_para = 0.25; //0.25
  1097. // check angle
  1098. double angle=-m_d(0).m_rav; // right+ left-
  1099. if(fabs(angle)>180)return; // invalid data
  1100. if(fabs(angle)<detect_angle || !m_last_fit_valid || fabs(m_last_fit_k)<0.01)return;
  1101. // find turning point
  1102. std::vector<line_v> turning_list=card_path::inst().find_possible_path(mpt, detect_area);;
  1103. //angle1
  1104. int fidx=find_first(0);
  1105. if(fidx<0)return;
  1106. double dist=m_d[fidx].loc_dist(mpt);
  1107. point pt1;
  1108. pt1.set(m_d[fidx].m_sol[0]);
  1109. double angle1;
  1110. if(m_last_fit_k * dist>0)
  1111. angle1=calc_turning_angle(pt1, mpt);
  1112. else
  1113. angle1=calc_turning_angle(mpt, pt1);
  1114. if(angle1<0)return;
  1115. //finding
  1116. for(unsigned int i=0;i<turning_list.size();i++)
  1117. {
  1118. line_v &l=turning_list[i];
  1119. double angle2=calc_turning_angle(l.v[0], l.v[1]);
  1120. double delta=angle1-angle2;
  1121. if(delta>180)delta=delta-360;
  1122. if(delta<-180)delta=delta+360;
  1123. if(fabs(delta)<5)continue;
  1124. if(fabs(delta)>175)continue;
  1125. if(angle*delta>0 && fabs(angle)>fabs(delta)*detect_para)
  1126. {
  1127. printf("turning:(%.5f,%.5f)(%.5f,%.5f)(%.5f,%.5f),a1:%f,a2:%f,delta:%f,angle:%f\n",
  1128. pt1.x,pt1.y,l.v[0].x,l.v[0].y,l.v[1].x,l.v[1].y,angle1,angle2,delta,angle);
  1129. m_if_turning=true;
  1130. m_turning_pt.set(l.v[0]);
  1131. m_turning_ept.set(l.v[1]);
  1132. break;
  1133. }
  1134. }
  1135. }
  1136. double base_card::calc_turning_angle(point &a, point &b)
  1137. {
  1138. if(fabs(a.x-b.x)<0.001)
  1139. {
  1140. if(fabs(a.y-b.y)<0.001)return -1;
  1141. return b.y>a.y?90:270;
  1142. }
  1143. double angle=std::atan((b.y-a.y)/(b.x-a.x))*180/3.1415926;
  1144. if(a.x>b.x)angle=angle+180;
  1145. if(angle<0)angle=angle+360;
  1146. return angle;
  1147. }
  1148. void base_card::turning_mapping(point &rpt,const site*sit)
  1149. {
  1150. if(!m_if_turning)return;
  1151. point tmp;
  1152. tmp.set(sit->x,sit->y);
  1153. point sit_location; //projection
  1154. sit->mapping(tmp,sit_location);
  1155. double dist1=sit_location.dist(rpt);
  1156. double dist2=sit_location.dist(m_turning_pt);
  1157. double dist3=m_turning_pt.dist(rpt);
  1158. if(dist1<=dist2 || dist1<=dist3)
  1159. {
  1160. if(dist2-dist1>3||dist1<=dist3)
  1161. {
  1162. printf("reset turning\n");
  1163. reset_turning(); // may encounter problems
  1164. }
  1165. return;
  1166. }
  1167. if(dist3>10)dist3=10; // turning distance no more than 10
  1168. double turning_x,turning_y;
  1169. double dist4=m_turning_pt.dist(m_turning_ept);
  1170. turning_x=m_turning_pt.x + dist3 * (m_turning_ept.x - m_turning_pt.x) / dist4;
  1171. turning_y=m_turning_pt.y + dist3 * (m_turning_ept.y - m_turning_pt.y) / dist4;
  1172. /*printf("turning mapping:(%.2f,%.2f)->(%.2f,%.2f),d1:%f,d2:%f,d3:%f\n",
  1173. rpt.x,rpt.y,turning_x,turning_y,dist1,dist2,dist3);*/
  1174. rpt.set(turning_x,turning_y);
  1175. }
  1176. void base_card::smooth_reset()
  1177. {
  1178. smooth_initial_setting=false;
  1179. smooth_speed=0; //smoothed speed
  1180. //smooth_speed_presentation=0;
  1181. smooth_speed_presentation_cnt=0;
  1182. smooth_last_position=point(0,0); //last position of smoothed point
  1183. smooth_last_true_position=point(0,0); //last position of true point
  1184. smooth_last_time_sec=0; //last time second
  1185. smooth_halt_condition = false;
  1186. smooth_halt_count=0;
  1187. }
  1188. bool base_card::smooth_initiate(point &pt, double t, const site*sit)
  1189. {
  1190. smooth_initial_setting=true;
  1191. smooth_speed=0;
  1192. //smooth_speed_presentation=0;
  1193. smooth_speed_presentation_cnt=0;
  1194. smooth_last_position = pt;
  1195. smooth_last_true_position = pt;
  1196. smooth_last_time_sec = t;
  1197. smooth_halt_condition=false;
  1198. smooth_halt_count=0;
  1199. smooth_halt_position=pt;
  1200. smooth_halt_position_plus=pt;
  1201. smooth_halt_position_minus=pt;
  1202. return true;
  1203. }
  1204. void base_card::smooth_dist_car(point &pt, double t, int ct, const site*sit, point dstp, loc_point *m_lp)
  1205. {
  1206. point init_pt(pt.x, pt.y);
  1207. if(smooth_line.empty() || !smooth_line.contain(pt,0.1) || smooth_halt_count>6)
  1208. {
  1209. if(!smooth_line_reset)
  1210. {
  1211. if(!smooth_line.empty() && !smooth_line.contain(pt,0.1) && !smooth_last_true_position.empty())
  1212. {
  1213. std::vector<point> path=card_path::inst().find_path(smooth_last_true_position, pt);
  1214. if(!path.empty() && smooth_last_true_position.dist(path[0])>200)
  1215. path.clear();
  1216. debug_print_syslog(0,"generating critical point in smooth(car):(%.2f,%.2f)->(%.2f,%.2f)\n",
  1217. smooth_last_true_position.x, smooth_last_true_position.y, pt.x, pt.y);
  1218. if(!path.empty())
  1219. {
  1220. point critical_point=path[0];
  1221. debug_print_syslog(0,"critical point generated in smooth(car):pt=(%.2f,%.2f),(%.2f,%.2f)->(%.2f,%.2f)\n",
  1222. critical_point.x, critical_point.y, smooth_last_true_position.x, smooth_last_true_position.y,
  1223. pt, pt.y);
  1224. init_pt.set(critical_point);
  1225. }
  1226. }
  1227. smooth_reset();
  1228. smooth_line_reset=true;
  1229. }
  1230. else
  1231. {
  1232. std::vector<point> path=card_path::inst().find_path(smooth_last_true_position, pt);
  1233. if(!path.empty() && smooth_last_true_position.dist(path[0])>200)
  1234. path.clear();
  1235. if(path.empty())
  1236. {
  1237. smooth_line.set(smooth_last_true_position, pt);
  1238. smooth_line_reset=false;
  1239. }
  1240. else
  1241. {
  1242. smooth_reset();
  1243. }
  1244. }
  1245. }
  1246. else
  1247. smooth_line_reset=false;
  1248. if(!smooth_initial_setting)
  1249. {
  1250. smooth_initiate(init_pt, t, sit);
  1251. }
  1252. else
  1253. {
  1254. double current_dist = dstp.dist_direct(pt);
  1255. double last_position = dstp.dist_direct(smooth_last_position);
  1256. double last_true_position = dstp.dist_direct(smooth_last_true_position);
  1257. double rec_kx=0;
  1258. double rec_ky=0;
  1259. if(current_dist!=0)
  1260. {
  1261. rec_kx = (pt.x - dstp.x)/current_dist;
  1262. rec_ky = (pt.y - dstp.y)/current_dist;
  1263. }
  1264. double next_dist = last_position + smooth_speed * (t-smooth_last_time_sec);
  1265. double max_span = 200;
  1266. //printf("smooth dist:%f,%f,%f\n",next_dist,current_dist,next_dist-current_dist);
  1267. if(fabs(next_dist-current_dist)<max_span && t - smooth_last_time_sec < 10)
  1268. {
  1269. double new_speed = (current_dist-last_true_position) / (t - smooth_last_time_sec);
  1270. // judge halting
  1271. if(fabs(new_speed)<0.1)smooth_halt_count++;
  1272. else{
  1273. smooth_halt_count=0;
  1274. }
  1275. if(!smooth_halt_condition && smooth_halt_count>=3 && fabs(smooth_speed) < 0.2)
  1276. {
  1277. smooth_halt_condition=true;
  1278. smooth_halt_position=smooth_last_position;
  1279. smooth_halt_position_plus=pt;
  1280. smooth_halt_position_minus=pt;
  1281. }
  1282. // handle speed
  1283. if(smooth_halt_condition)
  1284. {
  1285. double halt_position = dstp.dist_direct(smooth_halt_position);
  1286. double halt_position_plus = dstp.dist_direct(smooth_halt_position_plus);
  1287. double halt_position_minus = dstp.dist_direct(smooth_halt_position_minus);
  1288. if(halt_position_plus<current_dist)halt_position_plus=current_dist;
  1289. if(halt_position_minus>current_dist)halt_position_minus=current_dist;
  1290. smooth_halt_position_plus = point(dstp.x + halt_position_plus * rec_kx, dstp.y + halt_position_plus * rec_ky);
  1291. smooth_halt_position_minus = point(dstp.x + halt_position_minus * rec_kx, dstp.y + halt_position_minus * rec_ky);
  1292. if(fabs(halt_position_plus - halt_position_minus)>1)
  1293. {
  1294. smooth_halt_condition=false;
  1295. last_position = halt_position;
  1296. smooth_speed = 0;
  1297. //printf("smooth stop halting\n");
  1298. }
  1299. }
  1300. else
  1301. {
  1302. if(fabs(smooth_speed)<1e-6 || _isnan(smooth_speed))
  1303. {
  1304. smooth_speed=new_speed;
  1305. if(smooth_speed>2.5)smooth_speed=2.5;
  1306. if(smooth_speed<-2.5)smooth_speed=-2.5;
  1307. }
  1308. else
  1309. {
  1310. double speed_differ = fabs(new_speed-smooth_speed);
  1311. if(speed_differ>1)
  1312. new_speed=smooth_speed +1*(new_speed>smooth_speed?1:-1);
  1313. smooth_speed = smooth_speed * 0.4 + new_speed * 0.6;
  1314. }
  1315. if(fabs(smooth_speed_presentation)<1e-6 ||_isnan(smooth_speed_presentation))
  1316. {
  1317. smooth_speed_presentation=fabs(smooth_speed);
  1318. }
  1319. else
  1320. smooth_speed_presentation = smooth_speed_presentation * 0.4 + fabs(smooth_speed) * 0.6;
  1321. //printf(",%f,%f\n",new_speed,smooth_speed);
  1322. // must obey speed direction
  1323. if(smooth_speed * (current_dist-last_position) > 0)
  1324. {
  1325. last_position = last_position+smooth_speed*(t-smooth_last_time_sec);
  1326. if(smooth_speed * (current_dist-last_position) < 0)
  1327. {
  1328. last_position = current_dist;
  1329. }
  1330. smooth_speed_presentation_cnt=0;
  1331. }
  1332. else
  1333. {
  1334. if(smooth_speed_presentation_cnt<3)
  1335. smooth_speed_presentation_cnt++;
  1336. else
  1337. smooth_speed_presentation=0;
  1338. }
  1339. if(fabs(smooth_speed)<0.1)smooth_speed_presentation=0;
  1340. double revise_para = 0.2;
  1341. if(fabs(smooth_speed) < 0.01 || smooth_speed * (current_dist-last_position) < 0)
  1342. revise_para=0;
  1343. last_position=last_position+(current_dist-last_position)*revise_para;
  1344. }
  1345. smooth_last_position = point(dstp.x + last_position * rec_kx, dstp.y + last_position * rec_ky);
  1346. smooth_last_true_position = pt;
  1347. smooth_last_time_sec = t;
  1348. }
  1349. else
  1350. {
  1351. smooth_reset();
  1352. smooth_initiate(pt, t, sit);
  1353. }
  1354. }
  1355. if(m_lp != nullptr)//m_time,m_ct,x,y,m_speed,m_stat
  1356. {
  1357. smooth_set_loc_point(t, ct, sit, m_lp);
  1358. }
  1359. }
  1360. void base_card::smooth_dist_man(point &pt, double t, int ct, const site*sit, point dstp,loc_point *m_lp)
  1361. {
  1362. if(smooth_line.empty() || !smooth_line.contain(pt,0.1))
  1363. {
  1364. if(!smooth_line_reset)
  1365. {
  1366. smooth_reset();
  1367. smooth_line_reset=true;
  1368. }
  1369. else
  1370. {
  1371. std::vector<point> path=card_path::inst().find_path(smooth_last_true_position, pt);
  1372. if(!path.empty() && smooth_last_true_position.dist(path[0])>200)
  1373. path.clear();
  1374. if(path.empty())
  1375. {
  1376. smooth_line.set(smooth_last_true_position, pt);
  1377. smooth_line_reset=false;
  1378. }
  1379. else
  1380. {
  1381. smooth_reset();
  1382. }
  1383. }
  1384. }
  1385. else
  1386. smooth_line_reset=false;
  1387. if(!smooth_initial_setting)
  1388. {
  1389. smooth_initiate(pt, t, sit);
  1390. }
  1391. else
  1392. {
  1393. double current_dist = dstp.dist_direct(pt);
  1394. double last_true_position = dstp.dist_direct(smooth_last_true_position);
  1395. double max_span = 100;
  1396. if(fabs(current_dist-last_true_position)<max_span && t - smooth_last_time_sec < 10)
  1397. {
  1398. //计算人卡速度
  1399. double new_speed = (current_dist-last_true_position) / (t - smooth_last_time_sec);
  1400. double speed_differ = fabs(new_speed-smooth_speed);
  1401. if(speed_differ>1)new_speed=smooth_speed +1*(new_speed>smooth_speed?1:-1);
  1402. smooth_speed = smooth_speed * 0.4 + new_speed * 0.6;
  1403. smooth_last_true_position = pt;
  1404. smooth_last_position = pt;
  1405. smooth_last_time_sec = t;
  1406. if(fabs(smooth_speed_presentation)<1e-6 ||_isnan(smooth_speed_presentation))
  1407. {
  1408. smooth_speed_presentation=fabs(smooth_speed);
  1409. }
  1410. else
  1411. smooth_speed_presentation = smooth_speed_presentation * 0.4 + fabs(smooth_speed) * 0.6;
  1412. if(fabs(smooth_speed)<0.1)smooth_speed_presentation=0;
  1413. }
  1414. else
  1415. {
  1416. smooth_reset();
  1417. smooth_initiate(pt, t, sit);
  1418. }
  1419. }
  1420. if(m_lp != nullptr)//m_time,m_ct,x,y,m_speed,m_stat
  1421. {
  1422. smooth_set_loc_point(t, ct, sit, m_lp);
  1423. }
  1424. }
  1425. void base_card::smooth_set_loc_point(double t, int ct, const site*sit, loc_point *lp)
  1426. {
  1427. point pt;
  1428. if(smooth_halt_condition)
  1429. pt.set(smooth_halt_position);
  1430. else
  1431. pt.set(smooth_last_position);
  1432. lp->m_dist2=sit->dist_direct(pt);
  1433. lp->m_smooth_x=pt.x;
  1434. lp->m_smooth_y=pt.y;
  1435. lp->m_time=(int64_t)(t*1000);
  1436. lp->m_ct=ct;
  1437. if(smooth_halt_condition)
  1438. {
  1439. lp->m_speed=0;
  1440. lp->m_stat=0;
  1441. }
  1442. else
  1443. {
  1444. lp->m_speed=smooth_speed_presentation * (3.6*sit->m_scale) ; //(m/s) to (km/h)
  1445. if(smooth_speed<0)
  1446. lp->m_speed = -lp->m_speed;
  1447. if(_isnan(lp->m_speed))
  1448. lp->m_speed=0;
  1449. lp->m_stat=1;
  1450. //if(fabs(smooth_speed_presentation) < 0.1)
  1451. // lp->m_speed=0;
  1452. }
  1453. }
  1454. void base_card::generate_list(point &pt, const site*sit, bool is_whole_list)
  1455. {
  1456. if(is_whole_list)
  1457. {
  1458. put_loc_point(pt, sit, m_d(0).m_ct, m_d(0));
  1459. }
  1460. else
  1461. {
  1462. put_single_loc_point(pt, sit, m_d(0).m_ct, m_d(0));
  1463. }
  1464. }
  1465. void base_card::put_single_loc_point(point &pt, const site*sit, int ct, loc_point &lp)
  1466. {
  1467. point rpt;
  1468. rpt.set(pt);
  1469. turning_mapping(rpt,sit);
  1470. if(!card_path::inst().is_at_path(rpt)) //if point not on the path
  1471. {
  1472. //lp.debug_out();
  1473. /*printf("out of path:t=%ld,sit=%d,card=%d,ct=%d,"
  1474. "tof1=%d,tof2=%d,pt=(%.2lf,%.2lf)\n",
  1475. m_d(0).m_time, m_d(0).m_sid, m_d(0).m_cid,m_d(0).m_ct,
  1476. m_d(0).m_tof[0], m_d(0).m_tof[1], pt.x, pt.y);*/
  1477. debug_print_syslog(0,"[not on path----]cardid: %s,ct: %d,x: %.3f, y: %.3f.\n",m_card->cardId().c_str(), m_ct,rpt.x,rpt.y);
  1478. return;
  1479. }
  1480. point dstp; //projection
  1481. if(!m_if_turning)
  1482. {
  1483. point tmp;
  1484. tmp.set(sit->x,sit->y);
  1485. sit->mapping(tmp,dstp);
  1486. }
  1487. else
  1488. {
  1489. dstp.set(m_turning_pt);
  1490. }
  1491. push_data_point dp;
  1492. dp.sit=sit;
  1493. dp.dstp.set(dstp);
  1494. dp.ct=ct;
  1495. dp.valid=true;
  1496. dp.stop_cnt=5;
  1497. dp.lp=lp;
  1498. dp.set(rpt);
  1499. { //don't delete the braces
  1500. atomic_lock lock(mutex);
  1501. m_push_list.clear();
  1502. m_push_list.push(dp);
  1503. }
  1504. }
  1505. void base_card::put_loc_point(point &pt, const site*sit, int ct, loc_point &lp)
  1506. {
  1507. point rpt;
  1508. rpt.set(pt);
  1509. turning_mapping(rpt,sit);
  1510. if(!card_path::inst().is_at_path(pt)) //if point not on the path
  1511. {
  1512. lp.debug_out();
  1513. /*printf("out of path:t=%ld,sit=%d,card=%d,ct=%d,"
  1514. "tof1=%d,tof2=%d,pt=(%.2lf,%.2lf)\n",
  1515. m_d(0).m_time, m_d(0).m_sid, m_d(0).m_cid,m_d(0).m_ct,
  1516. m_d(0).m_tof[0], m_d(0).m_tof[1], pt.x, pt.y);*/
  1517. return;
  1518. }
  1519. point dstp; //projection
  1520. if(!m_if_turning)
  1521. {
  1522. point tmp;
  1523. tmp.set(sit->x,sit->y);
  1524. sit->mapping(tmp,dstp);
  1525. }
  1526. else
  1527. {
  1528. dstp.set(m_turning_pt);
  1529. }
  1530. int size = 0;
  1531. push_data_point dp[13];
  1532. dp[size].sit=sit;
  1533. dp[size].dstp.set(dstp);
  1534. dp[size].ct=ct;
  1535. dp[size].valid=true;
  1536. dp[size].stop_cnt=5;
  1537. dp[size].lp=lp;
  1538. dp[size].set(rpt);
  1539. double missing_time = m_d(0).m_time/1000.;
  1540. size++;
  1541. for(;size<13;size++)
  1542. {
  1543. dp[size].sit=sit;
  1544. dp[size].dstp.set(dstp);
  1545. dp[size].ct = ct;
  1546. dp[size].valid=true;
  1547. dp[size].stop_cnt=5;
  1548. double mt = missing_time + size;
  1549. double missing_dist = estimate_point_by_history(sit, mt);
  1550. point missing_point = convert_dist_to_pt(missing_dist, sit);
  1551. if(!card_path::inst().is_at_path(missing_point)) //if point not on the path
  1552. {
  1553. break;
  1554. }
  1555. turning_mapping(missing_point,sit); //turning
  1556. dp[size].set(missing_point);
  1557. dp[size].lp.set(missing_point);
  1558. dp[size].lp.m_time=(int64_t)(missing_time * 1000);
  1559. dp[size].lp.m_sid = sit->m_id;
  1560. dp[size].lp.m_cid = m_d(0).m_cid;
  1561. }
  1562. { //don't delete the braces
  1563. atomic_lock lock(mutex);
  1564. m_push_list.clear();
  1565. for(int i=0;i<size;i++)
  1566. {
  1567. m_push_list.push(dp[i]);
  1568. }
  1569. }
  1570. }
  1571. loc_point base_card::grab_loc_point()
  1572. {
  1573. loc_point lp;
  1574. push_data_point dpt;
  1575. { //don't delete the braces
  1576. atomic_lock lock(mutex);
  1577. if (m_push_list.empty()) //empty
  1578. {
  1579. return lp;
  1580. }
  1581. else if(m_push_list.size()==1) //size=1
  1582. {
  1583. dpt = m_push_list[0];
  1584. m_push_list[0].valid=false;
  1585. if(m_push_list[0].stop_cnt>5)m_push_list[0].stop_cnt=5;
  1586. if(m_push_list[0].stop_cnt>0)m_push_list[0].stop_cnt--;
  1587. }
  1588. else{ //size>1
  1589. dpt = m_push_list[0];
  1590. m_push_list.skip(1);
  1591. }
  1592. }
  1593. point pt;
  1594. pt.set(dpt);
  1595. double current_t=time(NULL);
  1596. if(dpt.valid)
  1597. {
  1598. if (m_card->smoothFlag())
  1599. {
  1600. smooth_dist_car(pt, current_t, dpt.ct, dpt.sit, dpt.dstp, &lp);
  1601. lp.set(lp.m_smooth_x,lp.m_smooth_y);
  1602. }
  1603. else
  1604. {
  1605. smooth_dist_man(pt, current_t, dpt.ct, dpt.sit, dpt.dstp, &lp);
  1606. lp.set(pt);
  1607. }
  1608. dpt.lp.m_dist2=lp.m_dist2;
  1609. dpt.lp.m_time=lp.m_time;
  1610. dpt.lp.m_ct=lp.m_ct;
  1611. dpt.lp.set(lp);
  1612. dpt.lp.m_dist=dpt.sit->dist_direct(dpt);
  1613. dpt.lp.debug_out();
  1614. }
  1615. else
  1616. {
  1617. //if(dpt.stop_cnt<=0)smooth_line.clear();
  1618. smooth_set_loc_point(current_t, dpt.ct, dpt.sit, &lp);
  1619. if (m_card->smoothFlag())
  1620. {
  1621. lp.set(lp.m_smooth_x,lp.m_smooth_y);
  1622. if(dpt.stop_cnt<=0)
  1623. {
  1624. lp.m_speed=0;
  1625. lp.m_stat=0;
  1626. }
  1627. }
  1628. else
  1629. {
  1630. if(dpt.stop_cnt<=0)
  1631. {
  1632. lp.m_speed=0;
  1633. lp.m_stat=0;
  1634. }
  1635. lp.set(pt);
  1636. }
  1637. }
  1638. return lp;
  1639. }
  1640. void base_card::his_reset()
  1641. {
  1642. his_speed = 0;
  1643. his_speed_presentation = 0;
  1644. his_last_time = 0;
  1645. his_last_pt.set(0, 0);
  1646. his_line.clear();
  1647. }
  1648. std::vector<his_location> base_card::make_up_more_points_for_history(point &pt, uint64_t t,const site * sit)
  1649. {
  1650. //debug_print_syslog(0,"[sit_scale make_up_more_points_for_history]%f",sit->m_scale);
  1651. std::vector<his_location> retv;
  1652. if (sit->is_path_empty())
  1653. {
  1654. return std::move(retv);
  1655. }
  1656. double RESET_SPEED_TIME=2;
  1657. double checkTime=3; // car card
  1658. double speedChangeLimit=1;
  1659. if(!m_card->smoothFlag()) // man card // IMPORTANT:uncomment when transplant
  1660. {
  1661. speedChangeLimit=0.5;
  1662. checkTime=5;
  1663. }
  1664. double cur_time = t / 1000.;
  1665. if(his_last_pt.empty() || cur_time - his_last_time > 120)
  1666. {
  1667. if(!his_last_pt.empty())
  1668. retv.push_back(his_location(his_last_pt, uint64_t((his_last_time + RESET_SPEED_TIME)*1000), 0, false)) ;
  1669. his_reset();
  1670. his_last_time = cur_time;
  1671. his_last_pt.set(pt);
  1672. retv.push_back(his_location(pt, t, 0, false));
  1673. return std::move(retv);
  1674. }
  1675. if(his_last_pt==pt)return std::move(retv);
  1676. //line check: get critical point and total length
  1677. double sum_len = 0;
  1678. std::vector<point> path;
  1679. std::vector<double> sec_len;
  1680. if(his_line.empty() || !his_line.contain(pt,0.1))
  1681. {
  1682. path=card_path::inst().find_path(his_last_pt, pt);
  1683. if(!path.empty() && his_last_pt.dist(path[0])>200)
  1684. path.clear();
  1685. if(path.empty())
  1686. {
  1687. his_line.set(his_last_pt, pt);
  1688. sum_len = his_last_pt.dist_direct(pt);
  1689. sec_len.push_back(fabs(sum_len));
  1690. }
  1691. else
  1692. {
  1693. his_line.set(path.back(), pt);
  1694. point tmp = his_last_pt;
  1695. for(std::vector<point>::iterator i=path.begin();i!=path.end();i++)
  1696. {
  1697. point cur = *i;
  1698. double len = tmp.dist(cur);
  1699. sec_len.push_back(len);
  1700. sum_len += len;
  1701. tmp = cur;
  1702. }
  1703. double len = tmp.dist(pt);
  1704. sec_len.push_back(len);
  1705. sum_len += len;
  1706. }
  1707. }
  1708. else
  1709. {
  1710. sum_len = his_last_pt.dist_direct(pt);
  1711. sec_len.push_back(fabs(sum_len));
  1712. }
  1713. //update his_speed
  1714. double cur_speed = sum_len / (cur_time - his_last_time);
  1715. double new_his_speed;
  1716. if(fabs(his_speed)<1e-6 || _isnan(his_speed))
  1717. {
  1718. new_his_speed=cur_speed;
  1719. if(new_his_speed>25/(3.6*sit->m_scale))new_his_speed=25/(3.6*sit->m_scale);
  1720. else if(new_his_speed<-25/(3.6*sit->m_scale))new_his_speed=-25/(3.6*sit->m_scale);
  1721. }
  1722. else
  1723. {
  1724. if(!path.empty())
  1725. {
  1726. double end_d = (*path.rbegin()).dist_direct(pt);
  1727. if(fabs(end_d)>=1e-6)
  1728. {
  1729. double speed_sign = fabs(end_d) / end_d;
  1730. cur_speed = fabs(cur_speed) * speed_sign;
  1731. his_speed = fabs(his_speed) * speed_sign;
  1732. }
  1733. }
  1734. if(cur_time - his_last_time < 4)
  1735. {
  1736. double tmp_cur = cur_speed;
  1737. double speed_differ = fabs(tmp_cur-his_speed);
  1738. if(speed_differ>speedChangeLimit)
  1739. tmp_cur = his_speed + speedChangeLimit*(tmp_cur>his_speed?1:-1);
  1740. new_his_speed = his_speed * 0.4 + tmp_cur * 0.6;
  1741. }
  1742. else
  1743. {
  1744. new_his_speed = cur_speed;
  1745. if(new_his_speed>25/(3.6*sit->m_scale))new_his_speed=25/(3.6*sit->m_scale);
  1746. else if(new_his_speed<-25/(3.6*sit->m_scale))new_his_speed=-25/(3.6*sit->m_scale);
  1747. }
  1748. }
  1749. if(_isnan(new_his_speed))
  1750. new_his_speed=0;
  1751. if(fabs(new_his_speed)<0.1)
  1752. {
  1753. if(his_speed!=0)
  1754. {
  1755. double halt_time = cur_time-his_last_time>RESET_SPEED_TIME?his_last_time+RESET_SPEED_TIME:cur_time;
  1756. retv.push_back(his_location(his_last_pt, uint64_t(halt_time*1000), 0, false));
  1757. }
  1758. his_last_time = cur_time;
  1759. his_speed = 0;
  1760. return std::move(retv);
  1761. }
  1762. //speed check
  1763. double speed;
  1764. if(fabs(his_speed) > fabs(cur_speed) * 1.5 && cur_time - his_last_time > checkTime)
  1765. speed = fabs(his_speed);
  1766. else
  1767. speed = fabs(cur_speed);
  1768. //insert vector
  1769. point tmp_pt = his_last_pt;
  1770. double tmp_t = his_last_time;
  1771. std::vector<double>::iterator len_i=sec_len.begin();
  1772. for(std::vector<point>::iterator path_i=path.begin();path_i!=path.end();path_i++,len_i++)
  1773. {
  1774. double len=*len_i;
  1775. point tmp_pt2 = *path_i;
  1776. double tmp_t2 = tmp_t + len / speed;
  1777. if(tmp_t2 - tmp_t > checkTime)
  1778. {
  1779. retv.push_back(his_location(tmp_pt, uint64_t(tmp_t*1000),
  1780. tmp_pt2, uint64_t(tmp_t2*1000), new_his_speed*(3.6*sit->m_scale),false));
  1781. }
  1782. retv.push_back(his_location(tmp_pt2, uint64_t(tmp_t2*1000), new_his_speed*(3.6*sit->m_scale),true));
  1783. tmp_pt = tmp_pt2;
  1784. tmp_t = tmp_t2;
  1785. }
  1786. double len=*len_i;
  1787. double end_time_sec = tmp_t + len / speed;
  1788. if(end_time_sec - tmp_t > checkTime)
  1789. {
  1790. retv.push_back(his_location(tmp_pt, uint64_t(tmp_t*1000),
  1791. pt, uint64_t(end_time_sec*1000), new_his_speed*(3.6*sit->m_scale),false));
  1792. }
  1793. retv.push_back(his_location(pt, uint64_t(end_time_sec*1000), new_his_speed*(3.6*sit->m_scale),false));
  1794. if(cur_time - end_time_sec > checkTime)
  1795. retv.push_back(his_location(pt, uint64_t((end_time_sec + RESET_SPEED_TIME)*1000), 0,false));
  1796. //update history
  1797. his_last_time = cur_time;
  1798. his_last_pt.set(pt);
  1799. his_speed = new_his_speed;
  1800. return std::move(retv);
  1801. }
  1802. NAMESPACE_POINT_END(NAMESPACE_POINT)
  1803. /*
  1804. std::unique_ptr<sit_list> sites(new sit_list());
  1805. inline const char* now(char*date_str,uint64_t time)
  1806. {
  1807. time_t ntime=time/1000;
  1808. struct tm buff;
  1809. const struct tm*t=localtime_r(&ntime, &buff);
  1810. int main()
  1811. {
  1812. sites->load("data_reader_antenna.txt","dat_reader_path_tof.txt");
  1813. card_path::init(*sites);
  1814. printf("%s\n",sites->to_string().c_str());
  1815. fflush(stdout);
  1816. #ifdef _SMOOTH
  1817. message_file mf(stdin); //../raw_s_20171201.log.02
  1818. #else
  1819. message_file mf(stdin);
  1820. #endif
  1821. uint64_t ms;
  1822. char buf[2048];
  1823. std::unique_ptr<std::array<card,2000>> cards(new std::array<card,2000>());
  1824. for(int i=0,len=cards->size();i<len;i++)
  1825. cards->at(i).m_id=i;
  1826. int len;
  1827. while((len=mf.get_line(&ms,buf,sizeof(buf))))
  1828. {
  1829. loc_package lp(ms,buf);
  1830. for(int i=0,ct=lp.count();i<ct;i++)
  1831. {
  1832. loc_info li=lp.get(i);
  1833. if(li.m_card_type!=2) continue;
  1834. // if(li.m_card_id!=1222) continue;
  1835. card&c=cards->at(li.m_card_id);
  1836. printf("t=%ld,sit=%d,ant=%d,card=%d,ct=%d,tof=%d,rav=%d,acc=%d,rsp=%.1f\n"
  1837. ,li.m_loc_time,lp.m_sit_id, li.m_ant_id, c.m_id ,li.m_card_ct,li.m_tof
  1838. ,li.m_rav,li.m_acc,li.m_card_sp );
  1839. c.on_tof_data(&(*sites)[lp.m_sit_id],li);
  1840. }
  1841. }
  1842. return 0;
  1843. }
  1844. */