-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
52 lines (41 loc) · 1.36 KB
/
train.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
from curio import sleep
from bricknil import attach, start
from bricknil.hub import PoweredUpHub
from bricknil.sensor import TrainMotor
import logging
@attach(TrainMotor, name="motor")
class Train(PoweredUpHub):
def __init__(self, *args, **kwargs):
"""
Initialize the Train object.
Args:
*args: Variable length argument list.
**kwargs: Arbitrary keyword arguments.
"""
super().__init__(*args, **kwargs)
self.file_vitesse = "speed.txt"
async def run(self):
"""
Run the train.
Reads the speed from a file and sets the motor speed accordingly.
"""
start_vitesse = 0
self.message_info("Running")
while True:
try:
with open(self.file_vitesse, "r") as f:
vitesse = int(f.read())
self.message_info("vitesse {}".format(vitesse))
if vitesse != start_vitesse:
self.message_info("change speed")
start_vitesse = vitesse
await self.motor.set_speed(vitesse)
await sleep(0.1)
except Exception as e:
logging.error(f"Error: {e}")
pass
async def system():
Train("My train")
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)
start(system)