SunFounder PiCrawler Kit
(continued from previous page)
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.
from robot_hat import Ultrasonic
Then initialize the ultrasonic pins.
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.
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:
(continues on next page)
3.6. Obstacle Avoidance 55