| 1 | //
|
|---|
| 2 | // Class processing user input
|
|---|
| 3 | //
|
|---|
| 4 |
|
|---|
| 5 | #include "User.h"
|
|---|
| 6 | #include <readline/readline.h>
|
|---|
| 7 |
|
|---|
| 8 | using namespace std;
|
|---|
| 9 |
|
|---|
| 10 | // Branch table for command evaluation
|
|---|
| 11 | static const struct CL_Struct { const char *Name;
|
|---|
| 12 | void (User::*CommandPointer)();
|
|---|
| 13 | unsigned int MinNumParameter;
|
|---|
| 14 | const char *Parameters;
|
|---|
| 15 | const char *Help;
|
|---|
| 16 | } CommandList[] =
|
|---|
| 17 | {{"pixel", &User::cmd_hv, 2, "<pixel id> <v>", "Change bias of pixel"},
|
|---|
| 18 | {"channel", &User::cmd_hv, 2, "<channel range> <v>", "Change bias of (all) channels of active crate"},
|
|---|
| 19 | {"gs", &User::cmd_gs, 1, "[crate] <volt>", "Global voltage set active crate"},
|
|---|
| 20 | {"reset", &User::cmd_reset, 0, "", "Reset active crate"},
|
|---|
| 21 | {"synch", &User::cmd_synch, 0, "", "Synchronize active crate"}, {"crate", &User::cmd_crate, 1, "<number>", "Select active crate"},
|
|---|
| 22 | {"status", &User::cmd_status, 0, "[dac|current]", "Show status information (DAC values if requested)"},
|
|---|
| 23 | {"mode", &User::cmd_mode, 1, "<static|dynamic>", "Set voltage stabilization mode (experimental)"},
|
|---|
| 24 | {"load", &User::cmd_load, 1, "<file>", "Load and set bias settings from file"},
|
|---|
| 25 | {"save", &User::cmd_save, 1, "<file>", "Save current bias settings to file"},
|
|---|
| 26 | {"rate", &User::cmd_rate, 1, "<rate>", "Set refresh rate in Hz"},
|
|---|
| 27 | {"timeout", &User::cmd_timeout, 1, "<time>", "Set timeout to return from read in seconds"},
|
|---|
| 28 | {"help", &User::cmd_help, 0, "", "Print help"},
|
|---|
| 29 | {"exit", &User::cmd_exit, 0, "", "Exit program"}};
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 | //
|
|---|
| 33 | // Constructor
|
|---|
| 34 | //
|
|---|
| 35 | User::User(): EvidenceServer(SERVER_NAME) {
|
|---|
| 36 |
|
|---|
| 37 | MainThread = pthread_self();
|
|---|
| 38 |
|
|---|
| 39 | // DIM console service used in PrintMessage()
|
|---|
| 40 | ConsoleText = NULL;
|
|---|
| 41 | ConsoleOut = new DimService(SERVER_NAME"/ConsoleOut", (char *) "");
|
|---|
| 42 |
|
|---|
| 43 | // Get configuration data
|
|---|
| 44 | vector<string> Boards = Tokenize(GetConfig("Boards"), " \t");
|
|---|
| 45 | Boards = Tokenize("FTE00FOH", " \t");
|
|---|
| 46 | fTimeOut = atof(GetConfig("TimeOut").c_str());
|
|---|
| 47 | fStatusRefreshRate = atof(GetConfig("StatusRefreshRate").c_str());
|
|---|
| 48 | fMaxDiff = atof(GetConfig("HVMaxDiffNew").c_str());
|
|---|
| 49 |
|
|---|
| 50 | if (fStatusRefreshRate < MIN_RATE || fStatusRefreshRate > MAX_RATE) fStatusRefreshRate = 1;
|
|---|
| 51 |
|
|---|
| 52 | // Open devices
|
|---|
| 53 | for (unsigned int i=0; i<Boards.size(); i++) {
|
|---|
| 54 |
|
|---|
| 55 | class Crate *New = new class Crate(Boards[i], Crates.size(), this);
|
|---|
| 56 |
|
|---|
| 57 | if (New->InitOK && New->Synch()) {
|
|---|
| 58 | PrintMessage("Synchronized and reset crate %s (#%d)\n", Boards[i].c_str(), Crates.size());
|
|---|
| 59 | Crates.push_back(New);
|
|---|
| 60 | }
|
|---|
| 61 | else {
|
|---|
| 62 | Message(WARN, "Failed to synchronize crate %s", Boards[i].c_str());
|
|---|
| 63 | delete New;
|
|---|
| 64 | }
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 | // Set active crate
|
|---|
| 68 | if (!Boards.empty()) ActiveCrate = 0;
|
|---|
| 69 | else ActiveCrate = -1;
|
|---|
| 70 |
|
|---|
| 71 | // Create PixelMap instance (map from config server)
|
|---|
| 72 | DimRpcInfo RPC((char *) "ConfigRequest", (char *) "");
|
|---|
| 73 | RPC.setData((char *) "Misc PixelMap");
|
|---|
| 74 | PixMap = new PixelMap(std::string(RPC.getString(), RPC.getSize()));
|
|---|
| 75 |
|
|---|
| 76 | // Install DIM command (after all initialized)
|
|---|
| 77 | DIMCommand = new DimCommand((char *) SERVER_NAME"/Command", (char *) "C", this);
|
|---|
| 78 |
|
|---|
| 79 | // Create monitor thread and make accessible for sending signal
|
|---|
| 80 | if ((pthread_create(&Thread, NULL, (void * (*)(void *)) LaunchMonitor,(void *) this)) != 0) {
|
|---|
| 81 | Message(FATAL, "pthread_create() failed with Monitor thread");
|
|---|
| 82 | }
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 | //
|
|---|
| 86 | // Destructor
|
|---|
| 87 | //
|
|---|
| 88 | User::~User() {
|
|---|
| 89 |
|
|---|
| 90 | // Wait for thread to quit
|
|---|
| 91 | if (pthread_cancel(Thread) != 0) Message(ERROR, "pthread_cancel() failed in ()");
|
|---|
| 92 | if (pthread_join(Thread, NULL) != 0) Message(ERROR, "pthread_join() failed in ()");
|
|---|
| 93 |
|
|---|
| 94 | // Delete all crates
|
|---|
| 95 | for (unsigned int i=0; i<Crates.size(); i++) delete Crates[i];
|
|---|
| 96 |
|
|---|
| 97 | delete DIMCommand;
|
|---|
| 98 | delete PixMap;
|
|---|
| 99 | delete ConsoleOut;
|
|---|
| 100 | free(ConsoleText);
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | //
|
|---|
| 104 | // Process user input
|
|---|
| 105 | //
|
|---|
| 106 | void User::commandHandler() {
|
|---|
| 107 |
|
|---|
| 108 | // Build string safely
|
|---|
| 109 | string Command = string(getCommand()->getString(), getCommand()->getSize());
|
|---|
| 110 |
|
|---|
| 111 | // Check if command is legal and ignore empty commands
|
|---|
| 112 | if (getCommand() != DIMCommand || Command.size() < 2) return;
|
|---|
| 113 |
|
|---|
| 114 | // Shell command
|
|---|
| 115 | if(Command[0]=='.') {
|
|---|
| 116 | if (system(Command.c_str()+1) == 1) {
|
|---|
| 117 | PrintMessage("Error with system() call\n");
|
|---|
| 118 | }
|
|---|
| 119 | return;
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 | // Parse command into tokens
|
|---|
| 123 | Parameter = Tokenize(Command, " ");
|
|---|
| 124 |
|
|---|
| 125 | // Search for command in command list
|
|---|
| 126 | for(unsigned int CmdNumber=0; CmdNumber<sizeof(CommandList)/sizeof(CL_Struct); CmdNumber++) {
|
|---|
| 127 | if (Match(Parameter[0], CommandList[CmdNumber].Name)) {
|
|---|
| 128 | if(Parameter.size()-1 < CommandList[CmdNumber].MinNumParameter) {
|
|---|
| 129 | PrintMessage("Usage: %s %s\n", CommandList[CmdNumber].Name, CommandList[CmdNumber].Parameters);
|
|---|
| 130 | return;
|
|---|
| 131 | }
|
|---|
| 132 |
|
|---|
| 133 | // Jump to command function
|
|---|
| 134 | (this->*CommandList[CmdNumber].CommandPointer)();
|
|---|
| 135 | return;
|
|---|
| 136 | }
|
|---|
| 137 | }
|
|---|
| 138 | PrintMessage("Unknown command '%s'\n", Parameter[0].c_str());
|
|---|
| 139 | }
|
|---|
| 140 |
|
|---|
| 141 |
|
|---|
| 142 | // Print help
|
|---|
| 143 | void User::cmd_help() {
|
|---|
| 144 |
|
|---|
| 145 | char Buffer[MAX_COM_SIZE];
|
|---|
| 146 | for(unsigned int i=0; i<sizeof(CommandList)/sizeof(CL_Struct); i++) {
|
|---|
| 147 | snprintf(Buffer, sizeof(Buffer), "%s %s", CommandList[i].Name, CommandList[i].Parameters);
|
|---|
| 148 | PrintMessage("%-28s%s\n", Buffer, CommandList[i].Help);
|
|---|
| 149 | }
|
|---|
| 150 |
|
|---|
| 151 | PrintMessage(".<command> Execute shell command\n\n"
|
|---|
| 152 | "Items in <> are mandatory, in [] optional, | indicates mutual exclusive or.\n"
|
|---|
| 153 | "Channel ranges can be 'all', a single number or in the form 'a-b'.\n");
|
|---|
| 154 | }
|
|---|
| 155 |
|
|---|
| 156 | //
|
|---|
| 157 | // Synchronize boards
|
|---|
| 158 | //
|
|---|
| 159 | void User::cmd_synch() {
|
|---|
| 160 |
|
|---|
| 161 | if (ActiveCrate == -1) return;
|
|---|
| 162 |
|
|---|
| 163 | if (Crates[ActiveCrate]->Synch()) PrintMessage("Synchronized crate %d\n", ActiveCrate);
|
|---|
| 164 | else PrintMessage("Failed to synchronize crate %d\n", ActiveCrate);
|
|---|
| 165 | }
|
|---|
| 166 |
|
|---|
| 167 | //
|
|---|
| 168 | // Select active crate
|
|---|
| 169 | //
|
|---|
| 170 | void User::cmd_crate() {
|
|---|
| 171 |
|
|---|
| 172 | int Crate;
|
|---|
| 173 |
|
|---|
| 174 | if (!ConvertToInt(Parameter[1], &Crate)) {
|
|---|
| 175 | PrintMessage("Error: Wrong number format\n");
|
|---|
| 176 | return;
|
|---|
| 177 | }
|
|---|
| 178 |
|
|---|
| 179 | // Check limits
|
|---|
| 180 | if (Crate<0 || Crate>=(int) Crates.size()) {
|
|---|
| 181 | PrintMessage("Crate number %d not existing\n", Crate);
|
|---|
| 182 | return;
|
|---|
| 183 | }
|
|---|
| 184 |
|
|---|
| 185 | ActiveCrate = Crate;
|
|---|
| 186 | }
|
|---|
| 187 |
|
|---|
| 188 | //
|
|---|
| 189 | // Set new bias voltage
|
|---|
| 190 | //
|
|---|
| 191 | void User::cmd_hv() {
|
|---|
| 192 |
|
|---|
| 193 | unsigned int Errors=0;
|
|---|
| 194 | double Double;
|
|---|
| 195 | struct Range Crt, Chan;
|
|---|
| 196 | vector< map<unsigned int, double> > Voltages (Crates.size());
|
|---|
| 197 |
|
|---|
| 198 | // Loop over all parameters
|
|---|
| 199 | for (unsigned int n=1; n < Parameter.size()-1; n+=2) {
|
|---|
| 200 |
|
|---|
| 201 | // Pixel identification?
|
|---|
| 202 | if (Match(Parameter[0], "pixel")) {
|
|---|
| 203 | // Skip if first character is not digit
|
|---|
| 204 | if (isdigit(Parameter[n][0] == 0)) continue;
|
|---|
| 205 | // Skip if pixel ID not existing
|
|---|
| 206 | if (PixMap->Pixel_to_HVcrate(atoi(Parameter[n].c_str())) == PixMap->PM_ERROR_CODE) continue;
|
|---|
| 207 |
|
|---|
| 208 | Crt.Min = Crt.Max = PixMap->Pixel_to_HVcrate(atoi(Parameter[n].c_str()));
|
|---|
| 209 | Chan.Min = Chan.Max = PixMap->Pixel_to_HVboard(atoi(Parameter[n].c_str()))*NUM_CHANNELS + PixMap->Pixel_to_HVchannel(atoi(Parameter[n].c_str()));
|
|---|
| 210 | }
|
|---|
| 211 | // Channel identification
|
|---|
| 212 | else {
|
|---|
| 213 | if (ActiveCrate == -1) continue;
|
|---|
| 214 | else Crt.Min = Crt.Max = ActiveCrate;
|
|---|
| 215 |
|
|---|
| 216 | Chan.Min = 0;
|
|---|
| 217 | Chan.Max = MAX_NUM_BOARDS*NUM_CHANNELS-1;
|
|---|
| 218 |
|
|---|
| 219 | if (!ConvertToRange(Parameter[n], Chan)) {
|
|---|
| 220 | PrintMessage("Numeric conversion or out-of-range error for parameter %d, skipping channel\n", n);
|
|---|
| 221 | continue;
|
|---|
| 222 | }
|
|---|
| 223 | }
|
|---|
| 224 |
|
|---|
| 225 | // Convert voltage value and check format
|
|---|
| 226 | if (!ConvertToDouble(Parameter[n+1], &Double)) {
|
|---|
| 227 | PrintMessage("Error: Wrong number format for voltage setting\n");
|
|---|
| 228 | continue;
|
|---|
| 229 | }
|
|---|
| 230 |
|
|---|
| 231 | // Loop over given crates and channels
|
|---|
| 232 | for (int i=Crt.Min; i<=Crt.Max; i++) for (int j=Chan.Min; j<=Chan.Max; j++) {
|
|---|
| 233 | // Check that indices are in legal range
|
|---|
| 234 | if (i<0 || i>=(int) Crates.size() || j<0 || j >=MAX_NUM_BOARDS*NUM_CHANNELS) continue;
|
|---|
| 235 |
|
|---|
| 236 | // Voltage change (number starts with + oder -) ignored if current DAC value is zero
|
|---|
| 237 | if (isdigit(Parameter[n+1][0])==0 && Crates[i]->GetDAC(j) == 0) continue;
|
|---|
| 238 |
|
|---|
| 239 | // Relative or absolute change?
|
|---|
| 240 | if (isdigit(Parameter[n+1][0]) == 0) Voltages[i][j] = Crates[i]->GetVoltage(j) + Double;
|
|---|
| 241 | else Voltages[i][j] = Double;
|
|---|
| 242 | } // Channels
|
|---|
| 243 | } // Loop over command argument
|
|---|
| 244 |
|
|---|
| 245 | // Ramp voltages and update DIM services
|
|---|
| 246 | for (unsigned int i=0; i<Voltages.size(); i++) {
|
|---|
| 247 | Errors += RampVoltages(i, Voltages[i]);
|
|---|
| 248 | Crates[i]->UpdateDIM();
|
|---|
| 249 | }
|
|---|
| 250 |
|
|---|
| 251 | // Error message only if not yet too many errors
|
|---|
| 252 | if (Errors > 0) {
|
|---|
| 253 | for (unsigned int i=0; i<Crates.size(); i++) {
|
|---|
| 254 | if (Crates[i]->ErrorCount > MAX_ERR_COUNT) return;
|
|---|
| 255 | }
|
|---|
| 256 | Message(ERROR, "%d errors occurred from SetChannels()", Errors);
|
|---|
| 257 | }
|
|---|
| 258 | }
|
|---|
| 259 |
|
|---|
| 260 | //
|
|---|
| 261 | // Load bias settings from file
|
|---|
| 262 | //
|
|---|
| 263 | void User::cmd_load() {
|
|---|
| 264 |
|
|---|
| 265 | char Buffer[MAX_COM_SIZE];
|
|---|
| 266 | int Errors = 0, Channel;
|
|---|
| 267 | double Value;
|
|---|
| 268 | FILE *File;
|
|---|
| 269 | map<unsigned int, double> Voltages;
|
|---|
| 270 |
|
|---|
| 271 | // Open file
|
|---|
| 272 | if ((File=fopen(Parameter[1].c_str(), "r")) == NULL) {
|
|---|
| 273 | PrintMessage("Error: Could not open file '%s' (%s)\n", Parameter[1].c_str(), strerror(errno));
|
|---|
| 274 | return;
|
|---|
| 275 | }
|
|---|
| 276 |
|
|---|
| 277 | // Scan through file line by line
|
|---|
| 278 | while (fgets(Buffer, sizeof(Buffer), File) != NULL) {
|
|---|
| 279 | for (unsigned int Crate=0; Crate<Crates.size(); Crate++) if (Match(Crates[Crate]->Name, Buffer)) {
|
|---|
| 280 |
|
|---|
| 281 | if ((int) Crate != ActiveCrate) continue;
|
|---|
| 282 | PrintMessage("Found bias settings for crate %s (#%d)\n\r", Crates[Crate]->Name, Crate);
|
|---|
| 283 |
|
|---|
| 284 | Voltages.clear();
|
|---|
| 285 | Channel = 0;
|
|---|
| 286 | while (fscanf(File, "%lf", &Value)==1 && Channel<MAX_NUM_BOARDS*NUM_CHANNELS) {
|
|---|
| 287 | Voltages[Channel++] = Value;
|
|---|
| 288 | }
|
|---|
| 289 |
|
|---|
| 290 | // Ramp channels
|
|---|
| 291 | Errors += RampVoltages(Crate, Voltages);
|
|---|
| 292 |
|
|---|
| 293 | // Update DIM service
|
|---|
| 294 | Crates[Crate]->UpdateDIM();
|
|---|
| 295 |
|
|---|
| 296 | if (ferror(File) != 0) {
|
|---|
| 297 | PrintMessage("Error reading DAC value from file, terminating. (%s)\n",strerror(errno));
|
|---|
| 298 | return;
|
|---|
| 299 | }
|
|---|
| 300 | else PrintMessage("\nFinished updating board\n");
|
|---|
| 301 | } // Loop over boards
|
|---|
| 302 | } // while()
|
|---|
| 303 |
|
|---|
| 304 | if (Errors != 0) PrintMessage("Warning: %d error(s) occurred\n", Errors);
|
|---|
| 305 | if (fclose(File) != 0) PrintMessage("Error: Could not close file '%s'\n", Parameter[1].c_str());
|
|---|
| 306 | }
|
|---|
| 307 |
|
|---|
| 308 | //
|
|---|
| 309 | // Set refresh rate
|
|---|
| 310 | //
|
|---|
| 311 | void User::cmd_rate() {
|
|---|
| 312 |
|
|---|
| 313 | double Rate;
|
|---|
| 314 |
|
|---|
| 315 | if (!ConvertToDouble(Parameter[1], &Rate)) {
|
|---|
| 316 | PrintMessage("Error: Wrong number format\n");
|
|---|
| 317 | return;
|
|---|
| 318 | }
|
|---|
| 319 |
|
|---|
| 320 | // Check limits
|
|---|
| 321 | if (Rate<MIN_RATE || Rate>MAX_RATE) {
|
|---|
| 322 | PrintMessage("Refresh rate out of range (min: %.2f Hz, max: %.2f Hz)\n", MIN_RATE, MAX_RATE);
|
|---|
| 323 | return;
|
|---|
| 324 | }
|
|---|
| 325 |
|
|---|
| 326 | fStatusRefreshRate = Rate;
|
|---|
| 327 | PrintMessage("Refresh rate set to %.2f Hz\n", fStatusRefreshRate);
|
|---|
| 328 | }
|
|---|
| 329 |
|
|---|
| 330 | //
|
|---|
| 331 | // Reset crates
|
|---|
| 332 | //
|
|---|
| 333 | void User::cmd_reset() {
|
|---|
| 334 |
|
|---|
| 335 | if (ActiveCrate == -1) return;
|
|---|
| 336 |
|
|---|
| 337 | if (Crates[ActiveCrate]->SystemReset() == 1) PrintMessage("System reset of crate %d\n", ActiveCrate);
|
|---|
| 338 | else PrintMessage("Error: Could not reset crate %d\n", ActiveCrate);
|
|---|
| 339 |
|
|---|
| 340 | }
|
|---|
| 341 |
|
|---|
| 342 | //
|
|---|
| 343 | // Read channel
|
|---|
| 344 | //
|
|---|
| 345 | void User::cmd_gs() {
|
|---|
| 346 |
|
|---|
| 347 | double Voltage;
|
|---|
| 348 |
|
|---|
| 349 | if (ActiveCrate == -1) return;
|
|---|
| 350 |
|
|---|
| 351 | if (!ConvertToDouble(Parameter[1], &Voltage)) {
|
|---|
| 352 | PrintMessage("Error: Wrong number format\n");
|
|---|
| 353 | return;
|
|---|
| 354 | }
|
|---|
| 355 |
|
|---|
| 356 | if (Crates[ActiveCrate]->GlobalSet(Voltage) != 1) {
|
|---|
| 357 | PrintMessage("Error: Could not global set crate %d\n", ActiveCrate);
|
|---|
| 358 | }
|
|---|
| 359 | }
|
|---|
| 360 |
|
|---|
| 361 | //
|
|---|
| 362 | // Save bias settings of all boards
|
|---|
| 363 | //
|
|---|
| 364 | void User::cmd_save() {
|
|---|
| 365 |
|
|---|
| 366 | FILE *File;
|
|---|
| 367 | time_t Time = time(NULL);
|
|---|
| 368 |
|
|---|
| 369 | if ((File = fopen(Parameter[1].c_str(), "w")) == NULL) {
|
|---|
| 370 | PrintMessage("Error: Could not open file '%s' (%s)\n", Parameter[1].c_str(), strerror(errno));
|
|---|
| 371 | return;
|
|---|
| 372 | }
|
|---|
| 373 |
|
|---|
| 374 | fprintf(File,"********** Bias settings of %s **********\n\n", ctime(&Time));
|
|---|
| 375 |
|
|---|
| 376 | for (unsigned int i=0; i<Crates.size(); i++) {
|
|---|
| 377 | fprintf(File, "%s\n\n", Crates[i]->Name);
|
|---|
| 378 |
|
|---|
| 379 | for (int j=0; j<MAX_NUM_BOARDS*NUM_CHANNELS; j++) fprintf(File,"%.3f ",Crates[i]->GetVoltage(j));
|
|---|
| 380 | fprintf(File, "\n");
|
|---|
| 381 | }
|
|---|
| 382 |
|
|---|
| 383 | if (fclose(File) != 0) {
|
|---|
| 384 | PrintMessage("Error: Could not close file '%s' (%s)\n", Parameter[1].c_str(), strerror(errno));
|
|---|
| 385 | }
|
|---|
| 386 | }
|
|---|
| 387 |
|
|---|
| 388 | //
|
|---|
| 389 | // Set operation mode
|
|---|
| 390 | //
|
|---|
| 391 | void User::cmd_mode() {
|
|---|
| 392 |
|
|---|
| 393 | if (Match(Parameter[1], "static")) Mode = mode_static;
|
|---|
| 394 | else {
|
|---|
| 395 | Mode = mode_dynamic;
|
|---|
| 396 | for (unsigned int i=0; i<Crates.size(); i++) {
|
|---|
| 397 | Crates[i]->SetRefCurrent();
|
|---|
| 398 | }
|
|---|
| 399 | }
|
|---|
| 400 | }
|
|---|
| 401 |
|
|---|
| 402 | //
|
|---|
| 403 | // Print status
|
|---|
| 404 | //
|
|---|
| 405 | void User::cmd_status() {
|
|---|
| 406 |
|
|---|
| 407 | PrintMessage(" Number of crates: %d\n", Crates.size());
|
|---|
| 408 | PrintMessage(" Active crate: %d\n", ActiveCrate);
|
|---|
| 409 | PrintMessage(" Refresh rate: %.2f Hz\n", fStatusRefreshRate);
|
|---|
| 410 | PrintMessage(" Time out: %.2f s\n\n", fTimeOut);
|
|---|
| 411 | PrintMessage(" MaxDiff : %u\n", fMaxDiff);
|
|---|
| 412 |
|
|---|
| 413 | for (unsigned int i=0; i<Crates.size(); i++) {
|
|---|
| 414 | PrintMessage(" CRATE %d (%s)\n Wrap counter: %s (%d) Reset: %s Error count: %d\n ",
|
|---|
| 415 | i, Crates[i]->Name, Crates[i]->WrapOK ? "ok":"error", Crates[i]->WrapCount,
|
|---|
| 416 | Crates[i]->ResetHit ? "yes" : "no", Crates[i]->ErrorCount);
|
|---|
| 417 |
|
|---|
| 418 | if (Parameter.size() == 1) PrintMessage("Channel voltages (in V)");
|
|---|
| 419 | else if (Match(Parameter[1], "dac")) PrintMessage("Channel voltages (in DAC values)");
|
|---|
| 420 | else PrintMessage("Channel currents (in uA)");
|
|---|
| 421 |
|
|---|
| 422 | for (int j=0; j<MAX_NUM_BOARDS*NUM_CHANNELS; j++) {
|
|---|
| 423 | if (j%12 == 0) PrintMessage("\n%3.1d: ", j);
|
|---|
| 424 | if (!Crates[i]->Present[j/NUM_CHANNELS][j%NUM_CHANNELS]) PrintMessage(" - ");
|
|---|
| 425 | else if (Parameter.size() == 1) PrintMessage("%#5.2f ",Crates[i]->GetVoltage(j));
|
|---|
| 426 | else if (Match(Parameter[1], "dac")) PrintMessage("%5d ", Crates[i]->GetDAC(j));
|
|---|
| 427 | else PrintMessage("%#5.2f %s ", Crates[i]->GetCurrent(j), Crates[i]->OC[j/NUM_CHANNELS][j%NUM_CHANNELS] ? "OC":"");
|
|---|
| 428 | }
|
|---|
| 429 | PrintMessage("\n");
|
|---|
| 430 | }
|
|---|
| 431 | }
|
|---|
| 432 |
|
|---|
| 433 | //
|
|---|
| 434 | // Set timeout to return from read
|
|---|
| 435 | //
|
|---|
| 436 | void User::cmd_timeout() {
|
|---|
| 437 |
|
|---|
| 438 | double Timeout;
|
|---|
| 439 |
|
|---|
| 440 | if (!ConvertToDouble(Parameter[1], &Timeout)) {
|
|---|
| 441 | PrintMessage("Error: Wrong number format\n");
|
|---|
| 442 | return;
|
|---|
| 443 | }
|
|---|
| 444 |
|
|---|
| 445 | fTimeOut = Timeout;
|
|---|
| 446 | PrintMessage("Timeout set to %.2f s\n", fTimeOut);
|
|---|
| 447 | }
|
|---|
| 448 |
|
|---|
| 449 | //
|
|---|
| 450 | // Exit program (Signal makes readline return and sets ExitRequest)
|
|---|
| 451 | //
|
|---|
| 452 | void User::cmd_exit() {
|
|---|
| 453 |
|
|---|
| 454 | pthread_kill(MainThread, SIGTERM);
|
|---|
| 455 | }
|
|---|
| 456 |
|
|---|
| 457 |
|
|---|
| 458 | //
|
|---|
| 459 | // Print message to screen and to DIM text service
|
|---|
| 460 | //
|
|---|
| 461 | void User::PrintMessage(const char *Format, ...) {
|
|---|
| 462 |
|
|---|
| 463 | static char Error[] = "vasprintf() failed in PrintMessage()";
|
|---|
| 464 | char *Text;
|
|---|
| 465 |
|
|---|
| 466 | // Evaluate arguments
|
|---|
| 467 | va_list ArgumentPointer;
|
|---|
| 468 | va_start(ArgumentPointer, Format);
|
|---|
| 469 | if (vasprintf(&Text, Format, ArgumentPointer) == -1) Text = Error;
|
|---|
| 470 | va_end(ArgumentPointer);
|
|---|
| 471 |
|
|---|
| 472 | // Print to console
|
|---|
| 473 | printf("%s", Text); // New prompt
|
|---|
| 474 | fflush(stdout);
|
|---|
| 475 | if (strlen(Text)>0 && Text[strlen(Text)-1]=='\n') rl_on_new_line(); // New prompt
|
|---|
| 476 |
|
|---|
| 477 | // Send to DIM text service
|
|---|
| 478 | ConsoleOut->updateService(Text);
|
|---|
| 479 |
|
|---|
| 480 | // Free old text
|
|---|
| 481 | if (ConsoleText != Error) free(ConsoleText);
|
|---|
| 482 | ConsoleText = Text;
|
|---|
| 483 | }
|
|---|
| 484 |
|
|---|
| 485 |
|
|---|
| 486 | // Ramp to new voltage with maximum step size given in fMaxDiff
|
|---|
| 487 | // No ramping when decreasing voltage
|
|---|
| 488 | unsigned int User::RampVoltages(int Crate, map<unsigned int, double> Voltages) {
|
|---|
| 489 |
|
|---|
| 490 | map<unsigned int, double> Target;
|
|---|
| 491 | int Errors = 0;
|
|---|
| 492 |
|
|---|
| 493 | // Ramp until all channels at desired value
|
|---|
| 494 | while (!Voltages.empty() && Errors < MAX_ERR_COUNT) {
|
|---|
| 495 | // Remove channels already at target (check for DAC, not for floating-point voltage)
|
|---|
| 496 | for (map<unsigned int, double>::iterator it = Voltages.begin(); it != Voltages.end(); ++it) {
|
|---|
| 497 | //if (Crates[Crate]->GetDAC(it->first) == (unsigned int ) (it->second/90.0*0x0fff)) Voltages.erase(it);
|
|---|
| 498 | if (fabs(Crates[Crate]->GetVoltage(it->first)-it->second) < 0.001) Voltages.erase(it);
|
|---|
| 499 | }
|
|---|
| 500 |
|
|---|
| 501 | // Limit voltage changes to fMaxDiff
|
|---|
| 502 | Target = Voltages;
|
|---|
| 503 | for (map<unsigned int, double>::iterator it = Target.begin(); it != Target.end(); ++it) {
|
|---|
| 504 | if (Crates[Crate]->GetVoltage(it->first) + fMaxDiff < it->second) {
|
|---|
| 505 | it->second = Crates[Crate]->GetVoltage(it->first) + fMaxDiff;
|
|---|
| 506 | }
|
|---|
| 507 | }
|
|---|
| 508 |
|
|---|
| 509 | // Set channels to next target and wait 10 ms
|
|---|
| 510 | if (Crates[Crate]->SetChannels(Target) != 1) Errors++;
|
|---|
| 511 | usleep(10000);
|
|---|
| 512 | }
|
|---|
| 513 |
|
|---|
| 514 | return Errors;
|
|---|
| 515 | }
|
|---|
| 516 |
|
|---|
| 517 |
|
|---|
| 518 | //
|
|---|
| 519 | // Check status
|
|---|
| 520 | //
|
|---|
| 521 | void User::Monitor() {
|
|---|
| 522 |
|
|---|
| 523 | static bool Warned = false;
|
|---|
| 524 | int Ret;
|
|---|
| 525 |
|
|---|
| 526 | while (!ExitRequest) {
|
|---|
| 527 |
|
|---|
| 528 | // Wait (this is the only allowed cancelation point)
|
|---|
| 529 | if ((Ret=pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL)) != 0) {
|
|---|
| 530 | Message(FATAL, "pthread_setcancelstate() failed (%s)", strerror(Ret));
|
|---|
| 531 | }
|
|---|
| 532 | if (usleep((unsigned long) floor(1000000./fStatusRefreshRate)) == -1) {
|
|---|
| 533 | Message(FATAL, "usleep() failed (%s)", strerror(errno));
|
|---|
| 534 | }
|
|---|
| 535 | if ((Ret=pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL)) != 0) {
|
|---|
| 536 | Message(FATAL, "pthread_setcancelstate() failed (%s)", strerror(Ret));
|
|---|
| 537 | }
|
|---|
| 538 |
|
|---|
| 539 | // Check all crates
|
|---|
| 540 | for (unsigned int i=0; i<Crates.size(); i++) {
|
|---|
| 541 | if (Crates[i]->ErrorCount > MAX_ERR_COUNT) {
|
|---|
| 542 | if (!Warned) {
|
|---|
| 543 | Warned = true;
|
|---|
| 544 | Message(WARN, "Crate %d has many read/write errors, further error reporting disabled", i);
|
|---|
| 545 | }
|
|---|
| 546 | continue;
|
|---|
| 547 | }
|
|---|
| 548 |
|
|---|
| 549 | if (Crates[i]->ResetHit) {
|
|---|
| 550 | Message(INFO, "Manual reset of board %d, setting voltages to zero and issuing system reset", i);
|
|---|
| 551 | Crates[i]->GlobalSet(0);
|
|---|
| 552 | Crates[i]->SystemReset();
|
|---|
| 553 | }
|
|---|
| 554 |
|
|---|
| 555 | if (!Crates[i]->WrapOK) {
|
|---|
| 556 | Message(ERROR, "Wrap counter mismatch of board %d", i);
|
|---|
| 557 | }
|
|---|
| 558 |
|
|---|
| 559 | if (Crates[i]->ReadAll() != 1) {
|
|---|
| 560 | Message(ERROR, "Monitor could not read status from crate %d", i);
|
|---|
| 561 | continue;
|
|---|
| 562 | }
|
|---|
| 563 |
|
|---|
| 564 | map<unsigned int, double> Voltages;
|
|---|
| 565 |
|
|---|
| 566 | for (int j=0; j<MAX_NUM_BOARDS*NUM_CHANNELS; j++) {
|
|---|
| 567 | if (Crates[i]->OC[j/NUM_CHANNELS][j%NUM_CHANNELS]) {
|
|---|
| 568 | Message(WARN, "Overcurrent on crate %d, board %d, channel %d, setting voltage to zero", i, j/NUM_CHANNELS, j%NUM_CHANNELS);
|
|---|
| 569 | Voltages[j] = 0;
|
|---|
| 570 | }
|
|---|
| 571 | }
|
|---|
| 572 | if (!Voltages.empty()) {
|
|---|
| 573 | Crates[i]->SetChannels(Voltages);
|
|---|
| 574 | Crates[i]->SystemReset();
|
|---|
| 575 | }
|
|---|
| 576 |
|
|---|
| 577 | if (Mode == mode_dynamic) Crates[i]->AdaptVoltages();
|
|---|
| 578 | } // for
|
|---|
| 579 | } // while
|
|---|
| 580 | }
|
|---|
| 581 |
|
|---|
| 582 | // Call monitor loop inside class
|
|---|
| 583 | void User::LaunchMonitor(User *m) {
|
|---|
| 584 |
|
|---|
| 585 | m->Monitor();
|
|---|
| 586 | }
|
|---|
| 587 |
|
|---|
| 588 |
|
|---|
| 589 | //
|
|---|
| 590 | // Check if two strings match (min 1 character must match)
|
|---|
| 591 | //
|
|---|
| 592 | bool User::Match(string str, const char *cmd) {
|
|---|
| 593 |
|
|---|
| 594 | return strncasecmp(str.c_str(),cmd,strlen(str.c_str())==0 ? 1:strlen(str.c_str())) ? false:true;
|
|---|
| 595 | }
|
|---|
| 596 |
|
|---|
| 597 | //
|
|---|
| 598 | // Conversion function from string to double or int
|
|---|
| 599 | //
|
|---|
| 600 | // Return false if conversion did not stop on whitespace or EOL character
|
|---|
| 601 | bool User::ConvertToDouble(string String, double *Result) {
|
|---|
| 602 |
|
|---|
| 603 | char *EndPointer;
|
|---|
| 604 |
|
|---|
| 605 | *Result = strtod(String.c_str(), &EndPointer);
|
|---|
| 606 | if(!isspace(*EndPointer) && *EndPointer!='\0') return false;
|
|---|
| 607 | return true;
|
|---|
| 608 | }
|
|---|
| 609 |
|
|---|
| 610 | bool User::ConvertToInt(string String, int *Result) {
|
|---|
| 611 |
|
|---|
| 612 | char *EndPointer;
|
|---|
| 613 |
|
|---|
| 614 | *Result = (int) strtol(String.c_str(), &EndPointer, 0);
|
|---|
| 615 | if(!isspace(*EndPointer) && *EndPointer!='\0') return false;
|
|---|
| 616 | return true;
|
|---|
| 617 | }
|
|---|
| 618 |
|
|---|
| 619 | //
|
|---|
| 620 | // Interprets a range
|
|---|
| 621 | //
|
|---|
| 622 | bool User::ConvertToRange(string String, struct User::Range &R) {
|
|---|
| 623 |
|
|---|
| 624 | int N=0, M=0; // Init to avoid compiler warning
|
|---|
| 625 |
|
|---|
| 626 | // Full range
|
|---|
| 627 | if (Match(String, "all")) return true;
|
|---|
| 628 |
|
|---|
| 629 | // Single number
|
|---|
| 630 | if (ConvertToInt(String, &N)) {
|
|---|
| 631 | if (N>= R.Min && N<=R.Max) {
|
|---|
| 632 | R.Max = R.Min = N;
|
|---|
| 633 | return true;
|
|---|
| 634 | }
|
|---|
| 635 | return false;
|
|---|
| 636 | }
|
|---|
| 637 |
|
|---|
| 638 | // Range a-b
|
|---|
| 639 | vector<string> V = EvidenceServer::Tokenize(String, "-");
|
|---|
| 640 | if (V.size()==2 && ConvertToInt(V[0], &N) && ConvertToInt(V[1], &M) && N>=R.Min && M<=R.Max) {
|
|---|
| 641 | R.Min = N;
|
|---|
| 642 | R.Max = M;
|
|---|
| 643 | return true;
|
|---|
| 644 | }
|
|---|
| 645 |
|
|---|
| 646 | return false;
|
|---|
| 647 | }
|
|---|