In this lesson, we will see how to control a servo motor with Arduino and Ros to.
We will use the knowledge acquired in the previous lesson to publish a certain position command for
a servo motor within a ros2 topic.
This command will be received and serialized by the transmitter node implemented in the previous lesson
and then will be sent to the Arduino through the serial port.
Upon receiving this command instead of turning on or off a Led as we did in the previous lesson, Arduino
will instead actuate a servo motor to our desired position.
So let's open the Arduino IDE and here let's create a new sketch.
So a new project.
And within this new sketch still we have the setup function, which is executed once at the startup
of the Arduino and the loop function, which is repeatedly executed.
To interact with the servo motor and to send commands to control it.
Arduino as a nice library called Servo.
So let's start by importing this library.
So let's include the library server dot age.
And let's use this library to create a new variable.
So a new object of type server.
And let's call this one motor.
This class.
So the server class contains a series of functions and tools that make it very easy to send commands
and so to operate a servo motor.
To initialize the communication with the servo motor.
So let's go.
Here in the setup function, we need to use the motor object that we have just created.
And on this one let's use the function attach.
So with this function, we can pass the number of the pin to each servo motor is connected.
For example, let's assume that we want to connect the servo motor to the PIN number eight of our board.
And now still using the servo library, we can move our servo motor to a certain angle.
And this is as simple as calling the function right on the object motor.
And to this function we need to pass the angle in degrees to which we want to rotate the servo motor.
For example, at the start up, let's make the motor to be in the position 90 degrees so in the middle
of its range.
Now still in the setup function after initializing the communication with the motor and after moving
it to the position 90 degrees.
Let's also initialize the serial communication between the Arduino and our PC on which Ros two is running.
So let's use the function begin and then let's set the about rate that we want to use for this communication
to 115 200.
And then let's also, as usual, use a timeout.
So with the function set timeout and let's set a timeout of one.
With this, the setup function is completed.
And now regarding the loop function.
So regarding this function here, in this case, we want the microcontroller.
So at each iteration of the loop function, we want to check if there is any new message available on
the serial port.
So if there is any new message coming from Ros two.
So let's check this one with the serial available function.
So with this one, we are checking if there is any new message.
And in this case, so if there is a new message available, let's read it with the function read String.
And since we know that we are going to receive an integer, let's also convert this one to an integer.
Let's store this integer here within a new variable and let's call this one angle.
And now we can use this variable.
So the angle variable in order to move the servo motor in the desired position.
And so we can do this by using the function motor dot write and so we can write the angle position that
we just set.
Finally, let's also add a delay in the loop function here of 0.1 so that there is so that there is
a small interval between two execution of the loop function.
We can save this code.
So let's save it within the Arduino workspace, within the Arduino board firmware, and within the firmware
folder.
And let's call this one simple servo control.
So let's save it.
Now before actually uploading this code on the Arduino board, let's verify that it compiles so that
we have no errors.
So it compiles properly.
So now, before uploading the code into the Arduino board, let's first connect the servo motor to the
board.
And we are going to need three jumper wires to connect the servo motor pins to the board.
And let's connect the brown wire to the GMD pin of the Arduino.
Then let's connect the red wire to the board's power, which is the five volt pin.
And the last wire.
So the orange one is the one that transmits the signal to move the robot.
And so we are going to connect it to the PIN eight of the Arduino, as we said in the setup function
of our script.
Now that's the Servomotor is connected to the PIN eight of the Arduino.
We can proceed to connect the Arduino to the PC.
And once we do so, the Arduino IDE should be able to automatically detect the port to which it is connected.
So if we go to the board now, we can see that in my case it is connected to the port ACM zero.
So let's click on this one and let's upload the code on the Arduino.
Now, as soon as we upload the code, we can see that the servo motor moves to its initial position,
which is 90 degrees, as we indicated in the setup function.
Now we can finally send movements to the motor and to do so, let's open a new terminal.
And here let's go to our workspace.
Let's source it.
So let's source the setup dot bash.
And now let's see how we can send command to our servo motor using our ros2 topic.
So as a first thing, let's run a Ros two node that is in the Arduino bot firmware package and that
we have implemented in the previous lesson.
That is the simple serial transmitter here.
I'm using the Cplusplus implementation, but if you want you can use the Pi implementation by adding
the suffix dot pi to this node.
We need to pass a Ros argument, so we need to pass a parameter.
So a value to the parameter port.
And in my case this is connected to the port.
ACM, ACM zero.
So let's press enter and upon starting this node does nothing but initializing the communication with
the Arduino.
So the serial communication with the Arduino and this node is listening on a topic on a Ros two topic
for any new message that is sent through that topic in order to forward it to the Arduino through the
serial port.
So let's open a new terminal and let's use the command Ros two topic list.
To see that actually there is the serial transmitter topic.
So now let's use this topic to move our servo motor.
So with the command Ros2 topic pub and let's publish a message within the serial transmitter.
Here the message type that is accepted is string.
So let's insert a standard message string.
Then let's use double quote.
Let's type D and press tab, and this will insert an empty prototype of the string message.
And for example, let's publish the message zero.
So the angle zero.
So with this, we are asking the Arduino to move the servo motor to the position zero.
So let's press enter.
And indeed we can see that as soon as we publish this message, it is serialized and sent to the Arduino,
which then moves the motor accordingly.
Let's send another message.
This time let's make the servo motor reach the position 180 degrees.
So let's press enter.
And again the servomotor is moved as requested.
So in the desired position.
Building upon this code, in the next lesson, we are going to extend this control to all the four servo
motors of the robot.
And so we are going to control all the servo motors of our robot.