# Control of a Mobile Line-Following Robot Using Neural Networks
## 4 Implementation
### Pid  
The objective is to control the robot to follow a line drawn on the ground utilizing neural networks through infrared sensors. To achieve this, in order to collect and classify data to be used to train the LSTM network, a PID controller was first implemented. Therefore, whilst the robot is being controlled by the PID, the infrared sensor data and right and left speeds are saved for use in the next experiment. Figure 7 shows the main loop of the PID-controlled system. This loop is executed 80 times per second. While the user does not interrupt the program, the algorithm will start on each loop by obtaining the new error value to be fed to the PID controller. This error is defined as the approximate distance between the central position of the infrared sensor module and the central position of the line. Consequently, the current position value is first calculated by the following equation:  
\[Position=\frac{(0\times S1)+(2750\times S2)+(3550\times S3)+(6300\times S4)}{S1 +S2+S3+S4} \tag{5}\]  
where S1 to S4 represent the binary logic values of each sensor. This estimate is completed using a weighted average of the sensor indexes multiplied by a specified value. The values \begin{table}
\begin{tabular}{c c c c c c c} \hline \hline \multirow{2}{*}{**Layer**} & **Layer** & **Feature** & \multirow{2}{*}{**Size**} & **Kernel** & \multirow{2}{*}{**Stride**} & **Activation** \\  & **Type** & **Map** & & & **Size** & & \\ \hline Input & Image & 1 & 32 \(\times\) 32 & - & - & - \\ \hline
1 & Convolution & 6 & 28 \(\times\) 28 & 5 \(\times\) 5 & 1 & tanh \\ \hline
2 & Average & 6 & 14 \(\times\) 14 & 2 \(\times\) 2 & 2 & tanh \\  & Pooling & & & & & \\ \hline
3 & Convolution & 16 & 10 \(\times\) 10 & 5 \(\times\) 5 & 1 & tanh \\ \hline
4 & Average & 16 & 5 \(\times\) 5 & 2 \(\times\) 2 & 2 & tanh \\  & Pooling & & & & & \\ \hline
5 & Convolution & 120 & 1 \(\times\) 1 & 5 \(\times\) 5 & 1 & tanh \\ \hline
6 & FC & - & 84 & - & - & tanh \\ \hline Output & FC & - & 10 & - & - & softmax \\ \hline \hline \end{tabular}
\end{table} 
Table 2: LeNet-5 architecture. used can be chosen arbitrarily, so the controller gains would need to be adjusted accordingly. However, since the four sensors are not spaced equally apart, the solution found was to match the gains to the distance between the different sensors. By measuring this distance, the values were set to 0, 2750, 3550 and 6300, with the first weight being 0, kept in the equation for clarity of the weight used for each infrared sensor of the module, as shown in Figure 8.  
Finally, with the distance calculated, the error can be determined by subtracting the desired value (3150) from the current position, \(Error=3150-Position\). In this way, if the error is 0, the sensor is centered with the line; if it is negative, the line is to its right; and, if it is positive, to its left. Furthermore, if the previous function returns \(-1\), that is, the  
Figure 8: Diagram of the weighted average used by the system and the error calculation. Figure 7: Main loop of the PID controller. sensor 'loses' the line, the error will be adjusted taking into account the previous error. If the previous error is negative, it will be changed to \(-6000\), and, if positive, it will be set to \(6000\), as described in the implementation of Figure 7.  
With the error determined, this value can be used as the parameter for the function PID_Linefollow. Figure 9 illustrates the steps of this function:  
* **Calculation of terms**: proportional (\(P=error\)), integrative (\(I=I+error\)), and derivative (\(D=Error-previousError\)), multiply by their respective gains, and add them to calculate the PID value. The gains were obtained experimentally, trying several combinations so that oscillations were minimized and responsiveness optimized. In this case, the gains are \(K_{p}=0.04\), \(K_{i}=0.00002\), and \(K_{d}=0.035\). * **Initial wheel speed adjustment**: a default value is set for the motors to turn, in this case \(70\), called the base speed.