def haptic(): c = MetaWearClient(str(address), debug=True) print("New client created: {0}".format(c)) # time.sleep(1) # c.led.load_preset_pattern('blink', repeat_count=10) time.sleep(1) c.haptic.start_buzzer(100) c.accelerometer.set_settings(data_rate=50, data_range=8) def acc_callback(c, data): """Handle a (epoch, (x,y,z)) accelerometer tuple.""" com = sum(extract_data(data['value'])) if com > 3: c.haptic.start_motor(100, 1000) print("\n\n\n\n\n\n") print("com", com) print("\n\n\n\n\n\n") elif np.random.random() > 0.9: print("com", com) c.accelerometer.notifications(lambda x: acc_callback(c, x)) time.sleep(60) c.led.load_preset_pattern('blink', repeat_count=10) c.haptic.start_buzzer(100) time.sleep(2) c.disconnect()
def blink_10(): c = MetaWearClient(str(address), debug=True) print("New client created: {0}".format(c)) print("Blinking 10 times with green LED...") pattern = c.led.load_preset_pattern('blink', repeat_count=10) c.led.write_pattern(pattern, 'g') c.led.play() time.sleep(5.0) c.disconnect()
def run(self): while not self._stop: try: m = MetaWearClient( str(self._address), connect=True, debug=False) self._device = m self._model_name = self.get_model_name() except: logging.info('Retry connect to ' + self._address) time.sleep(1) continue break logging.info("New metawear connected: {0}".format(m)) # high frequency throughput connection setup m.settings.set_connection_parameters(7.5, 7.5, 0, 6000) # Up to 4dB for Class 2 BLE devices # https://github.com/hbldh/pymetawear/blob/master/pymetawear/modules/settings.py # https://mbientlab.com/documents/metawear/cpp/0/settings_8h.html#a335f712d5fc0587eff9671b8b105d3ed # Hossain AKMM, Soh WS. A comprehensive study of Bluetooth signal parameters for localization. 2007 Ieee 18th International Symposium on Personal, Indoor and Mobile Radio Communications, Vols 1-9. 2007:428-32. m.settings.set_tx_power(power=4) m.accelerometer.set_settings( data_rate=self._accel_sr, data_range=self._accel_grange) m.accelerometer.high_frequency_stream = True m.accelerometer.notifications(callback=self._accel_handler) m.settings.notifications(callback=self._battery_handler) self._status = "Stream" while not self._stop: time.sleep(1) m.settings.read_battery_state() return self
def log(): client = MetaWearClient(str(address), debug=False) print("New client created: {0}".format(client)) settings = client.accelerometer.get_possible_settings() print("Possible accelerometer settings of client:") for k, v in settings.items(): print(k, v) print("Write accelerometer settings...") client.accelerometer.set_settings(data_rate=400, data_range=4.0) settings = client.accelerometer.get_current_settings() print("Accelerometer settings of client: {0}".format(settings)) client.accelerometer.high_frequency_stream = False client.accelerometer.start_logging() print("Logging accelerometer data...") for i in range(5): time.sleep(1.0) print(i) client.accelerometer.stop_logging() print("Logging stopped.") print("Downloading data...") download_done = False n = 0 data = None while (not download_done) and n < 3: try: data = client.accelerometer.download_log() download_done = True except PyMetaWearDownloadTimeout: print("Download of log interrupted. Trying to reconnect...") client.disconnect() client.connect() n += 1 if data is None: raise PyMetaWearException("Download of logging data failed.") print("Disconnecting...") client.disconnect() with open('data.txt', 'w') as f: for d in data: f.write(str(d) + "\n")
def run(): """Example of how to run the Complimentary filter.""" address = select_device() c = MetaWearClient(str(address), 'pygatt', timeout=10, debug=False) print("New client created: {0}".format(c)) f = ComplimentaryFilter(0.075, 50.0) print("Write accelerometer settings...") c.accelerometer.set_settings(data_rate=50.0, data_range=4.0) c.gyroscope.set_settings(data_rate=50.0, data_range=250.0) print("Subscribing to accelerometer signal notifications...") c.accelerometer.high_frequency_stream = False c.accelerometer.notifications(f.add_accelerometer_data) c.gyroscope.notifications(f.add_gyroscope_data) time.sleep(10.0) print("Unsubscribe to notification...") c.accelerometer.notifications(None) c.gyroscope.notifications(None) c.disconnect() return f
def _setup_metawear(self, addr): try: self._device = MetaWearClient(addr, connect=True, debug=False) self._device_name = self.get_device_name() except Exception as e: logging.error(str(e)) logging.info('Retry connect to ' + addr) time.sleep(1) logging.info("New metawear connected: {0}".format(self._device)) # high frequency throughput connection setup self._device.settings.set_connection_parameters(7.5, 7.5, 0, 6000) # Up to 4dB for Class 2 BLE devices # https://github.com/hbldh/pymetawear/blob/master/pymetawear/modules/settings.py # https://mbientlab.com/documents/metawear/cpp/0/settings_8h.html#a335f712d5fc0587eff9671b8b105d3ed # Hossain AKMM, Soh WS. A comprehensive study of Bluetooth signal parameters for localization. 2007 Ieee 18th International Symposium on Personal, Indoor and Mobile Radio Communications, Vols 1-9. 2007:428-32. self._device.settings.set_tx_power(power=4) self._device.accelerometer.set_settings(data_rate=self._sr, data_range=self._grange) self._device.accelerometer.high_frequency_stream = True
print(f"Committing {label} data to database...") s = Session() s.add_all([ body_acc_x, body_acc_y, body_acc_z, body_gyr_x, body_gyr_y, body_gyr_z ]) s.commit() print("Finished!") s.close() if __name__ == "__main__": # Create a MetaWear device d = MetaWearClient('EE:50:E7:BF:21:83') # Substitute with your MAC exercise = int( input("\nChoose a number for an exercise below\n" "1. Jumping Jacks\n" "2. Squats\n" "3. Jogs\n" "4. Body Stretch(Arms)\n")) while exercise not in [1, 2, 3, 4]: exercise = int( input("\nChoose a number for an exercise below\n" "1. Jumping Jacks\n" "2. Squats\n" "3. Jogs\n" "4. Body Stretch(Arms)\n"))
#Block - pin mappings block_pin = {2: [block.DIRT.id, "DIRT"], 3: [block.WOOD.id, "WOOD"], 4: [block.COBBLESTONE.id, "COBBLESTONE"], 5: [block.GLASS.id, "GLASS"], 6: [block.DIAMOND_BLOCK.id, "DIAMOND"], 13:[block.STONE.id, "STONE"], 19:[block.TNT.id, "TNT"] } # Initialize metawear backend = 'pygatt' # Or 'pybluez' while True: try: c = MetaWearClient('D5:05:98:AF:47:1D', backend) time.sleep(1.0) break except: mc.postToChat(" Connecting to Wand...") acc_buf = RingBuffer(20) # magnitude acc_xbuf = RingBuffer(20) acc_ybuf = RingBuffer(20) acc_zbuf = RingBuffer(20) gyr_buf = RingBuffer(20) # magnitude gyr_xbuf = RingBuffer(20) gyr_ybuf = RingBuffer(20) gyr_zbuf = RingBuffer(20) last_move_time = 0 last_spin_time = 0
x_data = float(values[4]) y_data = float(values[6]) z_data = float(values[8]) self.current2 = np.hstack((x_data, y_data, z_data)) self.buffer2[self.jj] = self.current2 self.df2 = pd.DataFrame(self.buffer2) self.df2.columns = ['X-data', 'Y-data', 'Z-data'] self.jj += 1 if __name__ == '__main__': # Set a LE Bluetooth device1 with given MAC address as client address1 = select_device() device1 = MetaWearClient(str(address1), debug=True) ''' Set accelerometer settings to preset values Possible settings are: rate in Hz: 0.78125, 1.5625, 3.125, 6.25, 12.5, 25.0, 50.0, 100.0, 200.0, 400.0, 800.0, 1600.0 range in Gs: 2, 4, 8, 16 For streaming data the rate is only possible up to 100 Hz. ''' device1.accelerometer.set_settings(data_rate=12.5) device1.accelerometer.set_settings(data_range=4.0) ''' Set gyroscope settings to preset values Possible settings are: rate in Hz: 25, 50, 100, 200, 400, 800, 1600, 3200 range in Gs: 125, 250, 500, 1000, 2000 ''' device1.gyroscope.set_settings(data_rate=25)
Created by hbldh <*****@*****.**> Created on 2016-04-10 """ from __future__ import division from __future__ import print_function from __future__ import absolute_import import time from pymetawear.discover import select_device from pymetawear.client import MetaWearClient address = select_device() c = MetaWearClient(str(address), 'pygatt', debug=True) print("New client created: {0}".format(c)) def acc_callback(data): """Handle a (epoch, (x,y,z)) accelerometer tuple.""" print("Epoch time: [{0}] - X: {1}, Y: {2}, Z: {3}".format( data[0], *data[1])) print("Write accelerometer settings...") c.accelerometer.set_settings(data_rate=100.0, data_range=4.0) print("Subscribing to accelerometer signal notifications...") c.accelerometer.high_frequency_stream = False c.accelerometer.notifications(acc_callback)
try: adapter = sys.argv[1] device_key = sys.argv[2] address = config['devices'][device_key] except Exception as e: print(e) print("[ADAPTER#] [DEVICE#] --via config.yaml") exit() monitor = MonitorSender(adapter, device_key) rater = Rater(monitor) log.info("Using %s to connect to %s..." % (adapter, address)) while True: try: c = MetaWearClient(address, 'pygatt', debug=False, adapter=adapter) log.info("--> MetaWear initialized: {0}".format(c)) log.info(c.accelerometer) ## setup # c.accelerometer.set_settings(data_rate=100.0, data_range=2.0) # c.soft_reset() # c.disconnect() # time.sleep(4) # exit() except Exception as e: log.error(log.exc(e)) log.warning("Retrying...") else: break log.info("Blinking 10 times...")
Created on 2018-04-20 """ from __future__ import division from __future__ import print_function from __future__ import absolute_import import time from pymetawear.discover import select_device from pymetawear.client import MetaWearClient address = select_device() client = MetaWearClient(str(address), debug=False) print("New client created: {0}".format(client)) settings = client.magnetometer.get_possible_settings() print("Possible magnetometer settings of client:") for k, v in settings.items(): print(k, v) print("Write magnetometer settings...") client.magnetometer.set_settings(power_preset='REGULAR') # Not implemented yet... # settings = client.magnetometer.get_current_settings() # print("Magnetometer settings of client: {0}".format(settings)) time.sleep(0.2)
from __future__ import division from __future__ import print_function from __future__ import absolute_import from coSMIC_SensDataProc import SensDataProc import parallel import time from pymetawear.client import MetaWearClient address1 = 'E3:53:A4:26:93:0F' address2 = 'F4:94:79:03:D2:93' useTwoClients = True client1 = MetaWearClient(str(address1), debug=False) print("New client created: {0}".format(client1)) if useTwoClients: client2 = MetaWearClient(str(address2), debug=False) print("New client created: {0} \n".format(client2)) print("Get possible accelerometer settings of client 1...") settings = client1.accelerometer.get_possible_settings() print(settings) if useTwoClients: print("Get possible accelerometer settings of client 2...") settings = client2.accelerometer.get_possible_settings() print(settings)
Created on 2016-04-02 """ from __future__ import division from __future__ import print_function from __future__ import absolute_import import time from pymetawear.client import MetaWearClient address_1 = 'D1:75:74:0B:59:1F' address_2 = 'F1:D9:71:7E:34:7A' print("Connect to {0}...".format(address_1)) client_1 = MetaWearClient(str(address_1), debug=True) print("New client created: {0}".format(client_1)) print("Connect to {0}...".format(address_2)) client_2 = MetaWearClient(str(address_2), debug=True) print("New client created: {0}".format(client_2)) print("Blinking 10 times with green LED on client 1...") pattern = client_1.led.load_preset_pattern('blink', repeat_count=10) client_1.led.write_pattern(pattern, 'g') client_1.led.play() print("Blinking 10 times with red LED on client 2...") pattern = client_2.led.load_preset_pattern('blink', repeat_count=10) client_2.led.write_pattern(pattern, 'r') client_2.led.play()
if callback is None: self.stop() super(GpioModule, self).notifications(None) else: super(GpioModule, self).notifications(data_handler(callback)) self.start() def start(self, pin=None): """Switches the gpio to active mode.""" libmetawear.mbl_mw_gpio_start_pin_monitoring(self.board, pin) def stop(self, pin=None): """Switches the gpio to standby mode.""" libmetawear.mbl_mw_gpio_stop_pin_monitoring(self.board, pin) client = MetaWearClient("CC:3E:36:3A:4B:50", debug=True) gp = GpioModule( client.board, libmetawear.mbl_mw_metawearboard_lookup_module( client.board, modules.Modules.MBL_MW_MODULE_GPIO)) settings = gp.get_possible_settings() print("Possible accelerometer settings of client:") for k, v in settings.items(): print(k, v) gp.set_settings(pin=0, rmode='ADC', pmode=None, ptype=None) gp.notifications(handle_gpio_notification)
Created by hbldh <*****@*****.**> Created on 2016-04-26 """ from __future__ import division from __future__ import print_function from __future__ import absolute_import import time from pymetawear.discover import select_device from pymetawear.client import MetaWearClient address = select_device() c = MetaWearClient(str(address), debug=True) print("New client created: {0}".format(c)) if c.ambient_light.is_present: print("Get ambient light settings...") settings = c.ambient_light.get_possible_settings() print(settings) time.sleep(1.0) print("Write ambient light settings...") c.ambient_light.set_settings(gain=4, integration_time=200, measurement_rate=200) time.sleep(1.0) print("Subscribing to ambient light signal notifications...")
# %matplotlib inline # import matplotlib.pyplot as plt from pymetawear.discover import select_device from pymetawear.client import MetaWearClient from mbientlab.metawear.cbindings import SensorFusionData, SensorFusionGyroRange, SensorFusionAccRange, SensorFusionMode global ACC_x global ACC_y global ACC_z global EU_pitch global EU_roll global EU_yaw #address = select_device() c = MetaWearClient(str('C2:9B:59:07:56:C9'), debug=True) # c = MetaWearClient(str('FB:81:71:31:92:7A'), debug=True) print("New client created: {0}".format(c)) ACC_x = np.array(0) ACC_y = np.array(0) ACC_z = np.array(0) EU_pitch = np.array(0) EU_roll = np.array(0) EU_yaw = np.array(0) def status_detection(z): # Status
""" from __future__ import division from __future__ import print_function from __future__ import absolute_import import time import sys from pymetawear.discover import select_device from pymetawear.client import MetaWearClient from pymetawear.mbientlab.metawear.cbindings import SensorFusionData, SensorFusionGyroRange, SensorFusionAccRange, SensorFusionMode address = sys.argv[1] #select_device() c = MetaWearClient(str(address), 'pygatt', debug=False, timeout=10) print("New client created: {0}".format(c)) def handle_quat(data): # Handle a (epoch_time, (w,x,y,z)) quaternion tuple.¬ epoch = data[0] xyzaccu = data[1] print("QUAT [{0}] W {1}, X {2}, Y {3}, Z {4}".format(epoch, *xyzaccu)) def handle_notification(data): # Handle a (epoch_time, (x,y,z,accuracy)) corrected acc¬ # tuple.¬ epoch = data[0] xyzaccu = data[1] print("ACC [{0}] X: {1}, Y: {2}, Z: {3}".format(epoch, *xyzaccu[:-1]))
Created on 2016-04-02 """ from __future__ import division from __future__ import print_function from __future__ import absolute_import import time from pymetawear.client import MetaWearClient address_1 = 'DD:3A:7D:4D:56:F0' address_2 = 'FF:50:35:82:3B:5A' print("Connect to {0}...".format(address_1)) client_1 = MetaWearClient(str(address_1), timeout=10.0, debug=False) print("New client created: {0}".format(client_1)) print("Connect to {0}...".format(address_2)) client_2 = MetaWearClient(str(address_2), timeout=10.0, debug=False) print("New client created: {0}".format(client_2)) print("Blinking 10 times with green LED on client 1...") pattern = client_1.led.load_preset_pattern('blink', repeat_count=10) client_1.led.write_pattern(pattern, 'g') client_1.led.play() print("Blinking 10 times with red LED on client 2...") pattern = client_2.led.load_preset_pattern('blink', repeat_count=10) client_2.led.write_pattern(pattern, 'r') client_2.led.play()
def run() -> None: """kjsk Starts the program :return None: """ # Create a MetaWear device address = select_device() # d = MetaWearClient('EE:50:E7:BF:21:83') d = MetaWearClient(str(address)) proceed = True prompt = """ \nChoose a number for an exercise below 1. Walking 2. Sitting 3. Lying down 4. Standing """ while proceed: exercise = int(input(prompt)) while exercise not in [1, 2, 3, 4]: exercise = int(input(prompt)) if exercise == 1: action = 'walking' elif exercise == 2: action = 'sitting' elif exercise == 3: action = 'laying' else: action = 'standing' time_ = int(input(f"\nEnter Seconds You'll do {action}:\n")) # time_ = 60 # Start to stream and record data try: acc_fname, gyro_fname = write_data_csv(title=action, device=d, time_=time_) upload_choice = input("\nUpload data?\n1. Yes\n2. No\n") if upload_choice == '1': upload_csv(acc_fname) upload_csv(gyro_fname) choice = input('Continue or exit\n1. Continue\n2. Exit\n') if choice != '1': print("Exiting...") proceed = False except Exception as message: print("Commit to database failed terribly due to\n", message) choice = input('Repeat or exit\n1. Repeat\n2. Exit\n') if choice != '1': print("Exiting...") proceed = False # Disconnect device reset(d)