-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Updated Naive implementation with Dependency Injection #19
Conversation
d5070d1
to
bee2351
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So, I'd like the naive example to stay... naive. Like close to what you would see in a ros tutorial. I have wondered if it should be renamed to something other than naive. Can we make this it's own example?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, I didn't see the superclass. I'd still like to keep the names. One could be naive or a nicer word and the other, dependency_injection or something like folders with pubsub and services under a folder describing the technique
*/ | ||
struct MiddlewareHandle { | ||
using Callback = std::function<void(const std_msgs::msg::Int64::SharedPtr)>; | ||
virtual void registerCallback(Callback cb) = 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we get doxygen comments on these methods?
std::shared_ptr<rclcpp::Node> node_; | ||
std::shared_ptr<rclcpp::Publisher<std_msgs::msg::Int64>> pub_; | ||
std::shared_ptr<rclcpp::Subscription<std_msgs::msg::Int64>> sub_; | ||
std::unique_ptr<MiddlewareHandle> mw_; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's keep this as a unique_ptr
but we should either have a comment or example mentioning that raw pointers and references will work here too. Holding a reference feels odd to me but other DI examples do it.
/********************************************************************* | ||
* Software License Agreement (BSD License) | ||
* | ||
* Copyright (c) 2022, PickNik, LLC. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* Copyright (c) 2022, PickNik, LLC. | |
* Copyright (c) 2023, PickNik, LLC. |
static constexpr int QUEUE_SIZE = 10; | ||
rclcpp::Node::SharedPtr node_handle_; | ||
std::string in_topic_; | ||
rclcpp::Subscription<std_msgs::msg::Int64>::SharedPtr subscriber_; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would prefer
rclcpp::Subscription<std_msgs::msg::Int64>::SharedPtr subscriber_; | |
std::shared_ptr<rclcpp::Subscription<std_msgs::msg::Int64>> subscriber_; |
but if you think it's more instructive to use the ros types, i'm ok with that.
public: | ||
RosMiddleware(const rclcpp::Node::SharedPtr node_handle, std::string in_topic, | ||
const std::string& out_topic); | ||
void registerCallback(Callback cb) override; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
doxygen
int main(int argc, char **argv) { | ||
rclcpp::init(argc, argv); | ||
auto const node = std::make_shared<rclcpp::Node>("incrementer"); | ||
Incrementer incrementer{node, "/in", "/out"}; | ||
Incrementer incrementer{std::make_unique<RosMiddleware>(node, "/in", "/out")}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Incrementer incrementer{std::make_unique<RosMiddleware>(node, "/in", "/out")}; | |
Incrementer const incrementer{std::make_unique<RosMiddleware>(node, "/in", "/out")}; |
/** | ||
* @brief Incrementer constructor | ||
* | ||
* @param[in] mw Unique pointer to a MiddlewareHandle object |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
update
Incrementer::Incrementer(const std::string node_name, const std::string in_topic, const std::string out_topic) | ||
: Node(node_name) | ||
{ | ||
publisher_ = this->create_publisher<std_msgs::msg::Int64>(out_topic, QUEUE_SIZE); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, well, we probably can still use the initializer list instead of assignment
: Node(node_name) | ||
{ | ||
publisher_ = this->create_publisher<std_msgs::msg::Int64>(out_topic, QUEUE_SIZE); | ||
subscriber_ = this->create_subscription<std_msgs::msg::Int64>(in_topic, QUEUE_SIZE, std::bind(&Incrementer::callback, this, std::placeholders::_1)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
subscriber_ = this->create_subscription<std_msgs::msg::Int64>(in_topic, QUEUE_SIZE, std::bind(&Incrementer::callback, this, std::placeholders::_1)); | |
subscriber_ = this->create_subscription<std_msgs::msg::Int64>(in_topic, QUEUE_SIZE, [this](const std_msgs::msg::Int64::SharedPtr msg){ | |
std_msgs::msg::Int64 incremented; | |
incremented.data = msg->data + 1; | |
publisher_->publish(incremented); | |
}); |
https://abseil.io/tips/108 (you might even want to add this as a comment)
or
subscriber_ = this->create_subscription<std_msgs::msg::Int64>(in_topic, QUEUE_SIZE, std::bind(&Incrementer::callback, this, std::placeholders::_1)); | |
subscriber_ = this->create_subscription<std_msgs::msg::Int64>(in_topic, QUEUE_SIZE, [this](const std_msgs::msg::Int64::SharedPtr msg){ callback(msg); }); |
Description
Updated the Naive implementation with Dependency Injection.
Testing
Launch the docker container and run:
Make sure he package builds and all the tests pass.