# Control of a Mobile Line-Following Robot Using Neural Networks
## 4 Implementation
### Cnn  
In the second experiment, the aim is to develop a platform for the robot to follow a line drawn on the ground using neural networks, as in the previous experiment, but using a camera. In this case, the Raspberry Pi camera mounted on the robot will point downward so that it can take successive images of the ground in front of it. These images will then be fed to a neural network that will control the robot movement along the path. With this strategy, the use of an LSTM neural network would offer limited benefits compared to a CNN since the LSTM network was chosen for the previous experiment to simulate the behavior of the PID controller, which requires past information to calculate the new output. However, the use of real-time images to control the robot does not involve the temporal dependence found in the PID controller. However, the use of an LSTM network could easily, if configured with an incorrect number of steps for each sequence, lead to  
Figure 12: Evolution of the LSTM network’s error during training. Figure 13: Block diagram of the implemented LSTM controller. the network memorizing the path it was trained to follow, capable of causing the robot to behave abnormally or incorrectly if placed on a different track. For these reasons, a CNN was chosen. As in the first experiment, a large amount of data is required to train the neural network. In this case, the data will be a large number of images taken by the robot itself, so it is necessary to develop a method for obtaining these images. As mentioned, the first step is to take several images of the path and save them for use in neural network training. This will be completed following the steps outlined in the flowchart of Figure 14:  
* **Camera initialization and positioning**: During initialization, the camera is configured to capture images and is positioned correctly for the task. Additionally, the positions of the servos connected to the camera are adjusted to ensure that the camera is aligned pointing straight down for accurate image capture;
* **Taking a new image**: In each iteration of the loop, a new image is captured from the camera (Figure 15). This continuous image acquisition is crucial for real-time image processing;
* **Image processing for line detection**: The process begins by converting the color image to grayscale (Figure 16), which simplifies the complexity of the image and improves the effectiveness of binary thresholding. Binary thresholding is then applied (Figure 17), where a threshold value transforms the image into a binary representation, highlighting the relevant pixels in black and white. To optimize processing, the image is then cropped to keep only the top part (Figure 18), thus reducing unnecessary areas and improving the detection of the desired line near the robot, which is essential for the control operation;
* **Save image**: After processing, the final image is saved to a file. The file path is generated dynamically and the image is saved in.jpg format. With this procedure, several images of the track are taken at different locations, positions, and angles to ensure that the neural network is trained to handle any situation the robot may encounter. The dataset has a size of 1800 images. Figure 16: Example of a grayscale image. Figure 17: Example of an image with binary thresholding. Figure 15: Example of a raw image. With the dataset obtained, the next step is to classify the images so that they can be used to train the neural network. For this, each image will be assigned the action that the robot will have to complete. These actions will be divided into the categories listed in Table 4.  
The wheel speed values are between \(-255\) and \(+255\), where negative values cause the wheel to spin backward and positive values spin forward. Additionally, in the last category ('lost_line') corresponding to when the robot loses the line, the robot will move accordingly to the previous reading.