update demo

This commit is contained in:
Robot 2024-01-29 08:16:52 +08:00
parent 2f2b70a0d8
commit 37f04b96e2
6 changed files with 100 additions and 3 deletions

View File

@ -10,7 +10,7 @@
1. 获取源码:
```
cd ~/ros2_ws/src/
git clone https://gitee.com/zhangwanjie/wpr_simulation2.git
git clone https://github.com/6-robot/wpr_simulation2.git
```
2. 安装依赖项:
ROS2 Humble (Ubuntu 22.04)

View File

@ -0,0 +1,35 @@
cmake_minimum_required(VERSION 3.8)
project(my_pkg)
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)
# uncomment the following section in order to fill in
# further dependencies manually.
# find_package(<dependency> REQUIRED)
find_package(rclcpp REQUIRED)
find_package(std_msgs REQUIRED)
add_executable(my_node src/my_node.cpp)
ament_target_dependencies(my_node "rclcpp")
install(TARGETS
my_node
DESTINATION lib/${PROJECT_NAME})
if(BUILD_TESTING)
find_package(ament_lint_auto REQUIRED)
# the following line skips the linter which checks for copyrights
# comment the line when a copyright and license is added to all source files
set(ament_cmake_copyright_FOUND TRUE)
# the following line skips cpplint (only works in a git repo)
# comment the line when this package is in a git repo and when
# a copyright and license is added to all source files
set(ament_cmake_cpplint_FOUND TRUE)
ament_lint_auto_find_test_dependencies()
endif()
ament_package()

View File

@ -1,5 +1,4 @@
#include <rclcpp/rclcpp.hpp>
#include <std_msgs/msg/string.hpp>
#include <sensor_msgs/msg/laser_scan.hpp>
#include <geometry_msgs/msg/twist.hpp>

View File

@ -108,7 +108,7 @@ void Cam_RGB_Callback(const sensor_msgs::msg::Image::SharedPtr msg)
// Show the image
imshow("Result", imgThresholded);
imshow("Original", imgOriginal);
imshow("RGB", imgOriginal);
cv::waitKey(5);
}
@ -123,6 +123,9 @@ int main(int argc, char** argv)
10,
Cam_RGB_Callback);
namedWindow("RGB");
namedWindow("Result");
rclcpp::spin(node);
cv::destroyAllWindows();

View File

@ -0,0 +1,20 @@
<?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>my_pkg</name>
<version>0.0.0</version>
<description>TODO: Package description</description>
<maintainer email="robot@6-robot.com">robot</maintainer>
<license>TODO: License declaration</license>
<buildtool_depend>ament_cmake</buildtool_depend>
<depend>rclcpp</depend>
<test_depend>ament_lint_auto</test_depend>
<test_depend>ament_lint_common</test_depend>
<export>
<build_type>ament_cmake</build_type>
</export>
</package>

View File

@ -0,0 +1,40 @@
#include <rclcpp/rclcpp.hpp>
#include <sensor_msgs/msg/image.hpp>
#include <cv_bridge/cv_bridge.h>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
std::shared_ptr<rclcpp::Node> node;
void CamRGBCallback(const sensor_msgs::msg::Image::SharedPtr msg)
{
cv_bridge::CvImagePtr cv_ptr;
cv_ptr = cv_bridge::toCvCopy(msg, sensor_msgs::image_encodings::BGR8);
cv::Mat imgOriginal = cv_ptr->image;
cv::imshow("RGB", imgOriginal);
cv::waitKey(1);
// Save the image to file
cv::imwrite("/home/robot/image.jpg",imgOriginal);
printf("Saved the image to file!\n");
}
int main(int argc, char **argv)
{
rclcpp::init(argc, argv);
node = std::make_shared<rclcpp::Node>("cv_save_file");
auto rgb_sub = node->create_subscription<sensor_msgs::msg::Image>(
"/kinect2/qhd/image_raw", 1, CamRGBCallback);
cv::namedWindow("RGB");
rclcpp::spin(node);
cv::destroyAllWindows();
rclcpp::shutdown();
return 0;
}