# HG changeset patch # User Michiel Broek # Date 1449408577 -3600 # Node ID 7fe45f6e4f484a98a1229654232635236b515d4e # Parent b48368855ec4e6cd428b7689713d6edbc0eb06ca Added PID editor. diff -r b48368855ec4 -r 7fe45f6e4f48 brewco/pid.c --- a/brewco/pid.c Sun Dec 06 12:34:44 2015 +0100 +++ b/brewco/pid.c Sun Dec 06 14:29:37 2015 +0100 @@ -40,7 +40,7 @@ pid->inAuto = FALSE; PID_setOutputLimits(pid, 0, 255); pid->SampleTime = 100; - PID_setControllerDirection(pid, ControllerDirection); + PID_setDirection(pid, ControllerDirection); PID_setTunings(pid, Kp, Ki, Kd); pid->lastTime = millis() - pid->SampleTime; } @@ -136,7 +136,7 @@ * know which one, because otherwise we may increase the output when we should * be decreasing. This is called from PID_init(). */ -void PID_setControllerDirection(pid_var *pid, int Direction) +void PID_setDirection(pid_var *pid, int Direction) { if (pid->inAuto && Direction != pid->Direction) { pid->Kp = (0 - pid->Kp); @@ -204,6 +204,13 @@ +int PID_getSampleTime(pid_var *pid) +{ + return pid->SampleTime; +} + + + /* * This, as they say, is where the magic happens. this function should be called * every time "void loop()" executes. the function will decide for itself whether a new diff -r b48368855ec4 -r 7fe45f6e4f48 brewco/pid.h --- a/brewco/pid.h Sun Dec 06 12:34:44 2015 +0100 +++ b/brewco/pid.h Sun Dec 06 14:29:37 2015 +0100 @@ -32,13 +32,14 @@ void PID_setMode(pid_var *, int); void PID_setOutputLimits(pid_var *, double, double); void PID_setTunings(pid_var *, double, double, double); -void PID_setControllerDirection(pid_var *, int); +void PID_setDirection(pid_var *, int); void PID_setSampleTime(pid_var *, int); double PID_getKp(pid_var *); double PID_getKi(pid_var *); double PID_getKd(pid_var *); int PID_getMode(pid_var *); int PID_getDirection(pid_var *); +int PID_getSampleTime(pid_var *); int PID_compute(pid_var *); diff -r b48368855ec4 -r 7fe45f6e4f48 brewco/prompt.c --- a/brewco/prompt.c Sun Dec 06 12:34:44 2015 +0100 +++ b/brewco/prompt.c Sun Dec 06 14:29:37 2015 +0100 @@ -90,6 +90,12 @@ break; case 134: snprintf(message, Config.lcd_cols + 1, " Change time "); break; + case 135: snprintf(message, Config.lcd_cols + 1, " Edit PID "); + break; + case 137: snprintf(message, Config.lcd_cols + 1, " Direct/Reverse "); + break; + case 138: snprintf(message, Config.lcd_cols + 1, " Change parameter "); + break; case 200: snprintf(message, Config.lcd_cols + 1, text); break; case 202: snprintf(message, Config.lcd_cols + 1, " Manage Recipes "); diff -r b48368855ec4 -r 7fe45f6e4f48 brewco/rdconfig.c --- a/brewco/rdconfig.c Sun Dec 06 12:34:44 2015 +0100 +++ b/brewco/rdconfig.c Sun Dec 06 14:29:37 2015 +0100 @@ -999,7 +999,7 @@ key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); for (i = 0; i < 2; i++) { if (! xmlStrcmp(key, (const xmlChar *)PIDDIRECTION[i])) { - PID_setControllerDirection(unit->PID_hlt, i); + PID_setDirection(unit->PID_hlt, i); break; } } @@ -1033,7 +1033,7 @@ key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); for (i = 0; i < 2; i++) { if (! xmlStrcmp(key, (const xmlChar *)PIDDIRECTION[i])) { - PID_setControllerDirection(unit->PID_mlt, i); + PID_setDirection(unit->PID_mlt, i); break; } } diff -r b48368855ec4 -r 7fe45f6e4f48 brewco/setup.c --- a/brewco/setup.c Sun Dec 06 12:34:44 2015 +0100 +++ b/brewco/setup.c Sun Dec 06 14:29:37 2015 +0100 @@ -78,16 +78,17 @@ -void editInteger(int *value, int low, int high, char *text) +void editDouble(double *value, double low, double high, char *text) { - int key, new = *value; + int key; + double new = *value; char pmpt[81]; prompt(0, NULL); - prompt(134, NULL); + prompt(138, NULL); for (;;) { - snprintf(pmpt, Config.lcd_cols + 1, "%s: %2d mins", text, new); + snprintf(pmpt, Config.lcd_cols + 1, "%s: %5.2lf", text, new); prompt(200, pmpt); if (new == low) prompt(404, NULL); @@ -100,12 +101,12 @@ if ((key == KEY_RETURN) || my_shutdown) return; if (key == KEY_UP) { - new++; + new = new + 0.5; if (new > high) new = high; } if (key == KEY_DOWN) { - new--; + new = new - 0.5; if (new < low) new = low; } @@ -118,17 +119,57 @@ -void toggleYesNo(int *value, char *text) +void editInteger(int *value, int low, int high, int step, char *text, char *text2) +{ + int key, new = *value; + char pmpt[81]; + + prompt(0, NULL); + prompt(134, NULL); + + for (;;) { + snprintf(pmpt, Config.lcd_cols + 1, "%s: %3d %s", text, new, text2); + prompt(200, pmpt); + if (new == low) + prompt(404, NULL); + else if (new == high) + prompt(402, NULL); + else + prompt(403, NULL); + + key = keywait(); + if ((key == KEY_RETURN) || my_shutdown) + return; + if (key == KEY_UP) { + new += step; + if (new > high) + new = high; + } + if (key == KEY_DOWN) { + new -= step; + if (new < low) + new = low; + } + if (key == KEY_ENTER) { + *value = new; + return; + } + } +} + + + +void togglePmpts(int *value, char *text, int pno, char *pmpt1, char *pmpt2) { int key, new = *value; char pmpt[81]; prompt(0, NULL); - prompt(132, NULL); + prompt(pno, NULL); for (;;) { - snprintf(pmpt, Config.lcd_cols + 1, "%s: %s", text, new ? (char *)"Yes":(char *)"No "); + snprintf(pmpt, Config.lcd_cols + 1, "%s: %s", text, new ? pmpt1:pmpt2); prompt(200, pmpt); if (new) { prompt(404, NULL); @@ -152,6 +193,97 @@ +void toggleYesNo(int *value, char *text) +{ + togglePmpts(value, text, 132, (char *)"Yes", (char *)"No "); +} + + + +void toggleDirection(int *value, char *text) +{ + togglePmpts(value, text, 137, (char *)"Reverse", (char *)"Direct "); +} + + + +void editPID(pid_var *pid) +{ + int index = 1, key, val; + char pmpt[81]; + double Kp, Ki, Kd; + + if (debug) + fprintf(stdout, "editPID()\n"); + + for (;;) { + prompt(0, NULL); + prompt(135, NULL); + + if (index == 1) + prompt(402, NULL); + else if (index == 5) + prompt(404, NULL); + else + prompt(403, NULL); + + switch (index) { + case 1: snprintf(pmpt, Config.lcd_cols + 1, "PID Kp: %5.2f", PID_getKp(pid)); + break; + case 2: snprintf(pmpt, Config.lcd_cols + 1, "PID Ki: %5.2f", PID_getKi(pid)); + break; + case 3: snprintf(pmpt, Config.lcd_cols + 1, "PID Kd: %5.2f", PID_getKd(pid)); + break; + case 4: snprintf(pmpt, Config.lcd_cols + 1, "SampleTime: %3d mSec", PID_getSampleTime(pid)); + break; + case 5: snprintf(pmpt, Config.lcd_cols + 1, "Direction: %s", PID_getDirection(pid) ? (char *)"Reverse" : (char *)"Direct"); + break; + } + prompt(200, pmpt); + + key = keywait(); + if ((key == KEY_RETURN) || my_shutdown) { + if (debug) + fprintf(stdout, "End editPID\n"); + return; + } + + if ((key == KEY_UP) && (index > 1)) + index--; + if ((key == KEY_DOWN) && (index < 5)) + index++; + + if (key == KEY_ENTER) { + + Kp = PID_getKp(pid); + Ki = PID_getKi(pid); + Kd = PID_getKd(pid); + + switch(index) { + case 1: editDouble(&Kp, 0.5, 50, (char *)"PID Kp"); + PID_setTunings(pid, Kp, Ki, Kd); + break; + case 2: editDouble(&Ki, 0.5, 50, (char *)"PID Ki"); + PID_setTunings(pid, Kp, Ki, Kd); + break; + case 3: editDouble(&Kd, 0.5, 50, (char *)"PID Kd"); + PID_setTunings(pid, Kp, Ki, Kd); + break; + case 4: val = PID_getSampleTime(pid); + editInteger(&val, 50, 500, 10, (char *)"SampleTime", (char *)"mSec"); + PID_setSampleTime(pid, val); + break; + case 5: val = PID_getDirection(pid); + toggleDirection(&val, (char *)"Direction"); + PID_setDirection(pid, val); + break; + } + } + } +} + + + /* * Edit a single unit */ @@ -204,10 +336,10 @@ case 7: snprintf(pmpt, Config.lcd_cols + 1, "MLT heat b4 HLT: %s", unit->hlt_heater_mltfirst ? (char *)"Yes":(char *)"No"); prompt(200, pmpt); break; - case 8: snprintf(pmpt, Config.lcd_cols + 1, "Pump cycle: %2d mins", unit->pump_cycle); + case 8: snprintf(pmpt, Config.lcd_cols + 1, "Pump cycle: %3d mins", unit->pump_cycle); prompt(200, pmpt); break; - case 9: snprintf(pmpt, Config.lcd_cols + 1, "Pump rest : %2d mins", unit->pump_rest); + case 9: snprintf(pmpt, Config.lcd_cols + 1, "Pump rest : %3d mins", unit->pump_rest); prompt(200, pmpt); break; case 10: snprintf(pmpt, Config.lcd_cols + 1, " Pump pre-mash: %s", unit->pump_premash ? (char *)"Yes":(char *)"No"); @@ -279,9 +411,9 @@ break; case 7: toggleYesNo(&unit->hlt_heater_mltfirst, (char *)"MLT heat b4 HLT"); break; - case 8: editInteger(&unit->pump_cycle, 5, 15, (char *)"Pump cycle"); + case 8: editInteger(&unit->pump_cycle, 5, 15, 1, (char *)"Pump cycle", (char *)"mins"); break; - case 9: editInteger(&unit->pump_rest, 1, 5, (char *)"Pump rest "); + case 9: editInteger(&unit->pump_rest, 1, 5, 1, (char *)"Pump rest ", (char *)"mins"); break; case 10: toggleYesNo(&unit->pump_premash, (char *)" Pump pre-mash"); break; @@ -299,11 +431,13 @@ break; case 17: toggleYesNo(&unit->skip_iodine, (char *)"Skip iodine test"); break; + case 18: editInteger(&unit->iodine_time, 10, 240, 10, (char *)"Iodine time", (char *)"min"); + break; case 19: toggleYesNo(&unit->whirlpool, (char *)"Do a whirlpool"); break; - case 20: // PID_hlt + case 20: editPID(unit->PID_hlt); break; - case 21: // PID_mlt + case 21: editPID(unit->PID_mlt); break; } }