1
|
|
|
import logging |
2
|
|
|
from itertools import cycle |
3
|
|
|
from curio import sleep |
4
|
|
|
from bricknil import attach, start |
5
|
|
|
from bricknil.hub import PoweredUpHub |
6
|
|
|
from bricknil.sensor import TrainMotor, VisionSensor, Button, LED |
7
|
|
|
from bricknil.process import Process |
8
|
|
|
from bricknil.const import Color |
9
|
|
|
from random import randint |
10
|
|
|
|
11
|
|
|
@attach(Button, name='train_btn', capabilities=['sense_press']) |
12
|
|
|
@attach(LED, name='train_led') |
13
|
|
|
@attach(VisionSensor, name='train_sensor', capabilities=['sense_color', 'sense_reflectivity']) |
14
|
|
|
@attach(TrainMotor, name='motor') |
15
|
|
|
class Train(PoweredUpHub): |
16
|
|
|
|
17
|
|
|
def __init__(self, name): |
18
|
|
|
self.go = False |
19
|
|
|
super().__init__(name) |
20
|
|
|
|
21
|
|
|
async def train_btn_change(self): |
22
|
|
|
self.message_info(f'train button push {self.train_btn.value}') |
23
|
|
|
btn = self.train_btn.value[Button.capability.sense_press] |
24
|
|
|
if btn == 1 and not self.go: |
25
|
|
|
# Pushed! |
26
|
|
|
self.go = True |
27
|
|
|
elif btn==1 and self.go: |
28
|
|
|
self.go = False |
29
|
|
|
|
30
|
|
|
|
31
|
|
|
async def train_sensor_change(self): |
32
|
|
|
#self.message_info(f'Train sensor value change {self.train_sensor.value}') |
33
|
|
|
refl = self.train_sensor.value[VisionSensor.capability.sense_reflectivity] |
34
|
|
|
if refl >18: |
35
|
|
|
self.message_info('Switch!') |
36
|
|
|
color = self.train_sensor.value[VisionSensor.capability.sense_color] |
37
|
|
|
c = Color(color) |
38
|
|
|
if c == Color.blue: |
39
|
|
|
self.message_info('Blue') |
40
|
|
|
self.slow = False |
41
|
|
|
elif c == Color.yellow: |
42
|
|
|
self.message_info('Yellow') |
43
|
|
|
self.slow = True |
44
|
|
|
|
45
|
|
|
|
46
|
|
|
#count = self.train_sensor.value[VisionSensor.capability.sense_count] |
47
|
|
|
#self.message_info(f'Count {count}') |
48
|
|
|
|
49
|
|
|
|
50
|
|
|
async def run(self): |
51
|
|
|
self.message_info("Running") |
52
|
|
|
self.motor_speed = 0 |
53
|
|
|
self.go = False |
54
|
|
|
slow = 40 |
55
|
|
|
fast = 70 |
56
|
|
|
|
57
|
|
|
# Blink the color from purple and yellow |
58
|
|
|
colors = cycle([Color.purple, Color.yellow]) |
59
|
|
|
while not self.go: # Wait until the hub button is pushed |
60
|
|
|
await self.train_led.set_color(next(colors)) |
61
|
|
|
await sleep(1) |
62
|
|
|
|
63
|
|
|
colors = cycle([Color.green, Color.orange]) |
64
|
|
|
# Ready to go, let's change the color to green! |
65
|
|
|
await self.motor.ramp_speed(fast, 2000) |
66
|
|
|
self.slow = False |
67
|
|
|
while self.go: |
68
|
|
|
#speed = randint(30,30) |
69
|
|
|
#await self.motor.ramp_speed(speed, 2000) |
70
|
|
|
await self.train_led.set_color(next(colors)) |
71
|
|
|
if self.slow: |
72
|
|
|
await self.motor.ramp_speed(slow, 2000) |
73
|
|
|
await self.train_led.set_color(Color.red) |
74
|
|
|
while self.slow: |
75
|
|
|
await sleep(1) |
76
|
|
|
await self.motor.ramp_speed(fast, 2000) |
77
|
|
|
await sleep(1) |
78
|
|
|
|
79
|
|
|
async def system(): |
80
|
|
|
train = Train('My Train') |
81
|
|
|
|
82
|
|
|
if __name__ == '__main__': |
83
|
|
|
logging.basicConfig(level=logging.INFO) |
84
|
|
|
start(system) |
85
|
|
|
|