start writing thermistor code
This commit is contained in:
+2
-1
@@ -46,7 +46,8 @@ pico_enable_stdio_usb(main 1)
|
|||||||
|
|
||||||
# Add the standard library to the build
|
# Add the standard library to the build
|
||||||
target_link_libraries(main
|
target_link_libraries(main
|
||||||
pico_stdlib)
|
pico_stdlib
|
||||||
|
hardware_adc)
|
||||||
|
|
||||||
# Add the standard include files to the build
|
# Add the standard include files to the build
|
||||||
target_include_directories(main PRIVATE
|
target_include_directories(main PRIVATE
|
||||||
|
|||||||
@@ -2,11 +2,15 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <array>
|
#include <array>
|
||||||
#include "pico/stdlib.h"
|
#include "pico/stdlib.h"
|
||||||
|
#include <math.h>
|
||||||
|
#include "hardware/gpio.h"
|
||||||
|
#include "hardware/adc.h"
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
#define POLL_PERIOD 50 //time between polls (ms)
|
#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 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:
|
trying to keep the code clean, so some conventions:
|
||||||
@@ -15,13 +19,15 @@ function: camelCase
|
|||||||
var names: snake_case
|
var names: snake_case
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
//thermistor equation is R=R_0*e^(1/T-1/T_0)
|
||||||
|
|
||||||
class CustomArray {
|
class CustomArray {
|
||||||
int* items;
|
int* items;
|
||||||
int size;
|
int size;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
CustomArray() {
|
CustomArray(int s = MAX_STORED_VALUES) {
|
||||||
size = MAX_STORED_VALUES;
|
size = s;
|
||||||
items = (int*) malloc(size * sizeof(int));
|
items = (int*) malloc(size * sizeof(int));
|
||||||
for (int i = 0; i < size; i++ ) items[i] = 0;
|
for (int i = 0; i < size; i++ ) items[i] = 0;
|
||||||
}
|
}
|
||||||
@@ -52,14 +58,54 @@ class CustomArray {
|
|||||||
|
|
||||||
class PID {
|
class PID {
|
||||||
private:
|
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;
|
bool on;
|
||||||
CustomArray error_array;
|
CustomArray error_array;
|
||||||
|
|
||||||
public:
|
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();
|
error_array = CustomArray();
|
||||||
|
|
||||||
this->kp = kp;
|
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 ) {
|
int calulatePid( int x ) {
|
||||||
@@ -68,26 +114,33 @@ class PID {
|
|||||||
int e_dot = (e - error_array.getElement(0)) / POLL_PERIOD;
|
int e_dot = (e - error_array.getElement(0)) / POLL_PERIOD;
|
||||||
int e_int = error_array.getSum() * 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;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
void init() {
|
void init() {
|
||||||
|
stdio_init_all();
|
||||||
|
adc_init();
|
||||||
|
|
||||||
gpio_init(25);
|
gpio_init(25);
|
||||||
|
gpio_init(13);
|
||||||
gpio_set_dir(25, GPIO_OUT);
|
gpio_set_dir(25, GPIO_OUT);
|
||||||
|
gpio_set_dir(13, GPIO_OUT);
|
||||||
|
|
||||||
|
adc_gpio_init(27);
|
||||||
}
|
}
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
stdio_init_all();
|
init();
|
||||||
|
|
||||||
|
PID test_pid = PID(0, 6, 1);
|
||||||
|
|
||||||
|
gpio_put(13, 1);
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
//printf("Hello, world!\n");
|
printf("%f\n", test_pid.getTemp());
|
||||||
gpio_put(25, 0);
|
//printf("asdf");
|
||||||
sleep_ms(250);
|
sleep_ms(500);
|
||||||
gpio_put(25, 1);
|
|
||||||
sleep_ms(250);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user