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