XPRESS/Sweep/Time.pm untested

filter: Statementstmt 0.0% 0 / 90 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 138 CC 11 SCAR 31.3
CC
11
Cov
0%
exit_loop
56.0
check_config_paramters
20.0
new
6.0
CRAP 22.8
1 package Lab::XPRESS::Sweep::Time;
2 #ABSTRACT: Simple time-controlled repeater
3 $Lab::XPRESS::Sweep::Time::VERSION = '3.899';
4 use v5.20;
5  
6 use Lab::XPRESS::Sweep;
7 use Time::HiRes qw/usleep/, qw/time/;
8 use Statistics::Descriptive;
9 use Carp;
10 use strict;
11  
12 our @ISA = ('Lab::XPRESS::Sweep');
13  
14 sub new {
15 my $proto = shift;
16 my @args = @_;
17 my $class = ref($proto) || $proto;
18  
19 # define default values for the config parameters:
20 my $self->{default_config} = {
21 id => 'Time_sweep',
22 interval => 1,
23 points => [0], #[0,10],
24 duration => undef,
25 stepwidth => 1,
26 mode => 'continuous',
27 allowed_instruments => [undef],
28 allowed_sweep_modes => ['continuous'],
29  
30 stabilize => 0,
31 sensor => undef,
32 sensor_args => [],
33 std_dev_sensor => 1e-6,
34 stabilize_observation_time => 3 * 60,
35 };
36  
37 if ( ref( @args[0]->{duration} ) ne "ARRAY" ) {
38 @args[0]->{duration} = [ @args[0]->{duration} ];
39 }
40  
41 foreach my $d ( @{ @args[0]->{duration} } ) {
42 push( @{ $self->{default_config}->{points} }, $d );
43 }
44  
45 # create self from Sweep basic class:
46 $self = $class->SUPER::new( $self->{default_config}, @args );
47 bless( $self, $class );
48  
49 # check and adjust config values if necessary:
50 $self->check_config_paramters();
51  
52 # init mandatory parameters:
53 $self->{DataFile_counter} = 0;
54 $self->{DataFiles} = ();
55  
56 $self->{stabilize}->{data} = ();
57  
58 return $self;
59 }
60  
61 sub check_config_paramters {
62 my $self = shift;
63  
64 # No Backsweep allowed; adjust number of Repetitions if Backsweep is 1:
65 if ( $self->{config}->{backsweep} == 1 ) {
66 $self->{config}->{repetitions} /= 2;
67 $self->{config}->{backsweep} = 0;
68 }
69  
70 # Set loop-Interval to Measurement-Interval:
71 $self->{loop}->{interval} = $self->{config}->{interval};
72  
73 # check correct initialization of stabilize
74 if ( $self->{config}->{stabilize} == 1 ) {
75 if ( not defined $self->{config}->{sensor} ) {
76 croak('Stabilization activated, but no sensor defined!');
77 }
78  
79 }
80  
81 }
82  
83 sub exit_loop {
84 my $self = shift;
85  
86 if (
87 @{ $self->{config}->{points} }[ $self->{sequence} ] > 0
88 and $self->{iterator} >= (
89 @{ $self->{config}->{points} }[ $self->{sequence} ]
90 / @{ $self->{config}->{interval} }[ $self->{sequence} ]
91 )
92 ) {
93 if (
94 not
95 defined @{ $self->{config}->{points} }[ $self->{sequence} + 1 ] )
96 {
97 if ( $self->{config}->{stabilize} == 1 ) {
98 carp('Reached maximum stabilization time.');
99 }
100 return 1;
101 }
102  
103 $self->{iterator} = 0;
104 $self->{sequence}++;
105 return 0;
106 }
107 elsif ( $self->{config}->{stabilize} == 1 ) {
108 my @sensor_args = @{ $self->{config}->{sensor_args} };
109 push(
110 @{ $self->{stabilize}->{data} },
111 $self->{config}->{sensor}->get_value(@sensor_args)
112 );
113  
114 my $SENSOR_STD_DEV_PRINT = '-' x 10;
115 say "ELAPSED: " . sprintf( '%.2f', $self->{Time} );
116  
117 if ( $self->{Time} >= $self->{config}->{stabilize_observation_time} )
118 {
119 shift( @{ $self->{stabilize}->{data} } );
120  
121 my $stat = Statistics::Descriptive::Full->new();
122 $stat->add_data( $self->{stabilize}->{data} );
123 my $SENSOR_STD_DEV = $stat->standard_deviation();
124 $SENSOR_STD_DEV_PRINT = sprintf( '%.3e', $SENSOR_STD_DEV );
125  
126 say "CURRENT_STDD: "
127 . $SENSOR_STD_DEV_PRINT
128 . " / TARGET_STDD: "
129 . sprintf( '%.3e', $self->{config}->{std_dev_sensor} );
130  
131 if ( $SENSOR_STD_DEV <= $self->{config}->{std_dev_sensor} ) {
132 carp('Reached stabilization criterion.');
133 return 1;
134 }
135 }
136  
137 return 0;
138 }
139 else {
140 return 0;
141 }
142 }
143  
144 sub get_value {
145 my $self = shift;
146 return $self->{Time};
147 }
148  
149 sub go_to_sweep_start {
150 my $self = shift;
151  
152 $self->{sequence}++;
153 }
154  
155 sub halt {
156 return shift;
157 }
158  
159 1;
160  
161 __END__