|
1
|
|
|
|
|
2
|
|
|
from itertools import cycle |
|
3
|
|
|
from curio import sleep |
|
4
|
|
|
from bricknil import attach, start |
|
5
|
|
|
from bricknil.hub import DuploTrainHub |
|
6
|
|
|
from bricknil.sensor import DuploTrainMotor, DuploSpeedSensor, LED, DuploVisionSensor, DuploSpeaker, Button |
|
7
|
|
|
from bricknil.const import Color |
|
8
|
|
|
import logging |
|
9
|
|
|
|
|
10
|
|
|
@attach(DuploSpeaker, name='speaker') |
|
11
|
|
|
@attach(DuploVisionSensor, name='vision_sensor', capabilities=['sense_color', 'sense_ctag', 'sense_reflectivity']) |
|
12
|
|
|
@attach(LED, name='led') |
|
13
|
|
|
@attach(DuploSpeedSensor, name='speed_sensor', capabilities=['sense_speed', 'sense_count']) |
|
14
|
|
|
@attach(DuploTrainMotor, name='motor') |
|
15
|
|
|
class Train(DuploTrainHub): |
|
16
|
|
|
|
|
17
|
|
|
def __init__(self, *args, **kwargs): |
|
18
|
|
|
super().__init__(*args, **kwargs) |
|
19
|
|
|
self.go = False # Only becomes true with hub button is pressed |
|
20
|
|
|
|
|
21
|
|
|
async def speed_sensor_change(self): |
|
22
|
|
|
speed = self.speed_sensor.value[DuploSpeedSensor.capability.sense_speed] |
|
23
|
|
|
if not self.go and speed > 0: |
|
24
|
|
|
self.go = True |
|
25
|
|
|
self.message_info('Movement detected: starting...') |
|
26
|
|
|
elif self.go: |
|
27
|
|
|
count = self.speed_sensor.value[DuploSpeedSensor.capability.sense_count] |
|
28
|
|
|
self.message_info(f'Speed sensor changed speed: {speed} count: {count}') |
|
29
|
|
|
|
|
30
|
|
|
async def vision_sensor_change(self): |
|
31
|
|
|
cap = DuploVisionSensor.capability |
|
32
|
|
|
color = self.vision_sensor.value[cap.sense_color] |
|
33
|
|
|
ctag = self.vision_sensor.value[cap.sense_ctag] |
|
34
|
|
|
reflt = self.vision_sensor.value[cap.sense_reflectivity] |
|
35
|
|
|
if self.go: |
|
36
|
|
|
self.message_info(f'Vision sensor changed color: {color} ctag: {ctag} reflt: {reflt}') |
|
37
|
|
|
|
|
38
|
|
|
async def run(self): |
|
39
|
|
|
self.message_info("Running") |
|
40
|
|
|
|
|
41
|
|
|
colors = cycle([Color.red, Color.purple, Color.yellow, Color.blue, Color.white]) |
|
42
|
|
|
|
|
43
|
|
|
snd = DuploSpeaker.sounds |
|
44
|
|
|
sounds = cycle([snd.brake, snd.station, snd.water, snd.horn, snd.steam]) |
|
45
|
|
|
|
|
46
|
|
|
self.message_info('Please move the train to start the program') |
|
47
|
|
|
while not self.go: |
|
48
|
|
|
await self.led.set_color(next(colors)) |
|
49
|
|
|
await sleep(0.1) |
|
50
|
|
|
|
|
51
|
|
|
for i in range(5): |
|
52
|
|
|
await self.led.set_color(next(colors)) # Cycle through the colors |
|
53
|
|
|
await self.speaker.play_sound(next(sounds)) # cycle through the sounds |
|
54
|
|
|
tgt_speed = 20 + i*15 # Keep increasing the speed |
|
55
|
|
|
await self.motor.ramp_speed(tgt_speed, 2000) |
|
56
|
|
|
self.message_info(f"Set speed to {i}") |
|
57
|
|
|
await sleep(3) |
|
58
|
|
|
|
|
59
|
|
|
self.message_info("Done") |
|
60
|
|
|
|
|
61
|
|
|
async def system(): |
|
62
|
|
|
hub = Train('train') |
|
63
|
|
|
|
|
64
|
|
|
if __name__ == '__main__': |
|
65
|
|
|
logging.basicConfig(level=logging.INFO) |
|
66
|
|
|
start(system) |
|
67
|
|
|
|