Instrument/Vectormagnet.pm untested

filter: Statementstmt 0.0% 0 / 377 Branchbran 0.0% 0 / 96 Conditioncond 0.0% 0 / 30 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 0.0% 0 / 25 Podpod 0.0% 0 / 18 totaltotal 0.0% 0 / 562 CC 49 SCAR 43.5
CC
49
Cov
0%
config_CIRC_sweep
182.0
config_DIR_sweep
156.0
get_field
72.0
CRAP 77.4
1 package Lab::Instrument::Vectormagnet;
2 #ABSTRACT: ???
3 $Lab::Instrument::Vectormagnet::VERSION = '3.899';
4 use v5.20;
5  
6 use strict;
7 use Time::HiRes qw/usleep/, qw/time/;
8 use Math::Trig;
9 use Lab::Instrument::IPS;
10 use Lab::XPRESS::hub;
11 use Lab::Generic;
12  
13 our @ISA = ('Lab::Generic');
14  
15 sub new {
16  
17 my $proto = shift;
18 my $class = ref($proto) || $proto;
19 my $self = $class->SUPER::new(@_);
20 my @args = @_;
21  
22 my $hub = new Lab::XPRESS::hub();
23  
24 # init the three IPS-instuments for the 3d-vector magnet:
25 if ( $args[0] =~ /GPIB|gpib/ ) {
26 my $gpib_board = 0;
27 my $gpib_address_x = 1;
28 my $gpib_address_y = 2;
29 my $gpib_address_z = 3;
30  
31 $self->{IPS_x} = new Lab::Instrument::IPS(
32 {
33 'connection_type' => 'VISA_GPIB',
34 'gpib_address' => $gpib_address_x
35 }
36 );
37 $self->{IPS_y} = new Lab::Instrument::IPS(
38 {
39 'connection_type' => 'VISA_GPIB',
40 'gpib_address' => $gpib_address_y
41 }
42 );
43 $self->{IPS_z} = new Lab::Instrument::IPS(
44 {
45 'connection_type' => 'VISA_GPIB',
46 'gpib_address' => $gpib_address_z
47 }
48 );
49 }
50 else {
51  
52 $self->{IPS_x} = new Lab::Instrument::IPS(
53 {
54 'connection' => $hub->Connection(
55 'VISA_RS232', { 'rs232_address' => 7 }
56 )
57 }
58 );
59 $self->{IPS_y} = new Lab::Instrument::IPS(
60 {
61 'connection' => $hub->Connection(
62 'VISA_RS232', { 'rs232_address' => 8 }
63 )
64 }
65 );
66 $self->{IPS_z} = new Lab::Instrument::IPS(
67 {
68 'connection' => $hub->Connection(
69 'VISA_RS232', { 'rs232_address' => 9 }
70 )
71 }
72 );
73 }
74  
75 # set limits:
76 $self->{IPS_x}->{LIMITS} = {
77 'magneticfield' => 1,
78 'field_intervall_limits' => [ 0, 1 ],
79 'rate_intervall_limits' => [ 0.6, 0.01 ]
80 };
81 $self->{IPS_y}->{LIMITS} = {
82 'magneticfield' => 1,
83 'field_intervall_limits' => [ 0, 1 ],
84 'rate_intervall_limits' => [ 0.6, 0.01 ]
85 };
86 $self->{IPS_z}->{LIMITS} = {
87 'magneticfield' => 1,
88 'field_intervall_limits' => [ 0, 1 ],
89 'rate_intervall_limits' => [ 0.6, 0.01 ]
90 };
91  
92 # init magnets:
93 $self->{IPS_x}->_init_magnet();
94 $self->{IPS_y}->_init_magnet();
95 $self->{IPS_z}->_init_magnet();
96  
97 # set ID:
98 $self->{IPS_x}->{ID} = 'IPS_x';
99 $self->{IPS_y}->{ID} = 'IPS_Y';
100 $self->{IPS_z}->{ID} = 'IPS_Z';
101  
102 # set xy-plane as the initial sweeping plane:
103 $self->{KAPPA} = 0;
104 $self->{RHO} = 0;
105 $self->{STARTING_SPEED} = 0.6;
106  
107 # register instrument:
108 push( @{ ${Lab::Instrument::INSTRUMENTS} }, $self );
109 return $self
110  
111 }
112  
113 sub create_header { }
114  
115 sub get_config_data {
116 my $self = shift;
117 return $self;
118 }
119  
120 sub abort {
121 my $self = shift;
122  
123 $self->{IPS_x}->abort();
124 $self->{IPS_y}->abort();
125 $self->{IPS_z}->abort();
126  
127 return;
128  
129 }
130  
131 sub trg {
132 my $self = shift;
133  
134 $self->{IPS_x}->trg();
135 $self->{IPS_y}->trg();
136 $self->{IPS_z}->trg();
137  
138 return;
139 }
140  
141 sub active {
142  
143 # returns a value > 0 if MAGNET is SWEEPING. Else MAGNET is not sweeping.
144 my $self = shift;
145  
146 my $active_x = $self->{IPS_x}->active();
147 my $active_y = $self->{IPS_y}->active();
148 my $active_z = $self->{IPS_z}->active();
149  
150 if ( $active_x or $active_y or $active_z ) {
151 return 1;
152 }
153 else {
154 return 0;
155 }
156  
157 }
158  
159 sub wait {
160  
161 # waits while magnets are sweeping
162 my $self = shift;
163 my $seconds = shift;
164 my $min = 0.5;
165  
166 my $time_0 = time();
167  
168 if ( not defined $seconds ) {
169 while ( $self->active() ) {
170  
171 #wait ...
172 }
173 return 0;
174 }
175 else {
176 while ( $self->active() ) {
177 my $time_1 = time();
178 if ( ( $time_1 - $time_0 ) > ( $seconds - $min ) ) {
179 if ( ( $seconds - ( $time_1 - $time_0 ) ) < 0 ) {
180 last;
181 }
182 usleep( ( $seconds - ( $time_1 - $time_0 ) ) * 1e6 );
183 last;
184 }
185 }
186 return 0;
187 }
188  
189 }
190  
191 sub get_value {
192 my $self = shift;
193 return $self->get_field(@_);
194 }
195  
196 sub get_field {
197 my $self = shift;
198 my ( $mode, $tail ) = $self->_check_args( \@_, ['mode'] );
199 my @field;
200  
201 if ( $tail->{read_mode} eq 'request' ) {
202 $self->{IPS_x}->get_field( { read_mode => 'request' } );
203 $self->{IPS_y}->get_field( { read_mode => 'request' } );
204 $self->{IPS_z}->get_field( { read_mode => 'request' } );
205 }
206  
207 my $x = $self->{IPS_x}->get_field();
208 my $y = $self->{IPS_y}->get_field();
209 my $z = $self->{IPS_z}->get_field();
210  
211 if ( not defined $mode ) {
212 $mode = 's';
213 }
214  
215 if ( $mode =~ /^(spherical|SPHERICAL|s|s)$/ ) {
216  
217 # returns BR, PHI, THETA:
218 my ( $r, $phi, $theta ) = cartesian_to_spherical( $x, $y, $z );
219 $phi = ( $phi / pi ) * 180;
220 $theta = ( $theta / pi ) * 180;
221 $self->{value} = [ $r, $theta, $phi ];
222 }
223 elsif ( $mode =~ /^(cartesian|CARTESIAN|C|c)$/ ) {
224  
225 # returns X, Y, Z:
226 $self->{value} = [ $x, $y, $z ];
227 }
228 elsif ( $mode =~ /^(all|ALL|A|a)$/ ) {
229  
230 # returns BR, PHI, THETA, X, Y, Z:
231 my ( $r, $phi, $theta ) = cartesian_to_spherical( $x, $y, $z );
232 $phi = ( $phi * 180 ) / pi;
233 $theta = ( $theta * 180 ) / pi;
234 $r
235 = ( ( $theta <= 90 ) and ( $phi > -90 and $phi <= 90 ) )
236 ? $r
237 : -$r;
238 $self->{value} = [ $r, $theta, $phi, $x, $y, $z ];
239 }
240  
241 if ( wantarray() ) {
242 return @{ $self->{value} };
243 }
244 else {
245 return $self->{value};
246 }
247  
248 }
249  
250 sub change_plane {
251 my $self = shift;
252 my $kappa = shift;
253 my $rho = shift;
254 my $starting_speed = shift;
255 if ( defined $kappa ) {
256 $self->{KAPPA} = $kappa;
257 }
258 if ( defined $rho ) {
259 $self->{RHO} = $rho;
260 }
261 if ( defined $starting_speed ) {
262 $self->{STARTING_SPEED} = $starting_speed;
263 }
264 }
265  
266 sub config_CIRC_sweep {
267 my $self = shift;
268  
269 my ( $B_R, $phi_start, $phi_stop, $v_phi, $interval, $resolution )
270 = $self->_check_args(
271 \@_,
272 [ 'b_r', 'phi_start', 'phi_stop', 'rate', 'interval', 'resolution' ]
273 );
274  
275 if ( not defined $interval ) {
276 $interval = 1;
277 }
278 if ( not defined $v_phi ) {
279 $v_phi = 1;
280 }
281 if ( not defined $resolution ) {
282 $resolution = ( $v_phi / 60 ) * 2 * $interval;
283 }
284 if ( not defined $phi_start ) {
285 $phi_start = -180;
286 }
287 if ( not defined $phi_stop ) {
288 $phi_stop = +180;
289 }
290 if ( not defined $B_R ) {
291 die "B_R is mandatory value in sub config_CIRC_sweep\n";
292 }
293  
294 my ( $x, $y, $z, $vx, $vy, $vz ) = $self->create_basic_trace(
295 $B_R, $phi_start, $phi_stop, $v_phi,
296 $resolution
297 );
298 my @x = @$x;
299 my @y = @$y;
300 my @z = @$z;
301 my @vx = @$vx;
302 my @vy = @$vy;
303 my @vz = @$vz;
304  
305 # my $l = @x;
306  
307 # use Lab::XPRESS::Data::XPRESS_DataFile;
308  
309 # my $file = new Lab::XPRESS::Data::XPRESS_DataFile('test.dat');
310 # $file->add_column('X');
311 # $file->add_column('Y');
312 # $file->add_column('Z');
313 # $file->add_column('VX');
314 # $file->add_column('VY');
315 # $file->add_column('VZ');
316  
317 # for (my $i = 0; $i < $l; $i++) {
318  
319 # $file->LOG({
320 # 'X' => $x[$i],
321 # 'Y' => $y[$i],
322 # 'Z' => $z[$i],
323 # 'VX' => $vx[$i],
324 # 'VY' => $vy[$i],
325 # 'VZ' => $vz[$i],
326 # });
327 # }
328  
329 # exit;
330  
331 my ( $x_c, $y_c, $z_c ) = $self->get_field('C');
332 if ( ( $x_c, $y_c, $z_c ) != ( $x[0], $y[0], $z[0] ) ) {
333 $self->config_DIR_sweep(
334 $x[0], $y[0], $z[0], $self->{STARTING_SPEED},
335 1, 'C'
336 );
337 print "Goto starting point...";
338 $self->trg();
339 $self->wait();
340 print " done\n";
341 }
342  
343 my @X = $self->{IPS_x}->config_sweep( \@x, \@vx, $interval );
344 my @Y = $self->{IPS_y}->config_sweep( \@y, \@vy, $interval );
345 my @Z = $self->{IPS_z}->config_sweep( \@z, \@vz, $interval );
346  
347 my @r;
348 my @phi;
349 my @theta;
350 my @dphi;
351 my $len_x = @X;
352 my $len_y = @Y;
353 my $len_z = @Z;
354 my $len = ( $len_x >= $len_y ) ? $len_x : $len_y;
355 $len = ( $len >= $len_z ) ? $len : $len_z;
356  
357 for ( my $i = 0; $i < $len; $i++ ) {
358 if ( ( my $len_x = @X ) >= $i ) {
359 push( @X, $X[-1] );
360 }
361 if ( ( my $len_y = @Y ) >= $i ) {
362 push( @Y, $Y[-1] );
363 }
364 if ( ( my $len_z = @Z ) >= $i ) {
365 push( @Z, $Z[-1] );
366 }
367 ( $r[$i], $phi[$i], $theta[$i] )
368 = cartesian_to_spherical( $X[$i], $Y[$i], $Z[$i] );
369 $phi[$i] = ( $phi[$i] * 180 ) / pi;
370 $theta[$i] = ( $theta[$i] * 180 ) / pi;
371 $dphi[$i] = $phi[$i] - $phi[0];
372 }
373  
374 return \@r, \@theta, \@phi, \@dphi, \@X, \@Y, \@Z;
375  
376 }
377  
378 sub config_DIR_sweep {
379 my $self = shift;
380  
381 my ( $B_R, $theta, $phi, $rate, $interval, $mode ) = $self->_check_args(
382 \@_,
383 [ 'b_r', 'theta', 'phi', 'rate', 'interval', 'mode' ]
384 );
385  
386 my ( $x_1, $y_1, $z_1 );
387  
388 if ( $mode =~ /^(cartesian|CARTESIAN|c|C)$/ ) {
389 $x_1 = $B_R;
390 $y_1 = $theta;
391 $z_1 = $phi;
392 }
393 elsif ( not defined $mode or $mode =~ /^(spherical|SPHERICAL|s|S)$/ ) {
394 $mode = 'spherical';
395 $B_R = abs($B_R);
396 ( $x_1, $y_1, $z_1 ) = spherical_to_cartesian(
397 $B_R, pi * $phi / 180,
398 pi * $theta / 180
399 );
400 }
401 else {
402 die
403 "Give mode for magnetic field sweep in Vectormagnet is not supported. \n";
404 }
405  
406 if ( not defined $interval ) {
407 $interval = 1;
408 }
409  
410 if ( ( $x_1**2 + $y_1**2 + $z_1**2 ) > 1.01 ) {
411 die
412 "unexpected values in sub config_DIR_sweep. Magnetude of target magnetic field > 1 Tesla.";
413 }
414  
415 if ( $rate <= 0 ) {
416 die "unexpected value for RATE ($rate) in sub config_DIR_sweep.";
417 }
418  
419 # get current magnetic field:
420 my ( $x_0, $y_0, $z_0 ) = $self->get_field('C');
421  
422 #calculate sweep parameter:
423 my $trace_length
424 = ( ( $x_1 - $x_0 )**2 + ( $y_1 - $y_0 )**2 + ( $z_1 - $z_0 )**2 )
425 **0.5;
426 my $sweep_time = $trace_length / $rate;
427 my $rate_x;
428 my $rate_y;
429 my $rate_z;
430  
431 if ( $sweep_time == 0 ) {
432 $rate_x = 0.1;
433 $rate_y = 0.1;
434 $rate_z = 0.1;
435 }
436 else {
437 $rate_x = abs( ( $x_1 - $x_0 ) / $sweep_time );
438 $rate_y = abs( ( $y_1 - $y_0 ) / $sweep_time );
439 $rate_z = abs( ( $z_1 - $z_0 ) / $sweep_time );
440 }
441  
442 # config sweep:
443 my @X = $self->{IPS_x}->config_sweep( $x_1, $rate_x, $interval );
444 my @Y = $self->{IPS_y}->config_sweep( $y_1, $rate_y, $interval );
445 my @Z = $self->{IPS_z}->config_sweep( $z_1, $rate_z, $interval );
446  
447 # calculate trace:
448 my @r;
449 my @phi;
450 my @theta;
451 my @dphi;
452 my $len_x = @X;
453 my $len_y = @Y;
454 my $len_z = @Z;
455 my $len = ( $len_x >= $len_y ) ? $len_x : $len_y;
456 $len = ( $len >= $len_z ) ? $len : $len_z;
457  
458 for ( my $i = 0; $i < $len; $i++ ) {
459 if ( ( my $len_x = @X ) >= $i ) {
460 push( @X, $X[-1] );
461 }
462 if ( ( my $len_y = @Y ) >= $i ) {
463 push( @Y, $Y[-1] );
464 }
465 if ( ( my $len_z = @Z ) >= $i ) {
466 push( @Z, $Z[-1] );
467 }
468 ( $r[$i], $phi[$i], $theta[$i] )
469 = cartesian_to_spherical( $X[$i], $Y[$i], $Z[$i] );
470 $phi[$i] = ( $phi[$i] * 180 ) / pi;
471 $theta[$i] = ( $theta[$i] * 180 ) / pi;
472 $dphi[$i] = $phi[$i] - $phi[0];
473 }
474  
475 printf(
476 "Vectormagnet: estimate total duration for sweep: %dm %ds\n",
477 ( ( $len / 60 ) * $interval ),
478 ( ( ( $len / 60 ) * $interval ) % 1 ) * 60
479 );
480 return \@r, \@theta, \@phi, \@dphi, \@X, \@Y, \@Z;
481  
482 }
483  
484 sub cartesian_to_spherical {
485 my ( $x, $y, $z ) = @_;
486  
487 my $rho = sqrt( $x * $x + $y * $y + $z * $z );
488  
489 return ( $rho, atan2( $y, $x ), $rho ? acos_real( $z / $rho ) : 0 );
490 }
491  
492 sub spherical_to_cartesian {
493 my ( $rho, $theta, $phi ) = @_;
494  
495 return (
496 $rho * cos($theta) * sin($phi),
497 $rho * sin($theta) * sin($phi),
498 $rho * cos($phi)
499 );
500 }
501  
502 sub acos_real {
503 return 0 if $_[0] >= 1;
504 return pi if $_[0] <= -1;
505 return acos( $_[0] );
506 }
507  
508 sub Trafo_RHO {
509 my $self = shift;
510 my $x = shift;
511 my $y = shift;
512 my $z = shift;
513  
514 my $X = $x * cos( pi * $self->{RHO} / 180 )
515 + $y * cos( pi * ( $self->{RHO} + 90 ) / 180 );
516 my $Y = $x * cos( pi * ( $self->{RHO} - 90 ) / 180 )
517 + $y * cos( pi * $self->{RHO} / 180 );
518 my $Z = $z;
519  
520 return $X, $Y, $Z;
521  
522 }
523  
524 sub Trafo_KAPPA {
525 my $self = shift;
526 my $x = shift;
527 my $y = shift;
528 my $z = shift;
529  
530 my $X = $x * cos( pi * $self->{KAPPA} / 180 )
531 + $z * cos( pi * ( 90 - $self->{KAPPA} ) / 180 );
532 my $Y = $y;
533 my $Z = $x * cos( pi * ( $self->{KAPPA} - 90 ) / 180 )
534 + $z * cos( -1 * pi * $self->{KAPPA} / 180 );
535  
536 return $X, $Y, $Z;
537  
538 }
539  
540 sub create_basic_trace {
541 my $self = shift;
542 my $R = shift;
543 my $phi_start = shift;
544 my $phi_stop = shift;
545 my $v = shift;
546 my $resolution = shift;
547  
548 # resoltion:
549 if ( not defined $resolution
550 or not defined $v
551 or not defined $phi_start
552 or not defined $phi_stop
553 or not defined $R ) {
554 die
555 "ERROR in sub 'create_basic_trace'. Some of the parameters are not defined.";
556 }
557  
558 # calculate magnet sweep trace points:
559 my @x;
560 my @y;
561 my @z;
562 my $n = 0;
563 print "PHI STOP" . $phi_stop . "\n";
564 print "Res = " . $resolution . "\n";
565 for ( my $i = $phi_start; $i < $phi_stop; $i += $resolution ) {
566  
567 $x[$n] = $R * cos( pi * ($i) / 180 );
568 $y[$n] = $R * sin( pi * ($i) / 180 );
569 $z[$n] = 0;
570  
571 #print $x[$n]."\t".$y[$n]."\t".$z[$n]."\n";
572 ( $x[$n], $y[$n], $z[$n] )
573 = $self->Trafo_KAPPA( $x[$n], $y[$n], $z[$n] );
574 ( $x[$n], $y[$n], $z[$n] )
575 = $self->Trafo_RHO( $x[$n], $y[$n], $z[$n] );
576 $n++;
577 }
578  
579 # Add Phi_stop (final value) to points array
580 $x[$n] = $R * cos( pi * ($phi_stop) / 180 );
581 $y[$n] = $R * sin( pi * ($phi_stop) / 180 );
582 $z[$n] = 0;
583  
584 #print $x[$n]."\t".$y[$n]."\t".$z[$n]."\n";
585 ( $x[$n], $y[$n], $z[$n] ) = $self->Trafo_KAPPA( $x[$n], $y[$n], $z[$n] );
586 ( $x[$n], $y[$n], $z[$n] ) = $self->Trafo_RHO( $x[$n], $y[$n], $z[$n] );
587  
588 # calculate magnet sweep rate:
589 my @vx;
590 my @vy;
591 my @vz;
592 my $len = @x;
593 for ( my $i = 0; $i < $len; $i++ ) {
594 $vx[$i] = abs(
595 (
596 $x[ ( $i == $len - 1 ) ? $i : $i + 1 ]
597 - $x[ ( $i == $len - 1 ) ? $i - 1 : $i ]
598 ) / ( ( $resolution / $v ) )
599 );
600 $vy[$i] = abs(
601 (
602 $y[ ( $i == $len - 1 ) ? $i : $i + 1 ]
603 - $y[ ( $i == $len - 1 ) ? $i - 1 : $i ]
604 ) / ( ( $resolution / $v ) )
605 );
606 $vz[$i] = abs(
607 (
608 $z[ ( $i == $len - 1 ) ? $i : $i + 1 ]
609 - $z[ ( $i == $len - 1 ) ? $i - 1 : $i ]
610 ) / ( ( $resolution / $v ) )
611 );
612 }
613  
614 #open LOG2, ">test2.dat";
615 #my $len = @x;
616 #for ( my $i =0; $i < $len; $i++)
617 # {
618 # print LOG2 $x[$i]."\t".$y[$i]."\t".$z[$i]."\t".$vx[$i]."\t".$vy[$i]."\t".$vz[$i]."\n";
619 # }
620 #close LOG2;
621  
622 return \@x, \@y, \@z, \@vx, \@vy, \@vz;
623  
624 }
625  
626 1;
627  
628 __END__