fix temp sensor kind of

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