# Control of a Mobile Line-Following Robot Using Neural Networks
## 4 Implementation
### Lstm  
With the data collected, it is now possible to train an LSTM neural network so that it can simulate the behavior of the PID controller. To accomplish this, it is necessary to choose a suitable model for this implementation.  
Since the PID controller takes previous values to calculate the new output value, it is important to consider a model that has the ability to use past values to calculate new motor speeds. Therefore, an LSTM network was selected.  
The implementation of an LSTM neural network involves several steps. First, the training data are prepared by organizing the CSV data, identifying the inputs and outputs, creating sequences, and splitting the data into training and test sets. The network is then defined by determining the depth, type, and size of each layer. The following step is the network training, which includes testing different configurations and depths while comparing the errors obtained in each case. Finally, the trained model is saved and can be transferred to the Raspberry Pi of the robot, where it will be used for control.  
The LSTM model is designed to accurately deliver new left and right speeds to the motors, striking a balance between complexity and regularization to optimize performance while mitigating overfitting. Figure 11 illustrates the network model used in this implementation. Table 3 lists the network parameters.  
Figure 10: Block diagram of the implemented PID controller.  
The network was trained along 60 epochs, that is, the number of times the entire training dataset is passed through the network; for that and to ensure that the model  
Figure 11: Diagram of the LSTM network used.  
\begin{table}
\begin{tabular}{c c} \hline \hline
**Hyperparameter** & **Value** \\ \hline LSTM Units (Layer 1) & 50 \\ \hline Input Shape & \((n\_steps,1)\) \\ \hline Return Sequences (Layer 1) & True \\ \hline Dropout (Layer 1) & 0.1 (10\%) \\ \hline LSTM Units (Layer 2) & 50 \\ \hline Return Sequences (Layer 2) & False \\ \hline Dropout (Layer 2) & 0.1 (10\%) \\ \hline Dense Units (Hidden) & 50 \\ \hline Activation Function (Hidden) & ReLU \\ \hline Dense Units (Output) & 2 \\ \hline Optimizer & Adam \\ \hline Loss Function & MSE (Mean Squared Error) \\ \hline Batch size & 32 \\ \hline Epochs & 60 \\ \hline Validation Data & \((X\_test,y\_test)\) \\ \hline \hline \end{tabular}
\end{table}
Table 3: Hyperparameters of the LSTM model.  
is trained correctly, the dataset was divided into training and test data, the split was configured so that 80% of the data are used for training and 20% for testing. It was observed that training with more epochs enables the model to learn better, but too many can lead to overfitting. Figure 12 shows the evolution of the model error as it is trained.  
The resulting controller is shown in Figure 13. Evidently, the PID controller and the anti-windup system were removed and replaced by the LSTM network.  
This approach enabled the robot to be controlled with a neural network using only infrared sensors as input. Its control is very similar to the PID control, which is expected, since data from that controller were used to train the neural network. However, existing similarities validate that the LSTM network was able to learn the associated patterns and temporal dependencies of the PID controller.