ivscheduler.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. #include "ivscheduler.h"
  2. #include <iostream>
  3. static ivscheduler * givs;
  4. static QMutex gMutexGPSIMU;
  5. static iv::gps::gpsimu ggpsimu;
  6. static bool gbUpdate = false;
  7. class xodrobj
  8. {
  9. public:
  10. double flatsrc;
  11. double flonsrc;
  12. double fhgdsrc;
  13. double flat;
  14. double flon;
  15. int lane;
  16. };
  17. void Listengpsimu(const char * strdata,const unsigned int nSize,const unsigned int index,const QDateTime * dt,const char * strmemname)
  18. {
  19. // ggpsimu->UpdateGPSIMU(strdata,nSize);
  20. iv::gps::gpsimu xgpsimu;
  21. if(!xgpsimu.ParseFromArray(strdata,nSize))
  22. {
  23. std::cout<<"Listengpsimu Parse error."<<std::endl;
  24. return;
  25. }
  26. (void)&index;
  27. (void)dt;
  28. (void)strmemname;
  29. gMutexGPSIMU.lock();
  30. ggpsimu.CopyFrom(xgpsimu);
  31. gbUpdate = true;
  32. gMutexGPSIMU.unlock();
  33. }
  34. void Listenscheduler(const char * strdata,const unsigned int nSize,const unsigned int index,const QDateTime * dt,const char * strmemname)
  35. {
  36. // ggpsimu->UpdateGPSIMU(strdata,nSize);
  37. iv::scheduler pscheduler;
  38. if(!pscheduler.ParseFromArray(strdata,nSize))
  39. {
  40. std::cout<<"Listenscheduler Parse error."<<std::endl;
  41. return;
  42. }
  43. (void)&index;
  44. (void)dt;
  45. (void)strmemname;
  46. givs->setscheduler(&pscheduler);
  47. }
  48. ivscheduler::ivscheduler(std::string strmemname,std::string strschedulername)
  49. {
  50. givs = this;
  51. mstrmemname = strmemname;
  52. mstrschedulername = strschedulername;
  53. iv::modulecomm::RegisterRecv(mstrmemname.data(),Listengpsimu);
  54. iv::modulecomm::RegisterRecv(mstrschedulername.data(),Listenscheduler);
  55. mpadst = iv::modulecomm::RegisterSend("xodrreq",1000,1);
  56. }
  57. void ivscheduler::run()
  58. {
  59. iv::gps::gpsimu xgpsimu;
  60. while(!QThread::isInterruptionRequested())
  61. {
  62. if(gbUpdate)
  63. {
  64. gMutexGPSIMU.lock();
  65. xgpsimu.CopyFrom(ggpsimu);
  66. gbUpdate = false;
  67. gMutexGPSIMU.unlock();
  68. emit updategps(xgpsimu.lon(),xgpsimu.lat(),xgpsimu.heading());
  69. if(mpscheduler == 0)continue;
  70. mMutex.lock();
  71. if(mncycle<mncyclecount)
  72. {
  73. if(mnprocess<mpscheduler->mscheduler_unit_size())
  74. {
  75. switch (mnstep) {
  76. case 0:
  77. {
  78. mfLatInit = xgpsimu.lat();
  79. mfLonInit = xgpsimu.lon();
  80. mfHeadingInit = xgpsimu.heading();
  81. mnTimeInit = QDateTime::currentMSecsSinceEpoch();
  82. //Sending Obj;
  83. iv::scheduler_unit * pscheduler_unit = mpscheduler->mutable_mscheduler_unit(mnprocess);
  84. mfLatObj = pscheduler_unit->mflat();
  85. mfLonObj = pscheduler_unit->mflon();
  86. mfHeadingObj = pscheduler_unit->mfheading();
  87. mnLastSendObj = QDateTime::currentMSecsSinceEpoch();
  88. SendObj(mfLonObj,mfLatObj);
  89. mnstep = 1;
  90. emit updatestep(mnstep);
  91. }
  92. break;
  93. case 1:
  94. if(IsVehicleMoving(&xgpsimu))
  95. {
  96. mnstep = 2;
  97. emit updatestep(mnstep);
  98. }
  99. else
  100. {
  101. if((QDateTime::currentMSecsSinceEpoch() - mnLastSendObj) > 3000)
  102. {
  103. //Send Obj
  104. SendObj(mfLonObj,mfLatObj);
  105. mnLastSendObj = QDateTime::currentMSecsSinceEpoch();
  106. }
  107. }
  108. if(IsVehcileArrivingStation(&xgpsimu))
  109. {
  110. mnArrivingTime = QDateTime::currentMSecsSinceEpoch();
  111. mnstep = 3;
  112. emit updatestep(mnstep);
  113. }
  114. //Decide Vechicle Running.
  115. break;
  116. case 2:
  117. if(IsVehcileArrivingStation(&xgpsimu))
  118. {
  119. mnArrivingTime = QDateTime::currentMSecsSinceEpoch();
  120. mnstep = 3;
  121. emit updatestep(mnstep);
  122. }
  123. //Decide Vehiclde Arrive End Point;
  124. break;
  125. case 3:
  126. {
  127. iv::scheduler_unit * pscheduler_unit = mpscheduler->mutable_mscheduler_unit(mnprocess);
  128. if((QDateTime::currentMSecsSinceEpoch() - mnArrivingTime)>= (pscheduler_unit->mstopsecond()*1000) )
  129. {
  130. mnstep = 4;
  131. emit updatestep(mnstep);
  132. }
  133. }
  134. //Count Stop Time;
  135. break;
  136. case 4:
  137. mnstep = 0;
  138. emit updatestep(mnstep);
  139. mnprocess++;
  140. break;
  141. default:
  142. break;
  143. }
  144. }
  145. else
  146. {
  147. mncycle++;
  148. mnprocess = 0;
  149. }
  150. }
  151. emit updatestate(mncycle,mncyclecount,mnprocess,mpscheduler->mscheduler_unit_size());
  152. mMutex.unlock();
  153. }
  154. else
  155. {
  156. msleep(10);
  157. }
  158. }
  159. }
  160. void ivscheduler::setscheduler(iv::scheduler *pscheduler)
  161. {
  162. mMutex.lock();
  163. if(mpscheduler != 0)delete mpscheduler;
  164. mpscheduler = new iv::scheduler;
  165. mpscheduler->CopyFrom(*pscheduler);
  166. mncyclecount = pscheduler->mcyble();
  167. mncycle = 0;
  168. mnprocess = 0;
  169. mnstep = 0;
  170. mMutex.unlock();
  171. }
  172. void ivscheduler::GetProcess(int &nProc, int &nProcTotal)
  173. {
  174. if(mpscheduler == 0)
  175. {
  176. nProc = 0;
  177. nProcTotal = 0;
  178. return;
  179. }
  180. mMutex.lock();
  181. nProcTotal = mpscheduler->mscheduler_unit_size();
  182. nProc = mnprocess;
  183. mMutex.unlock();
  184. }
  185. void ivscheduler::GetCycle(int &ncycle, int &ncyclecount)
  186. {
  187. ncycle = mncycle;
  188. ncyclecount = mncyclecount;
  189. }
  190. bool ivscheduler::IsVehicleMoving(iv::gps::gpsimu * pgpsimu)
  191. {
  192. double dis;
  193. double x1,y1;
  194. double x2,y2;
  195. GaussProjCal(mfLonInit,mfLatInit,&x1,&y1);
  196. GaussProjCal(pgpsimu->lon(),pgpsimu->lat(),&x2,&y2);
  197. dis = sqrt(pow(x1-x2,2)+pow(y1-y2,2));
  198. if(dis > 10.0)return true;
  199. return false;
  200. }
  201. bool ivscheduler::IsVehcileArrivingStation(iv::gps::gpsimu *pgpsimu)
  202. {
  203. double dis;
  204. double x1,y1;
  205. double x2,y2;
  206. GaussProjCal(mfLonObj,mfLatObj,&x1,&y1);
  207. GaussProjCal(pgpsimu->lon(),pgpsimu->lat(),&x2,&y2);
  208. dis = sqrt(pow(x1-x2,2)+pow(y1-y2,2));
  209. if(dis < 10.0)
  210. {
  211. double fheaddiff = pgpsimu->heading() - mfHeadingObj;
  212. if(fheaddiff< 0)fheaddiff = fheaddiff + 360.0;
  213. if((fheaddiff>300)||(fheaddiff<60))
  214. return true;
  215. }
  216. return false;
  217. }
  218. void ivscheduler::SendObj(double flon, double flat)
  219. {
  220. xodrobj xo;
  221. xo.flon = flon;
  222. xo.flat = flat;
  223. xo.lane = 3;
  224. iv::modulecomm::ModuleSendMsg(mpadst,(char *)&xo,sizeof(xodrobj));
  225. }