~mrp/EC155_controller

2354f45ee5c32f0857e8c2d894982d17d2a99318 — Mark Penner 2 years ago 4416264
adjust sample time shorter for quicker response when turning pump on

measured loop time was around 500 ms, so trying that for sample time
1 files changed, 21 insertions(+), 19 deletions(-)

M EC155Controller.ino
M EC155Controller.ino => EC155Controller.ino +21 -19
@@ 9,7 9,10 @@ const uint8_t temp_pot = A3;
const uint8_t pump_pot = A2;
const uint8_t temp_sw = 2;

const uint16_t sample_time = 833; // milliseconds; 100 half cycles at 60 Hz
// 833 ms is 100 half cycles at 60 Hz results in resolution of 1 in 100
// loop time turned out to be around 500 ms which is a resolution of 1 in 60 @ 60 Hz
// so trying that
const uint16_t sample_time = 500;

// 12 MHz / (1024 (prescaler) * 2 (timer counts up and down) * (pwm frequency))
const int16_t top_count = F_CPU / (1024 * 2 * (1000.0 / sample_time));


@@ 19,9 22,9 @@ const uint8_t p_gain_idle = 4;
const uint8_t p_gain_pump = 20;
const uint8_t i_band = 10;

// samples not seconds, could instead use: (seconds / (sample_time / 1000.0));
const uint8_t i_time = 127;
const uint8_t d_time = 3;
// time constant stores samples not seconds: seconds / (sample_time / 1000.0);
const uint8_t i_time = 106 / (sample_time / 1000.0);
const uint8_t d_time = 2.5 / (sample_time / 1000.0);

uint8_t set_temp[] = {93, 115};
uint8_t temp_index;


@@ 42,29 45,27 @@ uint8_t p_gain = p_gain_idle;
SparkFunMAX31855k probe(tc_pin); 
SerLCD lcd;

// TODO use interrupts for PID timing

void setup() {
    pinMode(heater, OUTPUT);
    pinMode(pump, OUTPUT);
    pinMode(temp_pot, INPUT);
    pinMode(pump_pot, INPUT);
    pinMode(temp_sw, INPUT);
    

    // set up Timer 1
    TCCR1A = _BV(COM1A1) | _BV(COM1B1) | _BV(WGM11);
    TCCR1B = _BV(WGM13) | _BV(CS12) | _BV(CS10);
    ICR1 = top_count;
    OCR1A = 0;
    OCR1B = 0;
    

    Wire.begin();
    lcd.begin(Wire);
    lcd.setBacklight(0, 255, 0);
    lcd.setContrast(0);
    lcd.setCursor(0, 0);
    lcd.print("Initializing...");
    

    do {
        raw_temp = probe.readTempC();
    } while (isnan(raw_temp));


@@ 72,6 73,7 @@ void setup() {
    last_temp = temp;
}
void loop() {
//    uint32_t last_loop_time = total_time;
    total_time = millis();
    if (pump_pwm) {
        pump_time = (total_time - start_time) / 1000;


@@ 95,7 97,7 @@ void loop() {
    if (!isnan(raw_temp)) {
        temp = t_alpha * raw_temp + (1 - t_alpha) * temp;
    }
    

    if (total_time - last_time >= sample_time) {
        d_temp = d_alpha * (temp - last_temp) + (1 - d_alpha) * d_temp;



@@ 104,9 106,9 @@ void loop() {
            i_term += 1.0 / i_time * error;
        }
        i_term = (i_term > 3) ? 3 : i_term;
        

        heater_pwm = p_gain * (error + i_term - d_time * d_temp);
        

        if (heater_pwm > 100) {
            heater_pwm = 100;
        } else if (heater_pwm < 0) {


@@ 115,11 117,11 @@ void loop() {

        OCR1A = top_count * 0.01 * heater_pwm;
        OCR1B = top_count * 0.01 * pump_pwm;
        

        last_temp = temp;
        last_time = total_time;
    }
    

    lcd.setCursor(0, 0);
    if (set_temp[temp_index] < 100) lcd.print(' ');
    lcd.print(set_temp[temp_index]);


@@ 137,11 139,11 @@ void loop() {
    if (temp < 100) lcd.print(' ');
    lcd.print(temp);
    lcd.print("       ");
//     lcd.setCursor(7, 1);
//     lcd.print(i_term);
//     lcd.print(' ');
//     lcd.setCursor(12, 1);
//     lcd.print(d_time * d_temp);
//    lcd.setCursor(7, 1);
//    lcd.print(total_time - last_loop_time);
//    lcd.print(' ');
//    lcd.setCursor(12, 1);
//    lcd.print(d_time * d_temp);
    lcd.setCursor(13, 1);
    if (pump_time < 100) lcd.print(' ');
    if (pump_time < 10) lcd.print(' ');