.. note:: Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts. **Why Join?** - **Expert Support**: Solve post-sale issues and technical challenges with help from our community and team. - **Learn & Share**: Exchange tips and tutorials to enhance your skills. - **Exclusive Previews**: Get early access to new product announcements and sneak peeks. - **Special Discounts**: Enjoy exclusive discounts on our newest products. - **Festive Promotions and Giveaways**: Take part in giveaways and holiday promotions. 👉 Ready to explore and create with us? Click [|link_sf_facebook|] and join today! .. _py_avoid: Obstacle Avoidance ===================== In this project, picrawler will use an ultrasonic module to detect obstacles in front. When PiCrawler detects an obstacle, it will send a signal and look for another direction to move forward. .. image:: img/avoid1.png **Run the Code** .. raw:: html .. code-block:: cd /home/pi/picrawler/examples sudo python3 avoid.py After the code runs, PiCrawler will walk forward. If it detects that the distance of the obstacle ahead is less than 10cm, it will stop and sound a warning, then turn left and stop. If there is no obstacle in the direction after turning left or the obstacle distance is greater than 10, it will continue to move forward. **Code** .. note:: You can **Modify/Reset/Copy/Run/Stop** the code below. But before that, you need to go to source code path like ``picrawler\examples``. After modifying the code, you can run it directly to see the effect. .. raw:: html .. code-block:: python from picrawler import Picrawler from robot_hat import TTS, Music from robot_hat import Ultrasonic from robot_hat import Pin import time import os tts = TTS() music = Music() crawler = Picrawler([10,11,12,4,5,6,1,2,3,7,8,9]) #crawler.set_offset([0,0,0,0,0,0,0,0,0,0,0,0]) sonar = Ultrasonic(Pin("D2") ,Pin("D3")) alert_distance = 15 speed = 100 def main(): distance = sonar.read() print(distance) if distance < 0: pass elif distance <= alert_distance: try: music.sound_effect_threading('./sounds/sign.wav') except Exception as e: print(e) crawler.do_action('turn left angle',3,speed) time.sleep(0.2) else : crawler.do_action('forward', 1,speed) time.sleep(0.2) if __name__ == "__main__": while True: main() **How it works?** You can get the distance by importing the ``Ultrasonic`` class. .. code-block:: python from robot_hat import Ultrasonic Then initialize the ultrasonic pins. .. code-block:: python sonar = Ultrasonic(Pin("D2") ,Pin("D3")) Here is the main program. * Read the ``distance`` detected by ultrasonic module and filter out the values less than 0 (When the ultrasonic module is too far from the obstacle or cannot read the data correctly, ``distance<0`` will appear). * When the ``distance`` is less than or equal to ``alert_distance`` (the threshold value set earlier, which is 10), play the sound effect ``sign.wav``. PiCrawler does ``turn left angle`` . * When the ``distance`` is greater than ``alert_distance``, PiCrawler will move ``forward``. .. code-block:: python distance = sonar.read() print(distance) if distance < 0: pass elif distance <= alert_distance: try: music.sound_effect_threading('./sounds/sign.wav') except Exception as e: print(e) crawler.do_action('turn left angle',3,speed) time.sleep(0.2) else : crawler.do_action('forward', 1,speed) time.sleep(0.2) .. note:: You can add different sound effects or music to ``musics`` or ``sounds`` folder via :ref:`filezilla`.