Skip to content

Commit

Permalink
Allow showimage and cam2image to subscribe/publish to alternate topic…
Browse files Browse the repository at this point in the history
…s. (#117)

* Allow showimage and cam2image to subscribe/publish to alternate topics.

This allows them to be a little more generic and useful for
debugging.  The command line option is "-t", and we print
out which topic we are publishing/subscribing to on program startup.
  • Loading branch information
clalancette authored Mar 8, 2017
1 parent 1e9b846 commit 1205388
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 7 deletions.
3 changes: 2 additions & 1 deletion image_tools/include/image_tools/options.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ bool parse_command_options(
int argc, char ** argv, size_t * depth,
rmw_qos_reliability_policy_t * reliability_policy,
rmw_qos_history_policy_t * history_policy, bool * show_camera = nullptr, double * freq = nullptr,
size_t * width = nullptr, size_t * height = nullptr, bool * burger_mode = nullptr);
size_t * width = nullptr, size_t * height = nullptr, bool * burger_mode = nullptr,
std::string * topic = nullptr);

#endif // IMAGE_TOOLS__OPTIONS_HPP_
8 changes: 5 additions & 3 deletions image_tools/src/cam2image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,12 @@ int main(int argc, char * argv[])
size_t width = 320;
size_t height = 240;
bool burger_mode = false;
std::string topic("image");

// Configure demo parameters with command line options.
bool success = parse_command_options(
argc, argv, &depth, &reliability_policy, &history_policy, &show_camera, &freq, &width, &height,
&burger_mode);
&burger_mode, &topic);
if (!success) {
return 0;
}
Expand All @@ -113,9 +114,10 @@ int main(int argc, char * argv[])
// parameter.
custom_camera_qos_profile.history = history_policy;

printf("Publishing data on topic '%s'\n", topic.c_str());
// Create the image publisher with our custom QoS profile.
auto pub = node->create_publisher<sensor_msgs::msg::Image>(
"image", custom_camera_qos_profile);
topic, custom_camera_qos_profile);

// is_flipped will cause the incoming camera image message to flip about the y-axis.
bool is_flipped = false;
Expand Down Expand Up @@ -193,7 +195,7 @@ int main(int argc, char * argv[])
cv::waitKey(1);
}
// Publish the image message and increment the frame_id.
std::cout << "Publishing image #" << i << std::endl;
printf("Publishing image #%zd\n", i);
pub->publish(msg);
++i;
}
Expand Down
13 changes: 12 additions & 1 deletion image_tools/src/options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ bool parse_command_options(
int argc, char ** argv, size_t * depth,
rmw_qos_reliability_policy_t * reliability_policy,
rmw_qos_history_policy_t * history_policy, bool * show_camera,
double * freq, size_t * width, size_t * height, bool * burger_mode)
double * freq, size_t * width, size_t * height, bool * burger_mode,
std::string * topic)
{
std::vector<std::string> args(argv, argv + argc);

Expand Down Expand Up @@ -76,6 +77,9 @@ bool parse_command_options(
if (burger_mode != nullptr) {
ss << " -b: produce images of burgers rather than connecting to a camera" << std::endl;
}
if (topic != nullptr) {
ss << " -t TOPIC: use topic TOPIC instead of the default" << std::endl;
}
std::cout << ss.str();
return false;
}
Expand Down Expand Up @@ -123,5 +127,12 @@ bool parse_command_options(
*burger_mode = get_flag_option(args, "-b");
}

if (topic != nullptr) {
std::string tmptopic = get_command_option(args, "-t");
if (!tmptopic.empty()) {
*topic = tmptopic;
}
}

return true;
}
7 changes: 5 additions & 2 deletions image_tools/src/showimage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,12 @@ int main(int argc, char * argv[])
rmw_qos_reliability_policy_t reliability_policy = RMW_QOS_POLICY_RELIABILITY_RELIABLE;
rmw_qos_history_policy_t history_policy = RMW_QOS_POLICY_HISTORY_KEEP_ALL;
bool show_camera = true;
std::string topic("image");

// Configure demo parameters with command line options.
if (!parse_command_options(
argc, argv, &depth, &reliability_policy, &history_policy, &show_camera))
argc, argv, &depth, &reliability_policy, &history_policy, &show_camera, nullptr, nullptr,
nullptr, nullptr, &topic))
{
return 0;
}
Expand Down Expand Up @@ -128,9 +130,10 @@ int main(int argc, char * argv[])
show_image(msg, show_camera);
};

printf("Subscribing to topic '%s'\n", topic.c_str());
// Initialize a subscriber that will receive the ROS Image message to be displayed.
auto sub = node->create_subscription<sensor_msgs::msg::Image>(
"image", callback, custom_qos_profile);
topic, callback, custom_qos_profile);

rclcpp::spin(node);

Expand Down

0 comments on commit 1205388

Please sign in to comment.