-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for ros_launch() rules (#298)
Co-authored-by: Eric Cousineau <[email protected]>
- Loading branch information
1 parent
b392587
commit 955245e
Showing
10 changed files
with
355 additions
and
3 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
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
18 changes: 18 additions & 0 deletions
18
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,18 @@ | ||
from bazel_ros_env import Rlocation | ||
from launch import LaunchDescription | ||
from launch.actions import ExecuteProcess | ||
|
||
|
||
def generate_launch_description(): | ||
# See bazel_ros2_rules/ros2/README.md, Launch Files, for notes on | ||
# features and limitations. | ||
prefix = "ros2_example_bazel_installed/ros2_example_apps" | ||
talker_bin = Rlocation(f"{prefix}/eg_talker") | ||
listener_bin = Rlocation(f"{prefix}/eg_listener") | ||
|
||
return LaunchDescription([ | ||
# Running a talker written in python. | ||
ExecuteProcess(cmd=[talker_bin]), | ||
# Running a listener written in cpp. | ||
ExecuteProcess(cmd=[listener_bin]), | ||
]) |
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,8 @@ | ||
<launch> | ||
<!-- | ||
See bazel_ros2_rules/ros2/README.md, Launch Files, for notes on | ||
features and limitations. | ||
--> | ||
<executable cmd="ros2_example_apps/eg_talker" output="screen"/> | ||
<executable cmd="ros2_example_apps/eg_listener" output="screen"/> | ||
</launch> |
48 changes: 48 additions & 0 deletions
48
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,48 @@ | ||
#include <chrono> | ||
#include <memory> | ||
|
||
#include "rclcpp/rclcpp.hpp" | ||
#include "std_msgs/msg/string.hpp" | ||
using std::placeholders::_1; | ||
|
||
class MinimalSubscriber : public rclcpp::Node | ||
{ | ||
public: | ||
MinimalSubscriber(int max_count = 10) | ||
: Node("minimal_subscriber"), max_count_(max_count) | ||
{ | ||
subscription_ = this->create_subscription<std_msgs::msg::String>( | ||
"topic", 10, std::bind(&MinimalSubscriber::topic_callback, this, _1)); | ||
} | ||
|
||
bool is_done() const { return count_ >= max_count_; } | ||
|
||
private: | ||
void topic_callback(const std_msgs::msg::String::SharedPtr msg) | ||
{ | ||
RCLCPP_INFO(this->get_logger(), "I heard: '%s'", msg->data.c_str()); | ||
++count_; | ||
} | ||
rclcpp::Subscription<std_msgs::msg::String>::SharedPtr subscription_; | ||
|
||
int max_count_{}; | ||
int count_{}; | ||
}; | ||
|
||
int main(int argc, char * argv[]) | ||
{ | ||
rclcpp::init(argc, argv); | ||
|
||
// Use explicit `spin_some` so we can manually check for completion. | ||
auto node = std::make_shared<MinimalSubscriber>(); | ||
rclcpp::executors::SingleThreadedExecutor executor; | ||
executor.add_node(node); | ||
// Use large enough non-zero max duration to ensure we do not block. | ||
const std::chrono::microseconds spin_max_duration{50}; | ||
|
||
while (rclcpp::ok() && !node->is_done()) { | ||
executor.spin_some(spin_max_duration); | ||
} | ||
|
||
return 0; | ||
} |
42 changes: 42 additions & 0 deletions
42
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,42 @@ | ||
import rclpy | ||
from rclpy.executors import SingleThreadedExecutor | ||
from rclpy.node import Node | ||
|
||
from std_msgs.msg import String | ||
|
||
|
||
class MinimalPublisher(Node): | ||
|
||
def __init__(self, *, max_count = 10): | ||
super().__init__("node") | ||
self.publisher = self.create_publisher(String, "topic", 10) | ||
timer_period = 0.1 # seconds | ||
self.timer = self.create_timer(timer_period, self.timer_callback) | ||
self.count = 0 | ||
self.max_count = max_count | ||
|
||
def timer_callback(self): | ||
msg = String() | ||
msg.data = f"Hello World: {self.count}" | ||
self.publisher.publish(msg) | ||
self.get_logger().info(f"Publishing: '{msg.data}'") | ||
self.count += 1 | ||
|
||
def is_done(self): | ||
return self.count >= self.max_count | ||
|
||
|
||
def main(): | ||
rclpy.init() | ||
|
||
node = MinimalPublisher() | ||
|
||
# Use explicit `spin_once` so we can manually check for completion. | ||
executor = SingleThreadedExecutor() | ||
executor.add_node(node) | ||
while rclpy.ok() and not node.is_done(): | ||
executor.spin_once(timeout_sec=1e-3) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
Oops, something went wrong.