reflow-oven-control-sw/stm-firmware/calibration.c

89 lines
2.3 KiB
C

/* Reflow Oven Controller
*
* Copyright (C) 2020 Mario Hüttel <mario.huettel@gmx.net>
*
* This file is part of the reflow Oven Controller Project.
*
* The reflow oven controller is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* GDSII-Converter is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GDSII-Converter. If not, see <http://www.gnu.org/licenses/>.
*/
#include <reflow-controller/calibration.h>
#include <reflow-controller/adc-meas.h>
#include <helper-macros/helper-macros.h>
#include <arm_math.h>
#include <stdlib.h>
void calibration_calculate(float low_measured, float low_setpoint, float high_measured, float high_setpoint,
float *sens_deviation, float *sens_corrected_offset)
{
if (!sens_deviation || !sens_corrected_offset)
return;
float delta_y;
float delta_x;
float sens_corr_mult;
delta_y = high_measured - low_measured;
delta_x = high_setpoint - low_setpoint;
sens_corr_mult = delta_x / delta_y;
*sens_deviation = sens_corr_mult - 1.0f;
*sens_corrected_offset = low_setpoint - low_measured * sens_corr_mult;
}
int calibration_acquire_data(float *mu, float *sigma, uint32_t count)
{
int status;
float *stream_mem;
static volatile int flag = 0;
if (!mu || !sigma || !count)
return -1000;
stream_mem = (float *)calloc(count, sizeof(float));
if (!stream_mem)
return -2;
/* Clear errors of PT1000 reading */
adc_pt1000_clear_error();
status = adc_pt1000_stream_raw_value_to_memory(stream_mem, count, &flag);
if (status)
return status;
/* Wait for data to be transferred */
while (flag == 0);
if (flag != 1) {
/* Error */
return -1;
}
/* Convert the stream memory to Ohm readings */
adc_pt1000_convert_raw_value_array_to_resistance(NULL, stream_mem, count);
/* Do not compute std-deviation. Too imprecise
* arm_std_f32(stream_mem, count, sigma);
*/
*sigma = 0;
arm_mean_f32(stream_mem, count, mu);
free(stream_mem);
return 0;
}