From 657a72077bc465d23f88ae79a954d7b84ee49c51 Mon Sep 17 00:00:00 2001
From: galperti <cristian.galperti@epfl.ch>
Date: Fri, 24 Jun 2022 15:20:19 +0200
Subject: [PATCH] files for autocreating MARTe2 cfg file

---
 algos/template/marte2cfg/algo_template.cfgsrc |   5 +
 .../algo_template_create_marte2_cfg.m         |  86 +++++++++++++--
 algos/template/marte2cfg/data_states.cfgsrc   | 101 ++++++++++++++++++
 algos/template/marte2cfg/rtapp-postsl.cfgsrc  |  18 ++++
 algos/template/marte2cfg/rtapp-presl.cfgsrc   |  40 +++++++
 .../marte2cfg/rtapp-simulinkwrapper.cfgsrc    |  24 +++++
 6 files changed, 267 insertions(+), 7 deletions(-)
 create mode 100644 algos/template/marte2cfg/algo_template.cfgsrc
 create mode 100644 algos/template/marte2cfg/data_states.cfgsrc
 create mode 100644 algos/template/marte2cfg/rtapp-postsl.cfgsrc
 create mode 100644 algos/template/marte2cfg/rtapp-presl.cfgsrc
 create mode 100644 algos/template/marte2cfg/rtapp-simulinkwrapper.cfgsrc

diff --git a/algos/template/marte2cfg/algo_template.cfgsrc b/algos/template/marte2cfg/algo_template.cfgsrc
new file mode 100644
index 0000000..a004160
--- /dev/null
+++ b/algos/template/marte2cfg/algo_template.cfgsrc
@@ -0,0 +1,5 @@
+>MDSPARAMS<
+>RTAPPPRESL<
+>RTAPPSL<
+>RTAPPPOSTSL<
+>DATASTATES<
diff --git a/algos/template/marte2cfg/algo_template_create_marte2_cfg.m b/algos/template/marte2cfg/algo_template_create_marte2_cfg.m
index 4797f21..7240f93 100644
--- a/algos/template/marte2cfg/algo_template_create_marte2_cfg.m
+++ b/algos/template/marte2cfg/algo_template_create_marte2_cfg.m
@@ -7,29 +7,101 @@
 % single realtime thread, LinuxTimer synched at 1kHz
 % algorithms output MDSplus link via standard MDSWriter
 
+%% Parameters
+% source MDSplus shotnumber
+mdssrcshot=-1;
+% destination MDSplus shotnumber (-1 stands for increment current by 1)
+mdsdstshot=2;
+% verbosity level
+verbosity=2;
+
 startpath=pwd;
 fcnpath=fileparts(mfilename('fullpath'));
 eval(sprintf('cd %s',fcnpath));
 
-%% Preclean
-system('rm -rf mdsparameters.cfg');
-system('rm -rf mdswavegen.cfg');
-
 %% MDSParameters part generation
-
+system('rm -rf mdsparameters.cfg');
 diary mdsparameters.cfg
 obj.printMARTe2parconfig(-1);
 diary off
 system('sed -i ''s/obj.printMARTe2parconfig(-1);//g'' mdsparameters.cfg');
+system('sed -i ''s/MDSParameters/MDSParamLoader1/g'' mdsparameters.cfg');
 
 %% MDSWavegen datasource part generation
-
+system('rm -rf mdswavegen.cfg');
 diary mdswavegen.cfg
-obj.printMARTe2wgbusconfig(-1,'algo_template_inBus',1000);
+wg0dim=obj.printMARTe2wgbusconfig(-1,'algo_template_inBus',1000);
 diary off
 system('sed -i ''s/obj.printMARTe2wgbusconfig(-1,''algo_template_inBus'',1000);//g'' mdswavegen.cfg');
+system('sed -i ''s/MDSWavegen_algo_template_inBus_1/MDSWavegen_0/g'' mdswavegen.cfg');
+system('sed -i ''s/wavegen_algo_template_inBus_1/wavegen_0/g'' mdswavegen.cfg');
 
 %% Input bus expansion
+system('rm -rf inputbusexp.cfg');
+inbus=Simulink.data.evalinGlobal('algo_template','algo_template_inBus');
+diary inputbusexp.cfg
+insignals=bus2marte2cfg(inbus,'input','DDB1');
+diary off
+system('sed -i ''s/bus2marte2cfg(inbus,''input'',''DDB1'');//g'' inputbusexp.cfg');
+clear inbus
+
+%% Output bus expansion
+system('rm -rf outputbusexp.cfg');
+outbus=Simulink.data.evalinGlobal('algo_template','algo_template_outBus');
+diary outputbusexp.cfg
+outsignals=bus2marte2cfg(outbus,'output','DDB1');
+diary off
+system('sed -i ''s/bus2marte2cfg(outbus,''output'',''DDB1'');//g'' outputbusexp.cfg');
+clear outbus
+
+%% Wavegen input stubs
+system('rm -rf wavegenbroker1.cfg');
+diary wavegenbroker1.cfg
+for i=1:numel(insignals)
+    fprintf("%s = { DataSource = DDB1 Type = %s }\n", char(insignals{i}{1}),char(insignals{i}{2}));
+end
+diary off
+
+%% StorageBroker output stubs
+system('rm -rf storagebroker1.cfg');
+diary storagebroker1.cfg
+for i=1:numel(outsignals)
+    fprintf("%s = { DataSource = DDB1 Type = %s }\n", char(outsignals{i}{1}),char(outsignals{i}{2}));
+end
+diary off
+system('rm -rf storagebroker2.cfg');
+diary storagebroker2.cfg
+for i=1:numel(outsignals)
+    fprintf("%s = { DataSource = MDSWriter_0 Type = %s }\n", char(outsignals{i}{1}),char(outsignals{i}{2}));
+end
+diary off
+
+%% MDSWriter stubs
+system('rm -rf mdswriter1.cfg');
+diary mdswriter1.cfg
+for i=1:numel(outsignals)
+    fprintf("%s = { NodeName =""template.outputs.%s"" Period = 1 AutomaticSegmentation = 0 MakeSegmentAfterNWrites = 2000 SamplePhase = 0 } \n", char(outsignals{i}{1}),char(outsignals{i}{5}));
+end
+diary off
 
+%% cfg build
+system('cp algo_template.cfgsrc algo_template.cfg');
+system('sed -i -e "/>MDSPARAMS</r mdsparameters.cfg" -e "/>MDSPARAMS</d" algo_template.cfg');
+system('sed -i -e "/>RTAPPPRESL</r rtapp-presl.cfgsrc" -e "/>RTAPPPRESL</d" algo_template.cfg');
+system('sed -i -e "/>RTAPPSL</r rtapp-simulinkwrapper.cfgsrc" -e "/>RTAPPSL</d" algo_template.cfg');
+system('sed -i -e "/>RTAPPPOSTSL</r rtapp-postsl.cfgsrc" -e "/>RTAPPPOSTSL</d" algo_template.cfg');
+system('sed -i -e "/>DATASTATES</r data_states.cfgsrc" -e "/>DATASTATES</d" algo_template.cfg');
+system('sed -i -e "/>INPUTBUSFLAT</r wavegenbroker1.cfg" -e "/>INPUTBUSFLAT</d" algo_template.cfg');
+system('sed -i -e "/>INPUTBUSSTRUCT</r inputbusexp.cfg" -e "/>INPUTBUSSTRUCT</d" algo_template.cfg');
+system('sed -i -e "/>OUTPUTBUSSTRUCT</r outputbusexp.cfg" -e "/>OUTPUTBUSSTRUCT</d" algo_template.cfg');
+system('sed -i -e "/>OUTPUTBUSFLAT</r storagebroker1.cfg" -e "/>OUTPUTBUSFLAT</d" algo_template.cfg');
+system('sed -i -e "/>OUTPUTBUSFLATMDS</r storagebroker2.cfg" -e "/>OUTPUTBUSFLATMDS</d" algo_template.cfg');
+system('sed -i -e "/>MDSWAVEGEN0</r mdswavegen.cfg" -e "/>MDSWAVEGEN0</d" algo_template.cfg');
+system('sed -i -e "/>OUTPUTBUSMDSWRITER</r mdswriter1.cfg" -e "/>OUTPUTBUSMDSWRITER</d" algo_template.cfg');
+system(sprintf('sed -i "s/>WAVEGEN0_ELEMS</%d/g" algo_template.cfg',wg0dim));
 
+%% cfg adapt
+system(sprintf('sed -i "s/MDSSRCSHOT/%d/g" algo_template.cfg',mdssrcshot));
+system(sprintf('sed -i "s/MDSDSTSHOT/%d/g" algo_template.cfg',mdsdstshot));
+system(sprintf('sed -i "s/VERBOSITY/%d/g" algo_template.cfg',verbosity));
 
diff --git a/algos/template/marte2cfg/data_states.cfgsrc b/algos/template/marte2cfg/data_states.cfgsrc
new file mode 100644
index 0000000..aa749ae
--- /dev/null
+++ b/algos/template/marte2cfg/data_states.cfgsrc
@@ -0,0 +1,101 @@
+    +Data = {
+        Class = ReferenceContainer
+        DefaultDataSource = DDB1
+        +DDB1 = { Class = GAMDataSource }    
+        +LoggerDataSource = { Class = LoggerDataSource }
+        +Timings = { Class = TimingDataSource }
+        +Timer = {
+            Class = LinuxTimer
+            SleepNature = "Busy" // Default | Busy
+            ExecutionMode = RealTimeThread // IndependentThread | RealTimeThread
+            CPUMask = 0x8 // IndependentThread CPU mask
+            Signals = {
+                Counter = { Type = uint32 }
+                Time = { Type = uint32 }
+            }
+        }
+        >MDSWAVEGEN0<
+
+        +MDSWriter_0 = {
+            Class = MDSWriter
+            NumberOfBuffers = 2000 //Compulsory. Number of buffers in the circular buffer defined above. Each buffer is capable of holding a copy of all the DataSourceI signals.
+            CPUMask = 0x8 //Compulsory. Affinity assigned to the threads responsible for asynchronously flush data into the MDSplus database.
+            StackSize = 10000000 //Compulsory. Stack size of the thread above.
+            TreeName = "scdds" //Compulsory. Name of the MDSplus tree.
+            PulseNumber = MDSDSTSHOT //Optional. If -1 a new pulse will be created and the MDSplus pulse number incremented.
+            StoreOnTrigger = 0 //Compulsory. If 0 all the data in the circular buffer is continuously stored. If 1 data is stored when the Trigger signal is 1 (see below).
+            EventName = "updatejScope" //Compulsory. Event sent to jScope when TimeRefresh seconds have elapsed.
+            TimeRefresh = 5 //Compulsory. An event with the name set in the property EventName is sent to jScope when TimeRefresh seconds have elapsed.
+            NumberOfPreTriggers = 0 //Compulsory iff StoreOnTrigger = 1.  Number of cycles to store before the trigger.
+            NumberOfPostTriggers = 0 //Compulsory iff StoreOnTrigger = 1.  Number of cycles to store after the trigger.
+            Signals = {
+            /*
+                Trigger = { //Compulsory when StoreOnTrigger = 1. Must be set in index 0 of the Signals node. When the value of this signal is 1 data will be stored.
+                    Type = uint8 //Type must be uint8
+                }
+                Time = { //Compulsory when StoreOnTrigger = 1. Can be store in any index, but TimeSignal must be set = 1
+                    Type = uint32 //Type must be uint32 or int32
+                    TimeSignal = 1 //When set, this signal will be considered as the time source against which all signals will be stored.
+                    TimeSignalMultiplier = 1e-9 //Default = 1e-6. Multiplier to convert the time signal units into seconds,
+                }
+            */
+            
+                Thread1_Cycletime       = { NodeName = "ST.CH01" Period = 1 AutomaticSegmentation = 0 MakeSegmentAfterNWrites = 2000 SamplePhase = 0 }
+                GAMSimulink1_ReadTime   = { NodeName = "ST.CH02" Period = 1 AutomaticSegmentation = 0 MakeSegmentAfterNWrites = 2000 SamplePhase = 0 }
+                GAMSimulink1_ExecTime   = { NodeName = "ST.CH03" Period = 1 AutomaticSegmentation = 0 MakeSegmentAfterNWrites = 2000 SamplePhase = 0 }
+                GAMSimulink1_WriteTime  = { NodeName = "ST.CH04" Period = 1 AutomaticSegmentation = 0 MakeSegmentAfterNWrites = 2000 SamplePhase = 0 }
+                
+                >OUTPUTBUSMDSWRITER<
+            }
+            /*
+            +Messages = { //Optional. If set a message will be fired every time one of the events below occur
+                Class = ReferenceContainer
+                +TreeOpenedOK = { //Optional, but if set, the name of the Object shall be TreeOpenedOK. If set a message containing a ConfigurationDatabase with param1=PULSE_NUMBER will be sent to the Destination, every time the Tree is successfully opened
+                    Class = Message
+                    Destination = SomeObject
+                    Function = SomeFunction
+                    Mode = ExpectsReply
+                }
+                +TreeOpenedFail = { //Optional, but if set, the name of the Object shall be TreeOpenedFail. If set a message will be sent to the Destination, every time the Tree cannot be successfully opened
+                    Class = Message
+                    Destination = SomeObject
+                    Function = SomeFunction
+                    Mode = ExpectsReply
+                }
+                +TreeFlushed = { //Optional, but if set, the name of the Object shall be TreeFlushed. If set a message will be sent to the Destination, every time the Tree is flushed.
+                    Class = Message
+                    Destination = SomeObject
+                    Function = SomeFunction
+                    Mode = ExpectsReply
+                }
+            }
+            */
+        }
+    }
+    +States = {
+        Class = ReferenceContainer
+        +State1 = {
+            Class = RealTimeState
+            +Threads = {
+                Class = ReferenceContainer
+                +Thread1 = {
+                    Class = RealTimeThread 
+                    CPUs = 0x4
+                    Functions = {
+                      GAMTimer                         	      
+                      Constants
+                      Wavegen0TimeBroker
+                      Wavegen0DataBroker
+                      GAMSimulink1
+                      StorageBroker
+                      //LoggerBroker
+                    }
+                }
+            }
+        }        
+    }
+    +Scheduler = {
+        Class = GAMScheduler
+        TimingDataSource = Timings
+    }
+}
diff --git a/algos/template/marte2cfg/rtapp-postsl.cfgsrc b/algos/template/marte2cfg/rtapp-postsl.cfgsrc
new file mode 100644
index 0000000..0a9a1f9
--- /dev/null
+++ b/algos/template/marte2cfg/rtapp-postsl.cfgsrc
@@ -0,0 +1,18 @@
+		+StorageBroker = {
+		 Class = IOGAM
+		 InputSignals = {            
+            Thread1_CycleTime           = { DataSource = Timings Type = uint32 Alias = State1.Thread1_CycleTime }
+            GAMSimulink1_ReadTime  		= { DataSource = Timings Type = uint32 } 
+	        GAMSimulink1_ExecTime  		= { DataSource = Timings Type = uint32 }  
+	        GAMSimulink1_WriteTime 		= { DataSource = Timings Type = uint32 } 
+	        >OUTPUTBUSFLAT<
+		 }
+		 OutputSignals = {                        
+            Thread1_Cycletime           = { DataSource = MDSWriter_0 Type = uint32 }
+            GAMSimulink1_ReadTime  		= { DataSource = MDSWriter_0 Type = uint32 } 
+	        GAMSimulink1_ExecTime  		= { DataSource = MDSWriter_0 Type = uint32 }  
+	        GAMSimulink1_WriteTime 		= { DataSource = MDSWriter_0 Type = uint32 } 
+            >OUTPUTBUSFLATMDS<
+		 }
+		} 
+    }
diff --git a/algos/template/marte2cfg/rtapp-presl.cfgsrc b/algos/template/marte2cfg/rtapp-presl.cfgsrc
new file mode 100644
index 0000000..7d0085f
--- /dev/null
+++ b/algos/template/marte2cfg/rtapp-presl.cfgsrc
@@ -0,0 +1,40 @@
+$TestApp = {
+    Class = RealTimeApplication
+    +Functions = {
+        Class = ReferenceContainer
+        +GAMTimer = {
+            Class = IOGAM
+            InputSignals = {
+                Counter = {                  DataSource = Timer Type = uint32 }                
+                Time 	= { Frequency = 1000 DataSource = Timer Type = uint32 }
+            }
+            OutputSignals = {
+                Counter = {                  DataSource = DDB1  Type = uint32 }                
+                Time    = {                	 DataSource = DDB1  Type = uint32 }            
+            }
+        }
+	    +Constants = {
+	     Class = ConstantGAM
+	     OutputSignals = {
+				zero	= { DataSource = DDB1 Type = float32  NumberOfElements = 1  NumberOfDimensions=0 Default=0 }				
+	     }
+	    }
+        +Wavegen0TimeBroker = {
+            Class = IOGAM
+            InputSignals = { 
+                Time = { DataSource = DDB1 Type = uint32 } 
+            }
+            OutputSignals = { 
+                time = { DataSource = MDSWavegen_0 Type = int32 } 
+            }
+        }                   
+        +Wavegen0DataBroker = {
+            Class = IOGAM
+            InputSignals = {
+                wavegen_0 = { DataSource = MDSWavegen_0 Type = float32 NumberOfElements = >WAVEGEN0_ELEMS< }
+            }
+            OutputSignals = {
+                >INPUTBUSFLAT<
+            }
+        }
+        
\ No newline at end of file
diff --git a/algos/template/marte2cfg/rtapp-simulinkwrapper.cfgsrc b/algos/template/marte2cfg/rtapp-simulinkwrapper.cfgsrc
new file mode 100644
index 0000000..c9b4a80
--- /dev/null
+++ b/algos/template/marte2cfg/rtapp-simulinkwrapper.cfgsrc
@@ -0,0 +1,24 @@
+		+GAMSimulink1 = {
+ 			Class = SimulinkWrapperGAM
+			Library = "/root/algo_template.so"			
+			SymbolPrefix = "algo_template"			
+			Verbosity = VERBOSITY
+			SkipInvalidTunableParams = 0
+            TunableParamExternalSource= "MDSParamLoader1"
+            //NonVirtualBusMode = "ByteArray" // NOTE: homogeneus buses are not handled correctly, wrong
+                                            // handling of NumberOfElements = 1
+            NonVirtualBusMode = "Structured" 
+                                            
+			InputSignals = {		
+				realtime	= { DataSource = DDB1 Type = float32 NumberOfElements = 1 NumberOfDimensions=0 Alias = wavegen_0 }
+                >INPUTBUSSTRUCT<
+ 			}
+ 			OutputSignals = { 
+                >OUTPUTBUSSTRUCT<
+            }
+            /*
+			Parameters    =  {
+                k1 = (float32) 2.5
+            }
+            */
+		}
-- 
GitLab