Sfoglia il codice sorgente

complete apollodriver_nvcan.

yuchuli 1 mese fa
parent
commit
f20b305a54

+ 5 - 0
src/apollo/apollolib/nvcan/nvcan.cpp

@@ -383,3 +383,8 @@ int nvcan::SetMessage(const int nch, basecan_msg *pMsg)
     return 0;
 }
 
+bool nvcan::IsOpen()
+{
+    return mbCANOpen;
+}
+

+ 2 - 0
src/apollo/apollolib/nvcan/nvcan.h

@@ -19,6 +19,8 @@ public:
     int GetMessage(const int nch,basecan_msg * pMsg,const int nCap);
     int SetMessage(const int nch,basecan_msg * pMsg);
 
+    bool IsOpen();
+
 //private slots:
 //    void onMsg(bool bCAN,int nR,const char * strres);
 

+ 5 - 0
src/apollo/code/apollodriver_nvcan/Readme.md

@@ -0,0 +1,5 @@
+copy libnvcan.so from nvcan folder.
+
+/apollo/.cache/bazel/679551712d2357b63e6e0ce858ebf90e/execroot/__main__/bazel-out/aarch64-opt-exec-2B5CBBC6/bin/external/com_google_protobuf/protoc -I=./ --cpp_out=./  *.proto
+qmake apollodriver_nvcan.pro  
+make

+ 5 - 2
src/apollo/code/apollodriver_nvcan/apollodriver_nvcan.pro

@@ -1,7 +1,8 @@
+#/apollo/.cache/bazel/679551712d2357b63e6e0ce858ebf90e/execroot/__main__/bazel-out/aarch64-opt-exec-2B5CBBC6/bin/external/com_google_protobuf/protoc -I=./ --cpp_out=./  *.proto
 #qmake apollodriver_nvcan.pro  
 #make
 
-QT -= gui
+QT -= gui core
 
 CONFIG += c++11 console
 CONFIG -= app_bundle
@@ -18,7 +19,9 @@ DEFINES += QT_DEPRECATED_WARNINGS
 #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
 
 SOURCES += \
-        main.cpp
+        main.cpp \
+        canraw.pb.cc  \
+        canmsg.pb.cc
 
 # Default rules for deployment.
 qnx: target.path = /tmp/$${TARGET}/bin

+ 14 - 0
src/apollo/code/apollodriver_nvcan/canmsg.proto

@@ -0,0 +1,14 @@
+
+syntax = "proto2";
+
+package iv.can;
+
+import "canraw.proto";
+
+message canmsg
+{
+ required uint32 index = 1;
+ required uint32 channel = 2;
+ repeated canraw rawmsg = 3;
+ optional int64 mstime = 4;
+};

+ 13 - 0
src/apollo/code/apollodriver_nvcan/canraw.proto

@@ -0,0 +1,13 @@
+syntax = "proto2";
+
+package iv.can;
+
+message canraw
+{
+ required uint32 id = 1;
+ required bool bExt = 2;
+ required bool bRemote = 3;
+ required uint32 len = 4;
+ required bytes data = 5;
+ optional int64 rectime = 6;  //unit s 20251028
+};

+ 126 - 6
src/apollo/code/apollodriver_nvcan/main.cpp

@@ -1,4 +1,4 @@
-#include <QCoreApplication>
+//#include <QCoreApplication>
 
 #include <thread>
 #include <mutex>
@@ -8,10 +8,13 @@
 #include "cyber/time/rate.h"
 #include "cyber/time/time.h"
 #include "nvcan.h"
+#include <canmsg.pb.h>
 
 bool gbthreadrun = true;
 bool gbcanopen = false;
 
+int mindex[2] = {0,0};
+
 nvcan * gpccan;
 
 std::vector<basecan_msg> gsendmsgvector1;
@@ -23,6 +26,52 @@ std::mutex mMutexcan2;
 
 std::thread * gpthreadsendrecv;
 
+std::shared_ptr<apollo::cyber::Writer<iv::can::canmsg>>
+      can0recv_writer_;
+std::shared_ptr<apollo::cyber::Writer<iv::can::canmsg>>
+      can1recv_writer_;
+std::shared_ptr<apollo::cyber::Reader<iv::can::canmsg>>
+      can0send_reader_;
+std::shared_ptr<apollo::cyber::Reader<iv::can::canmsg>>
+      can1send_reader_;
+
+void sharecanmsg(std::shared_ptr<apollo::cyber::Writer<iv::can::canmsg>> canrecvwriter, basecan_msg * pxmsg,int ncount,int nch)
+{
+    iv::can::canmsg xmsg;
+
+    int i;
+    for(i=0;i<ncount;i++)
+    {
+        iv::can::canraw * praw = xmsg.add_rawmsg();
+        praw->set_id(pxmsg[i].id);
+        if(pxmsg[i].nLen>0) praw->set_data(pxmsg[i].data,pxmsg[i].nLen);
+        praw->set_bext(pxmsg[i].isExtern);
+        praw->set_bremote(pxmsg[i].isRemote);
+        if(pxmsg[i].frecvtime<0.001){
+            int64_t timenow = std::chrono::system_clock::now().time_since_epoch().count();
+            int64_t timesec = timenow/1e9;
+            int64_t timenano = timenow - timesec * 1e9;
+            double frecvtime = timenano;
+            frecvtime = frecvtime*1e-9;
+            frecvtime = frecvtime + timesec;
+            praw->set_rectime(frecvtime);
+ //       praw->set_rectime(QDateTime::currentMSecsSinceEpoch());
+        }
+        else
+        {
+            praw->set_rectime(pxmsg[i].frecvtime);
+        }
+        praw->set_len(pxmsg[i].nLen);
+
+    }
+    xmsg.set_channel(nch);
+
+    xmsg.set_index(mindex[nch]);
+    mindex[nch]++;
+
+    canrecvwriter->Write(xmsg);
+}
+
 void threadsendrecv()
 {
     int i;
@@ -36,12 +85,12 @@ void threadsendrecv()
             int nRec1,nRec2,nSend1,nSend2;
             if((nRec1 =gpccan->GetMessage(0,xmsg,2500))>0)
             {
-//                sharecanmsg(mparecv0,xmsg,nRec1,0);
+                sharecanmsg(can0recv_writer_,xmsg,nRec1,0);
             }
 
             if((nRec2 =gpccan->GetMessage(1,xmsg,2500))>0)
             {
-//                sharecanmsg(mparecv1,xmsg,nRec2,1);
+                sharecanmsg(can1recv_writer_,xmsg,nRec2,1);
             }
 
 
@@ -69,23 +118,94 @@ void threadsendrecv()
         else
         {
           std::this_thread::sleep_for(std::chrono::milliseconds(1));
+            gbcanopen = gpccan->IsOpen();
         }
 //        mpcan->mfault->SetFaultState(0, 0, "ok");
     }
 
     std::cout<<"  threadsendrecv exit."<<std::endl;
-    qDebug("thread canctrl complete."); 
 }
 
+void RequestSendMsg(const std::shared_ptr<iv::can::canmsg> &xmsg,int nch)
+{
+    if((nch <0)||(nch > 1))
+    {
+        return;
+    }
+
+    std::vector<basecan_msg> * psendmsgvector;
+    std::mutex * pMutex;
+    if(nch == 0)
+    {
+        pMutex = &mMutexcan1;
+        psendmsgvector = &gsendmsgvector1;
+    }
+    if(nch == 1)
+    {
+        pMutex = &mMutexcan2;
+        psendmsgvector = &gsendmsgvector2;
+    }
+
+    if(psendmsgvector->size() > SENDMSGBUFSIZE)
+    {
+        std::cout<<" sendmsg buf full"<<std::endl;
+        return;
+    }
 
 
+    pMutex->lock();
+    if(psendmsgvector->size() > 1000)psendmsgvector->clear();
+    int i;
+    for(i=0;i<xmsg->rawmsg_size();i++)
+    {
+        basecan_msg sendmsg;
+        iv::can::canraw x;
+        x.CopyFrom(xmsg->rawmsg(i));
+        sendmsg.id = x.id();
+        sendmsg.isExtern = x.bext();
+        sendmsg.isRemote = x.bremote();
+        int nlen = x.len();
+
+        if((nlen < 0) || (nlen > 64))
+        {
+            nlen = 0;
+            std::cout<<" sendmsg nlen err."<<std::endl;
+            continue;
+ //           mpcan->mfault->SetFaultState(1, 0, "sendmsg nlen err");
+        }
+        sendmsg.nLen = nlen;
+        if(sendmsg.nLen > 0)
+        {
+            memcpy(sendmsg.data,x.data().data(),sendmsg.nLen);
+        }
+
+        psendmsgvector->push_back(sendmsg);
+    }
+    pMutex->unlock();
+}
+
 int main(int argc, char *argv[])
 {
-    QCoreApplication a(argc, argv);
+//    QCoreApplication a(argc, argv);
 
     apollo::cyber::Init("apllodriver_nvcan");
     std::unique_ptr<apollo::cyber::Node> pilot_node  = apollo::cyber::CreateNode("apllodriver_nvcan");
-
+    can0recv_writer_ = pilot_node->CreateWriter<iv::can::canmsg>("/adc/canrecv0");
+    can1recv_writer_ = pilot_node->CreateWriter<iv::can::canmsg>("/adc/canrecv1");
+
+    can0send_reader_ = pilot_node->CreateReader<iv::can::canmsg>(
+        "/adc/cansend0",
+      [](const std::shared_ptr<iv::can::canmsg> &xmsg) {
+            RequestSendMsg(xmsg,0);
+      });
+
+    can1send_reader_ = pilot_node->CreateReader<iv::can::canmsg>(
+        "/adc/cansend1",
+      [](const std::shared_ptr<iv::can::canmsg> &xmsg) {
+            RequestSendMsg(xmsg,1);
+      });
+
+    gpccan = new nvcan();
     gpthreadsendrecv = new std::thread(threadsendrecv);
 
     apollo::cyber::WaitForShutdown();

+ 2 - 0
src/apollo/code/apollodriver_nvcan/nvcan.h

@@ -19,6 +19,8 @@ public:
     int GetMessage(const int nch,basecan_msg * pMsg,const int nCap);
     int SetMessage(const int nch,basecan_msg * pMsg);
 
+    bool IsOpen();
+
 //private slots:
 //    void onMsg(bool bCAN,int nR,const char * strres);