Instrument/Source.pm untested

filter: Statementstmt 0.0% 0 / 232 Branchbran 0.0% 0 / 106 Conditioncond 0.0% 0 / 90 MC/DCmcdc 0.0% 0 / 58 Subroutinesub 0.0% 0 / 23 Podpod 0.0% 0 / 13 totaltotal 0.0% 0 / 522 CC 54 SCAR 46.8
CC
54
Cov
0%
check_sweep_config
210.0
new
182.0
sweep_to_level
156.0
CRAP 108.0
1 package Lab::Instrument::Source;
2 #ABSTRACT: Generic voltage source base class
3 $Lab::Instrument::Source::VERSION = '3.899';
4 use v5.20;
5  
6 use strict;
7 use warnings;
8  
9 use Lab::Exception;
10 use Lab::Instrument;
11 use Lab::Measurement::KeyboardHandling qw(labkey_check);
12  
13 use Time::HiRes qw(usleep gettimeofday);
14 use Clone qw(clone);
15  
16 our @ISA = ('Lab::Instrument');
17  
18 our %fields = (
19 supported_connections => [],
20  
21 parent_source => undef,
22 child_sources => [],
23  
24 # supported config options
25 device_settings => {
26 gate_protect => undef,
27 gp_max_units_per_second => undef,
28 gp_max_units_per_step => undef,
29 gp_max_step_per_second => undef,
30 gp_min_units => undef,
31 gp_max_units => undef,
32  
33 gp_equal_level => 0,
34 fast_set => undef,
35 autorange => 0
36 , # silently ignored by instruments (or drivers) which don't support autorange
37  
38 read_default => 'device'
39 },
40  
41 # Config hash passed to subchannel objects, or to $self->configure()
42 default_device_settings => {},
43  
44 gpData => {},
45  
46 default_channel => 1,
47 max_channels => 1,
48  
49 device_cache => {
50 level => undef,
51 range => undef
52 }
53 );
54  
55 sub new {
56 my $proto = shift;
57 my $class = ref($proto) || $proto;
58  
59 #
60 # The following is for compatibility with the old syntax for subchannel object derivation.
61 # If we get some Instrument::Source object along with an integer (channel number), use it to derive a subchannel and return this.
62 #
63 # Is it an object at all? Is it a Source object?
64 if ( ref( $_[0] )
65 && UNIVERSAL::can( $_[0], 'can' )
66 && UNIVERSAL::isa( $_[0], "Lab::Instrument::Source" ) ) {
67 Lab::Exception::CorruptParameter->throw( error =>
68 "Got a valid Source object, but an invalid channel number: $_[1]. Can't create subchannel, sorry."
69 ) if !defined $_[1] || $_[1] !~ /^[0-9]*$/;
70  
71 # Use the given parent object to derive a subchannel and return it
72 my ( $parent, $channel ) = ( shift, shift );
73 my %conf = ();
74 if ( ref $_[0] eq 'HASH' ) { %conf = %{ ; shift } }
75 else { %conf = (@_) }
76 return $parent->create_subsource( channel => $channel, %conf );
77 }
78  
79 # compatibility mode stop, continue normally (phew)
80 my $self = $class->SUPER::new(@_);
81 $self->${ \( __PACKAGE__ . '::_construct' ) }(__PACKAGE__);
82  
83 #
84 # Parameter parsing
85 #
86  
87 # checking if a valid default_device_settings hash was set by _construct.
88 # if not, initialize it with $self->device_settings
89 if ( defined( $self->default_device_settings() ) ) {
90 if ( ref( $self->default_device_settings() ) !~ /HASH/ ) {
91 Lab::Exception::CorruptParameter->throw(
92 error => 'Given default config is not a hash.' );
93 }
94 elsif ( scalar keys %{ $self->default_device_settings() } == 0 )
95 { # poor thing's empty
96 $self->default_device_settings(
97 clone( $self->device_settings() ) );
98 }
99 }
100 else {
101 $self->default_device_settings( clone( $self->device_settings() ) );
102 }
103  
104 # check max channels
105 if ( defined( $self->config('max_channels') ) ) {
106 if ( $self->config('max_channels') !~ /^[0-9]*$/ ) {
107 Lab::Exception::CorruptParameter->throw(
108 error => 'Parameter max_channels has to be an Integer' );
109 }
110 else { $self->max_channels( $self->config('max_channels') ); }
111 }
112  
113 # checking default channel number
114 if (
115 defined( $self->default_channel() )
116 && ( $self->default_channel() > $self->max_channels()
117 || $self->default_channel() < 1 )
118 ) {
119 Lab::Exception::CorruptParameter->throw( error =>
120 'Default channel number is not within the available channels.'
121 );
122 }
123  
124 if ( defined( $self->parent_source() ) ) {
125 if (
126 !UNIVERSAL::isa(
127 $self->parent_source(), "Lab::Instrument::Source"
128 )
129 ) {
130 Lab::Exception::CorruptParameter->throw( error =>
131 'Given parent_source object is not a valid Lab::Instrument::Source.'
132 );
133 }
134  
135 # instead of maintaining our own one, check if a valid reference to the gpData from the parent object was given
136 if ( !defined( $self->gpData() )
137 || !ref( $self->gpData() ) =~ /HASH/ ) {
138 Lab::Exception::CorruptParameter->throw(
139 error => 'Given gpData from parent_source is invalid.' );
140 }
141  
142 # shared connection *should* be okay, but keep this in mind
143 $self->connection( $self->parent_source()->connection() );
144 }
145 else {
146 # fill gpData
147 for ( my $i = 1; $i <= $self->max_channels(); $i++ ) {
148 $self->gpData()->{$i} = { LastSettimeMus => undef };
149 }
150 }
151  
152 return $self;
153 }
154  
155 sub configure {
156 my $self = shift;
157  
158 my $config = shift;
159 if ( ref($config) ne 'HASH' ) {
160 Lab::Exception::CorruptParameter->throw(
161 error => 'Given Configuration is not a hash.' );
162 }
163 else {
164 #
165 # first do the standard Instrument::configure() on $config
166 #
167 $self->SUPER::configure($config);
168  
169 #
170 # now parse in default_device_settings
171 #
172 for my $conf_name ( keys %{ $self->device_settings() } ) {
173 $self->device_settings()->{$conf_name}
174 = $self->default_device_settings()->{$conf_name}
175 if exists( $self->default_device_settings()->{$conf_name} )
176 && !defined( $self->device_settings($conf_name) );
177 }
178 }
179 }
180  
181 sub create_subsource
182 { # create_subsource( channel => $channel_nr, more=>options );
183 my $self = shift;
184 my $class = ref($self);
185 my $args = undef;
186 if ( ref $_[0] eq 'HASH' ) { $args = shift }
187 else { $args = {@_} }
188  
189 # we may be a subsource ourselfes, here - in this case, use our parent source instead of $self
190 my $parent_to_be = $self->parent_source() || $self;
191  
192 Lab::Exception::CorruptParameter->throw( error =>
193 'No channel number specified! You have to set the channel=>$number parameter.'
194 ) if ( !exists( $args->{'channel'} ) );
195 Lab::Exception::CorruptParameter->throw(
196 error => "Invalid channel number: "
197 . $args->{'channel'}
198 . ". Integer expected." )
199 if ( $args->{'channel'} !~ /^[0-9]*/ );
200  
201 my %default_device_settings
202 = %{ $parent_to_be->default_device_settings() };
203 delete local $default_device_settings{'channel'};
204 @default_device_settings{ keys %{$args} } = values %{$args};
205  
206 no strict 'refs';
207 my $subsource = $class->new(
208 {
209 parent_source => $parent_to_be,
210 gpData => $parent_to_be->gpData(),
211 %default_device_settings
212 }
213 );
214 use strict;
215 $parent_to_be->child_sources(
216 [ @{ $parent_to_be->child_sources() }, $subsource ] );
217 return $subsource;
218 }
219  
220 sub set_level {
221 my $self = shift;
222 my ( $target, $tail ) = $self->_check_args( \@_, ['target'] );
223  
224 my $current_level = $self->get_level( { read_mode => 'cache' } );
225  
226 if ( $target == $current_level ) {
227 return $current_level;
228 }
229  
230 if ( $self->device_settings()->{'gate_protect'}
231 and $self->device_settings()->{'gp_max_units_per_step'}
232 < abs( $target - $current_level ) ) {
233 return $self->sweep_to_level( $target, $tail );
234 }
235 else {
236 return $self->_set_level( $target, $tail );
237 }
238 }
239  
240 sub get_value {
241 my $self = shift;
242  
243 return $self->get_level(@_);
244 }
245  
246 sub check_sweep_config {
247 my $self = shift;
248 $self->_check_gate_protect();
249 my ( $target, $rate, $time, $tail )
250 = $self->_check_args( \@_, [ 'points', 'rate', 'time' ] );
251  
252 # get current position:
253 my $start = $self->get_level( { read_mode => 'device' }, $tail );
254  
255 my $duration;
256  
257 if ( defined $rate and not defined $time ) {
258 $duration = int( abs( $start - $target ) / abs($rate) );
259 }
260 elsif ( not defined $rate and defined $time ) {
261 $duration = $time;
262 $rate = abs( $start - $target ) / $time;
263 }
264 elsif ( defined $rate and defined $time ) {
265 Lab::Exception::CorruptParameter->throw(
266 "Definition of rate and time simultanousely is inconsistent!");
267 }
268 else {
269 if ( $self->device_settings()->{gate_protect} ) {
270 $rate = $self->device_settings()->{gp_max_units_per_second};
271 $duration = int( abs( $start - $target ) / abs($rate) );
272 }
273 else {
274 Lab::Exception::CorruptParameter->throw(
275 "If not in gate protection mode, please define at least rate or time"
276 );
277 }
278  
279 }
280  
281 # check if the given target value and given rate are within the GATE-PROTECTION limts:
282 if ( $self->device_settings()->{gate_protect} ) {
283  
284 if ( $target < $self->device_settings()->{gp_min_units}
285 or $target > $self->device_settings()->{gp_max_units} ) {
286 Lab::Exception::CorruptParameter->throw( error =>
287 "SWEEP-TARGET $target exceeds GATE_PROTECTION LIMITS: "
288 . $self->device_settings()->{gp_min_volt} . " ... "
289 . $self->device_settings()->{gp_max_volt} );
290 }
291 if (
292 abs($rate)
293 > abs( $self->device_settings()->{gp_max_units_per_second} ) ) {
294 Lab::Exception::CorruptParameter->throw(
295 error => "SWEEP-RATE $rate exceeds GATE_PROTECTION LIMITS: "
296 . $self->device_settings()->{gp_max_units_per_second} );
297 }
298 }
299  
300 # check if rate is within limits:
301 if ( $rate == 0 ) {
302 print Lab::Exception::CorruptParameter->new( error =>
303 " Sweep rate too small: Maximum Sweep duration is limited to 176400 sec. "
304 );
305 $rate = abs( $start - $target ) / 176400;
306 }
307 elsif ( abs( $start - $target ) / $rate > 176400 ) {
308 print Lab::Exception::CorruptParameter->new( error =>
309 " Sweep rate too small: Maximum Sweep duration is limited to 176400 sec. "
310 );
311 $rate = abs( $start - $target ) / 176400;
312 }
313 elsif ( abs( $start - $target ) / $rate < 0.1 ) {
314  
315 #print Lab::Exception::CorruptParameter->new( error=> " Sweep rate too large: Minimum Sweep duration is limited to 0.1 sec. ");
316 $duration = 0.1;
317 }
318  
319 # calculate duration and the number of points for the sweep:
320  
321 # Test if $target in range and start programming the device:
322 my $range = $self->get_range( { read_mode => 'cache' }, $tail );
323  
324 if ( $target > $range ) {
325 Lab::Exception::CorruptParameter->throw( error =>
326 "SWEEP-TARGET $target exceeds selected RANGE $range. Change SWEEP-TARGET to MAX within RANGE."
327 );
328 }
329 elsif ( $target < -$range ) {
330 Lab::Exception::CorruptParameter->throw( error =>
331 "SWEEP-TARGET $target exceeds selected RANGE $range. Change SWEEP-TARGET to MAX within RANGE."
332 );
333 }
334  
335 # split sweep longer than 3600 sec into sections
336 my $sections = int( $duration / 3600 ) + 1;
337 $duration = sprintf( "%.1f", $duration / $sections );
338  
339 if ( $sections > 50 ) {
340 Lab::Exception::CorruptParameter->throw( error =>
341 "Configured Sweep takes too long. Sweep time is limited to 176400s."
342 );
343 }
344  
345 return ( $start, $target, $duration, $sections, $tail );
346  
347 }
348  
349 sub sweep_to_level {
350 my $self = shift;
351  
352 my ( $target, $time, $stepsize, $args )
353 = $self->_check_args( \@_, [ 'target', 'time', 'stepsize' ] );
354  
355 if ( not defined $target || ref($target) eq 'HASH' ) {
356 Lab::Exception::CorruptParameter->throw(
357 error => 'No target voltage given.' );
358 }
359  
360 # Check correct channel setup
361  
362 $self->_check_gate_protect();
363  
364 # Make sure stepsize is within gate_protect boundaries.
365  
366 # Returns undef if gate_protect is unset.
367 my $upstep = $self->get_gp_max_units_per_step();
368  
369 if ( ( not defined $upstep ) && ( not defined $stepsize ) ) {
370 Lab::Exception::CorruptParameter->throw( error =>
371 'Need either gp_max_units_per_step or stepsize parameter.' );
372 }
373  
374 if ( not defined $upstep ) {
375 $upstep = $stepsize;
376 }
377  
378 if ( defined $stepsize && defined $upstep && $upstep > $stepsize ) {
379 $upstep = $stepsize;
380 }
381  
382 my $apsec = $self->get_gp_max_units_per_second();
383  
384 my $spsec = $self->get_gp_max_step_per_second();
385  
386 my $current = $self->get_level();
387  
388 if ( $target == $current ) {
389 return $target;
390 }
391  
392 if ( $self->device_settings()->{gate_protect} && $time ) {
393 if ( abs( $target - $current ) > $apsec * $time ) {
394  
395 # we need to increase time to stay within the
396 # max_units_per_second limit.
397 $time = abs( $target - $current ) / $apsec;
398 }
399 }
400 elsif ( not defined($time) ) {
401 $time = abs( $target - $current ) / $apsec;
402 }
403  
404 # Sweep to current.
405  
406 if ( $self->can("_sweep_to_level") ) {
407 return $self->_sweep_to_level( $target, $time, $args );
408 }
409 else {
410  
411 my $steptime = $time / ( abs( $current - $target ) / $upstep );
412 while (1) {
413 if ( abs( $target - $current ) <= $upstep ) {
414 $self->_set_level( $target, $args );
415 last;
416 }
417  
418 my $step = $upstep;
419  
420 if ( $target < $current ) {
421 $step *= -1.;
422 }
423  
424 $current = $self->_set_level( $current + $step, $args );
425  
426 usleep( 1e6 * $steptime );
427 }
428 return $current;
429 }
430  
431 }
432  
433 sub is_me_channel {
434 my $self = shift;
435 my $channel = shift;
436 if ( $channel < 0 ) {
437 Lab::Exception::CorruptParameter->throw( error =>
438 'Channel must not be negative! Did you swap voltage and channel number?'
439 );
440 }
441 if ( int($channel) != $channel ) {
442 Lab::Exception::CorruptParameter->throw( error =>
443 'Channel must be an integer! Did you swap voltage and channel number?'
444 );
445 }
446  
447 }
448  
449 # Check if all gate protect variables are set correctly. And if not, calculate the missing values. Error if
450 # volt_per_step
451 # or volt_per_sec / steps_per_sec is not given
452  
453 sub _check_gate_protect {
454 my $self = shift;
455 my $mode = shift;
456 my $device_settings = $self->device_settings();
457  
458 if ( !$device_settings->{gate_protect} ) {
459 return;
460 }
461  
462 # Check if one of gp_max_units_per_second or gp_max_step_per_second is given
463  
464 my $apsec = $device_settings->{gp_max_units_per_second};
465 my $apstep = $device_settings->{gp_max_units_per_step};
466 my $spsec = $device_settings->{gp_max_step_per_second};
467  
468 # Make sure the gate protect vars are correctly set and consistent
469  
470 if ( ( !defined($apstep) || $apstep <= 0 ) ) {
471 Lab::Exception::CorruptParameter->throw( error =>
472 "To use gate protection, you have to gp_max_units_per_step (now: $apstep) to a positive, non-zero value."
473 );
474 }
475  
476 if ( ( defined($apsec) && $apsec > 0 )
477 && ( !defined($spsec) || $spsec < 0 ) ) {
478 $spsec = $apsec / $apstep;
479 }
480 elsif (( defined($spsec) && $spsec > 0 )
481 && ( !defined($apsec) || $apsec < 0 ) ) {
482 $apsec = $spsec * $apstep;
483 }
484 elsif (( !defined($apsec) || $apsec <= 0 )
485 && ( !defined($spsec) || $spsec < 0 ) ) {
486 Lab::Exception::CorruptParameter->throw(
487 "Please supply one of either gp_max_units_per_second or gp_max_steps_per_second."
488 );
489 }
490 else {
491 if ( $apsec <= $spsec * $apstep ) {
492 $spsec = $apsec / $apstep;
493 }
494 else {
495 $apsec = $spsec * $apstep;
496 }
497 }
498  
499 $device_settings->{gp_max_units_per_second} = $apsec;
500 $device_settings->{gp_max_units_per_step} = $apstep;
501 $device_settings->{gp_max_step_per_second} = $spsec;
502  
503 # gp_max_units and gp_min_units are mandatory
504 my $gp_max_units = $device_settings->{gp_max_units};
505 my $gp_min_units = $device_settings->{gp_min_units};
506  
507 if ( not defined $gp_max_units or not defined $gp_min_units ) {
508 Lab::Exception::CorruptParameter->throw(
509 "Missing gate_protect parameters for \"gp_max_units\" or \"gp_min_units\".\nYou must provide those during instrument initialization.\nIf you do not want to use the gate_protect feature,\nyou have to set gate_protect to 0."
510 );
511 }
512  
513 if ( $gp_max_units <= $gp_min_units ) {
514 Lab::Exception::CorruptParameter->throw(
515 "value of \"gp_max_units\" is smaller or equal to value of \"gp_min_units\""
516 );
517 }
518 }
519  
520 sub _set_level {
521 my $self = shift;
522  
523 Lab::Exception::DriverError->throw( "The unimplemented method stub "
524 . __PACKAGE__
525 . "::_set_level() has been called. I can't work like this.\n" );
526 }
527  
528 sub get_level {
529 my $self = shift;
530  
531 Lab::Exception::DriverError->throw( "The unimplemented method stub "
532 . __PACKAGE__
533 . "::get_level() has been called. I can't work like this.\n" );
534  
535 }
536  
537 sub get_range {
538 my $self = shift;
539  
540 Lab::Exception::DriverError->throw( "The unimplemented method stub "
541 . __PACKAGE__
542 . "::get_range() has been called. I can't work like this.\n" );
543 }
544  
545 sub set_range {
546 my $self = shift;
547  
548 Lab::Exception::DriverError->throw( "The unimplemented method stub "
549 . __PACKAGE__
550 . "::set_range() has been called. I can't work like this.\n" );
551  
552 }
553  
554 1;
555  
556 __END__