-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP: Support for ros launch with a new rule
- Loading branch information
1 parent
4c17af0
commit 1ddf90e
Showing
10 changed files
with
220 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from .roslaunch_util import ExecuteBazelTarget | ||
|
||
__all__ = ['ExecuteBazelTarget'] |
52 changes: 52 additions & 0 deletions
52
bazel_ros2_rules/ros2/tools/roslaunch_util/roslaunch_util.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import subprocess | ||
import sys | ||
import os | ||
|
||
from launch.actions import ExecuteProcess | ||
from launch.frontend import expose_action | ||
|
||
# This util file serves as a wrapper over the cmdline | ||
# way to run launch, using "ros2 launch launch_file.py". | ||
# The ros_launch() bazel rule starts this script, which | ||
# gets run as a ros_py_binary(). This way we get the ros | ||
# environment set up for us for free. | ||
|
||
def find_rel_path(name, path): | ||
for root, _, files in os.walk(path): | ||
if name in files: | ||
return os.path.relpath(os.path.join(root, name)) | ||
|
||
# Launch action to wrap over ExecuteProcess. | ||
@expose_action('execute_bazel_target') | ||
class ExecuteBazelTarget(ExecuteProcess): | ||
def __init__(self,target,**kwargs): | ||
super().__init__(cmd=[find_rel_path(target, os.getcwd())], | ||
**kwargs) | ||
|
||
@classmethod | ||
def parse(cls, entity, parser): | ||
_, kwargs = super().parse(entity, parser, ignore=['cmd']) | ||
kwargs['target'] = entity.get_attr('target') | ||
return cls, kwargs | ||
|
||
def execute(self, context): | ||
return super().execute(context) | ||
|
||
if __name__ == '__main__': | ||
# TODO (Aditya): How do I stop installing this every time ? | ||
# Required to get xml launch working, as we need to register an entry | ||
# point for launch extensions. | ||
os.system("pip install ../bazel_ros2_rules/ros2/tools/roslaunch_util >/dev/null 2>&1") | ||
# Actual launch files should be able to import the custom action now. | ||
env = {**os.environ, 'PYTHONPATH': os.getcwd() + '/external/bazel_ros2_rules/ros2/tools:' | ||
+ os.environ['PYTHONPATH']} | ||
|
||
launch_file_name = sys.argv[1] | ||
|
||
roslaunch_cli = "./external/ros2/ros2" | ||
action = "launch" | ||
# TODO : Is there a better way to locate the launch file exactly ? | ||
launch_file = find_rel_path(launch_file_name, os.getcwd()) | ||
|
||
# This basically runs "ros2 launch launch_file" | ||
subprocess.run([roslaunch_cli, action, launch_file], env=env) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
from setuptools import setup | ||
|
||
setup( | ||
name = "roslaunch_util", | ||
entry_points={ | ||
'launch.frontend.launch_extension': [ | ||
'roslaunch_util = roslaunch_util', | ||
], | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
ros2_example_bazel_installed/ros2_example_apps/eg_launch.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
from launch import LaunchDescription | ||
from roslaunch_util import ExecuteBazelTarget | ||
|
||
def generate_launch_description(): | ||
return LaunchDescription([ | ||
# Running a talker written in python. | ||
ExecuteBazelTarget('eg_talker'), | ||
# Running a listener written in cpp. | ||
ExecuteBazelTarget('eg_listener'), | ||
]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<launch> | ||
<execute_bazel_target target="eg_talker"/> | ||
<execute_bazel_target target="eg_listener"/> | ||
</launch> |
31 changes: 31 additions & 0 deletions
31
ros2_example_bazel_installed/ros2_example_apps/roslaunch_eg_nodes/eg_listener.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
39 changes: 39 additions & 0 deletions
39
ros2_example_bazel_installed/ros2_example_apps/roslaunch_eg_nodes/eg_talker.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import rclpy | ||
from rclpy.node import Node | ||
|
||
from std_msgs.msg import String | ||
|
||
|
||
class MinimalPublisher(Node): | ||
|
||
def __init__(self): | ||
super().__init__('minimal_publisher') | ||
self.publisher_ = self.create_publisher(String, 'topic', 10) | ||
timer_period = 0.5 # seconds | ||
self.timer = self.create_timer(timer_period, self.timer_callback) | ||
self.i = 0 | ||
|
||
def timer_callback(self): | ||
msg = String() | ||
msg.data = 'Hello World: %d' % self.i | ||
self.publisher_.publish(msg) | ||
self.get_logger().info('Publishing: "%s"' % msg.data) | ||
self.i += 1 | ||
|
||
|
||
def main(args=None): | ||
rclpy.init(args=args) | ||
|
||
minimal_publisher = MinimalPublisher() | ||
|
||
rclpy.spin(minimal_publisher) | ||
|
||
# Destroy the node explicitly | ||
# (optional - otherwise it will be done automatically | ||
# when the garbage collector destroys the node object) | ||
minimal_publisher.destroy_node() | ||
rclpy.shutdown() | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |