|
1
|
|
|
import logging |
|
2
|
|
|
|
|
3
|
|
|
from curio import sleep, Queue |
|
4
|
|
|
from bricknil import attach, start |
|
5
|
|
|
from bricknil.hub import PoweredUpRemote, BoostHub |
|
6
|
|
|
from bricknil.sensor import InternalMotor, RemoteButtons, LED, Button, ExternalMotor |
|
7
|
|
|
from bricknil.process import Process |
|
8
|
|
|
from bricknil.const import Color |
|
9
|
|
|
|
|
10
|
|
|
from random import randint |
|
11
|
|
|
|
|
12
|
|
|
@attach(LED, name='led') |
|
13
|
|
|
@attach(ExternalMotor, name='motor') |
|
14
|
|
|
class Robot(BoostHub): |
|
15
|
|
|
""" Rotate the external motor connected to a boost hub by degrees |
|
16
|
|
|
|
|
17
|
|
|
Demonstrate both the absolute positioning as well as relative rotation. |
|
18
|
|
|
""" |
|
19
|
|
|
|
|
20
|
|
|
async def run(self): |
|
21
|
|
|
self.message("Running") |
|
22
|
|
|
|
|
23
|
|
|
# Set the robot LED to green to show we're ready |
|
24
|
|
|
await self.led.set_color(Color.green) |
|
25
|
|
|
await sleep(2) |
|
26
|
|
|
|
|
27
|
|
|
# The powered on position becomes the 12 o'clock reference point |
|
28
|
|
|
|
|
29
|
|
|
for i in range(5): |
|
30
|
|
|
# Turn to 11 o'clock position |
|
31
|
|
|
await self.led.set_color(Color.blue) |
|
32
|
|
|
await self.motor.set_pos(-30, speed=50) |
|
33
|
|
|
await sleep(2) |
|
34
|
|
|
await self.led.set_color(Color.red) |
|
35
|
|
|
# Turn to 1 o'clock position |
|
36
|
|
|
await self.motor.set_pos(30, speed=30) |
|
37
|
|
|
await sleep(2) |
|
38
|
|
|
await self.led.set_color(Color.yellow) |
|
39
|
|
|
# Turn to 3 o'clock position |
|
40
|
|
|
await self.motor.set_pos(90, speed=20) |
|
41
|
|
|
await sleep(2) |
|
42
|
|
|
|
|
43
|
|
|
# Rotate a random amount of degrees from 1 to 180 |
|
44
|
|
|
await self.led.set_color(Color.purple) |
|
45
|
|
|
await self.motor.rotate(randint(1,180), speed=10) |
|
46
|
|
|
await sleep(3) |
|
47
|
|
|
|
|
48
|
|
|
# Then reset to 12 o'clock position |
|
49
|
|
|
await self.led.set_color(Color.green) |
|
50
|
|
|
await self.motor.set_pos(0) |
|
51
|
|
|
await sleep(2) |
|
52
|
|
|
|
|
53
|
|
|
|
|
54
|
|
|
|
|
55
|
|
|
async def system(): |
|
56
|
|
|
robot = Robot('Vernie') |
|
57
|
|
|
|
|
58
|
|
|
if __name__ == '__main__': |
|
59
|
|
|
logging.basicConfig(level=logging.DEBUG) |
|
60
|
|
|
start(system) |
|
61
|
|
|
|