## 5 Applying Computer Vision and Fuzzy Logic
As the obstacle approaches, the risk level increases and the robot's speed decreases until the Andruino-R2 stops when the obstacle ahead is close enough (sensor measurement is also shown in red and displayed a warning message). To illustrate the efficiency of the controller, a second, small, cylindrical obstacle was placed at the curve end in the path, making it difficult to be detected by the ultrasonic sensor. Thus, once the first obstacle is removed, the robot continues to advance as shown in Figure 12. However, as can be seen, the second obstacle is not detected by the front ultrasonic sensor until exiting the curve when it is very close, but the control system is precise enough to stop just 9 cm from the obstacle. Once removed, the vehicle continues to track the path until it ends. Figure 10: Nonlinear function corresponding to the Fuzzy Collision Risk system. As a direct extension of the work, the student can modify the Fuzzy Collision Risk system to also consider the speed of the robot when assessing the risk of collision. In this way, a second input is added to the system, _Speed_, with two adjectives (_Normal_ and _Fast_) as shown in Figure 13. The rule base is updated accordingly resulting in:  
\[\text{\emph{Rule}}\ \#1: \text{\emph{IF Distance IS Close AND Speed is Normal THEN RiskLevel IS High}} \tag{7}\] \[\text{\emph{Rule}}\ \#2: \text{\emph{IF Distance IS Close AND Speed is Fast THEN RiskLevel IS High}}\] (8) \[\text{\emph{Rule}}\ \#3: \text{\emph{IF Distance IS Medium AND Speed is Normal THEN RiskLevel IS Medium}}\] (9) \[\text{\emph{Rule}}\ \#4: \text{\emph{IF Distance IS Medium AND Speed is Fast THEN RiskLevel IS High}}\] (10) \[\text{\emph{Rule}}\ \#5: \text{\emph{IF Distance IS Far AND Speed is Normal THEN RiskLevel IS Low}}\] (11) \[\text{\emph{Rule}}\ \#6: \text{\emph{IF Distance IS Far AND Speed is Fast THEN RiskLevel IS Low}} \tag{12}\]  
Figure 11: Andruino-R2 performing a visual line-tracking task using Android camera, detects and obstacle, and stops when it is close enough. In this case, since there are several variables in the antecedent of each rule, the weight of each rule will depend on the level of membership of each variable to each membership function. The product has been selected as the AND operator. In this way the weight of each rule will be given by the product of the membership functions, that is, \(w_{i}(Distance,Speed)=\mu_{Di}(Distance)\mu_{Si}(Speed)\). The output of the new fuzzy system is given by:  
Figure 10:0  
Figure 10:1 Andruino-R2 being able to detect a small obstacle at the end of a curve, stop until it is removed, and complete the path afterwards. which is a nonlinear surface depending on _Distance_ and _Speed_, as shown in Figure 10:2 The rest of the system does not have to be modified. Finally, in advanced mode, the student can incorporate the so-called direction of attention from the trajectory of the robot to the Fuzzy Collision Risk system to take into  
Figure 10:3 Decision surface for the Fuzzy Collision Risk with two inputs: _Distance_ and _Speed_. Figure 10:4 Fuzzy Collision Risk system with two inputs: Distance and Speed. account the lateral ultrasonic sensors when the robot moves in a curve in such a way that it can anticipate situations such as those shown with the second obstacle in the experiment.