Browse Source

add can bridge module

HAPO-9# 3 years ago
parent
commit
f4c98725e1

+ 42 - 0
src/driver/driver_agx_bridge/driver_agx_bridge.pro

@@ -0,0 +1,42 @@
+QT -= gui
+
+QT += dbus
+
+CONFIG += c++11 console
+CONFIG -= app_bundle
+
+# The following define makes your compiler emit warnings if you use
+# any feature of Qt which as been marked deprecated (the exact warnings
+# depend on your compiler). Please consult the documentation of the
+# deprecated API in order to know how to port your code away from it.
+DEFINES += QT_DEPRECATED_WARNINGS
+
+# You can also make your code fail to compile if you use deprecated APIs.
+# In order to do so, uncomment the following line.
+# You can also select to disable deprecated APIs only up to a certain version of Qt.
+#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
+
+
+HEADERS += \
+    ../../include/msgtype/bridge.pb.h \
+    ../../include/msgtype/canmsg.pb.h \
+    ../../include/msgtype/canraw.pb.h
+
+SOURCES += main.cpp \
+    ../../include/msgtype/bridge.pb.cc \
+    ../../include/msgtype/canmsg.pb.cc \
+    ../../include/msgtype/canraw.pb.cc
+
+!include(../../../include/common.pri ) {
+    error( "Couldn't find the common.pri file!" )
+}
+
+!include(../../../include/ivprotobuf.pri ) {
+    error( "Couldn't find the ivprotobuf.pri file!" )
+}
+
+!include(../../../include/ivboost.pri ) {
+    error( "Couldn't find the ivboost.pri file!" )
+}
+
+

+ 8 - 0
src/driver/driver_agx_bridge/driver_agx_bridge.xml

@@ -0,0 +1,8 @@
+<xml>	
+	<node name="driver_vbox_gaohong">
+		<param name="canrecv" value="canrecv1_017100004EF" />
+		<param name="cansend" value="cansend1_017100004EF" />
+		<param name="vbox" value="vbox" />
+		<param name="modulename" value="driver_vbox_gaohong" />
+	</node>
+</xml>

+ 214 - 0
src/driver/driver_agx_bridge/main.cpp

@@ -0,0 +1,214 @@
+#include <QCoreApplication>
+
+#include <iostream>
+#include <QDateTime>
+#include <math.h>
+
+#include <thread>
+#include <QElapsedTimer>
+#include "modulecomm.h"
+#include "xmlparam.h"
+#include "ivlog.h"
+#include "ivfault.h"
+#include "ivversion.h"
+
+iv::Ivlog * givlog;
+iv::Ivfault * givfault;
+
+#include "canmsg.pb.h"
+#include "bridge.pb.h"
+
+iv::bridge::reciver grecv;
+
+unsigned char dataToVision[8] = {0};
+unsigned char dataToArm[8] = {0};
+bool hasVisionCtrl = false;
+bool hasArmCtrl = false;
+int gnIndex = 0;
+void * gpa , * gpb;
+
+QElapsedTimer gTime;
+
+void ShareResult()
+{
+    char * str = new char[grecv.ByteSize()];
+    int nsize = grecv.ByteSize();
+    if(grecv.SerializeToArray(str,nsize))
+    {
+        iv::modulecomm::ModuleSendMsg(gpa,str,nsize);
+    }
+    delete[] str;
+    str = nullptr;
+}
+
+void ShareResultToCan()
+{
+    iv::can::canmsg xmsg;
+    iv::can::canraw xraw;
+
+    if(hasVisionCtrl){
+        xraw.set_id(0x7F);
+        xraw.set_data(dataToVision,8);
+        xraw.set_bext(false);
+        xraw.set_bremote(false);
+        xraw.set_len(8);
+        iv::can::canraw * pxraw = xmsg.add_rawmsg();
+        pxraw->CopyFrom(xraw);
+        xmsg.set_channel(0);
+        xmsg.set_index(gnIndex);
+        hasVisionCtrl = false;
+        dataToVision[0] = 0;
+        dataToVision[1] = 0;
+    }
+    if(hasArmCtrl){
+        xraw.set_id(0x7B);
+        xraw.set_data(dataToArm,8);
+        xraw.set_bext(false);
+        xraw.set_bremote(false);
+        xraw.set_len(8);
+        iv::can::canraw * pxraw1 = xmsg.add_rawmsg();
+        pxraw1->CopyFrom(xraw);
+        xmsg.set_channel(0);
+        xmsg.set_index(gnIndex);
+        hasArmCtrl = false;
+        dataToArm[0] = 0;
+    }
+    gnIndex++;
+    xmsg.set_mstime(QDateTime::currentMSecsSinceEpoch());
+    int ndatasize = xmsg.ByteSize();
+    char * strser = new char[ndatasize];
+    std::shared_ptr<char> pstrser;
+    pstrser.reset(strser);
+    if(xmsg.SerializeToArray(strser,ndatasize))
+    {
+        iv::modulecomm::ModuleSendMsg(gpb,strser,ndatasize);
+    }
+    else
+    {
+        std::cout<<"MainWindow::onTimer serialize error."<<std::endl;
+    }
+}
+void DecodeData(iv::can::canmsg xmsg)
+{
+    int type=0;
+    int color=0;
+    if(xmsg.rawmsg_size() < 1)return;
+
+    int i;
+    for(i=0;i<xmsg.rawmsg_size();i++)
+    {
+        iv::can::canraw canmsg = xmsg.rawmsg(i);
+
+        if(canmsg.id() == 0x7E)//精准识别=》自动驾驶系统
+        {
+            int  data[8];
+            unsigned char cdata[8];
+            memcpy(cdata,canmsg.data().data(),8);
+
+            for(int j=0;j<8;j++)
+                data[j] = cdata[j];
+            grecv.set_stop(data[0]);//停车
+            grecv.set_pause(data[1]);//控制权移交
+            ShareResult();
+        }
+        else if(canmsg.id() == 0x7A)//机械控制=》自动驾驶系统
+        {
+
+            int  data[8];
+            unsigned char cdata[8];
+            memcpy(cdata,canmsg.data().data(),8);
+
+            for(int j=0;j<8;j++)
+                data[j] = cdata[j];
+            grecv.set_st_rbshbox(data[0]);//垃圾箱回位状态1-》回位
+            ShareResult();
+        }
+    }
+
+}
+
+void Listencan0(const char * strdata,const unsigned int nSize,const unsigned int index,const QDateTime * dt,const char * strmemname)
+{
+    if(nSize<1)return;
+    iv::can::canmsg xmsg;
+
+    if(false == xmsg.ParseFromArray(strdata,nSize))
+    {
+        return;
+    }
+    DecodeData(xmsg);
+
+}
+
+void ListenBrain(const char * strdata,const unsigned int nSize,const unsigned int index,const QDateTime * dt,const char * strmemname)
+{
+    if(nSize<1)return;
+    iv::bridge::sender xmsg;
+    unsigned char cdata[8];
+
+    if(false == xmsg.ParseFromArray(strdata,nSize))
+    {
+        return;
+    }
+    if(xmsg.has_camerastart())
+    {
+        hasVisionCtrl = true;
+        dataToVision[0] = xmsg.camerastart();
+    }
+    if(xmsg.has_carctrl())
+    {
+        hasVisionCtrl = true;
+        dataToVision[1] = xmsg.carctrl();
+    }
+    if(xmsg.has_rbshboxctrl())
+    {
+        hasArmCtrl = true;
+        dataToArm[0] = xmsg.rbshboxctrl();
+    }
+    ShareResultToCan();
+}
+
+void threadstate()
+{
+    while(1)
+    {
+        std::this_thread::sleep_for(std::chrono::milliseconds(10));
+    }
+}
+int main(int argc, char *argv[])
+{
+    showversion("driver_vbox_gaohong");
+    QCoreApplication a(argc, argv);
+
+    gTime.start();
+
+    QString strpath = QCoreApplication::applicationDirPath();
+    if(argc < 2)
+        strpath = strpath + "/driver_agx_bridge.xml";
+    else
+        strpath = argv[1];
+    std::cout<<strpath.toStdString()<<std::endl;
+
+    iv::xmlparam::Xmlparam xp(strpath.toStdString());
+
+    std::string strmemcan = xp.GetParam("canrecv","canrecv1");
+    std::string strmemsend = xp.GetParam("cansend","cansend1");
+    std::string strmemBRec = xp.GetParam("bridgeRec","bridgeRec");
+    std::string strmemBSend = xp.GetParam("bridgeSend","bridgeSend");
+    std::string strmodulename = xp.GetParam("modulename","driver_agx_bridge");
+
+
+    givlog = new iv::Ivlog(strmodulename.data());
+    givfault = new iv::Ivfault(strmodulename.data());
+
+    givfault->SetFaultState(1,1,"初始化");
+    givlog->info("bridge","Initialized");
+
+    gpa = iv::modulecomm::RegisterSend(strmemBRec.data(),100000,3);//to brain
+    gpb = iv::modulecomm::RegisterSend(strmemsend.data(),100000,3);// to cansend1
+    iv::modulecomm::RegisterRecv(strmemcan.data(),Listencan0);// form canrecv1
+    iv::modulecomm::RegisterRecv(strmemBSend.data(),ListenBrain);//form brain
+
+    std::thread threadfault(threadstate);
+    return a.exec();
+}

+ 18 - 0
src/include/proto/bridge.proto

@@ -0,0 +1,18 @@
+syntax = "proto2";
+
+package iv.bridge;
+
+message reciver //从机械臂/识别系统接受数据
+{
+	optional uint32 stop		= 1;//停车:1 无意义:0
+	optional uint32 pause 		= 2;//车辆控制权 交出:1
+	optional uint32 st_rbshBox	= 3;//垃圾箱状态 移动中:0 回位:1
+};
+
+
+message sender //从brain接受数据
+{
+	optional uint32 cameraStart	= 1;//开启摄像头:1 无意义:0
+	optional uint32 carCtrl		= 2;//车辆控制权 交出:1
+	optional uint32 rbshBoxCtrl	= 3;//垃圾箱状态 无意义:0 倾倒:1
+};