Now we'll be creating the cpp 2
publisher you know already what is the publisher its a topic on which the data 3
will be publishing okay come on to the screen and here is the method how to create a publisher 4
using the C plus plus okay it is saying that here using this link you can download the code 5
okay we have already created the package inside the already uh C plus plus file present and what we 6
will do we have to keep our new code in this SRC folder in order to maintain a package structure 7
click right open in terminal and then you can just paste the link and these file is downloaded 8
so we also want this package to be opened in the vs code click right here code space 9
and here we have let's learn more about this code so we will just remove that these 10
two libraries that will allow us to handle the memory and uh like using the number so 11
these two libraries are related to the uh ros2 RCL CPP is a ros client library with 12
C plus plus and here it is the string that we string message that we are going to use in our 13
publisher you can also use the chatgpt to get the full explanation of the code here we 14
have the namespace and here is the class minimal publisher that is here from the RCL CPP : : and the node 15
here is the function minimal publisher which is custructor here we are mentioning the node name you can even change this name here we have the variable 16
that is initialized with the zero and here we are creating a publisher that is publishing messages 17
of string type and the topic name is topic and here we have a timer and it will be repeating itself 18
after 500 millisecond node is pointing towards a minimal publisher and here we have the timer 19
callback that is declared in the private here is the definition of the timer callback here we have 20
the message of type string and the message dot data so it has an instance data in it so 21
we are assigning it the string value like a hello word we're also converting the integer 22
to the string in order to add the string here we have the RCL CPP info it will be printing the 23
information on the terminal then it publishes the message onto the publisher here we have the three 24
private member Declaration the timer and the publisher and the variable count and the main 25
it initializes and then creates the instance of minimal publisher Class feed into the spin 26
function it will be like circling and processing ros messages and data until it will be shut down 27
in order to run this node we will need to make some changes in package dot XML and 28
the cmake list so here we have the method to add the dependencies in the package.xml 29
file so open up here the package.xml so we have seen two dependencies in the code 30
first one is the RCL CPP and the second one is the STD message so we will be declaring inside 31
the package dot XML we will just copy and we will add these lines after the ament cmake 32
we will come here and hit enter and Ctrl V so we have added these two dependencies 33
in our package.xml alright now it will come to the C make list 34
all right here we have already the executable file that was created when we created the 35
package and now we will just copy and go to here and here we have the find package line 36
find package RCL CPP and STD messages so these are the dependents that we are 37
mentioning here now we have to declare our node so here is the method as executable 38
and there there we are mentioning the name of the node talker and then the related C plus plus file 39
ament target dependencies talker depends upon the RCL CPP and STD messages simply copy this and go 40
to here paste it here alright so we are giving it the name talker okay now we have to mention 41
the name of the node inside the install Target so that draws to run can find the executable okay so 42
uh here is the node that we have to mention inside the install so uh here is the line install we will 43
just hit enter there is already an executable file present so we will just enter and talker 44
all right so we have mentioned our node so now we have told our package that there is a C plus plus 45
file and the node name is talker and we have mentioned the dependencies in package XML file 46
we will try to build it open the terminal go to your ros2 workspace colcon 47
build minus minus sym link minus installed 48
so it successfully built now we will Source our local setup install local setup.bash 49
so if we have source to first base now we will write ros2 run 50
package name which is turtlesimbot and if we double tab 51
it will show us all the executable files that are available so we will issues topic 52
alright so it is publishing now hello world and the number next to it 53
let's modify this publisher we want a list in which there are several string 54
members we will be calling each line one by one now I will create a list my list 55
so we will have to declare the type we will use the static 56
and then std double colon vector and inside std 57
double colon string 58
and then space my list 59
okay so here we have declared our list and now we have to mention that how we are going 60
to call the member of the list so if we come here message dot data we are giving the value 61
call my list my list and then the member so so we can use the remainder method so count 62
and percentage and then the size of my list my list dot size 63
let's try to build this 64
now we'll run our node talker 65
uh it is saying I'm learning growth I haven't made a mistake that this count variable is not 66
being increased so previously it was increasing build it again to node yeah I am learning raw 67
through and I will learn micros and I will make my first row a hardware Robot Turtle 68
Sim oh yes so this is how we can modify any code let's make it even more good looking 69
yes and here now we can see that a string is ending and now it is like printing it 70
here so what we will do open up an other terminal see the ros2 topic list 71
here is the topic name uh topic so we will try to echo it Ros2 topic echo 72
and topic name 73
and here we can see it is printing all the 74
information here as it was printing it here