Skip to content

Commit

Permalink
Added xml eg and cc listener
Browse files Browse the repository at this point in the history
Signed-off-by: Aditya Pande <[email protected]>
  • Loading branch information
adityapande-1995 committed Aug 24, 2023
1 parent ba42a2b commit 7d2ced7
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 11 deletions.
4 changes: 2 additions & 2 deletions bazel_ros2_rules/ros2/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ py_library(
)

py_library(
name = "roslaunch_base.py",
srcs = ["tools/roslaunch_base.py"],
name = "roslaunch_util.py",
srcs = ["tools/roslaunch_util.py"],
visibility = ["//visibility:public"],
deps = ["@bazel_tools//tools/python/runfiles"],
)
Expand Down
10 changes: 6 additions & 4 deletions bazel_ros2_rules/ros2/ros_py.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,15 @@ def ros_py_binary(
)
py_binary_rule(name = name, **kwargs)

# Use impl pattern here, obtain the workspace from ctx.
def _impl_ros_launch(ctx):
pass

def ros_launch(
name,
main = "@bazel_ros2_rules//ros2:roslaunch_base.py",
launch_file_dir = None,
main = "@bazel_ros2_rules//ros2:roslaunch_util.py",
launch_file = None,
node_targets = [],
**kwargs):

if "deps" not in kwargs.keys():
Expand All @@ -150,10 +151,11 @@ def ros_launch(
kwargs["args"] = []

kwargs["deps"] += ["@ros2//:ros2"]
kwargs["deps"] += ["@bazel_ros2_rules//ros2:roslaunch_base.py"]
kwargs["srcs"] = ["@bazel_ros2_rules//ros2:roslaunch_base.py"]
kwargs["deps"] += ["@bazel_ros2_rules//ros2:roslaunch_util.py"]
kwargs["srcs"] = ["@bazel_ros2_rules//ros2:roslaunch_util.py"]

kwargs["data"] += [launch_file]
kwargs["data"] += node_targets
kwargs["args"] += [launch_file]

ros_py_binary(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import subprocess
import sys

print("ARGS: ", sys.argv[1:])
# print("ARGS: ", sys.argv[1:])
launch_file_name = sys.argv[1]
# TODO (Aditya) : dir should not be hardcoded here.
launch_file_dir = "ros2_example_apps"

roslaunch_cli = "./external/ros2/ros2"
Expand Down
29 changes: 26 additions & 3 deletions ros2_example_bazel_installed/ros2_example_apps/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -204,14 +204,37 @@ ros_py_binary(
# ROS launch example.
ros_py_binary(
name = "eg_talker",
srcs = ["eg_talker.py"],
srcs = ["roslaunch_eg_nodes/eg_talker.py"],
deps = [
"@ros2//:rclpy_py",
"@ros2//:std_msgs_py",
]
)

ros_cc_binary(
name = "eg_listener",
srcs = ["roslaunch_eg_nodes/eg_listener.cpp"],
deps = [
"@ros2//:rclcpp_cc",
"@ros2//:std_msgs_cc",
],
)

ros_launch(
name = "roslaunch_eg",
name = "roslaunch_eg_py",
launch_file = "eg_launch.py",
deps = [
node_targets = [
":eg_talker",
":eg_listener",
],
)

ros_launch(
name = "roslaunch_eg_xml",
launch_file = "eg_launch.xml",
node_targets = [
":eg_talker",
":eg_listener",
],
)

Expand Down
7 changes: 6 additions & 1 deletion ros2_example_bazel_installed/ros2_example_apps/eg_launch.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@

def generate_launch_description():
return LaunchDescription([
# Running a talker python node.
ExecuteProcess(
cmd=['python3','ros2_example_apps/eg_talker.py'],
cmd=['python3','ros2_example_apps/roslaunch_eg_nodes/eg_talker.py'],
),
# Running a listener python node.
ExecuteProcess(
cmd=['./ros2_example_apps/eg_listener'],
),
])
4 changes: 4 additions & 0 deletions ros2_example_bazel_installed/ros2_example_apps/eg_launch.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<launch>
<executable cmd="python3 ros2_example_apps/roslaunch_eg_nodes/eg_talker.py"/>
<executable cmd="./ros2_example_apps/eg_listener"/>
</launch>
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include <memory>

#include "rclcpp/rclcpp.hpp"
#include "std_msgs/msg/string.hpp"
using std::placeholders::_1;

class MinimalSubscriber : public rclcpp::Node
{
public:
MinimalSubscriber()
: Node("minimal_subscriber")
{
subscription_ = this->create_subscription<std_msgs::msg::String>(
"topic", 10, std::bind(&MinimalSubscriber::topic_callback, this, _1));
}

private:
void topic_callback(const std_msgs::msg::String::SharedPtr msg) const
{
RCLCPP_INFO(this->get_logger(), "I heard: '%s'", msg->data.c_str());
}
rclcpp::Subscription<std_msgs::msg::String>::SharedPtr subscription_;
};

int main(int argc, char * argv[])
{
rclcpp::init(argc, argv);
rclcpp::spin(std::make_shared<MinimalSubscriber>());
rclcpp::shutdown();
return 0;
}

0 comments on commit 7d2ced7

Please sign in to comment.