Files
imdd_silas/Classes/05_Lab/Exfo_laser.m
Silas Labor Zizou 99fe2ca106 Commit Friday evening.
PDFA and EXFO Laser are now part of the family
2024-10-25 20:31:50 +02:00

359 lines
12 KiB
Matlab

classdef Exfo_laser < handle
properties(Access=private)
serialport_number
mainframe_channel
end
properties(Access=public)
cur_state
cur_wavelength
cur_power
safety_mode
end
methods (Access=public)
function obj = Exfo_laser(options)
arguments
options.safety_mode = 1;
options.serialport_number = 'COM8';
options.mainframe_channel = 1;
end
%
fn = fieldnames(options);
for n = 1:numel(fn)
try
obj.(fn{n}) = options.(fn{n});
end
end
% Connect to the laser
o = serialport(obj.serialport_number, 9600);
configureTerminator(o, "CR", "CR"); % Set the terminator to carriage return (CR)
flush(o);
writeline(o, "*IDN?");
answ = readline(o);
if obj.safety_mode
disp(['Yay! We can talk to Instrument: ',char(answ)]);
end
% Read states the first time
obj.cur_state = obj.getLaserStatus_(o,obj.mainframe_channel);
obj.cur_wavelength = obj.getLaserWavelength_(o,obj.mainframe_channel);
obj.cur_power = obj.getLaserPower_(o,obj.mainframe_channel);
clear o;
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
warning("safety_mode OFF: EVERYTHING IS EXECUTED WITHOUT ASKING :-) BE SHURE 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
methods (Access=private)
function serialobj = connectLaser_(obj)
% Connect to the laser
serialobj = serialport(obj.serialport_number, 9600);
configureTerminator(serialobj, "CR", "CR"); % Set the terminator to carriage return (CR)
flush(serialobj);
writeline(serialobj, "*IDN?");
answ = readline(serialobj);
if obj.safety_mode
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
manual_flush = 1;
while manual_flush
response = readline(serialObj);
manual_flush = contains(response, {'OK'});
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(['Unknown Laser State ->', 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']);
response = readline(serialObj);
isok = contains(response, {'OK'});
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']);
response = readline(serialObj);
isok = contains(response, {'OK'});
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 SET the wavelength of the laser
function success = setLaserWavelength_(~,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);
pause(0.2);
response = readline(serialObj);
success = contains(response, 'OK');
end
% Function to SET the laser power
function success = setLaserPower_(~,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);
pause(0.2);
response = readline(serialObj);
success = contains(response, 'OK');
end
end
end