ConfigBase类完成-CPP

This commit is contained in:
liangyuxuan
2025-12-05 18:48:09 +08:00
parent 250e2600f8
commit 8c7c38eaf4
15 changed files with 472 additions and 259 deletions

View File

@@ -14,6 +14,7 @@ find_package(sensor_msgs REQUIRED)
find_package(cv_bridge REQUIRED)
find_package(message_filters REQUIRED)
find_package(interfaces REQUIRED)
find_package(ament_index_cpp REQUIRED)
add_executable(

View File

@@ -2,11 +2,13 @@
"node_name": "default_config_detect_service",
"output_boxes": "True",
"output_masks": "False",
"calibrition_path": {
"calibration": {
"left_hand": "configs/hand_eye_mat/eye_in_left_hand.json",
"right_hand": "configs/hand_eye_mat/eye_in_right_hand.json",
"head": "configs/hand_eye_mat/eye_to_hand.json"
},
"get_camera_mode": "Service",
"Service_configs": {
"service_name": "/vision_object_recognition"
@@ -17,10 +19,11 @@
"depth_image_topic_name": "/camera/depth/image_raw",
"camera_info_topic_name": "/camera/color/camera_info"
},
"detect_mode": "Detect",
"Detect_configs": {
"checkpoint_path": "checkpoints/yolo11s-seg.pt",
"confidence": 0.25,
"confidence": 0.50,
"classes": []
},
"Color_configs": {
@@ -30,24 +33,18 @@
"Crossboard_configs": {
"pattern_size": [8, 5]
},
"calculate_mode": "PCA",
"PCA_configs": {
"depth_scale": 1000.0,
"depth_trunc": 3.0,
"voxel_size": 0.020,
"nb_points": 10,
"radius": 0.1,
"nb_neighbors": 20,
"std_ratio": 3.0
"voxel_size": 0.010
},
"ICP_configs": {
"complete_model_path": "pointclouds/bottle_model.pcd",
"depth_scale": 1000.0,
"depth_trunc": 2.0,
"nb_points": 10,
"radius": 0.1,
"nb_neighbors": 20,
"std_ratio": 3.0,
"voxel_size": 0.010,
"ransac_voxel_size": 0.005,
"icp_voxel_radius": [0.004, 0.002, 0.001],
"icp_max_iter": [50, 30, 14]

View File

@@ -0,0 +1,80 @@
#include <string>
#include <vector>
#include <array>
#include <cstdint>
#include <nlohmann/json.hpp>
#include <Eigen/Dense>
#include "rclcpp/rclcpp.hpp"
#include "message_filters/subscriber.hpp"
#include "message_filters/sync_policies/approximate_time.hpp"
#include "sensor_msgs/msg/image.hpp"
#include "sensor_msgs/msg/camera_info.hpp"
#include "interfaces/srv/vision_object_recognition.hpp"
#include "interfaces/msg/pose_array_class_and_id.hpp"
#include "ament_index_cpp/get_package_share_directory.hpp"
#include "vision_test/utils/read_file_tools.hpp"
namespace policy {
using SyncPolicy = message_filters::sync_policies::ApproximateTime<
sensor_msgs::msg::Image, sensor_msgs::msg::Image>;
}
namespace path {
extern std::string shared_path;
}
class ConfigBase: public rclcpp::Node {
public:
explicit ConfigBase(const std::string name);
protected:
nlohmann::json configs;
nlohmann::json calculate_configs;
nlohmann::json camera_data;
bool output_box, output_mask;
std::string get_camera_mode, detect_mode, calculate_mode;
std::string service_name, topic_name;
std::string depth_topic_name, color_topic_name, camera_info_topic_name;
std::string position;
std::string checkpoint_path;
float confidence;
std::uint16_t distance;
std::array<int, 2> pattern_size;
std::array<double, 9> k;
std::array<double, 5> k;
Eigen::Matrix4d left_hand_mat, right_hand_mat, head_mat, hand_eye_mat;
void (ConfigBase::*detect_function)();
void (ConfigBase::*calculate_function)();
rclcpp::Publisher<sensor_msgs::msg::Image>::SharedPtr pub_detect_img;
rclcpp::Publisher<interfaces::msg::PoseArrayClassAndID>::SharedPtr pub_pose_list;
message_filters::Subscriber<sensor_msgs::msg::Image> color_img_sub;
message_filters::Subscriber<sensor_msgs::msg::Image> depth_img_sub;
std::shared_ptr<policy::SyncPolicy> sync_sub;
rclcpp::Subscription<sensor_msgs::msg::CameraInfo>::SharedPtr camera_info_sub;
rclcpp::Service<interfaces::srv::VisionObjectRecognition>::SharedPtr detect_server;
std::vector<std::vector<std::vector<int>>> color_range;
std::vector<int> classes;
void read_calibration_mat();
};

View File

@@ -0,0 +1,3 @@
#include "vision_test/config_base.hpp"

View File

@@ -12,6 +12,8 @@
#include "interfaces/srv/vision_object_recognition.hpp"
#include "interfaces/msg/pose_array_class_and_id.hpp"
// #include "vision_test/init_base.hpp"
class VisionDetectNode: public rclcpp::Node {
public: explicit VisionDetectNode(const std::string& name);

View File

@@ -1,4 +0,0 @@
#include "vision_test/CalculateTools.hpp"

View File

@@ -1,243 +0,0 @@
#include "vision_test/VisionDetectNode.hpp"
#include "vision_test/utils.hpp"
using json = nlohmann::json;
using SyncPolicy=message_filters::sync_policies::ApproximateTime<sensor_msgs::msg::Image, sensor_msgs::msg::Image>;
VisionDetectNode::VisionDetectNode(const std::string& name): Node(name) {
/**/
this->init_param();
this->init_calibration_mat();
if (this->output_box || this->output_mask) {
this->init_publisher();
}
if (this->get_camera_mode == "Service") {
this->get_camera_configs = json::parse(this->get_parameter("ServiceConfigs").as_string());
this->init_service();
this->init_subscriber();
}
else if (this->get_camera_mode == "Topic") {
this->get_camera_configs = json::parse(this->get_parameter("TopicConfigs").as_string());
this->init_topic();
}
else {
RCLCPP_WARN(this->get_logger(), "Unkonw get_camera_mode, Use Service");
this->get_camera_configs = json::parse(this->get_parameter("ServiceConfigs").as_string());
this->init_service();
this->init_subscriber();
}
if (this->detect_mode == "Detect") {
this->init_model();
this->segmentation_configs = json::parse(this->get_parameter("DetectConfigs").as_string());
this->detect_function = &VisionDetectNode::seg_img;
}
else if (this->detect_mode == "Color") {
this->segmentation_configs = json::parse(this->get_parameter("ColorConfigs").as_string());
this->detect_function = &VisionDetectNode::seg_color;
}
else if (this->detect_mode == "Crossboard") {
this->segmentation_configs = json::parse(this->get_parameter("CrossboardConfigs").as_string());
this->detect_function = &VisionDetectNode::seg_crossboard;
}
else {
RCLCPP_WARN(this->get_logger(), "Unkonw detect_mode, Use Service");
this->segmentation_configs = json::parse(this->get_parameter("DetectConfigs").as_string());
}
RCLCPP_INFO(this->get_logger(), "Init process done");
}
void VisionDetectNode::init_param() {
/*Init Param*/
this->declare_parameter<bool>("output_box", true);
this->declare_parameter<bool>("output_mask", false);
this->output_box = this->get_parameter("output_box").as_bool();
this->output_mask = this->get_parameter("output_mask").as_bool();
this->declare_parameter<std::string>("get_camera_mode", "Service");
this->declare_parameter<std::string>("detect_mode", "Detect");
this->declare_parameter<std::string>("calculate_mode", "PCA");
this->get_camera_mode = this->get_parameter("get_camera_mode").as_string();
this->detect_mode = this->get_parameter("detect_mode").as_string();
this->calculate_mode = this->get_parameter("calculate_mode").as_string();
this->declare_parameter<std::string>("calibrition_path", "{}");
this->calibrition_path = json::parse(this->get_parameter("calibrition_path").as_string());
this->declare_parameter<std::string>("ServiceConfigs", "{}");
this->declare_parameter<std::string>("TopicConfigs", "{}");
this->declare_parameter<std::string>("DetectConfigs", "{}");
this->declare_parameter<std::string>("ColorConfigs", "{}");
this->declare_parameter<std::string>("CrossboardConfigs", "{}");
this->declare_parameter<std::string>("PCAConfigs", "{}");
this->declare_parameter<std::string>("ICPConfigs", "{}");
RCLCPP_INFO(this->get_logger(), "Init param done");
// Done
}
void VisionDetectNode::init_model() {
/*Init Model*/
RCLCPP_INFO(this->get_logger(), "Init model done");
}
void VisionDetectNode::init_calibration_mat() {
/*Init Calibration Mat*/
std::string info;
bool sign;
info = "left";
this->left_hand_mat = read_data_from_json(
this->calibrition_path["left_hand"].get<std::string>(),
info,
sign
);
if (sign) {
RCLCPP_INFO(this->get_logger(), info.c_str());
}
else {
RCLCPP_WARN(this->get_logger(), info.c_str());
}
info = "right";
this->right_hand_mat = read_data_from_json(
this->calibrition_path["right_hand"].get<std::string>(),
info,
sign
);
if (sign) {
RCLCPP_INFO(this->get_logger(), info.c_str());
}
else {
RCLCPP_WARN(this->get_logger(), info.c_str());
}
info = "head";
this->head_mat = read_data_from_json(
this->calibrition_path["head"].get<std::string>(),
info,
sign
);
if (sign) {
RCLCPP_INFO(this->get_logger(), info.c_str());
}
else {
RCLCPP_WARN(this->get_logger(), info.c_str());
}
RCLCPP_INFO(this->get_logger(), "Init calibration mat done");
// Done
}
void VisionDetectNode::init_service() {
/*Init_service*/
detect_server = this->create_service<interfaces::srv::VisionObjectRecognition>(
this->get_camera_configs["service_name"].get<std::string>(),
std::bind(
&VisionDetectNode::service_callback,
this,
std::placeholders::_1,
std::placeholders::_2
)
);
RCLCPP_INFO(this->get_logger(), "Init service done");
// Done
}
void VisionDetectNode::init_topic() {
/*Init Topic*/
this->color_img_sub.subscribe(this, this->get_camera_configs["color_image_topic_name"].get<std::string>());
this->depth_img_sub.subscribe(this, this->get_camera_configs["depth_image_topic_name"].get<std::string>());
this->sync_sub = std::make_shared<message_filters::Synchronizer<SyncPolicy>>(
SyncPolicy(10),
color_img_sub, depth_img_sub
);
this->sync_sub->registerCallback(&VisionDetectNode::topic_callback, this);
this->camera_info_sub = this->create_subscription<sensor_msgs::msg::CameraInfo>(
this->get_camera_configs["camera_info_topic_name"].get<std::string>(),
10,
std::bind(&VisionDetectNode::camera_info_callback, this, std::placeholders::_1)
);
RCLCPP_INFO(this->get_logger(), "Init topic done");
// Done
}
void VisionDetectNode::init_publisher() {
/*Init Publisher*/
pub_detect_img = this->create_publisher<sensor_msgs::msg::Image>("/image/detect_image", 10);
if (get_camera_mode == "Topic") {
pub_pose_list = this->create_publisher<interfaces::msg::PoseArrayClassAndID>("/pose/cv_detect_pose", 10);
}
RCLCPP_INFO(this->get_logger(), "Init publisher done");
}
void VisionDetectNode::init_subscriber() {
/*Init Subscriber*/
RCLCPP_INFO(this->get_logger(), "Init subscriber done");
}
void VisionDetectNode::service_callback(
const std::shared_ptr<interfaces::srv::VisionObjectRecognition::Request> &request,
std::shared_ptr<interfaces::srv::VisionObjectRecognition::Response> response
) {
/**/
}
void VisionDetectNode::topic_callback(const std::shared_ptr<sensor_msgs::msg::Image> &msg) {
/**/
}
void VisionDetectNode::camera_info_callback(const std::shared_ptr<sensor_msgs::msg::CameraInfo> &msg) {
/**/
}
void VisionDetectNode::seg_img() {
/**/
}
void VisionDetectNode::seg_color() {
/**/
}
void VisionDetectNode::seg_crossboard() {
/**/
}

View File

@@ -0,0 +1,4 @@
#include "vision_test/utils/calculate_tools.hpp"

View File

@@ -0,0 +1,126 @@
#include <fstream>
#include <algorithm>
#include <nlohmann/json.hpp>
#include "vision_test/config_base.hpp"
namespace path {
std::string shared_path;
}
ConfigBase::ConfigBase(const std::string name): Node(name) {
this->declare_parameter("configs_path", "configs/launch_configs/default_config.json");
std::string config_path = this->get_parameter("configs_path").as_string();
std::ifstream file(config_path);
file >> this->configs;
this->output_box = (this->configs["output_box"].get<std::string>()) == "True";
this->output_mask = (this->configs["output_mask"].get<std::string>()) == "True";
this->get_camera_mode = this->configs["get_camera_mode"].get<std::string>();
this->detect_mode = this->configs["detect_mode"].get<std::string>();
this->calculate_mode = this->configs["calculate_mode"].get<std::string>();
this->read_calibration_mat();
if (this->get_camera_mode == "Service") {
this->service_name = this->configs["service_name"].get<std::string>();
}
else if (this->get_camera_mode == "Topic") {
this->topic_name = this->configs["topic_name"].get<std::string>();
this->depth_topic_name = this->configs["depth_topic_name"].get<std::string>();
this->color_topic_name = this->configs["color_topic_name"].get<std::string>();
this->camera_info_topic_name = this->configs["camera_info_topic_name"].get<std::string>();
}
else {
RCLCPP_WARN(this->get_logger(), "Unknown get_camera_mode, Use Service");
this->service_name = this->configs["service_name"].get<std::string>();
}
if (this->detect_mode == "Detect") {
this->confidence = this->configs["confidence"].get<float>();
this->checkpoint_path = this->configs["checkpoint_path"].get<std::string>();
this->classes = this->configs["classes"].get<std::vector<int>>();
}
else if (this->detect_mode == "Color") {
this->color_range = this->configs["color_range"].get<std::vector<std::vector<std::vector<int>>>>();
this->distance = this->configs["distance"].get<std::uint16_t>();
}
else if (this->detect_mode == "Crossboard") {
this->pattern_size = this->configs["pattern_size"].get<std::array<int, 2>>();
}
else {
RCLCPP_WARN(this->get_logger(), "Unknown detect_mode, Use Detect");
this->confidence = this->configs["confidence"].get<float>();
this->checkpoint_path = this->configs["checkpoint_path"].get<std::string>();
this->classes = this->configs["classes"].get<std::vector<int>>();
}
if (this->calculate_mode == "PCA") {
this->calculate_configs = this->configs["PCA_configs"];
}
else if (this->calculate_mode == "ICP") {
this->calculate_configs = this->configs["ICP_configs"];
// source
}
else {
RCLCPP_WARN(this->get_logger(), "Unknown calculate_mode, Use PCA");
this->calculate_configs = this->configs["PCA_configs"];
}
RCLCPP_INFO(this->get_logger(), "Get parameters done");
// Done
}
void ConfigBase::read_calibration_mat() {
/*Init Calibration Mat*/
std::string info;
bool sign;
info = "left";
this->left_hand_mat = read_data_from_json(
this->configs["calibration"]["left_hand"].get<std::string>(),
info,
sign
);
if (sign) {
RCLCPP_INFO(this->get_logger(), info.c_str());
}
else {
RCLCPP_WARN(this->get_logger(), info.c_str());
}
info = "right";
this->right_hand_mat = read_data_from_json(
this->configs["calibration"]["right_hand"].get<std::string>(),
info,
sign
);
if (sign) {
RCLCPP_INFO(this->get_logger(), info.c_str());
}
else {
RCLCPP_WARN(this->get_logger(), info.c_str());
}
info = "head";
this->head_mat = read_data_from_json(
this->configs["calibration"]["head"].get<std::string>(),
info,
sign
);
if (sign) {
RCLCPP_INFO(this->get_logger(), info.c_str());
}
else {
RCLCPP_WARN(this->get_logger(), info.c_str());
}
// Done
}

View File

@@ -1,11 +1,12 @@
#include <iostream>
#include <string>
#include "rclcpp/rclcpp.hpp"
#include "vision_test/VisionDetectNode.hpp"
#include "vision_test/vision_detect_node.hpp"
int main(int argc, char ** argv) {
rclcpp::init(argc, argv);
path::shared_path = ament_index_cpp::get_package_share_directory("vision_test");
auto node = std::make_shared<VisionDetectNode>("detect");
rclcpp::spin(node);

View File

@@ -0,0 +1 @@
#include "vision_test/init_base.hpp"

View File

@@ -3,6 +3,8 @@
#include <Eigen/Dense>
#include <nlohmann/json.hpp>
#include "vision_test/utils/read_file_tools.hpp"
Eigen::Matrix4d read_data_from_json(const std::string &file_path, std::string &info, bool &sign) {
Eigen::Matrix4d mat;

View File

@@ -0,0 +1,243 @@
#include "vision_test/vision_detect_node.hpp"
#include "vision_test/utils/read_file_tools.hpp"
using json = nlohmann::json;
VisionDetectNode::VisionDetectNode(const std::string& name): Node(name) {
/**/
// this->init_param();
// this->init_calibration_mat();
// if (this->output_box || this->output_mask) {
// this->init_publisher();
// }
// if (this->get_camera_mode == "Service") {
// this->get_camera_configs = json::parse(this->get_parameter("ServiceConfigs").as_string());
// this->init_service();
// this->init_subscriber();
// }
// else if (this->get_camera_mode == "Topic") {
// this->get_camera_configs = json::parse(this->get_parameter("TopicConfigs").as_string());
// this->init_topic();
// }
// else {
// RCLCPP_WARN(this->get_logger(), "Unkonw get_camera_mode, Use Service");
// this->get_camera_configs = json::parse(this->get_parameter("ServiceConfigs").as_string());
// this->init_service();
// this->init_subscriber();
// }
// if (this->detect_mode == "Detect") {
// this->init_model();
// this->segmentation_configs = json::parse(this->get_parameter("DetectConfigs").as_string());
// this->detect_function = &VisionDetectNode::seg_img;
// }
// else if (this->detect_mode == "Color") {
// this->segmentation_configs = json::parse(this->get_parameter("ColorConfigs").as_string());
// this->detect_function = &VisionDetectNode::seg_color;
// }
// else if (this->detect_mode == "Crossboard") {
// this->segmentation_configs = json::parse(this->get_parameter("CrossboardConfigs").as_string());
// this->detect_function = &VisionDetectNode::seg_crossboard;
// }
// else {
// RCLCPP_WARN(this->get_logger(), "Unkonw detect_mode, Use Service");
// this->segmentation_configs = json::parse(this->get_parameter("DetectConfigs").as_string());
// }
// RCLCPP_INFO(this->get_logger(), "Init process done");
}
// void VisionDetectNode::init_param() {
// /*Init Param*/
// this->declare_parameter<bool>("output_box", true);
// this->declare_parameter<bool>("output_mask", false);
// this->output_box = this->get_parameter("output_box").as_bool();
// this->output_mask = this->get_parameter("output_mask").as_bool();
// this->declare_parameter<std::string>("get_camera_mode", "Service");
// this->declare_parameter<std::string>("detect_mode", "Detect");
// this->declare_parameter<std::string>("calculate_mode", "PCA");
// this->get_camera_mode = this->get_parameter("get_camera_mode").as_string();
// this->detect_mode = this->get_parameter("detect_mode").as_string();
// this->calculate_mode = this->get_parameter("calculate_mode").as_string();
// this->declare_parameter<std::string>("calibrition_path", "{}");
// this->calibrition_path = json::parse(this->get_parameter("calibrition_path").as_string());
// this->declare_parameter<std::string>("ServiceConfigs", "{}");
// this->declare_parameter<std::string>("TopicConfigs", "{}");
// this->declare_parameter<std::string>("DetectConfigs", "{}");
// this->declare_parameter<std::string>("ColorConfigs", "{}");
// this->declare_parameter<std::string>("CrossboardConfigs", "{}");
// this->declare_parameter<std::string>("PCAConfigs", "{}");
// this->declare_parameter<std::string>("ICPConfigs", "{}");
// RCLCPP_INFO(this->get_logger(), "Init param done");
// // Done
// }
void VisionDetectNode::init_model() {
/*Init Model*/
RCLCPP_INFO(this->get_logger(), "Init model done");
}
// void VisionDetectNode::init_calibration_mat() {
// /*Init Calibration Mat*/
// std::string info;
// bool sign;
// info = "left";
// this->left_hand_mat = read_data_from_json(
// this->calibrition_path["left_hand"].get<std::string>(),
// info,
// sign
// );
// if (sign) {
// RCLCPP_INFO(this->get_logger(), info.c_str());
// }
// else {
// RCLCPP_WARN(this->get_logger(), info.c_str());
// }
// info = "right";
// this->right_hand_mat = read_data_from_json(
// this->calibrition_path["right_hand"].get<std::string>(),
// info,
// sign
// );
// if (sign) {
// RCLCPP_INFO(this->get_logger(), info.c_str());
// }
// else {
// RCLCPP_WARN(this->get_logger(), info.c_str());
// }
// info = "head";
// this->head_mat = read_data_from_json(
// this->calibrition_path["head"].get<std::string>(),
// info,
// sign
// );
// if (sign) {
// RCLCPP_INFO(this->get_logger(), info.c_str());
// }
// else {
// RCLCPP_WARN(this->get_logger(), info.c_str());
// }
// RCLCPP_INFO(this->get_logger(), "Init calibration mat done");
// // Done
// }
void VisionDetectNode::init_service() {
/*Init_service*/
detect_server = this->create_service<interfaces::srv::VisionObjectRecognition>(
this->get_camera_configs["service_name"].get<std::string>(),
std::bind(
&VisionDetectNode::service_callback,
this,
std::placeholders::_1,
std::placeholders::_2
)
);
RCLCPP_INFO(this->get_logger(), "Init service done");
// Done
}
void VisionDetectNode::init_topic() {
/*Init Topic*/
this->color_img_sub.subscribe(this, this->get_camera_configs["color_image_topic_name"].get<std::string>());
this->depth_img_sub.subscribe(this, this->get_camera_configs["depth_image_topic_name"].get<std::string>());
this->sync_sub = std::make_shared<message_filters::Synchronizer<SyncPolicy>>(
SyncPolicy(10),
color_img_sub, depth_img_sub
);
this->sync_sub->registerCallback(&VisionDetectNode::topic_callback, this);
this->camera_info_sub = this->create_subscription<sensor_msgs::msg::CameraInfo>(
this->get_camera_configs["camera_info_topic_name"].get<std::string>(),
10,
std::bind(&VisionDetectNode::camera_info_callback, this, std::placeholders::_1)
);
RCLCPP_INFO(this->get_logger(), "Init topic done");
// Done
}
void VisionDetectNode::init_publisher() {
/*Init Publisher*/
pub_detect_img = this->create_publisher<sensor_msgs::msg::Image>("/image/detect_image", 10);
if (get_camera_mode == "Topic") {
pub_pose_list = this->create_publisher<interfaces::msg::PoseArrayClassAndID>("/pose/cv_detect_pose", 10);
}
RCLCPP_INFO(this->get_logger(), "Init publisher done");
}
void VisionDetectNode::init_subscriber() {
/*Init Subscriber*/
RCLCPP_INFO(this->get_logger(), "Init subscriber done");
}
void VisionDetectNode::service_callback(
const std::shared_ptr<interfaces::srv::VisionObjectRecognition::Request> &request,
std::shared_ptr<interfaces::srv::VisionObjectRecognition::Response> response
) {
/**/
}
void VisionDetectNode::topic_callback(const std::shared_ptr<sensor_msgs::msg::Image> &msg) {
/**/
}
void VisionDetectNode::camera_info_callback(const std::shared_ptr<sensor_msgs::msg::CameraInfo> &msg) {
/**/
}
void VisionDetectNode::seg_img() {
/**/
}
void VisionDetectNode::seg_color() {
/**/
}
void VisionDetectNode::seg_crossboard() {
/**/
}