Files
imdd_silas/Classes/05_Lab/Exfo_laser.m
2025-12-18 09:56:52 +01:00

391 lines
13 KiB
Matlab

classdef Exfo_laser < handle
properties(Access=private)
connection_id
lab_interface
mainframe_channel
end
properties(Access=public)
cur_state
cur_wavelength
cur_power
safety_mode
end
%% Any user can call these methods to alter the laser state
methods (Access=public)
function obj = Exfo_laser(options)
arguments
options.safety_mode = 1;
options.lab_interface lab_interface = lab_interface.gbib;
options.connection_id = '8';
options.mainframe_channel = 1;
end
%
fn = fieldnames(options);
for n = 1:numel(fn)
try
obj.(fn{n}) = options.(fn{n});
end
end
v = obj.connectLaser_();
% Read states the first time
obj.cur_state = obj.getLaserStatus_(v,obj.mainframe_channel);
obj.cur_wavelength = obj.getLaserWavelength_(v,obj.mainframe_channel);
obj.cur_power = obj.getLaserPower_(v,obj.mainframe_channel);
clear v;
if obj.safety_mode
warning("Safety_mode ON: Display all information. Ask when switching laser. Turn safety_mode off in class instance or during initialization Exfo_laser(...,'safety_mode',0)");
else
disp("safety_mode OFF: EVERYTHING IS EXECUTED WITHOUT ASKING :-) BE SURE WHAT YOU DO");
end
end
function [isEnabled,power,lambda] = getLaserInfo(obj)
o = obj.connectLaser_();
isEnabled = obj.getLaserStatus_(o,obj.mainframe_channel);
power = obj.getLaserPower_(o,obj.mainframe_channel);
lambda = obj.getLaserWavelength_(o,obj.mainframe_channel);
end
function success = enableLaser(obj)
success = 0;
o = obj.connectLaser_();
if obj.safety_mode
% Prompt the user to enable the laser
choice = questdlg('Do you want to enable the laser?', ...
'Enable Laser', ...
'Yes', 'No', 'No');
else
choice = 'Yes';
end
% Handle the response
switch choice
case 'Yes'
% Enable the laser on channel 1
success = obj.enableLaser_(o,obj.mainframe_channel);
disp('Laser enabled.');
case 'No'
% Abort the operation
disp('Operation aborted. Laser remains disabled.');
end
end
function success = disableLaser(obj)
success = 0;
o = obj.connectLaser_();
if obj.safety_mode
% Prompt the user to enable the laser
choice = questdlg('Do you want to disable the laser?', ...
'Enable Laser', ...
'Yes', 'No', 'No');
else
choice = 'Yes';
end
% Handle the response
switch choice
case 'Yes'
% Enable the laser on channel 1
success = obj.disableLaser_(o,obj.mainframe_channel);
case 'No'
% Abort the operation
disp('Operation aborted. Laser remains enabled.');
end
end
function success = setPower(obj,desiredPower)
success = 0;
o = obj.connectLaser_();
if obj.safety_mode
% Prompt the user to enable the laser
choice = questdlg(['Do you want to set the laser to ', num2str(desiredPower) ,' dBm?'], ...
'SET POWER', ...
'Yes', 'No', 'No');
else
choice = 'Yes';
end
% Handle the response
switch choice
case 'Yes'
% Enable the laser on channel 1
success = obj.setLaserPower_(o,obj.mainframe_channel,desiredPower);
case 'No'
% Abort the operation
disp('Operation aborted. Laser remains at power.');
end
end
function success = setWavelength(obj,desriedLambda)
success = 0;
o = obj.connectLaser_();
if obj.safety_mode
% Prompt the user to enable the laser
choice = questdlg(['Do you want to set the laser wavelength to ', num2str(desriedLambda) ,' nm?'], ...
'SET WAVELENGTH', ...
'Yes', 'No', 'No');
else
choice = 'Yes';
end
% Handle the response
switch choice
case 'Yes'
% Enable the laser on channel 1
success = obj.setLaserWavelength_(o,obj.mainframe_channel,desriedLambda);
case 'No'
% Abort the operation
disp('Operation aborted. Laser remains at wavelength.');
end
end
end %public mehtods
% DAU must not be able to call these funcitons outside of the class!
% Hence they are private! :-)
methods (Access=private)
function v = connectLaser_(obj)
% Connect to the laser
if obj.lab_interface == lab_interface.serial
% Connect to the laser via SERIAL (USB)
v = serialport(obj.connection_id, 9600);
configureTerminator(v, "CR", "CR"); % Set the terminator to carriage return (CR)
elseif obj.lab_interface == lab_interface.gpib
% Connect via GPIB adress
visastring = ['GPIB1::',obj.connection_id,'::INSTR'];
v = visadev(visastring);
else
error('Interface not supported');
end
if obj.safety_mode
flush(v);
writeline(v, "*IDN?");
answ = readline(v);
disp(['Yay! We can talk to Instrument: ',char(answ)]);
end
end
% Function to GET the STATE of the laser
function isEnabled = getLaserStatus_(obj,serialObj, channel)
writeline(serialObj, ['CH', num2str(channel), ':ENABLE?']); % Query current wavelength
if serialObj.Type == visalib.InterfaceType.serial
manual_flush = 1;
while manual_flush
response = readline(serialObj);
manual_flush = contains(response, {'OK'});
end
else
response = readline(serialObj);
end
isEnabled = 0;
isEnabled = contains(response, {'ENABLED'});
isDisabled = 0;
isDisabled = contains(response, {'DISABLED'});
if isEnabled && ~isDisabled
obj.cur_state = isEnabled;
elseif ~isEnabled && isDisabled
obj.cur_state = isEnabled;
else
error(['Quantum Laser State; your laser is ON and OFF ->', char(response)]);
end
if obj.safety_mode
if isEnabled
disp(['Laser ', num2str(channel) ,' is currently ON -> ', char(response)]);
elseif isDisabled
disp(['Laser ', num2str(channel) ,' is currently OFF -> ', char(response)]);
else
error(['Unknown Laser State ->', char(response)]);
end
end
end
% Function to GET the wavelength of the laser
function current_wavelen = getLaserWavelength_(obj,serialObj, channel)
writeline(serialObj, ['CH', num2str(channel), ':L?']); % Query current wavelength
response = readline(serialObj);
current_wavelen = str2double(regexp(response, '\d+\.\d+', 'match', 'once'));
if obj.safety_mode
disp(['Current Laser ', num2str(channel) ,' Wavelength: ', num2str(current_wavelen),' --> ',char(response)]);
end
obj.cur_wavelength = current_wavelen;
end
% Function to GET the laser power
function powerValue = getLaserPower_(obj,serialObj, channel)
writeline(serialObj, ['CH', num2str(channel), ':P?']); % Query current power
response = readline(serialObj);
isDisabled = contains(response, 'Disabled');
if ~isDisabled
powerValue = str2double(regexp(response, '\d+\.\d+', 'match', 'once'));
else
powerValue = -200;
end
if obj.safety_mode
disp(['Current Laser ', num2str(channel) ,' Power: ', num2str(powerValue), ' dBm --> ',char(response)]);
end
obj.cur_power = powerValue;
end
% Function to enable the laser
function success = enableLaser_(obj,serialObj,channel)
success = 0;
isEnabled = obj.getLaserStatus_(serialObj,obj.mainframe_channel);
writeline(serialObj, ['CH',num2str(channel),':ENABLE']);
if serialObj.Type == visalib.InterfaceType.serial
response = readline(serialObj);
isok = contains(response, {'OK'});
end
cnt=0;
while ~isEnabled || cnt>10
isEnabled = obj.getLaserStatus_(serialObj,obj.mainframe_channel);
pause(0.2);
cnt = cnt+1;
end
if ~isEnabled
error('Laser not enabled but command was send')
else
success = 1;
end
end
% Function to enable the laser
function success = disableLaser_(obj,serialObj,channel)
success = 0;
isEnabled = obj.getLaserStatus_(serialObj,obj.mainframe_channel);
writeline(serialObj, ['CH',num2str(channel),':DISABLE']);
if serialObj.Type == visalib.InterfaceType.serial
response = readline(serialObj);
isok = contains(response, {'OK'});
end
cnt=0;
while isEnabled || cnt>10
isEnabled = obj.getLaserStatus_(serialObj,obj.mainframe_channel);
pause(0.5);
cnt = cnt+1;
end
if serialObj.Type == visalib.InterfaceType.serial
if isEnabled
error('Laser not enabled but command was send')
else
success = 1;
end
end
end
% Function to SET the wavelength of the laser
function success = setLaserWavelength_(obj,serialObj, channel, desiredWavelength_nm)
success = 0;
writeline(serialObj, ['CH',num2str(channel),':NM?']);
modeResponse = readline(serialObj);
isNMMode = contains(modeResponse, ['CH',num2str(channel),':1']);
if ~isNMMode
warning(['Laser is in GHz Mode, you requested to set Wavelength (nm) -> ',modeResponse])
return
end
command = ['CH', num2str(channel), ':L=', num2str(desiredWavelength_nm)];
writeline(serialObj, command);
if abs(obj.cur_wavelength - desiredWavelength_nm)>30
pause(5); %it takes time to tune the laser! If this is not here it WILL break!
else
pause(1.5);
end
if serialObj.Type == visalib.InterfaceType.serial
pause(1.5);
response = readline(serialObj);
success = contains(response, 'OK');
end
success = desiredWavelength_nm == obj.getLaserWavelength_(serialObj, channel);
end
% Function to SET the laser power
function success = setLaserPower_(obj,serialObj, channel, desiredPower_dBm)
success = 0;
writeline(serialObj, ['CH',num2str(channel),':MW?']);
modeResponse = readline(serialObj);
isMWMode = contains(modeResponse, ['CH',num2str(channel),':1']);
if isMWMode
warning('Laser is in MW Mode, you requested to set dBm power')
return
end
command = ['CH', num2str(channel), ':P=', num2str(desiredPower_dBm)];
writeline(serialObj, command);
if serialObj.Type == visalib.InterfaceType.serial
pause(0.2);
response = readline(serialObj);
success = contains(response, 'OK');
end
success = desiredPower_dBm == obj.getLaserPower_(serialObj, channel);
end
end
end