48
48
49
49
#include < limits>
50
50
#include < memory>
51
+ #include < optional>
51
52
#include < string>
52
53
#include < unordered_map>
53
54
#include < vector>
@@ -402,6 +403,7 @@ void createPublisherWithQoS(
402
403
rclcpp::Node::SharedPtr test_node, std::string topic_name,
403
404
std::shared_ptr<rclcpp::Publisher<T>> & publisher)
404
405
{
406
+ // override QoS settings for specific message types
405
407
if constexpr (
406
408
std::is_same_v<T, LaneletRoute> || std::is_same_v<T, LaneletMapBin> ||
407
409
std::is_same_v<T, OperationModeState>) {
@@ -443,18 +445,23 @@ void setPublisher(
443
445
* @param topic_name The name of the topic to subscribe to.
444
446
* @param callback The callback function to call when a message is received.
445
447
* @param subscriber A reference to the subscription to be created.
448
+ * @param qos The QoS settings for the subscription (optional).
446
449
*/
447
450
template <typename T>
448
451
void createSubscription (
449
- rclcpp::Node::SharedPtr test_node, std::string topic_name,
452
+ rclcpp::Node::SharedPtr test_node, const std::string & topic_name,
450
453
std::function<void (const typename T::ConstSharedPtr)> callback,
451
- std::shared_ptr<rclcpp::Subscription<T>> & subscriber)
454
+ std::shared_ptr<rclcpp::Subscription<T>> & subscriber,
455
+ std::optional<rclcpp::QoS> qos = std::nullopt)
452
456
{
453
- if constexpr (std::is_same_v<T, Trajectory>) {
454
- subscriber = test_node->create_subscription <T>(topic_name, rclcpp::QoS{1 }, callback);
455
- } else {
456
- subscriber = test_node->create_subscription <T>(topic_name, 10 , callback);
457
+ if (!qos.has_value ()) {
458
+ if constexpr (std::is_same_v<T, Trajectory>) {
459
+ qos = rclcpp::QoS{1 };
460
+ } else {
461
+ qos = rclcpp::QoS{10 };
462
+ }
457
463
}
464
+ subscriber = test_node->create_subscription <T>(topic_name, *qos, callback);
458
465
}
459
466
460
467
/* *
@@ -467,14 +474,17 @@ void createSubscription(
467
474
* @param topic_name The name of the topic to subscribe to.
468
475
* @param subscriber A reference to the subscription to be set.
469
476
* @param count A reference to a counter that increments on message receipt.
477
+ * @param qos The QoS settings for the subscription (optional).
470
478
*/
471
479
template <typename T>
472
480
void setSubscriber (
473
481
rclcpp::Node::SharedPtr test_node, std::string topic_name,
474
- std::shared_ptr<rclcpp::Subscription<T>> & subscriber, size_t & count)
482
+ std::shared_ptr<rclcpp::Subscription<T>> & subscriber, size_t & count,
483
+ std::optional<rclcpp::QoS> qos = std::nullopt)
475
484
{
476
485
createSubscription (
477
- test_node, topic_name, [&count](const typename T::ConstSharedPtr) { count++; }, subscriber);
486
+ test_node, topic_name, [&count](const typename T::ConstSharedPtr) { count++; }, subscriber,
487
+ qos);
478
488
}
479
489
480
490
/* *
@@ -534,28 +544,40 @@ class AutowareTestManager
534
544
template <typename MessageType>
535
545
void test_pub_msg (
536
546
rclcpp::Node::SharedPtr target_node, const std::string & topic_name, MessageType & msg)
547
+ {
548
+ rclcpp::QoS qos (rclcpp::KeepLast (10 ));
549
+ test_pub_msg (target_node, topic_name, msg, qos);
550
+ }
551
+
552
+ template <typename MessageType>
553
+ void test_pub_msg (
554
+ rclcpp::Node::SharedPtr target_node, const std::string & topic_name, MessageType & msg,
555
+ rclcpp::QoS qos)
537
556
{
538
557
if (publishers_.find (topic_name) == publishers_.end ()) {
539
- auto publisher = test_node_->create_publisher <MessageType>(topic_name, 10 );
558
+ auto publisher = test_node_->create_publisher <MessageType>(topic_name, qos );
540
559
publishers_[topic_name] = std::static_pointer_cast<rclcpp::PublisherBase>(publisher);
541
560
}
542
561
543
562
auto publisher =
544
563
std::dynamic_pointer_cast<rclcpp::Publisher<MessageType>>(publishers_[topic_name]);
545
564
546
- autoware::test_utils::publishToTargetNode (test_node_, target_node, topic_name, publisher, msg);
565
+ publisher->publish (msg);
566
+ const int repeat_count = 3 ;
567
+ autoware::test_utils::spinSomeNodes (test_node_, target_node, repeat_count);
547
568
RCLCPP_INFO (test_node_->get_logger (), " Published message on topic '%s'" , topic_name.c_str ());
548
569
}
549
570
550
571
template <typename MessageType>
551
572
void set_subscriber (
552
573
const std::string & topic_name,
553
- std::function<void (const typename MessageType::ConstSharedPtr)> callback)
574
+ std::function<void (const typename MessageType::ConstSharedPtr)> callback,
575
+ std::optional<rclcpp::QoS> qos = std::nullopt)
554
576
{
555
577
if (subscribers_.find (topic_name) == subscribers_.end ()) {
556
578
std::shared_ptr<rclcpp::Subscription<MessageType>> subscriber;
557
579
autoware::test_utils::createSubscription<MessageType>(
558
- test_node_, topic_name, callback, subscriber);
580
+ test_node_, topic_name, callback, subscriber, qos );
559
581
subscribers_[topic_name] = std::static_pointer_cast<rclcpp::SubscriptionBase>(subscriber);
560
582
} else {
561
583
RCLCPP_WARN (test_node_->get_logger (), " Subscriber %s already set." , topic_name.c_str ());
0 commit comments