44import os .path
55
66from rip .RIPGeneric import RIPGeneric
7- from .MatlabConnector import CommandBuilder , MatlabConnector
8- from .SimulinkConnector import SimulinkConnector
7+ from rip .matlab .MatlabConnector import CommandBuilder , MatlabConnector
98
109import matlab .engine
1110
@@ -14,87 +13,62 @@ class RIPMatlab(RIPGeneric):
1413 RIP MATLAB Adapter
1514 '''
1615
17- def __init__ (self , name = 'Octave' , description = 'An implementation of RIP to control Octave' , authors = 'J. Chacon' , keywords = 'Octave' ):
16+ def __init__ (self , info ):
1817 '''
1918 Constructor
2019 '''
21- super ().__init__ (name , description , authors , keywords )
20+ super ().__init__ (info )
2221
23- self .commandBuilder = CommandBuilder ()
24- self .matlab = MatlabConnector ()
25- self .simulink = SimulinkConnector ()
22+ self .matlab = matlab .engine .start_matlab ('-desktop' )
2623
27- self .addMethods ({
28- 'connect' : { 'description' : 'Start a new MATLAB session' ,
29- 'params' : {},
30- 'implementation' : self .connect ,
31- },
32- 'disconnect' : { 'description' : 'Finish the current MATLAB session' ,
33- 'params' : {},
34- 'implementation' : self .disconnect ,
35- },
36- 'get' : { 'description' : 'Read variables from the MATLAB\' s workspace' ,
37- 'params' : { 'expId' : 'string' , 'variables' : '[string]' },
38- 'implementation' : self .get ,
39- },
40- 'set' : { 'description' : 'Send values to variables in the MATLAB\' s workspace' ,
41- 'params' : { 'expId' : 'string' , 'variables' : '[string]' , 'values' :'[]' },
42- 'implementation' : self .set ,
43- },
44- 'eval' : { 'description' : 'Run MATLAB code' ,
45- 'params' : { 'expId' : 'string' , 'code' : '[string]' },
46- 'implementation' : self .set ,
47- },
48- })
49-
50- def connect (self ):
51- self ._matlab = matlab .engine .start_matlab ('-desktop' )
52- self .simulink .set
53- return True
54-
55- def disconnect (self ):
56- self ._matlab .quit ()
24+ def default_info (self ):
25+ '''
26+ Default metadata.
27+ '''
28+ return {
29+ 'name' : 'Matlab' ,
30+ 'description' : 'An implementation of RIP to control MATLAB' ,
31+ 'authors' : 'J. Chacon' ,
32+ 'keywords' : 'MATLAB, Raspberry PI' ,
33+ 'readables' : [],
34+ 'writables' : [],
35+ }
5736
58- def set (self , variables , values ):
37+ def set (self , expid , variables , values ):
38+ if isinstance (variables , list ):
39+ size = len (variables )
40+ for i in range (size ):
41+ try :
42+ name , value = variables [i ], values [i ]
43+ self .matlab .workspace [name ] = value
44+ except :
45+ pass
46+ else :
47+ try :
48+ name , value = variables , values
49+ self .matlab .workspace [name ] = value
50+ except :
51+ pass
5952 self .matlab .set (variables , values )
6053
61- def get (self , variables ):
62- return self .matlab .get (variables )
54+ def get (self , expid , variables ):
55+ readables = self ._getReadables ()
56+ values = []
57+ for n in variables :
58+ try :
59+ if n in readables :
60+ v = self .matlab .workspace [n ]
61+ values .append (v )
62+ except :
63+ values .append ('###ERROR###' )
64+ return [variables , values ]
65+
66+ def preGetValuesToNotify (self ):
67+ self .matlab .workspace ['time' ] = self .sampler .time
6368
64- def eval (self , command ):
65- try :
66- result = self ._matlab .eval (command , nargout = 0 )
67- except :
68- pass
69- return result
69+ def getValuesToNotify (self , expid = None ):
70+ values = self .get (expid , self ._getReadables ())
71+ return values
7072
71- # def _open(self, path):
72- # try:
73- # #open
74- # dirname = os.path.dirname(path)
75- # cd = self.commandBuilder.cd(dirname)
76- # model = os.path.basename(path)
77- # load_system = self.commandBuilder.load_system(model)
78- # command = cd + load_system
79- # result = self._matlab.eval(command, nargout=0)
80- # #addEjsSubsystem
81- # _load_model(model)
82- #
83- # except:
84- # return None
85- # return result
86- #
87- # def _load_model(self, model):
88- # command = self.commandBuilder.addEjsSubblock(model) + \
89- # self.commandBuilder.addPauseBlock(model) + \
90- # self.commandBuilder.set_param(model, "StartTime", "0") + \
91- # self.commandBuilder.set_param(model, "StopTime", "inf")
92- # self._matlab.eval(command)
93- #
94- #
95- # def _step(self, command):
96- # try:
97- # result = self._matlab.eval(command, nargout=0)
98- # except:
99- # return None
100- # return result
73+ def postGetValuesToNotify (self ):
74+ pass
0 commit comments