Blame |
Last modification |
View Log
| RSS feed
#include "K6517-ctrl.h"
#include <tcpsupp.h>
#include <rs232.h>
#include <utility.h>
#include <userint.h>
#include <ansi_c.h>
#include "K2231A.h"
#include "TempCtrl.h"
FILE
*gFp
;
#define RSTREG(a,x) (a&=(~(x)))
#define SETREG(a,x) (a|=x)
int gMask
=0xF;
static int pa
;
#define TEXT_LENGTH 2000
char read_data
[TEXT_LENGTH
];
int read_term_index
;
int read_term
;
int read_cnt
;
int bytes_read
;
#define COM_PORT 11
typedef struct {
double dState
; // Last temperature input
double iState
; // Integrator state
double iMax
, iMin
; // Maximum and minimum allowable integrator state
double iGain
, // integral gain
pGain
, // proportional gain
dGain
; // derivative gain
} SPid
;
SPid pid
;
double UpdatePID
(SPid
*pid
, double error
, double temperature
) {
double pTerm
, dTerm
, iTerm
;
pTerm
= pid
->pGain
* error
;
// calculate the proportional term
// calculate the integral state with appropriate limiting
pid
->iState
+= error
;
if (pid
->iState
> pid
->iMax
) pid
->iState
= pid
->iMax
;
else if (pid
->iState
< pid
->iMin
) pid
->iState
= pid
->iMin
;
//if (debug )
iTerm
= pid
->iGain
* pid
->iState
; // calculate the integral term
dTerm
= pid
->dGain
* (temperature
- pid
->dState
);
pid
->dState
= temperature
;
return pTerm
+ iTerm
- dTerm
;
}
void SetDimming
(int state
) {
SetCtrlAttribute
(pa
, PA_START
, ATTR_DIMMED
, state
);
SetCtrlAttribute
(pa
, PA_EXIT
, ATTR_DIMMED
, state
);
SetCtrlAttribute
(pa
, PA_STOP
, ATTR_DIMMED
, !state
);
}
int CVICALLBACK ReadVoltageCurrentCB
(int panel
, int control
, int event
,
void *callbackData
, int eventData1
, int eventData2
) {
int iRet
;
char ch
=0;
double Voltage
;
double Current
;
char cv_cc
;
switch (event
) {
case EVENT_COMMIT
:
GetCtrlVal
(pa
,PA_CHANNEL
, &ch
);
char cmd
[0xFF];
K2231A_SelectChannel
(ch
+1);
cv_cc
= K2231A_GetOperationMode
();
Voltage
=K2231A_GetVoltageMonitor
();
Current
=K2231A_GetCurrentMonitor
();
SetCtrlVal
(panel
, PA_VMON
, Voltage
);
SetCtrlVal
(panel
, PA_IMON
, Current
);
SetCtrlVal
(panel
, PA_CVCC
, cv_cc
);
break;
}
return 0;
}
int CVICALLBACK SelectChannelCB
(int panel
, int control
, int event
,
void *callbackData
, int eventData1
, int eventData2
) {
unsigned char state
, preset
;
double Voltage
, Current
;
unsigned char ch
;
switch (event
) {
case EVENT_COMMIT
:
GetCtrlVal
(pa
,PA_CHANNEL
, &ch
);
K2231A_SelectChannel
(ch
+1);
Voltage
=K2231A_GetSetVoltage
();
Current
=K2231A_GetSetCurrent
();
SetCtrlVal
(pa
, PA_VSET
, Voltage
);
SetCtrlVal
(pa
, PA_ISET
, Current
);
break;
}
return 0;
}
int SetCooling
(char onoff
){
int nb
=0;
nb
= ComWrt
(COM_PORT
, &onoff
, 1);
return nb
;
}
#define tcpChk(f) if ((g_TCPError=(f)) < 0) {ReportTCPError(); return -1; }
/*---------------------------------------------------------------------------*/
/* Module-globals */
/*---------------------------------------------------------------------------*/
static unsigned int g_hconversation
;
static int g_TCPError
= 0;
/*---------------------------------------------------------------------------*/
/* Internal function prototypes */
/*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/
/* Report TCP Errors if any */
/*---------------------------------------------------------------------------*/
static void ReportTCPError
(void) {
if (g_TCPError
< 0) {
char messageBuffer
[1024];
sprintf(messageBuffer
,
"TCP library error message: %s\nSystem error message: %s",
GetTCPErrorString
(g_TCPError
), GetTCPSystemErrorString
());
MessagePopup
("Error", messageBuffer
);
}
}
int CVICALLBACK ServerTCPCB
(unsigned handle
, int event
, int error
,
void *callbackData
) {
char receiveBuf
[256] = {0};
ssize_t dataSize
= sizeof (receiveBuf
) - 1;
char addrBuf
[31];
switch (event
) {
case TCP_CONNECT
:
if (g_hconversation
) {
/* We already have one client, don't accept another... */
tcpChk
(GetTCPPeerAddr
(handle
, addrBuf
, 31));
sprintf (receiveBuf
, "-- Refusing conection request from "
"%s --\n", addrBuf
);
printf("%s\n", receiveBuf
);
tcpChk
(DisconnectTCPClient
(handle
));
} else {
/* Handle this new client connection */
g_hconversation
= handle
;
tcpChk
(GetTCPPeerAddr
(g_hconversation
, addrBuf
, 31));
printf("%s\n", addrBuf
);
tcpChk
(GetTCPPeerName
(g_hconversation
, receiveBuf
, 256));
printf("%s\n", receiveBuf
);
sprintf (receiveBuf
, "-- New connection from %s --\n",
addrBuf
);
printf("%s\n", receiveBuf
);
/* Set the disconect mode so we do not need to terminate */
/* connections ourselves. */
tcpChk
(SetTCPDisconnectMode
(g_hconversation
, TCP_DISCONNECT_AUTO
));
}
break;
case TCP_DATAREADY
:
if ((dataSize
= ServerTCPRead
(g_hconversation
, receiveBuf
, dataSize
, 1000)) < 0) {
printf("Receive Error\n");
} else {
receiveBuf
[dataSize
] = '\0';
printf("Received %s\n", receiveBuf
);
int cmd
=0;
float arg
;
sscanf(receiveBuf
,"%d%f",&cmd
,&arg
);
switch (cmd
){
case 1:
SetCtrlVal
(pa
,PA_TSET
,arg
);
break;
default:
break;
}
}
break;
case TCP_DISCONNECT
:
if (handle
== g_hconversation
) {
/* The client we were talking to has disconnected... */
g_hconversation
= 0;
printf("-- Client disconnected --\n");
/* Note that we do not need to do any more because we set the*/
/* disconnect mode to AUTO. */
}
break;
}
return 0;
}
int main
(int argc
, char *argv
[]) {
int DeviceId
=0;
unsigned char ch
;
unsigned char MainOutput
, preset
=1;
double Voltage
, Current
, tinterval
;
char str
[0xFF];
if (InitCVIRTE
(0, argv
, 0) == 0)
return -1; /* out of memory */
SetStdioPort
(CVI_STDIO_WINDOW
);
pid.
iGain= 0.1;
pid.
dGain= 2;
pid.
pGain = 5;
pid.
iMax =100;
pid.
iMin =-100 ;
pid.
iState=0;
pid.
dState=0;
if ((pa
= LoadPanel
(0, "TempCtrl.uir", PA
)) < 0)
return -1;
DisableBreakOnLibraryErrors
();
K2231A_SetPort
(6);
K2231A_SetDebug
(1);
if (K2231A_Open
()== 0) MessagePopup
("Error","Cannot open USB device");
K2231A_Test
();
MainOutput
= K2231A_GetSwitch
();
SetCtrlVal
(pa
, PA_ONOFF
, MainOutput
);
SetCtrlVal
(pa
, PA_PRESET
, preset
);
int debug1
;
GetCtrlVal
(pa
,PA_DEBUG_1
, &debug1
);
K2231A_SetDebug
(1);
SetCtrlVal
(pa
,PA_CHANNEL
, 0);
/*
GetCtrlVal(pa, P1_TINTERVAL, &tinterval);
SetCtrlAttribute (pa, P1_TIMER, ATTR_INTERVAL, tinterval);
*/
SetWaitCursor
(1);
int portNum
= 10000;
int registered
= 0;
char tempBuf
[256] = {0};
if (RegisterTCPServer
(portNum
, ServerTCPCB
, 0) < 0)
MessagePopup
("TCP Server", "Server registration failed!");
else {
SetWaitCursor
(0);
registered
= 1;
/* We are successfully connected -- gather info */
if (GetTCPHostAddr
(tempBuf
, 256) >= 0) printf("%s\n" ,tempBuf
);
if (GetTCPHostName
(tempBuf
, 256) >= 0) printf("%s\n" ,tempBuf
);
}
SelectChannelCB
(pa
, PA_CHANNEL
, EVENT_COMMIT
, NULL
, 0,0);
ReadVoltageCurrentCB
(pa
, PA_CHANNEL
, EVENT_COMMIT
, NULL
, 0,0);
DisplayPanel
(pa
);
SetDimming
(0);
RunUserInterface
();
CloseCom
(COM_PORT
);
if (registered
)
UnregisterTCPServer
(portNum
);
DiscardPanel
(pa
);
K2231A_Close
();
if (gFp
) fclose(gFp
);
return 0;
}
int CVICALLBACK ExitCB
(int panel
, int control
, int event
,
void *callbackData
, int eventData1
, int eventData2
) {
switch (event
) {
case EVENT_COMMIT
:
QuitUserInterface
(0);
break;
}
return 0;
}
int CVICALLBACK SwitchCoolingOnOffCB
(int panel
, int control
, int event
,
void *callbackData
, int eventData1
, int eventData2
) {
int onoff
=0;
switch (event
) {
case EVENT_COMMIT
:
GetCtrlVal
(pa
,control
,&onoff
);
SetCooling
(onoff
);
break;
}
return 0;
}
int CVICALLBACK SwitchOnOffCB
(int panel
, int control
, int event
,
void *callbackData
, int eventData1
, int eventData2
) {
unsigned char state
;
switch (event
) {
case EVENT_COMMIT
:
GetCtrlVal
(panel
, control
, &state
);
K2231A_SetSwitch
(state
);
break;
}
return 0;
}
/* Callback Function */
void ComCallback
(int portNumber
, int eventMask
, void *callbackdata
) {
static double told
=0;
if (eventMask
& LWRS_RXFLAG
) {
//printf("Received specified character\n");
int strLen
= GetInQLen
(COM_PORT
);
bytes_read
= ComRd
(COM_PORT
, read_data
, strLen
);
double temp
= atof(read_data
);
double f
[10];
int debug1
;
GetCtrlVal
(pa
,PA_DEBUG_1
, &debug1
);
K2231A_SetDebug
(debug1
);
//printf("%f#%s#", temp, read_data);
// temp1 , temp2 , temp3 , temp4 , temp humidity sens, rel.humidity
if (bytes_read
>0) {
SetCtrlVal
(pa
,PA_ARDUINO
, read_data
);
}
sscanf(read_data
,"%lf %lf %lf %lf %lf %lf",&f
[0],&f
[1],&f
[2],&f
[3],&f
[4],&f
[5]);
double humidity
= f
[5];
double htemp
= f
[4];
// printf("%lf %lf %lf %lf %lf %lf",f[0],f[1],f[2],f[3],f[4],f[5]);
int RS232Error
= ReturnRS232Err
();
if (ReturnRS232Err
()) {
sprintf(read_data
,"#%s\n", GetRS232ErrorString
(RS232Error
));
MessagePopup
("RS232Err",read_data
);
} else {
int polar
;
ProcessSystemEvents
();
SelectChannelCB
(pa
, PA_CHANNEL
, EVENT_COMMIT
, NULL
, 0,0);
ProcessSystemEvents
();
ReadVoltageCurrentCB
(pa
, PA_CHANNEL
, EVENT_COMMIT
, NULL
, 0,0);
ProcessSystemEvents
();
double tset
,vmon
,imon
,vset
,iset
,vmax
;
unsigned char ch
, preset
;
GetCtrlVal
(pa
,PA_TSET
,&tset
);
GetCtrlVal
(pa
,PA_VMON
,&vmon
);
GetCtrlVal
(pa
,PA_IMON
,&imon
);
GetCtrlVal
(pa
,PA_CHANNEL
, &ch
);
//GetCtrlVal(pa,PA_PRESET,&preset);
//TMI_Preset(TMI_DeviceId,preset);
GetCtrlVal
(pa
,PA_POLAR
,&polar
);
double tdiff
= temp
- tset
;
double retpid
= UpdatePID
(&pid
, tdiff
, temp
);
const double troom
= htemp
;
double Pheat
= 0.2 * (temp
- troom
);
double Ptec
= retpid
- Pheat
*2;
vset
= (polar
*Ptec
>0) ? sqrt(fabs(Ptec
)) : 0;
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
);
GetCtrlVal
(pa
,PA_VMAX
,&vmax
);
if (vset
>vmax
) vset
=vmax
;
if (vset
<0) vset
=0;
if (debug1
) printf("vset --->%f \n",vset
);
K2231A_SelectChannel
(ch
+1);
K2231A_SetVoltage
(vset
);
GetCtrlVal
(pa
,PA_IMAX
,&iset
);
K2231A_SetCurrent
(iset
);
ReadVoltageCurrentCB
(pa
, PA_CHANNEL
, EVENT_COMMIT
, NULL
, 0,0);
PlotStripChart
(pa
, PA_GRAPH
, &temp
, 1, 0, 0, VAL_DOUBLE
);
double pgraph
[3]= {vmon
,imon
, 100*(temp
-told
)};
PlotStripChart
(pa
, PA_GRAPH_VMON
, pgraph
, 3, 0, 0, VAL_DOUBLE
);
PlotStripChart
(pa
, PA_GRAPH_3
, &humidity
, 1, 0, 0, VAL_DOUBLE
);
SetCtrlVal
(pa
,PA_TMON
,temp
);
SetCtrlVal
(pa
,PA_HTMON
,htemp
);
SetCtrlVal
(pa
,PA_HUMIDITY
,humidity
);
char transmitBuf
[512]= {0};
sprintf(transmitBuf
, "%u %f %f %f %f\n", time(NULL
), humidity
, temp
, tdiff
, temp
-told
);
if (g_hconversation
)
if ( ServerTCPWrite
(g_hconversation
, transmitBuf
, strlen (transmitBuf
), 1000) < 0) printf("Transmit Error\n");
int log=0;
GetCtrlVal
(pa
,PA_LOG
, &log);
if (log) {
char fname
[0xFF];
GetCtrlVal
(pa
,PA_FNAME
, fname
);
FILE
*fp
= fopen (fname
,"a");
fprintf(fp
,transmitBuf
);
fclose(fp
);
}
told
= temp
;
}
}
if (eventMask
& LWRS_TXEMPTY
)
printf("Transmit queue now empty\n");
if (eventMask
& LWRS_RECEIVE
) {
printf("50 or more bytes in input queue\n");
}
}
int CVICALLBACK StartCB
(int panel
, int control
, int event
,
void *callbackData
, int eventData1
, int eventData2
) {
switch (event
) {
case EVENT_COMMIT
:
OpenComConfig
(COM_PORT
, "", 115200, 0, 8, 1, 512, 512);
/* Turn off Hardware handshaking (loopback test will not function with it on) */
SetCTSMode
(COM_PORT
, LWRS_HWHANDSHAKE_OFF
);
/* Make sure Serial buffers are empty */
FlushInQ
(COM_PORT
);
FlushOutQ
(COM_PORT
);
int notifyCount
= 50; /* Wait for at least 50 bytes in queue. */
int eventChar
= 10; /* Wait for LF. */
int eventMask
= LWRS_RXFLAG
| LWRS_TXEMPTY
| LWRS_RECEIVE
;
InstallComCallback
(COM_PORT
, eventMask
, notifyCount
, eventChar
, ComCallback
, NULL
);
SetDimming
(1);
break;
}
return 0;
}
int CVICALLBACK StopCB
(int panel
, int control
, int event
,
void *callbackData
, int eventData1
, int eventData2
) {
switch (event
) {
case EVENT_COMMIT
:
CloseCom
(COM_PORT
);
SetDimming
(0);
break;
}
return 0;
}
int CVICALLBACK SetPresetCB
(int panel
, int control
, int event
,
void *callbackData
, int eventData1
, int eventData2
) {
switch (event
) {
case EVENT_COMMIT
: {
unsigned char preset
;
double Voltage
, Current
;
GetCtrlVal
(panel
, control
, &preset
);
K2231A_RecallFromMemory
(preset
);
break;
}
}
return 0;
}