@@ -26,9 +26,13 @@ enum MlRunnerError {
26
26
#define ML_DEBUG_PRINT 0
27
27
#endif
28
28
#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__)
30
32
#else
33
+ #define DEBUG_PREFIX " "
31
34
#define DEBUG_PRINT (...)
35
+ #define DEBUG_PRINT_RAW (...)
32
36
#endif
33
37
34
38
// Configure the period between ML runs, can be set in pxt.json
@@ -47,6 +51,7 @@ namespace mlrunner {
47
51
static const int ML_EVENT_NONE_ID = 1 ;
48
52
49
53
static bool initialised = false ;
54
+ static int samplesPeriodMillisec = 0 ;
50
55
static int mlSampleCountsPerInference = 0 ;
51
56
static int lastPredictionEventId = -1 ;
52
57
static ml_actions_t *actions = NULL ;
@@ -83,21 +88,20 @@ namespace mlrunner {
83
88
uBit.panic (MlRunnerError::ErrorModelInference);
84
89
}
85
90
86
- DEBUG_PRINT (" Prediction (%d ms): " , uBit.systemTime () - time_start);
91
+ DEBUG_PRINT (" P (%d ms): " , uBit.systemTime () - time_start);
87
92
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 );
91
96
} else {
92
- DEBUG_PRINT (" None\n " );
97
+ DEBUG_PRINT_RAW (" None\t\t " );
93
98
}
94
- DEBUG_PRINT (" \t Individual:" );
95
99
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 ));
99
103
}
100
- DEBUG_PRINT (" \n\n " );
104
+ DEBUG_PRINT_RAW (" \n\n " );
101
105
102
106
// Model prediction events start after the None event ID
103
107
uint16_t predictionEventId = predictions->index + ML_EVENT_NONE_ID + 1 ;
@@ -108,6 +112,15 @@ namespace mlrunner {
108
112
void recordAccData (MicroBitEvent) {
109
113
if (!initialised) return ;
110
114
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
+
111
124
const Sample3D accSample = uBit.accelerometer .getSample ();
112
125
const float accData[3 ] = {
113
126
accSample.x / 1000 .0f ,
@@ -194,7 +207,7 @@ namespace mlrunner {
194
207
uBit.panic (MlRunnerError::ErrorSamplesDimension);
195
208
}
196
209
197
- const int samplesPeriodMillisec = ml_getSamplesPeriod ();
210
+ samplesPeriodMillisec = ml_getSamplesPeriod ();
198
211
DEBUG_PRINT (" \t Model samples period: %d ms\n " , samplesPeriodMillisec);
199
212
if (samplesPeriodMillisec <= 0 ) {
200
213
DEBUG_PRINT (" Model samples period invalid\n " );
@@ -241,7 +254,7 @@ namespace mlrunner {
241
254
DEBUG_PRINT (" \t Actions (%d):\n " , actions->len );
242
255
for (size_t i = 0 ; i < actions->len ; i++) {
243
256
DEBUG_PRINT (" \t\t Action '%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 ));
245
258
}
246
259
247
260
predictions = ml_allocatePredictions ();
@@ -282,7 +295,7 @@ namespace mlrunner {
282
295
DEBUG_PRINT (" Attempting to stop running ML, but was already stopped.\n " );
283
296
return ;
284
297
}
285
- DEBUG_PRINT (" Stop running the ML model... " );
298
+ DEBUG_PRINT (" Stop running the ML model...\n " );
286
299
287
300
initialised = false ;
288
301
0 commit comments