Rev 303 | Rev 306 | 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); |
||
| 175 | } |
||
| 176 | break; |
||
| 177 | case TCP_DISCONNECT: |
||
| 178 | if (handle == g_hconversation) { |
||
| 179 | /* The client we were talking to has disconnected... */ |
||
| 180 | g_hconversation = 0; |
||
| 181 | printf("-- Client disconnected --\n"); |
||
| 182 | |||
| 183 | /* Note that we do not need to do any more because we set the*/ |
||
| 184 | /* disconnect mode to AUTO. */ |
||
| 185 | } |
||
| 186 | break; |
||
| 187 | } |
||
| 188 | return 0; |
||
| 189 | } |
||
| 190 | |||
| 191 | |||
| 192 | |||
| 193 | |||
| 194 | |||
| 195 | int main (int argc, char *argv[]) { |
||
| 303 | f9daq | 196 | int DeviceId=0; |
| 197 | unsigned char ch; |
||
| 198 | unsigned char MainOutput, preset; |
||
| 305 | f9daq | 199 | double Voltage, Current, tinterval; |
| 200 | char str[0xFF]; |
||
| 303 | f9daq | 201 | if (InitCVIRTE (0, argv, 0) == 0) |
| 202 | return -1; /* out of memory */ |
||
| 305 | f9daq | 203 | SetStdioPort (CVI_STDIO_WINDOW); |
| 204 | pid.iGain= 0.1; |
||
| 205 | pid.dGain= 2; |
||
| 206 | pid.pGain = 5; |
||
| 303 | f9daq | 207 | pid.iMax =100; |
| 208 | pid.iMin =-100 ; |
||
| 209 | pid.iState=0; |
||
| 210 | pid.dState=0; |
||
| 211 | |||
| 212 | if ((pa = LoadPanel (0, "TempCtrl.uir", PA)) < 0) |
||
| 213 | return -1; |
||
| 305 | f9daq | 214 | DisableBreakOnLibraryErrors(); |
| 215 | if (TMI_Open()== 0) MessagePopup("Error","Cannot open USB device"); |
||
| 216 | DeviceId = TMI_OpenHandle ("PW-A","USB:1:1"); |
||
| 217 | if (TMI_Test() == 0 )MessagePopup("DLL error","Dll Error"); |
||
| 218 | if (DeviceId < 0) MessagePopup("Error","Not Connected"); |
||
| 219 | printf("TMI device ID %d\n",TMI_DeviceId); |
||
| 303 | f9daq | 220 | |
| 305 | f9daq | 221 | TMI_MainOutputQ(TMI_DeviceId, &MainOutput); |
| 303 | f9daq | 222 | TMI_PresetQ(TMI_DeviceId, &preset); |
| 305 | f9daq | 223 | SetCtrlVal(pa, PA_ONOFF, MainOutput); |
| 224 | SetCtrlVal(pa, PA_PRESET, preset); |
||
| 303 | f9daq | 225 | /* |
| 305 | f9daq | 226 | GetCtrlVal(pa, P1_TINTERVAL, &tinterval); |
| 227 | SetCtrlAttribute (pa, P1_TIMER, ATTR_INTERVAL, tinterval); |
||
| 228 | */ |
||
| 229 | |||
| 230 | SetWaitCursor (1); |
||
| 231 | int portNum = 10000; |
||
| 232 | int registered = 0; |
||
| 233 | char tempBuf[256] = {0}; |
||
| 234 | if (RegisterTCPServer (portNum, ServerTCPCB, 0) < 0) |
||
| 235 | MessagePopup("TCP Server", "Server registration failed!"); |
||
| 236 | else { |
||
| 237 | SetWaitCursor (0); |
||
| 238 | registered = 1; |
||
| 239 | |||
| 240 | /* We are successfully connected -- gather info */ |
||
| 241 | |||
| 242 | if (GetTCPHostAddr (tempBuf, 256) >= 0) printf("%s\n" ,tempBuf); |
||
| 243 | if (GetTCPHostName (tempBuf, 256) >= 0) printf("%s\n" ,tempBuf); |
||
| 244 | |||
| 245 | |||
| 246 | |||
| 247 | } |
||
| 248 | |||
| 249 | SelectChannelCB (pa, PA_CHANNEL, EVENT_COMMIT, NULL, 0,0); |
||
| 303 | f9daq | 250 | ReadVoltageCurrentCB (pa, PA_CHANNEL, EVENT_COMMIT, NULL, 0,0); |
| 305 | f9daq | 251 | |
| 252 | |||
| 253 | |||
| 303 | f9daq | 254 | DisplayPanel (pa); |
| 255 | SetDimming(0); |
||
| 256 | RunUserInterface (); |
||
| 257 | CloseCom(COM_PORT); |
||
| 305 | f9daq | 258 | if (registered) |
| 259 | UnregisterTCPServer (portNum); |
||
| 303 | f9daq | 260 | DiscardPanel (pa); |
| 305 | f9daq | 261 | TMI_Close(); |
| 262 | if (gFp) fclose(gFp); |
||
| 303 | f9daq | 263 | return 0; |
| 264 | } |
||
| 265 | |||
| 266 | |||
| 267 | int CVICALLBACK ExitCB (int panel, int control, int event, |
||
| 268 | void *callbackData, int eventData1, int eventData2) { |
||
| 269 | switch (event) { |
||
| 270 | case EVENT_COMMIT: |
||
| 271 | QuitUserInterface (0); |
||
| 272 | break; |
||
| 273 | } |
||
| 274 | return 0; |
||
| 275 | } |
||
| 276 | |||
| 277 | |||
| 278 | |||
| 279 | int CVICALLBACK SwitchOnOffCB (int panel, int control, int event, |
||
| 305 | f9daq | 280 | void *callbackData, int eventData1, int eventData2) { |
| 281 | unsigned char state; |
||
| 282 | switch (event) { |
||
| 283 | case EVENT_COMMIT: |
||
| 284 | GetCtrlVal(panel, control, &state); |
||
| 285 | TMI_MainOutput(TMI_DeviceId, state); |
||
| 286 | break; |
||
| 287 | } |
||
| 288 | return 0; |
||
| 303 | f9daq | 289 | } |
| 290 | |||
| 291 | |||
| 292 | |||
| 293 | /* Callback Function */ |
||
| 294 | void ComCallback(int portNumber, int eventMask, void *callbackdata) { |
||
| 305 | f9daq | 295 | static double told=0; |
| 303 | f9daq | 296 | if (eventMask & LWRS_RXFLAG) { |
| 297 | //printf("Received specified character\n"); |
||
| 298 | int strLen = GetInQLen (COM_PORT); |
||
| 299 | bytes_read = ComRd (COM_PORT, read_data, strLen); |
||
| 300 | double temp= atof(read_data); |
||
| 301 | double f[10]; |
||
| 302 | int debug1; |
||
| 303 | GetCtrlVal(pa,PA_DEBUG_1, &debug1); |
||
| 304 | //printf("%f#%s#", temp, read_data); |
||
| 305 | sscanf(read_data,"%lf %lf %lf %lf %lf %lf",&f[0],&f[1],&f[2],&f[3],&f[4],&f[5]); |
||
| 306 | double humidity = f[5]; |
||
| 305 | f9daq | 307 | // printf("%lf %lf %lf %lf %lf %lf",f[0],f[1],f[2],f[3],f[4],f[5]); |
| 303 | f9daq | 308 | int RS232Error = ReturnRS232Err (); |
| 309 | if (ReturnRS232Err ()) { |
||
| 310 | sprintf(read_data,"#%s\n", GetRS232ErrorString(RS232Error)); |
||
| 311 | MessagePopup("RS232Err",read_data); |
||
| 312 | } else { |
||
| 305 | f9daq | 313 | |
| 314 | |||
| 303 | f9daq | 315 | int polar; |
| 316 | SelectChannelCB (pa, PA_CHANNEL, EVENT_COMMIT, NULL, 0,0); |
||
| 317 | ReadVoltageCurrentCB (pa, PA_CHANNEL, EVENT_COMMIT, NULL, 0,0); |
||
| 318 | double tset,vmon,imon,vset,iset,vmax; |
||
| 319 | unsigned char ch, preset; |
||
| 320 | GetCtrlVal(pa,PA_TSET,&tset); |
||
| 321 | GetCtrlVal(pa,PA_VMON,&vmon); |
||
| 305 | f9daq | 322 | GetCtrlVal(pa,PA_IMON,&imon); |
| 303 | f9daq | 323 | GetCtrlVal(pa,PA_CHANNEL, &ch); |
| 324 | GetCtrlVal(pa,PA_PRESET,&preset); |
||
| 325 | TMI_Preset(TMI_DeviceId,preset); |
||
| 326 | GetCtrlVal(pa,PA_POLAR,&polar); |
||
| 305 | f9daq | 327 | |
| 303 | f9daq | 328 | double tdiff = temp - tset; |
| 305 | f9daq | 329 | double retpid = UpdatePID(&pid, tdiff, temp); |
| 303 | f9daq | 330 | const double troom = 20; |
| 331 | double Pheat= 0.2 * (temp - troom); |
||
| 332 | double Ptec = retpid - Pheat*2; |
||
| 333 | vset = (polar*Ptec>0) ? sqrt(fabs(Ptec)) : 0; |
||
| 305 | f9daq | 334 | 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); |
| 335 | GetCtrlVal(pa,PA_VMAX,&vmax); |
||
| 303 | f9daq | 336 | if (vset >vmax) vset=vmax; |
| 337 | if (vset <0) vset =0; |
||
| 305 | f9daq | 338 | if (debug1) printf("vset --->%f \n",vset); |
| 303 | f9daq | 339 | TMI_Voltage(TMI_DeviceId, ch+1, preset, vset); |
| 340 | GetCtrlVal(pa,PA_IMAX,&iset); |
||
| 305 | f9daq | 341 | TMI_Current(TMI_DeviceId, ch+1, preset, iset); |
| 342 | |||
| 343 | |||
| 344 | |||
| 345 | |||
| 346 | |||
| 303 | f9daq | 347 | ReadVoltageCurrentCB (pa, PA_CHANNEL, EVENT_COMMIT, NULL, 0,0); |
| 305 | f9daq | 348 | |
| 303 | f9daq | 349 | PlotStripChart (pa, PA_GRAPH, &temp, 1, 0, 0, VAL_DOUBLE); |
| 305 | f9daq | 350 | double pgraph[3]= {vmon,imon, 100*(temp-told)}; |
| 351 | PlotStripChart (pa, PA_GRAPH_VMON, pgraph, 3, 0, 0, VAL_DOUBLE); |
||
| 303 | f9daq | 352 | PlotStripChart (pa, PA_GRAPH_3, &humidity, 1, 0, 0, VAL_DOUBLE); |
| 353 | SetCtrlVal(pa,PA_TMON,temp); |
||
| 305 | f9daq | 354 | |
| 355 | SetCtrlVal(pa,PA_HUMIDITY,humidity); |
||
| 356 | |||
| 357 | char transmitBuf[512]= {0}; |
||
| 358 | sprintf(transmitBuf, "%u %f %f %f %f\n", time(NULL), humidity, temp, tdiff, temp-told); |
||
| 359 | if (g_hconversation) |
||
| 360 | if ( ServerTCPWrite (g_hconversation, transmitBuf, strlen (transmitBuf), 1000) < 0) printf("Transmit Error\n"); |
||
| 303 | f9daq | 361 | int log=0; |
| 362 | GetCtrlVal(pa,PA_LOG, &log); |
||
| 363 | if (log) { |
||
| 364 | char fname[0xFF]; |
||
| 365 | GetCtrlVal(pa,PA_FNAME, fname); |
||
| 366 | FILE *fp = fopen (fname,"a"); |
||
| 305 | f9daq | 367 | fprintf(fp,transmitBuf); |
| 303 | f9daq | 368 | fclose(fp); |
| 369 | |||
| 370 | } |
||
| 305 | f9daq | 371 | told= temp; |
| 303 | f9daq | 372 | |
| 373 | } |
||
| 374 | } |
||
| 375 | |||
| 376 | if (eventMask & LWRS_TXEMPTY) |
||
| 377 | |||
| 378 | printf("Transmit queue now empty\n"); |
||
| 379 | |||
| 380 | if (eventMask & LWRS_RECEIVE) { |
||
| 381 | |||
| 382 | printf("50 or more bytes in input queue\n"); |
||
| 383 | |||
| 384 | } |
||
| 385 | |||
| 386 | } |
||
| 387 | |||
| 388 | |||
| 389 | int CVICALLBACK StartCB (int panel, int control, int event, |
||
| 390 | void *callbackData, int eventData1, int eventData2) { |
||
| 391 | switch (event) { |
||
| 392 | case EVENT_COMMIT: |
||
| 393 | OpenComConfig (COM_PORT, "", 115200, 0, 8, 1, 512, 512); |
||
| 394 | /* Turn off Hardware handshaking (loopback test will not function with it on) */ |
||
| 395 | SetCTSMode (COM_PORT, LWRS_HWHANDSHAKE_OFF); |
||
| 396 | |||
| 397 | /* Make sure Serial buffers are empty */ |
||
| 398 | FlushInQ (COM_PORT); |
||
| 399 | FlushOutQ (COM_PORT); |
||
| 400 | |||
| 401 | int notifyCount = 50; /* Wait for at least 50 bytes in queue. */ |
||
| 402 | int eventChar = 10; /* Wait for LF. */ |
||
| 403 | int eventMask = LWRS_RXFLAG | LWRS_TXEMPTY | LWRS_RECEIVE; |
||
| 404 | InstallComCallback (COM_PORT, eventMask, notifyCount, eventChar, ComCallback, NULL); |
||
| 405 | SetDimming(1); |
||
| 406 | |||
| 407 | break; |
||
| 408 | } |
||
| 409 | return 0; |
||
| 410 | } |
||
| 411 | |||
| 412 | |||
| 413 | int CVICALLBACK StopCB (int panel, int control, int event, |
||
| 414 | void *callbackData, int eventData1, int eventData2) { |
||
| 415 | switch (event) { |
||
| 416 | case EVENT_COMMIT: |
||
| 417 | CloseCom(COM_PORT); |
||
| 418 | SetDimming(0); |
||
| 419 | break; |
||
| 420 | } |
||
| 421 | return 0; |
||
| 422 | } |
||
| 423 | |||
| 424 | int CVICALLBACK SetPresetCB (int panel, int control, int event, |
||
| 425 | void *callbackData, int eventData1, int eventData2) { |
||
| 426 | |||
| 427 | switch (event) { |
||
| 305 | f9daq | 428 | case EVENT_COMMIT: { |
| 303 | f9daq | 429 | unsigned char preset; |
| 430 | double Voltage, Current; |
||
| 305 | f9daq | 431 | GetCtrlVal(panel, control, &preset); |
| 303 | f9daq | 432 | TMI_Preset(TMI_DeviceId, preset); |
| 305 | f9daq | 433 | |
| 303 | f9daq | 434 | break; |
| 435 | } |
||
| 436 | } |
||
| 437 | return 0; |
||
| 438 | } |
||
| 305 | f9daq | 439 |