Commit Friday evening.
PDFA and EXFO Laser are now part of the family
This commit is contained in:
@@ -1,8 +1,14 @@
|
||||
classdef Exfo_laser
|
||||
classdef Exfo_laser < handle
|
||||
|
||||
properties(Access=private)
|
||||
serialport_number
|
||||
mainframe_channel
|
||||
end
|
||||
properties(Access=public)
|
||||
wavelength
|
||||
power
|
||||
cur_state
|
||||
cur_wavelength
|
||||
cur_power
|
||||
safety_mode
|
||||
end
|
||||
|
||||
methods (Access=public)
|
||||
@@ -11,8 +17,9 @@ classdef Exfo_laser
|
||||
|
||||
|
||||
arguments
|
||||
options.wavelength = 1310; %dbm
|
||||
options.power = -10; %dbm
|
||||
options.safety_mode = 1;
|
||||
options.serialport_number = 'COM8';
|
||||
options.mainframe_channel = 1;
|
||||
end
|
||||
|
||||
%
|
||||
@@ -23,52 +30,330 @@ classdef Exfo_laser
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
function success = set(obj,options)
|
||||
|
||||
|
||||
arguments
|
||||
obj
|
||||
options.wavelength = obj.wavelength; %dbm
|
||||
options.power = obj.power; %dbm
|
||||
end
|
||||
|
||||
% Connect to the laser
|
||||
o = serialport("COM8", 9600);
|
||||
configureTerminator(o, "CR"); % Set the terminator to carriage return (CR)
|
||||
o = serialport(obj.serialport_number, 9600);
|
||||
configureTerminator(o, "CR", "CR"); % Set the terminator to carriage return (CR)
|
||||
flush(o);
|
||||
writeline(o, "*IDN?");
|
||||
pause(1);
|
||||
if o.NumBytesAvailable ~= 0
|
||||
disp(['Laser Mainframe: ', readline(o)]);
|
||||
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
|
||||
error('No connection to the mainframe');
|
||||
clear o;
|
||||
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 to set the wavelength of the laser
|
||||
function setLaserWavelength(~,serialObj, channel, wavelength)
|
||||
command = ['CH', num2str(channel), ':L=', num2str(wavelength)];
|
||||
writeline(serialObj, command);
|
||||
pause(0.5); % Allow time for the wavelength to change
|
||||
|
||||
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
|
||||
current_wavelen = readline(serialObj);
|
||||
disp(['Current Wavelength: ', current_wavelen]);
|
||||
|
||||
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 set the laser power
|
||||
function setLaserPower(~,serialObj, channel, power_dBm)
|
||||
command = ['CH', num2str(channel), ':P=', num2str(power_dBm)];
|
||||
writeline(serialObj, command);
|
||||
pause(0.2); % Allow time for power to adjust
|
||||
% Function to GET the laser power
|
||||
function powerValue = getLaserPower_(obj,serialObj, channel)
|
||||
|
||||
writeline(serialObj, ['CH', num2str(channel), ':P?']); % Query current power
|
||||
current_power = readline(serialObj);
|
||||
disp(['Current Power: ', current_power, ' dBm']);
|
||||
|
||||
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
|
||||
end
|
||||
Reference in New Issue
Block a user