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