Changeset - 7e85f405b6d0
[Not reviewed]
default
0 1 0
Ethan Zonca - 10 years ago 2014-08-23 23:36:42
ez@ethanzonca.com
PID setting now more user friendly
1 file changed with 99 insertions and 18 deletions:
main.c
99
18
0 comments (0 inline, 0 general)
main.c
Show inline comments
 
@@ -6,28 +6,28 @@
 
#include "hw_config.h"
 
#include "usb_lib.h"
 
#include "usb_desc.h"
 
#include "usb_pwr.h"
 
 
#define LED_POWER GPIOB,GPIO_Pin_9
 
#define LED_STAT  GPIOA,GPIO_Pin_15
 
 
#define MAX_CS GPIOB,GPIO_Pin_12
 
 
// TODO: Grab buttonpresses with interrupts
 
#define SW_BTN  GPIOB, GPIO_Pin_3
 
#define SW_UP   GPIOB, GPIO_Pin_7
 
#define SW_UP   GPIOB, GPIO_Pin_4
 
#define SW_DOWN GPIOB, GPIO_Pin_6
 
#define SW_LEFT GPIOB, GPIO_Pin_5
 
#define SW_RIGHT GPIOB, GPIO_Pin_4
 
#define SW_RIGHT GPIOB, GPIO_Pin_7
 
 
// USB Supporting Vars
 
extern __IO uint8_t Receive_Buffer[64];
 
extern __IO  uint32_t Receive_length ;
 
extern __IO  uint32_t length ;
 
uint8_t Send_Buffer[64];
 
uint32_t packet_sent=1;
 
uint32_t packet_receive=1;
 
 
 
char* itoa(int16_t i, char b[]){
 
    char const digit[] = "0123456789";
 
@@ -204,27 +204,27 @@ void update_temp() {
 
    }
 
    else {
 
        temp = temp_pre;
 
    }
 
 
    // Deassert CS
 
    Delay(1);
 
    GPIO_SetBits(MAX_CS);
 
}
 
 
 
int32_t setpoint = 0;
 
int32_t p = 1;
 
int32_t i = 1;
 
int32_t d = 1;
 
uint16_t k_p = 1;
 
uint16_t k_i = 1;
 
uint16_t k_d = 1;
 
 
 
// Process things
 
void process()
 
{
 
    update_temp(); // Read MAX31855
 
 
    // TODO: Add calibration offset (linear)
 
 
    // Perform PID calculations
 
    //if(
 
    GPIO_SetBits(LED_STAT);
 
@@ -239,169 +239,250 @@ enum state {
 
    STATE_SETP,
 
    STATE_SETI,
 
    STATE_SETD,
 
 
    STATE_PREHEAT_BREW,
 
    STATE_MAINTAIN_BREW,
 
    STATE_PREHEAT_STEAM,
 
    STATE_MAINTAIN_STEAM,
 
};
 
 
 
uint8_t state = STATE_IDLE;
 
uint8_t goto_mode = 2;
 
 
// State machine
 
void machine()
 
{
 
    uint8_t last_state = state;
 
 
    switch(state)
 
    {
 
        // Idle state
 
        case STATE_IDLE:
 
        {
 
            // Write text to OLED
 
            // [ therm :: idle ]
 
            ssd1306_DrawString("therm :: idle ", 0, 40);
 
 
            ssd1306_drawlogo();
 
            char tempstr[9];
 
            char tempstr[5];
 
            itoa_fp(temp, temp_frac, tempstr);
 
            ssd1306_DrawString("Temp: ", 2, 40);
 
            ssd1306_DrawString("    ", 2, 70);
 
            ssd1306_DrawString(tempstr, 2, 72);
 
            ssd1306_DrawString("Temp: ", 3, 40);
 
            ssd1306_DrawString("    ", 3, 70);
 
            ssd1306_DrawString(tempstr, 3, 72);
 
 
            ssd1306_drawlogo();
 
 
            switch(goto_mode) {
 
                case 2:
 
                {
 
                    ssd1306_DrawString("-> brew     ", 1, 40);
 
                } break;
 
 
                case 1:
 
                {
 
                    ssd1306_DrawString("-> set P/I/D", 1, 40);
 
                } break;
 
 
                case 0:
 
                {
 
                    ssd1306_DrawString("-> setup    ", 1, 40);
 
                } break;
 
            }
 
 
            // Button handler
 
            if(!GPIO_ReadInputDataBit(SW_BTN)) {
 
                state = STATE_SETP;
 
                switch(goto_mode) {
 
                    case 2:
 
                        state = STATE_PREHEAT_BREW;
 
                        break;
 
                    case 1:
 
                        state = STATE_SETP;
 
                        break;
 
                    case 0:
 
                        state = STATE_SETP;
 
                        break;
 
                    default:
 
                        state = STATE_PREHEAT_BREW;
 
                }
 
            }
 
            else if(!GPIO_ReadInputDataBit(SW_UP) && goto_mode < 2) {
 
                goto_mode++;
 
            }
 
            else if(!GPIO_ReadInputDataBit(SW_DOWN) && k_p > 0 && goto_mode > 0) {
 
                goto_mode--;
 
            }
 
 
 
            // Event Handler
 
            // N/A
 
 
        } break;
 
 
        case STATE_SETP:
 
        {
 
            // Write text to OLED
 
            // [ therm :: set p ]
 
            // [ p = 12         ]
 
            ssd1306_DrawString("therm :: set p", 0, 40);
 
            ssd1306_DrawString("Proportional", 0, 40);
 
 
            char tempstr[6];
 
            itoa(k_p, tempstr);
 
            ssd1306_DrawString("P=", 1, 45);
 
            ssd1306_DrawString("    ", 1, 57);
 
            ssd1306_DrawString(tempstr, 1, 57);
 
 
            ssd1306_DrawString("Press to accept", 3, 40);
 
            
 
            // Button handler
 
            if(!GPIO_ReadInputDataBit(SW_BTN)) {
 
                state = STATE_SETI;
 
            }
 
            else if(!GPIO_ReadInputDataBit(SW_UP)) {
 
                k_p++;
 
            }
 
            else if(!GPIO_ReadInputDataBit(SW_DOWN) && k_p > 0) {
 
                k_p--;
 
            }
 
 
            // Event Handler
 
            // N/A
 
 
 
        } break;
 
 
        case STATE_SETI:
 
        {
 
            // Write text to OLED
 
            // [ therm :: set i ]
 
            // [ i = 12         ]
 
            ssd1306_DrawString("therm :: set i", 0, 40);
 
            ssd1306_DrawString("Integral", 0, 40);
 
 
            char tempstr[6];
 
            itoa(k_i, tempstr);
 
            ssd1306_DrawString("I=", 1, 45);
 
            ssd1306_DrawString("    ", 1, 57);
 
            ssd1306_DrawString(tempstr, 1, 57);
 
 
            ssd1306_DrawString("Press to accept", 3, 40);
 
            
 
            // Button handler
 
            if(!GPIO_ReadInputDataBit(SW_BTN)) {
 
                state = STATE_SETD;
 
            }
 
            else if(!GPIO_ReadInputDataBit(SW_UP)) {
 
                k_i++;
 
            }
 
            else if(!GPIO_ReadInputDataBit(SW_DOWN) && k_i > 0) {
 
                k_i--;
 
            }
 
 
 
            // Event Handler
 
            // N/A
 
 
 
        } break;
 
 
        case STATE_SETD:
 
        {
 
            // Write text to OLED
 
            // [ therm :: set d ]
 
            // [ d = 12         ]
 
            ssd1306_DrawString("therm :: set d", 0, 40);
 
            ssd1306_DrawString("Derivative", 0, 40);
 
 
            char tempstr[6];
 
            itoa(k_d, tempstr);
 
            ssd1306_DrawString("D=", 1, 45);
 
            ssd1306_DrawString("    ", 1, 57);
 
            ssd1306_DrawString(tempstr, 1, 57);
 
 
            ssd1306_DrawString("Press to accept", 3, 40);
 
 
            // Button handler
 
            if(!GPIO_ReadInputDataBit(SW_BTN)) {
 
                state = STATE_IDLE;
 
            }
 
            else if(!GPIO_ReadInputDataBit(SW_UP)) {
 
                k_d++;
 
            }
 
            else if(!GPIO_ReadInputDataBit(SW_DOWN) && k_d > 0) {
 
                k_d--;
 
            }
 
 
            // Event Handler
 
            // N/A
 
 
 
        } break;
 
 
        case STATE_PREHEAT_BREW:
 
        {
 
            // Write text to OLED
 
            // [ therm : preheating brew ]
 
            // [ 30 => 120 C             ]
 
            ssd1306_DrawString("therm :: Bpreheat", 0, 40);
 
            ssd1306_DrawString("Preheating...", 0, 40);
 
 
            // Button handler
 
            if(!GPIO_ReadInputDataBit(SW_BTN)) {
 
                state = STATE_IDLE;
 
            }
 
 
            // Event Handler
 
            if(temp >= setpoint) {
 
                state = STATE_MAINTAIN_BREW;
 
            }
 
 
 
        } break;
 
 
        case STATE_MAINTAIN_BREW:
 
        {
 
            // Write text to OLED
 
            // [ therm : ready to brew ]
 
            // [ 30 => 120 C           ]
 
            ssd1306_DrawString("therm :: Bready", 0, 40);
 
            ssd1306_DrawString("Ready to Brew!", 0, 40);
 
 
 
            // Button handler
 
            if(!GPIO_ReadInputDataBit(SW_BTN)) {
 
                state = STATE_IDLE;
 
            }
 
 
            // Event Handler
 
            // N/A
 
 
 
        } break;
 
 
        case STATE_PREHEAT_STEAM:
 
        {
 
            // Write text to OLED
 
            // [ therm : preheating steam ]
 
            // [ 30 => 120 C           ]
 
            ssd1306_DrawString("therm :: Spreheat", 0, 40);
 
            ssd1306_DrawString("Preheating...", 0, 40);
 
 
            // Button handler
 
            if(!GPIO_ReadInputDataBit(SW_BTN)) {
 
                state = STATE_IDLE;
 
            }
 
 
            // Event Handler
 
            if(temp >= setpoint) {
 
                state = STATE_MAINTAIN_STEAM;
 
            }
 
 
 
        } break;
 
 
        case STATE_MAINTAIN_STEAM:
 
        {
 
            // Write text to OLED
 
            // [ therm : ready to steam ]
 
            // [ 30 => 120 C            ]
 
            ssd1306_DrawString("therm :: Sready", 0, 40);
 
            ssd1306_DrawString("Ready to Steam!", 0, 40);
 
 
            // Button handler
 
            if(!GPIO_ReadInputDataBit(SW_BTN)) {
 
                state = STATE_IDLE;
 
            }
 
 
            // Event Handler
 
            // N/A
 
 
 
        } break;
 
 
0 comments (0 inline, 0 general)