Moose/Instrument/LinearStepSweep.pm

filter: Statementstmt 90.9% 60 / 66 Branchbran 65.0% 13 / 20 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 9 / 9 Podpod 50.0% 1 / 2 totaltotal 85.5% 83 / 97 CC 12 SCAR 18.1
CC
12
Cov
85%
linear_step_sweep
10.5
check_max_and_min
3.4
BEGIN
1.0
CRAP 6.1
« Moose/Sweep/Step/Magnet.pm ↑ Summary Moose/Connection/VISA/USB.pm »
1 package Lab::Moose::Instrument::LinearStepSweep;
2 7 $Lab::Moose::Instrument::LinearStepSweep::VERSION = '3.931';
3 #ABSTRACT: Role for linear step sweeps used by voltage/current sources.
4  
5 7 ▶use v5.20;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 7 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 ▶use MooseX::Params::Validate;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use Lab::Moose::Instrument 'setter_params';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 # time() returns floating seconds.
11 7 ▶use Time::HiRes qw/time usleep/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 7 ▶use Lab::Moose 'linspace';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 7 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 7 requires qw/max_units_per_second max_units_per_step min_units max_units
16 source_level cached_source_level source_level_timestamp/;
17  
18  
19 # Enforce max_units/min_units.
20 170 ▶sub check_max_and_min {
Subroutine check_max_and_min: called CC=3 SCAR=12.3
Pod undocumented
21 170 my $self = shift;
22 170 my $to = shift;
23  
24 170 my $min = $self->min_units();
25 170 my $max = $self->max_units();
26 170 ▶ if ( $to < $min ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($to < $min) { } 0 170 170
elsif ($to > $max) { } 0 170 170
27 0 croak "target $to is below minimum allowed value $min";
28 }
29 elsif ( $to > $max ) {
30 0 croak "target $to is above maximum allowed value $max";
31 }
32 }
33  
34 170 ▶sub linear_step_sweep {
Subroutine linear_step_sweep: called CC=10 SCAR=23.5
Pod documented
35 170 my ( $self, %args ) = validated_hash(
36 \@_,
37 to => { isa => 'Num' },
38 verbose => { isa => 'Bool', default => 1 },
39 setter_params(),
40 );
41 170 my $to = delete $args{to};
42 170 my $verbose = delete $args{verbose};
43 170 my $from = $self->cached_source_level();
44 170 my $last_timestamp = $self->source_level_timestamp();
45 170 my $distance = abs( $to - $from );
46  
47 170 $self->check_max_and_min($to);
48  
49 170 ▶ if ( not defined $last_timestamp ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $last_timestamp) 19 151 170
50 19 $last_timestamp = time();
51 }
52  
53 # Enforce step size and rate.
54 170 my $step = abs( $self->max_units_per_step() );
55  
56 170 my $rate = abs( $self->max_units_per_second() );
57 170 ▶ if ( $step < 1e-9 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($step < "1e-09") 0 170 170
58 0 croak "step size must be > 0";
59 }
60  
61 170 ▶ if ( $rate < 1e-9 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rate < "1e-09") 0 170 170
62 0 croak "rate must be > 0";
63 }
64  
65 170 my @steps = linspace(
66 from => $from, to => $to, step => $step,
67 exclude_from => 1
68 );
69  
70 170 my $time_per_step;
71 170 ▶ if ( $distance < $step ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($distance < $step) { } 167 3 170
72 167 $time_per_step = $distance / $rate;
73 }
74 else {
75 3 $time_per_step = $step / $rate;
76 }
77  
78 170 my $time = time();
79  
80 170 ▶ if ( $time < $last_timestamp ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($time < $last_timestamp) 0 170 170
81  
82 # should never happen
83 # -> but it does seem to happen on Windows, so do not croak here.
84 }
85  
86 # Do we have to wait to enforce the maximum rate or can we start right now?
87 170 my $waiting_time = $time_per_step - ( $time - $last_timestamp );
88 170 ▶ if ( $waiting_time > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($waiting_time > 0) 3 167 170
89 3 usleep( 1e6 * $waiting_time );
90 }
91 170 $self->source_level( value => shift @steps, %args );
92  
93 # enable autoflush
94 170 my $autoflush = STDOUT->autoflush();
95 170 for my $step (@steps) {
96 36 usleep( 1e6 * $time_per_step );
97  
98 # YokogawaGS200 has 5 + 1/2 digits precision
99 36 ▶ if ($verbose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose) 0 36 36
100 0 printf(
101 "Sweeping to %.5g: Setting level to %.5e \r", $to,
102 $step
103 );
104 }
105 36 $self->source_level( value => $step, %args );
106 }
107 170 ▶ if ($verbose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose) 0 170 170
108 0 print " " x 70 . "\r";
109 }
110  
111 # reset autoflush to previous value
112 170 STDOUT->autoflush($autoflush);
113 170 $self->source_level_timestamp( time() );
114 }
115  
116  
117 7 1;
118  
119 __END__
« Moose/Sweep/Step/Magnet.pm ↑ Summary Moose/Connection/VISA/USB.pm »