Commit 8dca7aebfc94728c9afe92c651cc47f5c75d2a9f

Authored by henyxia
1 parent 35bb800b

Automatic Pumping and Heating

@@ -3,7 +3,7 @@ CC_FLAGS=-c -Wall -Werror -std=gnu99 -g @@ -3,7 +3,7 @@ CC_FLAGS=-c -Wall -Werror -std=gnu99 -g
3 CC_LIBS=-pthread 3 CC_LIBS=-pthread
4 INCLUDES= 4 INCLUDES=
5 5
6 -SOURCES=main.c printx.c ui.c serial.c nfc.c bus.c hvc.c 6 +SOURCES=main.c printx.c ui.c serial.c nfc.c bus.c hvc.c heat.c pump.c
7 OBJECTS=$(SOURCES:.c=.o) 7 OBJECTS=$(SOURCES:.c=.o)
8 OUTPUT=tweekd 8 OUTPUT=tweekd
9 9
@@ -12,6 +12,8 @@ @@ -12,6 +12,8 @@
12 #include "ui.h" 12 #include "ui.h"
13 #include "nfc.h" 13 #include "nfc.h"
14 #include "hvc.h" 14 #include "hvc.h"
  15 +#include "heat.h"
  16 +#include "pump.h"
15 17
16 #define CMD_MAX 70 18 #define CMD_MAX 70
17 #define KEY 1100 19 #define KEY 1100
@@ -94,7 +96,8 @@ void P(int i) @@ -94,7 +96,8 @@ void P(int i)
94 void V(int i) 96 void V(int i)
95 { 97 {
96 PV(i, 1); 98 PV(i, 1);
97 -} 99 +}
  100 +
98 101
99 void stopBus() 102 void stopBus()
100 { 103 {
@@ -132,6 +135,12 @@ void processCmd(char* buffer) @@ -132,6 +135,12 @@ void processCmd(char* buffer)
132 printx(INFO, BUS, "Setting HEAT OFF"); 135 printx(INFO, BUS, "Setting HEAT OFF");
133 setHeatWantedState(false); 136 setHeatWantedState(false);
134 } 137 }
  138 + else if(strcmp(buffer, "setheaton2s") == 0)
  139 + {
  140 + printx(INFO, BUS, "Setting HEAT ON for 2 secs");
  141 + setHeatTimer(2);
  142 + setHeatWantedState(true);
  143 + }
135 else if(strcmp(buffer, "setheaton5s") == 0) 144 else if(strcmp(buffer, "setheaton5s") == 0)
136 { 145 {
137 printx(INFO, BUS, "Setting HEAT ON for 5 secs"); 146 printx(INFO, BUS, "Setting HEAT ON for 5 secs");
@@ -144,12 +153,29 @@ void processCmd(char* buffer) @@ -144,12 +153,29 @@ void processCmd(char* buffer)
144 setHeatTimer(10); 153 setHeatTimer(10);
145 setHeatWantedState(true); 154 setHeatWantedState(true);
146 } 155 }
  156 + else if(strcmp(buffer, "setheaton15s") == 0)
  157 + {
  158 + printx(INFO, BUS, "Setting HEAT ON for 15 secs");
  159 + setHeatTimer(15);
  160 + setHeatWantedState(true);
  161 + }
147 else if(strcmp(buffer, "setpumpon5s") == 0) 162 else if(strcmp(buffer, "setpumpon5s") == 0)
148 { 163 {
149 printx(INFO, BUS, "Setting PUMP ON for 5 secs"); 164 printx(INFO, BUS, "Setting PUMP ON for 5 secs");
150 setPumpTimer(5); 165 setPumpTimer(5);
151 setPumpWantedState(true); 166 setPumpWantedState(true);
152 } 167 }
  168 + else if(strcmp(buffer, "setpumpon10s") == 0)
  169 + {
  170 + printx(INFO, BUS, "Setting PUMP ON for 10 secs");
  171 + setPumpTimer(10);
  172 + setPumpWantedState(true);
  173 + }
  174 + else if(strcmp(buffer, "autoheat") == 0)
  175 + {
  176 + printx(INFO, BUS, "Processing Auto Heat to 90°C");
  177 + processHeat(NULL);
  178 + }
153 179
154 //printx(DEBUG, BUS, "STRLEN : %d and strcmp ret %d", strlen(buffer), strcmp(buffer, "quit")); 180 //printx(DEBUG, BUS, "STRLEN : %d and strcmp ret %d", strlen(buffer), strcmp(buffer, "quit"));
155 } 181 }
@@ -162,8 +188,6 @@ void* processBus(void* we) @@ -162,8 +188,6 @@ void* processBus(void* we)
162 while(!busStop) 188 while(!busStop)
163 { 189 {
164 P(SEM_OUTPUT); 190 P(SEM_OUTPUT);
165 - //while(busFree);  
166 - //while(!busFree);  
167 printx(DEBUG, BUS, "Event receved !\n"); 191 printx(DEBUG, BUS, "Event receved !\n");
168 busFree = false; 192 busFree = false;
169 lseek(bus, 0, SEEK_SET); 193 lseek(bus, 0, SEEK_SET);
@@ -192,8 +216,6 @@ bool initBus() @@ -192,8 +216,6 @@ bool initBus()
192 return false; 216 return false;
193 } 217 }
194 218
195 - //P(SEM_INPUT);  
196 -  
197 return true; 219 return true;
198 } 220 }
199 221
heat.c 0 → 100644
@@ -0,0 +1,234 @@ @@ -0,0 +1,234 @@
  1 +#include <stdio.h>
  2 +#include <time.h>
  3 +#include <stdbool.h>
  4 +#include "hvc.h"
  5 +#include "heat.h"
  6 +
  7 +#define TASK_NUMBER 20
  8 +#define T_INIT_THRESH 400
  9 +#define T_STEP_THRESH 860
  10 +#define T_HOLD_THRESH 880
  11 +#define INIT_HEAT_TIME 15
  12 +#define INIT_WAIT_TIME 15
  13 +#define STEP_HEAT_TIME 2
  14 +#define STEP_WAIT_TIME 5
  15 +#define HOLD_HEAT_TIME 1
  16 +#define HOLD_WAIT_TIME 2
  17 +#define HOLD_PUMP_HEAT_TIME 3
  18 +#define HOLD_PUMP_WAIT_TIME 1
  19 +
  20 +
  21 +//Globals
  22 +time_t tTask;
  23 +time_t tNow;
  24 +time_t tAct; //Lecture de la température du capteur
  25 +bool task[TASK_NUMBER]; //A initialiser (task[0]=true, sinon false)
  26 + /*for (int i=0;i<TASK_NUMBER;i++)
  27 + {
  28 + if (i == 0)
  29 + task[i]=true;
  30 + else
  31 + task[i]=false;
  32 + }*/
  33 +bool stop=false;
  34 +bool heat_ok=false;
  35 +bool hold_heat=true;
  36 +bool pumpitup=false; //Lecture de l'état de la pompe
  37 +bool eco_mode=false;
  38 +
  39 +void* processHeat(void* arg)
  40 +{
  41 + while(!stop)
  42 + {
  43 + tNow = clock();
  44 +
  45 + //Chaufffe initiale
  46 + if (task[0])
  47 + {
  48 + tTask = clock();
  49 + if (tAct < T_INIT_THRESH)
  50 + {
  51 + setHeatOn();
  52 + task[0] = false;
  53 + task[1] = true;
  54 + }
  55 +
  56 + else
  57 + {
  58 + task[0] = false;
  59 + task[3] = true;
  60 + }
  61 + }
  62 +
  63 + else if (task[1])
  64 + {
  65 + if( ((tNow - tTask) / CLOCKS_PER_SEC) > INIT_HEAT_TIME )
  66 + {
  67 + setHeatOff();
  68 + task[1] = false;
  69 + task[2] = true;
  70 + }
  71 + }
  72 +
  73 + else if (task[2])
  74 + {
  75 + if( ((tNow - tTask) / CLOCKS_PER_SEC) > INIT_WAIT_TIME )
  76 + {
  77 + task[2] = false;
  78 + task[3] = true;
  79 + }
  80 + }
  81 +
  82 + //Chauffe par paliers
  83 + else if (task[3])
  84 + {
  85 + if (tAct < T_STEP_THRESH)
  86 + {
  87 + setHeatOn();
  88 + task[3] = false;
  89 + task[4] = true;
  90 + }
  91 +
  92 + else
  93 + {
  94 + task[3] = false;
  95 + task[6] = true; //Numéro de tache début "Maintien au chaud"
  96 + }
  97 + }
  98 +
  99 + else if (task[4])
  100 + {
  101 + if( ((tNow - tTask) / CLOCKS_PER_SEC) > STEP_HEAT_TIME )
  102 + {
  103 + setHeatOff();
  104 + task[4] = false;
  105 + task[5] = true;
  106 + }
  107 + }
  108 +
  109 + else if (task[5])
  110 + {
  111 + if( ((tNow - tTask) / CLOCKS_PER_SEC) > STEP_WAIT_TIME )
  112 + {
  113 + if ( tAct < T_STEP_THRESH )
  114 + {
  115 + task[5] = false;
  116 + task[3] = true;
  117 + }
  118 +
  119 + else
  120 + {
  121 + task[5] = false;
  122 + task[6] = true;
  123 + }
  124 + }
  125 + }
  126 +
  127 + // Maintien au chaud
  128 + else if (task[6])
  129 + {
  130 + heat_ok = true;
  131 + task[6]=false;
  132 + task[7]=true;
  133 + }
  134 +
  135 + else if (task[7])
  136 + {
  137 + if(hold_heat)
  138 + {
  139 + if( (tAct < T_HOLD_THRESH) && !pumpitup)
  140 + {
  141 + setHeatOn();
  142 + task[7] = false;
  143 + task[8] = true;
  144 + }
  145 +
  146 + else if ( (tAct < T_HOLD_THRESH) && pumpitup)
  147 + {
  148 + setHeatOn();
  149 + task[7]=false;
  150 + task[10]=true; //Numéro de tache "T<Thold && pumpitup"
  151 + }
  152 +
  153 + }
  154 +
  155 + else
  156 + {
  157 + task[6]=false;
  158 + task[12]=true; //Sinon go étape d'attente fin
  159 + }
  160 + }
  161 +
  162 + //Maintien au chaud sans pompage
  163 + else if (task[8])
  164 + {
  165 + if( ((tNow - tTask) / CLOCKS_PER_SEC) > HOLD_HEAT_TIME )
  166 + {
  167 + setHeatOff();
  168 + task[8] = false;
  169 + task[9] = true;
  170 + }
  171 + }
  172 +
  173 + else if (task[9])
  174 + {
  175 + if( ((tNow - tTask) / CLOCKS_PER_SEC) > HOLD_WAIT_TIME )
  176 + {
  177 + if ( tAct < T_HOLD_THRESH)
  178 + {
  179 + task[9] = false;
  180 + task[7] = true; //Retour au début de "Maintien au chaud"
  181 + }
  182 + }
  183 + }
  184 +
  185 + //Maintien au chaud avec pompage
  186 + else if (task[10])
  187 + {
  188 + if( ((tNow - tTask) / CLOCKS_PER_SEC) > HOLD_PUMP_HEAT_TIME )
  189 + {
  190 + setHeatOff();
  191 + task[10] = false;
  192 + task[11] = true;
  193 + }
  194 + }
  195 +
  196 + else if (task[11])
  197 + {
  198 + if( ((tNow - tTask) / CLOCKS_PER_SEC) > HOLD_PUMP_WAIT_TIME )
  199 + {
  200 + if ( tAct < T_HOLD_THRESH)
  201 + {
  202 + task[11] = false;
  203 + task[7] = true; //Retour au début de "Maintien au chaud"
  204 + }
  205 + }
  206 + }
  207 +
  208 + //Fin de maintien au chaud (demandé par l'extérieur) et reboucle quand hold_heat est remis
  209 + else if(task[12])
  210 + {
  211 + setHeatOff();
  212 + heat_ok=false;
  213 +
  214 + if(hold_heat)
  215 + {
  216 + task[12]=false;
  217 + task[1]=true; //Retour au début
  218 + }
  219 + }
  220 +
  221 +
  222 + //Mode économie d'énergie
  223 + else if(task[13])
  224 + {
  225 + //task[]=false;
  226 + //task[]=true;
  227 + }
  228 +
  229 +
  230 + }
  231 +
  232 +
  233 +return NULL;
  234 +}
heat.h 0 → 100644
@@ -0,0 +1,6 @@ @@ -0,0 +1,6 @@
  1 +#ifndef __HEAT_H__
  2 +#define __HEAT_H__
  3 +
  4 +void* processHeat(void*);
  5 +
  6 +#endif
@@ -13,6 +13,7 @@ @@ -13,6 +13,7 @@
13 #include "hvc.h" 13 #include "hvc.h"
14 #include "printx.h" 14 #include "printx.h"
15 #include "serial.h" 15 #include "serial.h"
  16 +#include "heat.h"
16 17
17 #define DEVICE1 "/dev/ttyACM1" 18 #define DEVICE1 "/dev/ttyACM1"
18 #define SPEED B19200 19 #define SPEED B19200
@@ -31,6 +32,7 @@ int hvc_fd = -1; @@ -31,6 +32,7 @@ int hvc_fd = -1;
31 bool hvcStop = false; 32 bool hvcStop = false;
32 bool sPump = false; 33 bool sPump = false;
33 bool sHeat = false; 34 bool sHeat = false;
  35 +bool autoHeat = false;
34 struct termios hvcSaveterm; 36 struct termios hvcSaveterm;
35 bool wHeat = false; 37 bool wHeat = false;
36 bool wPump = false; 38 bool wPump = false;
@@ -40,6 +42,7 @@ double tHeatTimer; @@ -40,6 +42,7 @@ double tHeatTimer;
40 time_t tPumpStart; 42 time_t tPumpStart;
41 double tPumpTimer; 43 double tPumpTimer;
42 time_t tPumpStop; 44 time_t tPumpStop;
  45 +int tempCons;
43 46
44 void stopHVC() 47 void stopHVC()
45 { 48 {
@@ -70,6 +73,11 @@ void setHeatTimer(double t) @@ -70,6 +73,11 @@ void setHeatTimer(double t)
70 tHeatTimer = t; 73 tHeatTimer = t;
71 } 74 }
72 75
  76 +void setAutoHeat(bool s)
  77 +{
  78 + autoHeat = s;
  79 +}
  80 +
73 bool initHVC() 81 bool initHVC()
74 { 82 {
75 unsigned char data; 83 unsigned char data;
@@ -97,6 +105,7 @@ bool initHVC() @@ -97,6 +105,7 @@ bool initHVC()
97 void* processHVC(void* we) 105 void* processHVC(void* we)
98 { 106 {
99 uint8_t data; 107 uint8_t data;
  108 + float temp;
100 109
101 while(!hvcStop) 110 while(!hvcStop)
102 { 111 {
@@ -104,7 +113,8 @@ void* processHVC(void* we) @@ -104,7 +113,8 @@ void* processHVC(void* we)
104 sendData(&hvc_fd, GET_TEMP); 113 sendData(&hvc_fd, GET_TEMP);
105 data = getData(&hvc_fd); 114 data = getData(&hvc_fd);
106 setTemp(data); 115 setTemp(data);
107 - printx(DEBUG, HVC, "TEMP %f\n", 25 + (((float)(80*data)))/255); 116 + temp = 25 + (((float)(80*data)))/255;
  117 + printx(DEBUG, HVC, "TEMP %f\n", temp);
108 if(tHeatTimer > 0) 118 if(tHeatTimer > 0)
109 { 119 {
110 tHeatStop = clock(); 120 tHeatStop = clock();
@@ -113,7 +123,6 @@ void* processHVC(void* we) @@ -113,7 +123,6 @@ void* processHVC(void* we)
113 wHeat = false; 123 wHeat = false;
114 tHeatTimer = 0; 124 tHeatTimer = 0;
115 } 125 }
116 -  
117 } 126 }
118 127
119 if(tPumpTimer > 0) 128 if(tPumpTimer > 0)
@@ -68,7 +68,7 @@ bool isTagPresent(char* tag) @@ -68,7 +68,7 @@ bool isTagPresent(char* tag)
68 if(data == PROFESSOR_TAG) 68 if(data == PROFESSOR_TAG)
69 { 69 {
70 sendData(&nfc_fd, GET_TAG_DETAILS); 70 sendData(&nfc_fd, GET_TAG_DETAILS);
71 - //printx(INFO, NFC, "Professor Tag Detected %02x\n", data); 71 + printx(INFO, NFC, "Professor Tag Detected %02x\n", data);
72 //return (read(serial_fd, tag, 6) == 6); 72 //return (read(serial_fd, tag, 6) == 6);
73 } 73 }
74 else if(data == STUDENT_TAG) 74 else if(data == STUDENT_TAG)
@@ -57,11 +57,11 @@ void printx(severity s, msgfrom from, char* str, ...) @@ -57,11 +57,11 @@ void printx(severity s, msgfrom from, char* str, ...)
57 char buffer1[MAX_BUFFER]; 57 char buffer1[MAX_BUFFER];
58 char buffer2[MAX_BUFFER]; 58 char buffer2[MAX_BUFFER];
59 va_list arglist; 59 va_list arglist;
60 - //float now = clock(); 60 + float now = clock();
61 va_start(arglist, str); 61 va_start(arglist, str);
62 vsprintf(buffer1, str, arglist); 62 vsprintf(buffer1, str, arglist);
63 - //fprintf(logfile, "[%6f] : %s", (now - start)/CLOCKS_PER_SEC, buffer1);  
64 - //fflush(logfile); 63 + fprintf(logfile, "[%6f] : %s", (now - start)/CLOCKS_PER_SEC, buffer1);
  64 + fflush(logfile);
65 sprintf(buffer2, "[%s] %s%s%s", f_name[from], s_color[s], buffer1, S_RESET); 65 sprintf(buffer2, "[%s] %s%s%s", f_name[from], s_color[s], buffer1, S_RESET);
66 removeCharFromString('\n', buffer2); 66 removeCharFromString('\n', buffer2);
67 addToLog(buffer2); 67 addToLog(buffer2);
pump.c 0 → 100644
@@ -0,0 +1,37 @@ @@ -0,0 +1,37 @@
  1 +#include <stdio.h>
  2 +#include <time.h>
  3 +#include <stdbool.h>
  4 +#include "hvc.h"
  5 +#include "pump.h"
  6 +
  7 +#define K_DEB 1925 //en imp/L
  8 +
  9 +
  10 +//Globals
  11 +bool stop=false;
  12 +bool heat_ok=false;
  13 +bool coffee_wanted=false;
  14 +int volDeb=0; // Volume du débitmetre en impulsion
  15 +int volWanted=0;
  16 +
  17 +void* processHeat(void* arg)
  18 +{
  19 + while (!stop)
  20 + {
  21 + if (heat_ok && coffee_wanted)
  22 + {
  23 + if (volDeb < volWanted)
  24 + {
  25 + setPumpOn();
  26 + }
  27 +
  28 + else
  29 + {
  30 + setPumpOff();
  31 + coffee_wanted=false;
  32 + volDeb=0;
  33 + }
  34 + }
  35 + sleep(1);
  36 + }
  37 +}
pump.h 0 → 100644
@@ -0,0 +1,6 @@ @@ -0,0 +1,6 @@
  1 +#ifndef __PUMP_H__
  2 +#define __PUMP_H__
  3 +
  4 +void* processPump(void*);
  5 +
  6 +#endif
@@ -225,6 +225,6 @@ void displayUI() @@ -225,6 +225,6 @@ void displayUI()
225 sprintf(buffer, "\u2503> %s", cmd); 225 sprintf(buffer, "\u2503> %s", cmd);
226 printf("%s\u2503\n", fillStatusWithSpaces(buffer, buffer)); 226 printf("%s\u2503\n", fillStatusWithSpaces(buffer, buffer));
227 printf("\u2517\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u251B\n"); 227 printf("\u2517\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u251B\n");
228 - printf("\x1b[2A\x1b[%dC", strlen(cmd) + 3); 228 + printf("\x1b[2A\x1b[%dC",(int)(strlen(cmd)) + 3);
229 fflush(stdout); 229 fflush(stdout);
230 } 230 }