nodeArchitectureCPP - AD-EYE/AD-EYE_Core GitHub Wiki
This file describe the architecture that C++ ROS Node should to follow in the AD-EYE platform. These rules attend to help producing a readable code.
The main idea is to have a clean and readable code in order to be easy to maintain. Then every common sense rule of having a clean and organized code is welcome in addition to the following guidelines.
We usually use classes to implement a C++ Node. They should follow the following architecture:
Just after the class declaration we define all the attributes (organized as possible). They come first so we can easily know what we are working with.
Note: the private
word is recommended (for readability) but not necessary as class
keyword sets private
by default.
Then the first method that should appear is the constructor.
It initialize the member ros::NodeHandle
reference and all other attributes.
Then we initialize the publishers and the subscribers.
Note: Here the public
keyword is necessary.
Even if variables are not initialized in the right order, we should clearly distinguish categories at the first look.
Then, we can define every callbacks that will be called by topic subscriptions.
After callbacks, a running method needs to be implemented. The name of the method should follow the logic of the Node. This method contains main instructions of the Node. It is where the ros::spin()
call should be.
Note: Another possible way is to call ros::spin()
at the end of the main
function and to call this running method inside a callback.
Any other function that the Node use, for any purpose will be implemented after.
Last but not least, the main function is implemented. If every previous rules has been respected, it should looks like the following, just creating the Node and eventually calling the run method (If it hasn't been done inside the constructor).
int main(int argc, char **argv)
{
ros::init(argc, argv, "<NodeName>");
ros::NodeHandle nh;
<NodeName> <instanceName>(nh);
<NodeName>.run();
}
Note: Using a reference attribute to the ros::NodeHandle
in the Node is recommended as it avoid having multiple useless copies of the ros::NodeHandle
:
class <NodeName>
{
private:
[...]
ros::NodeHandle& _nh;
public:
<NodeName>(ros::NodeHandle& nh) : _nh(nh) {
[...]
}
[...]
};