Skip to content

Commit

Permalink
📦 Backported intra-process optimization to foxy_devel branch. Verifie…
Browse files Browse the repository at this point in the history
…d within docker implement.

Signed-off-by: Bey Hao Yun <[email protected]>
  • Loading branch information
cardboardcode committed Mar 12, 2024
1 parent 9a5b076 commit 7225a0d
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 16 deletions.
6 changes: 4 additions & 2 deletions include/virtual_camera/image_viewer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ ImageViewer::ImageViewer()
: Node("image_viewer")
{
cv::namedWindow("image_viewer", cv::WINDOW_AUTOSIZE);
cv::moveWindow("image_viewer", 0, 375);
int x_position = 0;
int y_position = 0;
cv::moveWindow("image_viewer", x_position, y_position);
cv::waitKey(1);

size_t depth_ = rmw_qos_profile_default.depth;
Expand All @@ -69,7 +71,7 @@ ImageViewer::ImageViewer()
qos.reliability(reliability_policy_);

sub_1_ = this->create_subscription<sensor_msgs::msg::Image>(
"/image_viewer/image_input",
"/virtual_camera/image_raw",
qos, std::bind(&ImageViewer::image_callback, this, std::placeholders::_1));
}

Expand Down
30 changes: 19 additions & 11 deletions launch/showimageraw.launch.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,29 @@
# limitations under the License.

from launch import LaunchDescription
import launch_ros.actions
from launch.actions import DeclareLaunchArgument
from launch.substitutions import LaunchConfiguration
from launch_ros.actions import Node


def generate_launch_description():
return LaunchDescription([
launch_ros.actions.Node(

imageviewer_flag_param = DeclareLaunchArgument(
'use_image_viewer',
default_value='false',
description='Set use_image_viewer [yes/no]'
)

vcam_node = Node(
package='virtual_camera',
executable='virtual_camera',
output='screen',
),
launch_ros.actions.Node(
package='virtual_camera',
executable='image_viewer',
output='log',
remappings=[('/image_viewer/image_input', '/virtual_camera/image_raw')]
),
parameters=[{
'use_image_viewer': LaunchConfiguration('use_image_viewer')
}]
)

])
return LaunchDescription([
imageviewer_flag_param,
vcam_node
])
2 changes: 1 addition & 1 deletion scripts/show_image.bash
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

source /opt/ros/foxy/setup.bash
source install/setup.bash
ros2 launch virtual_camera showimageraw.launch.py
ros2 launch virtual_camera showimageraw.launch.py use_image_viewer:=true
39 changes: 37 additions & 2 deletions src/virtual_camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,47 @@

#include <memory>
#include "virtual_camera/virtual_camera.hpp"

#include "virtual_camera/image_viewer.hpp"

int main(int argc, char * argv[])
{
rclcpp::init(argc, argv);
rclcpp::spin(std::make_shared<VirtualCamera>());

rclcpp::executors::SingleThreadedExecutor executor;

// Connect the nodes as a pipeline: vcamera_node -> image_view_node
// Instantiate VirtualCamera ROS 2 node.
std::shared_ptr<VirtualCamera> vcamera_node = nullptr;
try {
vcamera_node = std::make_shared<VirtualCamera>();
} catch (const std::exception & e) {
fprintf(stderr, "%s Exiting ..\n", e.what());
return 1;
}

// Declare a boolean parameter
vcamera_node->declare_parameter("use_image_viewer", false);

// Get the boolean parameter value to determine
// if the image_view_node should be activated.
bool imageviewer_flag = vcamera_node->get_parameter("use_image_viewer").as_bool();
RCLCPP_INFO(
vcamera_node->get_logger(), "use_image_viewer set to: [%s]",
imageviewer_flag ? "true" : "false");

// Instantiate ImageViewer ROS 2 node.
auto image_view_node = std::make_shared<ImageViewer>();
// Add VirtualCamera ROS 2 node to intra-process executor
executor.add_node(vcamera_node);

// If use_image_viewer ROS 2 parameter is set to true,
// add ImageViewer ROS 2 node to intra-process executor.
if (imageviewer_flag) {
executor.add_node(image_view_node);
}

executor.spin();

rclcpp::shutdown();
return 0;
}

0 comments on commit 7225a0d

Please sign in to comment.