Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Polygon Info obtained from zbar #11

Merged
merged 7 commits into from
Apr 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ Subscriptions:
* `image` (`sensor_msgs/msg/Image`)

Publisher:
* `barcode` (`std_msgs/msg/String`)
* `symbol` (`zbar_ros_interfaces/msg/Symbol`)
* `barcode` (`std_msgs/msg/String`) - **DEPRECATED**


## Debugging the barcode_reader node
Expand Down
5 changes: 3 additions & 2 deletions CMakeLists.txt → zbar_ros/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ endif()
find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(sensor_msgs REQUIRED)
find_package(std_msgs REQUIRED)
find_package(zbar_ros_interfaces REQUIRED)
find_package(cv_bridge REQUIRED)

# Include header files
Expand All @@ -32,7 +32,8 @@ ament_target_dependencies(barcode_reader_node
rclcpp
sensor_msgs
std_msgs
cv_bridge)
cv_bridge
zbar_ros_interfaces)
target_link_libraries(barcode_reader_node zbar)

# Build executable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include "./zbar.h"
#include "sensor_msgs/msg/image.hpp"
#include "std_msgs/msg/string.hpp"
#include "zbar_ros_interfaces/msg/symbol.hpp"

namespace zbar_ros
{
Expand All @@ -52,7 +53,8 @@ class BarcodeReaderNode : public rclcpp::Node
void cleanCb();

rclcpp::Subscription<sensor_msgs::msg::Image>::SharedPtr camera_sub_;
rclcpp::Publisher<std_msgs::msg::String>::SharedPtr barcode_pub_;
rclcpp::Publisher<zbar_ros_interfaces::msg::Symbol>::SharedPtr symbol_pub_;
rclcpp::Publisher<std_msgs::msg::String>::SharedPtr barcode_pub_; // DEPRECATED
zbar::ImageScanner scanner_;

rclcpp::TimerBase::SharedPtr clean_timer_;
Expand Down
1 change: 1 addition & 0 deletions package.xml → zbar_ros/package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<depend>cv_bridge</depend>
<depend>sensor_msgs</depend>
<depend>std_msgs</depend>
<depend>zbar_ros_interfaces</depend>

<test_depend>ament_lint_auto</test_depend>
<test_depend>ament_lint_common</test_depend>
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ BarcodeReaderNode::BarcodeReaderNode()
{
scanner_.set_config(zbar::ZBAR_NONE, zbar::ZBAR_CFG_ENABLE, 1);


camera_sub_ = this->create_subscription<sensor_msgs::msg::Image>(
"image", 10, std::bind(&BarcodeReaderNode::imageCb, this, std::placeholders::_1));

symbol_pub_ = this->create_publisher<zbar_ros_interfaces::msg::Symbol>("symbol", 10);
barcode_pub_ = this->create_publisher<std_msgs::msg::String>("barcode", 10);

throttle_ = this->declare_parameter<double>("throttle_repeated_barcodes", 0.0);
Expand All @@ -74,14 +74,29 @@ void BarcodeReaderNode::imageCb(sensor_msgs::msg::Image::ConstSharedPtr image)
auto it_end = zbar_image.symbol_end();
if (it_start != it_end) {
// If there are barcodes in the image, iterate over all barcode readings from image
for (zbar::Image::SymbolIterator symbol = it_start; symbol != it_end; ++symbol) {
std::string barcode = symbol->get_data();
RCLCPP_DEBUG(get_logger(), "Barcode detected with data: '%s'", barcode.c_str());
for (zbar::Image::SymbolIterator symbol_it = it_start; symbol_it != it_end; ++symbol_it) {
zbar_ros_interfaces::msg::Symbol symbol;
symbol.data = symbol_it->get_data();
RCLCPP_DEBUG(get_logger(), "Barcode detected with data: '%s'", symbol.data.c_str());

RCLCPP_DEBUG(
get_logger(), "Polygon around barcode has %d points", symbol_it->get_location_size());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a shame vision_msgs only has 'bounding box' type boundaries rather than the polygons zbar reports.

for (zbar::Symbol::PointIterator point_it = symbol_it->point_begin();
point_it != symbol_it->point_end();
++point_it)
{
vision_msgs::msg::Point2D point;
point.x = (*point_it).x;
point.y = (*point_it).y;
RCLCPP_DEBUG(get_logger(), " Point: %f, %f", point.x, point.y);
symbol.points.push_back(point);
}

// verify if repeated barcode throttling is enabled
if (throttle_ > 0.0) {
const std::lock_guard<std::mutex> lock(memory_mutex_);

const std::string & barcode = symbol.data;
// check if barcode has been recorded as seen, and skip detection
if (barcode_memory_.count(barcode) > 0) {
// check if time reached to forget barcode
Expand All @@ -100,16 +115,32 @@ void BarcodeReaderNode::imageCb(sensor_msgs::msg::Image::ConstSharedPtr image)
now() + rclcpp::Duration(std::chrono::duration<double>(throttle_))));
}

// publish barcode
// publish symbol
RCLCPP_DEBUG(get_logger(), "Publishing Symbol");
symbol_pub_->publish(symbol);

// publish on deprecated barcode topic
RCLCPP_DEBUG(get_logger(), "Publishing data as string");
std_msgs::msg::String barcode_string;
barcode_string.data = barcode;
barcode_string.data = symbol.data;
barcode_pub_->publish(barcode_string);
}
} else {
RCLCPP_DEBUG(get_logger(), "No barcode detected in image");
}

// Warn if there are subscriptions on barcode topic, because it's deprecated.
static bool alreadyWarnedDeprecation = false;
if (!alreadyWarnedDeprecation && count_subscribers("barcode") > 0) {
alreadyWarnedDeprecation = true;
RCLCPP_WARN(
get_logger(),
"A subscription was detected on the deprecated topic 'barcode'. Please update the node "
"that is subscribing to use the new topic 'symbol' with type "
"'zbar_ros_interfaces::msg::Symbol' instead. The 'barcode' topic will be removed "
"in the next distribution.");
}

zbar_image.set_data(NULL, 0);
}

Expand Down
36 changes: 36 additions & 0 deletions zbar_ros_interfaces/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
cmake_minimum_required(VERSION 3.5)
project(zbar_ros_interfaces)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Usually these packages are labeled _msgs rather than _interfaces in the ROS ecosystem.

Copy link
Collaborator Author

@ijnek ijnek May 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My understanding is _msgs is the old ROS 1 convention, and _interfaces is the new ROS 2 convention.

Ref:
https://docs.ros.org/en/rolling/Tutorials/Beginner-Client-Libraries/Custom-ROS2-Interfaces.html
https://roboticsbackend.com/ros2-create-custom-message/ ("End with “_interfaces” so it’s clear the package is an interface package. You’ll often see packages ending with “_msgs”, but this is the old convention.")


# Default to C99
if(NOT CMAKE_C_STANDARD)
set(CMAKE_C_STANDARD 99)
endif()

# Default to C++14
if(NOT CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 14)
endif()

if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
add_compile_options(-Wall -Wextra -Wpedantic)
endif()

# find dependencies
find_package(ament_cmake REQUIRED)
find_package(rosidl_default_generators REQUIRED)
find_package(vision_msgs REQUIRED)

# generate interfaces
rosidl_generate_interfaces(${PROJECT_NAME}
"msg/Symbol.msg"
DEPENDENCIES vision_msgs
)

ament_export_dependencies(rosidl_default_runtime)

if(BUILD_TESTING)
find_package(ament_lint_auto REQUIRED)
ament_lint_auto_find_test_dependencies()
endif()

ament_package()
2 changes: 2 additions & 0 deletions zbar_ros_interfaces/msg/Symbol.msg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
string data
vision_msgs/Point2D[] points
24 changes: 24 additions & 0 deletions zbar_ros_interfaces/package.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0"?>
<?xml-model href="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
<package format="3">
<name>zbar_ros_interfaces</name>
<version>0.0.0</version>
<description>Package containing interfaces for zbar_ros to use to publish results</description>
<maintainer email="kenjibrameld@gmail.com">ijnek</maintainer>
<license>BSD</license>

<buildtool_depend>ament_cmake</buildtool_depend>

<depend>vision_msgs</depend>

<test_depend>ament_lint_auto</test_depend>
<test_depend>ament_lint_common</test_depend>

<build_depend>rosidl_default_generators</build_depend>
<exec_depend>rosidl_default_runtime</exec_depend>
<member_of_group>rosidl_interface_packages</member_of_group>

<export>
<build_type>ament_cmake</build_type>
</export>
</package>
Loading