comparison Discovery/Src/motion.c @ 578:beb4d47542f1

Rework pitch detection: To improve separation between pos and neg moves the history memory is now evaluated starting from the last detected start condition. This was needed because a small "overshoot" of the end position causes e.g. a pos event to be detected as negative event.
author Ideenmodellierer
date Sun, 29 Nov 2020 23:02:11 +0100
parents 01ee21dd311f
children 4dfdf230d8ba
comparison
equal deleted inserted replaced
577:9bb9a52d6ae5 578:beb4d47542f1
105 motionDeltaHistoryIdx = nextIndex; 105 motionDeltaHistoryIdx = nextIndex;
106 } 106 }
107 107
108 SDeltaHistory GetDeltaHistory(uint8_t stepback) 108 SDeltaHistory GetDeltaHistory(uint8_t stepback)
109 { 109 {
110 uint8_t loop = stepback; 110 uint8_t loop;
111 uint8_t index = motionDeltaHistoryIdx; 111 uint8_t index = motionDeltaHistoryIdx;
112 112
113 SDeltaHistory result = {0,0,0}; 113 SDeltaHistory result = {0,0,0};
114 114
115 stepback++; /* motionDeltaHistoryIdx is pointing to future entry => step back one to get the latest */
116 loop = stepback;
115 if(stepback < MOTION_DELTA_HISTORY_SIZE) 117 if(stepback < MOTION_DELTA_HISTORY_SIZE)
116 { 118 {
117 while(loop != 0) /* find requested entry */ 119 while(loop != 0) /* find requested entry */
118 { 120 {
119 loop--; 121 loop--;
293 295
294 /* Detect if user is generating an pitch including return to starting position */ 296 /* Detect if user is generating an pitch including return to starting position */
295 /* This is done by feeding the past movements value per value into a state machine */ 297 /* This is done by feeding the past movements value per value into a state machine */
296 detectionState_t detectPitch(float currentPitch) 298 detectionState_t detectPitch(float currentPitch)
297 { 299 {
300 static int8_t lastStart = 0;
298 uint8_t exit = 0; 301 uint8_t exit = 0;
299 uint8_t step = 0; 302 int8_t step = 0;
300 uint8_t duration = 0; 303 uint8_t duration = 0;
301 SDeltaHistory test; 304 SDeltaHistory test;
302 305
303 detectionState = DETECT_NOTHING; 306 if(lastStart < 0)
304 while((step != MOTION_DELTA_HISTORY_SIZE) && (!exit)) /* start backward evalution of pitch changes*/ 307 {
308 detectionState = DETECT_NOTHING;
309 lastStart = 0;
310 }
311 else
312 {
313 detectionState = DETECT_START;
314 }
315 step = lastStart;
316 do
305 { 317 {
306 test = GetDeltaHistory(step); 318 test = GetDeltaHistory(step);
307 step++;
308 duration++; 319 duration++;
309 switch (detectionState) 320 switch (detectionState)
310 { 321 {
311 case DETECT_NOTHING: if(test.pitch > MOTION_DELTA_STABLE) 322 case DETECT_NOTHING: if(test.pitch > MOTION_DELTA_STABLE)
312 { 323 {
313 exit = 1; 324 exit = 1;
325 lastStart = -2;
314 } 326 }
315 else 327 else
316 { 328 {
317 detectionState = DETECT_START; 329 detectionState = DETECT_START;
330 lastStart = -1;
318 } 331 }
319 break; 332 break;
320 case DETECT_START: if(test.pitch == MOTION_DELTA_RAISE) 333 case DETECT_START: if(test.pitch == MOTION_DELTA_RAISE)
321 { 334 {
322 detectionState = DETECT_POS_MOVE; 335 detectionState = DETECT_POS_MOVE;
323 } 336 lastStart = step;
337 }
338 else
324 if(test.pitch == MOTION_DELTA_FALL) 339 if(test.pitch == MOTION_DELTA_FALL)
325 { 340 {
326 detectionState = DETECT_NEG_MOVE; 341 detectionState = DETECT_NEG_MOVE;
342 lastStart = step;
343 }
344 else
345 {
346 lastStart = -1;
327 } 347 }
328 duration = 0; 348 duration = 0;
329 break; 349 break;
330 case DETECT_NEG_MOVE: 350 case DETECT_NEG_MOVE: if((test.pitch <= MOTION_DELTA_JITTER) || (test.pitch == MOTION_DELTA_RAISE))
331 case DETECT_POS_MOVE: if(test.pitch <= MOTION_DELTA_JITTER) 351 {
352 detectionState++;
353 }
354 break;
355 case DETECT_POS_MOVE: if((test.pitch <= MOTION_DELTA_JITTER) || (test.pitch == MOTION_DELTA_FALL))
332 { 356 {
333 detectionState++; 357 detectionState++;
334 } 358 }
335 break; 359 break;
336 case DETECT_MAXIMA: if(test.pitch == MOTION_DELTA_FALL) 360 case DETECT_MAXIMA: if(test.pitch == MOTION_DELTA_FALL)
344 } 368 }
345 break; 369 break;
346 case DETECT_RISEBACK: 370 case DETECT_RISEBACK:
347 case DETECT_FALLBACK: if(test.pitch == MOTION_DELTA_STABLE) 371 case DETECT_FALLBACK: if(test.pitch == MOTION_DELTA_STABLE)
348 { 372 {
349 if(duration > 5) /* avoid detection triggered by short moves */ 373 if(duration > 4) /* avoid detection triggered by short moves */
350 { 374 {
351 detectionState++; 375 detectionState++;
352 exit = 1;
353 } 376 }
354 else 377 exit = 1;
355 { 378 lastStart = -2;
356 detectionState = DETECT_NOTHING;
357 }
358 } 379 }
359 break; 380 break;
360 default: 381 default:
361 detectionState = DETECT_NOTHING; 382 detectionState = DETECT_NOTHING;
362 exit = 1; 383 exit = 1;
363 break; 384 break;
364 } 385 }
386 step--;
387 } while((step >= 0) && (!exit));
388
389 if((lastStart < MOTION_DELTA_HISTORY_SIZE))
390 {
391 lastStart++; /* prepare value for next iteration (history index will be increased) */
392 }
393 else
394 {
395 lastStart = -1;
365 } 396 }
366 if((detectionState != DETECT_POS_PITCH) && (detectionState != DETECT_NEG_PITCH)) /* nothing found */ 397 if((detectionState != DETECT_POS_PITCH) && (detectionState != DETECT_NEG_PITCH)) /* nothing found */
367 { 398 {
368 detectionState = DETECT_NOTHING; 399 detectionState = DETECT_NOTHING;
369 } 400 }