Przeglądaj źródła

1.修改数据发送细节,经过测试可以正常使用

kappo96 2 miesięcy temu
rodzic
commit
d9f7bd6b87

+ 34 - 0
.gitignore

@@ -4095,3 +4095,37 @@ build/Makefile
 build/.cmake/api/v1/reply/codemodel-v2-ff68604e9c8c7bb92954.json
 build/.cmake/api/v1/reply/index-2025-02-26T08-13-46-0073.json
 build/.cmake/api/v1/reply/target-UVC_Demo-Debug-60a9e96146f2940d6405.json
+build/CMakeFiles/3.30.0-rc3/CMakeCCompiler.cmake
+build/CMakeFiles/3.30.0-rc3/CMakeCXXCompiler.cmake
+build/CMakeFiles/3.30.0-rc3/CMakeRCCompiler.cmake
+build/CMakeFiles/3.30.0-rc3/CMakeSystem.cmake
+build/CMakeFiles/3.30.0-rc3/VCTargetsPath.txt
+build/CMakeFiles/3.30.0-rc3/VCTargetsPath.vcxproj
+build/CMakeFiles/3.30.0-rc3/CompilerIdC/CMakeCCompilerId.c
+build/CMakeFiles/3.30.0-rc3/CompilerIdC/CompilerIdC.vcxproj
+build/CMakeFiles/3.30.0-rc3/CompilerIdC/Debug/CompilerIdC.exe.recipe
+build/CMakeFiles/3.30.0-rc3/CompilerIdC/Debug/vcpkg.applocal.log
+build/CMakeFiles/3.30.0-rc3/CompilerIdC/Debug/CompilerIdC.tlog/CL.command.1.tlog
+build/CMakeFiles/3.30.0-rc3/CompilerIdC/Debug/CompilerIdC.tlog/Cl.items.tlog
+build/CMakeFiles/3.30.0-rc3/CompilerIdC/Debug/CompilerIdC.tlog/CL.read.1.tlog
+build/CMakeFiles/3.30.0-rc3/CompilerIdC/Debug/CompilerIdC.tlog/CL.write.1.tlog
+build/CMakeFiles/3.30.0-rc3/CompilerIdC/Debug/CompilerIdC.tlog/CompilerIdC.lastbuildstate
+build/CMakeFiles/3.30.0-rc3/CompilerIdC/Debug/CompilerIdC.tlog/link.command.1.tlog
+build/CMakeFiles/3.30.0-rc3/CompilerIdC/Debug/CompilerIdC.tlog/link.read.1.tlog
+build/CMakeFiles/3.30.0-rc3/CompilerIdC/Debug/CompilerIdC.tlog/link.secondary.1.tlog
+build/CMakeFiles/3.30.0-rc3/CompilerIdC/Debug/CompilerIdC.tlog/link.write.1.tlog
+build/CMakeFiles/3.30.0-rc3/CompilerIdCXX/CMakeCXXCompilerId.cpp
+build/CMakeFiles/3.30.0-rc3/CompilerIdCXX/CompilerIdCXX.vcxproj
+build/CMakeFiles/3.30.0-rc3/CompilerIdCXX/Debug/CompilerIdCXX.exe.recipe
+build/CMakeFiles/3.30.0-rc3/CompilerIdCXX/Debug/vcpkg.applocal.log
+build/CMakeFiles/3.30.0-rc3/CompilerIdCXX/Debug/CompilerIdCXX.tlog/CL.command.1.tlog
+build/CMakeFiles/3.30.0-rc3/CompilerIdCXX/Debug/CompilerIdCXX.tlog/Cl.items.tlog
+build/CMakeFiles/3.30.0-rc3/CompilerIdCXX/Debug/CompilerIdCXX.tlog/CL.read.1.tlog
+build/CMakeFiles/3.30.0-rc3/CompilerIdCXX/Debug/CompilerIdCXX.tlog/CL.write.1.tlog
+build/CMakeFiles/3.30.0-rc3/CompilerIdCXX/Debug/CompilerIdCXX.tlog/CompilerIdCXX.lastbuildstate
+build/CMakeFiles/3.30.0-rc3/CompilerIdCXX/Debug/CompilerIdCXX.tlog/link.command.1.tlog
+build/CMakeFiles/3.30.0-rc3/CompilerIdCXX/Debug/CompilerIdCXX.tlog/link.read.1.tlog
+build/CMakeFiles/3.30.0-rc3/CompilerIdCXX/Debug/CompilerIdCXX.tlog/link.secondary.1.tlog
+build/CMakeFiles/3.30.0-rc3/CompilerIdCXX/Debug/CompilerIdCXX.tlog/link.write.1.tlog
+build/CMakeFiles/3.30.0-rc3/VCTargetsPath/x64/Debug/VCTargetsPath.recipe
+build/CMakeFiles/3.30.0-rc3/VCTargetsPath/x64/Debug/VCTargetsPath.tlog/VCTargetsPath.lastbuildstate

+ 2 - 1
.vscode/c_cpp_properties.json

@@ -15,7 +15,8 @@
             "defines": [
                 "_DEBUG",
                 "UNICODE",
-                "_UNICODE"
+                "_UNICODE",
+                "_LINUX_"
             ],
             "windowsSdkVersion": "10.0.22621.0",
             "compilerPath": "cl.exe",

+ 44 - 2
CMakeLists.txt

@@ -9,7 +9,8 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
 
 set(TARGET "UVC_Demo")
 
-set(CMAKE_GENERATOR "Ninja")
+# use make file
+set(CMAKE_GENERATOR "Makefiles")
 
 set(CMAKE_CXX_STANDARD_REQUIRED ON)
 set(CMAKE_BUILD_TYPE "Debug")
@@ -25,6 +26,7 @@ include_directories(${CMAKE_CURRENT_SOURCE_DIR}/PublicLibrary/libuvc/include)
 include_directories(${CMAKE_SOURCE_DIR}/3rdparty/rga/RK3588/include)
 include_directories(${CMAKE_SOURCE_DIR}/3rdparty/mpp/include)
 include_directories(${CMAKE_SOURCE_DIR}/runtime/RK3588/Linux/librknn_api/include)
+include_directories(${CMAKE_SOURCE_DIR}/3rdparty/json/include)
 include_directories(${GRPC_DIR}/include)
 
 include(common.cmake)
@@ -67,4 +69,44 @@ target_link_libraries(${TARGET} libgpiod.so)
 target_link_libraries(${TARGET} uvc)
 target_link_libraries(${TARGET} librockchip_mpp.so)
 target_link_libraries(${TARGET} absl::check absl::flags absl::flags_parse absl::log)
-target_link_libraries(${TARGET} ${_REFLECTION} ${_GRPC_GRPCPP} ${_PROTOBUF_LIBPROTOBUF})
+target_link_libraries(${TARGET} ${_REFLECTION} ${_GRPC_GRPCPP} ${_PROTOBUF_LIBPROTOBUF})
+
+
+
+set(INSTALL_BIN /USBDevice)
+set(INSTALL_LIB /home/cat/UsbDevice/lib)
+set(INSTALL_INC /home/cat/UsbDevice/include)
+set(SYSTEM_LIB /usr/lib/aarch64-linux-gnu/)
+
+# Install rules
+install(TARGETS ${TARGET}
+        RUNTIME DESTINATION ${INSTALL_BIN}
+        LIBRARY DESTINATION ${INSTALL_LIB}
+        ARCHIVE DESTINATION ${INSTALL_LIB}
+)
+
+install(CODE "execute_process(COMMAND mkdir -p ${INSTALL_BIN})")
+
+# Install additional libraries
+install(FILES
+    ${CMAKE_SOURCE_DIR}/runtime/RK3588/Linux/librknn_api/aarch64/librknnrt.so
+    ${CMAKE_SOURCE_DIR}/3rdparty/mpp/Linux/aarch64/librockchip_mpp.so.0
+    ${CMAKE_SOURCE_DIR}/libuvc-0.0.7/libuvc-0.0.7/build/libuvc.so.0.0.7
+    ${SYSTEM_LIB}/libusb-1.0.so.0.3.0
+    ${SYSTEM_LIB}/libgpiod.so.2.2.2
+    DESTINATION ${INSTALL_BIN}
+)
+
+# Create symbolic links for .so files in the bin directory
+install(CODE "execute_process(COMMAND ${CMAKE_COMMAND} -E create_symlink ${INSTALL_LIB}/librknnrt.so ${INSTALL_BIN}/librknnrt.so)")
+install(CODE "execute_process(COMMAND ${CMAKE_COMMAND} -E create_symlink ${INSTALL_LIB}/librockchip_mpp.so.0 ${INSTALL_BIN}/librockchip_mpp.so.0)")
+install(CODE "execute_process(COMMAND ${CMAKE_COMMAND} -E create_symlink ${INSTALL_BIN}/libusb-1.0.so.0.3.0 ${INSTALL_BIN}/libusb-1.0.so)")
+install(CODE "execute_process(COMMAND ${CMAKE_COMMAND} -E create_symlink ${INSTALL_BIN}/libgpiod.so.2.2.2 ${INSTALL_BIN}/libgpiod.so)")
+install(CODE "execute_process(COMMAND ${CMAKE_COMMAND} -E create_symlink ${INSTALL_LIB}/libuvc.so.0.0.7 ${INSTALL_BIN}/libuvc.so.0)")
+
+set(LD_LIBRARY_PATH_COMMAND "export LD_LIBRARY_PATH=${INSTALL_BIN}:$ENV{LD_LIBRARY_PATH}")
+
+# Set LD_LIBRARY_PATH to include the bin directory
+install(CODE "execute_process(COMMAND LD_LIBRARY_PATH=${INSTALL_BIN}:$ENV{LD_LIBRARY_PATH} )")
+install(CODE "execute_process(COMMAND echo ${LD_LIBRARY_PATH_COMMAND} >> ~/.bashrc )")
+install(CODE "execute_process(COMMAND source ~/.bashrc )")

+ 6 - 9
GrpcTransfer/GrpcTransfer.cpp

@@ -1,11 +1,8 @@
 #include "GrpcTransfer.h"
 
-void MessageServiceClient::R360SendMessage(DataPackagePtr pkg)
+DataList* MessageServiceClient::R360SendMessage(DataPackagePtr pkg)
 {
-    ClientContext context;
-    Empty response;
-    std::unique_ptr<grpc::ClientWriter<DataList>> writer(
-        stub_->R360SendMessage(&context, &response));
+    
 
     DataList dataList;
     EmbeddedData *embeddedData = dataList.add_data();
@@ -27,8 +24,8 @@ void MessageServiceClient::R360SendMessage(DataPackagePtr pkg)
         alarm->set_type(R360::AlarmType(pkg->Result.results[i].cls_id)); // 使用枚举值
     }
 
-    if (!writer->Write(dataList))
-    {
-        std::cerr << "写入消息失败" << std::endl;
-    }
+    m_DataList = dataList;
+
+    return &m_DataList;
+
 }

+ 44 - 43
GrpcTransfer/GrpcTransfer.h

@@ -14,56 +14,57 @@ using R360::MessageService;
 class MessageServiceClient
 {
 public:
-    explicit MessageServiceClient(std::shared_ptr<Channel> channel)
-        : stub_(MessageService::NewStub(channel)) {}
+    explicit MessageServiceClient(){}
 
-    void R360SendMessage(char *pic)
-    {
-        ClientContext context;
-        Empty response;
-        std::unique_ptr<grpc::ClientWriter<DataList>> writer(
-            stub_->R360SendMessage(&context, &response));
+    // void R360SendMessage(char *pic)
+    // {
+    //     ClientContext context;
+    //     Empty response;
+    //     std::unique_ptr<grpc::ClientWriter<DataList>> writer(
+    //         stub_->R360SendMessage(&context, &response));
 
-        // 构造第一个DataList消息
-        DataList data_list;
-        EmbeddedData *embeddedData = data_list.add_data();
-        embeddedData->set_usb_camera_index(1);
-        // 填充图片数据(示例中使用字符串模拟)
-        embeddedData->set_image(pic);
-        embeddedData->set_degree(90);
+    //     // 构造第一个DataList消息
+    //     DataList data_list;
+    //     EmbeddedData *embeddedData = data_list.add_data();
+    //     embeddedData->set_usb_camera_index(1);
+    //     // 填充图片数据(示例中使用字符串模拟)
+    //     embeddedData->set_image(pic);
+    //     embeddedData->set_degree(90);
 
-        // 添加报警信息
-        Alarm *alarm = embeddedData->add_alarm();
-        alarm->set_x(100);
-        alarm->set_y(150);
-        alarm->set_w(50);
-        alarm->set_h(50);
-        alarm->set_score(0.95);
-        alarm->set_type(R360::UAV); // 使用枚举值
+    //     // 添加报警信息
+    //     Alarm *alarm = embeddedData->add_alarm();
+    //     alarm->set_x(100);
+    //     alarm->set_y(150);
+    //     alarm->set_w(50);
+    //     alarm->set_h(50);
+    //     alarm->set_score(0.95);
+    //     alarm->set_type(R360::UAV); // 使用枚举值
 
-        if (!writer->Write(data_list))
-        {
-            std::cerr << "写入消息失败" << std::endl;
-        }
+    //     if (!writer->Write(data_list))
+    //     {
+    //         std::cerr << "写入消息失败" << std::endl;
+    //     }
 
-        // 如有需要,可发送更多消息...
-        // DataList another_data_list;
-        // writer->Write(another_data_list);
+    //     // 如有需要,可发送更多消息...
+    //     // DataList another_data_list;
+    //     // writer->Write(another_data_list);
 
-        /*writer->WritesDone();
-        Status status = writer->Finish();
-        if (status.ok())
-        {
-            std::cout << "R360SendMessage RPC 调用成功" << std::endl;
-        }
-        else
-        {
-            std::cout << "R360SendMessage RPC 调用失败: " << status.error_message() << std::endl;
-        }*/
-    }
+    //     /*writer->WritesDone();
+    //     Status status = writer->Finish();
+    //     if (status.ok())
+    //     {
+    //         std::cout << "R360SendMessage RPC 调用成功" << std::endl;
+    //     }
+    //     else
+    //     {
+    //         std::cout << "R360SendMessage RPC 调用失败: " << status.error_message() << std::endl;
+    //     }*/
+    // }
 
-    void R360SendMessage(DataPackage *pkg);
+    DataList* R360SendMessage(DataPackage *pkg);
 
 private:
-    std::unique_ptr<MessageService::Stub> stub_;
+
+    DataList m_DataList;
+
 };

+ 1 - 0
GrpcTransfer/TransferManager.h

@@ -5,4 +5,5 @@
 class TransferManager
 {
 public:
+    
 };

+ 1 - 1
ImageTest/ImageTest.cpp

@@ -1,6 +1,6 @@
 #include "ImageTest.h"
 
-void ImageTest::saveImageFromData(unsigned char *data, int width, int height, const std::string &filename)
+void UsbTest::saveImageFromData(unsigned char *data, int width, int height, const std::string &filename)
 {
     cv::Mat image(height, width, CV_8UC3, data);
     cv::Mat BGRImage;

+ 142 - 2
ImageTest/ImageTest.h

@@ -5,8 +5,12 @@
 #include <string>
 #include <sys/time.h>
 #include <chrono>
+#include <fstream>
+#include <atomic>
+#include <thread>
+#include <mutex>
 
-namespace ImageTest
+namespace UsbTest
 {
 
     void saveImageFromData(unsigned char *data, int width, int height, const std::string &filename = "./output.png");
@@ -44,10 +48,146 @@ namespace ImageTest
             return std::chrono::duration<double, std::micro>(end_time - start_time).count();
         }
 
-    private:
+    protected:
         std::chrono::high_resolution_clock::time_point start_time;
         std::chrono::high_resolution_clock::time_point end_time;
     };
 
+    class GlobalResolutionTimer: public HighResolutionTimer
+    {
+        public:
+           static GlobalResolutionTimer& getInstance()
+           {
+               static GlobalResolutionTimer instance;
+               return instance;
+           }
+
+           void start()
+           {
+               start_time = std::chrono::high_resolution_clock::now();   
+           }
+
+           std::string pressStopWatchString()
+           {
+                end_time = std::chrono::high_resolution_clock::now();   
+                return "Elapsed Time: " + std::to_string(elapsed_milliseconds()) + " ms";
+           }
+    };
+
+    class SimulateTrigger
+    {
+    public:
+        static SimulateTrigger& getInstance()
+        {
+            static SimulateTrigger instance;
+            return instance;
+        }
+
+        void startTrigger()
+        {
+            if (!triggering)
+            {
+                triggering = true;
+                triggerStatus.resize(3);
+                triggerStatus[0]=false;
+                triggerStatus[1]=false;  
+                triggerStatus[2]=false;  
+                triggerThread = std::thread(&SimulateTrigger::triggerLoop, this);
+            }
+        }
+
+        void stopTrigger()
+        {
+            if (triggering)
+            {
+                triggering = false;
+                if (triggerThread.joinable())
+                {
+                    triggerThread.join();
+                }
+            }
+        }
+
+        bool isTriggering(int index)
+        {
+            bool status = triggerStatus[index];
+            std::lock_guard<std::mutex> lock(triggerMutex);
+            triggerStatus[index] = false;
+            return status;
+        }
+
+    private:
+        SimulateTrigger() : triggering(false) {}
+        ~SimulateTrigger()
+        {
+            stopTrigger();
+        }
+
+        // Delete copy constructor and assignment operator to prevent copying
+        SimulateTrigger(const SimulateTrigger&) = delete;
+        SimulateTrigger& operator=(const SimulateTrigger&) = delete;
+
+        void triggerLoop()
+        {
+            while (triggering)
+            {
+                std::this_thread::sleep_for(std::chrono::milliseconds(188));
+                if (triggering)
+                {
+                    // Simulate the trigger signal
+                    std::lock_guard<std::mutex> lock(triggerMutex);
+                    triggerStatus[0]=true;
+                    triggerStatus[1]=true;
+                    triggerStatus[2]=true;
+                }
+            }
+        }
+
+        std::atomic<bool> triggering;
+        std::thread triggerThread;
+        std::mutex triggerMutex;
+        std::vector<bool> triggerStatus;
+
+    };
+
+    class TimeRecorder
+    {
+    public:
+        static TimeRecorder& getInstance()
+        {
+            static TimeRecorder instance;
+            return instance;
+        }
+
+        bool init()
+        {
+            log_file = std::fstream("TimeLog.txt", std::ios::app);
+        }
+
+        
+
+        void recordTime(std::string &message)
+        {
+            if (log_file.is_open())
+            {
+                log_file << message << std::endl;
+            }
+        }
+
+    private:
+        TimeRecorder() {}
+        ~TimeRecorder()
+        {
+            if (log_file.is_open())
+            {
+                log_file.close();
+            }
+        }
+
+        std::fstream log_file;
+
+    };
+
+
 }
 #endif // IMAGE_TEST_H

+ 4 - 1
MppDecoder/MppManager.cpp

@@ -37,7 +37,7 @@ void MppManager::decodeThread()
     DataManager::getInstance().addDataPipe<DataPackagePtr>("resized" + std::to_string(index));
 
     // test
-    ImageTest::HighResolutionTimer timer;
+    UsbTest::HighResolutionTimer timer;
 
     while (m_threadSwitch)
     {
@@ -57,6 +57,9 @@ void MppManager::decodeThread()
 
             // ImageTest::saveImageFromData((unsigned char *)dataPackage->pResizeData, dataPackage->nResizeWidth, dataPackage->nResizeHeight);
 
+            std::string logMessage = "Decoded frame " + std::to_string(index) + " degree : " + std::to_string(dataPackage->dDegree) + " " + UsbTest::GlobalResolutionTimer::getInstance().pressStopWatchString();
+            //UsbTest::TimeRecorder::getInstance().recordTime(logMessage);
+
             // push data to the next pipe
             DataManager::getInstance().pushData("resized" + std::to_string(index), dataPackage);
         }

+ 16 - 4
UVCGrabber/UVCCallBack.cpp

@@ -11,6 +11,7 @@
 #include "../DataManager/DataPackage.h"
 #include <opencv4/opencv2/opencv.hpp>
 #include <opencv4/opencv2/imgcodecs.hpp>
+#include "../ImageTest/ImageTest.h"
 
 struct timeval pre_frame_time;
 
@@ -33,7 +34,7 @@ void cbSaveToLocal(uvc_frame_t *frame, void *ptr)
   // static MemoryPool<DataPackage> dataPool(BUFFER_SIZE, 3840, 2160);
   std::shared_ptr<DataPipe<DataPackage>> pipe;
 
-  // std::fstream readImage("./uav1.jpg", std::ios::binary | std::ios::in);
+  // std::fstream readImage("./auv.jpg", std::ios::binary | std::ios::in);
   // if (!readImage)
   // {
   //   std::cerr << "不能打开文件 example.bin" << std::endl;
@@ -41,6 +42,9 @@ void cbSaveToLocal(uvc_frame_t *frame, void *ptr)
   // readImage.seekg(0, std::ios::end);
   // std::streamsize fileSize = readImage.tellg();
   // readImage.seekg(0, std::ios::beg);
+  // unsigned char *buffer = new unsigned char[fileSize];
+  // readImage.read((char *)buffer, fileSize);
+  // readImage.close();
 
   switch (frame->frame_format)
   {
@@ -52,12 +56,17 @@ void cbSaveToLocal(uvc_frame_t *frame, void *ptr)
     break;
   case UVC_COLOR_FORMAT_MJPEG:
 
-    if (!g_gpioExplorer->getFailingStatus(index))
+    // if (!g_gpioExplorer->getFailingStatus(index))
+    // {
+    //   // std::cout << "Pass this wave" << std::endl;
+    //   break;
+    // }
+    // g_gpioExplorer->resetFailingStatus(index);
+
+    if(!UsbTest::SimulateTrigger::getInstance().isTriggering(index))
     {
-      // std::cout << "Pass this wave" << std::endl;
       break;
     }
-    g_gpioExplorer->resetFailingStatus(index);
 
     if (jpeg_count[index] / 23 >= 1)
       jpeg_group[index]++;
@@ -76,6 +85,9 @@ void cbSaveToLocal(uvc_frame_t *frame, void *ptr)
     dataPackage->nCameraID = index;
     dataPackage->dDegree = jpeg_count[index]++;
 
+    //std::string logMessage = "UVC Frame Received: Device " + std::to_string(index) + " UVC Received: Degree: " + std::to_string(jpeg_count[index] - 1) + UsbTest::GlobalResolutionTimer::getInstance().pressStopWatchString();
+    //UsbTest::TimeRecorder::getInstance().recordTime(logMessage);
+
     DataManager::getInstance().pushData<DataPackage>("uvc" + std::to_string(index), dataPackage);
 
     // fp = fopen("./Ouput4k.jpg", "w");

+ 3 - 0
config.json

@@ -0,0 +1,3 @@
+{
+    "grpc_server_address": "192.168.210.20:50051"
+}

+ 28 - 0
main.cpp

@@ -4,22 +4,47 @@
 #include "AIManager/RKNNManager.h"
 #include "DataManager/DataManager.h"
 #include "DataManager/DataPackage.h"
+#include "ImageTest/ImageTest.h"
 #include <stdexcept>
 #include <iostream>
+#include <json.hpp>
 
 #define DEVICE_NUM 3
 
+
+
 int main(int argc, char **argv)
 {
     // InfraredManager Infrar;
     // Infrar.init("rtsp://admin:admin123@192.168.211.108");
     // Infrar.openStream();
 
+    std::system("echo 64 > /sys/module/usbcore/parameters/usbfs_memory_mb");
+    std::system("rm ./TimeLog.txt");
+
+    // use json to read config file
+    nlohmann::json config;
+    try {
+        std::ifstream config_file("config.json");
+        config_file >> config;
+        config_file.close();
+    } catch (std::exception &e) {
+        std::cerr << "Error reading config file: " << e.what() << std::endl;
+        return 1;
+    }
+
+    UsbTest::TimeRecorder::getInstance().init();
+    UsbTest::GlobalResolutionTimer::getInstance().start();
+    UsbTest::SimulateTrigger::getInstance().startTrigger();
+
+
     // Initialize the UVC manager
     UVCManager::getInstance().init();
 
     MppManager::getInstance().addMppDecode();
 
+    RKNNManager::getInstance().setGrpcServerAddress(config["grpc_server_address"]);
+
     RKNNManager::getInstance().addRknnTask("../ppyoloe.rknn");
 
     // Start all streaming
@@ -28,8 +53,11 @@ int main(int argc, char **argv)
     // Wait for the user to press a key
     std::cin.get();
 
+    RKNNManager::getInstance().StopThread();
+
     // Stop all streaming
     UVCManager::getInstance().stopAllStreaming();
 
+
     return 0;
 }