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