Changeset 161 for hvcontrol


Ignore:
Timestamp:
02/05/10 09:01:28 (15 years ago)
Author:
ogrimm
Message:
Removed local configuration, now dependent on Evidence configuration server
Location:
hvcontrol
Files:
2 deleted
9 edited

Legend:

Unmodified
Added
Removed
  • hvcontrol/History.txt

    r126 r161  
    262624/9/2009   Program is terminated if too many errors are encountered by the
    2727            monitor thread
    28 8/12/2009   Implemented DIM servers (currently only needs DIM_DNS_NODE
    29             environment variable)
     288/12/2009   Implemented DIM servers
     2915/12/2009  Removed local log file and implemented logging via DColl
     3017/12/2009      Implemented DIM command handling and 'Textout' DIM service
     318/1/2010        Removed locale slow data writing, now handled by Evidence
     3213/1/2010       Only important messages are written to log file
     3320/1/2010       Removed local configuration, now dependent on Evidence configuration
     34                        server
     3529/1/2010       DAC value 0 is now equivilant to calibrated voltage value 0
  • hvcontrol/Makefile

    r135 r161  
    33#
    44
    5 SOURCES = hvcontrol.cpp src/HV.cc src/HVConfig.cc src/HVCalib.cc src/ProcessIO.cc ../pixelmap/Pixel.cc ../pixelmap/PixelMap.cc ../drsdaq/SlowData.cc ../Evidence/Evidence.cc
     5SOURCES = hvcontrol.cpp src/HV.cc src/HVCalib.cc src/ProcessIO.cc ../pixelmap/Pixel.cc ../pixelmap/PixelMap.cc ../Evidence/Evidence.cc
    66OBJECTS = $(addsuffix .o, $(basename $(SOURCES)))
    7 INCDIRS   = -I. -I./src -I../Evidence/DIM/
     7INCDIRS   = -I. -I../Evidence -I./src -I$(DIMDIR)/dim
    88
    9 CPPFLAGS = -O3 -Wall -DOS_LINUX $(INCDIRS)
    10 LDLIBS = -lstdc++ -lpthread -lfl -lreadline -ltermcap
     9CPPFLAGS = -O3 -Wall $(INCDIRS)
     10LDLIBS = -lstdc++ -lpthread -lfl -lreadline -ltermcap $(DIMDIR)/linux/libdim.a
    1111
    12 hvcontrol: $(OBJECTS) ../Evidence/DIM/libdim.a
     12hvcontrol: $(OBJECTS)
    1313
    1414clean:
  • hvcontrol/hvcontrol.cpp

    r126 r161  
    9393  signal(SIGQUIT, &CrashHandler);  // CTRL-Backspace
    9494  signal(SIGINT, &CrashHandler);   // CTRL-C
    95   signal(SIGHUP, &CrashHandler);  // CTRL-Backspace
     95  signal(SIGHUP, &CrashHandler);   // Terminal closed
    9696  signal(SIGTERM, &CrashHandler);
    9797
     
    170170
    171171    // Log command
    172     m->PrintMessage(MsgToLog, "CONSOLE> %s\n", Command);
     172    m->PrintMessage(Log, "CONSOLE> %s\n", Command);
    173173
    174174    // Process command     
     
    208208  // Set up server socket
    209209  if ((ServerSocket = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
    210     m->PrintMessage("Could not open server socket, no remote connection possible (%s).\n", strerror(errno));
     210    m->PrintMessage(All, "Could not open server socket, no remote connection possible (%s).\n", strerror(errno));
    211211    return;
    212212  }
     
    215215  int Value=1;
    216216  if (setsockopt(ServerSocket, SOL_SOCKET, SO_REUSEADDR, (char *) &Value, sizeof (Value)) == -1) {
    217     m->PrintMessage("Warning: Could not set server socket option SO_REUSEADDR (%s)\n", strerror(errno));
     217    m->PrintMessage(All, "Warning: Could not set server socket option SO_REUSEADDR (%s)\n", strerror(errno));
    218218  }
    219219
    220220  SocketAddress.sin_family = PF_INET;
    221   SocketAddress.sin_port = htons((unsigned short) m->config->fCCPort);
     221  SocketAddress.sin_port = htons((unsigned short) m->fCCPort);
    222222  SocketAddress.sin_addr.s_addr = INADDR_ANY;
    223223
    224224  if (bind(ServerSocket, (struct sockaddr *) &SocketAddress, sizeof(SocketAddress)) == -1) {
    225     m->PrintMessage("Could not bind port to socket (%s)\n", strerror(errno));
     225    m->PrintMessage(All, "Could not bind port to socket (%s)\n", strerror(errno));
    226226    close(ServerSocket);
    227227    return;
    228228  }
    229229  if (listen(ServerSocket, 0) == -1) {
    230     m->PrintMessage("Could not set socket to listening (%s)\n", strerror(errno));
     230    m->PrintMessage(All, "Could not set socket to listening (%s)\n", strerror(errno));
    231231    close(ServerSocket);
    232232    return;
     
    236236
    237237    if ((ConnectionSocket = accept(ServerSocket, (struct sockaddr *) &ClientAddress, &SizeClientAddress)) == -1) {
    238       if (errno!=EINTR) m->PrintMessage("Failed to accept incoming connection (%s)\n", strerror(errno));
     238      if (errno!=EINTR) m->PrintMessage(All, "Failed to accept incoming connection (%s)\n", strerror(errno));
    239239      close(ServerSocket);
    240240      return;
     
    242242
    243243    ClientName = gethostbyaddr((char *) &ClientAddress.sin_addr ,sizeof(struct sockaddr_in),AF_INET);
    244     m->PrintMessage("Connected to client at %s (%s).\n", inet_ntoa(ClientAddress.sin_addr), ClientName!=NULL ? ClientName->h_name:"name unknown");
     244    m->PrintMessage(All, "Connected to client at %s (%s).\n", inet_ntoa(ClientAddress.sin_addr), ClientName!=NULL ? ClientName->h_name:"name unknown");
    245245    m->Socket = ConnectionSocket;
    246246
     
    253253      if (ReadResult==0) break; // Client does not exist anymore
    254254      if (ReadResult==-1) {
    255         if (errno!=EINTR) m->PrintMessage("Error read from socket (%s)\n", strerror(errno));
    256         break;
     255                if (errno!=EINTR) m->PrintMessage(All, "Error read from socket (%s)\n", strerror(errno));
     256                break;
    257257      }
    258258      if (Command[strlen(Command)-1]=='\n') Command[strlen(Command)-1]='\0';  // Remove trailing newline
    259259     
    260260      // Log command
    261       m->PrintMessage(MsgToConsole|MsgToLog, "SOCKET> %s\n", Command);
     261      //m->PrintMessage(Console, "SOCKET> %s\n", Command);
     262      //m->PrintMessage(Log, "SOCKET> %s\n", Command);
    262263           
    263264      // Process command
     
    270271
    271272    m->Socket = -1;
    272     m->PrintMessage("Disconnected from client.\n");
     273    m->PrintMessage(All, "Disconnected from client.\n");
    273274    close(ConnectionSocket);
    274275  }
  • hvcontrol/src/HV.cc

    r126 r161  
    2222  m = PIO;
    2323
    24   SetTimeOut(m->config->fTimeOut);
     24  SetTimeOut(m->fTimeOut);
    2525  BoardNumber = DeviceNumber;
    2626  BoardName = DeviceName;
     
    3434      snprintf(Buffer, sizeof(Buffer), SERVER_NAME"/VOLT/ID%.2d/%.2d-%.3d", BoardNumber, i, j);
    3535      BiasVolt[i][j] = new DimService (Buffer, HVV[i][j]);
    36       snprintf(Buffer, sizeof(Buffer), SERVER_NAME"/DAC/ID%.2d/%.2d-%.3d", BoardNumber, i, j);
    37       BiasDAC[i][j] = new DimService (Buffer, HV[i][j]);
    3836    }
    3937    Overcurrent[i] = false;
     
    4341  LastWrapCount = -1;
    4442  ErrorCount = 0;
    45  
     43
    4644  ClearVoltageArrays();
    4745
     
    4947  snprintf(Buffer, BUFFER_LENGTH, "/dev/%s",DeviceName);
    5048  if ((fDescriptor = open(Buffer, O_RDWR|O_NOCTTY|O_NDELAY)) == -1) {
    51     if(errno != 2) m->PrintMessage("Error: Could not open device %d/%s (%s)\n", DeviceNumber,DeviceName, strerror(errno));
     49    if(errno != 2) m->PrintMessage(All, "Error: Could not open device %d/%s (%s)\n", DeviceNumber,DeviceName, strerror(errno));
    5250    return;
    5351  }
     
    5553  // Get current serial port settings
    5654  if (tcgetattr(fDescriptor, &tio) == -1) {
    57     m->PrintMessage("Error: tcgetattr() failed (%d/%s)\n", errno, strerror(errno));
     55    m->PrintMessage(All, "Error: tcgetattr() failed (%d/%s)\n", errno, strerror(errno));
    5856    return;   
    5957  }
    6058
    6159  // Set baudrate and raw mode
    62   if (cfsetspeed(&tio, BAUDRATE) == -1) m->PrintMessage("Error: Could not set baud rate (%s)\n", strerror(errno));
     60  if (cfsetspeed(&tio, BAUDRATE) == -1) m->PrintMessage(All, "Error: Could not set baud rate (%s)\n", strerror(errno));
    6361  cfmakeraw(&tio);
    64   if (tcsetattr(fDescriptor, TCSANOW, &tio ) == -1) m->PrintMessage("Error: tcsetattr() failed (%s)\n", strerror(errno));
     62  if (tcsetattr(fDescriptor, TCSANOW, &tio ) == -1) m->PrintMessage(All, "Error: tcsetattr() failed (%s)\n", strerror(errno));
    6563
    6664  //  Synchronize HV board (if fails, closes device and sets fDescriptor to -2)
     
    9290    for (int j=0; j<NUM_CHANNELS; j++) {
    9391      delete BiasVolt[i][j];
    94       delete BiasDAC[i][j];
    9592    }
    9693  }
     
    109106  // === Write data ===
    110107  if ((ret=write(fDescriptor, wbuf, Bytes)) < Bytes) {
    111     if (ret == -1) m->PrintMessage("Could not write data to HV board (%s)\n", strerror(errno));
    112     else m->PrintMessage("Could write only %d of %d bytes to HV board\n", ret, Bytes);
     108    if (ret == -1) m->PrintMessage(All, "Could not write data to HV board (%s)\n", strerror(errno));
     109    else m->PrintMessage(All, "Could write only %d of %d bytes to HV board\n", ret, Bytes);
    113110    ErrorCount++;
    114111    return 0;
    115   }
    116   if (m->Verbose) {
    117     m->PrintMessage("%d bytes written:\n", Bytes);
    118     for (int i=0; i<Bytes; i++) m->PrintMessage(" Byte %d: %#.2x\n",i,wbuf[i]);
    119112  }
    120113
     
    123116  struct timeval WaitTime = {(long) fTimeOut, (long) ((fTimeOut-(long) fTimeOut)*1e6)};
    124117  if (select(fDescriptor+1, &SelectDescriptor, NULL, NULL, &WaitTime)==-1) {
    125     m->PrintMessage("Error with select() (%s)\n", strerror(errno));
     118    m->PrintMessage(All, "Error with select() (%s)\n", strerror(errno));
    126119    return 0;
    127120  }
    128121  // Time-out expired?
    129   if (!FD_ISSET(fDescriptor, &SelectDescriptor)) {
    130     if (m->Verbose) m->PrintMessage("Time-out of %.2f seconds expired while reading\n", fTimeOut);
    131     return -1;
    132   }
     122  if (!FD_ISSET(fDescriptor, &SelectDescriptor)) return -1;
     123
    133124  // Read error?   
    134125  if ((ret = read(fDescriptor, &rbuf, 1)) == -1) {
    135     m->PrintMessage("Read error (%s)\n", strerror(errno));
     126    m->PrintMessage(All, "Read error (%s)\n", strerror(errno));
    136127    ErrorCount++;
    137128    return 0;
     
    147138  ResetButton = (bool) (rbuf & 0X80);
    148139
    149   if (m->Verbose && ret==1) m->PrintMessage(" 1 byte read: %#.2x\n", rbuf); 
    150  
    151140  return 1;
    152141}
     
    217206      // Update DIM services
    218207      BiasVolt[j][k]->updateService();
    219       BiasDAC[j][k]->updateService();
    220208    }
    221209  }
  • hvcontrol/src/HV.h

    r126 r161  
    99#include <sys/ioctl.h>
    1010
    11 #include "HVConfig.h"
    1211#include "dis.hxx"
     12#define NUM_CHAINS 4
     13#define NUM_CHANNELS 32
    1314
    1415#define BAUDRATE B115200
     
    5556   DimService *Name;
    5657   DimService *BiasVolt[NUM_CHAINS][NUM_CHANNELS];
    57    DimService *BiasDAC[NUM_CHAINS][NUM_CHANNELS];
    5858
    5959   void ClearVoltageArrays();
  • hvcontrol/src/HVCalib.cc

    r100 r161  
    1919#include "HVCalib.h"
    2020
     21#include "ProcessIO.h" // Must be not in HVCalib.h to avoid problem with declaring class ProcessIO
     22
    2123using namespace std;
    2224
    2325
    24 HVCalib::HVCalib(HVConfig* hvConfig) {
     26HVCalib::HVCalib(class ProcessIO *hvConfig) {
    2527
    2628  char calibfile[80], dLine[6000];
     
    3133
    3234  Config = hvConfig;
    33  
     35
    3436  iDACMin = Config->DACMin;
    3537  fHVCalibSlope = Config->fHVCalibSlope;
     
    5456  for(int i=0; i<Config->NumHVBoards; i++){
    5557    for(int j=0; j<NUM_CHAINS; j++){
    56       sprintf(calibfile,"Calib/%s_%c%d.txt",hvConfig->fUSBDevice[i],65+j,1);
     58      sprintf(calibfile,"Calib/%s_%c%d.txt",hvConfig->fHVBoard[i]->BoardName,65+j,1);
    5759      ifstream fin(calibfile);
    5860
     
    143145
    144146double HVCalib::DACToHV(int dac, int board, int chain, int channel) {
     147 
     148  if (dac == 0) return 0;
     149 
    145150  if (dac < iDACMin){
    146151    return HVArray[board][chain][channel][0] + (dac - iDACMin)*fHVCalibSlope;
     
    153158
    154159int HVCalib::HVToDAC(double hv, int board, int chain, int channel) {
     160
     161  if (hv == 0) return 0;
    155162  if (hv < HVArray[board][chain][channel][0]){
    156163        return iDACMin + (int)((hv - HVArray[board][chain][channel][0])/fHVCalibSlope);
  • hvcontrol/src/HVCalib.h

    r100 r161  
    44#include <string.h>
    55
    6 #include "HVConfig.h"
    7 
     6//#include "HVConfig.h"
     7class ProcessIO;
    88
    99class HVCalib {
     
    1919  double fHVCalibSlope;
    2020
    21   HVConfig *Config;
     21  class ProcessIO *Config;
    2222 
    2323 public:
    2424
    25   HVCalib(HVConfig *);
     25  HVCalib(class ProcessIO *);
    2626  ~HVCalib();
    2727
  • hvcontrol/src/ProcessIO.cc

    r126 r161  
    1515
    1616
    17 ProcessIO::ProcessIO(const char *ConfigFile): EvidenceServer(SERVER_NAME) {
     17ProcessIO::ProcessIO(const char *ConfigFile):
     18            DimCommand((char *) SERVER_NAME"/Command", (char *) "C"),
     19            EvidenceServer(SERVER_NAME) {
    1820
    1921  // Get program start time
    2022  time (&StartTime);
    2123
    22   // Create instances
    23   config = new HVConfig(ConfigFile);
    24   calib  = new HVCalib(config);
    25   pm     = new PixelMap(config->fPixMapTable);
     24  // Create DIM text output service
     25  Textout = new DimService (SERVER_NAME"/Textout", (char *) "");
    2626
    2727  // Initialize status variables
    2828  state     = active;
    2929  Exit      = false;
    30   Verbose   = false;
    3130  CmdFromSocket = false;
    3231  Socket    = -1;
     
    3736  FirstChain  = 0;
    3837  LastChain   = NUM_CHAINS-1;
    39  
    40   if (config->fStatusRefreshRate >= MIN_RATE && config->fStatusRefreshRate <= MAX_RATE)
    41     fStatusRefreshRate = config->fStatusRefreshRate;
    42   else fStatusRefreshRate = 1.;
    43    
     38
     39  // Get configuration data
     40  char *Boards = GetConfig(SERVER_NAME " Boards");
     41  fPixMapTable = GetConfig(SERVER_NAME " PixMapTable");
     42  fTimeOut = atof(GetConfig(SERVER_NAME " TimeOut"));
     43  fStatusRefreshRate = atof(GetConfig(SERVER_NAME " StatusRefreshRate"));
     44  fCCPort = atoi(GetConfig(SERVER_NAME " CCPort"));
     45  DACMin = atoi(GetConfig(SERVER_NAME " DACMin"));
     46  DACMax = atoi(GetConfig(SERVER_NAME " DACMax"));
     47  fHVCalibOffset = atof(GetConfig(SERVER_NAME " HVCalibOffset"));
     48  fHVCalibSlope = atof(GetConfig(SERVER_NAME " HVCalibSlope"));
     49  fHVMaxDiff = atoi(GetConfig(SERVER_NAME " HVMaxDiff"));
     50
     51  if (fStatusRefreshRate < MIN_RATE || fStatusRefreshRate > MAX_RATE)  fStatusRefreshRate = 1;
     52
    4453  // Open HV devices
    45   fHVBoard = new HVBoard* [config->NumHVBoards];
    46   for (int i=0; i<config->NumHVBoards; i++) {
    47     fHVBoard[NumHVBoards] = new HVBoard(i, config->fUSBDevice[i], this);
     54  fHVBoard = new HVBoard* [strlen(Boards)];
     55  char *Token = strtok(Boards, " \t");
     56  while (Token != NULL) {
     57    fHVBoard[NumHVBoards] = new HVBoard(NumHVBoards, Token, this);
    4858    if(fHVBoard[NumHVBoards]->fDescriptor >= 0) {
    49        printf("Synchronized and reset HV board %d (%s)\n",i,config->fUSBDevice[i]);
     59       PrintMessage("Synchronized and reset board %d (%s)\n",NumHVBoards,fHVBoard[NumHVBoards]->BoardName);
    5060       NumHVBoards++;
    5161    }
    5262    else {
    53       printf("Failed to synchronize to HV board %d (%s)\n",i,config->fUSBDevice[i]);
     63      PrintMessage(All, "Failed to synchronize board %d (%s)\n",NumHVBoards,fHVBoard[NumHVBoards]->BoardName);
    5464      delete fHVBoard[NumHVBoards];
    5565    }
    56   }
     66        Token = strtok(NULL, " \t");
     67  }
    5768  LastBoard = NumHVBoards-1;
    58 
    59   // Open log file
    60   if ((Logfile = fopen(config->fLogFile, "a")) == NULL) printf("Warning: Could not open log file '%s'\n", config->fLogFile);
    61   PrintMessage(MsgToLog,"********** Logging started **********\n");
    62 
    63   // Create instance of slow data class
    64   SlowDataClass = new SlowData("HV", config->fSlowDir);
    65   if (SlowDataClass->ErrorCode != 0) {
    66     PrintMessage("Warning: Could not open slowdata file (%s)\n", strerror(SlowDataClass->ErrorCode));
    67   }
    68   SlowDataClass->NewEntry("Value-Info", "Issued if new HV value set successfull: Board-Num HV-Board-Name Chain Channel DAC-Target Converted-Value");
    69   SlowDataClass->NewEntry("Error-Info", "Issued if error occurs when trying to set new HV value: Board-Num HV-Board-Name Chain Channel Attempted-DAC-Target Converted-Value");
     69 
     70  // Create instances
     71  calib  = new HVCalib(this);
     72  pm     = new PixelMap(fPixMapTable); 
    7073}
    7174
     
    7376ProcessIO::~ProcessIO() {
    7477 
    75   delete SlowDataClass;     
    76 
    7778  for (int i=0; i<NumHVBoards; i++) delete fHVBoard[i];
    7879  delete[] fHVBoard;
    7980   
    80   delete pm;   delete calib;    delete config;   
    81  
    82   if(Logfile != NULL) {
    83     if(fclose(Logfile) != 0) perror("Error closing logfile");
    84   }
     81  delete pm;        delete calib;
     82  delete Textout; 
    8583}
    8684
     
    9795
    9896  for(int i=0; i<MAX_NUM_TOKEN; i++) Param[i] = "";  // All pointers point initially to empty string
    99     NParam = ParseInput(Command, Param);
    100 
    101   // Adress HV board
     97  NParam = ParseInput(Command, Param);
     98
     99  // Adress board
    102100  if (Match(Param[0], "board")) {
    103101   
     
    158156  // Print HV utility configuration
    159157  else if (Match(Param[0], "config")) {
    160     PrintMessage( " Log file:          %s\n"
    161                   " Pixel map table:   %s\n"
    162                   " %d USB devices:\n", config->fLogFile, config->fPixMapTable,
    163                   config->NumHVBoards); 
    164     for (int i=0; i<config->NumHVBoards; i++) {
    165       PrintMessage(" Board %d: %s\n", i, config->fUSBDevice[i]);
     158    PrintMessage( " Pixel map table:   %s\n"
     159                  " %d USB devices:\n", fPixMapTable,
     160                  NumHVBoards); 
     161    for (int i=0; i<NumHVBoards; i++) {
     162      PrintMessage(" Board %d: %s\n", i, fHVBoard[i]->BoardName);
    166163    }
    167164    PrintMessage( " TimeOut:           %.2f s\n"
     
    172169                  " HVCalibOffset :    %f\n"
    173170                  " HVCalibSlope :     %f\n"
    174                   " HVMaxDiff :        %u\n", config->fTimeOut,
    175                   config->fStatusRefreshRate, config->fCCPort, config->DACMin,
    176                   config->DACMax, config->fHVCalibOffset, config->fHVCalibSlope,
    177                   config->fHVMaxDiff);
     171                  " HVMaxDiff :        %u\n", fTimeOut,
     172                  fStatusRefreshRate, fCCPort, DACMin,
     173                  DACMax, fHVCalibOffset, fHVCalibSlope,
     174                  fHVMaxDiff);
    178175
    179176    return;
     
    183180  // Print help
    184181  if (Match(Param[0], "help")) {
    185     puts(" board <i>|<i> <j>|<all>           Address board i, boards i-j or all boards or list boards");
    186     puts(" chain <i>|<i> <j>|<all>           Address chain i, chains i-j or all chains");
    187     puts(" hv <PXL id>|<ch>|<all> <v>        Set HV of pixel ID, ch. or all ch. of active chain(s)/board(s)");
    188     puts(" hvdiff <PXL id>|<ch>|<all> <diff> Change HV by <diff>");
    189     puts(" status [dac]                      Show status information (DAC values if requested)");
    190     puts(" config                            Print configuration");
    191     puts(" load <file>                       Load HV settings from <file>");
    192     puts(" save <file>                       Save current HV settings to [file]");
    193     puts(" exit                              Exit program");
    194     puts(" rate <rate>                       Set status refresh rate to <rate> [Hz]");
    195     puts(" timeout <time>                    Set timeout to return from read to <time> [s]");
    196     puts(" reset                             Reset active HV board");
    197     puts(" start                             Start HV status monitor");
    198     puts(" stop                              Stop HV status monitor - not recommended!");
    199     puts(" uptime                            Get program uptime [h:m:s]");
    200     puts(" verbose <on|off>|                 Enable|disable verbosity");
    201     puts(" help                              Print help");
    202     puts(" .<cmd>                            Execute shell command <cmd>");
     182    PrintMessage(" board <i>|<i> <j>|<all>           Address board i, boards i-j or all boards or list boards\n"
     183        " chain <i>|<i> <j>|<all>           Address chain i, chains i-j or all chains\n"
     184        " hv <PXL id>|<ch>|<all> <v>        Set bias of pixel ID, ch. or all ch. of active chain(s)/board(s)\n"
     185        " hvdiff <PXL id>|<ch>|<all> <diff> Change bias by <diff>\n"
     186                " status [dac]                      Show status information (DAC values if requested)\n"
     187                " config                            Print configuration\n"
     188                " load <file>                       Load and set bias settings from <file>\n"
     189                " save <file>                       Save current bias settings to [file]\n"
     190                " exit                              Exit program\n"
     191                " rate <rate>                       Set status refresh rate to <rate> [Hz]\n"
     192                " timeout <time>                    Set timeout to return from read to <time> [s]\n"
     193                " reset                             Reset active bias board\n"
     194                " start                             Start bias status monitor\n"
     195                " stop                              Stop bias status monitor - not recommended!\n"
     196                " uptime                            Get program uptime [h:m:s]\n"
     197                " help                              Print help\n"
     198                " .<cmd>                             Execute shell command <cmd>");
    203199
    204200    return;
     
    206202
    207203
    208   // Set new high voltage (if no boards available, simply returns OK)
     204  // Set new bias voltage (if no boards available, simply returns OK)
    209205  // First reponse to socket should be 'OK' if no error occurred.
    210206  if (Match(Param[0], "hv") || Match(Param[0], "hvdiff")) {
     
    249245    for (int i=FirstBoard; i<=LastBoard; i++) {
    250246      if (i!=Board && Board!=-1) continue;
    251 
    252       for (int j=FirstChain; j<=LastChain; j++) {
     247    for (int j=FirstChain; j<=LastChain; j++) {
    253248        if (j!=Chain && Chain!=-1) continue;
    254 
    255249        for (int k=0; k<NUM_CHANNELS; k++) {
    256250          if (k!=Channel && Channel!=-1) continue;
    257251
    258           // Convert from HV to DAC values
     252          // hvdiff is ignored if DAC value is zero
     253          if ((strlen(Param[0])>2 || isdigit(*Param[2])==0) && (fHVBoard[i]->HV[j][k] == 0)) continue;
     254
     255          // Determine new DAC values
    259256          if (!SetDac){
    260             if(strlen(Param[0]) > 2) fHVBoard[i]->HVV[j][k] += hvoltageV; // hvdiff
     257                if (strlen(Param[0])>2 || isdigit(*Param[2])==0) fHVBoard[i]->HVV[j][k] += hvoltageV; // hvdiff
    261258            else fHVBoard[i]->HVV[j][k] = hvoltageV;
    262259            DACValue = calib->HVToDAC(fHVBoard[i]->HVV[j][k], i, j, k);
    263260          }
    264261          else {
    265             if(strlen(Param[0]) > 2) DACValue = fHVBoard[i]->HV[j][k] + hvoltage; // hvdiff
     262            if(strlen(Param[0])>2 || isdigit(*Param[2])==0) DACValue = fHVBoard[i]->HV[j][k] + hvoltage; // hvdiff
    266263            else DACValue = hvoltage;
    267264          }
     
    271268          if (SetDac) fHVBoard[i]->HVV[j][k] = calib->DACToHV(fHVBoard[i]->HV[j][k], i, j, k);
    272269
    273           // Update DIM services
    274           fHVBoard[i]->BiasVolt[j][k]->updateService();
     270      // Update DIM services
     271      fHVBoard[i]->BiasVolt[j][k]->updateService();
    275272
    276273        } // Channels   
    277       } // Chains
     274    } // Chains
    278275    } // Boards
    279276
     
    306303      for (int Board=0; Board<NumHVBoards; Board++) {
    307304        if (Match(fHVBoard[Board]->BoardName, Buffer)) {
    308           PrintMessage("Found HV settings for board %d (%s)\n\r",fHVBoard[Board]->GetBoardNumber(), fHVBoard[Board]->BoardName);
     305          PrintMessage("Found bias settings for board %d (%s)\n\r",fHVBoard[Board]->GetBoardNumber(), fHVBoard[Board]->BoardName);
    309306
    310307          Chain = 0;  Channel = 0;
     
    339336           
    340337    if (NBoards != NumHVBoards) {
    341       PrintMessage("Warning: Could not load HV settings for all connected HV boards\n");
    342     }
    343     else if (Errors == 0) PrintMessage("Success: Read HV settings for all connected HV boards\n");
     338      PrintMessage("Warning: Could not load bias settings for all connected HV boards\n");
     339    }
     340    else if (Errors == 0) PrintMessage("Success: Read bias settings for all connected HV boards\n");
    344341    if (Errors != 0) PrintMessage("Warning: Errors on %d channel(s) occurred\n", Errors);
    345342   
     
    406403    }
    407404 
    408     fprintf(File,"********** HV settings, %04d %02d %02d, %02d:%02d:%02d **********\n\n",
     405    fprintf(File,"********** Bias settings, %04d %02d %02d, %02d:%02d:%02d **********\n\n",
    409406            1900 + Time->tm_year, 1 + Time->tm_mon,
    410407            Time->tm_mday, Time->tm_hour, Time->tm_min, Time->tm_sec);
     
    433430    state = active;
    434431    pthread_kill(HVMonitor, SIGUSR1);
    435     PrintMessage("Status monitoring activated\n");
     432    PrintMessage(All, "Status monitoring activated\n");
    436433    return; 
    437434  }
     
    442439   
    443440    PrintMessage("\n Status monitor: %s\n", state_str[state]);
    444     PrintMessage(" Verbose: %s\n", Verbose ? "on" : "off");
    445441    PrintMessage(" Status refresh rate [Hz]: %.2f\n", fStatusRefreshRate);
    446442    PrintMessage(" Socket state: %s\n", Socket==-1 ? "disconnected":"connected");
    447     PrintMessage(" Total number of HV boards: %d\n", NumHVBoards);
    448     PrintMessage(" Active HV boards: %d\n\n", LastBoard - FirstBoard + 1);
     443    PrintMessage(" Total number of boards: %d\n", NumHVBoards);
     444    PrintMessage(" Active boards: %d\n\n", LastBoard - FirstBoard + 1);
    449445
    450446    for (int i=FirstBoard; i<=LastBoard; i++) {
     
    479475    state = stopped;
    480476    pthread_kill(HVMonitor, SIGUSR1);
    481     PrintMessage("Status monitor stopped\n");
     477    PrintMessage(All, "Status monitor stopped\n");
    482478
    483479    return;
     
    520516
    521517
    522   // Enable/disable verbosity
    523   else if (Match(Param[0], "verbose")) {
    524 
    525     if (Match(Param[1], "on")) {
    526       Verbose = true;
    527       PrintMessage("Verbosity enabled\n");
    528     }   
    529     else if (Match(Param[1], "off")) {
    530       Verbose = false;
    531       PrintMessage("Verbosity disabled\n");
    532     }
    533     else PrintMessage("Usage: verbose <on>|<off>\n");
    534  
    535     return;
    536   }
    537  
    538  
    539518  // Exit program
    540519  else if(Match(Param[0], "exit")) {
     
    559538
    560539// Print message to selected target
    561 void ProcessIO::PrintMessage(int Target, const char *Format, ...) {
     540void ProcessIO::PrintMessage(MsgTarget Target, const char *Format, ...) {
    562541  va_list ArgumentPointer;
    563542  va_start(ArgumentPointer, Format);
     
    570549  va_list ArgumentPointer;
    571550  va_start(ArgumentPointer, Format);
    572   if (CmdFromSocket) DoPrintMessage(Format, ArgumentPointer, MsgToSocket|MsgToLog);
    573   else DoPrintMessage(Format, ArgumentPointer, MsgToConsole|MsgToLog);
     551  if (CmdFromSocket) DoPrintMessage(Format, ArgumentPointer, Client);
     552  else DoPrintMessage(Format, ArgumentPointer, Console);
    574553  va_end(ArgumentPointer);
    575554}
     
    577556// Function doing the actual printing work
    578557// Be careful with overloading variadic functions!
    579 void ProcessIO::DoPrintMessage(const char *Format, va_list ArgumentPointer, int Target) {
    580 
    581   char Textbuffer[MAX_COM_SIZE];
     558void ProcessIO::DoPrintMessage(const char *Format, va_list ArgumentPointer, MsgTarget Target) {
     559
     560  static char Textbuffer[MAX_COM_SIZE]; // static because of DIM service
    582561
    583562  memset(Textbuffer, 0, sizeof(Textbuffer)); 
     
    585564 
    586565  // Print to console
    587   if(Target & MsgToConsole) {
     566  if(Target & Console) {
    588567    if(strlen(Textbuffer)>0 && Textbuffer[strlen(Textbuffer)-1]=='\n') {
    589568      printf("\r%s%s", Textbuffer, Prompt);   // New prompt
     
    592571    fflush(stdout);
    593572  }
     573
     574  // Send to DIM service
     575  Textout->updateService(Textbuffer);
     576
    594577  // Print to socket
    595   if((Target & MsgToSocket) && Socket!=-1) {
     578  if((Target & Client) && Socket!=-1) {
    596579    write(Socket, Textbuffer, strlen(Textbuffer));
    597580  }
    598   // Print to log file (replace carriage return by linefeed)
    599   if((Target & MsgToLog) && Logfile!=NULL) {
    600     for (unsigned int i=0; i<strlen(Textbuffer); i++) {
    601       if(Textbuffer[i] == '\r') Textbuffer[i] = '\n';
    602     }
    603     time_t Time;
    604     strftime(Textbuffer+strlen(Textbuffer)+1,MAX_COM_SIZE-strlen(Textbuffer)-1, "%d/%m/%y %X", localtime(&(Time=time(NULL))));
    605     fprintf(Logfile, "%s: %s", Textbuffer+strlen(Textbuffer)+1, Textbuffer);
    606     fflush(Logfile);
    607   }
    608 }
    609 
    610 
    611 // Ramp to new voltage with maximum step size given in config->fHVMaxDiff
     581
     582  // Send to log
     583  if(Target & Log) {
     584    char *Buf;
     585    if (asprintf(&Buf, "%s %s", SERVER_NAME, Textbuffer) != -1) {
     586      DimClient::sendCommand("DColl/Log", Buf);
     587      free(Buf);
     588    }
     589    else DimClient::sendCommand("DColl/Log", SERVER_NAME" asprintf() failed");
     590  }
     591}
     592
     593
     594// Ramp to new voltage with maximum step size given in fHVMaxDiff
    612595// No ramping when decreasing voltage
    613596bool ProcessIO::RampVoltage(unsigned int Target, int Board, int Chain, int Channel) {
     
    615598  while (fHVBoard[Board]->HV[Chain][Channel] != (int) Target) {   
    616599    int Diff = Target - fHVBoard[Board]->HV[Chain][Channel];
    617     if (Diff > (int) config->fHVMaxDiff) Diff = config->fHVMaxDiff;
    618 
    619     if (fHVBoard[Board]->SetHV(Chain, Channel, fHVBoard[Board]->HV[Chain][Channel]+Diff) == 1) {
    620       if (Verbose) {
    621         PrintMessage("Board %d: high voltage of chain %d channel %d set to %d | 0X%.4X | %f V\n",fHVBoard[Board]->GetBoardNumber(),Chain, Channel, Target, Target, calib->DACToHV(Target,fHVBoard[Board]->GetBoardNumber(),Chain,Channel));
    622         PrintBoardStatus(Board);
    623       }
    624     }
    625     else {
    626       PrintMessage("Error: Could not set HV of board %d, chain %d, channel %d. Skipping channel\n",fHVBoard[Board]->GetBoardNumber(),Chain,Channel);
    627       SlowDataClass->NewEntry("Error");
    628       SlowDataClass->AddToEntry("%s %d %d %d %d %.2f ",fHVBoard[Board]->BoardName,Board, Chain, Channel, Target, calib->DACToHV(Target,Board,Chain,Channel));
     600    if (Diff > (int) fHVMaxDiff) Diff = fHVMaxDiff;
     601
     602    if (fHVBoard[Board]->SetHV(Chain, Channel, fHVBoard[Board]->HV[Chain][Channel]+Diff) != 1) {
     603      State(ERROR, "Could not set bias of board %d, chain %d, channel %d. Skipping channel\n",fHVBoard[Board]->GetBoardNumber(),Chain,Channel);
    629604      return false;
    630605    }
    631606  }
    632   SlowDataClass->NewEntry("Value");
    633   SlowDataClass->AddToEntry("%s %d %d %d %d %.2f ",fHVBoard[Board]->BoardName,Board, Chain, Channel, Target, calib->DACToHV(Target,Board,Chain,Channel));
    634 
    635   // Update DIM service
    636   fHVBoard[Board]->BiasDAC[Chain][Channel]->updateService();
    637  
     607
    638608  return true;
    639609}
     
    649619      if (!Warned) {
    650620        Warned = true;
    651         PrintMessage("Warning: Some board has many read/write errors, status monitor disabled\n");
     621        PrintMessage(All, "Warning: Some board has many read/write errors, status monitor disabled\n");
     622        State(WARN, "Warning: Some board has many read/write errors, status monitor disabled\n");
    652623      }
    653624      continue;
     
    655626
    656627    if (fHVBoard[i]->GetStatus() != 1) {
    657       PrintMessage("Error: Monitor could not read status of board %d\n", fHVBoard[i]->GetBoardNumber());
     628      PrintMessage(All, "Error: Monitor could not read status of board %d\n", fHVBoard[i]->GetBoardNumber());
     629      State(ERROR, "Error: Monitor could not read status of board %d\n", fHVBoard[i]->GetBoardNumber());
    658630    }
    659631   
    660632    if (fHVBoard[i]->ResetButton) {
    661       PrintMessage("Manual reset of board %d\n",fHVBoard[i]->GetBoardNumber());
     633      PrintMessage(All, "Manual reset of board %d\n",fHVBoard[i]->GetBoardNumber());
     634      State(INFO, "Manual reset of board %d\n",fHVBoard[i]->GetBoardNumber());
    662635      ResetBoard(i);
    663636    }
    664637   
    665638    if (!fHVBoard[i]->WrapOK) {
    666       PrintMessage("Error: Wrap counter mismatch board %d\n",fHVBoard[i]->GetBoardNumber());
     639      PrintMessage(All, "Error: Wrap counter mismatch board %d\n",fHVBoard[i]->GetBoardNumber());
     640      State(ERROR, "Error: Wrap counter mismatch board %d\n",fHVBoard[i]->GetBoardNumber());
    667641    }
    668642
    669643    for (int j=0; j<NUM_CHAINS; j++) {
    670644      if (fHVBoard[i]->Overcurrent[j]) {
    671         PrintMessage("Warning: Overcurrent in chain %d of board %d\n",j,fHVBoard[i]->GetBoardNumber());
    672         ResetBoard(i);
     645                PrintMessage(All, "Warning: Overcurrent in chain %d of board %d\n",j,fHVBoard[i]->GetBoardNumber());
     646                State(WARN, "Warning: Overcurrent in chain %d of board %d\n",j,fHVBoard[i]->GetBoardNumber());
     647                ResetBoard(i);
    673648      }
    674649    }
     
    724699}
    725700
     701//
     702// Command handling (the only command is 'BIAS/Command')
     703//
     704void ProcessIO::commandHandler() {
     705
     706    // Log command
     707    PrintMessage(Log, "DIM> %s\n", getString());
     708
     709    // Process command     
     710    pthread_mutex_lock(&control_mutex);
     711    CommandControl(getString());
     712    pthread_mutex_unlock(&control_mutex);
     713}
    726714
    727715// Check if two strings match (min 1 character must match)
  • hvcontrol/src/ProcessIO.h

    r126 r161  
    88#include <signal.h>
    99
    10 #define SERVER_NAME "BIAS"       // Name to use in DIM
    11 #include "../../Evidence/Evidence.h"
     10#define SERVER_NAME "Bias"       // Name to use in DIM
     11#include "Evidence.h"
    1212
    13 #include "HVConfig.h"
    1413#include "HVCalib.h"
    1514#include "HV.h"
    1615#include "../pixelmap/PixelMap.h"
    17 #include "../drsdaq/SlowData.h"
     16
     17#define MAX_COM_SIZE 5000
     18#define NUM_CHAINS 4
     19#define NUM_CHANNELS 32
     20#define BUFFER_LENGTH 256
     21
     22#define MIN_TIMEOUT 0.01
     23#define MAX_TIMEOUT 5.0
     24
     25#define MIN_RATE 0.01
     26#define MAX_RATE 50.0
    1827
    1928#define MAX_NUM_TOKEN 10
    2029
    21 #define MsgToConsole 1
    22 #define MsgToLog 2
    23 #define MsgToSocket 4
     30enum MsgTarget {Console=1, Log=2, Client=4, All=7};
     31typedef enum stateenum {active, stopped, na} state_enum;
    2432
    25 typedef enum stateenum { active, stopped, na } state_enum;
    26 
    27 class ProcessIO: public EvidenceServer {
     33class ProcessIO: public DimCommand, public EvidenceServer {
    2834
    2935  time_t StartTime;
    30   FILE *Logfile;
    3136  PixelMap *pm;
     37  DimService *Textout;
     38 
     39  void commandHandler();
    3240
    3341 public:
    3442 
    35   HVConfig    *config;
     43  //HVConfig    *config;
    3644  HVCalib     *calib;
    3745  HVBoard **fHVBoard;
    38   SlowData *SlowDataClass;
    3946 
    4047  pthread_mutex_t control_mutex;
     
    4451  int NParam;
    4552  bool CmdFromSocket;
    46  
     53
     54  // Configuration data
     55  char *fPixMapTable;
     56  float fTimeOut;
     57  float fStatusRefreshRate;
     58  int fCCPort;
     59  int DACMin;
     60  int DACMax;
     61  float fHVCalibOffset;
     62  float fHVCalibSlope;
     63  unsigned int fHVMaxDiff;
     64
    4765  // Status variables 
    4866  pthread_t HVMonitor;       // exit function sends signal to these threads
    4967  pthread_t SocketThread;
    5068 
    51   bool Verbose; 
    5269  int Socket;                // -1 if not connected
    5370
     
    5875  int LastChain;
    5976 
    60   float fStatusRefreshRate;
    6177  state_enum   state;
    6278  bool Exit;
     
    6682  ~ProcessIO();
    6783
    68   void PrintMessage(int, const char *, ...);
     84  void PrintMessage(MsgTarget, const char *, ...);
    6985  void PrintMessage(const char *, ...);
    70   void DoPrintMessage(const char *, va_list, int);
     86  void DoPrintMessage(const char *, va_list, MsgTarget);
    7187  void CommandControl(char*);
    7288  bool RampVoltage(unsigned int, int, int, int);
Note: See TracChangeset for help on using the changeset viewer.