| 1 | package Lab::Moose::Stabilizer; | ||||||||||||||||||||||||||||
| 2 | 2 | $Lab::Moose::Stabilizer::VERSION = '3.881'; | |||||||||||||||||||||||||||
| 3 | #ABSTRACT: Sensor stabilizer subroutine | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use v5.20; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use Lab::Moose (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use MooseX::Params::Validate 'validated_list'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use Time::HiRes qw/time sleep/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 2 | ▶ | use Lab::Moose::Countdown; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 2 | ▶ | use Statistics::Descriptive (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 2 | ▶ | use Scalar::Util 'looks_like_number'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 2 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 2 | ▶ | use Exporter 'import'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 2 | our @EXPORT = qw/stabilize/; | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | # inspired by old Lab::XPRESS stabilization routines | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | sub stabilize { | ||||||||||||||||||||||||||||
| 23 | my ( | ||||||||||||||||||||||||||||
| 24 | 1 | ▶ | $instrument, $setpoint, $getter, $tolerance_setpoint, | ||||||||||||||||||||||||||
|
Subroutine
stabilize: called CC=14 SCAR=28.5
Pod
documented
| |||||||||||||||||||||||||||||
| 25 | $tolerance_std_dev, | ||||||||||||||||||||||||||||
| 26 | $measurement_interval, $observation_time, $max_stabilization_time, | ||||||||||||||||||||||||||||
| 27 | $verbose | ||||||||||||||||||||||||||||
| 28 | ) | ||||||||||||||||||||||||||||
| 29 | = validated_list( | ||||||||||||||||||||||||||||
| 30 | \@_, | ||||||||||||||||||||||||||||
| 31 | instrument => { isa => 'Object' }, | ||||||||||||||||||||||||||||
| 32 | setpoint => { isa => 'Num' }, | ||||||||||||||||||||||||||||
| 33 | getter => { isa => 'CodeRef | Str' }, | ||||||||||||||||||||||||||||
| 34 | tolerance_setpoint => { isa => 'Lab::Moose::PosNum' }, | ||||||||||||||||||||||||||||
| 35 | tolerance_std_dev => { isa => 'Lab::Moose::PosNum' }, | ||||||||||||||||||||||||||||
| 36 | measurement_interval => { isa => 'Lab::Moose::PosNum' }, | ||||||||||||||||||||||||||||
| 37 | observation_time => { isa => 'Lab::Moose::PosNum' }, | ||||||||||||||||||||||||||||
| 38 | max_stabilization_time => | ||||||||||||||||||||||||||||
| 39 | { isa => 'Maybe[Lab::Moose::PosNum]', optional => 1 }, | ||||||||||||||||||||||||||||
| 40 | verbose => { isa => 'Bool' }, | ||||||||||||||||||||||||||||
| 41 | ); | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 1 | my @points = (); | |||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 1 | my $num_points = int( $observation_time / $measurement_interval ); | |||||||||||||||||||||||||||
| 46 | 1 | ▶ | if ( $num_points == 0 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | 0 | $num_points = 1; | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | # enable autoflush | ||||||||||||||||||||||||||||
| 51 | 1 | my $autoflush = STDOUT->autoflush(); | |||||||||||||||||||||||||||
| 52 | 1 | my $start_time = time(); | |||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 1 | while (1) { | |||||||||||||||||||||||||||
| 55 | 14 | my $new_value = $instrument->$getter(); | |||||||||||||||||||||||||||
| 56 | 14 | ▶ | if ( not looks_like_number($new_value) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | 0 | croak "$new_value is not a number"; | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | 14 | push @points, $new_value; | |||||||||||||||||||||||||||
| 60 | 14 | ▶ | if ( @points > $num_points ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | 4 | shift @points; | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 14 | ▶ | if ( @points == $num_points ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | 5 | my $crit_stddev; | |||||||||||||||||||||||||||
| 66 | my $crit_setpoint; | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 5 | my $stat = Statistics::Descriptive::Full->new(); | |||||||||||||||||||||||||||
| 69 | 5 | $stat->add_data(@points); | |||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 5 | my $std_dev = $stat->standard_deviation(); | |||||||||||||||||||||||||||
| 72 | 5 | ▶ | if ( $std_dev < $tolerance_std_dev ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | 1 | $crit_stddev = 1; | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 5 | my $median = $stat->median(); | |||||||||||||||||||||||||||
| 77 | 5 | ▶ | if ( abs( $setpoint - $median ) < $tolerance_setpoint ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | 4 | $crit_setpoint = 1; | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 5 | ▶ | if ($verbose) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | 0 | printf( | |||||||||||||||||||||||||||
| 83 | "Setpoint: %.6e, Value: %.6e, std_dev: %.6e, median: %.6e ", | ||||||||||||||||||||||||||||
| 84 | $setpoint, $new_value, $std_dev, $median | ||||||||||||||||||||||||||||
| 85 | ); | ||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | 5 | ▶ | if ( $crit_stddev and $crit_setpoint ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$crit_stddev and $crit_setpoint
MC/DC50%1/2
$crit_stddev and $crit_setpoint
$crit_stddev
$crit_setpoint
Truth table66%2/3
A and B
A= $crit_stddevB= $crit_setpoint
| |||||||||||||||||||||||||||||
| 88 | 1 | printf("reached stabilization criterion \n"); | |||||||||||||||||||||||||||
| 89 | 1 | last; | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | else { | ||||||||||||||||||||||||||||
| 92 | 4 | printf("\r"); | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | else { | ||||||||||||||||||||||||||||
| 97 | 9 | ▶ | if ($verbose) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | 0 | printf( | |||||||||||||||||||||||||||
| 99 | "Setpoint: %.6e, Value: %.6e, need more points... \r", | ||||||||||||||||||||||||||||
| 100 | $setpoint, $new_value | ||||||||||||||||||||||||||||
| 101 | ); | ||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 13 | ▶ | if ( $measurement_interval > 5 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | 0 | countdown( | |||||||||||||||||||||||||||
| 107 | $measurement_interval, | ||||||||||||||||||||||||||||
| 108 | "Measurement interval: Sleeping for " | ||||||||||||||||||||||||||||
| 109 | ); | ||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | else { | ||||||||||||||||||||||||||||
| 112 | 13 | sleep($measurement_interval); | |||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 13 | ▶ | if ( defined $max_stabilization_time ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 116 | 0 | ▶ | if ( time() - $start_time > $max_stabilization_time ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | 0 | printf( | |||||||||||||||||||||||||||
| 118 | "Reached maximum stabilization time \n" | ||||||||||||||||||||||||||||
| 119 | ); | ||||||||||||||||||||||||||||
| 120 | 0 | last; | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | # reset autoflush to previous value | ||||||||||||||||||||||||||||
| 126 | 1 | STDOUT->autoflush($autoflush); | |||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 2 | 1; | |||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | __END__ | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | =pod | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | =encoding UTF-8 | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | =head1 NAME | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | Lab::Moose::Stabilizer - Sensor stabilizer subroutine | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | =head1 VERSION | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | version 3.881 | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | Routine for sensor (temperature, magnetic field, ...) stabilization. | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | =head1 SUBROUTINES | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | =head2 stabilize | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | stabilize( | ||||||||||||||||||||||||||||
| 154 | instrument => $OI_ITC, | ||||||||||||||||||||||||||||
| 155 | setpoint => 10, | ||||||||||||||||||||||||||||
| 156 | getter => sub { ...; return $number}, # or method name like 'get_T' | ||||||||||||||||||||||||||||
| 157 | # will call '$instrument->$getter()' | ||||||||||||||||||||||||||||
| 158 | tolerance_setpoint => 0.1, # max. allowed median | ||||||||||||||||||||||||||||
| 159 | tolerance_std_dev => 0.1, # max. allowed standard deviation | ||||||||||||||||||||||||||||
| 160 | measurement_interval => 2, # time (s) between calls of getter | ||||||||||||||||||||||||||||
| 161 | observation_time => 20, # length of window (s) for median/std_dev | ||||||||||||||||||||||||||||
| 162 | max_stabilization_time => 100, # abort stabilization after (s, optional) | ||||||||||||||||||||||||||||
| 163 | verbose => 1 | ||||||||||||||||||||||||||||
| 164 | ); | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | Call the C<getter> method repeatedly. As soon as enough points have been measured, | ||||||||||||||||||||||||||||
| 167 | start calculating median and standard deviation and repeat until convergence. | ||||||||||||||||||||||||||||
| 168 | All times are given in seconds. Print status messages if C<verbose> is true. | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | This software is copyright (c) 2023 by the Lab::Measurement team; in detail: | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | Copyright 2018 Andreas K. Huettel, Simon Reinhardt | ||||||||||||||||||||||||||||
| 175 | 2019 Simon Reinhardt | ||||||||||||||||||||||||||||
| 176 | 2020 Andreas K. Huettel | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 180 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | =cut | ||||||||||||||||||||||||||||