Skip to content

Commit

Permalink
Use the rclpy APIs directly to publish a single message
Browse files Browse the repository at this point in the history
Signed-off-by: Pintaudi Giorgio <[email protected]>
  • Loading branch information
LastStarDust committed Aug 1, 2024
1 parent ac0fe1f commit 1621f01
Showing 1 changed file with 12 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
# limitations under the License.

import os
from subprocess import PIPE, Popen
import sys
import unittest

Expand All @@ -23,6 +22,7 @@
import launch_testing.actions
import launch_testing.markers
from launch_testing_ros import WaitForTopics
import rclpy
import pytest
from std_msgs.msg import String

Expand All @@ -39,13 +39,17 @@ def generate_node():


def trigger_callback():
command = 'ros2 topic pub --once --max-wait-time-secs 10 --keep-alive 1 \
/input std_msgs/msg/String "data: Hello World"'
p = Popen(command.split(), stdout=PIPE, stderr=PIPE)
stdout, stderr = p.communicate()
print("stdout: ", stdout)
print("stderr: ", stderr)
print('Callback triggered')
rclpy.init()
node = rclpy.create_node('trigger')
publisher = node.create_publisher(String, 'input', 10)
while publisher.get_subscription_count() == 0:
rclpy.spin_once(node, timeout_sec=0.1)
msg = String()
msg.data = 'Hello World'
publisher.publish(msg)
print('Published message')
node.destroy_node()
rclpy.shutdown()


@pytest.mark.launch_test
Expand Down

0 comments on commit 1621f01

Please sign in to comment.