## 5 Applying Computer Vision and Fuzzy Logic
Thus, a reference system change must be applied before calculating the lateral error which is fed back through a proportional controller to compute the desired linear (\(v_{d}\)) and angular velocities (\(\omega_{d}\)). Figure 8 shows the control screen with the different steps, starting with the image obtained from the robot's camera, its conversion into the HSV frame, the result of applying the mask to the original image and finally the image showing the selected reference point on the line, the computed steering action (in blue) and the distance measured by the front ultrasonic sensor (in green). As a next step, to get the robot to stop when there is an \"obstacle close enough ahead\", students implement a Fuzzy Collision Risk module, which is a first contact with qualitative knowledge systems using Fuzzy Logic. Thus, taking as input the distance measured by the front ultrasonic sensor, the fuzzy system must calculate a collision risk index (RiskLevel), in the range [0...1]. This index will be used later to modify the velocities (\(vd,\omega_{d}\)) calculated by visual tracking before being applied to the robot. For the fuzzy system, a simple Takagi-Sugeno system with one input and one output is considered, as shown in Figure 9. The input variable, _Distance_, is defined by three linguistic terms (close, medium and far) and their corresponding membership functions (\(\mu_{DC}\), \(\mu_{DM}\) and \(\mu_{DF}\)) that are set by the student. The output variable, _RiskLevel_, is defined  
Figure 7: Local and camera reference frames in Andruino-R2 (**left**). Image seen from the on-board camera when the robot is centered on the line (**right**). by three singleton values: _Low_, _Medium_, _High_. The following three rules complete the fuzzy system:  
\[Rule\ \#1:\quad\text{IF Distance IS Close THEN RiskLevel IS High} \tag{2}\] \[Rule\ \#2:\quad\text{IF Distance IS Medium THEN RiskLevel IS Medium}\] (3) \[Rule\ \#3:\quad\text{IF Distance IS Far THEN RiskLevel IS Low} \tag{4}\]  
Given a crisp measurement (_Distance_) of the ultrasonic sensor, the degree of truth of each rule, or weight (_w_(_Distance_)), is evaluated (Fuzzyfication). Since in this case only  
Figure 8: Control screen showing the different stages (from top to left): original image from Android camera; image in HSV framework; original image masked with the detected line; and original image with the selected target point marked on the line. Figure 9: Fuzzy Collision Risk system (from top to left): system blocks with one input (_Distance_) and one output (_RiskLevel_); linguistic terms and membership functions for the input; and singleton values for the output. one variable and one label are considered in the antecedents of each rule, the weight of the rule will be given directly by the involved membership function (\(w_{i}(Distance)=\mu_{Di}(Distance)\)). Then, the consequent of each rule (\(RiskLevel_{i}\)) is weighted by its degree of truth by using product operator, yielding the rule output (Inference). To obtain the unique output of the fuzzy system, it is necessary to combine the output of each of the rules (composition and defuzzification). In this case, a weighted average of the different rules has been selected. Therefore, the output of the Fuzzy Collision Risk system can be calculated using the next expression:  
\[RiskLevel=FCR(Distance)=\frac{\sum_{i=1}^{3}\mu_{Di}(Distance)RiskLevel_{i}}{ \sum_{i=1}^{3}\mu_{Di}(Distance)} \tag{5}\]  
which means that \(RiskLevel\) is a nonlinear function of _Distance_ as shown in Figure 7.0 This index is then applied to correct the velocities (\(vd,\omega_{d}\)) calculated by visual tracking before being applied to the robot (see Equation (1)):  
\[ \begin{bmatrix}v\\ \omega\end{bmatrix} =(1-RiskLevel) \begin{bmatrix}v_{d}\\ \omega_{d}\end{bmatrix}  \tag{6}\]  
Figure 11 shows the application of the aforementioned controller to an experiment with Andruino-R2 starting off from a green line, approaching it and keeping centered on it.