|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import time |
| 4 | +import threading |
| 5 | +import ev3dev.ev3 as ev3 |
| 6 | + |
| 7 | +def tail_waggler(): |
| 8 | + """ |
| 9 | + This is the first thread of execution that will be responsible for waggling |
| 10 | + r3ptar's tail every couple of seconds. |
| 11 | + """ |
| 12 | + m = ev3.MediumMotor(); assert m.connected |
| 13 | + |
| 14 | + while True: |
| 15 | + m.run_timed(speed_sp=90, time_sp=1000, stop_action='coast') |
| 16 | + time.sleep(1) |
| 17 | + ev3.Sound.play('rattle-snake.wav').wait() |
| 18 | + m.run_timed(speed_sp=-90, time_sp=1000, stop_action='coast') |
| 19 | + time.sleep(2) |
| 20 | + |
| 21 | +def hand_biter(): |
| 22 | + """ |
| 23 | + This is the second thread of execution. It will constantly poll the |
| 24 | + infrared sensor for proximity info and bite anything that gets too close. |
| 25 | + """ |
| 26 | + m = ev3.LargeMotor('outD'); assert m.connected |
| 27 | + s = ev3.InfraredSensor(); assert s.connected |
| 28 | + |
| 29 | + m.run_timed(speed_sp=-200, time_sp=1000, stop_action='brake') |
| 30 | + |
| 31 | + while True: |
| 32 | + # Wait until something (a hand?!) gets too close: |
| 33 | + while s.proximity() > 30: time.sleep(0.1) |
| 34 | + |
| 35 | + # Bite it! Also, don't forget to hiss: |
| 36 | + ev3.Sound.play('snake-hiss.wav') |
| 37 | + m.run_timed(speed_sp=600, time_sp=500, stop_action='brake') |
| 38 | + time.sleep(0.6) |
| 39 | + m.run_timed(speed_sp=-200, time_sp=500, stop_action='brake') |
| 40 | + time.sleep(1) |
| 41 | + |
| 42 | + |
| 43 | +# Now that we have the worker functions defined, lets start them both in |
| 44 | +# separate threads. |
| 45 | +# Actually, we could run just one of those in a separate thread, and call the |
| 46 | +# other from our own (master) thread, but this way seems to be more symmetric: |
| 47 | +threading.Thread(target=tail_waggler).start() |
| 48 | +threading.Thread(target=hand_biter).start() |
0 commit comments