| 1 | package Lab::Instrument::Yokogawa7651; | ||
| 2 | #ABSTRACT: Yokogawa 7651 DC source | ||
| 3 | $Lab::Instrument::Yokogawa7651::VERSION = '3.899'; | ||
| 4 | use v5.20; | ||
| 5 | |||
| 6 | use warnings; | ||
| 7 | use strict; | ||
| 8 | use Time::HiRes qw/usleep/; | ||
| 9 | |||
| 10 | |||
| 11 | |||
| 12 | use Lab::Instrument; | ||
| 13 | use Lab::Instrument::Source; | ||
| 14 | use Data::Dumper; | ||
| 15 | |||
| 16 | our @ISA = ('Lab::Instrument::Source'); | ||
| 17 | |||
| 18 | our %fields = ( | ||
| 19 | supported_connections => [ 'GPIB', 'VISA' ], | ||
| 20 | |||
| 21 | # default settings for the supported connections | ||
| 22 | connection_settings => { | ||
| 23 | gpib_board => 0, | ||
| 24 | gpib_address => undef, | ||
| 25 | timeout => 1 | ||
| 26 | }, | ||
| 27 | |||
| 28 | device_settings => { | ||
| 29 | gate_protect => 1, | ||
| 30 | |||
| 31 | #gp_equal_level => 1e-5, | ||
| 32 | gp_max_units_per_second => 0.005, | ||
| 33 | gp_max_units_per_step => 0.001, | ||
| 34 | gp_max_step_per_second => 5, | ||
| 35 | |||
| 36 | max_sweep_time => 3600, | ||
| 37 | min_sweep_time => 0.1, | ||
| 38 | |||
| 39 | stepsize => 0.01, | ||
| 40 | |||
| 41 | }, | ||
| 42 | |||
| 43 | device_cache => { | ||
| 44 | function => undef, | ||
| 45 | range => undef, | ||
| 46 | level => undef, | ||
| 47 | output => undef, | ||
| 48 | }, | ||
| 49 | |||
| 50 | device_cache_order => [ 'function', 'range' ], | ||
| 51 | request => 0 | ||
| 52 | ); | ||
| 53 | |||
| 54 | sub new { | ||
| 55 | my $proto = shift; | ||
| 56 | my $class = ref($proto) || $proto; | ||
| 57 | my $self = $class->SUPER::new(@_); | ||
| 58 | $self->${ \( __PACKAGE__ . '::_construct' ) }(__PACKAGE__); | ||
| 59 | |||
| 60 | return $self; | ||
| 61 | } | ||
| 62 | |||
| 63 | sub _device_init { | ||
| 64 | my $self = shift; | ||
| 65 | if ( $self->get_status('setting') ) { | ||
| 66 | $self->end_program(); | ||
| 67 | } | ||
| 68 | } | ||
| 69 | |||
| 70 | sub set_voltage { | ||
| 71 | my $self = shift; | ||
| 72 | my ( $voltage, $tail ) = $self->_check_args( \@_, ['voltage'] ); | ||
| 73 | |||
| 74 | my $function = $self->get_function( { 'read_mode' => 'cache' }, $tail ); | ||
| 75 | |||
| 76 | if ( $function !~ /voltage/i ) { | ||
| 77 | Lab::Exception::CorruptParameter->throw( error => | ||
| 78 | "Source is in mode $function. Can't set voltage level." ); | ||
| 79 | } | ||
| 80 | |||
| 81 | return $self->set_level( $voltage, $tail ); | ||
| 82 | } | ||
| 83 | |||
| 84 | sub set_current { | ||
| 85 | my $self = shift; | ||
| 86 | my ( $current, $tail ) = $self->_check_args( \@_, ['current'] ); | ||
| 87 | |||
| 88 | my $function = $self->get_function( { 'read_mode' => 'cache' }, $tail ); | ||
| 89 | |||
| 90 | if ( $function !~ /current/i ) { | ||
| 91 | Lab::Exception::CorruptParameter->throw( error => | ||
| 92 | "Source is in mode $function. Can't set current level." ); | ||
| 93 | } | ||
| 94 | |||
| 95 | $self->set_level( $current, $tail ); | ||
| 96 | } | ||
| 97 | |||
| 98 | sub set_setpoint { | ||
| 99 | my $self = shift; | ||
| 100 | my ( $value, $tail ) = $self->_check_args( \@_, ['value'] ); | ||
| 101 | my $cmd = sprintf( "S%+.4e", $value ); | ||
| 102 | $self->write( $cmd, { error_check => 1 }, $tail ); | ||
| 103 | } | ||
| 104 | |||
| 105 | sub _set_level { | ||
| 106 | my $self = shift; | ||
| 107 | my ( $value, $tail ) = $self->_check_args( \@_, ['value'] ); | ||
| 108 | |||
| 109 | my $range = $self->get_range( { read_mode => 'cache' }, $tail ); | ||
| 110 | |||
| 111 | if ( $value > $range || $value < -$range ) { | ||
| 112 | Lab::Exception::CorruptParameter->throw( | ||
| 113 | "The desired source level $value is not within the source range $range \n" | ||
| 114 | ); | ||
| 115 | } | ||
| 116 | |||
| 117 | my $cmd = sprintf( "S%ge", $value ); | ||
| 118 | |||
| 119 | $self->write( $cmd, { error_check => 1 }, $tail ); | ||
| 120 | |||
| 121 | return $self->{'device_cache'}->{'level'} = $value; | ||
| 122 | |||
| 123 | } | ||
| 124 | |||
| 125 | sub start_program { | ||
| 126 | my $self = shift; | ||
| 127 | my ($tail) = $self->_check_args( \@_ ); | ||
| 128 | my $cmd = sprintf("PRS"); | ||
| 129 | $self->write( $cmd, $tail ); | ||
| 130 | } | ||
| 131 | |||
| 132 | sub end_program { | ||
| 133 | my $self = shift; | ||
| 134 | my ($tail) = $self->_check_args( \@_ ); | ||
| 135 | my $cmd = sprintf("PRE"); | ||
| 136 | $self->write( $cmd, $tail ); | ||
| 137 | } | ||
| 138 | |||
| 139 | sub execute_program { | ||
| 140 | |||
| 141 | # 0 HALT | ||
| 142 | # 1 STEP | ||
| 143 | # 2 RUN | ||
| 144 | #3 Continue | ||
| 145 | my $self = shift; | ||
| 146 | my ( $value, $tail ) = $self->_check_args( \@_, ['value'] ); | ||
| 147 | my $cmd = sprintf( "RU%d", $value ); | ||
| 148 | $self->write( $cmd, $tail ); | ||
| 149 | } | ||
| 150 | |||
| 151 | sub trg { | ||
| 152 | my $self = shift; | ||
| 153 | my ($tail) = $self->_check_args( \@_ ); | ||
| 154 | $self->execute_program( 2, $tail ); | ||
| 155 | } | ||
| 156 | |||
| 157 | sub config_sweep { | ||
| 158 | my $self = shift; | ||
| 159 | my ( $start, $target, $duration, $sections, $tail ) | ||
| 160 | = $self->check_sweep_config(@_); | ||
| 161 | |||
| 162 | $self->set_output( 1, $tail ); | ||
| 163 | $self->set_run_mode( 'single', $tail ); | ||
| 164 | |||
| 165 | $self->start_program($tail); | ||
| 166 | |||
| 167 | for ( my $i = 1; $i <= $sections; $i++ ) { | ||
| 168 | $self->set_setpoint( $start + ( $target - $start ) / $sections * $i ); | ||
| 169 | } | ||
| 170 | $self->end_program($tail); | ||
| 171 | |||
| 172 | # programming sweep duration: | ||
| 173 | |||
| 174 | $self->set_time( $duration, $duration, $tail ); | ||
| 175 | |||
| 176 | # calculate trace | ||
| 177 | |||
| 178 | } | ||
| 179 | |||
| 180 | sub configure_sweep { | ||
| 181 | my $self = shift; | ||
| 182 | my ( $target, $time, $rate, $tail ) | ||
| 183 | = $self->_check_args( \@_, [ 'points', 'time', 'rate' ] ); | ||
| 184 | |||
| 185 | $self->config_sweep( $target, $rate, $time, $tail ); | ||
| 186 | } | ||
| 187 | |||
| 188 | sub wait_done { | ||
| 189 | my $self = shift; | ||
| 190 | my ($tail) = $self->_check_args( \@_ ); | ||
| 191 | |||
| 192 | $self->wait($tail); | ||
| 193 | |||
| 194 | } | ||
| 195 | |||
| 196 | sub abort { | ||
| 197 | my $self = shift; | ||
| 198 | my ($tail) = $self->_check_args( \@_ ); | ||
| 199 | $self->execute_program( 0, $tail ); | ||
| 200 | } | ||
| 201 | |||
| 202 | sub active { | ||
| 203 | my $self = shift; | ||
| 204 | my ($tail) = $self->_check_args( \@_ ); | ||
| 205 | |||
| 206 | if ( $self->get_status( "execution", $tail ) == 0 ) { | ||
| 207 | return 0; | ||
| 208 | } | ||
| 209 | else { | ||
| 210 | return 1; | ||
| 211 | } | ||
| 212 | |||
| 213 | } | ||
| 214 | |||
| 215 | sub wait { | ||
| 216 | my $self = shift; | ||
| 217 | my ($tail) = $self->_check_args( \@_ ); | ||
| 218 | my $flag = 1; | ||
| 219 | local $| = 1; | ||
| 220 | |||
| 221 | while (1) { | ||
| 222 | |||
| 223 | #my $status = $self->get_status(); | ||
| 224 | my $execution = $self->get_status('execution'); | ||
| 225 | my $current_level = $self->get_level($tail); | ||
| 226 | if ( $flag <= 1.1 and $flag >= 0.9 ) { | ||
| 227 | print "\t\t\t\t\t\t\t\t\t\r"; | ||
| 228 | print $self->get_id() . " is sweeping ($current_level )\r"; | ||
| 229 | |||
| 230 | #usleep(5e5); | ||
| 231 | } | ||
| 232 | elsif ( $flag <= 0 ) { | ||
| 233 | print "\t\t\t\t\t\t\t\t\t\r"; | ||
| 234 | print $self->get_id() . " is ($current_level ) \r"; | ||
| 235 | $flag = 2; | ||
| 236 | } | ||
| 237 | $flag -= 0.5; | ||
| 238 | if ( $execution == 0 ) { | ||
| 239 | print "\t\t\t\t\t\t\t\t\t\r"; | ||
| 240 | $| = 0; | ||
| 241 | last; | ||
| 242 | } | ||
| 243 | } | ||
| 244 | } | ||
| 245 | |||
| 246 | sub _sweep_to_level { | ||
| 247 | my $self = shift; | ||
| 248 | my ( $target, $time, $tail ) | ||
| 249 | = $self->_check_args( \@_, [ 'points', 'time' ] ); | ||
| 250 | |||
| 251 | # print "Yokogawa7651.pm: configuring sweep $target $time\n"; | ||
| 252 | $self->config_sweep( | ||
| 253 | { | ||
| 254 | points => $target, | ||
| 255 | time => $time | ||
| 256 | }, | ||
| 257 | $tail | ||
| 258 | ); | ||
| 259 | |||
| 260 | # print "Yokogawa7651.pm: executing program\n"; | ||
| 261 | $self->execute_program( 2, $tail ); | ||
| 262 | |||
| 263 | # print "Yokogawa7651.pm: waiting until done\n"; | ||
| 264 | $self->wait($tail); | ||
| 265 | |||
| 266 | # print "Yokogawa7651.pm: reading out source level\n"; | ||
| 267 | my $current = $self->get_level($tail); | ||
| 268 | |||
| 269 | # print "Yokogawa7651.pm: source level is $current\n"; | ||
| 270 | |||
| 271 | my $eql = $self->get_gp_equal_level(); | ||
| 272 | |||
| 273 | # my $difference=$current-$target; | ||
| 274 | # print "Yokogawa7651.pm: c $current t $target d $difference e $eql\n"; | ||
| 275 | |||
| 276 | if ( abs( $current - $target ) > $eql ) { | ||
| 277 | print "Yokogawa7651.pm: error current neq target\n"; | ||
| 278 | Lab::Exception::CorruptParameter->throw( | ||
| 279 | "Sweep failed: $target not equal to $current. \n"); | ||
| 280 | } | ||
| 281 | |||
| 282 | # print "Yokogawa7651.pm: reaching return from _sweep_to_level\n"; | ||
| 283 | return $self->device_cache()->{'level'} = $target; | ||
| 284 | } | ||
| 285 | |||
| 286 | sub get_function { | ||
| 287 | my $self = shift; | ||
| 288 | |||
| 289 | my ($tail) = $self->_check_args( \@_ ); | ||
| 290 | |||
| 291 | my $cmd = "OD"; | ||
| 292 | my $result = $self->query( $cmd, $tail ); | ||
| 293 | if ( $result =~ /^...([VA])/ ) { | ||
| 294 | return ( $1 eq "V" ) ? "voltage" : "current"; | ||
| 295 | } | ||
| 296 | else { | ||
| 297 | Lab::Exception::CorruptParameter->throw( | ||
| 298 | "Output of command OD is not valid. \n"); | ||
| 299 | } | ||
| 300 | |||
| 301 | } | ||
| 302 | |||
| 303 | sub get_level { | ||
| 304 | my $self = shift; | ||
| 305 | my $cmd = "OD"; | ||
| 306 | my $result; | ||
| 307 | |||
| 308 | my ($read_mode) = $self->_check_args( \@_, ['read_mode'] ); | ||
| 309 | |||
| 310 | if ( not defined $read_mode | ||
| 311 | or not $read_mode =~ /device|cache|request|fetch/ ) { | ||
| 312 | $read_mode = $self->device_settings()->{read_default}; | ||
| 313 | } | ||
| 314 | |||
| 315 | if ( $read_mode eq 'cache' | ||
| 316 | and defined $self->{'device_cache'}->{'level'} ) { | ||
| 317 | return $self->{'device_cache'}->{'level'}; | ||
| 318 | } | ||
| 319 | elsif ( $read_mode eq 'request' and $self->{request} == 0 ) { | ||
| 320 | $self->{request} = 1; | ||
| 321 | $self->write($cmd); | ||
| 322 | return; | ||
| 323 | } | ||
| 324 | elsif ( $read_mode eq 'request' and $self->{request} == 1 ) { | ||
| 325 | $result = $self->read(); | ||
| 326 | $self->write($cmd); | ||
| 327 | return; | ||
| 328 | } | ||
| 329 | elsif ( $read_mode eq 'fetch' and $self->{request} == 1 ) { | ||
| 330 | $self->{request} = 0; | ||
| 331 | $result = $self->read(); | ||
| 332 | } | ||
| 333 | else { | ||
| 334 | if ( $self->{request} == 1 ) { | ||
| 335 | $result = $self->read(); | ||
| 336 | $self->{request} = 0; | ||
| 337 | $result = $self->query($cmd); | ||
| 338 | } | ||
| 339 | else { | ||
| 340 | $result = $self->query($cmd); | ||
| 341 | } | ||
| 342 | } | ||
| 343 | |||
| 344 | $result =~ /....([\+\-\d\.E]*)/; | ||
| 345 | return $self->{'device_cache'}->{'level'} = $1; | ||
| 346 | } | ||
| 347 | |||
| 348 | sub get_voltage { | ||
| 349 | my $self = shift; | ||
| 350 | |||
| 351 | my $function = $self->get_function(); | ||
| 352 | |||
| 353 | if ( $function !~ /voltage/i ) { | ||
| 354 | Lab::Exception::CorruptParameter->throw( error => | ||
| 355 | "Source is in mode $function. Can't get voltage level." ); | ||
| 356 | } | ||
| 357 | |||
| 358 | return $self->get_level(@_); | ||
| 359 | } | ||
| 360 | |||
| 361 | sub get_current { | ||
| 362 | my $self = shift; | ||
| 363 | |||
| 364 | my $function = $self->get_function(); | ||
| 365 | |||
| 366 | if ( $function !~ /current/i ) { | ||
| 367 | Lab::Exception::CorruptParameter->throw( error => | ||
| 368 | "Source is in mode $function. Can't get current level." ); | ||
| 369 | } | ||
| 370 | |||
| 371 | return $self->get_level(@_); | ||
| 372 | } | ||
| 373 | |||
| 374 | sub set_function { | ||
| 375 | my $self = shift; | ||
| 376 | my ($function) = $self->_check_args( \@_, ['function'] ); | ||
| 377 | |||
| 378 | if ( $function !~ /(current|voltage)/i ) { | ||
| 379 | Lab::Exception::CorruptParameter->throw( | ||
| 380 | "$function is not a valid source mode. Choose 1 or 5 for current and voltage mode respectively. \n" | ||
| 381 | ); | ||
| 382 | } | ||
| 383 | |||
| 384 | if ( $self->get_function() eq $function ) { | ||
| 385 | return $function; | ||
| 386 | } | ||
| 387 | |||
| 388 | if ( $self->get_output() and $self->device_settings()->{gate_protect} ) { | ||
| 389 | Lab::Exception::Warning->throw( | ||
| 390 | 'Cannot switch function in gate-protection mode while output is activated.' | ||
| 391 | ); | ||
| 392 | } | ||
| 393 | |||
| 394 | my $my_function = ( $function =~ /current/i ) ? 5 : 1; | ||
| 395 | |||
| 396 | my $cmd = sprintf( "F%de", $my_function ); | ||
| 397 | |||
| 398 | $self->write($cmd); | ||
| 399 | return $self->{'device_cache'}->{'function'} = $function; | ||
| 400 | |||
| 401 | } | ||
| 402 | |||
| 403 | sub set_range { | ||
| 404 | my $self = shift; | ||
| 405 | my ($range) = $self->_check_args( \@_, ['range'] ); | ||
| 406 | |||
| 407 | my $function = $self->get_function(); | ||
| 408 | |||
| 409 | if ( $function =~ /voltage/i ) { | ||
| 410 | if ( $range <= 10e-3 ) { $range = 2; } | ||
| 411 | elsif ( $range <= 100e-3 ) { $range = 3; } | ||
| 412 | elsif ( $range <= 1 ) { $range = 4; } | ||
| 413 | elsif ( $range <= 10 ) { $range = 5; } | ||
| 414 | elsif ( $range <= 30 ) { $range = 6; } | ||
| 415 | else { | ||
| 416 | Lab::Exception::CorruptParameter->throw( error => | ||
| 417 | "unexpected value for RANGE in sub set_range. Expected values are between 10mV ... 30V and 1mA ... 100mA for voltage and current mode." | ||
| 418 | ); | ||
| 419 | } | ||
| 420 | } | ||
| 421 | elsif ( $function =~ /current/i ) { | ||
| 422 | if ( $range <= 1e-3 ) { $range = 4; } | ||
| 423 | elsif ( $range <= 10e-3 ) { $range = 5; } | ||
| 424 | elsif ( $range <= 100e-3 ) { $range = 6; } | ||
| 425 | else { | ||
| 426 | Lab::Exception::CorruptParameter->throw( error => | ||
| 427 | "unexpected value for RANGE in sub set_range. Expected values are between 10mV ... 30V and 1mA ... 100mA for voltage and current mode." | ||
| 428 | ); | ||
| 429 | } | ||
| 430 | } | ||
| 431 | else { | ||
| 432 | Lab::Exception::CorruptParameter->throw( | ||
| 433 | "$range is not a valid source range. Read the documentation for a list of allowed ranges in mode $function.\n" | ||
| 434 | ); | ||
| 435 | } | ||
| 436 | |||
| 437 | #fixed voltage mode | ||
| 438 | # 2 10mV | ||
| 439 | # 3 100mV | ||
| 440 | # 4 1V | ||
| 441 | # 5 10V | ||
| 442 | # 6 30V | ||
| 443 | #fixed current mode | ||
| 444 | # 4 1mA | ||
| 445 | # 5 10mA | ||
| 446 | # 6 100mA | ||
| 447 | |||
| 448 | my $cmd = sprintf( "R%ue", $range ); | ||
| 449 | |||
| 450 | $self->write($cmd); | ||
| 451 | return $self->{'device_cache'}->{'range'} = $self->get_range(); | ||
| 452 | } | ||
| 453 | |||
| 454 | sub get_info { | ||
| 455 | my $self = shift; | ||
| 456 | |||
| 457 | my ($read_mode) = $self->_check_args( \@_, ['read_mode'] ); | ||
| 458 | |||
| 459 | if ( not defined $read_mode or not $read_mode =~ /device|cache/ ) { | ||
| 460 | $read_mode = $self->device_settings()->{read_default}; | ||
| 461 | } | ||
| 462 | |||
| 463 | if ( $read_mode eq 'cache' and defined $self->{'device_cache'}->{'info'} ) | ||
| 464 | { | ||
| 465 | return $self->{'device_cache'}->{'info'}; | ||
| 466 | } | ||
| 467 | |||
| 468 | $self->write("OS"); | ||
| 469 | my @info; | ||
| 470 | for ( my $i = 0; $i <= 10; $i++ ) { | ||
| 471 | my $line = $self->connection()->Read( read_length => 300 ); | ||
| 472 | if ( $line =~ /END/ ) {last} | ||
| 473 | chomp $line; | ||
| 474 | $line =~ s/\r//; | ||
| 475 | push( @info, sprintf($line) ); | ||
| 476 | } | ||
| 477 | |||
| 478 | return @{ $self->{'device_cache'}->{'info'} } = @info; | ||
| 479 | } | ||
| 480 | |||
| 481 | sub get_range { | ||
| 482 | my $self = shift; | ||
| 483 | |||
| 484 | my ($read_mode) = $self->_check_args( \@_, ['read_mode'] ); | ||
| 485 | |||
| 486 | if ( not defined $read_mode or not $read_mode =~ /device|cache/ ) { | ||
| 487 | $read_mode = $self->device_settings()->{read_default}; | ||
| 488 | } | ||
| 489 | |||
| 490 | if ( $read_mode eq 'cache' | ||
| 491 | and defined $self->{'device_cache'}->{'range'} ) { | ||
| 492 | return $self->{'device_cache'}->{'range'}; | ||
| 493 | } | ||
| 494 | |||
| 495 | my $range = ( $self->get_info() )[1]; | ||
| 496 | my $function = $self->get_function(); | ||
| 497 | |||
| 498 | if ( $range =~ /F(\d)R(\d)/ ) { | ||
| 499 | $range = $2; | ||
| 500 | |||
| 501 | # printf "rangenr=$range_nr\n"; | ||
| 502 | } | ||
| 503 | |||
| 504 | if ( $function =~ /voltage/i ) { | ||
| 505 | $range =~ /2/ ? $range | ||
| 506 | = 0.012 | ||
| 507 | : $range =~ /3/ ? $range | ||
| 508 | = 0.12 | ||
| 509 | : $range =~ /4/ ? $range | ||
| 510 | = 1.2 | ||
| 511 | : $range =~ /5/ ? $range | ||
| 512 | = 12 | ||
| 513 | : $range =~ /6/ ? $range | ||
| 514 | = 32 | ||
| 515 | : Lab::Exception::CorruptParameter->throw( | ||
| 516 | "$range is not a valid voltage range. Read the documentation for a list of allowed ranges in mode $function.\n" | ||
| 517 | ); | ||
| 518 | } | ||
| 519 | elsif ( $function =~ /current/i ) { | ||
| 520 | $range =~ /4/ ? $range | ||
| 521 | = 0.0012 | ||
| 522 | : $range =~ /5/ ? $range | ||
| 523 | = 0.012 | ||
| 524 | : $range =~ /6/ ? $range | ||
| 525 | = 0.12 | ||
| 526 | : Lab::Exception::CorruptParameter->throw( | ||
| 527 | "$range is not a valid current range. Read the documentation for a list of allowed ranges in mode $function.\n" | ||
| 528 | ); | ||
| 529 | } | ||
| 530 | else { | ||
| 531 | Lab::Exception::CorruptParameter->throw( | ||
| 532 | "$range is not a valid source range. Read the documentation for a list of allowed ranges in mode $function.\n" | ||
| 533 | ); | ||
| 534 | } | ||
| 535 | |||
| 536 | return $self->{'device_cache'}->{'range'} = $range; | ||
| 537 | } | ||
| 538 | |||
| 539 | sub set_run_mode { | ||
| 540 | my $self = shift; | ||
| 541 | my ($value) = $self->_check_args( \@_, ['value'] ); | ||
| 542 | |||
| 543 | # $value == 0 --> REPEAT-Mode | ||
| 544 | # $value == 1 --> SINGLE-Mode | ||
| 545 | |||
| 546 | if ( $value eq 'repeat' or $value eq 'REPEAT' ) { $value = 0; } | ||
| 547 | if ( $value eq 'single' or $value eq 'SINGLE' ) { $value = 1; } | ||
| 548 | |||
| 549 | if ( $value != 0 and $value != 1 ) { | ||
| 550 | Lab::Exception::CorruptParameter->throw( | ||
| 551 | error => "Run Mode $value not defined\n" ); | ||
| 552 | } | ||
| 553 | my $cmd = sprintf( "M%u", $value ); | ||
| 554 | $self->write($cmd); | ||
| 555 | } | ||
| 556 | |||
| 557 | sub set_time { # internal use only | ||
| 558 | my $self = shift; | ||
| 559 | my ( $sweep_time, $interval_time, $tail ) | ||
| 560 | = $self->_check_args( \@_, [ 'sweep_time', 'interval_time' ] ); | ||
| 561 | |||
| 562 | if ( $sweep_time < $self->device_settings()->{min_sweep_time} ) { | ||
| 563 | print Lab::Exception::CorruptParameter->new( error => | ||
| 564 | " Sweep Time: $sweep_time smaller than $self->device_settings()->{min_sweep_time} sec!\n Sweep time set to $self->device_settings()->{min_sweep_time} sec" | ||
| 565 | ); | ||
| 566 | $sweep_time = $self->device_settings()->{min_sweep_time}; | ||
| 567 | } | ||
| 568 | elsif ( $sweep_time > $self->device_settings()->{max_sweep_time} ) { | ||
| 569 | print Lab::Exception::CorruptParameter->new( error => | ||
| 570 | " Sweep Time: $sweep_time> $self->device_settings()->{max_sweep_time} sec!\n Sweep time set to $self->device_settings()->{max_sweep_time} sec" | ||
| 571 | ); | ||
| 572 | $sweep_time = $self->device_settings()->{max_sweep_time}; | ||
| 573 | } | ||
| 574 | if ( $interval_time < $self->device_settings()->{min_sweep_time} ) { | ||
| 575 | print Lab::Exception::CorruptParameter->new( error => | ||
| 576 | " Interval Time: $interval_time smaller than $self->device_settings()->{min_sweep_time} sec!\n Interval time set to $self->device_settings()->{min_sweep_time} sec" | ||
| 577 | ); | ||
| 578 | $interval_time = $self->device_settings()->{min_sweep_time}; | ||
| 579 | } | ||
| 580 | elsif ( $interval_time > $self->device_settings()->{max_sweep_time} ) { | ||
| 581 | print Lab::Exception::CorruptParameter->new( error => | ||
| 582 | " Interval Time: $interval_time> $self->device_settings()->{max_sweep_time} sec!\n Interval time set to $self->device_settings()->{max_sweep_time} sec" | ||
| 583 | ); | ||
| 584 | $interval_time = $self->device_settings()->{max_sweep_time}; | ||
| 585 | } | ||
| 586 | my $cmd = sprintf( "PI%.1f", $interval_time ); | ||
| 587 | $self->write( $cmd, $tail ); | ||
| 588 | $cmd = sprintf( "SW%.1f", $sweep_time ); | ||
| 589 | $self->write( $cmd, $tail ); | ||
| 590 | } | ||
| 591 | |||
| 592 | sub set_output { | ||
| 593 | my $self = shift; | ||
| 594 | my ($value) = $self->_check_args( \@_, ['value'] ); | ||
| 595 | |||
| 596 | my $current_level = undef; # for internal use only | ||
| 597 | |||
| 598 | if ( not defined $value ) { | ||
| 599 | return $self->get_output(); | ||
| 600 | } | ||
| 601 | |||
| 602 | if ( $self->device_settings()->{gate_protect} ) { | ||
| 603 | if ( $self->get_output() == 1 and $value == 0 ) { | ||
| 604 | $self->set_level(0); | ||
| 605 | } | ||
| 606 | elsif ( $self->get_output() == 0 and $value == 1 ) { | ||
| 607 | $current_level = $self->get_level(); | ||
| 608 | $self->set_level(0); | ||
| 609 | } | ||
| 610 | } | ||
| 611 | |||
| 612 | $self->wait(); | ||
| 613 | |||
| 614 | if ( $value == 1 ) { | ||
| 615 | $self->write('O1'); | ||
| 616 | $self->write('E'); | ||
| 617 | if ( defined $current_level ) { | ||
| 618 | $self->set_level($current_level); | ||
| 619 | } | ||
| 620 | |||
| 621 | } | ||
| 622 | elsif ( $value == 0 ) { | ||
| 623 | $self->write('O0'); | ||
| 624 | $self->write('E'); | ||
| 625 | |||
| 626 | } | ||
| 627 | else { | ||
| 628 | Lab::Exception::CorruptParameter->throw( | ||
| 629 | "$value is not a valid output status (on = 1 | off = 0)"); | ||
| 630 | } | ||
| 631 | |||
| 632 | return $self->{'device_cache'}->{'output'} = $self->get_output(); | ||
| 633 | |||
| 634 | } | ||
| 635 | |||
| 636 | sub get_output { | ||
| 637 | my $self = shift; | ||
| 638 | |||
| 639 | my ($read_mode) = $self->_check_args( \@_, ['read_mode'] ); | ||
| 640 | |||
| 641 | if ( not defined $read_mode or not $read_mode =~ /device|cache/ ) { | ||
| 642 | $read_mode = $self->device_settings()->{read_default}; | ||
| 643 | } | ||
| 644 | |||
| 645 | if ( $read_mode eq 'cache' | ||
| 646 | and defined $self->{'device_cache'}->{'output'} ) { | ||
| 647 | return $self->{'device_cache'}->{'output'}; | ||
| 648 | } | ||
| 649 | |||
| 650 | my $res = $self->get_status(); | ||
| 651 | return $self->{'device_cache'}->{'output'} = $res->{'output'} / 128; | ||
| 652 | } | ||
| 653 | |||
| 654 | sub initialize { | ||
| 655 | my $self = shift; | ||
| 656 | $self->reset(); | ||
| 657 | } | ||
| 658 | |||
| 659 | sub reset { | ||
| 660 | my $self = shift; | ||
| 661 | $self->write('RC'); | ||
| 662 | |||
| 663 | $self->_cache_init(); | ||
| 664 | } | ||
| 665 | |||
| 666 | sub set_voltage_limit { | ||
| 667 | my $self = shift; | ||
| 668 | my ($value) = $self->_check_args( \@_, ['value'] ); | ||
| 669 | |||
| 670 | my $cmd = sprintf( "LV%e", $value ); | ||
| 671 | $self->write($cmd); | ||
| 672 | |||
| 673 | $self->{'device_cache'}->{'voltage_limit'} = $value; | ||
| 674 | } | ||
| 675 | |||
| 676 | sub get_voltage_limit { | ||
| 677 | my $self = shift; | ||
| 678 | |||
| 679 | my ($read_mode) = $self->_check_args( \@_, ['read_mode'] ); | ||
| 680 | |||
| 681 | if ( not defined $read_mode or not $read_mode =~ /device|cache/ ) { | ||
| 682 | $read_mode = $self->device_settings()->{read_default}; | ||
| 683 | } | ||
| 684 | |||
| 685 | if ( $read_mode eq 'cache' | ||
| 686 | and defined $self->{'device_cache'}->{'voltage_limit'} ) { | ||
| 687 | return $self->{'device_cache'}->{'voltage_limit'}; | ||
| 688 | } | ||
| 689 | |||
| 690 | # read from device: | ||
| 691 | my $limit = @{ $self->get_info() }[3]; | ||
| 692 | |||
| 693 | my @limit = split( /LA/, $limit ); | ||
| 694 | @limit = split( /LV/, $limit[0] ); | ||
| 695 | return $self->{'device_cache'}->{'voltage_limit'} = $limit[1]; | ||
| 696 | |||
| 697 | } | ||
| 698 | |||
| 699 | sub set_current_limit { | ||
| 700 | my $self = shift; | ||
| 701 | my ($value) = $self->_check_args( \@_, ['value'] ); | ||
| 702 | |||
| 703 | my $cmd = sprintf( "LA%e", $value ); | ||
| 704 | $self->write($cmd); | ||
| 705 | |||
| 706 | $self->{'device_cache'}->{'current_limit'} = $value; | ||
| 707 | } | ||
| 708 | |||
| 709 | sub get_current_limit { | ||
| 710 | my $self = shift; | ||
| 711 | |||
| 712 | my ($read_mode) = $self->_check_args( \@_, ['read_mode'] ); | ||
| 713 | |||
| 714 | if ( not defined $read_mode or not $read_mode =~ /device|cache/ ) { | ||
| 715 | $read_mode = $self->device_settings()->{read_default}; | ||
| 716 | } | ||
| 717 | |||
| 718 | if ( $read_mode eq 'cache' | ||
| 719 | and defined $self->{'device_cache'}->{'current_limit'} ) { | ||
| 720 | return $self->{'device_cache'}->{'current_limit'}; | ||
| 721 | } | ||
| 722 | |||
| 723 | # read from device: | ||
| 724 | my $limit = @{ $self->get_info() }[3]; | ||
| 725 | |||
| 726 | my @limit = split( /LA/, $limit ); | ||
| 727 | return $self->{'device_cache'}->{'current_limit'} = $limit[1]; | ||
| 728 | |||
| 729 | } | ||
| 730 | |||
| 731 | sub get_status { | ||
| 732 | my $self = shift; | ||
| 733 | my $request = shift; | ||
| 734 | |||
| 735 | my $status = $self->query('OC'); | ||
| 736 | |||
| 737 | $status =~ /STS1=(\d*)/; | ||
| 738 | $status = $1; | ||
| 739 | my @flags = qw/ | ||
| 740 | CAL_switch memory_card calibration_mode output | ||
| 741 | unstable ERROR execution setting/; | ||
| 742 | my $result = {}; | ||
| 743 | for ( 0 .. 7 ) { | ||
| 744 | $result->{ $flags[$_] } = $status & 128; | ||
| 745 | $status <<= 1; | ||
| 746 | } | ||
| 747 | return $result->{$request} if defined $request; | ||
| 748 | return $result; | ||
| 749 | } | ||
| 750 | |||
| 751 | # | ||
| 752 | # Accessor implementations | ||
| 753 | # | ||
| 754 | |||
| 755 | sub autorange() { | ||
| 756 | my $self = shift; | ||
| 757 | |||
| 758 | return $self->{'autorange'} if scalar(@_) == 0; | ||
| 759 | my $value = shift; | ||
| 760 | |||
| 761 | if ( $value == 0 ) { | ||
| 762 | $self->{'autorange'} = 0; | ||
| 763 | } | ||
| 764 | elsif ( $value == 1 ) { | ||
| 765 | warn( | ||
| 766 | "Warning: Autoranging can give you some nice voltage spikes on the Yokogawa7651. You've been warned!\n" | ||
| 767 | ); | ||
| 768 | $self->{'autorange'} = 1; | ||
| 769 | } | ||
| 770 | else { | ||
| 771 | Lab::Exception::CorruptParameter->throw( error => | ||
| 772 | "Illegal value for autorange(), only 1 or 0 accepted.\n" ); | ||
| 773 | } | ||
| 774 | } | ||
| 775 | |||
| 776 | 1; | ||
| 777 | |||
| 778 | __END__ |