From 34af729d9115f05f880952ba2c3bb0b1903bd17e Mon Sep 17 00:00:00 2001
From: Lorenzo Pivetta <lorenzo.pivetta@elettra.eu>
Date: Mon, 29 May 2023 15:24:57 +0200
Subject: [PATCH] First import

---
 gen_seq_waveform.m | 85 ++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 85 insertions(+)
 create mode 100644 gen_seq_waveform.m

diff --git a/gen_seq_waveform.m b/gen_seq_waveform.m
new file mode 100644
index 0000000..52f09e5
--- /dev/null
+++ b/gen_seq_waveform.m
@@ -0,0 +1,85 @@
+clear all;
+
+for num_sequences=1:100
+
+seq_name = sprintf('seq_waveform_%d.txt',num_sequences);
+
+header{1} = '#';
+header{2} = '# Description: $description';
+header{3} = '# Author: Sequencer Library';
+header{4} = '# Date: 2020/02/05';
+header{5} = '#';
+header{6} = sprintf('# This sequence generates a waveform of %d samples',num_sequences);
+header{7} = '#';
+
+error_step = 3 + num_sequences * 3;
+
+cnt = 0;
+cnt = cnt + 1;
+blk{cnt}=sprintf('step%d;(write(this/StartValue) = read($device/$attribute)) && (write(this/NewValue) = read(this/StartValue)) && goto(%d);Saving $device/$attribute starting value;Error saving $device/$attribute starting value;-1',cnt,cnt+1);
+
+% waveform steps
+for seq=1:num_sequences
+    cnt = cnt + 1;
+    blk{cnt}=sprintf('step%d;sleep($sleep_monitor) && ((read($device/State) == $allowed_state) && (abs(read(this/NewValue) - read($device/$attribute)) < read(this/ErrorThrehold))) ? goto(%d) : goto(%d);Waiting settling time;Error waiting settling time;$settling_timeout000;tangoerror=goto(%d);timeouterror=goto(%d)',cnt,cnt+1,cnt,error_step,error_step);
+    cnt = cnt + 1;   
+    blk{cnt}=sprintf('step%d;read(this/AbsMode) == 0 ? (write(this/NewValue) = (read(this/StartValue)+(read(this/Value%d)*read(this/ScalingFactor))) && goto(%d) :  (write(this/NewValue) = (read(this/Value%d)*read(this/ScalingFactor))) && goto(%d);Calculating new value;Error calculating new value;-1;tangoerror=goto(%d)',cnt,seq,cnt+1,seq,cnt+1,error_step);
+    cnt = cnt + 1;   
+    blk{cnt}=sprintf('step%d;(write($device/$attribute) = read(this/NewValue)) && goto(%d);Setting $device/$attribute;Error setting $device/$attribute;-1;tangoerror=goto(%d)',cnt,cnt+1,error_step);
+end
+
+cnt = cnt + 1;
+blk{cnt}=sprintf('step%d;sleep($sleep_monitor) && ((read($device/State) == $allowed_state) && (abs(read(this/NewValue) - read($device/$attribute)) < read(this/ErrorThrehold))) ? goto(%d) : goto(%d);Waiting settling time;Error waiting settling time;$settling_timeout000;tangoerror=goto(%d);timeouterror=goto(%d)',cnt,cnt+2,cnt,error_step,error_step);
+
+cnt = cnt + 1;
+blk{cnt}=sprintf('step%d;(write($device/$attribute) = read(this/StartValue)) && goto(-1);Error, restoring StartValue and exit;Error restoring StartValue;-1',cnt);
+
+cnt = cnt + 1;
+blk{cnt}=sprintf('step%d;(write(this/NumCycles) = (read(this/NumCycles)+1)) && ((read(this/InfiniteLoop) == 1) || (read(this/NumCycles) < read(this/MaxNumCycles))) ? goto(3) : goto(%d);Checking NumCycles;Error checking NumCycles;-1',cnt,cnt+1);
+
+% dynamic attributes
+cnt = 0; 
+cnt = cnt + 1;
+footer{cnt} = 'attr;AbsMode;bool;0';
+cnt = cnt + 1;
+footer{cnt} = 'attr;MaxNumCycles;long;1';
+cnt = cnt + 1;
+footer{cnt} = 'attr;InfiniteLoop;bool;0';
+cnt = cnt + 1;
+footer{cnt} = 'attr;ScalingFactor;double;1';
+
+for seq=1:num_sequences
+    cnt = cnt + 1;
+    footer{cnt} = sprintf('attr;Value%d;double;0',seq);
+end
+
+cnt = cnt + 1;
+footer{cnt} = 'attr;NumCycles;long;0';
+cnt = cnt + 1;
+footer{cnt} = 'attr;StartValue;double;0';
+cnt = cnt + 1;
+footer{cnt} = 'attr;NewValue;double;0';
+
+
+fileID = fopen(seq_name,'w');
+for i=1:size(header,2)
+    fprintf(fileID,'%s\n',header{i});
+end
+for i=1:size(blk,2)
+    fprintf(fileID,'%s\n',blk{i});
+end
+for i=1:size(footer,2)
+    fprintf(fileID,'%s\n',footer{i});
+end
+
+fclose(fileID);
+
+end
+
+% Wildcards
+% $device=sl/piezo/hera_sl.02
+% $attribute=Position
+% $allowed_state=ON
+% $sleep_monitor=0.1
+% $settling_timeout=10
+
-- 
GitLab