/*============================================================================= Name: loopHdlr.c ffLoopThread - the main routine of the FF Executive Thread Abs: ffLoop contains the transverse loop thread framework The functions herein MUST BE REENTRANT - no globals allowed - because it is the same code for all. Auth: 28-May-2009, Debbie Rogind Note: FCOM source location: /afs/slac/g/lcls/epics/modules/fcom/devl/src -----------------------------------------------------------------------------*/ #include "copyright_SLAC.h" /* SLAC copyright comments */ /*----------------------------------------------------------------------------- Mod: (newest to oldest) =============================================================================*/ #include "debugPrint.h" #ifdef DEBUG_PRINT int ffLoopFlag = DP_INFO; #include #endif /* c library includes */ #include /* memcpy */ #include /* sin function*/ #include /* rand function*/ /* epics includes */ #include /* freeListFree */ #include /* epicsEventId */ #include /* ff common includes */ #include "ffGlob.h" /* thread globals, NULL, etc MUST BE FIRST INCLUDE*/ #include "ffTypes.h" /* */ #include "msgTypes.h" /* msgmail_ts */ #include "ffMsg.h" /* ffIOC internal msg function codes */ /* ff utility prototypes */ #include "msgQUtil.h" /* message queue utilities */ #include "errlog.h" /* errLogPrintf */ #include "ffUtil.h" /* ffGetThreadEnumByTid */ #include "devFbckData.h" /* findDevLoopIndex */ /* thread prototypes */ #include "loopHdlr.h" /* ffLoop threads */ #include "loopConfig.h" /* loopConfigInitialize */ #include "cadef.h" /* channel access */ #include "loopCACTypes.h" /* CAC variables and structures */ #include "loopCAC.h" /* CAC variables and structures */ #include "loopDev.h" /* real meas and act device names */ /* written in loopConfig */ #include "functionTypes.h" /* extern for functionFreeList_p */ #include "loopFcom.h" /* fcom functions */ /***************************************************************************/ /* global vars */ /***************************************************************************/ /* declare global vars to every loop here */ /* global - declared extern in loopDev.h */ loopDev_ts loop_as[MAX_LOOPS]; /* global - declared extern in loopCACTypes.h */ loopCAC_ts loopCAC_as[MAX_LOOPS]; /* internal function prototypes */ LOCAL void cleanup(ffThread_ts* ffLoop_ps); LOCAL int reConfigure (ffThread_te ffThread_e); LOCAL void debugActs(ffThread_te ffThread_e); LOCAL void updatePVs(ffThread_te ffThread_e); /*============================================================================= Name: ffLoopThread Abs: Start transverse loop thread framework. Args: none Rem: Side: Ret: none =============================================================================*/ void ffLoopThread(void) { ffThread_te ffThread_e; epicsBoolean go = epicsTrue; epicsBoolean previous_on = epicsFalse; int loopNum, i; ffThread_ts* ffLoop_ps; int status = OKOK; /* get enum for this thread to get at thread structure */ ffThread_e = ffGetThreadEnumByTid(NULL); ffLoop_ps = &ffThreads_as[ffThread_e]; loopNum = ffThread_e - LOOP_ENUM_OFFSET; /* create the wakeupEvent_ps for being woken up */ ffLoop_ps->wakeupFromTimerEvent_ps = epicsEventMustCreate(epicsEventEmpty); DEBUGPRINT(DP_INFO,ffLoopFlag, ("loopHdlr: loop %d has ffLoop_ps = %lu and wakeupFromTimerEvent_ps = %lu\n", loopNum, (unsigned long int) ffLoop_ps, (unsigned long int) ffLoop_ps->wakeupFromTimerEvent_ps)); DEBUGPRINT(DP_INFO, ffLoopFlag, ("ffExec: TimerDelay = %ld\n", ffDelay_in_usec)); /* copy loopNum into thread structure */ ffThreads_as[ffThread_e].loopNum = loopNum; /* *************************************************************************************/ /* loopConfigInitialize and loopCACInit are only called once upon bootup init */ /* init the loop DBADDRs for configs, meas, act, states - */ /* these PV addrs won't change except on reboot */ /* stored in global: loopConfigAddr_ts loopDBA_as[MAX_LOOPS]; */ /* cannot place loopDBA_as with other globals in loopHdlr due to cadef.h */ /* init CA Context and set all chid = NULL */ if ((status = loopConfigInitialize(ffThread_e)) != OKOK) { DEBUGPRINT(DP_FATAL,ffLoopFlag, ("loopHdlr: loop %d cannot init configuration\n", loopNum)); cleanup(ffLoop_ps); return; } /* init CA Context and set all chid = NULL */ if ((status = loopCACInit (ffThread_e)) != OKOK) { DEBUGPRINT(DP_FATAL,ffLoopFlag, ("loopHdlr: loop %d cannot init CAC\n", loopNum)); cleanup(ffLoop_ps); return; } /* the following is called after reading the loop configuration */ if ((status = reConfigure(ffThread_e)) != OKOK) { DEBUGPRINT(DP_FATAL,ffLoopFlag, ("loopHdlr: loop %d cannot reconfigure\n", loopNum)); cleanup(ffLoop_ps); return; } #ifdef __rtems__ /* if rtems init this loop's fcom ids - put them in loopDev */ /* also subscribes to meas data */ status = loopFcomInit(ffThread_e); #endif /* put this thread's POIs into the timer event list */ loopConfigInitPOIEvent(ffThread_e); /* set this thread to active */ ffLoop_ps->active = epicsTrue; /* init loop diagnostics times */ memset(&loopRT_as[loopNum].timeFromFiducial, 0, sizeof(struct timespec)); memset(&loopRT_as[loopNum].timeLoopProcess, 0, sizeof(struct timespec)); /* initialize the loop counter */ loopRT_as[loopNum].count = 1; /* initialize the data counts */ loopRT_as[loopNum].meas.numgood = 0; loopRT_as[loopNum].meas.num =0; loopRT_as[loopNum].meas.numused =0; loopRT_as[loopNum].act.num =0; loopRT_as[loopNum].act.numused =0; loopRT_as[loopNum].state.num =0; loopRT_as[loopNum].state.numused =0; memset(&(loopRT_as[loopNum].meas.diag_s), 0, sizeof(diag_ts)); memset(&(loopRT_as[loopNum].act.diag_s), 0, sizeof(diag_ts)); memset(&(loopRT_as[loopNum].state.diag_s), 0, sizeof(diag_ts)); DEBUGPRINT(DP_INFO,ffLoopFlag, ("loopHdlr: loop %d is active now\n", loopNum)); /* now we wait at our wakeup event - ffThreads_as[cnt].wakeupEvent_ps */ go = epicsTrue; /* go is set to false only when ffLoop must exit */ while (go) { /* wait on event flag */ if (ffLoop_ps->wakeupFromTimerEvent_ps) { /* DEBUGPRINT(DP_INFO,ffLoopFlag, ("loopHdlr: got event object, waiting for event ...\n") );*/ epicsEventMustWait(ffLoop_ps->wakeupFromTimerEvent_ps); /* DEBUGPRINT(DP_INFO,ffLoopFlag, ("loopHdlr:got a pulse - iterate now ...\n") );*/ /* if the feedback loop is ON (state=1)*/ if (loopRT_as[loopNum].loopState) { /* if this is an OFF -> ON transition then see if a reconfigure is req'd */ if (previous_on == epicsFalse) { /* DEBUGPRINT(DP_INFO,ffLoopFlag, ("loopHdlr: OFF - > ON transition ...\n") );*/ /* just turned on - read current config in case there were changes */ loopConfigOnConfig(ffThread_e); /* now reconfigure if necessary */ loop_as[loopNum].reConfigure=epicsTrue; reConfigure(ffThread_e); } /* get the data for this loop */ #ifdef __rtems__ if (loop_as[loopNum].network == NETWORK_FCOM) { loopFcomGetBlobs(ffThread_e); /*this is for debugging ffEpicsSend thread loopFcomDebugBlobs(ffThread_e);*/ /* call loop iterate function to process loop loopConfigCallIterFunc(ffThread_e);*/ /* call debug function to create actuator data */ debugActs(ffThread_e); /* send actuator command and states on FCOM */ loopFcomSendBlobs(ffThread_e); updatePVs(ffThread_e); /*if ((loopRT_as[loopNum].count % 30) ==0) { loopFcomShowBlobs(ffThread_e); loopFcomShowDiags(loopNum); } */ /* release blobs as a group here, after test/showblobs*/ loopFcomReleaseBlobs(ffThread_e); } else #endif loopCACGetBSAMeasData (ffThread_e); /* call loop iterate function loopConfigCallIterFunc(ffThread_e);*/ /* increment the loop counter */ loopRT_as[loopNum].count++; if (loopRT_as[loopNum].count>10000000) { loopRT_as[loopNum].count=1; memset(&loopRT_as[loopNum].timeFromFiducial, 0, sizeof(struct timespec)); memset(&loopRT_as[loopNum].timeLoopProcess, 0, sizeof(struct timespec)); for (i=0; itid_ps = NO_THREAD; /*epicsEventDestroy(ffThreads_as[ffLoop0 + loopNum].wakeupFromTimerEvent_ps); */ epicsEventDestroy( ffLoop_ps->wakeupFromTimerEvent_ps); ffLoop_ps->wakeupFromTimerEvent_ps = NULL; /* clear out thread name */ memset (ffLoop_ps->loopname_ca, 0, MAX_THREADNAME_SIZE); /* clear loopNum in thread structure */ ffLoop_ps->loopNum = -1; } /*============================================================================= Name: reConfigure Abs: Args: ffThread_e index into thread array Rem: reconfigure the loop, re-read all dbAddrs that can change when the loop is reconfigured. then go through all monitored channels and assign those not assigned previously. Side: Ret: int status - OKOK or error code =============================================================================*/ LOCAL int reConfigure (ffThread_te ffThread_e) { int i; int loopNum = ffThreads_as[ffThread_e].loopNum; int status = OKOK; /* see if we need to reconfigure the loop */ /* call loop reconfigure function loopConfigCallReconfigFunc(ffThread_e);*/ if ((status = loopConfigReConfig (ffThread_e)) != NOT_OK) { status = loopCACReConfig (ffThread_e); } /* initialize the loop counter */ loopRT_as[loopNum].count = 1; memset(&loopRT_as[loopNum].timeFromFiducial, 0, sizeof(struct timespec)); memset(&loopRT_as[loopNum].timeLoopProcess, 0, sizeof(struct timespec)); /* initialize the data and diagnostics counts */ for (i=0; iloopUnitName_ca ); } /*============================================================================= Name: debugActs Abs: debug routine to put dummy data into Actuator commands Args: ffThread_e index into the global thread structure for this loop. Rem: Side: Ret: none =============================================================================*/ void debugActs(ffThread_te ffThread_e) { int i; int loop = ffThreads_as[ffThread_e].loopNum; loopDev_ts *loop_ps = &loop_as[loop]; double val = 0.0200; for (i=0;iactNum; i++) { if (loop_ps->actDev_as[i].used) { loopRT_as[loop].act.numused++; loopRT_as[loop].act.used_au[i]=1; if (i==0) loopRT_as[loop].act.val_ad[i] = val; else if (i==1) loopRT_as[loop].act.val_ad[i] = val+0.00500; else if (i==2) loopRT_as[loop].act.val_ad[i] = val+0.0100; else if (i==3) loopRT_as[loop].act.val_ad[i] = val+0.01500; else loopRT_as[loop].act.val_ad[i] = val+0.01500; /* loopRT_as[loop].act.val_ad[i] = ((sin((3.1416/180)*(loopRT_as[loop].act.diag_s.iterations+10*i))-0.5)*0.05); sine between -0.05&0.05 */ } else loopRT_as[loop].act.used_au[i]=0; } loopRT_as[loop].act.num = loop_ps->actNum; }