I will separate this solution into three different videos, so it will be easier for you to follow.
First, in this video, the first one, I will create the LED panel node and publish the LED panel state
with a custom message type.
In the second video, I will create the service server inside the LED panel using also a custom service
type.
And in the third video that's going to be the battery node which will call the set LED server.
And once again I do the solution in Python and you can download the complete C plus plus code at the
end of the section.
So let's get started.
We first want to create a custom interface because we want to publish an LED panel state.
And for that we need a custom message.
So that's the first thing we need to create.
Let's go to our workspace and then to our Myrobot interfaces.
And here we are going to create a new message inside the MSG folder.
And I'm going to name it.
So let's do touch LED state Array dot msg.
Okay.
And I just start each word here with an uppercase.
And then I use lowercase for all the other letters.
All right.
Now let's go to VS code.
I'm going to open VS code here.
And let's create that interface.
So I'm going to close all of that.
And in MSG we have led state array.
So what I'm going to do is simply to create a list of integer numbers for the LED state.
To create a list of integer numbers I can put int 64.
That's the type.
And then those brackets to say it's a list and I'm going to name them LED states for example.
Okay.
So we're going to say that zero means off and one means on.
We could have used booleans as well.
But here I'm just using integers with zero and one.
Let's save.
Okay.
So we just have one field.
This is an array of integers.
And that's it.
Now we can build it.
To build it, I'm going to go to Cmakelists.txt and add another line here.
So I'm going to group all the messages and then all the services.
It doesn't matter in the end but it's just a bit cleaner.
So here I add another line MSG slash LED state array dot msg.
Make sure you don't make any typo.
Okay, for this file name let's save the Cmakelists.txt.
And that's it.
Okay we have created the interface.
We add it here.
Now we can build it.
Let's do click on build packages.
Select with my robot interfaces.
You see I still have this warning log but I'm not doing anything wrong.
So I'm just going to ignore that log.
All right.
And we have built the interface now because we have built the interface.
We will need to source all the terminals where we want to be able to use it.
We will also need to close VSCode and open it again from a terminal that is sourced.
So I'm just going to close VSCode and I'm going to close all the terminals.
Okay.
I'm going to open terminals again.
Okay.
And then start VSCode from that folder.
Okay.
So then I can use my interface with the Autocompletion for Python and for C plus.
Plus just a quick note for C plus plus you already have that line here okay in the C and cpp properties
dot JSON.
So for C plus plus it's also going to work.
But first of all let's go in the terminal and let's see if we can find this interface Ros2 interface.
Show my robot interfaces MSG.
And then let's put an L with Autocompletion.
We have led state array.
Great.
And if you can see that, it means that you can use your interface.
If you cannot, you need to go back and make sure that everything is working.
So now let's go here.
And actually I'm going to create a new node because I want to create a node specially for the LED panel.
So let's go to the Python package here and let's create that LED panel dot pi.
Let's make it executable okay.
And let's edit that file.
So I don't need that anymore LED panel dot pi I'm going to take the template I don't need that.
And let's put the template here and it's going to be LED panel node okay.
And let's just call it LED panel.
All right.
And what I want to do here is to publish the states for all the LEDs in this LED panel Every X amount
of time.
So as you can probably guess, we need what we need a publisher and we need a timer to create a publisher
I will need the interface.
So let's import from my robot interfaces dot msg import led state array.
Let's create a publisher here self dot led states publisher or just pub to take less space.
We have self create publisher with the type led state array.
That's the one here.
Then the topic name.
So let's call it LED panel state like the one I gave you in the challenge.
And then we have a queue size.
I'm just going to make it a bit cleaner okay.
After that I'm going to create here in the class a method to publish LED states.
Okay, I'm just going to put pass for now so that I can create a timer.
Self dot LED states.
Timer is equal to self dot.
Create timer.
And well let's say we want to publish every five seconds.
Okay.
We don't necessarily need that very often.
So every five seconds we're going to publish the LED states.
I put five and then self dot publish LED states.
That's the callback.
And finally let's add a log get logger info LED panel node has been started.
Okay.
And what do we do in this publish led state method.
We're going to create a message of type LED state array.
And this message here has a LED state field.
Okay.
If you are not sure you see we have it here from the terminal.
You can also well you have written the code.
So you can just go back to your interface and check that.
And we also have the Autocompletion led state of type.
Well that's the list of integers.
So I'm going to initialize in the constructor I'm going to initialize self led state.
And I'm going to initialize that to an array with three elements.
Let's say we have three LEDs just like in the example I gave you.
And those three LEDs are turned off.
So once again zero means turned off.
One would be turned on.
All right.
So now they are all turned off.
We're going to see how to turn them on with a service.
But for now we just publish the state as it is.
So edit state is equal to self led states.
And then we can do self dot led states.
Publisher dot publish the message.
Okay.
So once again we have a very classic publisher and timer combo where we publish every X amount of time
here every five seconds.
So I'm going to save that.
And let's create an executable here setup.py.
I add a comma and then LED panel is equal to my pi pkg dot led panel with the main function.
Okay let's go back here.
Let's build the package called con build packages.
Select with the Python one.
And I'm also going to add the symlink install option here.
So we can modify without having to build again.
So now from this one, for example, let's use the terminal and let's do Ros to run my Pi PG with LED
panel.
Okay.
The node has been started.
Let's just source all the terminals here.
Okay.
And let's do a Ros two topic list.
We should see our LED panel.
State Ros two topic.
Equal LED panel state.
So it's publishing every five seconds.
So you might need to wait a bit.
You see we have one.
And then we should have another one five seconds later etc..
And that's how you see an array.
Okay.
You see we have the name of the field.
And then one element per line here with a dash.
So when you do a ros2 topic that's how you're going to see lists of elements.
Great.
And the first step for this activity is finished.