|
| 1 | +# usage: python acc_threshold_detector [mac] |
| 2 | +from __future__ import print_function |
| 3 | +from mbientlab.metawear import MetaWear, libmetawear, parse_value |
| 4 | +from mbientlab.metawear.cbindings import * |
| 5 | +from time import sleep |
| 6 | +from threading import Event |
| 7 | + |
| 8 | +# CURRENT: |
| 9 | +# Sample accelerometer at 50Hz |
| 10 | +# Use the rms to combine X,Y,Z |
| 11 | +# Use the average to downsample to about 4Hz (averaging 8 samples) |
| 12 | +# Use the threshold detector to detect a bump |
| 13 | +# Also log all the event and download it later |
| 14 | + |
| 15 | +print("Searching for device...") |
| 16 | +d = MetaWear(sys.argv[1]) |
| 17 | +d.connect() |
| 18 | +print("Connected to " + d.address) |
| 19 | +print("Configuring device") |
| 20 | +libmetawear.mbl_mw_settings_set_connection_parameters(d.board, 7.5, 7.5, 0, 6000) |
| 21 | +sleep(1.0) |
| 22 | + |
| 23 | +e = Event() |
| 24 | +def create_voidp(create, resource): |
| 25 | + result = [None] |
| 26 | + def handler(ctx, pointer): |
| 27 | + result[0] = RuntimeError("Could not create " + resource) if pointer == None else pointer |
| 28 | + e.set() |
| 29 | + |
| 30 | + callback_wrapper = FnVoid_VoidP_VoidP(handler) |
| 31 | + create(callback_wrapper) |
| 32 | + e.wait() |
| 33 | + |
| 34 | + e.clear() |
| 35 | + if (result[0] is RuntimeError): |
| 36 | + raise result[0] |
| 37 | + return result[0] |
| 38 | + |
| 39 | +def end_event_record(evt): |
| 40 | + result = [None] |
| 41 | + def handler(ctx, pointer, status): |
| 42 | + if (status != Const.STATUS_OK): |
| 43 | + result[0] = RuntimeError("Event recording returned a non-zero status (%d)" % (status)) |
| 44 | + e.set() |
| 45 | + |
| 46 | + callback_wrapper = FnVoid_VoidP_VoidP_Int(handler) |
| 47 | + libmetawear.mbl_mw_event_end_record(evt, None, callback_wrapper) |
| 48 | + e.wait() |
| 49 | + |
| 50 | + e.clear() |
| 51 | + if (result[0] is RuntimeError): |
| 52 | + raise result[0] |
| 53 | + |
| 54 | +try: |
| 55 | + # setup accelerometer (odr 50Hz and 2Gs) |
| 56 | + libmetawear.mbl_mw_acc_bmi160_set_odr(d.board, AccBmi160Odr._50Hz) |
| 57 | + libmetawear.mbl_mw_acc_set_range(d.board, 2.0) |
| 58 | + libmetawear.mbl_mw_acc_write_acceleration_config(d.board) |
| 59 | + |
| 60 | + # start to setup rms->avg->thresh->log chain |
| 61 | + acc_signal = libmetawear.mbl_mw_acc_get_acceleration_data_signal(d.board) |
| 62 | + |
| 63 | + # create RMS - root mean square of acc X,Y,Z |
| 64 | + rms = create_voidp(lambda fn: libmetawear.mbl_mw_dataprocessor_rms_create(acc_signal, None, fn), "RMS") |
| 65 | + print("RMS created") |
| 66 | + |
| 67 | + # setup averager - averages over 8 RMS samples @ 50Hz |
| 68 | + avg = create_voidp(lambda fn: libmetawear.mbl_mw_dataprocessor_average_create(rms, 8, None, fn), "averager") |
| 69 | + print("Averager created") |
| 70 | + |
| 71 | + # setup event on avg - reset averager |
| 72 | + libmetawear.mbl_mw_event_record_commands(avg) |
| 73 | + libmetawear.mbl_mw_dataprocessor_average_reset(avg) |
| 74 | + end_event_record(avg) |
| 75 | + |
| 76 | + # setup threshold detector - detect anything above 1 |
| 77 | + ths = create_voidp(lambda fn: libmetawear.mbl_mw_dataprocessor_threshold_create(avg, ThresholdMode.BINARY, 1.0, 0.0, None, fn), "threshold detector") |
| 78 | + print("Threshold detector created") |
| 79 | + |
| 80 | + # setup logger - log the final signal of the averaged data |
| 81 | + ths_logger = create_voidp(lambda fn: libmetawear.mbl_mw_datasignal_log(ths, None, fn), "threshold logger") |
| 82 | + print("Threshold logger created") |
| 83 | + |
| 84 | + # start accelerometer and event logging |
| 85 | + libmetawear.mbl_mw_acc_enable_acceleration_sampling(d.board) |
| 86 | + libmetawear.mbl_mw_acc_start(d.board) |
| 87 | + libmetawear.mbl_mw_logging_start(d.board, 0) |
| 88 | + |
| 89 | + # change this to any time amount you want |
| 90 | + print("Logging data for 10s") |
| 91 | + sleep(10.0) |
| 92 | + |
| 93 | + libmetawear.mbl_mw_acc_stop(d.board) |
| 94 | + libmetawear.mbl_mw_acc_disable_acceleration_sampling(d.board) |
| 95 | + libmetawear.mbl_mw_logging_stop(d.board) |
| 96 | + |
| 97 | + def progress_update_handler(context, entries_left, total_entries): |
| 98 | + if (entries_left == 0): |
| 99 | + e.set() |
| 100 | + |
| 101 | + fn_wrapper = FnVoid_VoidP_UInt_UInt(progress_update_handler) |
| 102 | + download_handler= LogDownloadHandler(context = None, \ |
| 103 | + received_progress_update = fn_wrapper, \ |
| 104 | + received_unknown_entry = cast(None, FnVoid_VoidP_UByte_Long_UByteP_UByte), \ |
| 105 | + received_unhandled_entry = cast(None, FnVoid_VoidP_DataP)) |
| 106 | + |
| 107 | + callback = FnVoid_VoidP_DataP(lambda ctx, p: print("threshold crossed: {epoch: %d, value: %s}" % (p.contents.epoch, parse_value(p)))) |
| 108 | + libmetawear.mbl_mw_logger_subscribe(ths_logger, None, callback) |
| 109 | + libmetawear.mbl_mw_logging_download(d.board, 0, byref(download_handler)) |
| 110 | + e.wait() |
| 111 | + |
| 112 | +except RuntimeError as err: |
| 113 | + print(err) |
| 114 | +finally: |
| 115 | + e.clear() |
| 116 | + print("Resetting device") |
| 117 | + |
| 118 | + d.on_disconnect = lambda status: e.set() |
| 119 | + libmetawear.mbl_mw_debug_reset(d.board) |
| 120 | + e.wait() |
0 commit comments