Skip to content

Commit d41f777

Browse files
Add sampling drift to debug data & add prefix to all debug messages.
1 parent 0efc69e commit d41f777

File tree

1 file changed

+27
-14
lines changed

1 file changed

+27
-14
lines changed

pxtextension.cpp

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,13 @@ enum MlRunnerError {
2626
#define ML_DEBUG_PRINT 0
2727
#endif
2828
#if ML_DEBUG_PRINT
29-
#define DEBUG_PRINT(...) uBit.serial.printf(__VA_ARGS__)
29+
#define DEBUG_PREFIX "[ML] "
30+
#define DEBUG_PRINT(...) uBit.serial.printf(DEBUG_PREFIX __VA_ARGS__)
31+
#define DEBUG_PRINT_RAW(...) uBit.serial.printf(__VA_ARGS__)
3032
#else
33+
#define DEBUG_PREFIX ""
3134
#define DEBUG_PRINT(...)
35+
#define DEBUG_PRINT_RAW(...)
3236
#endif
3337

3438
// Configure the period between ML runs, can be set in pxt.json
@@ -47,6 +51,7 @@ namespace mlrunner {
4751
static const int ML_EVENT_NONE_ID = 1;
4852

4953
static bool initialised = false;
54+
static int samplesPeriodMillisec = 0;
5055
static int mlSampleCountsPerInference = 0;
5156
static int lastPredictionEventId = -1;
5257
static ml_actions_t *actions = NULL;
@@ -83,21 +88,20 @@ namespace mlrunner {
8388
uBit.panic(MlRunnerError::ErrorModelInference);
8489
}
8590

86-
DEBUG_PRINT("Prediction (%d ms): ", uBit.systemTime() - time_start);
91+
DEBUG_PRINT("P (%d ms): ", uBit.systemTime() - time_start);
8792
if (predictions->index >= 0) {
88-
DEBUG_PRINT("%d - %s\n",
89-
predictions->index,
90-
actions->action[predictions->index].label);
93+
DEBUG_PRINT_RAW("%d %s\t\t",
94+
predictions->index,
95+
actions->action[predictions->index].label);
9196
} else {
92-
DEBUG_PRINT("None\n");
97+
DEBUG_PRINT_RAW("None\t\t");
9398
}
94-
DEBUG_PRINT("\tIndividual:");
9599
for (size_t i = 0; i < actions->len; i++) {
96-
DEBUG_PRINT(" %s[%d]",
97-
actions->action[i].label,
98-
(int)(predictions->prediction[i] * 100));
100+
DEBUG_PRINT_RAW(" %s[%d]",
101+
actions->action[i].label,
102+
(int)(predictions->prediction[i] * 100));
99103
}
100-
DEBUG_PRINT("\n\n");
104+
DEBUG_PRINT_RAW("\n\n");
101105

102106
// Model prediction events start after the None event ID
103107
uint16_t predictionEventId = predictions->index + ML_EVENT_NONE_ID + 1;
@@ -108,6 +112,15 @@ namespace mlrunner {
108112
void recordAccData(MicroBitEvent) {
109113
if (!initialised) return;
110114

115+
#if ML_DEBUG_PRINT
116+
static uint32_t lastSampleTime = 0;
117+
uint32_t now = uBit.systemTime();
118+
if ((now - lastSampleTime) != (uint32_t)samplesPeriodMillisec) {
119+
DEBUG_PRINT("Sample period drift: %d ms\n", now - lastSampleTime);
120+
}
121+
lastSampleTime = now;
122+
#endif
123+
111124
const Sample3D accSample = uBit.accelerometer.getSample();
112125
const float accData[3] = {
113126
accSample.x / 1000.0f,
@@ -194,7 +207,7 @@ namespace mlrunner {
194207
uBit.panic(MlRunnerError::ErrorSamplesDimension);
195208
}
196209

197-
const int samplesPeriodMillisec = ml_getSamplesPeriod();
210+
samplesPeriodMillisec = ml_getSamplesPeriod();
198211
DEBUG_PRINT("\tModel samples period: %d ms\n", samplesPeriodMillisec);
199212
if (samplesPeriodMillisec <= 0) {
200213
DEBUG_PRINT("Model samples period invalid\n");
@@ -241,7 +254,7 @@ namespace mlrunner {
241254
DEBUG_PRINT("\tActions (%d):\n", actions->len);
242255
for (size_t i = 0; i < actions->len; i++) {
243256
DEBUG_PRINT("\t\tAction '%s' ", actions->action[i].label);
244-
DEBUG_PRINT("threshold = %d %%\n", (int)(actions->action[i].threshold * 100));
257+
DEBUG_PRINT_RAW("threshold = %d %%\n", (int)(actions->action[i].threshold * 100));
245258
}
246259

247260
predictions = ml_allocatePredictions();
@@ -282,7 +295,7 @@ namespace mlrunner {
282295
DEBUG_PRINT("Attempting to stop running ML, but was already stopped.\n");
283296
return;
284297
}
285-
DEBUG_PRINT("Stop running the ML model... ");
298+
DEBUG_PRINT("Stop running the ML model...\n");
286299

287300
initialised = false;
288301

0 commit comments

Comments
 (0)