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, VoltageSensor, CurrentSensor |
7
|
|
|
from bricknil.const import Color |
8
|
|
|
|
9
|
|
|
@attach(CurrentSensor, name='current', capabilities=[('sense_l', 2)]) |
10
|
|
|
@attach(TrainMotor, name='motor') |
11
|
|
|
class Train(PoweredUpHub): |
12
|
|
|
|
13
|
|
|
async def voltage_change(self): |
14
|
|
|
mv = self.voltage.value |
15
|
|
|
self.message_info(f'train voltage {mv}') |
16
|
|
|
|
17
|
|
|
async def current_change(self): |
18
|
|
|
ma = self.current.value |
19
|
|
|
self.message_info(f'train current {ma}') |
20
|
|
|
|
21
|
|
|
async def run(self): |
22
|
|
|
self.message_info("Running") |
23
|
|
|
|
24
|
|
|
await self.motor.ramp_speed(50,3000) |
25
|
|
|
await sleep(60) |
26
|
|
|
|
27
|
|
|
for i in range(10,100,10): |
28
|
|
|
self.message_info(f'ramping speed {i}') |
29
|
|
|
await self.motor.ramp_speed(i, 900) # Ramp to new speed in 0.9 seconds |
30
|
|
|
await sleep(3) |
31
|
|
|
for i in range(100,10,-10): |
32
|
|
|
self.message_info(f'ramping down speed {i}') |
33
|
|
|
await self.motor.ramp_speed(i, 900) # Ramp to new speed in 0.9 seconds |
34
|
|
|
await sleep(3) |
35
|
|
|
|
36
|
|
|
|
37
|
|
|
async def system(): |
38
|
|
|
train = Train('My Train') |
39
|
|
|
|
40
|
|
|
if __name__ == '__main__': |
41
|
|
|
logging.basicConfig(level=logging.INFO) |
42
|
|
|
start(system) |
43
|
|
|
|