SunFounder PiCrawler Kit
cd ~/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.
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()
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.
3.6. Obstacle Avoidance 49