| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257 |
- #include "ivscheduler.h"
- #include <iostream>
- static ivscheduler * givs;
- static QMutex gMutexGPSIMU;
- static iv::gps::gpsimu ggpsimu;
- static bool gbUpdate = false;
- class xodrobj
- {
- public:
- double flatsrc;
- double flonsrc;
- double fhgdsrc;
- double flat;
- double flon;
- int lane;
- };
- void Listengpsimu(const char * strdata,const unsigned int nSize,const unsigned int index,const QDateTime * dt,const char * strmemname)
- {
- // ggpsimu->UpdateGPSIMU(strdata,nSize);
- iv::gps::gpsimu xgpsimu;
- if(!xgpsimu.ParseFromArray(strdata,nSize))
- {
- std::cout<<"Listengpsimu Parse error."<<std::endl;
- return;
- }
- (void)&index;
- (void)dt;
- (void)strmemname;
- gMutexGPSIMU.lock();
- ggpsimu.CopyFrom(xgpsimu);
- gbUpdate = true;
- gMutexGPSIMU.unlock();
- }
- void Listenscheduler(const char * strdata,const unsigned int nSize,const unsigned int index,const QDateTime * dt,const char * strmemname)
- {
- // ggpsimu->UpdateGPSIMU(strdata,nSize);
- iv::scheduler pscheduler;
- if(!pscheduler.ParseFromArray(strdata,nSize))
- {
- std::cout<<"Listenscheduler Parse error."<<std::endl;
- return;
- }
- (void)&index;
- (void)dt;
- (void)strmemname;
- givs->setscheduler(&pscheduler);
- }
- ivscheduler::ivscheduler(std::string strmemname,std::string strschedulername)
- {
- givs = this;
- mstrmemname = strmemname;
- mstrschedulername = strschedulername;
- iv::modulecomm::RegisterRecv(mstrmemname.data(),Listengpsimu);
- iv::modulecomm::RegisterRecv(mstrschedulername.data(),Listenscheduler);
- mpadst = iv::modulecomm::RegisterSend("xodrreq",1000,1);
- }
- void ivscheduler::run()
- {
- iv::gps::gpsimu xgpsimu;
- while(!QThread::isInterruptionRequested())
- {
- if(gbUpdate)
- {
- gMutexGPSIMU.lock();
- xgpsimu.CopyFrom(ggpsimu);
- gbUpdate = false;
- gMutexGPSIMU.unlock();
- emit updategps(xgpsimu.lon(),xgpsimu.lat(),xgpsimu.heading());
- if(mpscheduler == 0)continue;
- mMutex.lock();
- if(mncycle<mncyclecount)
- {
- if(mnprocess<mpscheduler->mscheduler_unit_size())
- {
- switch (mnstep) {
- case 0:
- {
- mfLatInit = xgpsimu.lat();
- mfLonInit = xgpsimu.lon();
- mfHeadingInit = xgpsimu.heading();
- mnTimeInit = QDateTime::currentMSecsSinceEpoch();
- //Sending Obj;
- iv::scheduler_unit * pscheduler_unit = mpscheduler->mutable_mscheduler_unit(mnprocess);
- mfLatObj = pscheduler_unit->mflat();
- mfLonObj = pscheduler_unit->mflon();
- mfHeadingObj = pscheduler_unit->mfheading();
- mnLastSendObj = QDateTime::currentMSecsSinceEpoch();
- SendObj(mfLonObj,mfLatObj);
- mnstep = 1;
- emit updatestep(mnstep);
- }
- break;
- case 1:
- if(IsVehicleMoving(&xgpsimu))
- {
- mnstep = 2;
- emit updatestep(mnstep);
- }
- else
- {
- if((QDateTime::currentMSecsSinceEpoch() - mnLastSendObj) > 3000)
- {
- //Send Obj
- SendObj(mfLonObj,mfLatObj);
- mnLastSendObj = QDateTime::currentMSecsSinceEpoch();
- }
- }
- if(IsVehcileArrivingStation(&xgpsimu))
- {
- mnArrivingTime = QDateTime::currentMSecsSinceEpoch();
- mnstep = 3;
- emit updatestep(mnstep);
- }
- //Decide Vechicle Running.
- break;
- case 2:
- if(IsVehcileArrivingStation(&xgpsimu))
- {
- mnArrivingTime = QDateTime::currentMSecsSinceEpoch();
- mnstep = 3;
- emit updatestep(mnstep);
- }
- //Decide Vehiclde Arrive End Point;
- break;
- case 3:
- {
- iv::scheduler_unit * pscheduler_unit = mpscheduler->mutable_mscheduler_unit(mnprocess);
- if((QDateTime::currentMSecsSinceEpoch() - mnArrivingTime)>= (pscheduler_unit->mstopsecond()*1000) )
- {
- mnstep = 4;
- emit updatestep(mnstep);
- }
- }
- //Count Stop Time;
- break;
- case 4:
- mnstep = 0;
- emit updatestep(mnstep);
- mnprocess++;
- break;
- default:
- break;
- }
- }
- else
- {
- mncycle++;
- mnprocess = 0;
- }
- }
- emit updatestate(mncycle,mncyclecount,mnprocess,mpscheduler->mscheduler_unit_size());
- mMutex.unlock();
- }
- else
- {
- msleep(10);
- }
- }
- }
- void ivscheduler::setscheduler(iv::scheduler *pscheduler)
- {
- mMutex.lock();
- if(mpscheduler != 0)delete mpscheduler;
- mpscheduler = new iv::scheduler;
- mpscheduler->CopyFrom(*pscheduler);
- mncyclecount = pscheduler->mcyble();
- mncycle = 0;
- mnprocess = 0;
- mnstep = 0;
- mMutex.unlock();
- }
- void ivscheduler::GetProcess(int &nProc, int &nProcTotal)
- {
- if(mpscheduler == 0)
- {
- nProc = 0;
- nProcTotal = 0;
- return;
- }
- mMutex.lock();
- nProcTotal = mpscheduler->mscheduler_unit_size();
- nProc = mnprocess;
- mMutex.unlock();
- }
- void ivscheduler::GetCycle(int &ncycle, int &ncyclecount)
- {
- ncycle = mncycle;
- ncyclecount = mncyclecount;
- }
- bool ivscheduler::IsVehicleMoving(iv::gps::gpsimu * pgpsimu)
- {
- double dis;
- double x1,y1;
- double x2,y2;
- GaussProjCal(mfLonInit,mfLatInit,&x1,&y1);
- GaussProjCal(pgpsimu->lon(),pgpsimu->lat(),&x2,&y2);
- dis = sqrt(pow(x1-x2,2)+pow(y1-y2,2));
- if(dis > 10.0)return true;
- return false;
- }
- bool ivscheduler::IsVehcileArrivingStation(iv::gps::gpsimu *pgpsimu)
- {
- double dis;
- double x1,y1;
- double x2,y2;
- GaussProjCal(mfLonObj,mfLatObj,&x1,&y1);
- GaussProjCal(pgpsimu->lon(),pgpsimu->lat(),&x2,&y2);
- dis = sqrt(pow(x1-x2,2)+pow(y1-y2,2));
- if(dis < 10.0)
- {
- double fheaddiff = pgpsimu->heading() - mfHeadingObj;
- if(fheaddiff< 0)fheaddiff = fheaddiff + 360.0;
- if((fheaddiff>300)||(fheaddiff<60))
- return true;
- }
- return false;
- }
- void ivscheduler::SendObj(double flon, double flat)
- {
- xodrobj xo;
- xo.flon = flon;
- xo.flat = flat;
- xo.lane = 3;
- iv::modulecomm::ModuleSendMsg(mpadst,(char *)&xo,sizeof(xodrobj));
- }
|