Changeset 10226


Ignore:
Timestamp:
03/04/11 14:33:00 (14 years ago)
Author:
ogrimm
Message:
Bug fix in amplitude calibration loop
Location:
fact/FADctrl
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • fact/FADctrl/FAD.cc

    r10224 r10226  
    542542        while (fread(&Data, sizeof(Data), 1, File) == 1) {
    543543          for (unsigned int i=0; i<Boards.size(); i++) if (Data.DNA == Boards[i]->GetStatus().DNA) {
    544             PrintMessage("Found calibration data for board %d (%s)\n", i, ctime(&Data.Time));
     544            PrintMessage("Found calibration for board %d - %s", i, ctime(&Data.Time));
    545545            Boards[i]->ACalib = Data;
    546546          }
     
    566566//
    567567void FAD::cmd_status() {
     568
     569  int MinCount = std::numeric_limits<int>::max();
     570  unsigned int SlowestBoard = 0;
    568571
    569572  // ==== Print board overview ====
     
    575578          if (Boards[i]->Active) Count++;
    576579          if (!Boards[i]->CommOK) Error++;
     580          if (Boards[i]->Active && Boards[i]->Count < MinCount) {
     581                MinCount = Boards[i]->Count;
     582                SlowestBoard = i;
     583          }
    577584        }         
    578585
    579         PrintMessage("\rTotal boards: %d    (%d communication errors)\n", Boards.size(), Error);
     586        PrintMessage("\rTotal boards: %d    (%d with communication errors)\n", Boards.size(), Error);
    580587
    581588        // Print list of active boards
    582589        PrintMessage("Active are %d boards(s)   ", Count);
    583590        for (unsigned int i=0; i<Boards.size(); i++) {
    584           if (Boards[i]->Active) PrintMessage(" %d", i);
    585           if (!Boards[i]->Active) PrintMessage("!");
     591          if (Boards[i]->Active) PrintMessage(" %d%s", i, Boards[i]->CommOK ? "":"!");
    586592        }
    587593        PrintMessage("\n");
     
    589595        // Current mode
    590596        if (Mode == idle) PrintMessage("Current mode is IDLE\n");
    591         else if (Mode == acalib) PrintMessage("Current mode is ACALIB (3x%d events)\n", NumEventsRequested);
     597        else if (Mode == acalib) PrintMessage("Current mode is ACALIB (3x%d events, slowest board %d has %d events)\n", NumEventsRequested, SlowestBoard, MinCount);
    592598        else if (Mode == datarun) PrintMessage("Current mode is DATARUN (%d events requested, %d events taken)\n", NumEventsRequested, NumEvents);
    593599
     
    623629        // Board identification
    624630        PrintMessage("Board ID %.4x      Firmware revision %.4x      Serial %llx\n", S.BoardID, S.FirmwareRevision, S.DNA);
    625         PrintMessage("Board time %gs       Event counter %d\n", S.BoardTime/1.0e4 , S.EventCounter);
    626 
    627         // Slow data
     631        PrintMessage("Board time %g s       Event counter %d\n", S.BoardTime/1.0e4 , S.EventCounter);
     632
     633        // Other data
    628634        PrintMessage("Frequency %.2f GHz    Phase shift %d    PLL lock %d %d %d %d\n", S.Frequency, S.PhaseShift, S.Lock[0], S.Lock[1], S.Lock[2], S.Lock[3]);
    629         PrintMessage("DENABLE %d  DWRITE %d  SPI_clk %d DCM_lock: %d DCM_ready %d\n", S.denable, S.dwrite, S.spi_clk, S.DCM_lock, S.DCM_ready);
     635        PrintMessage("DENABLE %d   DWRITE %d   SPI_clk %d   DCM_lock %d  DCM_ready %d\n", S.denable, S.dwrite, S.spi_clk, S.DCM_lock, S.DCM_ready);   
    630636        PrintMessage("DAC %d %d %d %d   %d %d %d %d\n", S.DAC[0], S.DAC[1], S.DAC[2], S.DAC[3], S.DAC[4], S.DAC[5], S.DAC[6], S.DAC[7] );
    631637        PrintMessage("Temperature %.2f %.2f %.2f %.2f", S.Temp[0], S.Temp[1], S.Temp[2], S.Temp[3]);
     
    636642        }
    637643        PrintMessage("\n");
    638         /*
    639         if (GetBoard(i)->GetStatusReg() & BIT_RUNNING)
    640           PrintMessage("   Domino wave running\n");
    641 
    642         if (GetBoard(i)->GetCtrlReg() & BIT_DMODE)
    643           PrintMessage("   DMODE circular\n");
    644         else
    645           PrintMessage("   DMODE single shot\n");
    646  
    647     }*/
    648644  } // for()
    649645}
     
    811807void FAD::SaveAmplitudeCalibration() {
    812808
    813   PrintMessage("Amplitude calibration of all active boards finished, original ROI and DAC set\n");
    814  
    815809  // Open calibration data file
    816810  string Filename = string(getenv("HOME"))+"/FAD_ACal";
     
    822816  }
    823817 
    824   // Write calibration information
    825   for (unsigned int i=0; i<Boards.size(); i++) {
     818  // Write valid calibration information for active boards
     819  for (unsigned int i=0; i<Boards.size(); i++) {
     820        if (!Boards[i]->Active || Boards[i]->ACalib.Time == -1) continue;
     821       
    826822        if (fwrite(&Boards[i]->ACalib, sizeof(Boards[i]->ACalib), 1, File) != 1) {
    827823          PrintMessage("Could not write to calibration file '%s'\n", Filename.c_str());
     
    833829  if (fclose(File) != 0) PrintMessage("Could not close calibration file '%s'\n", Filename.c_str());
    834830 
    835   PrintMessage("Appended amplitude calibration information to file '%s'\n", Filename.c_str());
     831  PrintMessage("Appended amplitude calibration to file '%s'\n", Filename.c_str());
    836832}
    837833
     
    985981
    986982          // Write channel data (12 bit signed two's complement with out-of-range-bit and leading zeroes)
    987           int Count = 0;
    988           memset(Data, 0, Boards.size()*NChips*NChannels*NBins*sizeof(short));
     983          int Count = Brd*NChips*NChannels*NBins;
     984          memset(Data+Count, 0, NChips*NChannels*NBins*sizeof(short));
    989985
    990986          Boards[Brd]->Lock();
     
    10221018        if (Mode != datarun) continue;
    10231019       
    1024         // Check if all event numbers are the same
     1020        // Check if all event numbers of active boards are the same
    10251021        bool Same = true;
    10261022        for (unsigned int i=0; i<Boards.size(); i++) {
  • fact/FADctrl/FADBoard.cc

    r10224 r10226  
    146146// Perform amplitude calibration in steps
    147147//
     148// The steps are intended to assure that up to date data is available
    148149void FADBoard::AmplitudeCalibration() {
    149150
    150   enum StateType {wait, init, baseline, gain, secondary};
    151 
    152   static struct BoardStatus InitialStatus;
    153   static vector<unsigned short> ROICmd;
    154   static unsigned short DACCmd[] = {htons(CMD_Write | (BADDR_DAC + 1)), 0, htons(CMD_Write | (BADDR_DAC + 2)), 0, htons(CMD_Write | (BADDR_DAC + 3)), 0};
    155   static StateType State = wait;
    156   static int Count = 0;
    157 
    158   // Check if mode is amplitude calibration
    159   if (m->Mode != m->acalib) {
    160         State = init;
    161         return;
    162   }
     151  vector<unsigned short> ROICmd;
     152  unsigned short DACCmd[] = {htons(CMD_Write | (BADDR_DAC + 1)), 0, htons(CMD_Write | (BADDR_DAC + 2)), 0, htons(CMD_Write | (BADDR_DAC + 3)), 0};
     153  string Message = string("ACALIBDONE")+Name;
    163154
    164155  switch (State) {
    165   // ====== Part A: Initialization =====
    166   case init:
     156  // ====== Part A: Check if amplitude calibration should start and initialise =====
     157  case standbye:
     158        if (m->Mode != m->acalib) break;
     159
    167160        // Invalidate current calibration
    168161        ACalib.Time = -1;
     162        Count = 0;
    169163
    170164        // Save initial board status, set all ROIs to 1024 and set DAC values
     
    189183  // ====== Part B: Baseline calibration =====
    190184  case baseline:
     185        // Check for stopping
     186        if (m->Mode != m->acalib) {
     187          State = cleanup;
     188          break;
     189        }
     190
     191        // Average
    191192        for (unsigned int Chip=0; Chip<NChips; Chip++) {
    192193          for (unsigned int Chan=0; Chan<NChannels; Chan++) {
     
    199200       
    200201        // Determine baseline if integration finished
    201         if (Count == m->NumEventsRequested) {
    202           for (unsigned int i=0; i<NChips; i++) {
    203                 for (unsigned int j=0; j<NChannels; j++) {
    204                   for (unsigned int k=0; k<NBins; k++) {
    205                         ACalib.Baseline[i][j][k] = Sum[i][j][k] / m->NumEventsRequested;
    206                   }
    207                 }
    208           }
    209          
    210           // Set new DAC values and start accumulation
    211           DACCmd[1] = htons(50000);
    212           DACCmd[3] = htons(50000);
    213           DACCmd[5] = htons(50000);
    214           Send(DACCmd, sizeof(DACCmd));
    215 
    216           // Clear sum vector and set state to accumulate
    217           memset(Sum, 0, sizeof(Sum));
    218           Count = 0;
    219           State = gain;
    220         }
     202        if (Count < m->NumEventsRequested) break;
     203
     204        for (unsigned int i=0; i<NChips; i++) {
     205          for (unsigned int j=0; j<NChannels; j++) {
     206                for (unsigned int k=0; k<NBins; k++) {
     207                  ACalib.Baseline[i][j][k] = Sum[i][j][k] / m->NumEventsRequested;
     208                }
     209          }
     210        }
     211
     212        // Set new DAC values and start accumulation
     213        DACCmd[1] = htons(50000);
     214        DACCmd[3] = htons(50000);
     215        DACCmd[5] = htons(50000);
     216        Send(DACCmd, sizeof(DACCmd));
     217
     218        // Clear sum vector and set state to accumulate
     219        memset(Sum, 0, sizeof(Sum));
     220        Count = 0;
     221        State = gain;
    221222        break;
    222223
    223224  // ====== Part C: Gain calibration =====
    224225  case gain:
     226        // Check for stopping
     227        if (m->Mode != m->acalib) {
     228          State = cleanup;
     229          break;
     230        }
     231
     232        // Average
    225233        for (unsigned int Chip=0; Chip<NChips; Chip++) {
    226234          for (unsigned int Chan=0; Chan<NChannels; Chan++) {
     
    233241       
    234242        // Determine gain if integration finished
    235         if (Count == m->NumEventsRequested) {
    236           for (unsigned int i=0; i<NChips; i++) {
    237                 for (unsigned int j=0; j<NChannels; j++) {
    238                   for (unsigned int k=0; k<NBins; k++) {
    239                         ACalib.Gain[i][j][k] = (Sum[i][j][k] / m->NumEventsRequested) - ACalib.Baseline[i][j][k];
    240                   }
    241                 }
    242           }
    243          
    244           // Set new DAC values and start accumulation
    245           DACCmd[1] = htons(0);
    246           DACCmd[3] = htons(0);
    247           DACCmd[5] = htons(0);
    248           Send(DACCmd, sizeof(DACCmd));
    249 
    250           // Clear sum vector and set state to accumulate
    251           memset(Sum, 0, sizeof(Sum));
    252           Count = 0;
    253           State = secondary;
    254         }
     243        if (Count < m->NumEventsRequested) break;
     244       
     245        for (unsigned int i=0; i<NChips; i++) {
     246          for (unsigned int j=0; j<NChannels; j++) {
     247                for (unsigned int k=0; k<NBins; k++) {
     248                  ACalib.Gain[i][j][k] = (Sum[i][j][k] / m->NumEventsRequested) - ACalib.Baseline[i][j][k];
     249                }
     250          }
     251        }
     252
     253        // Set new DAC values and start accumulation
     254        DACCmd[1] = htons(0);
     255        DACCmd[3] = htons(0);
     256        DACCmd[5] = htons(0);
     257        Send(DACCmd, sizeof(DACCmd));
     258
     259        // Clear sum vector and set state to accumulate
     260        memset(Sum, 0, sizeof(Sum));
     261        Count = 0;
     262        State = secondary;
    255263        break;
    256264
    257265  // ====== Part D: Secondary calibration =====
    258266  case secondary:
     267        // Check for stopping
     268        if (m->Mode != m->acalib) {
     269          State = cleanup;
     270          break;
     271        }
     272
     273        // Average
    259274        for (unsigned int Chip=0; Chip<NChips; Chip++) {
    260275          for (unsigned int Chan=0; Chan<NChannels; Chan++) {
     
    267282       
    268283        // Determine secondary baseline if integration finished
    269         if (Count == m->NumEventsRequested) {
    270           for (unsigned int i=0; i<NChips; i++) {
    271                 for (unsigned int j=0; j<NChannels; j++) {
    272                   for (unsigned int k=0; k<NBins; k++) {
    273                         ACalib.Secondary[i][j][k] = Sum[i][j][k] / (double) m->NumEventsRequested;
    274                   }
    275                 }
    276           }
    277          
    278           // Write back original ROI and DAC settings
    279           ROICmd.clear();
    280           for (unsigned int i=0; i<NChips*NChannels; i++) {
    281                 ROICmd.push_back(htons(CMD_Write | (BADDR_ROI + i)));
    282                 ROICmd.push_back(htons(InitialStatus.ROI[i/NChannels][i%NChannels]));
    283           }
    284           Send(&ROICmd[0], ROICmd.size()*sizeof(unsigned short));
    285 
    286       DACCmd[1] = htons(InitialStatus.DAC[1]);
    287       DACCmd[3] = htons(InitialStatus.DAC[2]);
    288       DACCmd[5] = htons(InitialStatus.DAC[3]);
    289           Send(DACCmd, sizeof(DACCmd));
    290 
    291           // Store calibration time and temperature
    292           ACalib.DNA = Status.DNA;
    293           ACalib.Frequency = Status.Frequency;
    294           ACalib.Time = time(NULL);
    295           ACalib.Temp = 0;
    296           for (unsigned int i=0; i<NTemp; i++) ACalib.Temp += Status.Temp[i] / NTemp;
    297          
    298           // Inform event thread that calibration is finished
    299           string Message = string("ACALIBDONE")+Name;
    300           if (write(m->Pipe[1], Message.data(), Message.size()) == -1) {
    301                 m->Message(m->ERROR, "write() to Pipe[1] failed in class FADBoard::AmplitudeCalibration (%s)", strerror(errno));
    302           }
    303 
    304           State = wait;
    305         }
     284        if (Count < m->NumEventsRequested) break;
     285
     286        for (unsigned int i=0; i<NChips; i++) {
     287          for (unsigned int j=0; j<NChannels; j++) {
     288                for (unsigned int k=0; k<NBins; k++) {
     289                  ACalib.Secondary[i][j][k] = Sum[i][j][k] / (double) m->NumEventsRequested;
     290                }
     291          }
     292        }
     293
     294        // Store calibration time and temperature
     295        ACalib.DNA = Status.DNA;
     296        ACalib.Frequency = Status.Frequency;
     297        ACalib.Time = time(NULL);
     298        ACalib.Temp = 0;
     299        for (unsigned int i=0; i<NTemp; i++) ACalib.Temp += Status.Temp[i] / NTemp;
     300
     301        // Inform event thread that calibration is finished for this board
     302        if (write(m->Pipe[1], Message.data(), Message.size()) == -1) {
     303          m->Message(m->ERROR, "write() to Pipe[1] failed in class FADBoard::AmplitudeCalibration (%s)", strerror(errno));
     304        }
     305
     306        State = cleanup;
    306307        break;
    307308
    308   // ====== Wait for Mode not being acalib =====
     309  // ====== Part E: Write back original ROI and DAC settings =====
     310  case cleanup:
     311    // ROI values
     312        ROICmd.clear();
     313        for (unsigned int i=0; i<NChips*NChannels; i++) {
     314          ROICmd.push_back(htons(CMD_Write | (BADDR_ROI + i)));
     315          ROICmd.push_back(htons(InitialStatus.ROI[i/NChannels][i%NChannels]));
     316        }
     317        Send(&ROICmd[0], ROICmd.size()*sizeof(unsigned short));
     318
     319        // DAC values
     320    DACCmd[1] = htons(InitialStatus.DAC[1]);
     321    DACCmd[3] = htons(InitialStatus.DAC[2]);
     322    DACCmd[5] = htons(InitialStatus.DAC[3]);
     323        Send(DACCmd, sizeof(DACCmd));
     324       
     325        State = wait;
     326        break;
     327
     328  // ====== Wait for Mode not being idle =====
    309329  case wait:
    310         if (m->Mode != m->acalib) State = init;
     330        if (m->Mode == m->idle) State = standbye;
    311331        break;
    312332  }
     
    435455          }
    436456
    437                 // Extract Firmware status info
    438                 Status.denable = (bool) ( ntohs(Header->PLLLCK) & (1<<11) );
    439                 Status.dwrite = (bool) ( ntohs(Header->PLLLCK) & (1<<10) );
    440                 Status.DCM_lock = (bool) ( ntohs(Header->PLLLCK) & (1<<9) );
    441                 Status.DCM_ready = (bool) ( ntohs(Header->PLLLCK) & (1<<8) );
    442                 Status.spi_clk = (bool) ( ntohs(Header->PLLLCK) & (1<<7) );
    443 
     457          // Extract Firmware status info
     458          Status.denable = (bool) ( ntohs(Header->PLLLCK) & (1<<11) );
     459          Status.dwrite = (bool) ( ntohs(Header->PLLLCK) & (1<<10) );
     460          Status.DCM_lock = (bool) ( ntohs(Header->PLLLCK) & (1<<9) );
     461          Status.DCM_ready = (bool) ( ntohs(Header->PLLLCK) & (1<<8) );
     462          Status.spi_clk = (bool) ( ntohs(Header->PLLLCK) & (1<<7) );
    444463
    445464          // Extract temperatures (MSB indicates if temperature is positive or negative)
  • fact/FADctrl/FADBoard.h

    r10224 r10226  
    4343          uint32_t BoardTime;
    4444          uint32_t EventCounter;
     45          struct timeval Update;
     46
    4547          uint32_t TriggerID;
    4648          uint16_t TriggerType;
    4749          uint16_t TriggerCRC;
    4850          uint16_t TriggerCell[NChips];
     51
    4952          float Frequency;
    5053          bool Lock[NChips];
    51                 bool denable;
    52                 bool dwrite;
    53                 bool DCM_lock;
    54                 bool DCM_ready;
    55                 bool spi_clk;
    5654          int8_t PhaseShift;
     55
     56          bool denable;
     57          bool dwrite;
     58          bool DCM_lock;
     59          bool DCM_ready;
     60          bool spi_clk;
     61
    5762          uint16_t ROI[NChips][NChannels];
    5863          uint16_t DAC[NDAC];
    5964          float Temp[NTemp];
    60           struct timeval Update;
    6165        } Status;
    6266
     
    8892        pthread_t Thread;
    8993        pthread_cond_t CondVar;
     94       
     95  // Amplitude calibration
     96        int Count; 
     97
     98  private:
     99        enum StateType {standbye, baseline, gain, secondary, cleanup, wait};
     100        struct BoardStatus InitialStatus;
     101        StateType State;
    90102};
    91103
Note: See TracChangeset for help on using the changeset viewer.