So you don't have to hard-code the tuning parameters or enter them every time you use the controller, we save them in the Arduino's EEPROM. This makes the code a little more elegant as you can program the controller once, then re-tune if you ever change cookers but whenever you turn on your controller, it will remember the settings from the last time you used it.
SInce EEPROM can only be written a finite number of times (typically 100,000), we compare the contents before writing and only write if something has changed. This functionality is implemented in the following helper functions:
SInce EEPROM can only be written a finite number of times (typically 100,000), we compare the contents before writing and only write if something has changed. This functionality is implemented in the following helper functions:
// ************************************************ // Save any parameter changes to EEPROM // ************************************************ void SaveParameters() { if (Setpoint != EEPROM_readDouble(SpAddress)) { EEPROM_writeDouble(SpAddress, Setpoint); } if (Kp != EEPROM_readDouble(KpAddress)) { EEPROM_writeDouble(KpAddress, Kp); } if (Ki != EEPROM_readDouble(KiAddress)) { EEPROM_writeDouble(KiAddress, Ki); } if (Kd != EEPROM_readDouble(KdAddress)) { EEPROM_writeDouble(KdAddress, Kd); } } // ************************************************ // Load parameters from EEPROM // ************************************************ void LoadParameters() { // Load from EEPROM Setpoint = EEPROM_readDouble(SpAddress); Kp = EEPROM_readDouble(KpAddress); Ki = EEPROM_readDouble(KiAddress); Kd = EEPROM_readDouble(KdAddress); // Use defaults if EEPROM values are invalid if (isnan(Setpoint)) { Setpoint = 60; } if (isnan(Kp)) { Kp = 500; } if (isnan(Ki)) { Ki = 0.5; } if (isnan(Kd)) { Kd = 0.1; } } // ************************************************ // Write floating point values to EEPROM // ************************************************ void EEPROM_writeDouble(int address, double value) { byte* p = (byte*)(void*)&value; for (int i = 0; i < sizeof(value); i++) { EEPROM.write(address++, *p++); } } // ************************************************ // Read floating point values from EEPROM // ************************************************ double EEPROM_readDouble(int address) { double value = 0.0; byte* p = (byte*)(void*)&value; for (int i = 0; i < sizeof(value); i++) { *p++ = EEPROM.read(address++); } return value; }
Page last edited June 01, 2013
Text editor powered by tinymce.