/** * @brief Read Acc values, Calibrate them, filter them, and update the pitch value to be displayed * @param None * @retval None */ void ReadAcc(void){ /* Get values */ LIS3DSH_ReadACC(accValue); Calibrate(accValue); /* Filter values */ kalmanUpdate(xState, accValue[0]); kalmanUpdate(yState, accValue[1]); kalmanUpdate(zState, accValue[2]); pitchAngle = calcPitch(xState->x, yState->x, zState->x); rollAngle = calcRoll(xState->x, yState->x, zState->x); }
/** * @brief Temperature monitoring thread * @param none * @retval none */ void Thread_Temperature (void const *argument) { while(1) { HAL_ADC_Start(&ADC1_Handle); /* start ADC conversion */ if (HAL_ADC_PollForConversion(&ADC1_Handle, 10000) == HAL_OK) { /* wait for the conversion to be done and get data */ adc_val = HAL_ADC_GetValue(&ADC1_Handle); /* get the value */ kalmanUpdate(&adcState, adc_val); /* filter the data and update the filter parameters */ temperature = convertTemp(adcState.x); __HAL_ADC_CLEAR_FLAG(&ADC1_Handle, ADC_FLAG_EOC); } // check if the alarm needs to be triggered if (temperature > THRESHHOLD_TEMP_URGENT) { flash_alarm = 2; } else if (temperature > THRESHHOLD_TEMP) { flash_alarm = 1; } else { flash_alarm = 0; } osDelay(TEMP_DELAY); } }
float Barometer::getPressure() { sensors_event_t event; barometer.getEvent(&event); if(!event.pressure) { return NO_DATA; } kalmanUpdate(&altitude, event.pressure); return altitude.value; }
float Accelerometer::getHeading() { sensors_vec_t orientation; getMagOrientation(&orientation); if(!orientation.heading) { return NO_DATA; } kalmanUpdate(&heading, orientation.heading); return heading.value; }
float Accelerometer::getPitch() { sensors_vec_t orientation; getAccelOrientation(&orientation); if(!orientation.pitch) { return NO_DATA; } kalmanUpdate(&pitch, orientation.pitch); return pitch.value; }
float Accelerometer::getRoll() { sensors_vec_t orientation; getAccelOrientation(&orientation); if(!orientation.roll) { return NO_DATA; } kalmanUpdate(&roll, orientation.roll); return roll.value; }
int main(void) { /* MCU Configuration----------------------------------------------------------*/ HAL_Init(); /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ SystemClock_Config(); /* Configure the system clock */ ADC1_Config(); /* configure ADC1 */ Display_GPIO_Config(); /* Configure 7-Segment Displays */ Alarm_GPIO_Config(); /* Configure alarm pins */ initLCD(); /* configure LCD */ /* print temperature on the first line of the LCD */ returnHome(); /* just makes sure that start writing at the right place */ LCD_WriteString(" Temperature"); /* The 2 initial space are for centering */ adcState = malloc(sizeof(kalmanState)); /* Init Kalman Filter */ kalmanInit(adcState, INIT_q, INIT_r, INIT_x, INIT_p, INIT_k); /* main program to run in this infinite loop */ while (1) { if (adcTimer >= ADC_PERIOD) { /* 100Hz */ adcTimer = 0; HAL_ADC_Start(&ADC1_Handle); /* start ADC conversion */ /* wait for the conversion to be done and get data */ if (HAL_ADC_PollForConversion(&ADC1_Handle, 1000000) == HAL_OK) { adc_val = HAL_ADC_GetValue(&ADC1_Handle); /* get the value */ kalmanUpdate(adcState, adc_val); /* filter the data and update the filter parameters */ /* DON'T DELETE printf for matlab script */ //printf("%f,%f,%f,%f,%f,%f\n",adc_val, adcState->q,adcState->r, adcState->x, adcState->p, adcState->k); temp = convertTemp(adcState->x); /* convert the filterd value of the ADC into temperature */ /* Alarm triggering */ if (temp > THRESHHOLD_TEMP) { if ( filterAlarmCounter > 5 ){ /* 5 consecutive to avoid false positive */ trigger_alarm(); } else { filterAlarmCounter++; } } else { shutoff_alarm(); filterAlarmCounter = 0; } /* Update Measurement to Display at 2Hz */ if (updateMeasureForDisplayTimer >= UPDATE_MEASURE_PERIOD) { updateMeasureForDisplayTimer = 0; /* reset the displayTimer tick */ displayTemp = temp; displayTemp = floor(10 * displayTemp) / 10; /* truncate to 1 decimal without rounding */ /* LCD DISPLAY */ SetAdress(64); /* go to line 2 of LCD */ sprintf(tempToLCD, " %.1f", displayTemp); /* convert the float to a string and formats it */ LCD_WriteString(tempToLCD); /* print value to the LCD display */ /* LCD DISPLAY END */ } } } /* here display runs at DISPLAY_7_SEGMENT_PERIOD speed, but displayTemp gets updated at 2Hz */ if(display7segTimer >= DISPLAY_7_SEGMENT_PERIOD) { display7segTimer = 0; display(displayTemp); /* display on 7-segment display */ } } }
float Accelerometer::getAcceleration() { kalmanUpdate(&acceleration, getRawAcceleration()); return acceleration.value; }