1
|
|
|
import logging, struct |
2
|
|
|
|
3
|
|
|
from curio import sleep, Queue |
4
|
|
|
from bricknil import attach, start |
5
|
|
|
from bricknil.hub import BoostHub |
6
|
|
|
from bricknil.sensor import LED, ExternalMotionSensor, ExternalTiltSensor, WedoMotor |
7
|
|
|
from bricknil.process import Process |
8
|
|
|
from bricknil.const import Color |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
tilt_cap = ExternalTiltSensor.capability.sense_impact |
12
|
|
|
motion_cap = ExternalMotionSensor.capability.sense_distance |
13
|
|
|
|
14
|
|
|
@attach(LED, name='led') |
15
|
|
|
@attach(ExternalTiltSensor, name='tilt_sensor', capabilities=[tilt_cap]) |
16
|
|
|
@attach(WedoMotor, name='motor') |
17
|
|
|
#@attach(ExternalMotionSensor, name='motion_sensor', capabilities=[motion_cap]) |
18
|
|
|
#@@attach(ExternalMotor, name='motor') |
19
|
|
|
class Robot(BoostHub): |
20
|
|
|
""" Rotate the external motor connected to a boost hub by degrees |
21
|
|
|
|
22
|
|
|
Demonstrate both the absolute positioning as well as relative rotation. |
23
|
|
|
""" |
24
|
|
|
|
25
|
|
|
async def motion_sensor_change(self): |
26
|
|
|
new_value = self.motion_sensor.value[motion_cap] |
27
|
|
|
print(f'motion sensor changed {new_value}') |
28
|
|
|
|
29
|
|
|
async def tilt_sensor_change(self): |
30
|
|
|
new_value = self.tilt_sensor.value[tilt_cap] |
31
|
|
|
if tilt_cap == ExternalTiltSensor.capability.sense_angle: |
32
|
|
|
x_angle = new_value[0] |
33
|
|
|
y_angle = new_value[1] |
34
|
|
|
print(f'tilt sensor changed {x_angle}, {y_angle}') |
35
|
|
|
elif tilt_cap == ExternalTiltSensor.capability.sense_orientation: |
36
|
|
|
print(f'orientation {new_value}') |
37
|
|
|
elif tilt_cap == ExternalTiltSensor.capability.sense_impact: |
38
|
|
|
print(f'impacts {new_value}') |
39
|
|
|
|
40
|
|
|
async def run(self): |
41
|
|
|
self.message("Running") |
42
|
|
|
|
43
|
|
|
# Set the robot LED to green to show we're ready |
44
|
|
|
await self.led.set_color(Color.green) |
45
|
|
|
await sleep(2) |
46
|
|
|
|
47
|
|
|
|
48
|
|
|
while True: |
49
|
|
|
await self.led.set_color(Color.blue) |
50
|
|
|
await self.motor.ramp_speed(50, 2000) |
51
|
|
|
await sleep(3) |
52
|
|
|
await self.led.set_color(Color.white) |
53
|
|
|
await self.motor.ramp_speed(-50, 2000) |
54
|
|
|
await sleep(3) |
55
|
|
|
|
56
|
|
|
|
57
|
|
|
async def system(): |
58
|
|
|
robot = Robot('Vernie') |
59
|
|
|
|
60
|
|
|
if __name__ == '__main__': |
61
|
|
|
logging.basicConfig(level=logging.INFO) |
62
|
|
|
start(system) |
63
|
|
|
|