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