PID regulating (fix error being unsigned..)
This commit is contained in:
@@ -10,6 +10,7 @@
|
|||||||
#include "stm32f10x.h"
|
#include "stm32f10x.h"
|
||||||
#include "S100V0_1.h"
|
#include "S100V0_1.h"
|
||||||
#include "Bios.h"
|
#include "Bios.h"
|
||||||
|
#include "Interrupt.h"
|
||||||
extern volatile uint16_t ADC1ConvertedValue[2];
|
extern volatile uint16_t ADC1ConvertedValue[2];
|
||||||
|
|
||||||
uint16_t Get_ADC1Value(uint8_t i);
|
uint16_t Get_ADC1Value(uint8_t i);
|
||||||
|
|||||||
@@ -10,7 +10,7 @@
|
|||||||
#include "Interrupt.h"
|
#include "Interrupt.h"
|
||||||
#include "S100V0_1.h"
|
#include "S100V0_1.h"
|
||||||
#include "Oled.h"
|
#include "Oled.h"
|
||||||
|
#include "PID.h"
|
||||||
enum {
|
enum {
|
||||||
STARTUP, //we are sitting on the prompt to push a button
|
STARTUP, //we are sitting on the prompt to push a button
|
||||||
SOLDERING,
|
SOLDERING,
|
||||||
|
|||||||
@@ -103,8 +103,16 @@ uint16_t Get_ADC1Value(uint8_t i) {
|
|||||||
//This returns the calibrated temperature reading of the iron temp
|
//This returns the calibrated temperature reading of the iron temp
|
||||||
uint16_t readIronTemp(uint16_t calibration_temp) {
|
uint16_t readIronTemp(uint16_t calibration_temp) {
|
||||||
static uint16_t calTemp = 0;
|
static uint16_t calTemp = 0;
|
||||||
|
static uint16_t lastVal = 0;
|
||||||
|
static uint32_t lastUpdate = 0;
|
||||||
if (calibration_temp != 0)
|
if (calibration_temp != 0)
|
||||||
calTemp = calibration_temp;
|
calTemp = calibration_temp;
|
||||||
return (readTipTemp() * 1000 + 806 * readSensorTemp() - calTemp * 1000)
|
if (millis() - lastUpdate > 50) {
|
||||||
/ 806;
|
lastVal = (readTipTemp() * 1000 + 806 * readSensorTemp()
|
||||||
|
- calTemp * 1000) / 806;
|
||||||
|
lastUpdate = millis();
|
||||||
|
}
|
||||||
|
|
||||||
|
return lastVal;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -258,7 +258,7 @@ void Init_EXTI(void) {
|
|||||||
NVIC_InitTypeDef NVIC_InitStructure;
|
NVIC_InitTypeDef NVIC_InitStructure;
|
||||||
|
|
||||||
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,
|
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,
|
||||||
GPIO_PinSource6 | GPIO_PinSource9);
|
GPIO_PinSource6 | GPIO_PinSource9);
|
||||||
|
|
||||||
/* Configure EXTI0 line */
|
/* Configure EXTI0 line */
|
||||||
EXTI_InitStructure.EXTI_Line = EXTI_Line6 | EXTI_Line9;
|
EXTI_InitStructure.EXTI_Line = EXTI_Line6 | EXTI_Line9;
|
||||||
|
|||||||
@@ -15,7 +15,7 @@
|
|||||||
#include "Bios.h"
|
#include "Bios.h"
|
||||||
#include "I2C.h"
|
#include "I2C.h"
|
||||||
#include "MMA8652FC.h"
|
#include "MMA8652FC.h"
|
||||||
|
#include "PID.h"
|
||||||
#include "Oled.h"
|
#include "Oled.h"
|
||||||
#include "Interrupt.h"
|
#include "Interrupt.h"
|
||||||
|
|
||||||
@@ -35,10 +35,11 @@ int main(void) {
|
|||||||
|
|
||||||
Init_Oled(); //init the OLED display
|
Init_Oled(); //init the OLED display
|
||||||
Clear_Screen(); //clear the display buffer to black
|
Clear_Screen(); //clear the display buffer to black
|
||||||
systemSettings.SleepTemp = 200;
|
systemSettings.SleepTemp = 1000;
|
||||||
systemSettings.SleepTime = 1;
|
systemSettings.SleepTime = 1;
|
||||||
systemSettings.SolderingTemp = 320;
|
systemSettings.SolderingTemp = 1500;
|
||||||
readIronTemp(239);//load the default calibration value
|
readIronTemp(239); //load the default calibration value
|
||||||
|
setupPID(); //init the PID values
|
||||||
//OLED_DrawString("TEST012",7);
|
//OLED_DrawString("TEST012",7);
|
||||||
|
|
||||||
/*for (;;) {
|
/*for (;;) {
|
||||||
|
|||||||
@@ -40,8 +40,15 @@ void ProcessUI() {
|
|||||||
operatingMode = SLEEP; //Goto Sleep Mode
|
operatingMode = SLEEP; //Goto Sleep Mode
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
//If no buttons pushed we need to read the current temperatures
|
/*if (millis() - getLastButtonPress()
|
||||||
// and then setup the drive signal time for the iron
|
> (systemSettings.SleepTime * 60000))
|
||||||
|
operatingMode = SLEEP;
|
||||||
|
return;*/
|
||||||
|
//If no buttons pushed we need to perform the PID loop for the iron temp
|
||||||
|
int32_t newOutput = computePID(systemSettings.SolderingTemp);
|
||||||
|
if (newOutput >= 0) {
|
||||||
|
setIronTimer(newOutput);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case TEMP_ADJ:
|
case TEMP_ADJ:
|
||||||
@@ -99,15 +106,21 @@ void ProcessUI() {
|
|||||||
if (Buttons & BUT_A) {
|
if (Buttons & BUT_A) {
|
||||||
//A Button was pressed so we are moving back to soldering
|
//A Button was pressed so we are moving back to soldering
|
||||||
operatingMode = SOLDERING;
|
operatingMode = SOLDERING;
|
||||||
|
return;
|
||||||
} else if (Buttons & BUT_B) {
|
} else if (Buttons & BUT_B) {
|
||||||
//B Button was pressed so we are moving back to soldering
|
//B Button was pressed so we are moving back to soldering
|
||||||
operatingMode = SOLDERING;
|
operatingMode = SOLDERING;
|
||||||
|
return;
|
||||||
} else if (systemSettings.movementEnabled)
|
} else if (systemSettings.movementEnabled)
|
||||||
if (millis() - getLastMovement() > 100) {
|
if (millis() - getLastMovement() > 100) {
|
||||||
operatingMode = SOLDERING; //Goto active mode again
|
operatingMode = SOLDERING; //Goto active mode again
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
//else if nothing has been pushed we need to compute the PID to keep the iron at the sleep temp
|
||||||
|
int32_t newOutput = computePID(systemSettings.SleepTemp);
|
||||||
|
if (newOutput >= 0) {
|
||||||
|
setIronTimer(newOutput);
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
@@ -132,7 +145,7 @@ void DrawUI() {
|
|||||||
//We draw in the current system temp and an indicator if the iron is heating or not
|
//We draw in the current system temp and an indicator if the iron is heating or not
|
||||||
//First lets draw the current tip temp
|
//First lets draw the current tip temp
|
||||||
//OLED_DrawString("SOLD ", 6);
|
//OLED_DrawString("SOLD ", 6);
|
||||||
OLED_DrawThreeNumber(readIronTemp(0), 0);
|
OLED_DrawThreeNumber(readIronTemp(0)/10, 0);
|
||||||
|
|
||||||
break;
|
break;
|
||||||
case TEMP_ADJ:
|
case TEMP_ADJ:
|
||||||
|
|||||||
@@ -14,9 +14,9 @@ int32_t computePID(uint16_t setpoint) {
|
|||||||
int32_t ITerm = 0;
|
int32_t ITerm = 0;
|
||||||
static int16_t lastReading = 0;
|
static int16_t lastReading = 0;
|
||||||
if (millis() - lastSample > 50) {
|
if (millis() - lastSample > 50) {
|
||||||
//only sample every 100 milliseconds
|
//only sample every 50 milliseconds
|
||||||
uint16_t currentReading = readIronTemp(0); //get the current temp of the iron
|
uint16_t currentReading = readIronTemp(0); //get the current temp of the iron
|
||||||
uint16_t error = setpoint - currentReading; //calculate the error term
|
int16_t error = (int16_t)setpoint - (int16_t)currentReading; //calculate the error term
|
||||||
ITerm += (pidSettings.ki * error);
|
ITerm += (pidSettings.ki * error);
|
||||||
if (ITerm > MAXPIDOUTPUT)
|
if (ITerm > MAXPIDOUTPUT)
|
||||||
ITerm = MAXPIDOUTPUT;
|
ITerm = MAXPIDOUTPUT;
|
||||||
|
|||||||
Reference in New Issue
Block a user