start writing thermistor code

This commit is contained in:
2026-09-21 18:13:14 +02:00
parent 986ef55b05
commit 0794536e6d
2 changed files with 67 additions and 13 deletions
+2 -1
View File
@@ -46,7 +46,8 @@ pico_enable_stdio_usb(main 1)
# Add the standard library to the build
target_link_libraries(main
pico_stdlib)
pico_stdlib
hardware_adc)
# Add the standard include files to the build
target_include_directories(main PRIVATE
+64 -11
View File
@@ -2,11 +2,15 @@
#include <stdlib.h>
#include <array>
#include "pico/stdlib.h"
#include <math.h>
#include "hardware/gpio.h"
#include "hardware/adc.h"
using namespace std;
#define POLL_PERIOD 50 //time between polls (ms)
#define MAX_STORED_VALUES 10 //number of stored values to calculate e_dot and e_dot_dot values
#define REFERENCE_VOLTAGE 3.3 //reference voltage for the adc converter
/*
trying to keep the code clean, so some conventions:
@@ -15,13 +19,15 @@ function: camelCase
var names: snake_case
*/
//thermistor equation is R=R_0*e^(1/T-1/T_0)
class CustomArray {
int* items;
int size;
public:
CustomArray() {
size = MAX_STORED_VALUES;
CustomArray(int s = MAX_STORED_VALUES) {
size = s;
items = (int*) malloc(size * sizeof(int));
for (int i = 0; i < size; i++ ) items[i] = 0;
}
@@ -52,14 +58,54 @@ class CustomArray {
class PID {
private:
int kp, ki, kd, pwm_pin, thermistor_pin, index, desired_temp;
int kp, ki, kd, //PID
pwm_pin, thermistor_pin, //thermistor
thermistor_resistance, voltage_devider_resistance, thermistor_beta, thermistor_ref_temp, //thermistor
desired_temp, temp, //temperature
index; //misc
bool on;
CustomArray error_array;
public:
PID(int kp) {
PID(
int index,
int pwm_pin,
int thermistor_pin,
int 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
) {
error_array = CustomArray();
this->kp = kp;
this->ki = ki;
this->kd = kd;
this->thermistor_pin = thermistor_pin;
this->thermistor_resistance = thermistor_resistance;
this->thermistor_beta = thermistor_beta;
this->voltage_devider_resistance = voltage_devider_resistance;
this->desired_temp = desired_temp;
this->index = index;
measureTemp();
}
int getTemp() {
return this->temp;
}
int measureTemp() {
//adc_select_input(thermistor_pin);
//uint16_t result = adc_read();
this->temp = 10;
return this->temp;
}
int calulatePid( int x ) {
@@ -68,26 +114,33 @@ class PID {
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;
}
};
void init() {
stdio_init_all();
adc_init();
gpio_init(25);
gpio_init(13);
gpio_set_dir(25, GPIO_OUT);
gpio_set_dir(13, GPIO_OUT);
adc_gpio_init(27);
}
int main()
{
stdio_init_all();
init();
PID test_pid = PID(0, 6, 1);
gpio_put(13, 1);
while (true) {
//printf("Hello, world!\n");
gpio_put(25, 0);
sleep_ms(250);
gpio_put(25, 1);
sleep_ms(250);
printf("%f\n", test_pid.getTemp());
//printf("asdf");
sleep_ms(500);
}
}