Skip to content

Commit

Permalink
fix: bad_any_cast issue for custom publisher configuration callable
Browse files Browse the repository at this point in the history
  • Loading branch information
bpapaspyros committed Oct 3, 2024
1 parent 13378d4 commit e53f6cb
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -584,12 +584,14 @@ inline void BaseControllerInterface::add_output(const std::string& name, const s
outputs_.insert_or_assign(name, PublisherT());
custom_output_configuration_callables_.insert_or_assign(
name, [this](CustomPublishers& pub, const std::string& name) {
auto publishers = std::any_cast<PublisherT>(pub);
publishers.first = get_node()->create_publisher<T>(name, qos_);
publishers.second = std::make_shared<realtime_tools::RealtimePublisher<T>>(publishers.first);
std::shared_ptr<rclcpp::Publisher<T>> publisher =
std::any_cast<std::shared_ptr<rclcpp::Publisher<T>>>(pub.first);
publisher = get_node()->create_publisher<T>(name, qos_);
realtime_tools::RealtimePublisherSharedPtr<T> realtime_publisher =
std::any_cast<realtime_tools::RealtimePublisherSharedPtr<T>>(pub.second);
realtime_publisher = std::make_shared<realtime_tools::RealtimePublisher<T>>(publisher);
});
}

} else {
std::shared_ptr<state_representation::State> state_ptr = std::make_shared<T>();
create_output(EncodedStatePublishers(state_ptr, {}, {}), name, topic_name);
Expand Down
4 changes: 3 additions & 1 deletion source/modulo_controllers/src/BaseControllerInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -408,8 +408,10 @@ void BaseControllerInterface::add_outputs() {
custom_output_configuration_callables_.at(name)(pub, name);
}},
publishers);
} catch (const std::bad_any_cast& ex) {
RCLCPP_ERROR(get_node()->get_logger(), "Failed to add custom output '%s': %s", name.c_str(), ex.what());
} catch (const std::exception& ex) {
RCLCPP_ERROR(get_node()->get_logger(), "Failed to add input '%s': %s", name.c_str(), ex.what());
RCLCPP_ERROR(get_node()->get_logger(), "Failed to add output '%s': %s", name.c_str(), ex.what());
}
}
}
Expand Down
20 changes: 20 additions & 0 deletions source/modulo_controllers/test/test_controller_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,28 @@ TYPED_TEST_P(ControllerInterfaceTest, OutputTest) {
TYPED_TEST_P(ControllerInterfaceTest, CustomOutputTest) {
auto interface = std::make_unique<FriendControllerInterface>();
interface->init("controller_interface", "", 0, "", interface->define_custom_node_options());
interface->get_node()->set_parameter({"hardware_name", "test"});
interface->get_node()->set_parameter({"input_validity_period", 0.1});

interface->add_output<sensor_msgs::msg::Image>("output", "/output");
auto node_state = interface->get_node()->configure();
ASSERT_EQ(node_state.id(), lifecycle_msgs::msg::State::PRIMARY_STATE_INACTIVE);

// rclcpp::Node test_node("test_node");
// auto publisher = test_node.create_publisher<MsgT>("/input", rclcpp::SystemDefaultsQoS());

// for (auto [message_data, write_func, read_func, validation_func] : this->test_cases_) {
// message_data = write_func(message_data);
// auto message = std::get<1>(message_data);
// publisher->publish(message);
// rclcpp::spin_some(this->interface_->get_node()->get_node_base_interface());
// auto input = this->interface_->template read_input<DataT>("input");
// ASSERT_TRUE(input);
// EXPECT_TRUE(validation_func(message_data, std::make_tuple(*input, message)));
// std::this_thread::sleep_for(100ms);
// ASSERT_FALSE(this->interface_->template read_input<DataT>("input"));
// }

EXPECT_TRUE(true);
}

Expand Down
5 changes: 1 addition & 4 deletions source/modulo_core/include/modulo_core/concepts.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@ concept TranslatedMsgT = std::same_as<T, std_msgs::msg::Bool> || std::same_as<T,
|| std::same_as<T, std_msgs::msg::String> || std::same_as<T, modulo_core::EncodedState>;

template<typename T>
concept EncodedT = std::same_as<T, modulo_core::EncodedState>;

template<typename T>
concept CustomT = !CoreDataT<T> && !EncodedT<T>;
concept CustomT = !CoreDataT<T> && !std::same_as<T, modulo_core::EncodedState>;

}// namespace modulo_core::concepts

0 comments on commit e53f6cb

Please sign in to comment.