-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathboost_iot_center.py
183 lines (149 loc) · 4.59 KB
/
boost_iot_center.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import asyncio
import datetime
from time import sleep
from influxdb_client import Point
from paho.mqtt import client as mqtt_client
from playsound import playsound
from pylgbst import logging, get_connection_bleak
from pylgbst.hub import MoveHub, VisionSensor
from pylgbst.peripherals import EncodedMotor, TiltSensor
from lego_utils import auto_search
# MQTT settings
broker = "0.0.0.0"
port = 1883
topic = "iot_center"
def connect_mqtt():
def on_connect(rc):
if rc == 0:
print("Connected to MQTT Broker!")
else:
print("Failed to connect, return code %d\n", rc)
client2 = mqtt_client.Client()
client2.on_connect = on_connect
client2.connect(broker, port)
return client2
def send(data):
p = Point("environment") \
.tag("clientId", "lego_boost") \
.time(datetime.datetime.utcnow())
for key, value in data.items():
p = p.field(key, float(value))
logging.info("> " + p.to_line_protocol())
client_mqtt.publish(topic, p.to_line_protocol())
def run(mhub):
sensor_data = {}
def a_callback(speed):
sensor_data["speed_a"] = speed
def b_callback(speed):
sensor_data["speed_b"] = speed
def axis_callback(x, y, z):
sensor_data["x_axis"] = x
sensor_data["y_axis"] = y
sensor_data["z_axis"] = z
send(sensor_data)
def battery_callback(voltage):
sensor_data["voltage"] = round(voltage, 2)
def vision_callback(distance):
sensor_data["distance"] = distance
mhub.motor_A.subscribe(a_callback, mode=EncodedMotor.SENSOR_SPEED)
mhub.motor_B.subscribe(b_callback, mode=EncodedMotor.SENSOR_SPEED)
mhub.tilt_sensor.subscribe(axis_callback, mode=TiltSensor.MODE_3AXIS_ACCEL)
mhub.vision_sensor.subscribe(vision_callback, mode=VisionSensor.DISTANCE_INCHES)
mhub.voltage.subscribe(battery_callback)
try:
zorba_dance(hub)
except KeyboardInterrupt:
raise KeyboardInterrupt
finally:
mhub.led.unsubscribe(b_callback)
mhub.tilt_sensor.unsubscribe(axis_callback)
mhub.voltage.unsubscribe(battery_callback)
mhub.motor_A.unsubscribe(a_callback)
def zorba_dance(mhub):
maxbl = 60 / 42 # 6O / bpm
minbl = 60 / 160
mins = 0.1
p = 0.2
s = mins # speed (0 - 0.5)
bl = maxbl
def spin():
mhub.motor_A.start_speed(s)
mhub.motor_B.start_speed(-s)
sleep(bl / 2 - p)
mhub.motor_AB.stop()
sleep(p)
mhub.motor_A.start_speed(-s * 2)
mhub.motor_B.start_speed(+s * 2)
sleep(bl / 2 - p)
mhub.motor_AB.stop()
sleep(p)
mhub.motor_A.start_speed(s)
mhub.motor_B.start_speed(-s)
sleep(bl / 2 - p)
mhub.motor_AB.stop()
sleep(p)
def basic(direction): # Two bars
mhub.motor_AB.start_speed(direction * s)
sleep(bl / 2 - p)
mhub.motor_AB.stop()
sleep(p)
spin()
mhub.motor_AB.start_speed(-direction * s)
sleep(bl / 2 - p)
mhub.motor_AB.stop()
sleep(p)
spin()
def spinning(direction):
mhub.motor_A.start_speed(direction * minbl / bl / 1.5)
mhub.motor_B.start_speed(-direction * minbl / bl / 1.5)
sleep(2 * bl - p)
mhub.motor_AB.stop()
sleep(p)
# Zorba Dance version (https://musescore.com/user/14008706/scores/4866772)
playsound('Zorba_s_Dance_Sirrtaki.mp3', block=False)
sleep(5)
for x in range(3):
basic(1)
for x in range(1):
basic(-1)
for x in range(4):
basic(1)
for x in range(2):
basic(-1)
for x in range(5):
basic(1)
bl = 60 / 48
s = mins / (bl / maxbl)
for x in range(5):
basic(1)
bl = 60 / 68
s = mins / (bl / maxbl)
for x in range(2):
spinning(1)
for x in range(4):
spinning(-1)
for x in range(4):
spinning(1)
for x in range(6):
spinning(-1)
for x in range(10):
spinning(1)
for x in range(2):
spinning(-1)
if __name__ == '__main__':
logging.basicConfig(level=logging.INFO, format='%(relativeCreated)d\t%(levelname)s\t%(name)s\t%(message)s')
parameters = {}
hub = None
try:
client_mqtt = connect_mqtt()
name, UUID = asyncio.run(auto_search())
connection = get_connection_bleak(hub_mac=str(UUID), hub_name=str(name))
parameters['connection'] = connection
hub = MoveHub(**parameters)
logging.info("Running Demo...")
run(hub)
except Exception as e:
print(e)
finally:
if hub is not None:
hub.disconnect()