Read/Write PID parameters from configuration.

Sun, 06 Dec 2015 12:34:44 +0100

author
Michiel Broek <mbroek@mbse.eu>
date
Sun, 06 Dec 2015 12:34:44 +0100
changeset 447
b48368855ec4
parent 446
78e9d5234d15
child 448
7fe45f6e4f48

Read/Write PID parameters from configuration.

brewco/Makefile file | annotate | diff | comparison | revisions
brewco/brewco.h file | annotate | diff | comparison | revisions
brewco/rdconfig.c file | annotate | diff | comparison | revisions
brewco/setup.c file | annotate | diff | comparison | revisions
--- a/brewco/Makefile	Sat Dec 05 21:46:22 2015 +0100
+++ b/brewco/Makefile	Sun Dec 06 12:34:44 2015 +0100
@@ -5,7 +5,7 @@
 SRCS		= $(wildcard *.c)
 HDRS		= $(wildcard *.h)
 OBJS		= $(SRCS:.c=.o)
-SLIBS		= -lpthread
+SLIBS		= -lpthread -lz
 TARGET		= brewco
 OTHER		= Makefile
 
--- a/brewco/brewco.h	Sat Dec 05 21:46:22 2015 +0100
+++ b/brewco/brewco.h	Sun Dec 06 12:34:44 2015 +0100
@@ -32,6 +32,8 @@
 #include <dirent.h>
 #include <uuid/uuid.h>
 #include <math.h>
+#include <zlib.h>
+
 
 #ifndef HAVE_WIRINGPI_H
 #include <pthread.h>
--- a/brewco/rdconfig.c	Sat Dec 05 21:46:22 2015 +0100
+++ b/brewco/rdconfig.c	Sun Dec 06 12:34:44 2015 +0100
@@ -403,18 +403,22 @@
 	    }
 
             if (unit->PID_hlt) {
-                if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "PID_HLT_KP", "%.2lf", unit->PID_hlt->Kp)) < 0) {
+                if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "PID_HLT_KP", "%.2lf", unit->PID_hlt->dispKp)) < 0) {
+                    syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement");
+                    return 1;
+                }
+                if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "PID_HLT_KI", "%.2lf", unit->PID_hlt->dispKi)) < 0) {
                     syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement");
                     return 1;
                 }
-                if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "PID_HLT_KI", "%.2lf", unit->PID_hlt->Ki)) < 0) {
+                if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "PID_HLT_KD", "%.2lf", unit->PID_hlt->dispKd)) < 0) {
                     syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement");
                     return 1;
                 }
-                if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "PID_HLT_KD", "%.2lf", unit->PID_hlt->Kd)) < 0) {
-                    syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement");
-                    return 1;
-                }
+		if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "PID_HLT_SAMPLETIIME", "%ld", unit->PID_hlt->SampleTime)) < 0) {
+		    syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement");
+		    return 1;
+		}
                 if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "PID_HLT_DIRECTION", "%s", PIDDIRECTION[unit->PID_hlt->Direction])) < 0) {
                     syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement");
                     return 1;
@@ -422,18 +426,22 @@
             }
 
             if (unit->PID_mlt) {
-                if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "PID_MLT_KP", "%.2lf", unit->PID_mlt->Kp)) < 0) {
+                if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "PID_MLT_KP", "%.2lf", unit->PID_mlt->dispKp)) < 0) {
+                    syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement");
+                    return 1;
+                }
+                if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "PID_MLT_KI", "%.2lf", unit->PID_mlt->dispKi)) < 0) {
                     syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement");
                     return 1;
                 }
-                if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "PID_MLT_KI", "%.2lf", unit->PID_mlt->Ki)) < 0) {
+                if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "PID_MLT_KD", "%.2lf", unit->PID_mlt->dispKd)) < 0) {
                     syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement");
                     return 1;
                 }
-                if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "PID_MLT_KD", "%.2lf", unit->PID_mlt->Kd)) < 0) {
-                    syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement");
-                    return 1;
-                }
+		if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "PID_MLT_SAMPLETIIME", "%ld", unit->PID_mlt->SampleTime)) < 0) {
+		    syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement");
+		    return 1;
+		}
                 if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "PID_MLT_DIRECTION", "%s", PIDDIRECTION[unit->PID_mlt->Direction])) < 0) {
                     syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement");
                     return 1;
@@ -645,9 +653,7 @@
 
 int wrconfig(void)
 {
-    int		rc;
-
-    rc = do_wrconfig();
+    int	rc = do_wrconfig();
     syslog(LOG_NOTICE, "Rewritten configuration, rc=%d", rc);
     return rc;
 }
@@ -711,8 +717,9 @@
 {
     xmlChar     *key;
     int         i, ival;
+    long	lval;
     float       fval;
-    double	dval;
+    double	kpval, kival, kdval;
     units_list  *unit, *tmp;
 
     unit = (units_list *)malloc(sizeof(units_list));
@@ -738,6 +745,8 @@
     unit->whirlpool = 1;
     unit->PID_hlt = (pid_var *)malloc(sizeof(pid_var));
     unit->PID_mlt = (pid_var *)malloc(sizeof(pid_var));
+    PID_init(unit->PID_hlt, NULL, NULL, NULL, 2, 5, 1, P_DIRECT);
+    PID_init(unit->PID_mlt, NULL, NULL, NULL, 2, 5, 1, P_DIRECT);
 
     cur = cur->xmlChildrenNode;
     while (cur != NULL) {
@@ -965,41 +974,71 @@
 
         if ((!xmlStrcmp(cur->name, (const xmlChar *)"PID_HLT_KP"))) {
             key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
-            if (sscanf((const char *)key, "%lf", &dval) == 1)
-                unit->PID_hlt->Kp = dval;
+            sscanf((const char *)key, "%lf", &kpval);
             xmlFree(key);
         }
         if ((!xmlStrcmp(cur->name, (const xmlChar *)"PID_HLT_KI"))) {
             key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
-            if (sscanf((const char *)key, "%lf", &dval) == 1)
-                unit->PID_hlt->Ki = dval;
+            sscanf((const char *)key, "%lf", &kival);
             xmlFree(key);
         }
         if ((!xmlStrcmp(cur->name, (const xmlChar *)"PID_HLT_KD"))) {
             key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
-            if (sscanf((const char *)key, "%lf", &dval) == 1)
-                unit->PID_hlt->Kd = dval;
+            sscanf((const char *)key, "%lf", &kdval);
             xmlFree(key);
         }
+	if ((!xmlStrcmp(cur->name, (const xmlChar *)"PID_HLT_SAMPLETIME"))) {
+	    key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
+	    if (sscanf((const char *)key, "%ld", &lval) == 1) {
+		PID_setSampleTime(unit->PID_hlt, lval);
+		PID_setTunings(unit->PID_hlt, kpval, kival, kdval);
+	    }
+	    xmlFree(key);
+	}
+	if ((!xmlStrcmp(cur->name, (const xmlChar *)"PID_HLT_DIRECTION"))) {
+	    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);
+		    break;
+		}
+	    }
+	    xmlFree(key);
+	}
 
         if ((!xmlStrcmp(cur->name, (const xmlChar *)"PID_MLT_KP"))) {
             key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
-            if (sscanf((const char *)key, "%lf", &dval) == 1)
-                unit->PID_mlt->Kp = dval;
+            sscanf((const char *)key, "%lf", &kpval);
             xmlFree(key);
         }
         if ((!xmlStrcmp(cur->name, (const xmlChar *)"PID_MLT_KI"))) {
             key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
-            if (sscanf((const char *)key, "%lf", &dval) == 1)
-                unit->PID_mlt->Ki = dval;
+            sscanf((const char *)key, "%lf", &kival);
             xmlFree(key);
         }
         if ((!xmlStrcmp(cur->name, (const xmlChar *)"PID_MLT_KD"))) {
             key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
-            if (sscanf((const char *)key, "%lf", &dval) == 1)
-                unit->PID_mlt->Kd = dval;
+            sscanf((const char *)key, "%lf", &kdval);
             xmlFree(key);
         }
+	if ((!xmlStrcmp(cur->name, (const xmlChar *)"PID_MLT_SAMPLETIME"))) {
+	    key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
+	    if (sscanf((const char *)key, "%ld", &lval) == 1) {
+		PID_setSampleTime(unit->PID_mlt, lval);
+		PID_setTunings(unit->PID_mlt, kpval, kival, kdval);
+	    }
+	    xmlFree(key);       
+	}
+	if ((!xmlStrcmp(cur->name, (const xmlChar *)"PID_MLT_DIRECTION"))) {
+	    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);
+		    break;
+		}
+	    }
+	    xmlFree(key);
+	}
 
         cur = cur->next;
     }
--- a/brewco/setup.c	Sat Dec 05 21:46:22 2015 +0100
+++ b/brewco/setup.c	Sun Dec 06 12:34:44 2015 +0100
@@ -26,6 +26,7 @@
 #include "prompt.h"
 #include "xutil.h"
 #include "keyboard.h"
+#include "rdconfig.h"
 
 
 extern int		my_shutdown;
@@ -158,11 +159,14 @@
 {
     int		index = 1, key;
     char	pmpt[81];
+    uLong	ocrc, ncrc;
 
     if (debug)
     	fprintf(stdout, "Start edit brewsystem %d %s\n", unit->number, unit->uuid);
 
     prompt(0, NULL);
+    ncrc = ocrc = crc32(0L, Z_NULL, 0);
+    ocrc = crc32(ocrc, (const Bytef *) unit, sizeof(units_list));
 
     for (;;) {
 
@@ -245,8 +249,14 @@
 	}
 
 	key = keywait();
-	if ((key == KEY_RETURN) || my_shutdown)
+	if ((key == KEY_RETURN) || my_shutdown) {
+	    ncrc = crc32(ncrc, (const Bytef *)unit, sizeof(units_list));
+	    if (ocrc != ncrc)
+		wrconfig();
+	    if (debug)
+		fprintf(stdout, "End edit brewsystem %d %s\n", unit->number, unit->uuid);
 	    return;
+	}
 
 	if ((key == KEY_UP) && (index > 1))
 	    index--;
@@ -298,9 +308,6 @@
 	    }
 	}
     }
-
-    if (debug)
-	fprintf(stdout, "End edit brewsystem %d %s\n", unit->number, unit->uuid);
 }
 
 

mercurial