| 1 | package Lab::Instrument::YokogawaGS200; | ||
| 2 | #ABSTRACT: Yokogawa GS200 DC source | ||
| 3 | $Lab::Instrument::YokogawaGS200::VERSION = '3.899'; | ||
| 4 | use v5.20; | ||
| 5 | |||
| 6 | use strict; | ||
| 7 | use warnings; | ||
| 8 | |||
| 9 | use feature "switch"; | ||
| 10 | use Lab::Instrument; | ||
| 11 | use Lab::Instrument::Source; | ||
| 12 | use Data::Dumper; | ||
| 13 | use Lab::SCPI; | ||
| 14 | |||
| 15 | our @ISA = ('Lab::Instrument::Source'); | ||
| 16 | |||
| 17 | our %fields = ( | ||
| 18 | supported_connections => [ 'VISA_GPIB', 'GPIB', 'VISA' ], | ||
| 19 | |||
| 20 | # default settings for the supported connections | ||
| 21 | connection_settings => { | ||
| 22 | gpib_board => 0, | ||
| 23 | gpib_address => 22, | ||
| 24 | }, | ||
| 25 | |||
| 26 | device_settings => { | ||
| 27 | |||
| 28 | gate_protect => 1, | ||
| 29 | gp_equal_level => 1e-5, | ||
| 30 | gp_max_units_per_second => 0.05, | ||
| 31 | gp_max_units_per_step => 0.005, | ||
| 32 | gp_max_step_per_second => 10, | ||
| 33 | |||
| 34 | stepsize => 0.01, # default stepsize for sweep without gate protect | ||
| 35 | |||
| 36 | max_sweep_time => 3600, | ||
| 37 | min_sweep_time => 0.1, | ||
| 38 | }, | ||
| 39 | |||
| 40 | # If class does not provide set_$var for those, AUTOLOAD will take care. | ||
| 41 | device_cache => { | ||
| 42 | function => undef, | ||
| 43 | range => undef, | ||
| 44 | level => undef, | ||
| 45 | output => undef, | ||
| 46 | }, | ||
| 47 | |||
| 48 | device_cache_order => [ 'function', 'range' ], | ||
| 49 | request => 0 | ||
| 50 | ); | ||
| 51 | |||
| 52 | sub new { | ||
| 53 | my $proto = shift; | ||
| 54 | my $class = ref($proto) || $proto; | ||
| 55 | my $self = $class->SUPER::new(@_); | ||
| 56 | $self->${ \( __PACKAGE__ . '::_construct' ) }(__PACKAGE__); | ||
| 57 | |||
| 58 | $self->configure( $self->config() ); | ||
| 59 | |||
| 60 | return $self; | ||
| 61 | } | ||
| 62 | |||
| 63 | sub set_voltage { | ||
| 64 | my $self = shift; | ||
| 65 | my $voltage = shift; | ||
| 66 | |||
| 67 | my $function = $self->get_function( { read_mode => 'cache' } ); | ||
| 68 | |||
| 69 | if ( $function ne 'VOLT' ) { | ||
| 70 | Lab::Exception::CorruptParameter->throw( error => | ||
| 71 | "Source is in mode $function. Can't set voltage level." ); | ||
| 72 | } | ||
| 73 | |||
| 74 | return $self->set_level( $voltage, @_ ); | ||
| 75 | } | ||
| 76 | |||
| 77 | sub set_voltage_auto { | ||
| 78 | my $self = shift; | ||
| 79 | my $voltage = shift; | ||
| 80 | |||
| 81 | my $function = $self->get_function(); | ||
| 82 | |||
| 83 | if ( $function ne 'VOLT' ) { | ||
| 84 | Lab::Exception::CorruptParameter->throw( error => | ||
| 85 | "Source is in mode $function. Can't set voltage level." ); | ||
| 86 | } | ||
| 87 | |||
| 88 | if ( abs($voltage) > 32. ) { | ||
| 89 | Lab::Exception::CorruptParameter->throw( error => | ||
| 90 | "Source is not capable of voltage level > 32V. Can't set voltage level." | ||
| 91 | ); | ||
| 92 | } | ||
| 93 | |||
| 94 | $self->set_level_auto( $voltage, @_ ); | ||
| 95 | } | ||
| 96 | |||
| 97 | sub set_current_auto { | ||
| 98 | my $self = shift; | ||
| 99 | my $current = shift; | ||
| 100 | |||
| 101 | my $function = $self->get_function(); | ||
| 102 | |||
| 103 | if ( $function ne 'CURR' ) { | ||
| 104 | Lab::Exception::CorruptParameter->throw( error => | ||
| 105 | "Source is in mode $function. Can't set current level." ); | ||
| 106 | } | ||
| 107 | |||
| 108 | if ( abs($current) > 0.200 ) { | ||
| 109 | Lab::Exception::CorruptParameter->throw( error => | ||
| 110 | "Source is not capable of current level > 200mA. Can't set current level." | ||
| 111 | ); | ||
| 112 | } | ||
| 113 | |||
| 114 | $self->set_level_auto( $current, @_ ); | ||
| 115 | } | ||
| 116 | |||
| 117 | sub set_current { | ||
| 118 | my $self = shift; | ||
| 119 | my $current = shift; | ||
| 120 | |||
| 121 | my $function = $self->get_function(); | ||
| 122 | |||
| 123 | if ( $self->get_function() ne 'CURR' ) { | ||
| 124 | Lab::Exception::CorruptParameter->throw( error => | ||
| 125 | "Source is in mode $function. Can't set current level." ); | ||
| 126 | } | ||
| 127 | |||
| 128 | $self->set_level( $current, @_ ); | ||
| 129 | } | ||
| 130 | |||
| 131 | sub _set_level { | ||
| 132 | my $self = shift; | ||
| 133 | my $value = shift; | ||
| 134 | my $srcrange = $self->get_range(); | ||
| 135 | |||
| 136 | ( my $dec, my $exp ) = ( $srcrange =~ m/(^\d+)E([-\+]\d+)$/ ); | ||
| 137 | |||
| 138 | $srcrange = eval("$dec*10**$exp+2*$dec*10**($exp-1)"); | ||
| 139 | |||
| 140 | if ( abs($value) <= $srcrange ) { | ||
| 141 | my $cmd = sprintf( ":SOURce:LEVel %f", $value ); | ||
| 142 | |||
| 143 | #print $cmd; | ||
| 144 | $self->write($cmd); | ||
| 145 | return $self->{'device_cache'}->{'level'} = $value; | ||
| 146 | } | ||
| 147 | else { | ||
| 148 | Lab::Exception::CorruptParameter->throw( | ||
| 149 | error => "Level $value is out of curren range $srcrange." ); | ||
| 150 | } | ||
| 151 | |||
| 152 | } | ||
| 153 | |||
| 154 | sub set_level_auto { | ||
| 155 | my $self = shift; | ||
| 156 | my $value = shift; | ||
| 157 | |||
| 158 | my $cmd = sprintf( ":SOURce:LEVel:AUTO %e", $value ); | ||
| 159 | |||
| 160 | $self->write($cmd); | ||
| 161 | |||
| 162 | $self->{'device_cache'}->{'range'} = $self->get_range( from_device => 1 ); | ||
| 163 | |||
| 164 | return $self->{'device_cache'}->{'level'} = $value; | ||
| 165 | |||
| 166 | } | ||
| 167 | |||
| 168 | sub program_run { | ||
| 169 | my $self = shift; | ||
| 170 | my ($tail) = $self->_check_args( \@_ ); | ||
| 171 | |||
| 172 | # print "Running program\n"; | ||
| 173 | $self->write( ":PROG:RUN", $tail ); | ||
| 174 | |||
| 175 | } | ||
| 176 | |||
| 177 | sub program_pause { | ||
| 178 | my $self = shift; | ||
| 179 | |||
| 180 | my $cmd = sprintf(":PROGram:PAUSe"); | ||
| 181 | $self->write("$cmd"); | ||
| 182 | } | ||
| 183 | |||
| 184 | sub program_continue { | ||
| 185 | my $self = shift; | ||
| 186 | my $value = shift; | ||
| 187 | my $cmd = sprintf(":PROGram:CONTinue"); | ||
| 188 | $self->write("$cmd"); | ||
| 189 | |||
| 190 | } | ||
| 191 | |||
| 192 | sub program_halt { | ||
| 193 | my $self = shift; | ||
| 194 | my ($tail) = $self->_check_args( \@_ ); | ||
| 195 | |||
| 196 | $self->write( ":PROG:HALT", $tail ); | ||
| 197 | } | ||
| 198 | |||
| 199 | sub start_program { | ||
| 200 | my $self = shift; | ||
| 201 | my ($tail) = $self->_check_args( \@_ ); | ||
| 202 | |||
| 203 | #print "Start program\n"; | ||
| 204 | $self->write( ":PROG:EDIT:START", $tail ); | ||
| 205 | } | ||
| 206 | |||
| 207 | sub end_program { | ||
| 208 | my $self = shift; | ||
| 209 | my ($tail) = $self->_check_args( \@_ ); | ||
| 210 | |||
| 211 | #print "End program\n"; | ||
| 212 | $self->write( ":PROG:EDIT:END", $tail ); | ||
| 213 | } | ||
| 214 | |||
| 215 | sub set_setpoint { | ||
| 216 | my $self = shift; | ||
| 217 | my ( $value, $tail ) = $self->_check_args( \@_, ['value'] ); | ||
| 218 | my $cmd = sprintf(":SOUR:LEV $value"); | ||
| 219 | |||
| 220 | #print "Do $cmd"; | ||
| 221 | $self->write( $cmd, { error_check => 1 }, $tail ); | ||
| 222 | } | ||
| 223 | |||
| 224 | sub config_sweep { | ||
| 225 | my $self = shift; | ||
| 226 | my ( $start, $target, $duration, $sections, $tail ) | ||
| 227 | = $self->check_sweep_config(@_); | ||
| 228 | |||
| 229 | $self->write( ":PROG:REP 0", $tail ); | ||
| 230 | $self->write( "*CLS", $tail ); | ||
| 231 | $self->set_output( 1, $tail ); | ||
| 232 | |||
| 233 | $self->start_program($tail); | ||
| 234 | |||
| 235 | #print "Program:\n"; | ||
| 236 | for ( my $i = 1; $i <= $sections; $i++ ) { | ||
| 237 | $self->set_setpoint( $start + ( $target - $start ) / $sections * $i ); | ||
| 238 | printf "sweep to setpoint: %+.4e\n", | ||
| 239 | $start + ( $target - $start ) / $sections * $i; | ||
| 240 | } | ||
| 241 | $self->end_program($tail); | ||
| 242 | |||
| 243 | $self->set_time( $duration, $duration, $tail ); | ||
| 244 | |||
| 245 | } | ||
| 246 | |||
| 247 | sub set_time { # internal use only | ||
| 248 | my $self = shift; | ||
| 249 | my ( $sweep_time, $interval_time, $tail ) | ||
| 250 | = $self->_check_args( \@_, [ 'sweep_time', 'interval_time' ] ); | ||
| 251 | if ( $sweep_time < $self->device_settings()->{min_sweep_time} ) { | ||
| 252 | print Lab::Exception::CorruptParameter->new( error => | ||
| 253 | " 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" | ||
| 254 | ); | ||
| 255 | $sweep_time = $self->device_settings()->{min_sweep_time}; | ||
| 256 | } | ||
| 257 | elsif ( $sweep_time > $self->device_settings()->{max_sweep_time} ) { | ||
| 258 | print Lab::Exception::CorruptParameter->new( error => | ||
| 259 | " Sweep Time: $sweep_time> $self->device_settings()->{max_sweep_time} sec!\n Sweep time set to $self->device_settings()->{max_sweep_time} sec" | ||
| 260 | ); | ||
| 261 | $sweep_time = $self->device_settings()->{max_sweep_time}; | ||
| 262 | } | ||
| 263 | if ( $interval_time < $self->device_settings()->{min_sweep_time} ) { | ||
| 264 | print Lab::Exception::CorruptParameter->new( error => | ||
| 265 | " 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" | ||
| 266 | ); | ||
| 267 | $interval_time = $self->device_settings()->{min_sweep_time}; | ||
| 268 | } | ||
| 269 | elsif ( $interval_time > $self->device_settings()->{max_sweep_time} ) { | ||
| 270 | print Lab::Exception::CorruptParameter->new( error => | ||
| 271 | " Interval Time: $interval_time> $self->device_settings()->{max_sweep_time} sec!\n Interval time set to $self->device_settings()->{max_sweep_time} sec" | ||
| 272 | ); | ||
| 273 | $interval_time = $self->device_settings()->{max_sweep_time}; | ||
| 274 | } | ||
| 275 | $self->write( ":PROG:SLOP $sweep_time", $tail ); | ||
| 276 | $self->write( ":PROG:INT $interval_time", $tail ); | ||
| 277 | |||
| 278 | } | ||
| 279 | |||
| 280 | sub wait { | ||
| 281 | my $self = shift; | ||
| 282 | my ($tail) = $self->_check_args( \@_ ); | ||
| 283 | my $flag = 1; | ||
| 284 | local $| = 1; | ||
| 285 | |||
| 286 | while (1) { | ||
| 287 | |||
| 288 | #my $status = $self->get_status(); | ||
| 289 | |||
| 290 | my $current_level | ||
| 291 | = $self->get_level( { read_mode => 'device' }, $tail ); | ||
| 292 | if ( $flag <= 1.1 and $flag >= 0.9 ) { | ||
| 293 | print "\t\t\t\t\t\t\t\t\t\r"; | ||
| 294 | print $self->get_id() . " is sweeping ($current_level )\r"; | ||
| 295 | |||
| 296 | } | ||
| 297 | elsif ( $flag <= 0 ) { | ||
| 298 | print "\t\t\t\t\t\t\t\t\t\r"; | ||
| 299 | print $self->get_id() . " is ($current_level ) \r"; | ||
| 300 | $flag = 2; | ||
| 301 | } | ||
| 302 | $flag -= 0.5; | ||
| 303 | |||
| 304 | if ( $self->active($tail) == 0 ) { | ||
| 305 | print "\t\t\t\t\t\t\t\t\t\r"; | ||
| 306 | $| = 0; | ||
| 307 | last; | ||
| 308 | } | ||
| 309 | } | ||
| 310 | |||
| 311 | } | ||
| 312 | |||
| 313 | sub _sweep_to_level { | ||
| 314 | my $self = shift; | ||
| 315 | my ( $target, $time, $tail ) | ||
| 316 | = $self->_check_args( \@_, [ 'points', 'time' ] ); | ||
| 317 | |||
| 318 | $self->config_sweep( { points => $target, time => $time }, $tail ); | ||
| 319 | |||
| 320 | $self->program_run($tail); | ||
| 321 | |||
| 322 | $self->wait($tail); | ||
| 323 | my $current = $self->get_level($tail); | ||
| 324 | |||
| 325 | my $eql = $self->get_gp_equal_level(); | ||
| 326 | |||
| 327 | if ( abs( $current - $target ) > $eql ) { | ||
| 328 | print "YokogawaGS200.pm: error current neq target\n"; | ||
| 329 | Lab::Exception::CorruptParameter->throw( | ||
| 330 | "Sweep failed: $target not equal to $current. \n"); | ||
| 331 | } | ||
| 332 | |||
| 333 | $self->{'device_cache'}->{'level'} = $target; | ||
| 334 | |||
| 335 | return $target; | ||
| 336 | } | ||
| 337 | |||
| 338 | sub trg { | ||
| 339 | my $self = shift; | ||
| 340 | my ($tail) = $self->_check_args( \@_ ); | ||
| 341 | $self->write( "*CLS", $tail ); | ||
| 342 | $self->program_run($tail); | ||
| 343 | } | ||
| 344 | |||
| 345 | sub abort { | ||
| 346 | my $self = shift; | ||
| 347 | my ($tail) = $self->_check_args( \@_ ); | ||
| 348 | $self->program_halt($tail); | ||
| 349 | } | ||
| 350 | |||
| 351 | sub get_voltage { | ||
| 352 | my $self = shift; | ||
| 353 | |||
| 354 | my $function = $self->get_function(); | ||
| 355 | |||
| 356 | if ( !$function eq 'VOLT' ) { | ||
| 357 | Lab::Exception::CorruptParameter->throw( error => | ||
| 358 | "Source is in mode $function. Can't get voltage level." ); | ||
| 359 | } | ||
| 360 | |||
| 361 | return $self->get_level(@_); | ||
| 362 | } | ||
| 363 | |||
| 364 | sub active { | ||
| 365 | my $self = shift; | ||
| 366 | my ($tail) = $self->_check_args( \@_ ); | ||
| 367 | |||
| 368 | $self->write( "STAT:ENAB 128", $tail ); | ||
| 369 | if ( $self->get_status( "EES", $tail ) == 1 ) { | ||
| 370 | return 0; | ||
| 371 | } | ||
| 372 | else { | ||
| 373 | return 1; | ||
| 374 | } | ||
| 375 | |||
| 376 | } | ||
| 377 | |||
| 378 | sub get_status { | ||
| 379 | my $self = shift; | ||
| 380 | my ( $request, $tail ) = $self->_check_args( \@_, ['request'] ); | ||
| 381 | |||
| 382 | # The status byte is read | ||
| 383 | |||
| 384 | my $status = int( $self->query( '*STB?', $tail ) ); | ||
| 385 | |||
| 386 | #printf "Status: %i",$status; | ||
| 387 | |||
| 388 | my @flags = qw/NONE EES ESB MAV NONE EAV MSS NONE/; | ||
| 389 | my $result = {}; | ||
| 390 | for ( 0 .. 7 ) { | ||
| 391 | $result->{ $flags[$_] } = $status & 1; | ||
| 392 | $status >>= 1; | ||
| 393 | } | ||
| 394 | |||
| 395 | #print "EOP: $result->{'EOP'}\n"; | ||
| 396 | return $result->{$request} if defined $request; | ||
| 397 | return $result; | ||
| 398 | |||
| 399 | } | ||
| 400 | |||
| 401 | sub get_current { | ||
| 402 | my $self = shift; | ||
| 403 | |||
| 404 | my $function = $self->get_function(); | ||
| 405 | |||
| 406 | if ( !$self->get_function() eq 'CURR' ) { | ||
| 407 | Lab::Exception::CorruptParameter->throw( error => | ||
| 408 | "Source is in mode $function. Can't get current level." ); | ||
| 409 | } | ||
| 410 | |||
| 411 | return $self->get_level(@_); | ||
| 412 | } | ||
| 413 | |||
| 414 | sub get_level { | ||
| 415 | my $self = shift; | ||
| 416 | my ($tail) = $self->_check_args( \@_ ); | ||
| 417 | my $cmd = ":SOUR:LEV?"; | ||
| 418 | |||
| 419 | return $self->request( $cmd, $tail ); | ||
| 420 | } | ||
| 421 | |||
| 422 | sub get_function { | ||
| 423 | my $self = shift; | ||
| 424 | my ($tail) = $self->_check_args( \@_ ); | ||
| 425 | |||
| 426 | my $cmd = ":SOURce:FUNCtion?"; | ||
| 427 | return $self->query( $cmd, $tail ); | ||
| 428 | } | ||
| 429 | |||
| 430 | sub get_range { | ||
| 431 | my $self = shift; | ||
| 432 | my ($tail) = $self->_check_args( \@_ ); | ||
| 433 | |||
| 434 | my $cmd = ":SOUR:RANG?"; | ||
| 435 | return $self->query( $cmd, $tail ); | ||
| 436 | |||
| 437 | } | ||
| 438 | |||
| 439 | sub set_function { | ||
| 440 | my $self = shift; | ||
| 441 | my $func = shift; | ||
| 442 | |||
| 443 | if ( scpi_match( $func, 'current|voltage' ) ) { | ||
| 444 | my $cmd = ":SOURce:FUNCtion " . $func; | ||
| 445 | |||
| 446 | #print "$cmd\n"; | ||
| 447 | $self->write($cmd); | ||
| 448 | return $self->{'device_cache'}->{'function'} = $func; | ||
| 449 | } | ||
| 450 | else { | ||
| 451 | Lab::Exception::CorruptParameter->throw( | ||
| 452 | error => "source function $func not defined for this device.\n" ); | ||
| 453 | } | ||
| 454 | |||
| 455 | } | ||
| 456 | |||
| 457 | sub set_range { | ||
| 458 | my $self = shift; | ||
| 459 | my $range = shift; | ||
| 460 | |||
| 461 | my $srcf = $self->get_function(); | ||
| 462 | |||
| 463 | $self->write( "SOURce:RANGe $range", error_check => 1 ); | ||
| 464 | return $self->{'device_cache'}->{'range'} | ||
| 465 | = $self->get_range( from_device => 1 ); | ||
| 466 | |||
| 467 | } | ||
| 468 | |||
| 469 | sub set_output { | ||
| 470 | my $self = shift; | ||
| 471 | my $value = shift; | ||
| 472 | |||
| 473 | if ( $value =~ /(on|1)/i ) { | ||
| 474 | $value = 1; | ||
| 475 | } | ||
| 476 | elsif ( $value =~ /(off|0)/i ) { | ||
| 477 | $value = 0; | ||
| 478 | } | ||
| 479 | else { | ||
| 480 | Lab::Exception::CorruptParameter->throw( error => | ||
| 481 | "set_output accepts only on or off (case non-sensitive) and 1 or 0. $value is not valid." | ||
| 482 | ); | ||
| 483 | } | ||
| 484 | |||
| 485 | return $self->{"device_cache"}->{"output"} = $self->write(":OUTP $value"); | ||
| 486 | } | ||
| 487 | |||
| 488 | sub get_output { | ||
| 489 | my $self = shift; | ||
| 490 | my ($tail) = $self->_check_args( \@_ ); | ||
| 491 | |||
| 492 | return $self->query( ":OUTP?", $tail ); | ||
| 493 | } | ||
| 494 | |||
| 495 | sub set_voltage_limit { | ||
| 496 | my $self = shift; | ||
| 497 | my $value = shift; | ||
| 498 | my $cmd = ":SOURce:PROTection:VOLTage $value"; | ||
| 499 | |||
| 500 | if ( $value > 30. || $value < 1. ) { | ||
| 501 | Lab::Exception::CorruptParameter->throw( error => | ||
| 502 | "The voltage limit $value is not within the allowed range.\n" | ||
| 503 | ); | ||
| 504 | } | ||
| 505 | |||
| 506 | $self->connection()->write($cmd); | ||
| 507 | |||
| 508 | return $self->device_cache()->{'voltage_limit'} = $value; | ||
| 509 | |||
| 510 | } | ||
| 511 | |||
| 512 | sub set_current_limit { | ||
| 513 | my $self = shift; | ||
| 514 | my $value = shift; | ||
| 515 | my $cmd = ":SOURce:PROTection:CURRent $value"; | ||
| 516 | |||
| 517 | if ( $value > 0.2 || $value < 0.001 ) { | ||
| 518 | Lab::Exception::CorruptParameter->throw( error => | ||
| 519 | "The current limit $value is not within the allowed range.\n" | ||
| 520 | ); | ||
| 521 | } | ||
| 522 | |||
| 523 | $self->connection()->write($cmd); | ||
| 524 | |||
| 525 | return $self->device_cache()->{'current_limit'} = $value; | ||
| 526 | |||
| 527 | } | ||
| 528 | |||
| 529 | sub get_error { | ||
| 530 | my $self = shift; | ||
| 531 | |||
| 532 | my $cmd = ":SYSTem:ERRor?"; | ||
| 533 | |||
| 534 | return $self->query($cmd); | ||
| 535 | } | ||
| 536 | |||
| 537 | 1; | ||
| 538 | |||
| 539 | __END__ |