ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

kyubot's profile - activity

2023-09-14 13:52:49 -0500 received badge  Famous Question (source)
2023-07-02 05:28:26 -0500 received badge  Famous Question (source)
2023-06-27 13:28:05 -0500 marked best answer How to initialize image_transport using rclcpp

I am writing a camera node to publish compressed image whenever new image captured from a camera. To do this, I realized that I need to initialize image_transport with node handle however I just don't know how to do that.

Here is the code inside a class

#include "rclcpp/rclcpp.hpp"
#include "sensor_msgs/msg/image.hpp"
#include "cv_bridge/cv_bridge.h"
#include "sensor_msgs/msg/compressed_image.hpp"
#include "image_transport/image_transport.hpp"

class CamPublisher_: public rclcpp::Node
{
  public:
    CamPublisher_()
    : Node("camera")  {
        setvbuf(stdout, NULL, _IONBF, BUFSIZ);
        initialize();
    }
    ~CamPublisher_() {
        cap.release();
    }
  private:
void initialize()
    {
        topic_image = "/camera/raw";
        topic_compressed = "/camera/compressed";
        auto qos = rclcpp::QoS(rclcpp::KeepLast(10));
        pub_image = this->create_publisher<sensor_msgs::msg::Image>(
            topic_image, qos
        );
        // pub_compressed = this->create_publisher<sensor_msgs::msg::CompressedImage>(
        //  topic_compressed, qos
        // );
        pub_transport = it.advertise(topic_compressed, 10);

        cap.open(0);

        if (!cap.isOpened()) {
            RCLCPP_ERROR(this->get_logger(), "Could not open video stream");
            throw std::runtime_error("Could not open video stream");
        }
        cap.set(cv::CAP_PROP_FRAME_WIDTH, 640);
        cap.set(cv::CAP_PROP_FRAME_HEIGHT, 640);

        timer_ = this->create_wall_timer(
                std::chrono::milliseconds(static_cast<int>(1000 / 30)), // 30 fps
                std::bind(&CamPublisher_::timerCallback, this));
    }

    void timerCallback()
    {
        cv::Mat frame;
        cap >> frame;
        if (frame.empty()) {
            return;
        }
        convert_and_publish(frame);
    }

    std::string mat2encoding(int mat_type)
    {
        switch (mat_type) {
            case CV_8UC1:
                return "mono8";
            case CV_8UC3:
                return "bgr8";
            case CV_16SC1:
                return "mono16";
            case CV_8UC4:
                return "rgba8";
            default:
                std::runtime_error("Unsupported encoding type");
        }
        return 0;
    }

    void convert_and_publish(const cv::Mat& frame)
    {
        auto msg = sensor_msgs::msg::Image();
        msg.height = frame.rows;
        msg.width = frame.cols;
        msg.encoding = mat2encoding(std::move(frame.type()));
        msg.step = static_cast<sensor_msgs::msg::Image::_step_type>(frame.step);

        size_t size = frame.step * frame.rows;
        msg.data.resize(size);
        memcpy(&msg.data[0], frame.data, size);

        pub_image->publish(msg);
        sensor_msgs::msg::CompressedImage img_compressed_msg;
    }

    //vars
    cv::VideoCapture cap;

    std::string topic_image;
    std::string topic_compressed;
    rclcpp::Publisher<sensor_msgs::msg::Image>::SharedPtr pub_image;
    //rclcpp::Publisher<sensor_msgs::msg::CompressedImage>::SharedPtr pub_compressed;
    image_transport::ImageTransport it;
    image_transport::Publisher pub_transport;
    rclcpp::TimerBase::SharedPtr timer_;
};

Here is the error when I tried colcon build

ros2_ws/src/stream/src/camera.cpp: In constructor ‘CamPublisher_::CamPublisher_()’:
ros2_ws/src/stream/src/camera.cpp:27:20: error: no matching function for call to ‘image_transport::ImageTransport::ImageTransport()’
   27 |     : Node("camera")
      |                    ^
In file included from ros2_ws/src/stream/src/camera.cpp:18:
/opt/ros/foxy/include/image_transport/image_transport.hpp:116:12: note: candidate: ‘image_transport::ImageTransport::ImageTransport(rclcpp::Node::SharedPtr)’
  116 |   explicit ImageTransport(rclcpp::Node::SharedPtr node);
      |            ^~~~~~~~~~~~~~
/opt/ros/foxy/include/image_transport/image_transport.hpp:116:12: note:   candidate expects 1 argument, 0 provided
make[2]: *** [CMakeFiles/camera.dir/build.make:63: CMakeFiles/camera.dir/src/camera.cpp.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:107: CMakeFiles/camera.dir/all] Error 2
make: *** [Makefile:141: all] Error 2
2023-01-18 07:59:41 -0500 received badge  Famous Question (source)
2023-01-09 07:07:18 -0500 received badge  Famous Question (source)
2022-11-07 11:41:40 -0500 received badge  Notable Question (source)
2022-11-07 11:41:40 -0500 received badge  Popular Question (source)
2022-10-17 00:11:50 -0500 received badge  Famous Question (source)
2022-10-17 00:11:50 -0500 received badge  Notable Question (source)
2022-10-13 13:01:38 -0500 received badge  Famous Question (source)
2022-09-05 09:00:58 -0500 received badge  Famous Question (source)
2022-08-23 19:12:03 -0500 received badge  Famous Question (source)
2022-08-09 19:11:32 -0500 received badge  Popular Question (source)
2022-07-20 02:35:50 -0500 edited question How to initialize image_transport using rclcpp

How to initialize image_transport using rclcpp I am writing a camera node to publish compressed image whenever new image

2022-07-20 02:34:25 -0500 asked a question How to initialize image_transport using rclcpp

How to initialize image_transport using rclcpp I am writing a camera node to publish compressed image whenever new image

2022-07-13 10:28:22 -0500 answered a question ros2 header for magneticfield message

I found the exact path for MagneticField.msg ! It is actually as include "sensor_msgs/msg/magnetic_field.hpp"

2022-07-13 10:28:22 -0500 received badge  Rapid Responder (source)
2022-07-13 10:13:02 -0500 asked a question ros2 header for magneticfield message

ros2 header for magneticfield message I am trying to convert a ros package to ros2. This package includes imu and magne

2022-07-11 08:38:20 -0500 received badge  Notable Question (source)
2022-07-08 19:19:19 -0500 received badge  Popular Question (source)
2022-07-07 09:57:00 -0500 edited question file path not recognized as file ros2 launch

file path not recognized as file ros2 launch Hello, I am running ros2-foxy on ubuntu 20.04 I am writing a launch file t

2022-07-07 05:48:16 -0500 edited question file path not recognized as file ros2 launch

file path not recognized as file ros2 launch Hello, I am running ros2-foxy on ubuntu 20.04 I am writing a launch file t

2022-07-07 05:47:27 -0500 asked a question file path not recognized as file ros2 launch

file path not recognized as file ros2 launch Hello, I am running ros2-foxy on ubuntu 20.04 I am writing a launch file t

2022-07-07 01:36:02 -0500 commented answer ROS2 equivalent of ros::package::getPath

I agree. I need the source directory of the package rather than installed package. Any ideas?

2022-06-29 08:02:58 -0500 asked a question colcon build --symlink-install requres for every change in .py files

colcon build --symlink-install requres for every change in .py files Hello, This is maybe very stupid question but to

2022-06-29 08:02:56 -0500 asked a question colcon build --symlink-install requres for every change in .py files

colcon build --symlink-install requres for every change in .py files Hello, This is maybe very stupid question but to

2022-05-15 08:27:24 -0500 received badge  Famous Question (source)
2022-05-13 14:12:48 -0500 received badge  Notable Question (source)
2022-05-13 13:27:26 -0500 received badge  Popular Question (source)
2022-05-12 09:22:17 -0500 asked a question How Does a Camera Calibration File Work in ROS 2?

How camera calibration file works in ros2? Hello all, I am trying to implement a camera node for jetson nano using open

2022-05-10 05:56:21 -0500 received badge  Notable Question (source)
2022-05-09 22:38:22 -0500 answered a question ros2 autonomous robots in production

How about checking out this robot? https://omorobot.com/en/r1mini/ I developed this robot myself and currently in sales

2022-05-09 22:38:22 -0500 received badge  Rapid Responder (source)
2022-05-09 07:15:35 -0500 marked best answer ros2 foxy install jetson nano fails qt_gui_cpp

I apt installed ros2 on ubuntu 20.04 and worked just fine however I need to install opencv 4.2.0 which caused removing all ros2 installation.

So I need to build install ros2 as described in the document here https://docs.ros.org/en/foxy/Installa...

I followed every step in the process and when trying to colcon build, the process stuck at qt_gui_core as below

$ colcon build --symlink-install

Starting >>> ament_pyflakes
--- stderr: qt_gui_cpp                                                                
g++: error: unrecognized command line option ‘-ffile-prefix-map=/build/sip4-IM_1PV/sip4-4.19.21+dfsg=.’
make[3]: *** [Makefile:18: siplibqt_gui_cpp_sipcmodule.o] Error 1
make[2]: *** [src/qt_gui_cpp_sip/CMakeFiles/libqt_gui_cpp_sip.dir/build.make:61: src/qt_gui_cpp_sip/libqt_gui_cpp_sip.so] Error 2
make[1]: *** [CMakeFiles/Makefile2:237: src/qt_gui_cpp_sip/CMakeFiles/libqt_gui_cpp_sip.dir/all] Error 2
make: *** [Makefile:141: all] Error 2
---
Failed   <<< qt_gui_cpp [18.1s, exited with code 2]
Aborted  <<< ament_pyflakes [14.5s]                                                              
Aborted  <<< rosidl_typesupport_fastrtps_cpp [1min 42s]                                           
Aborted  <<< rviz_rendering [4min 25s]                                              

Summary: 120 packages finished [6min 38s]
  1 package failed: qt_gui_cpp
  3 packages aborted: ament_pyflakes rosidl_typesupport_fastrtps_cpp rviz_rendering
  2 packages had stderr output: qt_gui_cpp rviz_rendering
  187 packages not processed

When tried to build just qt_gui_cpp

$ colcon build --symlink-install --packages-select qt_gui_cpp
Starting >>> qt_gui_cpp
--- stderr: qt_gui_cpp                             
g++: error: unrecognized command line option ‘-ffile-prefix-map=/build/sip4-IM_1PV/sip4-4.19.21+dfsg=.’
g++: error: unrecognized command line option ‘-ffile-prefix-map=/build/sip4-IM_1PV/sip4-4.19.21+dfsg=.’
make[3]: *** [Makefile:18: siplibqt_gui_cpp_sipcmodule.o] Error 1
make[3]: *** Waiting for unfinished jobs....
make[3]: *** [Makefile:18: siplibqt_gui_cpp_sipQList0101qt_gui_cppPluginDescriptor.o] Error 1
g++: error: unrecognized command line option ‘-ffile-prefix-map=/build/sip4-IM_1PV/sip4-4.19.21+dfsg=.’
make[3]: *** [Makefile:18: siplibqt_gui_cpp_sipQList0101qt_gui_cppPluginProvider.o] Error 1
g++: error: unrecognized command line option ‘-ffile-prefix-map=/build/sip4-IM_1PV/sip4-4.19.21+dfsg=.’
make[3]: *** [Makefile:18: siplibqt_gui_cpp_sipQMap0100QString0100QString.o] Error 1
make[2]: *** [src/qt_gui_cpp_sip/CMakeFiles/libqt_gui_cpp_sip.dir/build.make:61: src/qt_gui_cpp_sip/libqt_gui_cpp_sip.so] Error 2
make[1]: *** [CMakeFiles/Makefile2:237: src/qt_gui_cpp_sip/CMakeFiles/libqt_gui_cpp_sip.dir/all] Error 2
make: *** [Makefile:141: all] Error 2
---
Failed   <<< qt_gui_cpp [0.97s, exited with code 2]

Summary: 0 packages finished [5.74s]
  1 package failed: qt_gui_cpp
  1 package had stderr output: qt_gui_cpp

I double checked the process with my own laptop running Ubuntu 20.04
Also followed same instruction and all build ok.
There was some warnings while building qt_gui_cpp as below

--- stderr: qt_gui_cpp                                                                   
qt.shiboken: (typesystem) Unable to locate Clang's built-in include directory (neither by checking the environment variables LLVM_INSTALL_DIR, CLANG_INSTALL_DIR  nor running llvm-config). This may lead to parse errors.
(typesystem) clang_parseTranslationUnit2(0x0, cmd[25]=-fPIC -Wno-constant-logical-operand -std=c++14 -I/home/khyou/ros2_foxy/src/ros-visualization/qt_gui_core/qt_gui_cpp/include -I/home/khyou/ros2_foxy/install/pluginlib/include -I/home/khyou/ros2_foxy/install/class_loader/include -I/home/khyou/ros2_foxy/install/rcpputils/include -I/home/khyou/ros2_foxy/install/rcutils/include -I/home/khyou/ros2_foxy/install/ament_index_cpp/include -I/opt/ros/foxy/include -I/usr/include -I/usr/include/x86_64-linux-gnu/qt5 -I/usr/include/x86_64-linux-gnu/qt5/QtWidgets -I/usr/include/x86_64-linux-gnu/qt5/QtGui -I/usr/include/x86_64-linux-gnu/qt5/QtCore -I/usr/lib/x86_64-linux-gnu/qt5/mkspecs/linux-g ...
(more)
2022-05-09 07:15:31 -0500 answered a question ros2 foxy install jetson nano fails qt_gui_cpp

It turned out,the issue is relate to the version of gcc, g++ currently in use. ubuntu build for Jetson nano comes with

2022-05-05 06:14:03 -0500 marked best answer ROS2 requires colcon build for any changes in python script?

I have been using ROS-melodic so far and my python package runs as I changed in the code without any catkin make however I just realized ROS2 is not working like this.
Every time I change the code, it requires colcon build to reveal its modification. Why this is needed?
Is this is intended feature or I am doing it wrong?

FYI, I am using ROS2 foxy on ubuntu 20.04 laptop. Installed ros and workspace as described in the manual.

2022-05-05 06:13:58 -0500 received badge  Notable Question (source)
2022-05-03 12:44:35 -0500 received badge  Popular Question (source)
2022-05-01 12:57:13 -0500 received badge  Famous Question (source)
2022-05-01 12:57:00 -0500 received badge  Notable Question (source)
2022-05-01 02:26:43 -0500 edited question ros2 foxy install jetson nano fails qt_gui_cpp

ros2 foxy install jetson nano fails qt_gui_cpp I apt installed ros2 on ubuntu 20.04 and worked just fine however I need

2022-04-30 12:36:38 -0500 asked a question ros2 foxy install jetson nano fails qt_gui_cpp

ros2 foxy install jetson nano fails qt_gui_cpp I apt installed ros2 on ubuntu 20.04 and worked just fine however I need

2022-04-05 10:36:26 -0500 received badge  Notable Question (source)
2022-03-31 21:22:24 -0500 marked best answer ROS2 cannot load custom my_package.srv

I am trying to add my custom service to my_package. I created a 'srv' folder under 'my_package' and add "Battery.srv" as below

---
float64 volt
float64 SOC
float64 current

and added followings to package.xml

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

and in CMakeLists.txt

find_package(builtin_interfaces REQUIRED)
find_package(rosidl_default_generators REQUIRED)

rosidl_generate_interfaces(${PROJECT_NAME}
  "srv/Battery.srv"
)

Also in my setup.py

setup(
    name=package_name,
    version='0.0.1',
    packages=find_packages(exclude=[]),
    data_files=[
        ('share/ament_index/resource_index/packages', ['resource/' + package_name]),
        ('share/' + package_name, ['package.xml']),
        (os.path.join('share', package_name, 'launch'), glob(os.path.join('launch', '*.launch.py'))),
        (os.path.join('share', package_name, 'param'), glob(os.path.join('param', '*.yaml'))),
        (os.path.join('share', package_name, 'srv'), glob(os.path.join('srv', '*.srv'))),
    ],

Finally in my package_node.py I tried to import

from my_package.srv import Battery

After colcon build this package successfully, to checked my service I entered below

 $ ros2 interface show my_package/srv/Battery
---
float64 volt
float64 SOC
float64 current

Now when I tried to run my package node I got error like this

File "/home/my/ros2_ws/build/my_package/my_package/scripts/my_package_node.py", line 14, in <module>
    from my_package.srv import Battery
ModuleNotFoundError: No module named 'my_package.srv'

Is there anything wrong with my practice? Any advice would be much appreciated. Kyu

2022-03-31 21:22:24 -0500 received badge  Scholar (source)
2022-03-31 21:21:38 -0500 received badge  Notable Question (source)
2022-03-31 01:11:13 -0500 commented answer ROS2 cannot load custom my_package.srv

As you said, source ~/bashrc did the trick and works not. I also realized srv file should not include any Capital letter

2022-03-31 01:11:00 -0500 commented answer ROS2 cannot load custom my_package.srv

As you said, source ~/bashrc did the trick and works not. I also realized srv file should not include any Capital letter

2022-03-31 01:09:31 -0500 received badge  Popular Question (source)
2022-03-31 01:07:49 -0500 received badge  Popular Question (source)