| 1 | package Lab::Instrument::TCD; | ||
| 2 | #ABSTRACT: Temperature control for our Oxford Instruments TLM dilution fridge | ||
| 3 | $Lab::Instrument::TCD::VERSION = '3.899'; | ||
| 4 | use v5.20; | ||
| 5 | |||
| 6 | use strict; | ||
| 7 | use Lab::Instrument; | ||
| 8 | |||
| 9 | our @ISA = ("Lab::Instrument"); | ||
| 10 | |||
| 11 | our %fields = ( | ||
| 12 | supported_connections => [ 'VISA_RS232', 'RS232', 'IsoBus', 'DEBUG' ], | ||
| 13 | |||
| 14 | connection_settings => { | ||
| 15 | baudrate => 9600, | ||
| 16 | databits => 8, | ||
| 17 | stopbits => 1, | ||
| 18 | parity => 'none', | ||
| 19 | handshake => 'none', | ||
| 20 | |||
| 21 | #rs232_echo => 'character', | ||
| 22 | termchar => "\r", | ||
| 23 | timeout => 1 | ||
| 24 | }, | ||
| 25 | |||
| 26 | device_settings => { read_default => 'device' }, | ||
| 27 | |||
| 28 | device_cache => { | ||
| 29 | id => 'Temperature Control', | ||
| 30 | |||
| 31 | #T => undef | ||
| 32 | } | ||
| 33 | |||
| 34 | ); | ||
| 35 | |||
| 36 | sub new { | ||
| 37 | my $proto = shift; | ||
| 38 | my $class = ref($proto) || $proto; | ||
| 39 | my $self = $class->SUPER::new(@_); | ||
| 40 | $self->${ \( __PACKAGE__ . '::_construct' ) }(__PACKAGE__); | ||
| 41 | |||
| 42 | return $self; | ||
| 43 | } | ||
| 44 | |||
| 45 | sub get_value { | ||
| 46 | my $self = shift; | ||
| 47 | return $self->get_T(@_); | ||
| 48 | } | ||
| 49 | |||
| 50 | sub get_T { | ||
| 51 | my $self = shift; | ||
| 52 | my ($read_mode) = $self->_check_args( \@_, ['read_mode'] ); | ||
| 53 | my $temperature = "xxxxxxxx"; | ||
| 54 | |||
| 55 | if ( not defined $read_mode | ||
| 56 | or not $read_mode =~ /device|cache|request|fetch/ ) { | ||
| 57 | $read_mode = $self->device_settings()->{read_default}; | ||
| 58 | } | ||
| 59 | |||
| 60 | if ( $read_mode eq 'cache' and defined $self->{'device_cache'}->{'T'} ) { | ||
| 61 | return $self->{'device_cache'}->{'T'}; | ||
| 62 | } | ||
| 63 | elsif ( $read_mode eq 'request' ) { | ||
| 64 | if ( $self->{'request'} != 0 ) { | ||
| 65 | eval "$self->read();"; | ||
| 66 | } | ||
| 67 | while ( ( $self->write("getTemp\r\n") < 0 ) ) { | ||
| 68 | |||
| 69 | #print "repeat sending\n"; | ||
| 70 | } | ||
| 71 | $self->{'request'} = 1; | ||
| 72 | } | ||
| 73 | elsif ( $read_mode eq 'fetch' and $self->{'request'} == 1 ) { | ||
| 74 | $self->{'request'} = 0; | ||
| 75 | |||
| 76 | my $temperature; | ||
| 77 | while (1) { | ||
| 78 | eval '$temperature = $self->read()'; | ||
| 79 | if ($@) { | ||
| 80 | next; | ||
| 81 | } | ||
| 82 | elsif ( $temperature < 0 or $temperature > 1.5 ) { | ||
| 83 | |||
| 84 | #print "from cache"; | ||
| 85 | return $self->{'device_cache'}->{'T'}; | ||
| 86 | } | ||
| 87 | else { | ||
| 88 | chomp $temperature; | ||
| 89 | $self->{'device_cache'}->{'T'} = $temperature; | ||
| 90 | return $self->{'device_cache'}->{'T'}; | ||
| 91 | } | ||
| 92 | } | ||
| 93 | |||
| 94 | } | ||
| 95 | |||
| 96 | else { | ||
| 97 | for ( my $i = 0; $i < 3; $i++ ) { | ||
| 98 | $self->write("getTemp\r\n"); | ||
| 99 | |||
| 100 | for ( my $j = 0; $j < 3; $j++ ) { | ||
| 101 | eval '$temperature = $self->read()'; | ||
| 102 | if ($@) { | ||
| 103 | next; | ||
| 104 | } | ||
| 105 | elsif ( $temperature < 0 or $temperature > 1.5 ) { | ||
| 106 | |||
| 107 | #print "from cache"; | ||
| 108 | return $self->{'device_cache'}->{'T'}; | ||
| 109 | } | ||
| 110 | else { | ||
| 111 | chomp $temperature; | ||
| 112 | $self->{'device_cache'}->{'T'} = $temperature; | ||
| 113 | return $self->{'device_cache'}->{'T'}; | ||
| 114 | } | ||
| 115 | } | ||
| 116 | |||
| 117 | } | ||
| 118 | |||
| 119 | return $self->{'device_cache'}->{'T'}; | ||
| 120 | } | ||
| 121 | |||
| 122 | } | ||
| 123 | |||
| 124 | sub set_T { | ||
| 125 | my $self = shift; | ||
| 126 | my ($temperature) = $self->_check_args( \@_, ['temperaure'] ); | ||
| 127 | |||
| 128 | my $temp = $self->query("setTemp\r\n$temperature\r\n"); | ||
| 129 | |||
| 130 | chomp $temp; | ||
| 131 | |||
| 132 | #sleep(1); | ||
| 133 | |||
| 134 | return $temp; | ||
| 135 | } | ||
| 136 | |||
| 137 | sub set_heateroff { | ||
| 138 | my $self = shift; | ||
| 139 | |||
| 140 | $self->write("heaterOff\r\n"); | ||
| 141 | |||
| 142 | } | ||
| 143 | |||
| 144 | sub set_heatercontrol { | ||
| 145 | my $self = shift; | ||
| 146 | |||
| 147 | return; | ||
| 148 | } | ||
| 149 | |||
| 150 | # sub stabilize_T { | ||
| 151 | # my $self = shift; | ||
| 152 | # my $external_sensor = shift; | ||
| 153 | |||
| 154 | # my $T = $self->get_T(); | ||
| 155 | # my $Idc = $external_sensor->get_value(); | ||
| 156 | |||
| 157 | # push(@{$self->{data_buffer_T}}, $T); | ||
| 158 | # push(@{$self->{data_buffer_Idc}}, $Idc); | ||
| 159 | |||
| 160 | # @{$self->{data_buffer_T}}= sort(@{$self->{data_buffer_T}}); | ||
| 161 | # @{$self->{data_buffer_Idc}} = sort(@{$self->{data_buffer_Idc}}); | ||
| 162 | |||
| 163 | # my $length = @{$self->{data_buffer_T}}; | ||
| 164 | |||
| 165 | # my $median_T = @{$self->{data_buffer_T}}[$length/2]; | ||
| 166 | # my $median_Idc = @{$self->{data_buffer_Idc}}[$length/2]; | ||
| 167 | |||
| 168 | # my $range_T = abs(@{$self->{data_buffer_T}}[-1]-@{$self->{data_buffer_T}}[0]); | ||
| 169 | # my $range_Idc = abs(@{$self->{data_buffer_Idc}}[-1]-@{$self->{data_buffer_Idc}}[0]); | ||
| 170 | |||
| 171 | # print "Legth of buffer = $length\n"; | ||
| 172 | # print "Median T = $median_T\n"; | ||
| 173 | # print "Range T = $range_T\n"; | ||
| 174 | # print "Median Idc = $median_Idc\n"; | ||
| 175 | # print "Range Idc = $range_Idc\n"; | ||
| 176 | # print "\n"; | ||
| 177 | # print "T = $T\n"; | ||
| 178 | # print "Idc = $Idc\n"; | ||
| 179 | |||
| 180 | # if ( $length > 30 ) | ||
| 181 | # { | ||
| 182 | # #print abs($median_T - @{$self->{data_buffer_T}}[-1])." ?= ".(0.01*$range_T)."\n"; | ||
| 183 | # if ( $range_T <= abs(0.05*$T) ) | ||
| 184 | # { | ||
| 185 | # print "T stable\n"; | ||
| 186 | # if ( $range_Idc <= abs(0.01*$Idc) ) | ||
| 187 | # { | ||
| 188 | # print "Idc stable\n"; | ||
| 189 | # return 0; | ||
| 190 | # } | ||
| 191 | # return 1; | ||
| 192 | # } | ||
| 193 | |||
| 194 | # shift @{$self->{data_buffer_T}}; | ||
| 195 | # shift @{$self->{data_buffer_Idc}}; | ||
| 196 | # } | ||
| 197 | |||
| 198 | # print "===============================\n\n\n"; | ||
| 199 | |||
| 200 | # return 1; | ||
| 201 | |||
| 202 | # } | ||
| 203 | |||
| 204 | 1; | ||
| 205 | |||
| 206 | __END__ |