Skip to content
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

Closed
wants to merge 3 commits into from

Conversation

bgill92
Copy link
Collaborator

@bgill92 bgill92 commented Sep 3, 2023

Description

Updated the Naive implementation with Dependency Injection.

Testing

Launch the docker container and run:

colcon build --packages-select naive
colcon test --packages-select naive

Make sure he package builds and all the tests pass.

Copy link
Collaborator

@griswaldbrooks griswaldbrooks left a 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?

Copy link
Collaborator

@griswaldbrooks griswaldbrooks left a 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;
Copy link
Collaborator

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_;
Copy link
Collaborator

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.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* 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_;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer

Suggested change
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;
Copy link
Collaborator

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")};
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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
Copy link
Collaborator

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);
Copy link
Collaborator

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));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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

Suggested change
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); });

@bgill92 bgill92 closed this Sep 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants