Skip to content

Commit d6acf0f

Browse files
committed
R3PTAR demo
1 parent 471b088 commit d6acf0f

File tree

4 files changed

+64
-0
lines changed

4 files changed

+64
-0
lines changed

demo/R3PTAR/README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
R3PTAR
2+
======
3+
4+
One of the most loved robots, the standing 35 cm. / 13,8 inch tall R3PTAR robot
5+
slithers across the floor like a real cobra, and strikes at lightning speed
6+
with it’s pointed red fangs.
7+
8+
Coincidentally, its also a nice example to demonstrate how to use threads in
9+
Python.
10+
11+
**Building instructions**: http://www.lego.com/en-us/mindstorms/build-a-robot/r3ptar
12+
13+
**Resources**:
14+
15+
* `rattle-snake.wav`: https://www.freesound.org/people/7h3_lark/sounds/268580/
16+
* `snake-hiss.wav`: https://www.freesound.org/people/Reitanna/sounds/343928/

demo/R3PTAR/r3ptar.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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()

demo/R3PTAR/rattle-snake.wav

236 KB
Binary file not shown.

demo/R3PTAR/snake-hiss.wav

76 KB
Binary file not shown.

0 commit comments

Comments
 (0)