Use inheritance for my first program (cpp)
Who is the intruder ?
Setting up the program
Welcome to this first Ontologenius tutorial. In this tutorial, we will take over the Ontologenius API using an OntologyManipulator. This is the most important thing you need to know to use Ontologenius in a simple way. We will also see how to launch Ontologenius by configuring it according to our needs.
To illustrate the different objectives of this tutorial, we will develop a mini-games that will consist of finding the intruder among a list of words. However, finding only the intruder is a much too simple problem. That's why we will also try to find the reason why one word is the intruder among the others.
In this part, we will code the bases of our program from which we will be able to write the more algorithmic part. Before that, I invite you to create a new package that we will call "ontologenius_tutorial" and which will contain only one source file that we will name "intruder.cpp". To be sure that your new package will be well configured to use Ontologenius, we advise you to follow this guide. We will just rename our executable "intruder" because it is much more logical than "my_exe".
Now we are ready! Let's open our main file and start coding:
Take a break here, we have already written a lot. We will go over all this in detail to better understand what we have done.
First of all, we have all the inclusions we need, like iostream to display messages on the screen, then vectors and strings to get vectors and strings ...
ros/ros.h is a convenience include that includes all the headers necessary to use the most common public pieces of the ROS system.
ros/ros.h is a convenience include that includes all the headers necessary to use the most common public pieces of the ROS system.
This includes the ROS String message, which resides in the std_msgs ROS package. We use it to create a topic on which we will send the words from which our program will have to find the intruder.
This includes what we call an ontology manipulator. In fact, this class is just an object to access all API ROS abstraction classes so that you can query and manage ontologenius. You can find the documentation for this object on the left panel.
As explained above, the ontology manipulator contains several objects to abstract ROS services. The set of services work in a connected mode which allows to greatly save in execution time but which makes the program less robust. For the lack of robustness, each ontologenius client takes care of everything for you by implementing recovery mechanisms. As for the connected mode, this means that the connection is established at the creation of the object and is broken only at its destruction. So we understand that it is better to avoid creating several ontology manipulators but it is better to create a single instance for our entire program. This pointer is used to collect the address of this single instance to make it available to different functions. However, we do not directly create a global instance of a manipulator because it needs a pointer to a NodeHandle of ROS.
This vector will allow us to store the different words on which we will perform our search for the intruder.
This is our synchronization variable. This variable will be false as long as we continue to collect words and we will put it in the true state as long as we wish to start the search for the intruder.
Here, the say function is actually just an on-screen display, but you can customize it to use a TTS for example.
Here, the summarize function concatenates several words in a single string. We created this function just to make our main function more readable.
Here we have the callback function of our input topic. This topic thus receives strings which will be the words from which our program will have to find the intruder. Inside the function, we can see that we use the word "start" as a synchronization word that will launch our reasoning. The different words of "start" are then stored in the vector names_ pending processing. To add security, line 30 verifies that the word is not already present in the names_ vector to avoid duplicates.
We have everything we need to start writing the main function to retrieve the list of words
to work on and start the reasoning although we do not have it yet.
Now, let's break the code down.
Initialize ROS.
We finally come back to using ontologenius! We create here our first manipulator ontology, what a great day ... This manipulator is unique to fully enjoy the benefits of connected services.
We do not forget to initialize the pointer on the manipulator by providing the address.
The close function link all the concepts loaded from files and the Internet. Before closing an ontology, exploration requests are not allowed.
For the moment, we are going to consider that the ontological file we are going to work on is provided in the ontologenius launcher. We will configure it in the next part of the tutorial. It is also possible to load files directly from the program (as above) but in doing so it makes our code less reusable.
We initialise our subscriber to the topic "intruder/input". We also link this topic to our callback function.
First we create a class which inherits from Node and initialize this later. This node is not mandatory to use Ontologenius in c++ but we need it for the ROS subscriber.
We initialise our subscriber to the topic "intruder/input". We also link this topic to our callback function.
We instantiate our subscription node and put in in an executor to be able to use the spin_once function later.
Just our almost infinite loop. Escaping with a Ctrl + C.
We initialize the synchronization variable and inform the user that the program is ready to listen to the words.
ros::spinOnce() is there to allow us to receive messages and r.sleep() avoids unnecessary use of the processor.
We only wait for the synchronisation...
We recall the words on which we will make the reasoning. It is just as a result of this that we will implement our reasoning.
Our loop is over, we can erase the words we used and start over!
Now, you just have to compile that before going on to the following:
Do not forget that this command must be done in your workspace