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