Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
311 | f9daq | 1 | #include "K6517-ctrl.h" |
2 | #include <tcpsupp.h> |
||
3 | #include <rs232.h> |
||
4 | #include <utility.h> |
||
5 | #include <userint.h> |
||
6 | #include <ansi_c.h> |
||
7 | #include "K2231A.h" |
||
8 | |||
9 | #include "TempCtrl.h" |
||
10 | FILE *gFp; |
||
11 | |||
12 | #define RSTREG(a,x) (a&=(~(x))) |
||
13 | #define SETREG(a,x) (a|=x) |
||
14 | |||
15 | int gMask=0xF; |
||
16 | |||
17 | static int pa; |
||
18 | |||
19 | #define TEXT_LENGTH 2000 |
||
20 | char read_data[TEXT_LENGTH]; |
||
21 | |||
22 | int read_term_index; |
||
23 | int read_term; |
||
24 | int read_cnt; |
||
25 | int bytes_read; |
||
26 | |||
27 | #define COM_PORT 11 |
||
28 | |||
29 | |||
30 | typedef struct { |
||
31 | double dState; // Last temperature input |
||
32 | double iState; // Integrator state |
||
33 | double iMax, iMin; // Maximum and minimum allowable integrator state |
||
34 | double iGain, // integral gain |
||
35 | pGain, // proportional gain |
||
36 | dGain; // derivative gain |
||
37 | } SPid; |
||
38 | |||
39 | SPid pid; |
||
40 | |||
41 | double UpdatePID(SPid *pid, double error, double temperature) { |
||
42 | double pTerm, dTerm, iTerm; |
||
43 | pTerm = pid->pGain * error; |
||
44 | // calculate the proportional term |
||
45 | // calculate the integral state with appropriate limiting |
||
46 | pid->iState += error; |
||
47 | if (pid->iState > pid->iMax) pid->iState = pid->iMax; |
||
48 | else if (pid->iState < pid->iMin) pid->iState = pid->iMin; |
||
49 | //if (debug ) |
||
50 | iTerm = pid->iGain * pid->iState; // calculate the integral term |
||
51 | dTerm = pid->dGain * (temperature - pid->dState); |
||
52 | pid->dState = temperature; |
||
53 | return pTerm + iTerm - dTerm; |
||
54 | } |
||
55 | |||
56 | void SetDimming(int state) { |
||
57 | SetCtrlAttribute (pa, PA_START, ATTR_DIMMED, state); |
||
58 | SetCtrlAttribute (pa, PA_EXIT, ATTR_DIMMED, state); |
||
59 | SetCtrlAttribute (pa, PA_STOP, ATTR_DIMMED, !state); |
||
60 | } |
||
61 | |||
62 | |||
63 | |||
64 | int CVICALLBACK ReadVoltageCurrentCB (int panel, int control, int event, |
||
65 | void *callbackData, int eventData1, int eventData2) { |
||
66 | |||
67 | int iRet; |
||
68 | char ch=0; |
||
69 | double Voltage; |
||
70 | double Current; |
||
71 | char cv_cc; |
||
72 | switch (event) { |
||
73 | case EVENT_COMMIT: |
||
74 | |||
75 | GetCtrlVal(pa,PA_CHANNEL, &ch); |
||
76 | char cmd[0xFF]; |
||
77 | |||
78 | K2231A_SelectChannel(ch+1); |
||
79 | cv_cc = K2231A_GetOperationMode(); |
||
80 | Voltage =K2231A_GetVoltageMonitor(); |
||
81 | Current =K2231A_GetCurrentMonitor(); |
||
82 | |||
83 | |||
84 | |||
85 | SetCtrlVal(panel, PA_VMON, Voltage); |
||
86 | SetCtrlVal(panel, PA_IMON, Current); |
||
87 | SetCtrlVal(panel, PA_CVCC, cv_cc); |
||
88 | |||
89 | break; |
||
90 | } |
||
91 | return 0; |
||
92 | } |
||
93 | |||
94 | |||
95 | int CVICALLBACK SelectChannelCB (int panel, int control, int event, |
||
96 | void *callbackData, int eventData1, int eventData2) { |
||
97 | unsigned char state, preset; |
||
98 | double Voltage, Current; |
||
99 | unsigned char ch; |
||
100 | switch (event) { |
||
101 | case EVENT_COMMIT: |
||
102 | GetCtrlVal(pa,PA_CHANNEL, &ch); |
||
103 | |||
104 | |||
105 | K2231A_SelectChannel(ch+1); |
||
106 | Voltage =K2231A_GetSetVoltage(); |
||
107 | Current =K2231A_GetSetCurrent(); |
||
108 | |||
109 | SetCtrlVal(pa, PA_VSET, Voltage); |
||
110 | SetCtrlVal(pa, PA_ISET, Current); |
||
111 | |||
112 | break; |
||
113 | } |
||
114 | return 0; |
||
115 | } |
||
116 | |||
117 | int SetCooling(char onoff){ |
||
118 | int nb=0; |
||
119 | nb = ComWrt (COM_PORT, &onoff, 1); |
||
120 | return nb; |
||
121 | } |
||
122 | |||
123 | #define tcpChk(f) if ((g_TCPError=(f)) < 0) {ReportTCPError(); return -1; } |
||
124 | |||
125 | /*---------------------------------------------------------------------------*/ |
||
126 | /* Module-globals */ |
||
127 | /*---------------------------------------------------------------------------*/ |
||
128 | static unsigned int g_hconversation; |
||
129 | static int g_TCPError = 0; |
||
130 | |||
131 | /*---------------------------------------------------------------------------*/ |
||
132 | /* Internal function prototypes */ |
||
133 | /*---------------------------------------------------------------------------*/ |
||
134 | /*---------------------------------------------------------------------------*/ |
||
135 | /* Report TCP Errors if any */ |
||
136 | /*---------------------------------------------------------------------------*/ |
||
137 | static void ReportTCPError (void) { |
||
138 | if (g_TCPError < 0) { |
||
139 | char messageBuffer[1024]; |
||
140 | sprintf(messageBuffer, |
||
141 | "TCP library error message: %s\nSystem error message: %s", |
||
142 | GetTCPErrorString (g_TCPError), GetTCPSystemErrorString()); |
||
143 | MessagePopup ("Error", messageBuffer); |
||
144 | } |
||
145 | } |
||
146 | |||
147 | |||
148 | int CVICALLBACK ServerTCPCB (unsigned handle, int event, int error, |
||
149 | void *callbackData) { |
||
150 | char receiveBuf[256] = {0}; |
||
151 | ssize_t dataSize = sizeof (receiveBuf) - 1; |
||
152 | char addrBuf[31]; |
||
153 | |||
154 | switch (event) { |
||
155 | case TCP_CONNECT: |
||
156 | if (g_hconversation) { |
||
157 | /* We already have one client, don't accept another... */ |
||
158 | tcpChk (GetTCPPeerAddr (handle, addrBuf, 31)); |
||
159 | sprintf (receiveBuf, "-- Refusing conection request from " |
||
160 | "%s --\n", addrBuf); |
||
161 | printf("%s\n", receiveBuf); |
||
162 | tcpChk (DisconnectTCPClient (handle)); |
||
163 | } else { |
||
164 | /* Handle this new client connection */ |
||
165 | g_hconversation = handle; |
||
166 | tcpChk (GetTCPPeerAddr (g_hconversation, addrBuf, 31)); |
||
167 | printf("%s\n", addrBuf); |
||
168 | tcpChk (GetTCPPeerName (g_hconversation, receiveBuf, 256)); |
||
169 | printf("%s\n", receiveBuf); |
||
170 | sprintf (receiveBuf, "-- New connection from %s --\n", |
||
171 | addrBuf); |
||
172 | printf("%s\n", receiveBuf); |
||
173 | |||
174 | /* Set the disconect mode so we do not need to terminate */ |
||
175 | /* connections ourselves. */ |
||
176 | tcpChk (SetTCPDisconnectMode (g_hconversation, TCP_DISCONNECT_AUTO)); |
||
177 | } |
||
178 | break; |
||
179 | case TCP_DATAREADY: |
||
180 | if ((dataSize = ServerTCPRead (g_hconversation, receiveBuf, dataSize, 1000)) < 0) { |
||
181 | printf("Receive Error\n"); |
||
182 | } else { |
||
183 | receiveBuf[dataSize] = '\0'; |
||
184 | printf("Received %s\n", receiveBuf); |
||
185 | int cmd=0; |
||
186 | float arg; |
||
187 | sscanf(receiveBuf,"%d%f",&cmd,&arg); |
||
188 | switch (cmd){ |
||
189 | case 1: |
||
190 | SetCtrlVal(pa,PA_TSET,arg); |
||
191 | break; |
||
192 | default: |
||
193 | break; |
||
194 | |||
195 | } |
||
196 | } |
||
197 | break; |
||
198 | case TCP_DISCONNECT: |
||
199 | if (handle == g_hconversation) { |
||
200 | /* The client we were talking to has disconnected... */ |
||
201 | g_hconversation = 0; |
||
202 | printf("-- Client disconnected --\n"); |
||
203 | |||
204 | /* Note that we do not need to do any more because we set the*/ |
||
205 | /* disconnect mode to AUTO. */ |
||
206 | } |
||
207 | break; |
||
208 | } |
||
209 | return 0; |
||
210 | } |
||
211 | |||
212 | |||
213 | |||
214 | |||
215 | |||
216 | int main (int argc, char *argv[]) { |
||
217 | int DeviceId=0; |
||
218 | unsigned char ch; |
||
219 | unsigned char MainOutput, preset=1; |
||
220 | double Voltage, Current, tinterval; |
||
221 | char str[0xFF]; |
||
222 | if (InitCVIRTE (0, argv, 0) == 0) |
||
223 | return -1; /* out of memory */ |
||
224 | SetStdioPort (CVI_STDIO_WINDOW); |
||
225 | pid.iGain= 0.1; |
||
226 | pid.dGain= 2; |
||
227 | pid.pGain = 5; |
||
228 | pid.iMax =100; |
||
229 | pid.iMin =-100 ; |
||
230 | pid.iState=0; |
||
231 | pid.dState=0; |
||
232 | |||
233 | if ((pa = LoadPanel (0, "TempCtrl.uir", PA)) < 0) |
||
234 | return -1; |
||
235 | DisableBreakOnLibraryErrors(); |
||
236 | K2231A_SetPort(6); |
||
237 | K2231A_SetDebug(1); |
||
238 | if (K2231A_Open()== 0) MessagePopup("Error","Cannot open USB device"); |
||
239 | K2231A_Test(); |
||
240 | MainOutput = K2231A_GetSwitch(); |
||
241 | SetCtrlVal(pa, PA_ONOFF, MainOutput); |
||
242 | SetCtrlVal(pa, PA_PRESET, preset); |
||
243 | |||
244 | int debug1; |
||
245 | GetCtrlVal(pa,PA_DEBUG_1, &debug1); |
||
246 | K2231A_SetDebug(1); |
||
247 | SetCtrlVal(pa,PA_CHANNEL, 0); |
||
248 | /* |
||
249 | GetCtrlVal(pa, P1_TINTERVAL, &tinterval); |
||
250 | SetCtrlAttribute (pa, P1_TIMER, ATTR_INTERVAL, tinterval); |
||
251 | */ |
||
252 | |||
253 | SetWaitCursor (1); |
||
254 | int portNum = 10000; |
||
255 | int registered = 0; |
||
256 | char tempBuf[256] = {0}; |
||
257 | if (RegisterTCPServer (portNum, ServerTCPCB, 0) < 0) |
||
258 | MessagePopup("TCP Server", "Server registration failed!"); |
||
259 | else { |
||
260 | SetWaitCursor (0); |
||
261 | registered = 1; |
||
262 | |||
263 | /* We are successfully connected -- gather info */ |
||
264 | |||
265 | if (GetTCPHostAddr (tempBuf, 256) >= 0) printf("%s\n" ,tempBuf); |
||
266 | if (GetTCPHostName (tempBuf, 256) >= 0) printf("%s\n" ,tempBuf); |
||
267 | |||
268 | |||
269 | |||
270 | } |
||
271 | |||
272 | SelectChannelCB (pa, PA_CHANNEL, EVENT_COMMIT, NULL, 0,0); |
||
273 | ReadVoltageCurrentCB (pa, PA_CHANNEL, EVENT_COMMIT, NULL, 0,0); |
||
274 | |||
275 | |||
276 | |||
277 | DisplayPanel (pa); |
||
278 | SetDimming(0); |
||
279 | RunUserInterface (); |
||
280 | CloseCom(COM_PORT); |
||
281 | if (registered) |
||
282 | UnregisterTCPServer (portNum); |
||
283 | DiscardPanel (pa); |
||
284 | K2231A_Close(); |
||
285 | if (gFp) fclose(gFp); |
||
286 | return 0; |
||
287 | } |
||
288 | |||
289 | |||
290 | int CVICALLBACK ExitCB (int panel, int control, int event, |
||
291 | void *callbackData, int eventData1, int eventData2) { |
||
292 | switch (event) { |
||
293 | case EVENT_COMMIT: |
||
294 | QuitUserInterface (0); |
||
295 | break; |
||
296 | } |
||
297 | return 0; |
||
298 | } |
||
299 | |||
300 | int CVICALLBACK SwitchCoolingOnOffCB (int panel, int control, int event, |
||
301 | void *callbackData, int eventData1, int eventData2) { |
||
302 | int onoff=0; |
||
303 | |||
304 | switch (event) { |
||
305 | case EVENT_COMMIT: |
||
306 | GetCtrlVal(pa,control,&onoff); |
||
307 | SetCooling(onoff); |
||
308 | |||
309 | break; |
||
310 | } |
||
311 | return 0; |
||
312 | } |
||
313 | |||
314 | int CVICALLBACK SwitchOnOffCB (int panel, int control, int event, |
||
315 | void *callbackData, int eventData1, int eventData2) { |
||
316 | unsigned char state; |
||
317 | switch (event) { |
||
318 | case EVENT_COMMIT: |
||
319 | GetCtrlVal(panel, control, &state); |
||
320 | K2231A_SetSwitch(state); |
||
321 | break; |
||
322 | } |
||
323 | return 0; |
||
324 | } |
||
325 | |||
326 | |||
327 | |||
328 | /* Callback Function */ |
||
329 | void ComCallback(int portNumber, int eventMask, void *callbackdata) { |
||
330 | static double told=0; |
||
331 | if (eventMask & LWRS_RXFLAG) { |
||
332 | //printf("Received specified character\n"); |
||
333 | int strLen = GetInQLen (COM_PORT); |
||
334 | bytes_read = ComRd (COM_PORT, read_data, strLen); |
||
335 | double temp= atof(read_data); |
||
336 | double f[10]; |
||
337 | int debug1; |
||
338 | GetCtrlVal(pa,PA_DEBUG_1, &debug1); |
||
339 | K2231A_SetDebug(debug1); |
||
340 | //printf("%f#%s#", temp, read_data); |
||
341 | // temp1 , temp2 , temp3 , temp4 , temp humidity sens, rel.humidity |
||
342 | if (bytes_read >0) { |
||
343 | SetCtrlVal(pa,PA_ARDUINO, read_data); |
||
344 | } |
||
345 | sscanf(read_data,"%lf %lf %lf %lf %lf %lf",&f[0],&f[1],&f[2],&f[3],&f[4],&f[5]); |
||
346 | double humidity = f[5]; |
||
347 | double htemp = f[4]; |
||
348 | // printf("%lf %lf %lf %lf %lf %lf",f[0],f[1],f[2],f[3],f[4],f[5]); |
||
349 | int RS232Error = ReturnRS232Err (); |
||
350 | if (ReturnRS232Err ()) { |
||
351 | sprintf(read_data,"#%s\n", GetRS232ErrorString(RS232Error)); |
||
352 | MessagePopup("RS232Err",read_data); |
||
353 | } else { |
||
354 | |||
355 | |||
356 | int polar; |
||
357 | |||
358 | ProcessSystemEvents (); |
||
359 | SelectChannelCB (pa, PA_CHANNEL, EVENT_COMMIT, NULL, 0,0); |
||
360 | ProcessSystemEvents (); |
||
361 | ReadVoltageCurrentCB (pa, PA_CHANNEL, EVENT_COMMIT, NULL, 0,0); |
||
362 | ProcessSystemEvents (); |
||
363 | double tset,vmon,imon,vset,iset,vmax; |
||
364 | unsigned char ch, preset; |
||
365 | GetCtrlVal(pa,PA_TSET,&tset); |
||
366 | GetCtrlVal(pa,PA_VMON,&vmon); |
||
367 | GetCtrlVal(pa,PA_IMON,&imon); |
||
368 | GetCtrlVal(pa,PA_CHANNEL, &ch); |
||
369 | //GetCtrlVal(pa,PA_PRESET,&preset); |
||
370 | //TMI_Preset(TMI_DeviceId,preset); |
||
371 | GetCtrlVal(pa,PA_POLAR,&polar); |
||
372 | |||
373 | double tdiff = temp - tset; |
||
374 | double retpid = UpdatePID(&pid, tdiff, temp); |
||
375 | const double troom = htemp; |
||
376 | double Pheat= 0.2 * (temp - troom); |
||
377 | double Ptec = retpid - Pheat*2; |
||
378 | vset = (polar*Ptec>0) ? sqrt(fabs(Ptec)) : 0; |
||
379 | if (debug1) printf("%d PID tmon=%f tset=%f tdiff=%f vmon=%f imom=%f pid=%f Pheat=%f =>vset %f", ch, temp, tset, tdiff, vmon, imon,retpid ,Pheat, vset); |
||
380 | GetCtrlVal(pa,PA_VMAX,&vmax); |
||
381 | if (vset >vmax) vset=vmax; |
||
382 | if (vset <0) vset =0; |
||
383 | if (debug1) printf("vset --->%f \n",vset); |
||
384 | K2231A_SelectChannel(ch+1); |
||
385 | K2231A_SetVoltage(vset); |
||
386 | |||
387 | GetCtrlVal(pa,PA_IMAX,&iset); |
||
388 | |||
389 | K2231A_SetCurrent(iset); |
||
390 | |||
391 | |||
392 | ReadVoltageCurrentCB (pa, PA_CHANNEL, EVENT_COMMIT, NULL, 0,0); |
||
393 | |||
394 | PlotStripChart (pa, PA_GRAPH, &temp, 1, 0, 0, VAL_DOUBLE); |
||
395 | double pgraph[3]= {vmon,imon, 100*(temp-told)}; |
||
396 | PlotStripChart (pa, PA_GRAPH_VMON, pgraph, 3, 0, 0, VAL_DOUBLE); |
||
397 | PlotStripChart (pa, PA_GRAPH_3, &humidity, 1, 0, 0, VAL_DOUBLE); |
||
398 | SetCtrlVal(pa,PA_TMON,temp); |
||
399 | SetCtrlVal(pa,PA_HTMON,htemp); |
||
400 | SetCtrlVal(pa,PA_HUMIDITY,humidity); |
||
401 | |||
402 | char transmitBuf[512]= {0}; |
||
403 | sprintf(transmitBuf, "%u %f %f %f %f\n", time(NULL), humidity, temp, tdiff, temp-told); |
||
404 | if (g_hconversation) |
||
405 | if ( ServerTCPWrite (g_hconversation, transmitBuf, strlen (transmitBuf), 1000) < 0) printf("Transmit Error\n"); |
||
406 | int log=0; |
||
407 | GetCtrlVal(pa,PA_LOG, &log); |
||
408 | if (log) { |
||
409 | char fname[0xFF]; |
||
410 | GetCtrlVal(pa,PA_FNAME, fname); |
||
411 | FILE *fp = fopen (fname,"a"); |
||
412 | fprintf(fp,transmitBuf); |
||
413 | fclose(fp); |
||
414 | |||
415 | } |
||
416 | told= temp; |
||
417 | |||
418 | } |
||
419 | } |
||
420 | |||
421 | if (eventMask & LWRS_TXEMPTY) |
||
422 | |||
423 | printf("Transmit queue now empty\n"); |
||
424 | |||
425 | if (eventMask & LWRS_RECEIVE) { |
||
426 | |||
427 | printf("50 or more bytes in input queue\n"); |
||
428 | |||
429 | } |
||
430 | |||
431 | } |
||
432 | |||
433 | |||
434 | int CVICALLBACK StartCB (int panel, int control, int event, |
||
435 | void *callbackData, int eventData1, int eventData2) { |
||
436 | switch (event) { |
||
437 | case EVENT_COMMIT: |
||
438 | OpenComConfig (COM_PORT, "", 115200, 0, 8, 1, 512, 512); |
||
439 | /* Turn off Hardware handshaking (loopback test will not function with it on) */ |
||
440 | SetCTSMode (COM_PORT, LWRS_HWHANDSHAKE_OFF); |
||
441 | |||
442 | /* Make sure Serial buffers are empty */ |
||
443 | FlushInQ (COM_PORT); |
||
444 | FlushOutQ (COM_PORT); |
||
445 | |||
446 | int notifyCount = 50; /* Wait for at least 50 bytes in queue. */ |
||
447 | int eventChar = 10; /* Wait for LF. */ |
||
448 | int eventMask = LWRS_RXFLAG | LWRS_TXEMPTY | LWRS_RECEIVE; |
||
449 | InstallComCallback (COM_PORT, eventMask, notifyCount, eventChar, ComCallback, NULL); |
||
450 | SetDimming(1); |
||
451 | |||
452 | break; |
||
453 | } |
||
454 | return 0; |
||
455 | } |
||
456 | |||
457 | |||
458 | int CVICALLBACK StopCB (int panel, int control, int event, |
||
459 | void *callbackData, int eventData1, int eventData2) { |
||
460 | switch (event) { |
||
461 | case EVENT_COMMIT: |
||
462 | CloseCom(COM_PORT); |
||
463 | SetDimming(0); |
||
464 | break; |
||
465 | } |
||
466 | return 0; |
||
467 | } |
||
468 | |||
469 | int CVICALLBACK SetPresetCB (int panel, int control, int event, |
||
470 | void *callbackData, int eventData1, int eventData2) { |
||
471 | |||
472 | switch (event) { |
||
473 | case EVENT_COMMIT: { |
||
474 | unsigned char preset; |
||
475 | double Voltage, Current; |
||
476 | GetCtrlVal(panel, control, &preset); |
||
477 | K2231A_RecallFromMemory(preset); |
||
478 | |||
479 | break; |
||
480 | } |
||
481 | } |
||
482 | return 0; |
||
483 | } |
||
484 |