Subversion Repositories f9daq

Compare Revisions

Ignore whitespace Rev 304 → Rev 305

/cvi/apps/TempCtrl/TempCtrl.c
1,10 → 1,12
#include "K6517-ctrl.h"
#include <tcpsupp.h>
#include <rs232.h>
#include <utility.h>
#include <userint.h>
#include <ansi_c.h>
#include <pw18-1.8aq.h>
#include "TempCtrl.h"
FILE *gFp;
#include <pw18-1.8aq.h>
#include "TempCtrl.h"
FILE *gFp;
 
#define RSTREG(a,x) (a&=(~(x)))
#define SETREG(a,x) (a|=x)
24,32 → 26,30
#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
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;
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) {
61,74 → 61,149
 
 
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);
iRet = TMI_TimeOut(TMI_DeviceId, 1);
iRet = TMI_Refresh(TMI_DeviceId);
iRet = TMI_MoniDataQ(TMI_DeviceId, ch+1, &Voltage, &Current, &cv_cc);
SetCtrlVal(panel, PA_VMON, Voltage);
SetCtrlVal(panel, PA_IMON, Current);
SetCtrlVal(panel, PA_CVCC, cv_cc);
break;
}
return 0;
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);
iRet = TMI_TimeOut(TMI_DeviceId, 1);
iRet = TMI_Refresh(TMI_DeviceId);
 
 
iRet = TMI_MoniDataQ(TMI_DeviceId, ch+1, &Voltage, &Current, &cv_cc);
 
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;
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);
unsigned char ch;
switch (event) {
case EVENT_COMMIT:
GetCtrlVal(pa,PA_CHANNEL, &ch);
GetCtrlVal(pa,PA_PRESET, &preset);
TMI_VoltageQ(TMI_DeviceId, ch+1, preset, &Voltage);
TMI_CurrentQ(TMI_DeviceId, ch+1, preset, &Current);
SetCtrlVal(pa, PA_VSET, Voltage);
SetCtrlVal(pa, PA_ISET, Current);
break;
}
return 0;
 
 
 
TMI_VoltageQ(TMI_DeviceId, ch+1, preset, &Voltage);
TMI_CurrentQ(TMI_DeviceId, ch+1, preset, &Current);
SetCtrlVal(pa, PA_VSET, Voltage);
SetCtrlVal(pa, PA_ISET, Current);
 
break;
}
return 0;
}
 
 
#define tcpChk(f) if ((g_TCPError=(f)) < 0) {ReportTCPError(); return -1; }
 
/*---------------------------------------------------------------------------*/
/* Module-globals */
/*---------------------------------------------------------------------------*/
static unsigned int g_hconversation;
static int g_TCPError = 0;
 
int main (int argc, char *argv[])
{
/*---------------------------------------------------------------------------*/
/* 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);
}
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;
double Voltage, Current, tinterval;
char str[0xFF];
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.3;
pid.dGain= 0.5;
pid.pGain = 3;
SetStdioPort (CVI_STDIO_WINDOW);
pid.iGain= 0.1;
pid.dGain= 2;
pid.pGain = 5;
pid.iMax =100;
pid.iMin =-100 ;
pid.iState=0;
136,33 → 211,55
 
if ((pa = LoadPanel (0, "TempCtrl.uir", PA)) < 0)
return -1;
DisableBreakOnLibraryErrors();
if (TMI_Open()== 0) MessagePopup("Error","Cannot open USB device");
DeviceId = TMI_OpenHandle ("PW-A","USB:1:1");
if (TMI_Test() == 0 )MessagePopup("DLL error","Dll Error");
if (DeviceId < 0) MessagePopup("Error","Not Connected");
printf("TMI device ID %d\n",TMI_DeviceId);
 
if (TMI_Open()== 0) MessagePopup("Error","Cannot open USB device");
DeviceId = TMI_OpenHandle ("PW-A","USB:1:1");
if (TMI_Test() == 0 )MessagePopup("DLL error","Dll Error");
if (DeviceId < 0) MessagePopup("Error","Not Connected");
printf("TMI device ID %d\n",TMI_DeviceId);
TMI_MainOutputQ(TMI_DeviceId, &MainOutput);
TMI_MainOutputQ(TMI_DeviceId, &MainOutput);
TMI_PresetQ(TMI_DeviceId, &preset);
SetCtrlVal(pa, PA_ONOFF, MainOutput);
SetCtrlVal(pa, PA_PRESET, preset);
SetCtrlVal(pa, PA_ONOFF, MainOutput);
SetCtrlVal(pa, PA_PRESET, preset);
/*
GetCtrlVal(pa, P1_TINTERVAL, &tinterval);
SetCtrlAttribute (pa, P1_TIMER, ATTR_INTERVAL, tinterval);
*/
SelectChannelCB (pa, PA_CHANNEL, EVENT_COMMIT, NULL, 0,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);
TMI_Close();
if (gFp) fclose(gFp);
TMI_Close();
if (gFp) fclose(gFp);
return 0;
}
 
180,17 → 277,15
 
 
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);
TMI_MainOutput(TMI_DeviceId, state);
break;
}
return 0;
void *callbackData, int eventData1, int eventData2) {
unsigned char state;
switch (event) {
case EVENT_COMMIT:
GetCtrlVal(panel, control, &state);
TMI_MainOutput(TMI_DeviceId, state);
break;
}
return 0;
}
 
 
197,7 → 292,7
 
/* 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);
209,14 → 304,14
//printf("%f#%s#", temp, 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];
// printf("%lf %lf %lf %lf %lf %lf",f[0],f[1],f[2],f[3],f[4],f[5]);
// 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;
SelectChannelCB (pa, PA_CHANNEL, EVENT_COMMIT, NULL, 0,0);
ReadVoltageCurrentCB (pa, PA_CHANNEL, EVENT_COMMIT, NULL, 0,0);
224,38 → 319,45
unsigned char ch, preset;
GetCtrlVal(pa,PA_TSET,&tset);
GetCtrlVal(pa,PA_VMON,&vmon);
GetCtrlVal(pa,PA_IMON,&imon);
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);
double retpid = UpdatePID(&pid, tdiff, temp);
const double troom = 20;
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 (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);
if (debug1) printf("vset --->%f \n",vset);
TMI_Voltage(TMI_DeviceId, ch+1, preset, vset);
GetCtrlVal(pa,PA_IMAX,&iset);
TMI_Current(TMI_DeviceId, ch+1, preset, iset);
TMI_Current(TMI_DeviceId, ch+1, preset, iset);
 
 
 
 
 
ReadVoltageCurrentCB (pa, PA_CHANNEL, EVENT_COMMIT, NULL, 0,0);
 
PlotStripChart (pa, PA_GRAPH, &temp, 1, 0, 0, VAL_DOUBLE);
double pgraph[2]={vmon,imon};
PlotStripChart (pa, PA_GRAPH_VMON, pgraph, 2, 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_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) {
262,10 → 364,11
char fname[0xFF];
GetCtrlVal(pa,PA_FNAME, fname);
FILE *fp = fopen (fname,"a");
fprintf(fp, "%u 0 %f\n", time(NULL), temp);
fprintf(fp,transmitBuf);
fclose(fp);
 
}
told= temp;
 
}
}
322,14 → 425,15
void *callbackData, int eventData1, int eventData2) {
 
switch (event) {
case EVENT_COMMIT:{
case EVENT_COMMIT: {
unsigned char preset;
double Voltage, Current;
GetCtrlVal(panel, control, &preset);
GetCtrlVal(panel, control, &preset);
TMI_Preset(TMI_DeviceId, preset);
 
break;
}
}
return 0;
}
 
/cvi/apps/TempCtrl/TempCtrl.h
18,24 → 18,25
#define PA_STOP 3 /* control type: command, callback function: StopCB */
#define PA_START 4 /* control type: command, callback function: StartCB */
#define PA_TSET 5 /* control type: numeric, callback function: (none) */
#define PA_TMON 6 /* control type: numeric, callback function: (none) */
#define PA_DEBUG_1 7 /* control type: radioButton, callback function: (none) */
#define PA_LOG 8 /* control type: radioButton, callback function: (none) */
#define PA_GRAPH_VMON 9 /* control type: strip, callback function: (none) */
#define PA_GRAPH_3 10 /* control type: strip, callback function: (none) */
#define PA_GRAPH 11 /* control type: strip, callback function: (none) */
#define PA_FNAME 12 /* control type: string, callback function: (none) */
#define PA_ONOFF 13 /* control type: binary, callback function: SwitchOnOffCB */
#define PA_CHANNEL 14 /* control type: numeric, callback function: SelectChannelCB */
#define PA_IMAX 15 /* control type: numeric, callback function: (none) */
#define PA_VMAX 16 /* control type: numeric, callback function: (none) */
#define PA_VSET 17 /* control type: numeric, callback function: (none) */
#define PA_VMON 18 /* control type: numeric, callback function: (none) */
#define PA_ISET 19 /* control type: numeric, callback function: (none) */
#define PA_POLAR 20 /* control type: ring, callback function: (none) */
#define PA_IMON 21 /* control type: numeric, callback function: (none) */
#define PA_CVCC 22 /* control type: LED, callback function: (none) */
#define PA_PRESET 23 /* control type: numeric, callback function: SetPresetCB */
#define PA_HUMIDITY 6 /* control type: numeric, callback function: (none) */
#define PA_TMON 7 /* control type: numeric, callback function: (none) */
#define PA_DEBUG_1 8 /* control type: radioButton, callback function: (none) */
#define PA_LOG 9 /* control type: radioButton, callback function: (none) */
#define PA_GRAPH_VMON 10 /* control type: strip, callback function: (none) */
#define PA_GRAPH_3 11 /* control type: strip, callback function: (none) */
#define PA_GRAPH 12 /* control type: strip, callback function: (none) */
#define PA_FNAME 13 /* control type: string, callback function: (none) */
#define PA_ONOFF 14 /* control type: binary, callback function: SwitchOnOffCB */
#define PA_CHANNEL 15 /* control type: numeric, callback function: SelectChannelCB */
#define PA_IMAX 16 /* control type: numeric, callback function: (none) */
#define PA_VMAX 17 /* control type: numeric, callback function: (none) */
#define PA_VSET 18 /* control type: numeric, callback function: (none) */
#define PA_VMON 19 /* control type: numeric, callback function: (none) */
#define PA_ISET 20 /* control type: numeric, callback function: (none) */
#define PA_POLAR 21 /* control type: ring, callback function: (none) */
#define PA_IMON 22 /* control type: numeric, callback function: (none) */
#define PA_CVCC 23 /* control type: LED, callback function: (none) */
#define PA_PRESET 24 /* control type: numeric, callback function: SetPresetCB */
 
 
/* Control Arrays: */
/cvi/apps/TempCtrl/TempCtrl.uir
Cannot display: file marked as a binary type.
svn:mime-type = application/octet-stream