Changeset - 6d43230d5986
[Not reviewed]
tip rtd
0 3 0
matthewreed - 6 years ago 2018-11-25 21:08:06

Untested changes for RTDs
3 files changed with 25 insertions and 9 deletions:
0 comments (0 inline, 0 general)
display.c
Show inline comments
 
#include "display.h"
 

	
 

	
 
// Private function prototypes
 
static void draw_setpoint(therm_status_t* status);
 

	
 

	
 
// Button transition variables
 
static uint8_t sw_btn_last = 0;
 
static uint8_t sw_up_last = 0;
 
static uint8_t sw_down_last = 0;
 
static uint8_t sw_left_last = 0;
 
static uint8_t sw_right_last = 0;
 

	
 

	
 
// Buttonpress macros
 
#define SW_BTN_PRESSED (sw_btn_last == 0 && sw_btn == 1) // rising edge on buttonpress
 
#define SW_UP_PRESSED (sw_up_last == 0 && sw_up == 1)
 
#define SW_DOWN_PRESSED (sw_down_last == 0 && sw_down == 1)
 
#define SW_LEFT_PRESSED (sw_left_last == 0 && sw_left == 1)
 
#define SW_RIGHT_PRESSED (sw_right_last == 0 && sw_right == 1)
 

	
 

	
 
// States
 
static uint8_t trigger_drawsetpoint = 1;
 
static int16_t last_temp = 21245;
 
static int16_t last_temp_frac = 21245;
 
static int16_t last_state = STATE_IDLE;
 
static int16_t last_state = STATE_RESET;
 
static uint8_t goto_mode = MODE_HEAT;
 
static uint8_t reset_mode = RESET_REBOOT;
 

	
 

	
 

	
 
// Display state machine
 
void display_process(therm_settings_t* set, therm_status_t* status)
 
{
 
    uint8_t state_changed = status->state != last_state;
 
    last_state = status->state;
 
    
 
    uint8_t temp_changed = status->temp != last_temp || status->temp_frac != last_temp_frac;
 
    last_temp = status->temp;
 
    last_temp_frac = status->temp_frac;
 

	
 
    uint8_t sw_btn = !HAL_GPIO_ReadPin(SW_BTN);
 
    uint8_t sw_up = !HAL_GPIO_ReadPin(SW_UP);
 
    uint8_t sw_down = !HAL_GPIO_ReadPin(SW_DOWN);
 
    uint8_t sw_left = !HAL_GPIO_ReadPin(SW_LEFT);
 
    uint8_t sw_right = !HAL_GPIO_ReadPin(SW_RIGHT);
 

	
 
    switch(status->state)
 
    {
 
        // Idle state
 
        case STATE_IDLE:
 
        {
 
            // Write text to OLED
 
            // [ therm :: idle ]
 
            ssd1306_drawstring("therm :: idle ", 0, 40);
 
            status->pid_enabled = 0;
 

	
 
            if(temp_changed || state_changed) {
 
                char tempstr[6];
 
                itoa_fp(status->temp, status->temp_frac, tempstr);
 
                ssd1306_drawstring("Temp: ", 3, 40);
 
                ssd1306_drawstring("    ", 3, 72);
 
                ssd1306_drawstring(tempstr, 3, 72);
 
            }
 

	
 
            if (state_changed) {
 
            	ssd1306_drawlogo();
 
            }
 

	
 
            switch(goto_mode) {
 

	
 
                case MODE_HEAT:
 
                {
 
                    ssd1306_drawstring("-> heat     ", 1, 40);
 
@@ -364,106 +364,119 @@ void display_process(therm_settings_t* s
 
            }
 
            else {
 
                user_input((uint16_t*)&set->val.setpoint_brew);
 
            }
 

	
 
            // Event Handler
 
            if(status->temp >= status->setpoint) {
 
                status->state = STATE_MAINTAIN;
 
            }
 
 
 
        } break;
 

	
 
        case STATE_MAINTAIN:
 
        {
 
            // Write text to OLED
 
            // [ therm : ready to brew ]
 
            // [ 30 => 120 C           ]
 
            ssd1306_drawstring("Preheated!", 0, 0);
 
            //ssd1306_drawlogo();
 
            draw_setpoint(status);
 
            status->pid_enabled = 1;
 
            status->setpoint = set->val.setpoint_brew;
 

	
 
            // Button handler
 
            if(SW_BTN_PRESSED) {
 
                status->state = STATE_IDLE;
 
            }
 
            else {
 
                user_input((uint16_t*)&set->val.setpoint_brew);
 
            }
 

	
 
            // Event Handler
 
            // N/A
 
 
 
        } break;
 

	
 
        // Thermocouple error
 
        case STATE_TC_ERROR:
 
        {
 
            // Write text to OLED
 
            // [ therm : ready to steam ]
 
            // [ 30 => 120 C            ]
 
            ssd1306_drawstring("Error:              ", 0, 0);
 

	
 
            char tempstr[6];
 
            itoa(status->error_code, tempstr, 10);
 
            ssd1306_drawstring(tempstr, 0, 57);
 

	
 
            //TODO: add RTD error codes
 

	
 
			#ifdef MAX31865_RTD_SENSOR
 
            // RTD error codes
 
            if(status->error_code & 0b00000100)
 
                ssd1306_drawstring(" RTD Over/Undervolt", 1, 0);
 
            else if(status->error_code & 0b00001000)
 
                ssd1306_drawstring("    RTD FORCE- Open", 1, 0);
 
            else if(status->error_code & 0b00010000)
 
                ssd1306_drawstring("RTD REF FORCE- Open", 1, 0);
 
            else if(status->error_code & 0b00100000)
 
                ssd1306_drawstring("    RTD REFIN- High", 1, 0);
 
            else
 
                ssd1306_drawstring("#?, Unknown Error", 1, 0);
 
			#else
 
            // TC error codes
 
            if(status->error_code == 1)
 
                ssd1306_drawstring("    TC Open Circuit", 1, 0);
 
            else if(status->error_code == 4)
 
                ssd1306_drawstring("    TC Short to GND", 1, 0);
 
            else if(status->error_code == 8)
 
                ssd1306_drawstring("    TC Short to VCC", 1, 0);
 
            else
 
                ssd1306_drawstring("#?, Unknown Error", 1, 0);
 
			#endif
 
            ssd1306_drawstring("                    ", 2, 0);
 

	
 
            ssd1306_drawstring("-> to ignore all or", 2, 0);
 
            ssd1306_drawstring("press to continue", 3, 0);
 

	
 
            // Button handler
 
            if(SW_BTN_PRESSED) {
 
                status->state = STATE_IDLE;
 
				#ifdef MAX31865_RTD_SENSOR
 
				max31865_clear_errors(spi_get());
 
				#endif
 
            }
 
            else if(SW_RIGHT_PRESSED) {
 
                set->val.ignore_error = 1;
 
                status->state = STATE_IDLE;
 
            }
 
            // Event Handler
 
            // Maybe handle if TC is plugged in
 
            // N/A
 
 
 
        } break;
 

	
 

	
 
        // Reset state
 
        case STATE_RESET:
 
        {
 
            // Write text to OLED
 
            // [ therm :: reset ]
 
            ssd1306_drawstring("therm :: reset ", 0, 40);
 
            status->pid_enabled = 0;
 

	
 
            ssd1306_drawlogo();
 

	
 
            switch(reset_mode) {
 
				case RESET_DEFAULTS:
 
				{
 
					ssd1306_drawstring("-> defaults   ", 1, 40);
 
				} break;
 
                case RESET_BOOTLOADER:
 
                {
 
                    ssd1306_drawstring("-> bootloader ", 1, 40);
 
                } break;
 
                case RESET_REBOOT:
 
                {
 
                    ssd1306_drawstring("-> reboot     ", 1, 40);
 
                } break;
 
                case RESET_EXIT:
 
                {
max31865.c
Show inline comments
 
@@ -44,104 +44,107 @@ void max31865_config(SPI_HandleTypeDef* 
 
    
 
    // Release CS
 
    HAL_GPIO_WritePin(MAX_CS, 1);
 
}
 

	
 
void max31865_clear_errors(SPI_HandleTypeDef* hspi1) {
 
	max31865_config(hspi1);
 
}
 

	
 
// Grab temperature reading from MAX31865
 
void max31865_readtemp(SPI_HandleTypeDef* hspi1, therm_settings_t* set, therm_status_t* status)
 
{
 

	
 
    // TODO: Set RTD ref resistance in set struct
 

	
 
    // Assert CS
 
    HAL_GPIO_WritePin(MAX_CS, 0);
 
    
 
    uint8_t regh = MAX31865_REG_RTD_MSB;
 
    
 
    uint8_t rxdatah[1] = {0x00};
 
    uint8_t rxdatal[1] = {0x00};
 

	
 
    HAL_SPI_Transmit(hspi1, &regh, 1, 100);
 
    HAL_SPI_Receive(hspi1, rxdatah, 1, 100);
 
    HAL_SPI_Receive(hspi1, rxdatal, 1, 100);
 

	
 
    // Release CS
 
    HAL_GPIO_WritePin(MAX_CS, 1);
 

	
 
    // Assemble data array into one var
 
    uint16_t adc_count = ((rxdatah[0] & 0x7F) << 8) | rxdatal[0];
 
    
 
    if((rxdatah[0] & 0x80) && !set->val.ignore_error) {
 

	
 
        // Assert CS
 
        HAL_GPIO_WritePin(MAX_CS, 0);
 

	
 
        uint8_t reg = MAX31865_REG_FAULTSTATUS;
 

	
 
        uint8_t data[1] = {0x00};
 

	
 
        HAL_SPI_Transmit(hspi1, &reg, 1, 100);
 
        HAL_SPI_Receive(hspi1, data, 1, 100);
 

	
 
        // Release CS
 
        HAL_GPIO_WritePin(MAX_CS, 1);
 

	
 
        // check to see if it's an error we care about
 
        if(data[0] & 0b00111100) {
 
        status->error_code = data[0];
 

	
 
        HAL_Delay(400); // FIXME: remove?
 
        status->state_resume = status->state;
 
        status->state = STATE_TC_ERROR;
 
        status->temp = 0;
 
        status->temp_frac = 0;
 
        
 
        }
 
        else {
 
        	max31865_clear_errors(spi_get());
 
        }
 
    }
 
    else 
 
    {
 
        // Convert to Fahrenheit
 
        if(set->val.temp_units == TEMP_UNITS_FAHRENHEIT)
 
        {
 
        	//use all fixed point math!
 

	
 
        	//convert adc to resistance
 
        	//Rrtd = adc / range * Rref
 
        	status->temp = (int32_t) adc_count * 48600L;
 
        	status->temp = (int32_t) adc_count * 40200L;
 
        	status->temp /= 32768L;
 
        	//resistance to temp
 
        	//(x - in1) * (cal2 - cal1) / (in2 - in1) + cal1
 
        	status->temp = ((status->temp) - 10000L) * (39200L - 3200L) / (17586L - 10000L) + 3200L;
 
        	//grab the fraction
 
        	status->temp_frac = (status->temp / 10) % 10L;
 
        	//scale back to degrees
 
        	status->temp = status->temp / 100L;
 
        	//add in the offset
 
            status->temp += set->val.temp_offset;
 
        }
 

	
 
        // Convert to Celsius
 
        else
 
        {
 
        	//use all fixed point math!
 

	
 
        	//convert adc to resistance
 
        	//Rrtd = adc / range * Rref
 
        	status->temp = (int32_t) adc_count * 48600L;
 
        	status->temp = (int32_t) adc_count * 40200L;
 
        	status->temp /= 32768L;
 
        	//resistance to temp
 
        	//(x - in1) * (cal2 - cal1) / (in2 - in1) + cal1
 
        	status->temp = (((status->temp) - 10000L) * (20000L- 0L)) / (17586L - 10000L) + 0L;
 
        	//grab the fraction
 
        	status->temp_frac = (status->temp / 10) % 100L;
 
        	//scale back to degrees
 
        	status->temp = status->temp / 100L;
 
        	//add in the offset
 
            status->temp += set->val.temp_offset;
 
        }
 
    }
 

	
 

	
 
}
 

	
 
// vim:softtabstop=4 shiftwidth=4 expandtab 
pid.c
Show inline comments
 
#include "pid.h"
 

	
 
// PID implementation
 

	
 
void pid_init(pid_state_t* state)
 
{
 
	state->i_state = 0;
 
	state->last_pid_temp = 0;
 
	state->last_pid_temp_frac = 0;
 
}
 

	
 
int16_t pid_update(therm_settings_t* set, therm_status_t* status, pid_state_t *state)
 
{
 

	
 
  // Convert temperature to fixed point number with 1/10th resolution
 
  int8_t temp_frac = status->temp_frac > 9 ? status->temp_frac / 10 : status->temp_frac;
 
  temp_frac = status->temp > 0 ? temp_frac : temp_frac * -1;
 
  int32_t temp = (status->temp * 10) + temp_frac;
 

	
 
  // Calculate instantaneous error
 
  int16_t error = status->setpoint * 10 - temp; // TODO: Use fixed point fraction
 
  int16_t error = status->setpoint * 10 - temp;
 

	
 
  // Proportional component
 
  int32_t p_term = set->val.k_p * error;
 

	
 
  // Error accumulator (integrator)
 
  state->i_state += error;
 

	
 
  // to prevent the iTerm getting huge from lots of 
 
  //  error, we use a "windup guard" 
 
  // (this happens when the machine is first turned on and
 
  // it cant help be cold despite its best efforts)
 
  // not necessary, but this makes windup guard values 
 
  // relative to the current iGain
 
  int32_t windup_guard_res = (set->val.windup_guard * 10) / set->val.k_i;
 

	
 
  // Calculate integral term with windup guard 
 
  if (state->i_state > windup_guard_res)
 
	  state->i_state = windup_guard_res;
 
  else if (state->i_state < -windup_guard_res)
 
	  state->i_state = -windup_guard_res;
 

	
 
  int32_t i_term = set->val.k_i * state->i_state;
 

	
 
  // Calculate differential term (slope since last iteration)
 
  int32_t d_term = (set->val.k_d * (temp - state->last_pid_temp));
 

	
 
  // Save temperature for next iteration
 
  state->last_pid_temp = temp;
 

	
 
  int16_t result = (p_term + i_term - d_term) / 10;
 

	
 
  // Put out tenths of percent, 0-1000. 
 
  if(result > 1000)
 
    result = 1000;
 
  else if(result < -1000)
 
    result = -1000;
 

	
 
  // Return feedback
 
  return result;
 
}
 

	
 

	
0 comments (0 inline, 0 general)