Please help me with my project
Hello there,
Can someone help me with my project?
Here's the list I want my MMR to do...
1. I want MMR to be on all the time by charging its battery with wireless charging station, so when it's taken out it can operate immediately.
2. I want my script to automatically detect and connect all the time in case bluetooth signal is low and it gets disconnected.
3. MMR should know when it's charging or not, so It can be used later.
I can't find any tutorials about what I want. Please help or guide me through specific documentation topic.
Thank you in advance,
Warut.
Here's my current code:
`from future import print_function
from mbientlab.metawear import MetaWear, libmetawear, parse_value
from mbientlab.metawear.cbindings import *
from threading import Event
from pythonosc import udp_client
from time import sleep
import sys
import array
import argparse
import random
import time
import numpy as np
d1 = "FD:1B:FC:47:C1:20"
d2 = "DC:9D:9E:7B:CB:2C"
d3 = "F2:8D:2C:A8:05:7D"
lightTreshold = 1.2
mediumTreshold = 1.6
highTreshold = 2.0
yLightTreshold = 0.38
zLightTreshold = 0.08
frameHis = 10
minVol = 0.1
maxVolLight = 0.5
maxVolMed = 0.75
maxVolHigh = 1.0
lightSpeed = 0.003
mediumSpeed = 0.004
highSpeed = 0.006
volDelay = 60
adjustDelay = 15
global currentVol
global currentMaxVol
global volDownDelay
global battery
currentVol = 0.0
currentMaxVol = 0.5
volDownDelay = 0
volAdjustDelay = 0
battery = 100
maxVolDelay = 15.0
address = []
address.append(d1)
client = udp_client.SimpleUDPClient("127.0.0.1", 7001)
resolume = udp_client.SimpleUDPClient("127.0.0.1", 7003)
fps = 30
second = 60
minute = 5
max_frames = fps * second * minute
current_frame = 1
magHis = []
prevMag = 0.0
currentMag = 0.0
tries = 5
sensors = []
print("osc sent")
time.sleep(1)
class Sensor:
def init(self, device):
self.device = device
self.samples = 0
self.callback = FnVoid_VoidP_DataP(self.data_handler)
self.battery_callback = FnVoid_VoidP_DataP(self.battery_handler)
self.magnitude = 0.0
def battery_handler(self, ctx, data):
print("Voltage: {0}, Charge: {1}".format(
data[0], data[1]))
def data_handler(self, ctx, data):
self.calculate(parse_value(data).x, parse_value(data).y, parse_value(data).z)
def calculate(self, x_data, y_data, z_data):
self.magnitude = np.sqrt(x_data**2 + y_data**2 + z_data**2)
def get_magnitude(self):
return self.magnitude
def sensor_off():
for s in sensors:
libmetawear.mbl_mw_led_stop(s.device.board)
libmetawear.mbl_mw_acc_stop(s.device.board)
libmetawear.mbl_mw_acc_disable_acceleration_sampling(s.device.board)
signal = libmetawear.mbl_mw_acc_get_acceleration_data_signal(s.device.board)
libmetawear.mbl_mw_datasignal_unsubscribe(signal)
libmetawear.mbl_mw_debug_disconnect(s.device.board)
print(d.address + " has been disconnected")
def battery_calback(data):
print("Voltage: {0}, Charge: {1}".format(data[0], data[1]))
def sensor_calc():
if currentMag < lightTreshold:
vol_handler(False, None, mediumSpeed)
elif currentMag < mediumTreshold:
vol_handler(True, "light", lightSpeed)
elif currentMag < highTreshold:
vol_handler(True, "med", mediumSpeed)
else:
vol_handler(True, "high", highSpeed)
def vol_handler(conducting, pace, speed):
global currentVol
global currentMaxVol
global volDownDelay
global volAdjustDelay
if pace == "light":
currentMaxVol = maxVolLight
elif pace == "med":
currentMaxVol = maxVolMed
elif pace == "high":
currentMaxVol = maxVolHigh
else:
currentMaxVol = minVol
if conducting is True:
client.send_message("/metawear", 1)
volDownDelay = 0
if currentVol <= currentMaxVol:
volAdjustDelay = 0
if currentVol < maxVolLight:
speed = highSpeed * 2
if currentVol + speed >= currentMaxVol:
currentVol = currentMaxVol
else:
currentVol += speed
else:
if volAdjustDelay >= adjustDelay:
if currentVol <= currentMaxVol:
currentVol = currentMaxVol
else:
currentVol -= speed * 2
else:
volAdjustDelay += 1
elif conducting is False:
client.send_message("/metawear", 0)
if volDownDelay >= volDelay:
if currentVol <= minVol:
currentVol = minVol
else:
if currentVol < maxVolLight:
speed = highSpeed
currentVol -= speed
else:
volDownDelay += 1
print(currentVol)
resolume.send_message("/composition/audio/volume", currentVol)
for i in range(len(address)):
##print(address[i])
d = MetaWear(address[i])
for attempt in range(tries):
try:
d.connect()
except:
print("GOT ERROR")
if i < tries - 1:
print("RETRYING")
continue
else:
print("CANNOT RETRY ANYMORE")
raise
break
print("Connected to stick no." + str(i + 1) + ": " + d.address)
sensors.append(Sensor(d))
for s in sensors:
libmetawear.mbl_mw_settings_set_connection_parameters(s.device.board, 7.5, 7.5, 0, 6000)
sleep(1.5)
libmetawear.mbl_mw_acc_set_odr(s.device.board, 30.0)
libmetawear.mbl_mw_acc_set_range(s.device.board, 16.0)
libmetawear.mbl_mw_acc_write_acceleration_config(s.device.board)
signal = libmetawear.mbl_mw_acc_get_acceleration_data_signal(s.device.board)
libmetawear.mbl_mw_datasignal_subscribe(signal, None, s.callback)
libmetawear.mbl_mw_acc_enable_acceleration_sampling(s.device.board)
libmetawear.mbl_mw_acc_start(s.device.board)
sleep(1.5)
#battery_data = libmetawear.mbl_mw_settings_get_battery_state_data_signal(s.device.board)
#voltage = libmetawear.mbl_mw_datasignal_get_component(battery_data, Const.SETTINGS_BATTERY_VOLTAGE_INDEX)
#charge = libmetawear.mbl_mw_datasignal_get_component(battery_data, Const.SETTINGS_BATTERY_CHARGE_INDEX)
#libmetawear.mbl_mw_datasignal_subscribe(charge, None, self.sensor_data_handler)
#libmetawear.mbl_mw_datasignal_read(voltage)
#libmetawear.mbl_mw_datasignal_read(charge)
pattern= LedPattern(repeat_count= Const.LED_REPEAT_INDEFINITELY)
libmetawear.mbl_mw_led_load_preset_pattern(byref(pattern), LedPreset.SOLID)
libmetawear.mbl_mw_led_write_pattern(s.device.board, byref(pattern), LedColor.GREEN)
libmetawear.mbl_mw_led_play(s.device.board)
sleep(1.5)
while current_frame <= max_frames:
try:
start = time.time()
for s in sensors:
currentMag = s.get_magnitude()
sensor_calc()
#print(s.get_magnitude())
current_frame += 1
time.sleep(max(1./fps - (time.time() - start), 0))
except (KeyboardInterrupt, SystemExit):
sensor_off()
sleep(1.5)
raise
sensor_off()
`
Comments
Try to do things one at a time (led, then read the voltage...). Don't try to do it all at once. Once each element works, put it all together.
This is best practice for development and debug.