Skip to main content
Visitor II
June 11, 2019
Question

State Machine and stack size ?

  • June 11, 2019
  • 1 reply
  • 745 views

Dear Members,

I have a state machine but it seems that it stuck in one state,

Is it related with stack size ?

It's stuck on "case PULSE_IDLE" no matter the value of sensor_value,

The code :

switch(currentPulseDetectorState)
 {
 case PULSE_IDLE:
 if(sensor_value >= PULSE_MIN_THRESHOLD) {
				//printf("Sensor_value PULSE_IDLE %f \r\n",sensor_value);
 currentPulseDetectorState = PULSE_TRACE_UP;
				
 values_went_down = 0;
				printf("PULSE IDLE\r\n");
				return true;
 }
 break;
 
 case PULSE_TRACE_UP:
			printf("Sensor value PULSE_TRACE_UP %f\r\n",sensor_value);
 if(sensor_value > prev_sensor_value)
 {
				
 printf("PULSE TRACE_UP\r\n");
				currentBeat =HAL_GetTick();
 lastBeatThreshold = sensor_value;
				
				
 }
 else
 {
 
 printf("Peak reached: ");
 printf("%f \r\n",sensor_value);
 printf(" ");
 printf("%f \r\n",prev_sensor_value); 
 
 uint32_t beatDuration = currentBeat - lastBeat;
 lastBeat = currentBeat;
 
 float rawBPM=0;
 if(beatDuration > 0)
 rawBPM = 60000.0 / (float)beatDuration;
 printf("RawBPM = %f\r\n",rawBPM);
 
 //This method sometimes glitches, it's better to go through whole moving average everytime
 //IT's a neat idea to optimize the amount of work for moving avg. but while placing, removing finger it can screw up
 //valuesBPMSum -= valuesBPM[bpmIndex];
 //valuesBPM[bpmIndex] = rawBPM;
 //valuesBPMSum += valuesBPM[bpmIndex];
 
 valuesBPM[bpmIndex] = rawBPM;
 valuesBPMSum = 0;
 for(int i=0; i<PULSE_BPM_SAMPLE_SIZE; i++)
 {
 valuesBPMSum += valuesBPM[i];
 }
 
 
 
 bpmIndex++;
 bpmIndex = bpmIndex % PULSE_BPM_SAMPLE_SIZE;
 
 if(valuesBPMCount < PULSE_BPM_SAMPLE_SIZE)
 valuesBPMCount++;
 
 currentBPM = valuesBPMSum / valuesBPMCount;
 
 printf("AVg. BPM: ");
 printf("%f",currentBPM);
 
 
 
 currentPulseDetectorState = PULSE_TRACE_DOWN;
 
 return true;
 }
 break;
 
 case PULSE_TRACE_DOWN:
 if(sensor_value < prev_sensor_value)
 {
 values_went_down++;
 }
 
 
 if(sensor_value < PULSE_MIN_THRESHOLD)
 {
 currentPulseDetectorState = PULSE_IDLE;
 }
 break;
 }
 
 prev_sensor_value = sensor_value;

Any clues on solving it ? thanks

    This topic has been closed for replies.

    1 reply

    Explorer
    June 11, 2019

    Almost certrainly not.

    With a stack problem, you would end up in the hardfault handler.

    Perhaps your "sensor_value" is always less then PULSE_MIN_THRESHOLD ?

    antoniusAuthor
    Visitor II
    June 11, 2019
    Data 7 Output MAX30100,0x09 register (FIFO DATA) IR : 1536 
     
    DC filterIR = -1147.117188
     
    MeanDiff filterIR = 52967.000000
     
    LowPass filterIR = 12989.482422
     
    Timestamp After IRRED data= 25885 
     
    sensor_value > PULSE_MAX_THRESHOLD
     
    Sensor_value PULSE_MAX_TRESHOLD 12989.482422 
     
    Sensor_value at PULSE_IDLE 12989.482422 
     
    PULSE IDLE
     
     

    code :

    switch(currentPulseDetectorState)
     {
     case PULSE_IDLE:
     if(sensor_value >= PULSE_MIN_THRESHOLD) {
    				printf("Sensor_value at PULSE_IDLE %f \r\n",sensor_value);
     
    				
     values_went_down = 0;
    				printf("PULSE IDLE\r\n");
    				 currentPulseDetectorState = PULSE_TRACE_UP;
    				//return currentPulseDetectorState = PULSE_TRACE_UP;
     }
     break;

    #define PULSE_MIN_THRESHOLD        300

    I don't understand why ?