This tutorial teaches how to create custom interfaces (messages and services) in ROS2 by first checking existing interfaces on GitHub, then creating a dedicated package for interfaces with proper configuration in package.xml and CMakeLists.txt, followed by defining message types using camelCase naming convention and primitive data types, and finally building and importing these interfaces into Python and C++ nodes.
Creating Custom ROS2 Interfaces: Messages and Services
Added:in this tutorial you will learn how to create your own roster custom interface with first a message I will also cover how to create custom services at the configuration and process are the same and first of all before you create any interface by yourself well it's quite worth it to check what interfaces are already available and for that you can just go on GitHub and search for us to Common interfaces you will get to this repository and here you have a lot of already existing interfaces that you can use in your own programs so for example you go to Geometry messages and here you have so messages and you have a lot of messages that you can see let's say you will need a vector 3 to represent so a vector in free space with X Y and Z coordinate while you already have a message for that so instead of creating it you just use that one and so maybe just by browsing those common interfaces you will find out that you actually don't need to create messages and so you have packages like standard messages standard services and also here you have the example interfaces so in exam simple interfaces you can see bull Char float int Etc but those are just for exampleing for testing purposes so if you find exactly what you need that's fine if you don't find what you need don't use example interfaces and packages like standard messages and standard services to build your application okay either you use real I would say real interfaces from other packages here but not from the standard messages not from the example interfaces as you can see here you even have a notice okay this is an example message okay if you want to test that's fine but if you are deploying you should create a semantically meaningful message type and that's exactly what we are going to do right now so let's close that and let's open the terminal and let's say that now you have checked the existing interfaces you haven't found what you need so you need to create your own let's start and let's go to our Ros 2 workspace so make sure you have a workspace and in this workspace you have a source folder and so where to create interfaces so technically you could create interfaces in any package you want so here I already have so I have just two packages a C plus plus package and a python package that I have named very basically for this tutorial I could create interfaces in those packages but this is not a good practice why is that because if I create an interface in this package and I also need to use that interface from that package then I will need to add a dependency from this to that package and if this package was already depending on that package then we have the cycling dependency and that's really not good and this is only with two packages but now imagine you have 10 or 20 packages if you start to create interfaces inside the packages which contain nodes that's gonna be a mess and so the best practice here is to create a package dedicated to rush to custom interfaces and only for that so let's create a new package ros2 PKG create and how do we name that package well to start the package what you can do is if you have a application name or a robot name you just use this name so let's say that I have a robot which name is my robot I just put my robot with an underscore here and then underscore and you can finish the name with interfaces okay that's a quite used convention so you start with application or robot name and then interfaces no need to put any dependency or any build type for now let's press enter this is going to create here you can see a new package and let's go inside that package from my robot interfaces so when you don't provide a build type this is going to be aim and CMH by default so basically your C plus plus package which is fine we need any way to see maclist.txt and we need the package.xml what we don't need is the include and Source folder because we're not going to write code in this package so let's just remove those folders include in source okay and now we are going to create new folders so we are going to create a folder named MSG if we want to create messages and we can also create a folder SRV which I'm going to keep for later for services so for now we have MSG here great and now what to do well I'm going to open this with Visual Studio code so from The Source repository of the workspace let's go in my robot interfaces and what we need to do is to configure the package for custom interfaces so that's a configuration that we will need to do just once and then it will be very easy to add custom interfaces so just follow along here and make sure that you write exactly what I write so let's go to package.xml you can see here anyway we have build tool depend payment C makes so we can confirm that this is an aim and C make package now after this you are going to need to add three new lines so let's add real tool depends so another build will depend tag with Ross IDL underscore default underscore generators and real tool depend so no need to really understand anything here that's just the tag that you need to add okay so Ros IDL default generators and then you have an exact depend with Ros ideal default runtime exec Depend and well Ros ideal is basically the rust to functionality that's going to be responsible to build your custom interfaces and then we have another one member of group like this Ross ideal underscore interface underscore packages with member a group great so make sure you have exactly those three lines with no syntax error okay and that's it for the package.xml let's close this and let's open cmclist.txt I'm just gonna remove this I'm not gonna use the build and test stink stuff and just remove that also so what we are going to do here is after find package with aim and cmage we are going to add find package with cross ideal default generators we've required okay so that's the same that we have put in the package.xml so just that one and then we will add a comment Ros IDL generate interfaces like that wave so we put project name with dollar sign curly bracket project name and project name is simply well the name of the package and then let's go back to a new line here and that is where here we will write the name of our custom interfaces and then after this line you can also do and that's going to be the last thing to add payment export dependencies with Ros ideal default runtime all right so if it doesn't make much sense well that's okay just make sure that you have those lines okay in the package.xml and see make list.txt and that's how you configure a package to build interfaces and now well also if you want to build this package if you have this command here with nothing inside that's gonna yield an arrow so if you want to build this package right now make sure that you comment this comments so the Ros ideal generate interfaces okay because if it's empty that's gonna bring an error all right another package is configured so we can start to create our custom interface and let's create a message first so an MSG and to create a message we are going to go inside our MSG folder here and I'm going to right click new file so I could do it in terminal but also here and well that's important we will need to use camel so camel case conver function here okay make sure you don't use underscores or any dashes or anything else and use camel case that's quite important and also don't use MSG or message in the name of the message okay we already know it's a message that would be redundant so let's say we want so we have the robot and we want our robot to reach a certain point which is defined by a name and some coordinates to reach so let's name this target coordinates so Target coordinates with camel case convention and then dot MSG that's going to be the extension for the file all right and now we need to write the message definition and so what kind of data type we can use here because what we're going to put inside our message will be kind of viable okay with a data type and then a name and here actually so if you go to the Ros 2 documentation you can find the primitive so the built-in currently supported types okay you can see all the types here so bull for Boolean and then byte Etc float 32 float64 you have also INT in 64 for like normal integer string and you can see also how it's gonna be used with C plus plus and python so a string for example here in C plus plus is going to be an STD string and with python it's going to be well a built-in string for python so you can use any combination of those and you can also use existing interfaces all right so you can put anything you want in your message so let's go to the terminal and actually let's do for us to interface show and I'm going to use an existing interface from so geometry message in the message folder point so that's already a interface that exists and that has been installed with ros2 this is already built and so I can already use it so you can see this contains the position of a point in three space with a float X float Y and Float Z and so that's perfect for my target coordinate message which so I want a name of a point and then I want a point in space and so for the name I'm going to use the data type string which is a primitive data type and so let's name it simply name okay and then I have geometry msgs slash point and so here we'll notice that if you use an existing message here we use geometry messages message points actually what you're gonna do is just use geometry messages so the name of the package slash directly the name of the interface with no for example no MSG like that so geometry message is point and this is going to be named coordinate all right so let's save this so this custom message will contain a name and coordinates the name of type string which is primitive data type and the coordinates of type point from the geometry messages package and now well you have written your first custom interface and well I'm gonna add a sponsor to this video and well actually there is no sponsor the sponsor is just me and my courses so if you like what I do if you like the way I teach and if you want to learn Russ too efficiently and with a complete structured course for beginner well I have the course just for you you can check out the link in the description and now let's continue with the video so we have our message now what do we need to do we need to build that message so how to build that message well first of all we need to do something because we are using a message from another package so the type string is okay we don't need to include any dependency but because we are using this package so we are using the geometry messages package inside my robot interfaces package then we need to add the dependency to that one so let's go to package.xml and let's add so for example here it depends tag to Geometry MS GS okay let's save it and in cmakelist.txt let's also add here after a month cmake find package geometry msgs with required and let's save this okay so make sure that you do that for every package that you use inside this package and now how to build this new message this new interface well everything is already configured in package.xml and everything is already configured in cmclist.txt all we need to do now is inside this Ros ideal generate interfaces we will need to so let's put some double quotes we will need to Simply put the path to this message and so the path from the package is MSG slash Target coordinate dot MSG so you just write MSG for this folder here slash the name of the file that's it and then if you have other custom interfaces you just add one the line make sure you don't add so don't add a comma here that's very important you can add one per line so for now let's keep picked just with one and then while that's quite also important otherwise you may have an error during compilation let's add dependencies so dependencies here with the dependencies that we need from those messages so because in this target coordinates we use geometry messages let's add dependencies geometry messages all right let's save this and now the configuration should be complete so let's go in the terminal and in our rust workspace to go back to your rust workspace and you can do call Con build and let's just do Dash Dash with packages select to just build my robot interfaces so it's going to take a few seconds and now it's successful so if you want to find your messages make sure that your source of course the rust to workspace so either with the bash SC directly from the install setup.bash and then you can do rush to interface show and you will be able to find it with my robot interfaces you can see with auto completion I have this my robot interfaces slash MSG slash Target coordinates let's press enter and you can see we have the message with string name and then point coordinates with X Y and Z great so that's successfully worked and you have a custom message now how to use this mid sage in your code okay how to use that message in C plus plus code and how to use that message in Python code so the first thing you need to know is that when you actually build the message the build system is going to create python files and also C plus plus header files so that you can actually include this message into your code okay so there is no magic here the build system is going to handle everything so then you can just import a c plus header or import a python module so let's do that in this workspace so I have my robot interfaces let's close that and I also have a python package and in this python package I already have a node simply a very basic note that I have named python test okay which initializes a node with a class and just prints hello with a callback so that's simply a node that I've taken from another tutorial but this note does nothing special now the thing is how to be able to use that custom interface that I've built inside this node well first I will go to the package.xml of the package where I want to use the interface and I will add here so I have depend SEL by to use the python Library I will add depend my robot interfaces okay so I added dependency to the interfaces package from the package where I want to use the interface which seems quite logic and then I will save this and I will go to the node where I want to use actually the file where I want to use the message and I simply need to do this I need to do from my robot interfaces and you can see I have the auto completion here on Visual Studio code from my robot interfaces actually dot MSG okay that's important that you use the dot MLG because that's from the MSG folder import Target coordinate and that's it that's how I include the custom message and now I can use it and just a quick note here why do I have a two completion here already that's because I am using here on vs code so that's specific to this setup but I'm using on vs code the Ros here the Ros extension which works for rust one and also ros2 so I would clearly recommend that if you are using this code you use this Ros extension which is going to give you Auto completion not only for rust 2 functionalities but also for your custom interfaces all right so now to use it well that's not the topic of this tutorial the topic of this tutorial is to create custom interfaces so I'm gonna leave it like this but at least you know how to include a custom interface in a python file and now let's see with C plus plus so let's go to our C plus plus package we will go to the package.xml and also after the depend RCL CPP we are going to add depend um my robot interfaces and let's close the depend tag let's save this and in cmake list.txt also I'm going to add here find package with my robot interfaces and required and in the payment Target dependencies of this executable that I've built for now I just have LCL CPP I'm also going to add my robot interfaces okay so make sure to add the dependency here as well and this is actually well this is a node that I've written here my first node.cpp so again that's a standard node that's just doing something very basic and the question is how to include the interface inside that node so make sure you have the correct dependency in the package.xml and also that you add the dependency here when you build the executable so let's go back here and well now we just need to do um include with quotes and my robot interfaces I also have the auto completion slash MSG slash Target coordinates dot HPP and you can see that the message here so this was written with camel case here with an apple case T and uppercase C here everything is lowercase and anytime you have a separation between two worlds you have also a underscore and so great now you can use the target coordinate message also in your C plus code and that's it for custom messages let's see actually how to create build and use a custom roster service and now that we've done all that that's gonna be super easy so let's close everything okay and let's actually go back to the terminal let's go to our source and to the my robot interfaces and well we have an MSG folder I am going to create an SRV folder where we are going to put Services let's go back to visual studio code and well the configuration is the same so all the configuration you've got here in the cemetlist.txt and in the package.xml it's going to be the same for messages and services so no need to do anything else then let's create a new service so we're going to use the same conversion for the name so let's create a new file here in the SRV folder but this time using the dot SRV extension not the dot MSG also a Service reflects an action or computation you want to do okay so maybe try to use a verb in the name for example set something trigger something activate compute something Etc and so let's say we want to set a color for an LED so very simplified example we can put for example set LED dot SRV so I use the word so the verb set and then LED I also use the camel case convention I don't put SRV in the name okay that will be redundant and I do that SRE for the extension press enter and for a custom service you will have actually two things you will have the requests and the response so let's keep things super simple here let's just use primitive data type let's use string color and then that's gonna be the request you could add many more Fields if you want and then how to separate the request from the response you will use three dashes okay just three not two not four just three dashes and then go back to a new line and write the response let's use simply bull success so we send color which is a string as a request and we expect a Boolean success as a response let's save this and well you can see that's pretty much the same as for messages you could also include messages from other packages if you want okay but I just try to keep things simple here the difference is is that we have three dashes to separate the request from the response and then let's build this so how to build this well if you had any dependency to add of course make sure you add them to the package.xml and the cmclist.txt but here nothing to do so I'm just gonna go back to new line here make sure you don't add any comma and I'm gonna do SRV so SRV this time because it's in the SRV folder we've set LED dot SRV and let's save this let's go back to the terminal and let's go back to our workspace and build like this build the package again all right and now let's source the setup so install setup.ash for us to interface show my robot interfaces and now you can see I have s RV and MSG so let's go to SRV set LED and we have the interface here which is the sales now and if you want to use this new service inside your code that's very easy now because in the python package we already have a dependency to my robot interfaces so we just keep it like this and in the code what you will just do from my robot interfaces dot SRV this time import set LED okay so that's the exact same thing you do not SRV instead of dot MS G and for C plus plus well you will do very simply include my robot interfaces slash okay here I don't have SRV yet so maybe let's let's just actually start Visual Studio code again after we have built and sourced so in the source folder code Dot because we should have the auto completion Okay so let's wait a bit great and now let's do slash SRV slash set LED dot HPP which is the new header file for the set LED service and then you can use the service as you would use any service and that's it you are not able to create your own customers to interfaces thank you for watching now subscribe here to get more tutorials in the future also check out my online courses if you like what I teach links in the description and see you in the next one
Up Next

Creating URDF & Xacro Robot Models in ROS2: RVIZ2 & Gazebo Guide
@By20-50
16.1K views•2023-10-22

IFS Therapy Demonstration: Complete Session with Unburdening
@IFSCA
95.9K views•2021-01-13

FastAPI vs Flask vs Django: Choosing the Right Python Web Framework
@TechWithTim
302.5K views•2024-05-26

Game of Thrones Opening Credits: A Cinematic Analysis
@gameofthrones
46.3M views•2011-04-18
Related Study Plans & Knowledge Roadmaps
Structured learning paths in General & Interdisciplinary Studies
![[GHW Open Source] Getting started with ROS 2 for Open Source projects](https://i.ytimg.com/vi/8pZYK6qACH0/maxresdefault.jpg)




![ROS2 Tutorial - ROS2 Humble 2H50 [Crash Course]](https://i.ytimg.com/vi_webp/Gg25GfA456o/maxresdefault.webp)


![4| Workspace in ROS2 - تعرف على (Workspace) في ROS2 [ROS2 بالعربى]](https://i.ytimg.com/vi_webp/kalZaTidXLE/maxresdefault.webp)






![[ROS 2 - 중급] #2 ROS 2 인터페이스](https://i.ytimg.com/vi/tSDbwRnO9Ig/maxresdefault.jpg)




















