]> jspc29.x-matter.uni-frankfurt.de Git - hades_mdc_settings.git/commitdiff
Various additions to power scripts
authorJan Michel <michel@physik.uni-frankfurt.de>
Mon, 5 Jun 2023 14:23:58 +0000 (16:23 +0200)
committerJan Michel <michel@physik.uni-frankfurt.de>
Mon, 5 Jun 2023 14:23:58 +0000 (16:23 +0200)
- reuse existing TCP connection
- add powerboard_status to see all settings of a power board
- add reverse lookup for powerboard outputs -> mbo addresses

scripts/MDC.pm
scripts/MDCPower.pm
scripts/mdc_voltage.pl
scripts/powerboard_status.pl [new file with mode: 0755]

index e12786e29eacede532aa070dd0a82c1cc03fb361..1f1fd572dc404d778fa9d810c08b2cf761e5fd40 100644 (file)
@@ -108,6 +108,44 @@ sub get_power_output {
      
   return($hostname,-1,-1);
   }
+
+  
+###############################################################################  
+#get the MBO a power output is connected to.
+#returns (boardaddress)
+###############################################################################
+sub get_connected_board {
+  my ($h,$b,$o) = @_;
+
+  my $pd_p; 
+  my $pd_s;
+  
+  open FILE, "../installation/powerdistributor_positions.db" or die $!."\npowerdistributor_positions.db not found.";
+  while (my $a = <FILE>) {
+    if(my @values = $a =~ /^\s*(\d)\s+(\d)\s+(\d+)/) {
+      $pd_p  = shift @values;
+      $pd_s  = shift @values;
+      $id    = shift @values;
+      last if "mdcpd".$id eq $h;
+      }
+    }
+  close FILE;  
+
+     
+  open FILE, "../installation/power_outputs.db" or die $!."\npower_outputs.db not found.";
+  while (my $a = <FILE>) {
+    if(my @values = $a =~ /^\s*(\d)\s+(\d)\s+(\d+)\s+(\d)\s+(\d)/) {
+      if($values[0] == $pd_p && $values[1] == $pd_s && $values[3] == $b && $values[4] == $o) {
+        return(get_address($pd_p,$pd_s,$values[2],5));
+        }
+      }
+    }
+     
+  close FILE;   
+     
+  return(-1);
+  }
+  
   
 1;
 
index 8b4ab65d234c363dc0f4c75624808a6775808906..4ff4602f853db1da64e2db5c183abbe6e8a160e1 100644 (file)
@@ -12,8 +12,8 @@ use IO::Handle;
 use Time::HiRes qw( usleep);
 
 my $powerboard_registers = {
-  'switch' => 0,
-  'adjust' => 1,
+  'switch' => 0, 'sw' => 0,
+  'adjust' => 1, 'adj' => 1,
   'vin' => 2,
   'cin' => 3,   'curr' => 3,  'current' => 3,
   'temp' => 4,
@@ -166,9 +166,11 @@ sub powerboard_command {
   
   my $cmd = sprintf("%s%02x%01x%01x%01x%04x",$rw,$board&0xfF,$output,$channel,$command,$value&0xFFFF);
   my $ret = powerboard_SendCmd($host,$cmd);
+#   print $ret."\n";
   return -7 unless defined $ret;
   return -8 unless length($ret)==10;
-  return -9 unless substr($ret,0,1) eq 'A';
+  return -9 if substr($ret,0,3) eq 'Can';
+  return -10 unless substr($ret,0,1) eq 'A';
   
   my $answ = hex(substr($ret,6));
   
@@ -176,14 +178,14 @@ sub powerboard_command {
     return 1;
     }
   elsif($rw eq 'W') {
-    return -10;
+    return -11;
     }
   else {  
     if($command == 0) { #Active
       $ret = $answ & 1;
       }  
     if($command == 1) { #Level Select
-      $ret = $answ & 1;
+      $ret = $answ & 7;
       }
     if($command == 2) { #Input Voltage
       $ret = ($answ&0xFFFF)*417/27*2.5/1000;
@@ -205,17 +207,25 @@ sub powerboard_command {
   return $ret;
   }
 
+  
+my $port;  
+my $portHost = 0;
 ###############################################################################  
 #RX/TX subroutine. not to be called directly
 ###############################################################################    
 sub powerboard_SendCmd {
   my ($device,$c) = @_;
-  my $port = IO::Socket::INET->new(PeerAddr => $device, PeerPort => 2323, Proto => "tcp", Type => SOCK_STREAM, Timeout => 1, Blocking => 0) 
-              or return "Can't bind to $device: $@\n";  
+  unless ($portHost eq $device && $port->connected()) {
+    $port->close() if $port;
+    $port = IO::Socket::INET->new(PeerAddr => $device, PeerPort => 2323, Proto => "tcp", Type => SOCK_STREAM, Timeout => 1, Blocking => 0) 
+              or (print "Can't bind to $device: $@\n" and return);  
+    }
+  $portHost = $device;
+  
   $c .= "\n";
   print $port  "$c";
   my $x = "";
-  for my $i (0..20) {
+  for my $i (0..40) {
     $x .=  <$port>//'';
     if($x && (substr($x,-1) eq "\n" || substr($x,-1) eq "\r" || length($x)>=10) ) {
       chomp $x;
index 46c30c2a86b7b39d1e7c89ca389fe40fc988c5ef..f7d2496c2d514fb914ae14b3903d7d2661779408 100755 (executable)
@@ -119,10 +119,10 @@ elsif(defined $board && !defined $channel) {
   printf("%04x\t%u\t%u\n\n",$board,$v1,$v2);
   
   if(defined $load) {
-    MDCPower::set_voltage($board,0,$v1,0);
-    die "Setting voltage failed $_" unless $_ == 1;
-    MDCPower::set_voltage($board,1,$v2,0);
-    die "Setting voltage failed $_" unless $_ == 1;
+    my $ret = MDCPower::set_voltage($board,0,$v1,0);
+    die "Setting voltage failed $ret" unless $ret == 1;
+    $ret = MDCPower::set_voltage($board,1,$v2,0);
+    die "Setting voltage failed $ret" unless $ret == 1;
     }
   }
 
diff --git a/scripts/powerboard_status.pl b/scripts/powerboard_status.pl
new file mode 100755 (executable)
index 0000000..527eaa0
--- /dev/null
@@ -0,0 +1,115 @@
+#!/usr/bin/perl -w
+use warnings;
+no warnings "portable";
+use Getopt::Long;
+use Data::Dumper;
+use Time::HiRes qw(usleep);
+use lib '.';
+use MDC;
+use MDCPower;
+no warnings 'numeric';
+
+my $help;
+my $board;
+my $plane;
+my $sector;
+Getopt::Long::Configure(qw(gnu_getopt));
+GetOptions(
+           'help|h'      => \$help,
+           'board|b=s'   => \$board,
+           'plane|p=s'   => \$plane,
+           'sector|s=s'   => \$sector,
+           );
+           
+if($help) {
+print <<HELP;
+Prints the status of the whole power assembly of the chamber the board address belongs to.
+
+Options
+=======
+  -b 0xnnnn    Board address
+  -p (0..3)    Plane
+  -s (0..5)    Sector
+
+All values are given in units °C, mV, mA or unitless.
+
+
+HELP
+
+exit;
+}
+
+
+
+$board = hex($board) if defined $board;
+if(defined $board && ($board < 0x8e00 || $board > 0x8fff)) {
+  die "Wrong board address (8e00 - 8fff)\n";
+  }
+if(defined $plane && ($plane < 0 || $plane > 3)) {
+  die "Wrong plane number\n";
+  } 
+if(defined $sector && ($sector < 0 || $sector > 5)) {
+  die "Wrong sector number\n";
+  }
+
+my $host;
+$board = MDC::get_address($plane,$sector,0,5) if defined $plane;
+($host) = MDC::get_power_output($board) if defined $board;
+
+if(!defined $host || $host == -1) {
+  die "Powerboard not found\n";
+  }
+
+my $data;
+
+
+$data->[4]{temp}  = MDCPower::powerboard_command($host,-1,0,0,"temp");
+for my $b (0..3) {
+  $data->[$b]{curr} = MDCPower::powerboard_command($host,$b,0,0,"curr"); 
+  $data->[$b]{vin}  = MDCPower::powerboard_command($host,$b,0,0,"vin"); 
+  $data->[$b]{temp}  = MDCPower::powerboard_command($host,$b,0,0,"temp");
+  
+  for my $o (0..3) {
+    $data->[$b]{vout}[$o]  = MDCPower::powerboard_command($host,$b,$o,0,"vout");
+    $data->[$b]{switch}[$o]  = MDCPower::powerboard_command($host,$b,$o,0,"switch");
+    for my $c (0..1) {
+      $data->[$b]{adjust}[$o][$c]  = MDCPower::powerboard_command($host,$b,$o,$c,"adjust");
+      }
+    }
+  }
+
+my ($mdcp,$mdcs) = MDC::get_position($board);
+  
+printf("\nMDC Powerboard \t Plane $mdcp \t Sector $mdcs\n");
+
+my $t = sprintf("%.1f",$data->[4]{temp});
+printf("\t Board $host\t Temperature %s\n\n",$data->[4]{temp}<0?'--':$t);
+
+print("Board\tInput\tVoltage\tCurrent\tTemp\n");
+for my $b (0..3) {
+  my $a1 = sprintf("%.2f",$data->[$b]{vin});
+  my $a2 = sprintf("%.2f",$data->[$b]{curr});
+  my $a3 = sprintf("%.1f",$data->[$b]{temp});
+
+  printf("$b\t\t%s\t%s\t%s\n",$data->[$b]{vin}<0?'--':$a1,
+                              $data->[$b]{curr}<0?'--':$a2,
+                              $data->[$b]{temp}<0?'--':$a3);
+  }
+print("\n\n");
+print("Output\tVoltage\tSet1\tSet2\tAddr\n");
+
+for my $b (0..3) {
+  for my $o (0..3) {
+    my $a1 = sprintf("%.2f",$data->[$b]{vout}[$o]);
+    if ($data->[$b]{switch}[$o]==0) {$data->[$b]{vout}[$o] = -20;}
+    my $a2 = sprintf("%i",$data->[$b]{adjust}[$o][0]);
+    my $a3 = sprintf("%i",$data->[$b]{adjust}[$o][1]);  
+    my $a4 = sprintf("%04x",MDC::get_connected_board($host,$b,$o));
+    
+    printf("$b  $o\t%s\t%s\t%s\t%.4s\n",$data->[$b]{vout}[$o]<0?'--':$a1
+                                       ,$data->[$b]{adjust}[$o][0]<0?'--':$a2
+                                       ,$data->[$b]{adjust}[$o][1]<0?'--':$a3
+                                       ,$a4
+      );
+    }
+  }