Skip to content

ROS1 templates

Francesco Ganci edited this page Jun 25, 2022 · 22 revisions

ROS1 Templates

If you have to quickly set up a node, and you don't want to waste your time in writing "init_node" then "spin" then "main" then ... here are some ready-to-use snippets.

A bit of philosophy

  • a ROS node just implements a architectural element or an interface
  • the functionalities should be implemented in a separated library, and not in the node itself unless the node hasn't a very peculiar role in the project
  • in each package, there should be at least two levels of implemetetion: the one of the nodes (the uppermost part of the package, implementint topics, services and actions), and the one of the libraries ("low level" implementation of the functionalities beneath the interfaces)

ROSCPP node blueprint

This pattern also contains some macros to speed up the logging on the console. I think that logging is a very significant part of the work, extremely helpful for the software debugging for instance.

#include "ros/ros.h"

#define NODE_NAME "NODE_NAME"

#define LOGSQUARE( str )  "[" << str << "] "
#define OUTLABEL          LOGSQUARE( NODE_NAME )
#define TLOG( msg )       ROS_INFO_STREAM( OUTLABEL << msg )
#define TWARN( msg )      ROS_WARN_STREAM( "WARNING: " << msg )
#define TERR( msg )       ROS_WARN_STREAM( "ERROR: " << msg )



int main( int argc, char* argv[] )
{
	ros::init( argc, argv, NODE_NAME );
	ros::NodeHandle nh;
	
	TLOG( "starting ... " );
	
	// TODO define here services and topics ...
	
	TLOG( "ready" );
	
	// TODO the functionality of the node ... 
	
	TLOG( "stopping ..." );
	
	return 0;
}

ROSCPP class-node blueprint

ROSCPP Simple publisher

ROSCPP Simple subscriber

ROSCPP Simple service

ROSCPP Simple client

ROSPY node blueprint

ROSPY class-node blueprint

ROSPY Simple publisher

ROSPY Simple subscriber

ROSPY Simple service

ROSPY Simple client

Clone this wiki locally