Babot is composed of four main organs : a Raspberry Pi 4, a camera, a custom PCB and three servo motors. All of those elements have to work together, each organ has a very specific role and has to do its work at the very right time. The responsiveness of Babot is defined by the computing power of the Raspberry Pi, the frame rate of the camera and the speed of the motors. Babot’s camera is set to operate at around 40 FPS, in this blog we will see how each image of the camera is processed and therefore what happens in Babot 40 times every single second. We can summarize the whole functioning of Babot in 6 main steps which are executed in a loop as fast as possible.
The first step is very simple, the camera take an image of the plate and send it to the Raspberry Pi for the next step.
The Raspberry Pi receive the image of the camera and has to process it. It means that it has to look at the image to check if there is a ball on the plate and if yes then it has to measure its position in the image. To detect the ball in the image the Raspberry Pi executes a python code able to isolate all the orange pixels of an image. If there is a ball on the plate then there will be orange pixels and these will be transformed to white pixels while the others will become black. This is what you can see in the middle image below. Then the script will average the position of each white pixel and this will provide the coordinates of the white spot center.
This step is quite simple, the goal is to calculate the distance between the current position of the ball and the place where we want to stabilize it. In our case we want to stabilize it in the center of the plate which corresponds to the center of the image. This distance will be called the error in the following steps.
This step is probably the most important because it is now that the magic happens. The stabilization of the ball is based on an algorithm called P-I-D, according to wikipedia a Proportional–Integral–Derivative controller is a control loop mechanism employing feedback that is widely used in industrial control systems and a variety of other applications requiring continuously modulated control. The general principle is summarized in the diagram below but I will try to explain it all much more simply.
The stabilization is based on three data weighted by the three coefficients P, I and D:
- (P) First, we look at the error (distance between the ball and the center of the plate) and we say that the bigger the error, the more we have to tilt the plate. The error is multiplied by the coefficient P and the result is the plate inclination.
- (D) Then we can compare the current error with the error measured on the previous frame. Thanks to the difference of these two errors we can deduce the speed of the ball because we know the time that separates two frames. As a reminder, the camera shoot at 40 FPS. The velocity of the ball is multiplied by the coefficient D and the result is added to the inclination previously calculated with the coefficient P.
- (I) With the coefficients P and D we already have a controller that works quite well but if we want to improve its accuracy we can add the coefficient I. This coefficient is multiplied by the sum of the errors over time and the result is added to the inclination of the plate previously calculated.
To summarize, the further the ball is from the center, the faster it moves away and the longer it is away from the center, the more we have to tilt the plate.
Now we know how many degrees to tilt the plate but we don’t know how many degrees to turn each motor to put the plate in the desired position. We can find these angles with inverse kinematic equations, I will not go into the details of long and boring calculations but the idea is to find the θ angles of the diagram below according to the α and β angles. These two latter angles define the orientation of a vector normal to the plate.
The last step is simply to send the angles of each motor to the PCB connected to the Raspberry Pi which will transmit this information to each motor. Then we can go back to step 1 and the cycle can start again.
Any questions ?
I tried to simplify the functioning of Babot as much as possible but if you have any questions I would be very happy to read and answer your comments 😉