Persistent Data
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:
// ************************************************
// 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))
Commentaires sur ces manuels