Rev 305 | Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
303 | f9daq | 1 | #include <rs232.h> |
2 | #include <utility.h> |
||
3 | #include <userint.h> |
||
4 | #include <ansi_c.h> |
||
5 | #include <pw18-1.8aq.h> |
||
6 | #include "TempCtrl.h" |
||
7 | FILE *gFp; |
||
8 | |||
9 | #define RSTREG(a,x) (a&=(~(x))) |
||
10 | #define SETREG(a,x) (a|=x) |
||
11 | |||
12 | int gMask=0xF; |
||
13 | |||
14 | static int pa; |
||
15 | |||
16 | #define TEXT_LENGTH 2000 |
||
17 | char read_data[TEXT_LENGTH]; |
||
18 | |||
19 | int read_term_index; |
||
20 | int read_term; |
||
21 | int read_cnt; |
||
22 | int bytes_read; |
||
23 | |||
24 | #define COM_PORT 11 |
||
25 | |||
26 | |||
27 | typedef struct |
||
28 | { |
||
29 | double dState; // Last temperature input |
||
30 | double iState; // Integrator state |
||
31 | double iMax, iMin; // Maximum and minimum allowable integrator state |
||
32 | double iGain, // integral gain |
||
33 | pGain, // proportional gain |
||
34 | dGain; // derivative gain |
||
35 | } SPid; |
||
36 | |||
37 | SPid pid; |
||
38 | |||
39 | double UpdatePID(SPid * pid, double error, double temperature) |
||
40 | { |
||
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; |
||
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, |
||
64 | void *callbackData, int eventData1, int eventData2) |
||
65 | { |
||
66 | |||
67 | int iRet; |
||
68 | char ch=0; |
||
69 | double Voltage; |
||
70 | double Current; |
||
71 | char cv_cc; |
||
72 | switch (event) |
||
73 | { |
||
74 | case EVENT_COMMIT: |
||
75 | GetCtrlVal(pa,PA_CHANNEL, &ch); |
||
76 | iRet = TMI_TimeOut(TMI_DeviceId, 1); |
||
77 | iRet = TMI_Refresh(TMI_DeviceId); |
||
78 | |||
79 | |||
80 | iRet = TMI_MoniDataQ(TMI_DeviceId, ch+1, &Voltage, &Current, &cv_cc); |
||
81 | |||
82 | SetCtrlVal(panel, PA_VMON, Voltage); |
||
83 | SetCtrlVal(panel, PA_IMON, Current); |
||
84 | SetCtrlVal(panel, PA_CVCC, cv_cc); |
||
85 | |||
86 | break; |
||
87 | } |
||
88 | return 0; |
||
89 | } |
||
90 | |||
91 | |||
92 | int CVICALLBACK SelectChannelCB (int panel, int control, int event, |
||
93 | void *callbackData, int eventData1, int eventData2) |
||
94 | { |
||
95 | unsigned char state, preset; |
||
96 | double Voltage, Current; |
||
97 | unsigned char ch; |
||
98 | switch (event) |
||
99 | { |
||
100 | case EVENT_COMMIT: |
||
101 | GetCtrlVal(pa,PA_CHANNEL, &ch); |
||
102 | GetCtrlVal(pa,PA_PRESET, &preset); |
||
103 | |||
104 | |||
105 | |||
106 | TMI_VoltageQ(TMI_DeviceId, ch+1, preset, &Voltage); |
||
107 | TMI_CurrentQ(TMI_DeviceId, ch+1, preset, &Current); |
||
108 | SetCtrlVal(pa, PA_VSET, Voltage); |
||
109 | SetCtrlVal(pa, PA_ISET, Current); |
||
110 | |||
111 | break; |
||
112 | } |
||
113 | return 0; |
||
114 | } |
||
115 | |||
116 | |||
117 | |||
118 | |||
119 | int main (int argc, char *argv[]) |
||
120 | { |
||
121 | int DeviceId=0; |
||
122 | unsigned char ch; |
||
123 | unsigned char MainOutput, preset; |
||
124 | double Voltage, Current, tinterval; |
||
125 | char str[0xFF]; |
||
126 | if (InitCVIRTE (0, argv, 0) == 0) |
||
127 | return -1; /* out of memory */ |
||
128 | SetStdioPort (CVI_STDIO_WINDOW); |
||
129 | pid.iGain= 0.3; |
||
130 | pid.dGain= 0.5; |
||
131 | pid.pGain = 3; |
||
132 | pid.iMax =100; |
||
133 | pid.iMin =-100 ; |
||
134 | pid.iState=0; |
||
135 | pid.dState=0; |
||
136 | |||
137 | if ((pa = LoadPanel (0, "TempCtrl.uir", PA)) < 0) |
||
138 | return -1; |
||
139 | |||
140 | if (TMI_Open()== 0) MessagePopup("Error","Cannot open USB device"); |
||
141 | DeviceId = TMI_OpenHandle ("PW-A","USB:1:1"); |
||
142 | if (TMI_Test() == 0 )MessagePopup("DLL error","Dll Error"); |
||
143 | if (DeviceId < 0) MessagePopup("Error","Not Connected"); |
||
144 | printf("TMI device ID %d\n",TMI_DeviceId); |
||
145 | |||
146 | TMI_MainOutputQ(TMI_DeviceId, &MainOutput); |
||
147 | TMI_PresetQ(TMI_DeviceId, &preset); |
||
148 | SetCtrlVal(pa, PA_ONOFF, MainOutput); |
||
149 | SetCtrlVal(pa, PA_PRESET, preset); |
||
150 | /* |
||
151 | GetCtrlVal(pa, P1_TINTERVAL, &tinterval); |
||
152 | SetCtrlAttribute (pa, P1_TIMER, ATTR_INTERVAL, tinterval); |
||
153 | */ |
||
154 | SelectChannelCB (pa, PA_CHANNEL, EVENT_COMMIT, NULL, 0,0); |
||
155 | ReadVoltageCurrentCB (pa, PA_CHANNEL, EVENT_COMMIT, NULL, 0,0); |
||
156 | |||
157 | |||
158 | |||
159 | DisplayPanel (pa); |
||
160 | SetDimming(0); |
||
161 | RunUserInterface (); |
||
162 | CloseCom(COM_PORT); |
||
163 | DiscardPanel (pa); |
||
164 | TMI_Close(); |
||
165 | if (gFp) fclose(gFp); |
||
166 | return 0; |
||
167 | } |
||
168 | |||
169 | |||
170 | int CVICALLBACK ExitCB (int panel, int control, int event, |
||
171 | void *callbackData, int eventData1, int eventData2) { |
||
172 | switch (event) { |
||
173 | case EVENT_COMMIT: |
||
174 | QuitUserInterface (0); |
||
175 | break; |
||
176 | } |
||
177 | return 0; |
||
178 | } |
||
179 | |||
180 | |||
181 | |||
182 | int CVICALLBACK SwitchOnOffCB (int panel, int control, int event, |
||
183 | void *callbackData, int eventData1, int eventData2) |
||
184 | { |
||
185 | unsigned char state; |
||
186 | switch (event) |
||
187 | { |
||
188 | case EVENT_COMMIT: |
||
189 | GetCtrlVal(panel, control, &state); |
||
190 | TMI_MainOutput(TMI_DeviceId, state); |
||
191 | break; |
||
192 | } |
||
193 | return 0; |
||
194 | } |
||
195 | |||
196 | |||
197 | |||
198 | /* Callback Function */ |
||
199 | void ComCallback(int portNumber, int eventMask, void *callbackdata) { |
||
200 | |||
201 | if (eventMask & LWRS_RXFLAG) { |
||
202 | //printf("Received specified character\n"); |
||
203 | int strLen = GetInQLen (COM_PORT); |
||
204 | bytes_read = ComRd (COM_PORT, read_data, strLen); |
||
205 | double temp= atof(read_data); |
||
206 | double f[10]; |
||
207 | int debug1; |
||
208 | GetCtrlVal(pa,PA_DEBUG_1, &debug1); |
||
209 | //printf("%f#%s#", temp, read_data); |
||
210 | sscanf(read_data,"%lf %lf %lf %lf %lf %lf",&f[0],&f[1],&f[2],&f[3],&f[4],&f[5]); |
||
211 | double humidity = f[5]; |
||
212 | // printf("%lf %lf %lf %lf %lf %lf",f[0],f[1],f[2],f[3],f[4],f[5]); |
||
213 | int RS232Error = ReturnRS232Err (); |
||
214 | if (ReturnRS232Err ()) { |
||
215 | sprintf(read_data,"#%s\n", GetRS232ErrorString(RS232Error)); |
||
216 | MessagePopup("RS232Err",read_data); |
||
217 | } else { |
||
218 | |||
219 | |||
220 | int polar; |
||
221 | SelectChannelCB (pa, PA_CHANNEL, EVENT_COMMIT, NULL, 0,0); |
||
222 | ReadVoltageCurrentCB (pa, PA_CHANNEL, EVENT_COMMIT, NULL, 0,0); |
||
223 | double tset,vmon,imon,vset,iset,vmax; |
||
224 | unsigned char ch, preset; |
||
225 | GetCtrlVal(pa,PA_TSET,&tset); |
||
226 | GetCtrlVal(pa,PA_VMON,&vmon); |
||
227 | GetCtrlVal(pa,PA_IMON,&imon); |
||
228 | GetCtrlVal(pa,PA_CHANNEL, &ch); |
||
229 | GetCtrlVal(pa,PA_PRESET,&preset); |
||
230 | TMI_Preset(TMI_DeviceId,preset); |
||
231 | GetCtrlVal(pa,PA_POLAR,&polar); |
||
232 | |||
233 | double tdiff = temp - tset; |
||
234 | double retpid = UpdatePID(&pid, tdiff, temp); |
||
235 | const double troom = 20; |
||
236 | double Pheat= 0.2 * (temp - troom); |
||
237 | double Ptec = retpid - Pheat*2; |
||
238 | vset = (polar*Ptec>0) ? sqrt(fabs(Ptec)) : 0; |
||
239 | 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); |
||
240 | GetCtrlVal(pa,PA_VMAX,&vmax); |
||
241 | if (vset >vmax) vset=vmax; |
||
242 | if (vset <0) vset =0; |
||
243 | if (debug1) printf("vset --->%f \n",vset); |
||
244 | TMI_Voltage(TMI_DeviceId, ch+1, preset, vset); |
||
245 | GetCtrlVal(pa,PA_IMAX,&iset); |
||
246 | TMI_Current(TMI_DeviceId, ch+1, preset, iset); |
||
247 | |||
248 | |||
249 | |||
250 | |||
251 | |||
252 | ReadVoltageCurrentCB (pa, PA_CHANNEL, EVENT_COMMIT, NULL, 0,0); |
||
253 | |||
254 | PlotStripChart (pa, PA_GRAPH, &temp, 1, 0, 0, VAL_DOUBLE); |
||
255 | double pgraph[2]={vmon,imon}; |
||
256 | PlotStripChart (pa, PA_GRAPH_VMON, pgraph, 2, 0, 0, VAL_DOUBLE); |
||
257 | PlotStripChart (pa, PA_GRAPH_3, &humidity, 1, 0, 0, VAL_DOUBLE); |
||
258 | SetCtrlVal(pa,PA_TMON,temp); |
||
259 | int log=0; |
||
260 | GetCtrlVal(pa,PA_LOG, &log); |
||
261 | if (log) { |
||
262 | char fname[0xFF]; |
||
263 | GetCtrlVal(pa,PA_FNAME, fname); |
||
264 | FILE *fp = fopen (fname,"a"); |
||
265 | fprintf(fp, "%u 0 %f\n", time(NULL), temp); |
||
266 | fclose(fp); |
||
267 | |||
268 | } |
||
269 | |||
270 | } |
||
271 | } |
||
272 | |||
273 | if (eventMask & LWRS_TXEMPTY) |
||
274 | |||
275 | printf("Transmit queue now empty\n"); |
||
276 | |||
277 | if (eventMask & LWRS_RECEIVE) { |
||
278 | |||
279 | printf("50 or more bytes in input queue\n"); |
||
280 | |||
281 | } |
||
282 | |||
283 | } |
||
284 | |||
285 | |||
286 | int CVICALLBACK StartCB (int panel, int control, int event, |
||
287 | void *callbackData, int eventData1, int eventData2) { |
||
288 | switch (event) { |
||
289 | case EVENT_COMMIT: |
||
290 | OpenComConfig (COM_PORT, "", 115200, 0, 8, 1, 512, 512); |
||
291 | /* Turn off Hardware handshaking (loopback test will not function with it on) */ |
||
292 | SetCTSMode (COM_PORT, LWRS_HWHANDSHAKE_OFF); |
||
293 | |||
294 | /* Make sure Serial buffers are empty */ |
||
295 | FlushInQ (COM_PORT); |
||
296 | FlushOutQ (COM_PORT); |
||
297 | |||
298 | int notifyCount = 50; /* Wait for at least 50 bytes in queue. */ |
||
299 | int eventChar = 10; /* Wait for LF. */ |
||
300 | int eventMask = LWRS_RXFLAG | LWRS_TXEMPTY | LWRS_RECEIVE; |
||
301 | InstallComCallback (COM_PORT, eventMask, notifyCount, eventChar, ComCallback, NULL); |
||
302 | SetDimming(1); |
||
303 | |||
304 | break; |
||
305 | } |
||
306 | return 0; |
||
307 | } |
||
308 | |||
309 | |||
310 | int CVICALLBACK StopCB (int panel, int control, int event, |
||
311 | void *callbackData, int eventData1, int eventData2) { |
||
312 | switch (event) { |
||
313 | case EVENT_COMMIT: |
||
314 | CloseCom(COM_PORT); |
||
315 | SetDimming(0); |
||
316 | break; |
||
317 | } |
||
318 | return 0; |
||
319 | } |
||
320 | |||
321 | int CVICALLBACK SetPresetCB (int panel, int control, int event, |
||
322 | void *callbackData, int eventData1, int eventData2) { |
||
323 | |||
324 | switch (event) { |
||
325 | case EVENT_COMMIT:{ |
||
326 | unsigned char preset; |
||
327 | double Voltage, Current; |
||
328 | GetCtrlVal(panel, control, &preset); |
||
329 | TMI_Preset(TMI_DeviceId, preset); |
||
330 | |||
331 | break; |
||
332 | } |
||
333 | } |
||
334 | return 0; |
||
335 | } |