Files
sics/site_ansto/west4100.c
Ferdi Franceschini 0749b0effa Merging release 2.0 branch with CVS trunk
r2601 | ffr | 2008-05-30 10:26:57 +1000 (Fri, 30 May 2008) | 2 lines
2012-11-15 13:38:17 +11:00

357 lines
11 KiB
C

/*---------------------------------------------------------------------------
W E S T 4 1 0 0 . C
This is the implementation for a WEST4100 object derived from a more general
environment controller.
Mark Koennecke, August 1997
Mark Lesha, January 2006 (based on ITC4 code)
Paul Barron, January 2008 (Note: This is based on the old LAKESHORE340 code and
not the new LS340 code written by Rodney Davies Feb 08)
Copyright:
Labor fuer Neutronenstreuung
Paul Scherrer Institut
CH-5423 Villigen-PSI
The authors hereby grant permission to use, copy, modify, distribute,
and license this software and its documentation for any purpose, provided
that existing copyright notices are retained in all copies and that this
notice is included verbatim in any distributions. No written agreement,
license, or royalty fee is required for any of the authorized uses.
Modifications to this software may be copyrighted by their authors
and need not follow the licensing terms described here, provided that
the new terms are clearly indicated on the first page of each file where
they apply.
IN NO EVENT SHALL THE AUTHORS OR DISTRIBUTORS BE LIABLE TO ANY PARTY
FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
ARISING OUT OF THE USE OF THIS SOFTWARE, ITS DOCUMENTATION, OR ANY
DERIVATIVES THEREOF, EVEN IF THE AUTHORS HAVE BEEN ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
THE AUTHORS AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT. THIS SOFTWARE
IS PROVIDED ON AN "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE
NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR
MODIFICATIONS.
----------------------------------------------------------------------------*/
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <tcl.h>
#include <fortify.h>
#include <sics.h>
#include <splitter.h>
#include <obpar.h>
#include <devexec.h>
#include <nserver.h>
#include <interrupt.h>
#include <emon.h>
#include <evcontroller.h>
#include <evcontroller.i>
#include "west4100.h"
int SetSetpointWEST4100(pEVDriver self, int Setpoint);
int SetAlarm1WEST4100(pEVDriver self, int Alarm1);
int SetAlarm2WEST4100(pEVDriver self, int Alarm2);
int SetPowerLimitWEST4100(pEVDriver self, int PowerLimit);
int SetRampRateWEST4100(pEVDriver self, int RampRate);
int GetSetpointWEST4100(pEVDriver self);
int GetAlarm1WEST4100(pEVDriver self);
int GetAlarm2WEST4100(pEVDriver self);
int GetPowerLimitWEST4100(pEVDriver self);
int GetRampRateWEST4100(pEVDriver self);
int GetProcessValueWEST4100(pEVDriver self);
int GetAddressWEST4100(pEVDriver self);
int GetTransactWEST4100(pEVDriver self);
int GetWorkingSetpointWEST4100(pEVDriver self);
/*---------------------------------------------------------------------------*/
int WEST4100SetPar(pEVControl self, char *name, float fNew, SConnection *pCon)
{
int iRet;
/* check authorisation */
if(!SCMatchRights(pCon,usUser))
{
SCWrite(pCon,"ERROR: you are not authorised to change this parameter",
eError);
return 0;
}
/* just catch those three names which we understand */
if(strcmp(name,"setpoint") == 0)
{
iRet = SetSetpointWEST4100(self->pDriv,(int)fNew);
if(!iRet)
{
SCWrite(pCon,"ERROR: value out of range",eError);
return 0;
}
iRet = ConfigWEST4100(self->pDriv);
if(iRet != 1)
{
SCWrite(pCon,"ERROR: WEST4100 configuration failed! ",eError);
SCWrite(pCon,"INFO: Probably comm problem, Retry!",eError);
return 0;
}
SCSendOK(pCon);
return 1;
}
if(strcmp(name,"alarm1") == 0)
{
iRet = SetAlarm1WEST4100(self->pDriv,(int)fNew);
if(!iRet)
{
SCWrite(pCon,"ERROR: value out of range",eError);
return 0;
}
iRet = ConfigWEST4100(self->pDriv);
if(iRet != 1)
{
SCWrite(pCon,"ERROR: WEST4100 configuration failed! ",eError);
SCWrite(pCon,"INFO: Probably comm problem, Retry!",eError);
return 0;
}
SCSendOK(pCon);
return 1;
}
if(strcmp(name,"alarm2") == 0)
{
iRet = SetAlarm2WEST4100(self->pDriv,(int)fNew);
if(!iRet)
{
SCWrite(pCon,"ERROR: value out of range",eError);
return 0;
}
iRet = ConfigWEST4100(self->pDriv);
if(iRet != 1)
{
SCWrite(pCon,"ERROR: WEST4100 configuration failed! ",eError);
SCWrite(pCon,"INFO: Probably comm problem, Retry!",eError);
return 0;
}
SCSendOK(pCon);
return 1;
}
if(strcmp(name,"powerlimit") == 0)
{
iRet = SetPowerLimitWEST4100(self->pDriv,(int)fNew);
if(!iRet)
{
SCWrite(pCon,"ERROR: value out of range",eError);
return 0;
}
iRet = ConfigWEST4100(self->pDriv);
if(iRet != 1)
{
SCWrite(pCon,"ERROR: WEST4100 configuration failed! ",eError);
SCWrite(pCon,"INFO: Probably comm problem, Retry!",eError);
return 0;
}
SCSendOK(pCon);
return 1;
}
if(strcmp(name,"ramprate") == 0)
{
iRet = SetRampRateWEST4100(self->pDriv,(int)fNew);
if(!iRet)
{
SCWrite(pCon,"ERROR: value out of range",eError);
return 0;
}
iRet = ConfigWEST4100(self->pDriv);
if(iRet != 1)
{
SCWrite(pCon,"ERROR: WEST4100 configuration failed! ",eError);
SCWrite(pCon,"INFO: Probably comm problem, Retry!",eError);
return 0;
}
SCSendOK(pCon);
return 1;
}
else
return EVCSetPar(self,name,fNew,pCon);
}
/*--------------------------------------------------------------------------*/
int WEST4100GetPar(pEVControl self, char *name, float *fNew)
{
int iRet;
/* just catch those two names which we understand */
if(strcmp(name,"setpoint") == 0)
{
iRet = GetSetpointWEST4100(self->pDriv);
*fNew = (float)iRet;
return 1;
}
else if(strcmp(name,"alarm1") == 0)
{
iRet = GetAlarm1WEST4100(self->pDriv);
*fNew = (float)iRet;
return 1;
}
else if(strcmp(name,"alarm2") == 0)
{
iRet = GetAlarm2WEST4100(self->pDriv);
*fNew = (float)iRet;
return 1;
}
else if(strcmp(name,"powerlimit") == 0)
{
iRet = GetPowerLimitWEST4100(self->pDriv);
*fNew = (float)iRet;
return 1;
}
else if(strcmp(name,"ramprate") == 0)
{
iRet = GetRampRateWEST4100(self->pDriv);
*fNew = (float)iRet;
return 1;
}
else if(strcmp(name,"numsensors") == 0)
{
iRet = 1; // Furnace only has 1 sensor
*fNew = (int)iRet;
return 1;
}
else if(strcmp(name,"sensora") == 0)
{
iRet = GetProcessValueWEST4100(self->pDriv); // Furnace only has 1 sensor
*fNew = (float)iRet;
return 1;
}
else
return EVCGetPar(self,name,fNew);
}
/*---------------------------------------------------------------------------*/
int WEST4100List(pEVControl self, SConnection *pCon)
{
char pBueffel[132];
int iRet;
iRet = EVCList(self,pCon);
sprintf(pBueffel,"%s.NumSensors = %d\n",self->pName,1);
SCWrite(pCon,pBueffel,eValue);
sprintf(pBueffel,"%s.ControlSensor = %s\n",self->pName,"SensorA");
SCWrite(pCon,pBueffel,eValue);
sprintf(pBueffel,"%s.ModbusAddress = %d\n",self->pName,GetAddressWEST4100(self->pDriv));
SCWrite(pCon,pBueffel,eValue);
sprintf(pBueffel,"%s.TransactionNumber = %d\n",self->pName,GetTransactWEST4100(self->pDriv));
SCWrite(pCon,pBueffel,eValue);
sprintf(pBueffel,"%s.ProcessValue = %d\n",self->pName,GetProcessValueWEST4100(self->pDriv));
SCWrite(pCon,pBueffel,eValue);
sprintf(pBueffel,"%s.Setpoint = %d\n",self->pName,GetSetpointWEST4100(self->pDriv));
SCWrite(pCon,pBueffel,eValue);
sprintf(pBueffel,"%s.WorkingSetpoint = %d\n",self->pName,GetWorkingSetpointWEST4100(self->pDriv));
SCWrite(pCon,pBueffel,eValue);
sprintf(pBueffel,"%s.Alarm1 = %d\n",self->pName,GetAlarm1WEST4100(self->pDriv));
SCWrite(pCon,pBueffel,eValue);
sprintf(pBueffel,"%s.Alarm2 = %d\n",self->pName,GetAlarm2WEST4100(self->pDriv));
SCWrite(pCon,pBueffel,eValue);
sprintf(pBueffel,"%s.PowerLimit = %d\n",self->pName,GetPowerLimitWEST4100(self->pDriv));
SCWrite(pCon,pBueffel,eValue);
sprintf(pBueffel,"%s.RampRate = %d\n",self->pName,GetRampRateWEST4100(self->pDriv));
SCWrite(pCon,pBueffel,eValue);
return iRet;
}
/*-------------------------------------------------------------------------*/
int WEST4100Wrapper(SConnection *pCon, SicsInterp *pSics, void *pData,
int argc, char *argv[])
{
pEVControl self = NULL;
char pBueffel[256];
int iRet;
double fNum;
float fVal;
int fValue;
self = (pEVControl)pData;
assert(self);
assert(pCon);
assert(pSics);
if(argc < 2)
{
return EVControlWrapper(pCon,pSics,pData,argc,argv);
}
// Set or Get
strtolower(argv[1]);
if( (strcmp(argv[1],"controlsensor") == 0) || (strcmp(argv[1],"sensorlist") == 0)){
sprintf(pBueffel,"%s.%s = %s\n",self->pName,argv[1],"sensorA");
SCWrite(pCon,pBueffel,eValue);
}
else if((strcmp(argv[1],"setpoint") == 0) || (strcmp(argv[1],"alarm1") == 0)
|| (strcmp(argv[1],"powerlimit") == 0) || (strcmp(argv[1],"alarm2") == 0)
|| (strcmp(argv[1],"ramprate") == 0) || (strcmp(argv[1],"numsensors") == 0)
|| (strcmp(argv[1],"sensora") == 0))
{
if(argc > 2) /* set case */
{
iRet = Tcl_GetDouble(pSics->pTcl,argv[2],&fNum);
if(iRet != TCL_OK)
{
sprintf(pBueffel,"ERROR: expected number, got %s",argv[2]);
SCWrite(pCon,pBueffel,eError);
return 0;
}
return WEST4100SetPar(self,argv[1],(float)fNum,pCon);
}
else /* get case */
{
iRet = WEST4100GetPar(self,argv[1],&fVal);
sprintf(pBueffel,"%s.%s = %f\n",self->pName,argv[1],fVal);
SCWrite(pCon,pBueffel,eValue);
return 1;
}
}
// Write or Query
else if(strcmp(argv[1],"parameter") == 0)
{
if(argc > 3) /* set case */
{
return WEST4100Write(self->pDriv,atoi(argv[2]),atoi(argv[3]));
}
else /* get case */
{
iRet = WEST4100Query(self->pDriv,atoi(argv[2]),&fValue);
sprintf(pBueffel,"%s.%s %s = %d\n",self->pName,argv[1],argv[2],fValue);
SCWrite(pCon,pBueffel,eValue);
return 1;
}
}
// List
else if(strcmp(argv[1],"list") == 0)
{
return WEST4100List(self,pCon);
}
else
{
return EVControlWrapper(pCon,pSics,pData,argc,argv);
}
/* not reached */
return 0;
}