# Evita obstáculos

##### <span style="color: rgb(22, 145, 121);">**Programa**</span>

El núcleo del programa es la función api

get\_distance(unit: str = 'cm')

Es sorprendente el sensor TOF como puede leer no sólo diréctamente sino a los lados :

- left\_tof: 45° to the left object distance
- center\_left\_tof: 22° to the left object distance
- center\_tof: center object distance
- center\_right\_tof: 22° to the right object distance
- right\_tof: 45° to the right object distance

El programa es extraido de [https://docs.arduino.cc/tutorials/alvik/getting-started/](https://docs.arduino.cc/tutorials/alvik/getting-started/) Author<svg fill="none" height="4" viewbox="0 0 3 4" width="3" xmlns="http://www.w3.org/2000/svg"><circle cx="1.5" cy="2" fill="#95A5A6" r="1.5"></circle></svg>Jose Garcia

```python
from arduino_alvik import ArduinoAlvik
from time import sleep_ms
import sys

alvik = ArduinoAlvik()
alvik.begin()
sleep_ms(5000)  # waiting for the robot to setup
distance = 20
degrees = 45.00
speed = 50.00

while (True):

    distance_l, distance_cl, distance_c, distance_r, distance_cr  = alvik.get_distance()
    sleep_ms(50)
    print(distance_c)

    if distance_c < distance:
        alvik.rotate(degrees, 'deg')
    elif distance_cl < distance:
        alvik.rotate(degrees, 'deg')
    elif distance_cr < distance:
        alvik.rotate(degrees, 'deg')
    elif distance_l < distance:
        alvik.rotate(degrees, 'deg')
    elif distance_r < distance:
        alvik.rotate(degrees, 'deg')
    else:
        alvik.drive(speed, 0.0, linear_unit='cm/s')

```

##### <span style="color: rgb(22, 145, 121);">**Resultado**</span>

<span style="color: rgb(0, 0, 0);">El código es mejorable, pues que rote 45 grados tantas veces puede hacer que se quede "enganchado" en una esquina, ver el final del vídeo:</span>  
<iframe allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen="allowfullscreen" frameborder="0" height="849" src="https://www.youtube.com/embed/CWx501rFpyA" title="July 12, 2024" width="478"></iframe>