Skip to content

Commit

Permalink
Rename C module to ml_runner, add Python ml.py.
Browse files Browse the repository at this point in the history
  • Loading branch information
microbit-carlos committed Jun 4, 2024
1 parent 8027eef commit 65bb735
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 26 deletions.
42 changes: 21 additions & 21 deletions ml-module/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,27 @@

import ml

print("Input size: {}".format(ml.get_input_length()))
print("Model labels: {}".format(ml.get_labels()))
ml.start()
labels = ml.get_labels()

TOTAL_SAMPLES = ml.get_input_length()
acc_x_y_z = [0] * TOTAL_SAMPLES

i = 0
while True:
acc_x_y_z[i + 0] = accelerometer.get_x()
acc_x_y_z[i + 1] = accelerometer.get_y()
acc_x_y_z[i + 2] = accelerometer.get_z()
i += 3
if i >= TOTAL_SAMPLES:
t = time.ticks_ms()
result = ml.predict(acc_x_y_z)
if result:
print("t[{}] {:8s}".format(time.ticks_ms() - t, result[1][result[0]][0]), end="")
for label_pred in result[1]:
print(" {}[{:.2f}]".format(label_pred[0][:1], label_pred[1]), end="")
print()
gesture = ml.get_result()
if gesture == "Jumping":
display.show("J")
if gesture == "Running":
display.show("R")
if gesture == "Walking":
display.show("W")
if gesture == "Standing":
display.show("S")

if button_a.is_pressed():
if ml.is_running():
ml.end()
else:
print("t[{}] {}".format(time.ticks_ms() - t, result))
i = 0
sleep(20)
ml.start()

if button_b.is_pressed():
display.clear()

sleep(100)
81 changes: 81 additions & 0 deletions ml-module/ml.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@

import ml_runner as _ml_runner
import microbit as _mb
from ml_runner import get_labels


_running = False
_acc_x_y_z = []
_acc_i = 0
_total_samples = 0
_last_results = []


def _capture_data():
global _acc_x_y_z, _acc_i, _running
if not _running:
return

_acc_x_y_z[_acc_i + 0] = _mb.accelerometer.get_x()
_acc_x_y_z[_acc_i + 1] = _mb.accelerometer.get_y()
_acc_x_y_z[_acc_i + 2] = _mb.accelerometer.get_z()
_acc_i += 3

if _acc_i >= _total_samples:
t = _mb.running_time()
result = _ml_runner.predict(_acc_x_y_z)
if result:
prediction_label = result[1][result[0]][0]
_last_results.append(prediction_label)
print("t[{}] {:8s}".format(_mb.running_time() - t, prediction_label), end="")
for label_pred in result[1]:
print(" {}[{:.2f}]".format(label_pred[0][:1], label_pred[1]), end="")
print()
else:
print("t[{}] ERROR! {}".format(_mb.running_time() - t, result))
_acc_i = 0


def start():
print("Input size: {}".format(_ml_runner.get_input_length()))
print("Model labels: {}".format(_ml_runner.get_labels()))

global _running, _acc_x_y_z, _acc_i, _total_samples
_total_samples = _ml_runner.get_input_length()
_acc_x_y_z = [0] * _total_samples
_acc_i = 0
_running = True

_mb.run_every(_capture_data, ms=25)


def end():
global _running
_running = False


def is_running():
return _running


def get_results():
global _last_results
results = _last_results
_last_results = []
return results


def get_result():
global _last_results
last = None
if _last_results:
last = _last_results[-1]
_last_results = []
return last


def was(label):
global _last_results
result = label in _last_results
_last_results = []
return result
10 changes: 5 additions & 5 deletions ml-module/src/mlmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,20 +107,20 @@ static MP_DEFINE_CONST_FUN_OBJ_0(ml___init___obj, ml___init__);
// and the MicroPython object reference.
// All identifiers and strings are written as MP_QSTR_xxx and will be
// optimized to word-sized integers by the build system (interned strings).
static const mp_rom_map_elem_t ml_module_globals_table[] = {
static const mp_rom_map_elem_t ml_runner_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_ml) },
{ MP_ROM_QSTR(MP_QSTR___init__), MP_ROM_PTR(&ml___init___obj) },
{ MP_ROM_QSTR(MP_QSTR_get_input_length), MP_ROM_PTR(&get_input_length_func_obj) },
{ MP_ROM_QSTR(MP_QSTR_get_labels), MP_ROM_PTR(&get_labels_func_obj) },
{ MP_ROM_QSTR(MP_QSTR_predict), MP_ROM_PTR(&predict_func_obj) },
};
static MP_DEFINE_CONST_DICT(ml_module_globals, ml_module_globals_table);
static MP_DEFINE_CONST_DICT(ml_runner_module_globals, ml_runner_module_globals_table);

// Define module object.
const mp_obj_module_t ml_cmodule = {
const mp_obj_module_t ml_runner_cmodule = {
.base = { &mp_type_module },
.globals = (mp_obj_dict_t *)&ml_module_globals,
.globals = (mp_obj_dict_t *)&ml_runner_module_globals,
};

// Register the module to make it available in Python.
MP_REGISTER_MODULE(MP_QSTR_ml, ml_cmodule);
MP_REGISTER_MODULE(MP_QSTR_ml_runner, ml_runner_cmodule);

0 comments on commit 65bb735

Please sign in to comment.