fix temp sensor kind of

This commit is contained in:
2026-09-26 12:55:28 +02:00
parent 0bd5fb3165
commit 569a393c76
+33 -27
View File
@@ -22,13 +22,13 @@ var names: snake_case
//thermistor equation is R=R_0*e^(1/T-1/T_0)
class CustomArray {
int* items;
float* items;
int size;
public:
CustomArray(int s = MAX_STORED_VALUES) {
size = s;
items = (int*) malloc(size * sizeof(int));
items = (float*) malloc(size * sizeof(int));
for (int i = 0; i < size; i++ ) items[i] = 0;
}
@@ -40,9 +40,9 @@ class CustomArray {
items[0] = item;
}
int getElement(int index) { return items[index]; }
int getSum() {
int sum = 0;
float getElement(int index) { return items[index]; }
float getSum() {
float sum = 0;
for (int i = 0; i < MAX_STORED_VALUES; i++ ) {
sum += items[i];
@@ -60,9 +60,12 @@ class PID {
private:
int kp, ki, kd, //PID
pwm_pin, thermistor_pin, //thermistor
index //misc
;
float desired_temp, temp, //temperature
thermistor_resistance, voltage_devider_resistance, thermistor_beta, thermistor_ref_temp, //thermistor
desired_temp, temp, //temperature
index; //misc
adc_conversion_factor
;
bool on;
CustomArray error_array;
@@ -71,14 +74,14 @@ class PID {
int index,
int pwm_pin,
int thermistor_pin,
int desired_temp = 0,
float desired_temp = 0,
int kp = 1000,
int ki = 100,
int kd = 10,
int thermistor_resistance = 100000,
int thermistor_beta = 3950,
int thermistor_ref_temp = 25, //C
int voltage_devider_resistance = 100000
float thermistor_resistance = 100000,
float thermistor_beta = 3950,
float thermistor_ref_temp = 25, //C
float voltage_devider_resistance = 100000
) {
error_array = CustomArray();
@@ -88,35 +91,37 @@ class PID {
this->thermistor_pin = thermistor_pin;
this->thermistor_resistance = thermistor_resistance;
this->thermistor_beta = thermistor_beta;
this->thermistor_ref_temp = thermistor_ref_temp + 273;
this->voltage_devider_resistance = voltage_devider_resistance;
this->desired_temp = desired_temp;
this->index = index;
this->adc_conversion_factor = 3.3f / (1 << 12);
measureTemp();
}
int getTemp() {
float getTemp() {
return this->temp;
}
int measureTemp() {
//adc_select_input(thermistor_pin);
float measureTemp() {
adc_select_input(thermistor_pin);
uint16_t result = adc_read();
uint16_t v_slash_vin = 2<<15 / result;
uint16_t r = v_slash_vin * voltage_devider_resistance / (1 / (1-v_slash_vin));
float v = (float)result * adc_conversion_factor; //calculate ratio of measured voltage and max voltage
float r = v / (3.3f - v) * voltage_devider_resistance;
int T = 1/(1/thermistor_beta * log(r/thermistor_resistance) + 1/thermistor_ref_temp);
float T = 1/(1/thermistor_beta * log(r/thermistor_resistance) + 1/thermistor_ref_temp) - 273;
this->temp = 10;
return this->temp;
this->temp = T;
return T;
}
int calulatePid( int x ) {
int e = desired_temp-x;
int calulatePid(float desired = -100) {
if (desired != -100) desired_temp = desired;
float e = desired_temp-temp;
int e_dot = (e - error_array.getElement(0)) / POLL_PERIOD;
int e_int = error_array.getSum() * POLL_PERIOD;
float e_dot = (e - error_array.getElement(0)) / POLL_PERIOD;
float e_int = error_array.getSum() * POLL_PERIOD;
return e * kp + e_dot * kd + e_int * ki;
}
@@ -143,8 +148,9 @@ int main()
gpio_put(13, 1);
while (true) {
printf("%f\n", test_pid.getTemp());
//printf("asdf");
float val = test_pid.measureTemp();
float pid = test_pid.calulatePid(80);
printf("temp: %f\npid: %f", val, pid);
sleep_ms(500);
}
}