Browse Source

change detection_lidar_PointPillars_MultiHead. fix a memory problem.

yuchuli 3 years ago
parent
commit
d6c048cd6c

+ 5 - 3
src/detection/detection_lidar_PointPillars_MultiHead/main.cpp

@@ -129,8 +129,9 @@ void GetLidarObj(std::vector<float> out_detections,std::vector<int> out_labels,
 
 void DectectOnePCD(const pcl::PointCloud<pcl::PointXYZI>::Ptr &pc_ptr)
 {
-    float* points_array = new float[pc_ptr->size() * kNumPointFeature];
-    PclXYZITToArray(pc_ptr, points_array, 1.0);
+    std::shared_ptr<float> points_array_ptr = std::shared_ptr<float>(new float[pc_ptr->size() * kNumPointFeature]);
+ //   float* points_array = new float[pc_ptr->size() * kNumPointFeature];
+    PclXYZITToArray(pc_ptr, points_array_ptr.get(), 1.0);
 
     int    in_num_points = pc_ptr->width;
 
@@ -143,7 +144,7 @@ void DectectOnePCD(const pcl::PointCloud<pcl::PointXYZI>::Ptr &pc_ptr)
     xTime.start();
 
     cudaDeviceSynchronize();
-    pPillars->DoInference(points_array, in_num_points, &out_detections, &out_labels , &out_scores);
+    pPillars->DoInference(points_array_ptr.get(), in_num_points, &out_detections, &out_labels , &out_scores);
     cudaDeviceSynchronize();
     int BoxFeature = 7;
     int num_objects = out_detections.size() / BoxFeature;
@@ -162,6 +163,7 @@ void DectectOnePCD(const pcl::PointCloud<pcl::PointXYZI>::Ptr &pc_ptr)
     int ntlen;
     std::string out = lidarobjvec.SerializeAsString();
     iv::modulecomm::ModuleSendMsg(gpdetect,out.data(),out.length());
+
 //    givlog->verbose("lenth is %d",out.length());
 }
 

+ 22 - 2
src/driver/driver_group_grpc_server/groupdb.cpp

@@ -10,6 +10,22 @@ groupdb::groupdb(std::string strdbpath)
 
 }
 
+groupdb::~groupdb()
+{
+    this->requestInterruption();
+    QTime xTime;
+    xTime.start();
+    while(mbRunning)
+    {
+        if(xTime.elapsed()>3000)
+        {
+            break;
+        }
+        std::this_thread::sleep_for(std::chrono::milliseconds(1));
+    }
+    qDebug("gropudb close.");
+
+}
 
 void groupdb::OpenDataBase()
 {
@@ -199,7 +215,7 @@ void groupdb::run()
 
             for(i=0;i<xvectordbmsg.size();i++)
             {
-                QSqlQuery query;
+                QSqlQuery query(mdatabase);
 
                 char strsen[1000];
                 snprintf(strsen,1000,"INSERT INTO groupdata(vehid,recordid,recvtime,msgtime,groupid,intragroupid,vehdata)"
@@ -241,6 +257,10 @@ void groupdb::run()
     }
 
     mdatabase.close();
+
+    qDebug("groupdb thread close");
+
+    mbRunning = false;
 }
 
 int groupdb::addmsg(qint64 recvtime, std::string strvehid, qint64 msgtime, int groupid,
@@ -301,7 +321,7 @@ qint64 groupdb::GetRecID(std::string strvehid,int groupid)
     if(bFind == false)
     {
 
-        QSqlQuery query;
+        QSqlQuery query(mdatabase);
 
         char strsen[1000];
         snprintf(strsen,1000,"INSERT INTO recorddata(recordid,groupid,vehid)"

+ 3 - 0
src/driver/driver_group_grpc_server/groupdb.h

@@ -40,6 +40,7 @@ class groupdb : public QThread
 {
 public:
     groupdb(std::string strdbpath);
+    ~groupdb();
 
 private:
     bool mbSQL = false;
@@ -105,6 +106,8 @@ private:
     void CheckRecID();
     int GetFilePathDir(QString & strdir);
 
+    bool mbRunning = true;
+
 
 };
 

+ 125 - 2
src/driver/driver_group_grpc_server/groupmsgbuf.cpp

@@ -17,9 +17,129 @@ groupmsgbuf::groupmsgbuf()
 
 }
 
+groupmsgbuf::~groupmsgbuf()
+{
+    if(gbSaveToDB)
+    {
+        delete mpgroupdb;
+    }
+    qDebug("groupmsgbuf close.");
+}
+
+void groupmsgbuf::WriteIVDData(char *str, int nsize, std::string strmemname, qint64 msgtime,QByteArray & ba)
+{
+    int nTotalSize,nHeadSize,nNameSize,nDataSize;
+    nHeadSize = sizeof(iv::RecordHead);
+    nNameSize = strnlen(strmemname.data(),255);
+    nDataSize = nsize;
+    nTotalSize = nHeadSize + nNameSize + nDataSize + 4*sizeof(int);
+    int nInfoStart;
+    nInfoStart = 4*sizeof(int);
+    char * strdata = new char[1+nTotalSize];
+    *strdata = 0x23;
+    memcpy(strdata+1,&nTotalSize,sizeof(int));
+    memcpy(strdata+1+1*sizeof(int),&nHeadSize,sizeof(int));
+    memcpy(strdata+1+2*sizeof(int),&nNameSize,sizeof(int));
+    memcpy(strdata+1+3*sizeof(int),&nDataSize,sizeof(int));
+
+    iv::RecordHead ird;
+    ird.mnDataIndex = mnIndex;mnIndex++;
+    QDateTime Xdt = QDateTime::fromMSecsSinceEpoch(msgtime);
+    QDateTime * dt = &Xdt;
+    ird.mDay =  dt->date().day();
+    ird.mHour = dt->time().hour();
+    ird.mMin = dt->time().minute();
+    ird.mMon = dt->date().month();
+    ird.mSec = dt->time().second();
+    ird.mMSec = dt->time().msec();
+    ird.mYear = dt->date().year();
+    ird.mnTime = 0;
+
+    int iPos = 1+4*sizeof(int);
+    memcpy(strdata+iPos,&ird,sizeof(iv::RecordHead));iPos = iPos+sizeof(iv::RecordHead);
+    if(nNameSize >0)
+    {
+        memcpy(strdata+iPos,strmemname.data(),nNameSize);iPos = iPos+nNameSize;
+    }
+    memcpy(strdata+iPos,str,nsize);
+
+    ba.append(strdata,1+nTotalSize);
+
+    delete strdata;
+}
+
+int groupmsgbuf::VehInfoToIVD(iv::group::vehicleinfo &xvinfo,qint64 msgtime, QByteArray &ba)
+{
+    int nHave = 0;
+
+    if(xvinfo.has_mbrainstate())
+    {
+        int nsize = xvinfo.mbrainstate().ByteSize();
+        std::shared_ptr<char> pstr_data = std::shared_ptr<char>(new char[nsize]);
+        if(xvinfo.mbrainstate().SerializeToArray(pstr_data.get(),nsize))
+        {
+            nHave++;
+            WriteIVDData(pstr_data.get(),nsize,"brainstate",msgtime,ba);
+        }
+        else
+        {
+            std::cout<<"groupmsgbuf::VehInfoToIVD serialize brainstate fail."<<std::endl;
+        }
+    }
+
+    if(xvinfo.has_mcarstate())
+    {
+        int nsize = xvinfo.mcarstate().ByteSize();
+        std::shared_ptr<char> pstr_data = std::shared_ptr<char>(new char[nsize]);
+        if(xvinfo.mcarstate().SerializeToArray(pstr_data.get(),nsize))
+        {
+            nHave++;
+            WriteIVDData(pstr_data.get(),nsize,"carstate",msgtime,ba);
+        }
+        else
+        {
+            std::cout<<"groupmsgbuf::VehInfoToIVD serialize carstate fail."<<std::endl;
+        }
+    }
+
+    if(xvinfo.has_mgpsimu())
+    {
+        int nsize = xvinfo.mgpsimu().ByteSize();
+        std::shared_ptr<char> pstr_data = std::shared_ptr<char>(new char[nsize]);
+        if(xvinfo.mgpsimu().SerializeToArray(pstr_data.get(),nsize))
+        {
+            nHave++;
+            WriteIVDData(pstr_data.get(),nsize,"hcp2_gpsimu",msgtime,ba);
+        }
+        else
+        {
+            std::cout<<"groupmsgbuf::VehInfoToIVD serialize brainstate fail."<<std::endl;
+        }
+    }
+
+
+    if(xvinfo.has_mdecition())
+    {
+        int nsize = xvinfo.mdecition().ByteSize();
+        std::shared_ptr<char> pstr_data = std::shared_ptr<char>(new char[nsize]);
+        if(xvinfo.mdecition().SerializeToArray(pstr_data.get(),nsize))
+        {
+            nHave++;
+            WriteIVDData(pstr_data.get(),nsize,"deciton",msgtime,ba);
+        }
+        else
+        {
+            std::cout<<"groupmsgbuf::VehInfoToIVD serialize brainstate fail."<<std::endl;
+        }
+    }
+
+    return nHave;
+}
+
 int groupmsgbuf::ProcGroupMsg(const iv::group::groupRequest * preq,iv::group::groupReply* reply)
 {
     iv::group::vehicleinfo xvinfo;
+
     if(!xvinfo.ParseFromArray(preq->xdata().data(),preq->xdata().size()))
     {
         std::cout<<"Parse Vehicle info fail"<<std::endl;
@@ -30,9 +150,12 @@ int groupmsgbuf::ProcGroupMsg(const iv::group::groupRequest * preq,iv::group::gr
     if(gbSaveToDB)
     {
         QByteArray ba;
-        ba.append(preq->xdata().data(),preq->xdata().size());
-        mpgroupdb->addmsg(QDateTime::currentMSecsSinceEpoch(),preq->strvehid(),preq->msgtime(),preq->ngroup(),
+        if(VehInfoToIVD(xvinfo,preq->msgtime(), ba)>0)
+        {
+ //           ba.append(preq->xdata().data(),preq->xdata().size());
+            mpgroupdb->addmsg(QDateTime::currentMSecsSinceEpoch(),preq->strvehid(),preq->msgtime(),preq->ngroup(),
                       preq->intragroupid(),ba);
+        }
     }
 
 

+ 23 - 0
src/driver/driver_group_grpc_server/groupmsgbuf.h

@@ -11,6 +11,21 @@
 
 #include "groupdb.h"
 
+namespace iv {
+    struct RecordHead
+    {
+        int mnDataIndex;
+        unsigned short mYear;
+        unsigned char mMon;
+        unsigned char mDay;
+        unsigned char mHour;
+        unsigned char mMin;
+        unsigned char mSec;
+        unsigned short mMSec;
+        int mnTime;
+    };
+}
+
 namespace iv {
 struct groupmsgunit
 {
@@ -25,6 +40,7 @@ class groupmsgbuf
 {
 public:
     groupmsgbuf();
+    ~groupmsgbuf();
 
 public:
     int ProcGroupMsg(const iv::group::groupRequest * preq,iv::group::groupReply* reply);
@@ -43,6 +59,13 @@ private:
 
 private:
     qint64 mnDataValidTime = 3000; //3 sceonds
+
+private:
+    int VehInfoToIVD(iv::group::vehicleinfo & xvinfo,qint64 msgtime,QByteArray & ba);
+
+    void WriteIVDData(char * str,int nsize,std::string strmemname,qint64 msgtime,QByteArray & ba);
+
+    int mnIndex = 0;
 };
 
 #endif // GROUPMSGBUF_H

+ 32 - 4
src/driver/driver_group_grpc_server/main.cpp

@@ -20,6 +20,8 @@
 
 #include "xmlparam.h"
 
+#include <signal.h>
+
 using grpc::Server;
 using grpc::ServerBuilder;
 using grpc::ServerContext;
@@ -37,6 +39,9 @@ std::string gstrdbpath;
 static std::thread * gptheadgroup;
 static std::thread * gptheaddb;
 
+static std::unique_ptr<Server> gserver_grpc;
+static std::unique_ptr<Server> gserver_db;
+
 // Logic and data behind the server's behavior.
 class GroupServiceImpl final : public iv::group::groupservice::Service{
   Status grpcgroup(ServerContext* context, const iv::group::groupRequest* request,
@@ -109,12 +114,14 @@ void RunServer() {
   // clients. In this case it corresponds to an *synchronous* service.
   builder.RegisterService(&service);
   // Finally assemble the server.
-  std::unique_ptr<Server> server(builder.BuildAndStart());
+ // std::unique_ptr<Server> server(builder.BuildAndStart());
+  gserver_grpc = builder.BuildAndStart();
   std::cout << "Server listening on " << server_address << std::endl;
 
   // Wait for the server to shutdown. Note that some other thread must be
   // responsible for shutting down the server for this call to ever return.
-  server->Wait();
+//  server->Wait();
+  gserver_grpc->Wait();
 }
 
 
@@ -144,12 +151,17 @@ void RunServerDB() {
   // clients. In this case it corresponds to an *synchronous* service.
   builder.RegisterService(&service);
   // Finally assemble the server.
-  std::unique_ptr<Server> server(builder.BuildAndStart());
+  gserver_db = builder.BuildAndStart();
+//  std::unique_ptr<Server> server(builder.BuildAndStart());
   std::cout << "Server listening on " << server_address << std::endl;
 
   // Wait for the server to shutdown. Note that some other thread must be
   // responsible for shutting down the server for this call to ever return.
-  server->Wait();
+//  server->Wait();
+  gserver_db->Wait();
+
+//  server->Shutdown();
+
 }
 
 
@@ -164,10 +176,24 @@ void threaddb()
 }
 
 
+void signal_handler(int sig)
+{
+    if(sig == SIGINT)
+    {
+        gserver_grpc->Shutdown();
+        gserver_db->Shutdown();
+        qDebug("shut down grpc.");
+        delete gpmsgbuf;
+        exit(0);
+    }
+}
+
 int main(int argc, char *argv[])
 {
     QCoreApplication a(argc, argv);
 
+
+
 //    std::shared_ptr<::google::protobuf::Message> msg_ptr = NULL;
 //    msg_ptr =std::shared_ptr<::google::protobuf::Message>(new iv::group::vehicleinfo);
 
@@ -197,5 +223,7 @@ int main(int argc, char *argv[])
     std::this_thread::sleep_for(std::chrono::milliseconds(100));
     gptheaddb = new std::thread(threaddb);
 
+    signal(SIGINT,signal_handler);
+
     return a.exec();
 }