Giter Site home page Giter Site logo

Comments (2)

Serafadam avatar Serafadam commented on August 11, 2024 1

Hi, since by default depthai_ros_driver is loaded as a Component(shared library), to use custom nodes you also need to "load" them from a shared library so that they retain their identity (without that, they are loaded as a BaseNode class and that slices off overriden virtual functions, which causes segfault when the driver tries to call them).
A quick test example to mitigate that:

  • In rgb_test.cpp, add following line:
extern "C" {
std::unique_ptr<depthai_ros_driver::dai_nodes::BaseNode> create(const std::string& daiNodeName,
                 rclcpp::Node* node,
                 std::shared_ptr<dai::Pipeline> pipeline,
                 dai::CameraBoardSocket socket,
                 depthai_ros_driver::dai_nodes::sensor_helpers::ImageSensor sensor,
                 bool publish) {
    return  std::make_unique<RGB_test>(daiNodeName, node, pipeline, socket, sensor, publish);
}
}

Then modify the test_pipeline.cpp to load the library via dlopen

#include "depthai_test_plugin/pipeline/test_base_types.hpp"
#include <dlfcn.h>

#include "depthai/device/Device.hpp"
#include "depthai/pipeline/Pipeline.hpp"
#include "depthai_ros_driver/dai_nodes/base_node.hpp"
#include "depthai_ros_driver/dai_nodes/nn/nn_helpers.hpp"
#include "depthai_ros_driver/dai_nodes/nn/nn_wrapper.hpp"
#include "depthai_ros_driver/dai_nodes/nn/spatial_nn_wrapper.hpp"
#include "depthai_ros_driver/dai_nodes/sensors/imu.hpp"
#include "depthai_ros_driver/dai_nodes/sensors/sensor_helpers.hpp"
#include "depthai_test_plugin/dai_nodes/sensors/rgb_test.hpp"
#include "depthai_ros_driver/dai_nodes/sensors/rgb.hpp"
#include "depthai_ros_driver/dai_nodes/stereo.hpp"
#include "depthai_ros_driver/pipeline/base_pipeline.hpp"
#include "depthai_ros_driver/utils.hpp"
#include "rclcpp/node.hpp"

using RGB_test_creator_t = std::function<std::unique_ptr<depthai_ros_driver::dai_nodes::BaseNode>(const std::string &,
                                                                                                  rclcpp::Node *,
                                                                                                  std::shared_ptr<dai::Pipeline>,
                                                                                                  dai::CameraBoardSocket,
                                                                                                  depthai_ros_driver::dai_nodes::sensor_helpers::ImageSensor,
                                                                                                  bool)>;
using RGB_test_raw_creator_t = std::unique_ptr<depthai_ros_driver::dai_nodes::BaseNode> (*)(const std::string &,
                                                                                            rclcpp::Node *,
                                                                                            std::shared_ptr<dai::Pipeline>,
                                                                                            dai::CameraBoardSocket,
                                                                                            depthai_ros_driver::dai_nodes::sensor_helpers::ImageSensor,
                                                                                            bool);
namespace depthai_test_plugin
{
    namespace pipeline_gen
    {
        std::vector<std::unique_ptr<depthai_ros_driver::dai_nodes::BaseNode>> RGB_test_pipeline::createPipeline(rclcpp::Node *node,
                                                                                                                std::shared_ptr<dai::Device> device,
                                                                                                                std::shared_ptr<dai::Pipeline> pipeline,
                                                                                                                const std::string &nnType)
        {
            auto sensorName = device->getCameraSensorNames().at(dai::CameraBoardSocket::CAM_A);
            std::vector<std::unique_ptr<depthai_ros_driver::dai_nodes::BaseNode>> daiNodes;
            std::vector<depthai_ros_driver::dai_nodes::sensor_helpers::ImageSensor>::iterator sensorIt =
                std::find_if(depthai_ros_driver::dai_nodes::sensor_helpers::availableSensors.begin(), depthai_ros_driver::dai_nodes::sensor_helpers::availableSensors.end(), [&sensorName](const depthai_ros_driver::dai_nodes::sensor_helpers::ImageSensor &s)
                             { return s.name == sensorName; });
            if (sensorIt == depthai_ros_driver::dai_nodes::sensor_helpers::availableSensors.end())
            {
                RCLCPP_ERROR(node->get_logger(), "Sensor %s not supported!", sensorName.c_str());
                throw std::runtime_error("Sensor not supported!");
            }

            void *handle = dlopen("libdepthai_test_plugin.so", RTLD_LAZY);
            if (!handle)
            {
                throw std::runtime_error(dlerror());
            }

            dlerror(); //
            RGB_test_raw_creator_t raw_creator = reinterpret_cast<RGB_test_raw_creator_t>(dlsym(handle, "create"));
            RGB_test_creator_t creator = raw_creator;
            const char *dlsym_error = dlerror();
            if (dlsym_error)
            {
                std::cerr << "Cannot load symbol 'create': " << dlsym_error << '\n';
                dlclose(handle);
                throw std::runtime_error(dlsym_error);
            }

            auto rgb = creator("rgb", node, pipeline, dai::CameraBoardSocket::CAM_A, (*sensorIt), true);
            dlopen("libdepthai_test_plugin.so", RTLD_LAZY);
            daiNodes.push_back(std::move(rgb));
            return daiNodes;
        }
    }
}

#include <pluginlib/class_list_macros.hpp>

PLUGINLIB_EXPORT_CLASS(depthai_test_plugin::pipeline_gen::RGB_test_pipeline, depthai_ros_driver::pipeline_gen::BasePipeline)

from depthai-ros.

BOOTCFG avatar BOOTCFG commented on August 11, 2024

That fixed it for me, thank you.

from depthai-ros.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.