blib/lib/IO/HyCon.pm

filter: Statementstmt 13.0% 32 / 246 Branchbran 0.0% 0 / 128 Conditioncond 0.0% 0 / 29 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 24.3% 9 / 37 Podpod 100% 28 / 28 totaltotal 14.2% 69 / 486 CC 85 SCAR 36.3
CC
85
Cov
8%
new
152.3
set_pt
90.0
get_response
42.0
CRAP 37.7
↑ Summary
1 #
2 # The HyCon-package provides an object oriented interface to the HYCON
3 # hybrid controller for the Analogparadigm Model-1 analog computer.
4 #
5 # 06-AUG-2016 B. Ulmann Initial version
6 # 07-AUG-2016 B. Ulmann Added extensive error checking, changed
7 # c-/C-commands for easier interfacing
8 # 08-AUG-2016 B. Ulmann Analog calibration capability added
9 # 31-AUG-2016 B. Ulmann Support of digital potentiometers
10 # 01-SEP-2016 B. Ulmann Initial potentiometer setting based on
11 # configuration file etc.
12 # 13-MAY-2017 B. Ulmann Start adaptation to new, AVR2560-based hybrid
13 # controller with lots of new features
14 # 16-MAY-2017 B. Ulmann single_run_sync() implemented
15 # 08-FEB-2018 B. Ulmann Changed read_element to expect the name of a
16 # computing element instead of its address
17 # 01-SEP-2018 B. Ulmann Adapted to the final implementation of the
18 # hybrid controller (version 0.4)
19 # 02-SEP-2018 B. Ulmann Bug fixes, get_response wasn't implemented too
20 # cleverly, it is now much faster than before :-)
21 # 13-SEP-2018 B. Ulmann Fixed a warning problem when used with
22 # hc_gui.pl
23 # 20-FEB-2019 B. Ulmann Changed the reset routine within new since the
24 # old one sometimes failed
25 # 31-JUL-2019 B. Ulmann read_elements() does no longer implicitly halt
26 # the analog computer!
27 # set_pt() now limits values outside of the
28 # interval [-1, +1] to -1/+1 and croaks.
29 # 05-SEP-2019 B. Ulmann Added set_ro_group and read_ro_group functions.
30 # 11-SEP-2019 B. Ulmann Made HyCon into a proper Perl module suitable
31 # for CPAN.
32 # 12-SEP-2019 B. Ulmann Added requirements to Makefile.PL which were
33 # missing.
34 # 15-SEP-2019 B. Ulmann Fixed some typos in the POD.
35 #
36  
37 package IO::HyCon;
38  
39 =pod
40  
41 =head1 NAME
42  
43 IO::HyCon - Perl interface to the Analog Paradigm hybrid controller.
44  
45 =head1 VERSION
46  
47 This document refers to version 0.1 of HyCon
48  
49 =head1 SYNOPSIS
50  
51 use strict;
52 use warnings;
53  
54 use File::Basename;
55 use HyCon;
56  
57 (my $config_filename = basename($0)) =~ s/\.pl$//;
58 print "Create object...\n";
59 my $ac = HyCon->new("$config_filename.yml");
60  
61 $ac->set_ic_time(500); # Set IC-time to 500 ms
62 $ac->set_op_time(1000); # Set OP-Time to 1000 ms
63 $ac->single_run(); # Perform a single computation run
64  
65 # Read a value from a specific computing element:
66 my $element_name = 'SUM8-0';
67 my $value = $ac->read_element($element_name);
68  
69 =head1 DESCRIPTION
70  
71 This module implements a simple object oriented interface to the Arduino based
72 Analog Paradigm hybrid controller which interfaces an analog computer to a
73 digital computer and thus allows true hybrid computation.
74  
75 =cut
76  
77 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
78 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
79  
80 1 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
81 1 our $VERSION = '0.1_3';
82  
83 1 ▶use YAML qw(LoadFile);
Subroutine BEGIN: called CC=1 SCAR=0.0
84 1 ▶use Carp qw(confess cluck carp);
Subroutine BEGIN: called CC=1 SCAR=0.0
85 1 ▶use Device::SerialPort;
Subroutine BEGIN: called CC=1 SCAR=0.0
86 1 ▶use Time::HiRes qw(usleep);
Subroutine BEGIN: called CC=1 SCAR=0.0
87  
88 use constant {
89 1 DIGITAL_OUTPUT_PORTS => 8,
90 DIGITAL_INPUT_PORTS => 8,
91 BUILTIN_DPT => 8,
92 BUILTIN_DPT_RESOLUTION => 10,
93 1 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
94  
95 1 my $instance;
96  
97 =head1 Functions and methods
98  
99 =head2 new($filename)
100  
101 This function generates a HyCon-object. Currently there is only one hybrid
102 controller supported, so this is, in fact, a singleton and every subsequent
103 invocation will cause a fatal error. This function expects a path to a YAML
104 configuration file of the following structure:
105  
106 config.yml:
107 serial:
108 port: /dev/tty.usbmodem621
109 bits: 8
110 baud: 250000
111 parity: none
112 stopbits: 1
113 poll_interval: 1000
114 poll_attempts: 200
115 builtin_dpt:
116 values: .1, .2, .3, .4, .5, .6, .7, .8
117 types:
118 0: PS
119 1: SUM8
120 2: INT4
121 3: PT8
122 4: CU
123 5: MLT8
124 6: MDS2
125 7: CMP4
126 8: HC
127 elements:
128 Y_DDOT: 0x0100
129 Y_DOT: 0x0101
130 PT_8-0: 0x0220
131 PT_8-1: 0x0221
132 PT_8-2: 0x0222
133 PT_8-3: 0x0223
134 PT_8-4: 0x0224
135 PT_8-5: 0x0225
136 PT_8-6: 0x0226
137 PT_8-7: 0x0227
138 manual_potentiometers:
139 PT_8-0,rT_8-1,PT_8-2,PT_8-3,PT_8-4,PT_8-5,PT_8-6,PT_8-7
140  
141 The setup shown above will not fit your particular analog computer
142 configuration; it just serves as an example. The remaining parameters
143 nevertheless apply in general and are mostly self-explanatory. 'poll_interval'
144 and 'poll_attempts' control how often this interface will poll the hybrid
145 controller to get a response to a command issued before. The values shown above
146 are overly pessimistic but this won't matter during normal operation.
147  
148 The section labeled 'builtin_dpt' contains data to setup and control the
149 built-in digital potentiometers of the hybrid controller (currently only eight
150 such potentiometers are supported by the hardware). The initial values of
151 these potentiometers can be specified by a list containing eight entries
152 following the key 'values'. If this part is missing, initial values of 0 are
153 assumed. The new() function will set all digitally controlled potentiometers of
154 the hybrid computer according to these values upon invocation.
155  
156 If the number of values specified in the array 'values' does not match the
157 number of configured potentiometers, the function will abort.
158  
159 The 'types' section contains the mapping of the devices types as returned by
160 the analog computer's readout system to their module names. This should not
161 be changed but will be expanded when new analog computer modules will be
162 developed.
163  
164 The 'elements' section contains a list of computing elements defined by an
165 arbitrary name and their respective address in the computer system. Calling
166 read_all_elements() will switch the computer into HALT-mode, read the
167 values of all elements in this list and return a reference to a hash
168 containing all values and IDs of the elements read. (If jitter during readout
169 is to be minimized, a readout-group should be defined instead, see below.)
170  
171 Ideally, all manual potentiometers are listed under
172 'manual_potentiometers' which is used for automatic readout of the settings
173 of these potentiometers by calling read_mpts(). This is useful if a
174 simulation has been parameterized manually and these parameters are required
175 for documentation purposes or the like. Caution: All potentiometers to be read
176 out by read_mpts() must be defined in the elements-section.
177  
178 The new() function will clear the communication buffer of the hybrid
179 controller by reading and discarding and data until a timeout will be
180 reached. This currently equals the product of 'poll_interval' and
181 'poll_attempts' and may take a few seconds during startup.
182  
183 =cut
184  
185 sub new {
186 0 ▶ my ($class, $config_filename) = @_;
Subroutine new: not called CC=13 SCAR=50.3
Pod documented
187  
188 0 ▶ confess "Only one instance of a HyCon-object at a time is supported!"
Branch: 1 branch
BranchTrueFalseTotal
if $instance++ 0 0 0
189 if $instance++;
190  
191 0 ▶ my $config = LoadFile($config_filename) or
Branch: 1 branch
BranchTrueFalseTotal
my $config = LoadFile($config_filename) or confess("Could not read configuration YAML-file: $!") 0 0 0
192 confess "Could not read configuration YAML-file: $!";
193  
194 0 ▶ my $port = Device::SerialPort->new($config->{serial}{port}) or
Branch: 1 branch
BranchTrueFalseTotal
my $port = "Device::SerialPort"->new($config->{"serial"}{"port"}) or confess("Unable to open USB-port: $!\n") 0 0 0
195 confess "Unable to open USB-port: $!\n";
196 0 $port->databits($config->{serial}{bits});
197 0 $port->baudrate($config->{serial}{baud});
198 0 $port->parity($config->{serial}{parity});
199 0 $port->stopbits($config->{serial}{stopbits});
200  
201 # If no poll-interval is specified, use 1000 microseconds
202 0 ▶ $config->{serial}{poll_interval} //= 1000;
Condition0%0/2
$config->{"serial"}{"poll_interval"} //= 1000
l!l
00
MC/DC0%0/1
$config->{"serial"}{"poll_interval"} //= 1000
$config->{"serial"}{"poll_interval"}
Truth table0%0/2
A
A= $config->{"serial"}{"poll_interval"}
Aresult
11
00
203 0 ▶ $config->{serial}{poll_attempts} //= 200; # and 200 such intervals.
Condition0%0/2
$config->{"serial"}{"poll_attempts"} //= 200
l!l
00
MC/DC0%0/1
$config->{"serial"}{"poll_attempts"} //= 200
$config->{"serial"}{"poll_attempts"}
Truth table0%0/2
A
A= $config->{"serial"}{"poll_attempts"}
Aresult
11
00
204  
205 # Get rid of any data which might still be in the serial line buffer
206 0 for my $i (1 .. 10) {
207 0 ▶ last if $port->lookfor();
Branch: 1 branch
BranchTrueFalseTotal
if $port->lookfor 0 0 0
208 }
209  
210 # Now reset the controller
211 0 print "Resetting the hybrid controller...\n";
212  
213 0 my ($attempt, $data);
214 0 for my $i (1 .. 10) {
215 0 print "Reset attempt $i\n";
216 0 $port->write('x'); # Reset the hybrid controller
217 0 sleep(1);
218 0 ▶ last if ($data = $port->lookfor()) eq 'RESET';
Branch: 1 branch
BranchTrueFalseTotal
if ($data = $port->lookfor) eq "RESET" 0 0 0
219 }
220 0 ▶ confess "Unexpected response from controller: >>$data<<\n" unless $data eq 'RESET';
Branch: 1 branch
BranchTrueFalseTotal
unless $data eq "RESET" 0 0 0
221 # print "Lookfor: ", $port->lookfor(), "\n";
222 # while (++$attempt < $config->{serial}{poll_attempts}) {
223 # $data = $port->lookfor();
224 # last OUTER if $data eq 'RESET';
225 # usleep($config->{serial}{poll_interval});
226 # }
227 # }
228  
229 # Create array of initial potentiometer values - if there is nothing
230 # specified assume zero:
231 my $pv = defined($config->{builtin_dpt}{values})
232 ? [ split(/\s*,\s*/, $config->{builtin_dpt}{values}) ]
233 0 ▶ : [ map{0}(1 .. BUILTIN_DPT) ];
Branch: 1 branch
BranchTrueFalseTotal
defined $config->{'builtin_dpt'}{'values'} ? : 0 0 0
234  
235 # Create the actual object
236 0 my $object;
237 {
238 1 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.2
239 $object = bless(my $self = {
240 port => $port,
241 poll_interval => $config->{serial}{poll_interval},
242 poll_attempts => $config->{serial}{poll_attempts},
243 builtin_dpt => {
244 number => BUILTIN_DPT,
245 resolution => BUILTIN_DPT_RESOLUTION,
246 values => $pv,
247 },
248 elements => $config->{elements},
249 types => $config->{types},
250 times => {
251 ic_time => -1,
252 op_time => -1,
253 },
254 manual_potentiometers =>
255 0 [ split(/\s*,\s*/, $config->{manual_potentiometers}) ],
256 }, $class);
257 }
258  
259 # Initial potentiometer setup
260 0 set_pt($object, $_, $pv->[$_]) for (0 .. BUILTIN_DPT - 1);
261  
262 0 return $object;
263 }
264  
265 =head2 get_response()
266  
267 In some cases, e.g. external HALT conditions, it is necessary to query the
268 hybrid controller for any messages which may have occured since the last
269 command. This can be done with this method - it will poll the controller
270 for a period of 'poll_interval' times 'poll_attemps' microseconds. If this
271 timeout value is not suitable, a different value (in milliseconds) can be
272 supplied as first argument of this method. If this argument is zero or negative,
273 get_response will wait indefinitely for a response from the hybrid controller.
274  
275 =cut
276  
277 sub get_response {
278 0 ▶ my ($self, $timeout) = @_;
Subroutine get_response: not called CC=6 SCAR=37.4
Pod documented
279 0 ▶ $timeout = $self->{poll_interval} unless defined($timeout);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $timeout 0 0 0
280  
281 0 my $attempt;
282 do {
283 0 my $response = $self->{port}->lookfor();
284 0 ▶ return $response if $response;
Branch: 1 branch
BranchTrueFalseTotal
if $response 0 0 0
285 # If we poll indefinitely, there is no need to wait at all
286 0 ▶ usleep($timeout) if $timeout > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $timeout > 0 0 0 0
287 0 ▶ } while ($timeout < 1 or ++$attempt < $self->{poll_attempts});
Branch: 1 branch
BranchTrueFalseTotal
$timeout < 1 || ++$attempt < $self->{"poll_attempts"} and do { do { my $response = $self->{"port"}->lookfor; return $response if $response; &usleep($timeout) if $timeout > 0 } } 0 0 0
Condition0%0/3
$timeout < 1 or ++$attempt < $self->{"poll_attempts"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$timeout < 1 or ++$attempt < $self->{"poll_attempts"}
$timeout < 1 ++$attempt < $self->{"poll_attempts"}
Truth table0%0/3
A or B
A= $timeout < 1B= ++$attempt < $self->{"poll_attempts"}
ABresult
1X1
011
000
288 }
289  
290 =head2 ic()
291  
292 This method switches the analog computer to IC (initial condition) mode
293 during which the integrators are (re)set to their respective initial value.
294 Since this involves charging a capacitor to a given value, this mode should
295 be activated for the a minimum duration as required by the time scale factors
296 involved.
297  
298 ic() and the two following methods should not be used when timing is critical.
299 Instead, IC- and OP-times should be setup explicitly (see below) and then a
300 single-run should be initiated which will be under control of the hybrid
301 controller. This avoids latencies involved with the communication to and from
302 the hybrid controller and allows sub-millisecond resolution.
303  
304 =head2 op()
305  
306 This method switches the analog computer to operating-mode.
307  
308 =head2 halt()
309  
310 Calling this method causes the analog computer to switch to HALT-mode. In
311 this mode the integrators are halted and store their last value. After
312 calling halt() it is possible to return to OP-mode by calling op() again.
313 Depending on the analog computer being controlled, there will be a more or
314 less substantial drift of the integrators in HALT-mode, so it is advisable
315 to keep the HALT-periods as short as possible to minimize errors.
316  
317 A typical operation cycle may look like this: IC-OP-HALT-OP-HALT-OP-HALT.
318 This would start a single computation with the possibility of reading
319 values from the analog computer during the HALT-intervals.
320  
321 Another typical cycle is called 'repetitive operation' and looks like this:
322 IC-OP-IC-OP-IC-OP... This is normally used with the integrators set to
323 time-constants of 100 or 1000 and allows to display a solution as a more or
324 less flicker free curve on an oscilloscope for example.
325  
326 =head2 enable_ovl_halt()
327  
328 During a normal computation on an analog computation there should be no
329 overloads of summers or integrators. Such overload conditions are typically
330 the result of an erroneous computer setup (normally caused by wrong scaling of
331 the underlying equations). To catch such problems it is usually a good idea to
332 switch the analog computer automatically to HALT-mode when an overload occurs.
333 The computing element(s) causing the overload condition can the easily
334 identified on the analog computer's console and the variables of the computation
335 run can be read out to identify the cause of the problem.
336  
337 =head2 disable_ovl_halt()
338  
339 Calling this method will disable the automatic halt-on-overload
340 functionality of the hybrid controller.
341  
342 =head2 enable_ext_halt()
343  
344 Sometimes it is necessary to halt a computation when some condition is
345 satisfied (some value reached etc.). This is normally detected by a
346 comparator used in the analog computer setup. The hybrid controller
347 features an EXT-HALT input jack that can be connected to such a comparator.
348 After calling this method, the hybrid controller will switch the analog
349 computer from OP-mode to HALT as soon as the input signal patched to this
350 input jack goes high.
351  
352 =head2 disable_ext_halt()
353  
354 This method disables the HALT-on-overflow feature of the hybrid controller.
355  
356 =head2 single_run()
357  
358 Calling this method will initiate a so-called 'single-run' on the analog
359 computer which automatically performs the sequence IC-OP-HALT. The times
360 spent in IC- and OP-mode are specified with the methods set_ic_time() and
361 set_op_time() (see below).
362  
363 It should be noted that the hybrid controller will not be blocked during
364 such a single-run - it is still possible to issue other commands to read or
365 set ports etc.
366  
367 =head2 single_run_sync()
368  
369 This function behaves quite like single_run() but waits for the termination
370 of the single run, thus blocking any further program execution. This method
371 returns true if the single-run mode was terminated by an external halt
372 condition. undef is returned otherwise.
373  
374 =head2 repetitive_run()
375  
376 This initiates repetitive operation, i.e. the analog computer is commanded
377 to perform an IC-OP-IC-OP-... sequence. The hybrid controller will not block
378 during this sequence. To terminate a repetitive run, either ic() or halt()
379 may be called. Note that these methods act immediately and will interrupt any
380 ongoing IC- or OP-period of the analog computer.
381  
382 =head2 pot_set()
383  
384 This function switches the analog computer to POTSET-mode i.e. the
385 integrators are set implicitly to HALT, while all (manual) potentiometers
386 are connected to +1 on their respective input side. This mode can be used
387 to read the current settings of the potentiometers.
388  
389 =cut
390  
391 # Create basic methods
392 1 my %methods = (
393 ic => ['i', '^IC'], # Switch AC to IC-mode
394 op => ['o', '^OP'], # Switch AC to OP-mode
395 halt => ['h', '^HALT'], # Switch AC to HALT-mode
396 disable_ovl_halt => ['a', '^OVLH=DISABLED'], # Disable HALT-on-overflow
397 enable_ovl_halt => ['A', '^OVLH=ENABLED'], # Enable HALT-on-overflow
398 disable_ext_halt => ['b', '^EXTH=DISABLED'], # Disable external HALT
399 enable_ext_halt => ['B', '^EXTH=ENABLED'], # Enable external HALT
400 repetitive_run => ['e', '^REP-MODE'], # Switch to RepOp
401 single_run => ['E', '^SINGLE-RUN'], # One IC-OP-HALT-cycle
402 pot_set => ['S', '^PS'], # Activate POTSET-mode
403 );
404  
405 eval ('
406 sub ' . $_ . ' {
407 my ($self) = @_;
408 $self->{port}->write("' . $methods{$_}[0] . '");
409 my $response = get_response($self);
410 confess "No response from hybrid controller! Command was \'' .
411 $methods{$_}[0] . '\'." unless $response;
412 confess "Unexpected response from hybrid controller:\\n\\tCOMMAND=\'' .
413 $methods{$_}[0] . '\', RESPONSE=\'$response\', PATTERN=\'' .
414 $methods{$_}[1] . '\'\\n"
415 if $response !~ /' . $methods{$_}[1] . '/;
416 }
417 0 ▶') for keys(%methods);
Subroutine disable_ext_halt: not called CC=3 SCAR=24.6
Subroutine disable_ovl_halt: not called CC=3 SCAR=24.6
Subroutine enable_ext_halt: not called CC=3 SCAR=24.6
Subroutine enable_ovl_halt: not called CC=3 SCAR=24.6
Subroutine halt: not called CC=3 SCAR=24.6
Subroutine ic: not called CC=3 SCAR=24.6
Subroutine op: not called CC=3 SCAR=24.6
Subroutine pot_set: not called CC=3 SCAR=24.6
Subroutine repetitive_run: not called CC=3 SCAR=24.6
Subroutine single_run: not called CC=3 SCAR=24.6
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Branch: 20 branches
BranchTrueFalseTotal
unless $response 0 0 0
if not $response =~ /^EXTH=DISABLED/ 0 0 0
unless $response 0 0 0
if not $response =~ /^OVLH=DISABLED/ 0 0 0
unless $response 0 0 0
if not $response =~ /^EXTH=ENABLED/ 0 0 0
unless $response 0 0 0
if not $response =~ /^OVLH=ENABLED/ 0 0 0
unless $response 0 0 0
if not $response =~ /^HALT/ 0 0 0
unless $response 0 0 0
if not $response =~ /^IC/ 0 0 0
unless $response 0 0 0
if not $response =~ /^OP/ 0 0 0
unless $response 0 0 0
if not $response =~ /^PS/ 0 0 0
unless $response 0 0 0
if not $response =~ /^REP-MODE/ 0 0 0
unless $response 0 0 0
if not $response =~ /^SINGLE-RUN/ 0 0 0
418  
419 sub single_run_sync() {
420 0 ▶ my ($self) = @_;
Subroutine single_run_sync: not called CC=6 SCAR=37.4
Pod documented
421 0 $self->{port}->write('F');
422 0 my $response = get_response($self);
423 0 ▶ confess "No Response from hybrid controller! Command was 'F'"
Branch: 1 branch
BranchTrueFalseTotal
unless $response 0 0 0
424 unless $response;
425 0 ▶ confess "Unexpected response:\n\tCOMMAND='F', RESPONSE='$response'\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $response =~ /^SINGLE-RUN/ 0 0 0
426 if $response !~ /^SINGLE-RUN/;
427 0 my $timeout = 1.1 * ($self->{times}{ic_time} + $self->{times}{op_time});
428 0 $response = get_response($self, $timeout);
429 0 ▶ confess "No Response during single_run_sync within $timeout ms"
Branch: 1 branch
BranchTrueFalseTotal
unless $response 0 0 0
430 unless $response;
431 0 ▶ confess "Unexpected response after single_run_sync: '$response'\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $response =~ /^EOSR/ or $response =~ /^EOSRHLT/ 0 0 0
Condition0%0/3
$response =~ /^EOSR/ or $response =~ /^EOSRHLT/
l!l&&r!l&&!r
000
MC/DC0%0/2
$response =~ /^EOSR/ or $response =~ /^EOSRHLT/
$response =~ /^EOSR/ $response =~ /^EOSRHLT/
Truth table0%0/3
A or B
A= $response =~ /^EOSR/B= $response =~ /^EOSRHLT/
ABresult
1X1
011
000
432 if $response !~ /^EOSR/ and $response !~ /^EOSRHLT/;
433 # Return true if the run was terminated by an external halt condition
434 0 return $response =~ /^EOSRHLT/;
435 }
436  
437 =head2 set_ic_time($milliseconds)
438  
439 It is normally advisable to let the hybrid controller take care of the overall
440 timing of OP and IC operations since the communication with the digital host
441 introduces quite some jitter. This method sets the time the analog computer
442 will spend in IC-mode during a single- or repetitive run. The time is
443 specified in milliseconds and must be positive and can not exceed 999999
444 milliseconds due to limitations of the hybrid controller firmware.
445  
446 =cut
447  
448 # Set IC-time
449 sub set_ic_time {
450 0 ▶ my ($self, $ic_time) = @_;
Subroutine set_ic_time: not called CC=5 SCAR=34.0
Pod documented
451 0 ▶ confess 'IC-time out of range - must be >= 0 and <= 999999!'
Branch: 1 branch
BranchTrueFalseTotal
if $ic_time < 0 or $ic_time > 999999 0 0 0
Condition0%0/3
$ic_time < 0 or $ic_time > 999999
l!l&&r!l&&!r
000
MC/DC0%0/2
$ic_time < 0 or $ic_time > 999999
$ic_time < 0 $ic_time > 999999
Truth table0%0/3
A or B
A= $ic_time < 0B= $ic_time > 999999
ABresult
1X1
011
000
452 if $ic_time < 0 or $ic_time > 999999;
453 0 my $pattern = "^T_IC=$ic_time\$";
454 0 my $command = sprintf("C%06d", $ic_time);
455 0 $self->{port}->write($command);
456 0 my $response = get_response($self);
457 0 ▶ confess 'No response from hybrid controller!' unless $response;
Branch: 1 branch
BranchTrueFalseTotal
unless $response 0 0 0
458 0 ▶ confess "Unexpected response: '$response', expected: '$pattern'"
Branch: 1 branch
BranchTrueFalseTotal
unless $response =~ /$pattern/ 0 0 0
459 if $response !~ /$pattern/;
460 0 $self->{times}{ic_time} = $ic_time;
461 }
462  
463 =head2 set_op_time($milliseconds)
464  
465 This method specifies the duration of the OP-cycle(s) during a single- or
466 repetitive analog computer run. The same limitations hold with respect to the
467 value specified as for the set_ic_time() method.
468  
469 =cut
470  
471 # Set OP-time
472 sub set_op_time {
473 0 ▶ my ($self, $op_time) = @_;
Subroutine set_op_time: not called CC=5 SCAR=34.0
Pod documented
474 0 ▶ confess 'OP-time out of range - must be >= 0 and <= 999999!'
Branch: 1 branch
BranchTrueFalseTotal
if $op_time < 0 or $op_time > 999999 0 0 0
Condition0%0/3
$op_time < 0 or $op_time > 999999
l!l&&r!l&&!r
000
MC/DC0%0/2
$op_time < 0 or $op_time > 999999
$op_time < 0 $op_time > 999999
Truth table0%0/3
A or B
A= $op_time < 0B= $op_time > 999999
ABresult
1X1
011
000
475 if $op_time < 0 or $op_time > 999999;
476 0 my $pattern = "^T_OP=$op_time\$";
477 0 my $command = sprintf("c%06d", $op_time);
478 0 $self->{port}->write($command);
479 0 my $response = get_response($self);
480 0 ▶ confess 'No response from hybrid controller!' unless $response;
Branch: 1 branch
BranchTrueFalseTotal
unless $response 0 0 0
481 0 ▶ confess "Unexpected response: '$response', expected: '$pattern'"
Branch: 1 branch
BranchTrueFalseTotal
unless $response =~ /$pattern/ 0 0 0
482 if $response !~ /$pattern/;
483 0 $self->{times}{op_time} = $op_time;
484 }
485  
486 =head2 read_element($name)
487  
488 This function expects the name of a computing element specified in the
489 configuation YML-file and applies the corresponding 16 bit value $address to
490 the address lines of the analog computer's bus system, asserts the active-low
491 /READ-line, reads one value from the READOUT-line, and de-asserts /READ again.
492 read_element(...) returns a reference to a hash containing the keys 'value' and
493 'id'.
494  
495 =cut
496  
497 sub read_element {
498 0 ▶ my ($self, $name) = @_;
Subroutine read_element: not called CC=4 SCAR=30.0
Pod documented
499 0 my $address = hex($self->{elements}{$name});
500 0 ▶ confess "Computing element $name not configured!\n"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $address 0 0 0
501 unless defined($address);
502 0 $self->{port}->write('g' . sprintf("%04X", $address & 0xffff));
503 0 my $response = get_response($self);
504 0 ▶ confess 'No response from hybrid controller!' unless $response;
Branch: 1 branch
BranchTrueFalseTotal
unless $response 0 0 0
505 0 my ($value, $id) = split(/\s+/, $response);
506 0 ▶ $id = $self->{types}{$id & 0xf} || 'UNKNOWN';
Condition0%0/2
$self->{'types'}{$id & 15} || 'UNKNOWN'
l!l
00
MC/DC0%0/1
$self->{'types'}{$id & 15} || 'UNKNOWN'
$self->{'types'}{$id & 15}
Truth table0%0/2
A
A= $self->{'types'}{$id & 15}
Aresult
11
00
507 0 return { value => $value, id => $id};
508 }
509  
510 =head2 read_element_by_address($address)
511  
512 This function expects the 16 bit address address of a computing element as
513 parameter and returns a data structure identically to that returned by
514 read_element. This routine should not be used in general as computing elements
515 are better addressed by their name. It is mainly provided for completeness.
516  
517 =cut
518  
519 sub read_element_by_address {
520 0 ▶ my ($self, $address) = @_;
Subroutine read_element_by_address: not called CC=3 SCAR=24.8
Pod documented
521 0 $self->{port}->write('g' . sprintf("%04X", $address & 0xffff));
522 0 my $response = get_response($self);
523 0 ▶ confess 'No response from hybrid controller!' unless $response;
Branch: 1 branch
BranchTrueFalseTotal
unless $response 0 0 0
524 0 my ($value, $id) = split(/\s+/, $response);
525 0 ▶ $id = $self->{types}{$id & 0xf} || 'UNKNOWN';
Condition0%0/2
$self->{'types'}{$id & 15} || 'UNKNOWN'
l!l
00
MC/DC0%0/1
$self->{'types'}{$id & 15} || 'UNKNOWN'
$self->{'types'}{$id & 15}
Truth table0%0/2
A
A= $self->{'types'}{$id & 15}
Aresult
11
00
526 0 return { value => $value, id => $id};
527 }
528  
529 =head2 read_all_elements()
530  
531 The routine read_all_elements() reads the current values from all elements
532 listed in the 'elements' section of the configuration file. It returns a
533 reference to a hash containing all elements read with their associated values
534 and IDs. It may be advisable to switch the analog computer to HALT mode before
535 calling read_all_elements() to minimize the effect of jitter. After calling
536 this routine the computer has to be switched back to OP mode again. A better
537 way to readout groups of elements is by means of a readout-group (see below).
538  
539 =cut
540  
541 sub read_all_elements {
542 0 ▶ my ($self) = @_;
Subroutine read_all_elements: not called CC=2 SCAR=17.9
Pod documented
543 0 my %result;
544 0 for my $key (sort(keys(%{$self->{elements}})))
545 {
546 0 my $result = $self->read_element($key);
547 0 $result{$key} = { value => $result->{value}, id => $result->{id} };
548 }
549 0 return \%result;
550 }
551  
552 =head2 set_ro_group()
553  
554 This function defines a readout group, i.e. a group of computing elements
555 specified by their respective names as defined in the configuration file. All
556 elements of such a readout group can be read by issuing a single call to
557 read_ro_group() thus reducing the communications overhead between the HC and
558 digital computer substantially. A typical call would look like this (provided
559 the names are defined in the configuration file):
560  
561 $ac->set_ro_group('INT0_1', 'SUM2_3');
562  
563 =cut
564  
565 sub set_ro_group {
566 0 ▶ my ($self, @names) = @_;
Subroutine set_ro_group: not called CC=3 SCAR=24.8
Pod documented
567  
568 0 my @addresses;
569 0 for my $name (@names) {
570 confess "Computing element $name not configured!\n"
571 0 ▶ unless defined($self->{elements}{$name});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"elements"}{$name} 0 0 0
572 0 push(@addresses, hex($self->{elements}{$name}));
573 }
574 0 $self->{'RO-GROUP'} = \@names;
575 0 my $command = 'G' . join(';', @addresses) . '.';
576 0 $self->{port}->write($command);
577 }
578  
579 =head2 read_ro_group()
580  
581 read_ro_group() reads all elements defined in a readout group. This minimizes
582 the communications overhead between digital and analog computer and reduces
583 the effect of jitter during readout as well as the risk of a serial line buffer
584 overflow on the side of the hybrid controller. The function returns a reference
585 to a hash containing the names of the elements forming the readout group with
586 their associated values.
587  
588 =cut
589  
590 sub read_ro_group {
591 0 ▶ my ($self) = @_;
Subroutine read_ro_group: not called CC=2 SCAR=17.9
Pod documented
592 0 $self->{port}->write('f'); # Issue read-ro-group command
593 0 my @values = split(/\s*;\s*/, get_response($self));
594 0 my %result;
595 0 $result{$_} = shift(@values) for @{$self->{'RO-GROUP'}};
596 0 return \%result;
597 }
598  
599 =head2 read_digital()
600  
601 In addition to these analog readout capabilities, the hybrid controller also
602 features eight digital inputs which can be used to read the state of
603 comparators or other logic elements of the analog computer being controlled.
604 This method returns an array-reference containing values of 0 or 1 for each of
605 the digital input ports.
606  
607 =cut
608  
609 # Read digital inputs
610 sub read_digital {
611 0 ▶ my ($self) = @_;
Subroutine read_digital: not called CC=3 SCAR=24.8
Pod documented
612 0 $self->{port}->write('R');
613 0 my $response = get_response($self);
614 0 ▶ confess 'No response from hybrid controller!' unless $response;
Branch: 1 branch
BranchTrueFalseTotal
unless $response 0 0 0
615 0 my $pattern = '^' . '\d+\s+' x (DIGITAL_INPUT_PORTS - 1) . '\d+';
616 0 ▶ confess "Unexpected response: '$response', expected: '$pattern'"
Branch: 1 branch
BranchTrueFalseTotal
unless $response =~ /$pattern/ 0 0 0
617 if $response !~ /$pattern/;
618 0 return [ split(/\s+/, $response) ];
619 }
620  
621 =head2 digital_output($port, $value)
622  
623 The hybrid controller also features eight digital outputs which can be used to
624 control the electronic switches which are part of the comparator unit. Calling
625 digital_output(0, 1) will set the first (0) digital output to 1 etc.
626  
627 =cut
628  
629 # Set/reset digital outputs
630 sub digital_output {
631 0 ▶ my ($self, $port, $state) = @_;
Subroutine digital_output: not called CC=4 SCAR=30.0
Pod documented
632 0 ▶ confess '$port must be >= 0 and < ' . DIGITAL_OUTPUT_PORTS
Branch: 1 branch
BranchTrueFalseTotal
if $port < 0 or $port > 8 0 0 0
Condition0%0/3
$port < 0 or $port > 8
l!l&&r!l&&!r
000
MC/DC0%0/2
$port < 0 or $port > 8
$port < 0 $port > 8
Truth table0%0/3
A or B
A= $port < 0B= $port > 8
ABresult
1X1
011
000
633 if $port < 0 or $port > DIGITAL_OUTPUT_PORTS;
634 0 ▶ $self->{port}->write(($state ? 'D' : 'd') . $port);
Branch: 1 branch
BranchTrueFalseTotal
$state ? : 0 0 0
635 0 $self->{'RO-GROUP'} = [];
636 }
637  
638 =head2 read_mpts()
639  
640 Calling read_mpts() returns a reference to a hash containing the current
641 settings of all manual potentiometers listed in the
642 'manual_potentiometers' section in the configuration file. To accomplish this,
643 the analog computer is switched to POTSET-mode (implying HALT for the
644 integrators). In this mode, all inputs of potentiometers are connected to
645 the positive machine unit +1, so that their current setting can be read out.
646 ("Free" potentiometers will behave erroneously unless their second input is
647 connected to ground, refer to the analog computer manual for more information
648 on that topic.)
649  
650 =cut
651  
652 sub read_mpts {
653 0 ▶ my ($self) = @_;
Subroutine read_mpts: not called CC=2 SCAR=17.9
Pod documented
654 0 $self->pot_set();
655 0 my %result;
656 0 for my $key (@{$self->{manual_potentiometers}}) {
657 0 my $result = $self->read_element($key);
658 0 $result{$key} = { value => $result->{value}, id => $result->{id} };
659 }
660 0 return \%result;
661 }
662  
663 =head2 set_pt($address, $value)
664  
665 To set a digital potentiometer, set_pt() is called. The first argument is the
666 address of the potentiometer to be set (0 <= number < number-of-potentiometers
667 as specified in the potentiometers section in the configuration YML-file), the
668 second argument is a floatingpoint value 0 <= v <= 1. If either the address or
669 the value is out of bounds, the function will die.
670  
671 =cut
672  
673 sub set_pt {
674 0 ▶ my ($self, $address, $value) = @_;
Subroutine set_pt: not called CC=9 SCAR=45.0
Pod documented
675 confess "Addr must be >= 0 and < $self->{builtin_dpt}{number}, it is $address"
676 0 ▶ if $address < 0 or $address >= $self->{builtin_dpt}{number};
Branch: 1 branch
BranchTrueFalseTotal
if $address < 0 or $address >= $self->{"builtin_dpt"}{"number"} 0 0 0
Condition0%0/3
$address < 0 or $address >= $self->{"builtin_dpt"}{"number"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$address < 0 or $address >= $self->{"builtin_dpt"}{"number"}
$address < 0 $address >= $self->{"builtin_dpt"}{"number"}
Truth table0%0/3
A or B
A= $address < 0B= $address >= $self->{"builtin_dpt"}{"number"}
ABresult
1X1
011
000
677 0 ▶ if ($value < 0 or $value > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value < 0 or $value > 1) 0 0 0
Condition0%0/3
$value < 0 or $value > 1
l!l&&r!l&&!r
000
MC/DC0%0/2
$value < 0 or $value > 1
$value < 0 $value > 1
Truth table0%0/3
A or B
A= $value < 0B= $value > 1
ABresult
1X1
011
000
678 0 carp "$value must be >= 0 and <= 1, has been limited\n";
679 0 ▶ $value = 1 if $value > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $value > 1 0 0 0
680 0 ▶ $value = 0 if $value < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $value < 0 0 0 0
681 }
682  
683 # Convert value to an integer suitable to setting the potentiometer and
684 # generate fixed length strings for the parameters address (single digit)
685 # and value (three digits, 0000 <= value <= 1023):
686 $value = sprintf('%04d',
687 0 int($value * (2 ** $self->{builtin_dpt}{resolution} - 1)));
688 0 $address = sprintf('%d', $address);
689 0 $self->{port}->write("P$address$value"); # Send command
690 0 my $response = get_response($self); # Get response
691 0 ▶ confess 'No response from hybrid controller!' unless $response;
Branch: 1 branch
BranchTrueFalseTotal
unless $response 0 0 0
692 0 my ($raddress, $rvalue) = $response =~ /^P(\d+)=(\d+)$/;
693 0 ▶ confess "set_pt failed! $address vs. $raddress, $value vs. $rvalue"
Branch: 1 branch
BranchTrueFalseTotal
if $address != $raddress 0 0 0
694 if $address != $raddress; #or $value != $rvalue;
695 }
696  
697 =head2 get_status()
698  
699 Calling get_status() yields a reference to a hash containing all current
700 status information of the hybrid controller. A typical hash structure
701 returned may look like this:
702  
703 $VAR1 = {
704 'IC-time' => '500',
705 'MODE' => 'HALT',
706 'OP-time' => '1000',
707 'STATE' => 'NORM',
708 'OVLH' => 'DIS',
709 'EXTH' => 'DIS',
710 'RO_GROUP' => [..., ..., ...]
711 };
712  
713 In this case the IC-time has been set to 500 ms while the OP-time is set to
714 one second. The analog computer is currently in HALT-mode, and the hybrid
715 controller is in its normal state, i.e. it is not currently performing a
716 single- or repetitive-run. HALT on overload and external HALT are both
717 disabled. A readout-group has been defined, too.
718  
719 =cut
720  
721 sub get_status {
722 0 ▶ my ($self) = @_;
Subroutine get_status: not called CC=3 SCAR=24.8
Pod documented
723 0 $self->{port}->write('s');
724 0 my $response = get_response($self);
725 0 ▶ confess 'No response from hybrid controller!' unless $response;
Branch: 1 branch
BranchTrueFalseTotal
unless $response 0 0 0
726 0 my %state;
727 0 for my $entry (split(/\s*,\s*/, $response)) {
728 0 my ($parameter, $value) = split(/\s*=\s*/, $entry);
729 0 $state{$parameter} = $value;
730 }
731 0 my @addresses = split(/\s*;\s*/, $state{'RO-GROUP'});
732 0 $state{'RO-GROUP'} = \@addresses;
733 0 return \%state;
734 }
735  
736 =head2 get_op_time()
737  
738 In some applications it is useful to be able to determine how long the analog
739 computer has been in OP-mode. As time as such is the only free variable of
740 integration in an analog-electronic analog computer, it is a central parameter
741 to know. Imagine that some integration is being performed by the analog
742 computer and the time which it took to reach some threshold value is of
743 interest. In this case, the hybrid controller would be configured
744 so that external-HALT is enabled. Then the analog computer would be placed to
745 IC-mode and then to OP-mode. After an external HALT has been triggered by some
746 comparator of the analog commputer, the hybrid controller will switch the
747 analog computer to HALT-mode immediately. Afterwards, the time the analog
748 computer spent in OP-mode can be determined by calling this method. The time
749 will be returned in microseconds (the resolution is about +/- 3 to 4
750 microseconds).
751  
752 =cut
753  
754 # Get current time the AC spent in OP-mode
755 sub get_op_time {
756 0 ▶ my ($self) = @_;
Subroutine get_op_time: not called CC=4 SCAR=30.0
Pod documented
757 0 $self->{port}->write('t');
758 0 my $response = get_response($self);
759 0 ▶ confess 'No response from hybrid controller!' unless $response;
Branch: 1 branch
BranchTrueFalseTotal
unless $response 0 0 0
760 0 my $pattern = 't_OP=\-?\d*';
761 0 ▶ confess "Unexpected response: '$response', expected: '$pattern'"
Branch: 1 branch
BranchTrueFalseTotal
unless $response =~ /$pattern/ 0 0 0
762 if $response !~ /$pattern/;
763 0 my ($time) = $response =~ /=\s*(\-?\d+)$/;
764 0 ▶ return $time ? $time : -1;
Branch: 1 branch
BranchTrueFalseTotal
$time ? : 0 0 0
765 }
766  
767 =head2 reset()
768  
769 The reset() method resets the hybrid controller to its initial setup. This
770 will also reset all digital potentiometer settings including their number!
771 During normal operations it should not be necessary to call this method which
772 was included primarily to aid debugging.
773  
774 =cut
775  
776 sub reset {
777 0 ▶ my ($self) = @_;
Subroutine reset: not called CC=3 SCAR=24.8
Pod documented
778 0 $self->{port}->write('x');
779 0 my $response = get_response($self);
780 0 ▶ confess 'No response from hybrid controller!' unless $response;
Branch: 1 branch
BranchTrueFalseTotal
unless $response 0 0 0
781 0 ▶ confess "Unexpected response: '$response', expected: 'RESET'"
Branch: 1 branch
BranchTrueFalseTotal
if $response ne "RESET" 0 0 0
782 if $response ne 'RESET';
783 }
784  
785 =head2 set_address(address)
786  
787 set_address() is used to set the hybrid controller to a different address than
788 its default address of 0x0090. The hybrid controller requires its own address
789 on the backplane in order to set the builtin digital potentiometers. If the
790 controller is placed into another slot than the last one of the main backplane
791 (something which is not recommended), then this method has to be called before
792 any changes to the builtin digitally controlled potentiometer setting are made.
793 If the hybrid controller's address is not set correctly setting the digital
794 potentiometers will fail silently! The address has to be specified in
795 hexadecimal notation with four digits (padded on the left with zeros if
796 necessary).
797  
798 =cut
799  
800 sub set_address() {
801 0 ▶ my ($self, $address) = @_;
Subroutine set_address: not called CC=5 SCAR=34.0
Pod documented
802 0 $self->{port}->write("m$address");
803 0 my $response = get_response($self);
804 0 ▶ confess 'No response from hybrid controller!' unless $response;
Branch: 1 branch
BranchTrueFalseTotal
unless $response 0 0 0
805 0 my ($value) = $response =~ /^MY_ADDR=(.+)$/;
806 0 ▶ confess "Unexpected response: '$response', expected: 'MY_ADDR=...'"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 0 0
807 unless defined($value);
808 0 $_ =~ s/^0+// for $address, $value;
809 0 ▶ confess "Address returned ($value) differs from address sent ($address)!"
Branch: 1 branch
BranchTrueFalseTotal
unless $address == $value 0 0 0
810 unless $address == $value;
811 }
812  
813 =head1 Examples
814  
815 The following example initates a repetitive run of the analog computer with 20
816 ms of operating time and 10 ms IC time:
817  
818 use strict;
819 use warnings;
820  
821 use File::Basename;
822 use HyCon;
823  
824 (my $config_filename = basename($0)) =~ s/\.pl$//;
825 my $ac = HyCon->new("$config_filename.yml");
826  
827 $ac->set_op_time(20);
828 $ac->set_ic_time(10);
829  
830 $ac->repetitive_run();
831  
832 =cut
833  
834 =head1 AUTHOR
835  
836 Dr. Bernd Ulmann, ulmann@analogparadigm.com
837  
838 =cut
839  
840 1 return 1;
↑ Summary