In this laboratory lesson, we're going to develop a code for the Arduino that constantly reads the
serial port, listening for new messages coming from Ros2.
Additionally, upon receiving a new message, it will turn on or off the Led that is integrated on the
board.
To complete this lesson, you will need the Arduino board and a USB cable to connect it to your PC.
All the code we are going to write during this lesson.
Use a programming language that is a variant of the Cplusplus specifically designed for the Arduino.
If you are familiar with Cplusplus, then you will have no difficulties into following along with the
development, even if you never used the Arduino before.
However, if you are not familiar with Cplusplus, it might be a bit more challenging to understand
the code.
Nevertheless, as I always say, this is not a programming course but a robotics course.
So try to grasp the key concepts and the logic rather than the syntax of the language that we are using.
In the following lessons, we are going to develop both Python and Cplusplus node for Ros2 to send messages
to the Arduino serial port.
And with this we are going to enable the communication between the Ros two and the Arduino.
Then also in the following lessons, we are also going to enable the reverse communication.
So the one from Arduino to Ros two enabling Arduino to send messages to Ros two.
Let's start by creating a new rescue package that will contain all the code developed in this section.
So let's go to our workspace and to the source folder.
And here let's create a new package.
So you are already familiar with the rescue package, create command.
And in this case, let's use as build type and.
See make as we are going to insert both Python and C plus plus scripts in this package.
And then let's name this package, Arduino Bot firmware.
So let's press enter.
And this has created our new Arduino Bot firmware package.
Then let's go back to the workspace and let's build it so that the new package is recognized and is
available in Ros two.
So this one since in this lesson we want to develop the code for the Arduino board, which receives
a command from the serial port and turns on or off a Led.
Accordingly, we are going to use the Arduino IDE, which had you download in one of the early setup
lessons.
So this one.
In this ID, so let's make it bigger.
Besides writing and compiling the code for the board, we can also directly upload the code to the board
once we are going to connect it through the USB cable.
So here we can compile it and here we can also upload the code to the board.
By default, when we create a new project within the Arduino IDE, it comes initialized with a script
that contains two functions the setup function and the loop function.
The first one.
So this one, the setup function will contain all the instructions that the Arduino needs to execute
only once, so only at the startup of the board.
Instead, the loop function.
This one will contain all the instructions that the Arduino needs to execute repeatedly so at regular
time intervals defined by the controller's frequency.
So let's start by defining a new global variable.
So let's go here and let's define a new global variable.
And let's call this one let pin and let's set it to 13.
This indicates the number of the pin that is on the Arduino board to which the Led is connected.
And this is the onboard Led that is already integrated with the board itself.
And we want to turn this led on or off based on the message that was received on the serial port.
So now in the setup function, so the one that is executed only once when the controller starts up,
we need to initialize this specific pin.
So the PIN 13 where the Led is connected.
And so let's use the function pin mode to set the Led pin variable and to set this one of type output.
So basically with this instruction we are setting that we want to send an output command to this pin
here in order to turn on or off the Led.
By default.
So at the controller start up, still in the setup function, we want the Led to be off.
So let's use the digital write function to write on the Led pin and let's write a low tension.
And so this basically will turn off the Led if it was on.
Next, still in the setup function.
Let's also initialize the serial communication.
So the serial communication with the our PC and so with the Ros two world and let's use the serial begin.
And within this function we have to specify the bound of the connection.
So let's set it to 115 200 and additionally let's set the timeout for the serial communication.
So serial set timeout and let's set a timeout of one.
With this, the board initialization is completed, so the setup function is completed and now we can
move on to define the behavior of the loop function.
So the one that is executed repeatedly after the setup function.
So here, let's check if we have any new messages on the serial port with the instruction serial available.
So if there is any new message available on the serial port.
So this means that our Ros two node sent a new message to the Arduino.
Now we want to read this message within the Arduino.
So to read this message, let's use the serial read.
String function and let's convert this one to an integer.
Since we know that all the messages that we are going to receive from our ros2 node are going to be
integers.
And let's store the output of this function into a new variable called X.
And now let's check which is this message that we have just received.
So basically, let's check which is the message.
So the content of the message that we have just received.
So if the value of X is equal to zero.
Now, in this case, we want to turn off the Led.
So let's use again the digital.
Write function to write on the pin of delete.
And we want to write again a low tension.
So in order to turn off the light, otherwise so else if we receive any other number, so any other
command we want to turn on delete.
So again, let's use the digital write function to write on the Led pin.
And in this case we want to write an high voltage.
And so basically this will turn on the Led finally at the end of the if statement.
So here in order to avoid this loop to be executed too rapidly, let's add a small delay of 0.1.
With this, we have completed our very simple script for the Arduino that receives a message from the
serial port and turns off or on the Led based on the message that we have received.
Now let's save this file.
So with Ctrl s and let's save it within our workspace and so within the new package that we have created.
So the Arduino bot firmware and here let's create a new folder called firmware and this will contain
all the code for the Arduino that we are going to develop in this section.
And let's call this one.
So let's call this script simple serial receiver.
So let's save it under this name.
Let's conclude this lesson by compiling and loading this code into the Arduino board so we can compile
it to verify that there are no no errors.
And actually we can see that there is an error indeed.
Because here there is a typo.
Time out.
Let's save and let's compile again.
And now we can see that it compiled properly so there are no more errors.
And now let's save it one more time.
And now we can connect our Arduino board to our PC using a USB cable.
And before uploading the code to the board, we need to configure the Arduino ID to indicate the type
of the Arduino board that we are using and also the port to which it is connected.
So in the tools section, let's change the board to the board that you are using.
In my case, I'm using an Arduino Uno and then still in the tools section.
Let's set the port.
And here, as you can see, the Arduino IDE was able to automatically detect that at the port.
ACM zero in my case is connected an Arduino Uno.
So let's click on this port and be aware that in your case it can be connected, for example, to a
different port with a different name.
So once you select the port correctly, now we can click on upload.
And this command will first compile the code to check for any errors, and then we load it into the
Arduino boards and also it will automatically start its execution.