File Coverage

blib/lib/Lab/Moose/Instrument/DummySource.pm
Criterion Covered Total %
statement 82 84 97.6
branch 16 18 88.8
condition n/a
subroutine 17 17 100.0
pod 0 8 0.0
total 115 127 90.5


line stmt bran cond sub pod time code
1             package Lab::Moose::Instrument::DummySource;
2             $Lab::Moose::Instrument::DummySource::VERSION = '3.880';
3             #ABSTRACT: Dummy YokogawaGS200 source for use with 'Debug' connection
4              
5 4     4   3886 use v5.20;
  4         16  
6              
7              
8 4     4   26 use Moose;
  4         10  
  4         34  
9 4     4   31914 use MooseX::Params::Validate;
  4         13  
  4         37  
10             use Lab::Moose::Instrument
11 4     4   1988 qw/validated_getter validated_setter setter_params/;
  4         9  
  4         280  
12 4     4   33 use Carp;
  4         12  
  4         270  
13 4     4   1834 use Lab::Moose::Instrument::Cache;
  4         11  
  4         25  
14 4     4   2699 use Time::HiRes qw/time sleep/;
  4         12  
  4         45  
15              
16 4     4   581 use namespace::autoclean;
  4         10  
  4         29  
17              
18             extends 'Lab::Moose::Instrument';
19              
20             has [
21             qw/
22             max_units_per_second
23             max_units_per_step
24             min_units
25             max_units
26             /
27             ] => ( is => 'ro', isa => 'Num', required => 1 );
28              
29             has source_level_timestamp => (
30             is => 'rw',
31             isa => 'Num',
32             init_arg => undef,
33             );
34              
35             has verbose => (
36             is => 'ro',
37             isa => 'Bool',
38             default => 1
39             );
40              
41             #
42             # Internal variables for continous sweep
43             #
44              
45             has _sweep_start_time => (
46             is => 'rw',
47             isa => 'Lab::Moose::PosNum',
48             );
49              
50             has _active => (
51             is => 'rw',
52             isa => 'Bool',
53             default => 0
54             );
55              
56             has _sweep_target_level => (
57             is => 'rw',
58             isa => 'Num',
59             );
60              
61             has _sweep_start_level => (
62             is => 'rw',
63             isa => 'Num',
64             );
65              
66             has _sweep_rate => (
67             is => 'rw',
68             isa => 'Num',
69             );
70              
71             with qw(
72             Lab::Moose::Instrument::Common
73             Lab::Moose::Instrument::LinearStepSweep
74             Lab::Moose::Instrument::SCPI::Source::Function
75             Lab::Moose::Instrument::SCPI::Source::Level
76             Lab::Moose::Instrument::SCPI::Source::Range
77             );
78              
79             sub BUILD {
80 18     18 0 44 my $self = shift;
81 18         127 $self->clear();
82 18         111 $self->cached_source_level(0);
83 18         89 $self->cached_source_function('VOLT');
84              
85             # $self->cls();
86              
87             # FIXME: check protect params
88             }
89              
90              
91             sub set_level {
92 167     167 0 739 my ( $self, $value, %args ) = validated_setter(
93             \@_,
94             value => { isa => 'Num' },
95             );
96              
97 167         5802 return $self->linear_step_sweep(
98             to => $value, verbose => $self->verbose,
99             %args
100             );
101             }
102              
103             sub cached_level {
104 5     5 0 28 my $self = shift;
105 5         22 return $self->get_level(@_);
106             }
107              
108             # return negative if sweep is done
109             sub _remaining_sweep_time {
110 25     25   70 my $self = shift;
111 25 50       986 if ( not $self->_active ) {
112 0         0 croak "no sweep";
113             }
114 25         1162 my $start_time = $self->_sweep_start_time();
115 25         999 my $rate = $self->_sweep_rate;
116 25         1039 my $target_level = $self->_sweep_target_level;
117 25         1077 my $start_level = $self->_sweep_start_level;
118              
119 25 100       143 my $sgn = $target_level >= $start_level ? 1 : -1;
120 25         129 my $duration = abs( ( $target_level - $start_level ) / $rate );
121 25         196 return $duration - ( time() - $start_time );
122             }
123              
124             sub get_level {
125 221     221 0 3002354 my ( $self, %args ) = validated_getter( \@_ );
126              
127 221 100       123439 if ( $self->_active() ) {
128              
129             # in sweep
130              
131 14         118 my $remaining_time = $self->_remaining_sweep_time();
132              
133 14         752 my $start_time = $self->_sweep_start_time();
134 14         702 my $rate = $self->_sweep_rate;
135 14         626 my $target_level = $self->_sweep_target_level;
136 14         719 my $start_level = $self->_sweep_start_level;
137              
138 14 100       97 my $sgn = $target_level >= $start_level ? 1 : -1;
139 14         63 my $duration = abs( ( $target_level - $start_level ) / $rate );
140 14 100       64 if ( $remaining_time < 0 ) {
141 3         131 $self->_active(0);
142 3         92 $self->source_level( value => $target_level );
143 3         162 return $target_level;
144             }
145             else {
146 11         111 return $start_level + $sgn * $rate * ( time - $start_time );
147             }
148             }
149             else {
150 207         716 return $self->cached_source_level();
151             }
152             }
153              
154             sub config_sweep {
155 5     5 0 16 my $self = shift;
156 5         34 my %args = @_;
157 5 50       233 if ( $self->_active ) {
158 0         0 croak("config_sweep called while instrument is active");
159             }
160 5         23 my $target = $args{point};
161 5         15 my $rate = abs( $args{rate} );
162 5         21 $self->_sweep_start_level( $self->cached_level );
163 5         268 $self->_sweep_target_level($target);
164 5         198 $self->_sweep_rate($rate);
165             }
166              
167             sub trg {
168 5     5 0 18 my $self = shift;
169 5         196 $self->_active(1);
170 5         269 $self->_sweep_start_time( time() );
171             }
172              
173             sub wait {
174 2     2 0 1029 my $self = shift;
175 2         77 my $start_time = $self->_sweep_start_time();
176 2         250 my $rate = $self->_sweep_rate;
177 2         80 my $target_level = $self->_sweep_target_level;
178 2         69 my $start_level = $self->_sweep_start_level;
179 2         11 my $duration = abs( ( $target_level - $start_level ) / $rate );
180              
181 2 100       17 if ( $start_time + $duration > time() ) {
182 1         996982 sleep( $start_time + $duration - time() );
183             }
184              
185             # Sweep done
186 2         199 $self->_active(0);
187 2         18 $self->source_level( value => $target_level );
188             }
189              
190             sub active {
191 14     14 0 2000941 my $self = shift;
192 14 100       655 if ( not $self->_active ) {
193 3         26 return 0;
194             }
195 11 100       62 if ( $self->_remaining_sweep_time > 0 ) {
196 10         72 return 1;
197             }
198 1         16 return 0;
199             }
200              
201             __PACKAGE__->meta()->make_immutable();
202              
203             1;
204              
205             __END__
206              
207             =pod
208              
209             =encoding UTF-8
210              
211             =head1 NAME
212              
213             Lab::Moose::Instrument::DummySource - Dummy YokogawaGS200 source for use with 'Debug' connection
214              
215             =head1 VERSION
216              
217             version 3.880
218              
219             =head1 SYNOPSIS
220              
221             use Lab::Moose;
222              
223             my $source = instrument(
224             type => 'DummySource',
225             connection_type => 'Debug',
226             connection_options => {verbose => 0},
227             # mandatory protection settings
228             max_units_per_step => 0.001, # max step is 1mV/1mA
229             max_units_per_second => 0.01,
230             min_units => -10,
231             max_units => 10,
232             );
233              
234             # Step-sweep to new level.
235             # Stepsize and speed is given by (max|min)_units* settings.
236             $source->set_level(value => 9);
237              
238             # Get current level from device cache (without sending a query to the
239             # instrument):
240             my $level = $source->cached_level();
241              
242             =head1 COPYRIGHT AND LICENSE
243              
244             This software is copyright (c) 2023 by the Lab::Measurement team; in detail:
245              
246             Copyright 2017 Simon Reinhardt
247             2020 Andreas K. Huettel, Simon Reinhardt
248              
249              
250             This is free software; you can redistribute it and/or modify it under
251             the same terms as the Perl 5 programming language system itself.
252              
253             =cut