Skip to content

Commit 47179bf

Browse files
author
Max 'MaxMax' Mönikes
committed
Added some Matlab s-function stuff nd mutex support
1 parent fcb12e7 commit 47179bf

File tree

8 files changed

+566
-38
lines changed

8 files changed

+566
-38
lines changed

Matlab_S-Functions/Sink.m

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
classdef Sink < matlab.System & coder.ExternalDependency
2+
%
3+
% System object template for a sink block.
4+
%
5+
% This template includes most, but not all, possible properties,
6+
% attributes, and methods that you can implement for a System object in
7+
% Simulink.
8+
%
9+
% NOTE: When renaming the class name Sink, the file name and
10+
% constructor name must be updated to use the class name.
11+
%
12+
13+
% Copyright 2016-2018 The MathWorks, Inc.
14+
%#codegen
15+
%#ok<*EMCA>
16+
17+
properties
18+
% Public, tunable properties.
19+
end
20+
21+
properties (Nontunable)
22+
% Public, non-tunable properties.
23+
end
24+
25+
properties (Access = private)
26+
% Pre-computed constants.
27+
end
28+
29+
methods
30+
% Constructor
31+
function obj = Sink(varargin)
32+
% Support name-value pair arguments when constructing the object.
33+
setProperties(obj,nargin,varargin{:});
34+
end
35+
end
36+
37+
methods (Access=protected)
38+
function setupImpl(obj) %#ok<MANU>
39+
if isempty(coder.target)
40+
% Place simulation setup code here
41+
else
42+
% Call C-function implementing device initialization
43+
% coder.cinclude('sink.h');
44+
% coder.ceval('sink_init');
45+
end
46+
end
47+
48+
function stepImpl(obj,u) %#ok<INUSD>
49+
if isempty(coder.target)
50+
% Place simulation output code here
51+
else
52+
% Call C-function implementing device output
53+
%coder.ceval('sink_output',u);
54+
end
55+
end
56+
57+
function releaseImpl(obj) %#ok<MANU>
58+
if isempty(coder.target)
59+
% Place simulation termination code here
60+
else
61+
% Call C-function implementing device termination
62+
%coder.ceval('sink_terminate');
63+
end
64+
end
65+
end
66+
67+
methods (Access=protected)
68+
%% Define input properties
69+
function num = getNumInputsImpl(~)
70+
num = 1;
71+
end
72+
73+
function num = getNumOutputsImpl(~)
74+
num = 0;
75+
end
76+
77+
function flag = isInputSizeMutableImpl(~,~)
78+
flag = false;
79+
end
80+
81+
function flag = isInputComplexityMutableImpl(~,~)
82+
flag = false;
83+
end
84+
85+
function validateInputsImpl(~, u)
86+
if isempty(coder.target)
87+
% Run input validation only in Simulation
88+
validateattributes(u,{'double'},{'scalar'},'','u');
89+
end
90+
end
91+
92+
function icon = getIconImpl(~)
93+
% Define a string as the icon for the System block in Simulink.
94+
icon = 'Sink';
95+
end
96+
end
97+
98+
methods (Static, Access=protected)
99+
function simMode = getSimulateUsingImpl(~)
100+
simMode = 'Interpreted execution';
101+
end
102+
103+
function isVisible = showSimulateUsingImpl
104+
isVisible = false;
105+
end
106+
end
107+
108+
methods (Static)
109+
function name = getDescriptiveName()
110+
name = 'Sink';
111+
end
112+
113+
function b = isSupportedContext(context)
114+
b = context.isCodeGenTarget('rtw');
115+
end
116+
117+
function updateBuildInfo(buildInfo, context)
118+
if context.isCodeGenTarget('rtw')
119+
% Update buildInfo
120+
srcDir = fullfile(fileparts(mfilename('fullpath')),'src'); %#ok<NASGU>
121+
includeDir = fullfile(fileparts(mfilename('fullpath')),'include');
122+
addIncludePaths(buildInfo,includeDir);
123+
% Use the following API's to add include files, sources and
124+
% linker flags
125+
%addIncludeFiles(buildInfo,'source.h',includeDir);
126+
%addSourceFiles(buildInfo,'source.c',srcDir);
127+
%addLinkFlags(buildInfo,{'-lSource'});
128+
%addLinkObjects(buildInfo,'sourcelib.a',srcDir);
129+
%addCompileFlags(buildInfo,{'-D_DEBUG=1'});
130+
%addDefines(buildInfo,'MY_DEFINE_1')
131+
end
132+
end
133+
end
134+
end
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
classdef TLE9012_Voltage_Sense < realtime.internal.SourceSampleTime & ...
2+
coder.ExternalDependency
3+
%
4+
% System object template for a source block.
5+
%
6+
% This template includes most, but not all, possible properties,
7+
% attributes, and methods that you can implement for a System object in
8+
% Simulink.
9+
%
10+
% NOTE: When renaming the class name Source, the file name and
11+
% constructor name must be updated to use the class name.
12+
%
13+
14+
% Copyright 2016-2018 The MathWorks, Inc.
15+
%#codegen
16+
%#ok<*EMCA>
17+
18+
properties
19+
% Public, tunable properties.
20+
end
21+
22+
properties (Nontunable)
23+
% Public, non-tunable properties.
24+
end
25+
26+
properties (Access = private)
27+
% Pre-computed constants.
28+
end
29+
30+
methods
31+
% Constructor
32+
function obj = TLE9012_Voltage_Sense(varargin)
33+
% Support name-value pair arguments when constructing the object.
34+
setProperties(obj,nargin,varargin{:});
35+
end
36+
end
37+
38+
methods (Access=protected)
39+
function setupImpl(obj) %#ok<MANU>
40+
if isempty(coder.target)
41+
% Place simulation setup code here
42+
else
43+
% Call C-function implementing device initialization
44+
%coder.cinclude('C:/Users/max/OneDrive/Dokumente/Arduino/libraries/TLE9012_Arduino_Lib/Matlab_S-Functions/inc/TLE9012_Matlab.h');
45+
coder.cinclude('C:/Users/max/OneDrive/Dokumente/Arduino/libraries/TLE9012_Arduino_Lib/TLE9012.h');
46+
coder.ceval('init_tle9012');
47+
end
48+
end
49+
50+
function cell_voltages = stepImpl(obj) %#ok<MANU>
51+
cell_voltages = zeros(12,1,'single')
52+
if isempty(coder.target)
53+
% Place simulation output code here
54+
cell_voltages = zeros(12,1,'single')
55+
else
56+
% Call C-function implementing device output
57+
coder.ceval('get_cell_voltages',coder.wref(cell_voltages));
58+
end
59+
end
60+
61+
function releaseImpl(obj) %#ok<MANU>
62+
if isempty(coder.target)
63+
% Place simulation termination code here
64+
else
65+
% Call C-function implementing device termination
66+
coder.ceval('tle9012_Terminate');
67+
end
68+
end
69+
end
70+
71+
methods (Access=protected)
72+
%% Define output properties
73+
function num = getNumInputsImpl(~)
74+
num = 0;
75+
end
76+
77+
function num = getNumOutputsImpl(~)
78+
num = 1;
79+
end
80+
81+
function varargout = isOutputFixedSizeImpl(~,~)
82+
varargout{1} = true;
83+
end
84+
85+
86+
function varargout = isOutputComplexImpl(~)
87+
varargout{1} = false;
88+
end
89+
90+
function varargout = getOutputSizeImpl(~)
91+
varargout{1} = [12,1];
92+
end
93+
94+
function varargout = getOutputDataTypeImpl(~)
95+
varargout{1} = 'single';
96+
end
97+
98+
function icon = getIconImpl(~)
99+
% Define a string as the icon for the System block in Simulink.
100+
icon = 'TLE9012 Voltage Sense';
101+
end
102+
end
103+
104+
methods (Static, Access=protected)
105+
function simMode = getSimulateUsingImpl(~)
106+
simMode = 'Interpreted execution';
107+
end
108+
109+
function isVisible = showSimulateUsingImpl
110+
isVisible = false;
111+
end
112+
end
113+
114+
methods (Static)
115+
function name = getDescriptiveName()
116+
name = 'Source';
117+
end
118+
119+
function b = isSupportedContext(context)
120+
b = context.isCodeGenTarget('rtw');
121+
end
122+
123+
function updateBuildInfo(buildInfo, context)
124+
if context.isCodeGenTarget('rtw')
125+
% Update buildInfo
126+
srcDir = fullfile(fileparts(mfilename('fullpath')),'src'); %#ok<NASGU>
127+
includeDir = fullfile(fileparts(mfilename('fullpath')),'include')
128+
libDir = 'C:/Users/max/OneDrive/Dokumente/Arduino/libraries/TLE9012_Arduino_Lib'
129+
addIncludePaths(buildInfo,includeDir);
130+
addIncludePaths(buildInfo,libDir);
131+
% Use the following API's to add include files, sources and
132+
% linker flags
133+
%addIncludeFiles(buildInfo,'TLE9012.h',libDir);
134+
addSourceFiles(buildInfo,'TLE9012.cpp',libDir);
135+
addIncludeFiles(buildInfo,'TLE9012_Matlab.h',includeDir);
136+
addSourceFiles(buildInfo,'TLE9012_Matlab.cpp',srcDir);
137+
%addLinkFlags(buildInfo,{'-lSource'});
138+
%addLinkObjects(buildInfo,'sourcelib.a',srcDir);
139+
%addCompileFlags(buildInfo,{'-D_DEBUG=1'});
140+
%addDefines(buildInfo,'MY_DEFINE_1')
141+
end
142+
end
143+
end
144+
end
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#ifndef _TLE9012_MATLAB_H_
2+
#define _TLE9012_MATLAB_H_
3+
4+
#ifdef __cplusplus
5+
extern "C" {
6+
#endif
7+
8+
void init_tle9012(void);
9+
void get_cell_voltages(float*);
10+
void get_cell_voltages_with_current(float*, float*);
11+
void tle9012_Terminate(void);
12+
13+
14+
#ifdef __cplusplus
15+
}
16+
#endif
17+
18+
#endif
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#include "../include/TLE9012_Matlab.h"
2+
#include "../../TLE9012.h"
3+
4+
5+
#define N_CELLS 6
6+
#define TXPIN 17
7+
#define RXPIN 16
8+
9+
TLE9012 tle9012;
10+
uint8_t tle_init_cplt = 0;
11+
12+
extern "C" void init_tle9012(void)
13+
{
14+
tle9012.init(&Serial2, 2000000,RXPIN,TXPIN); //Initialize driver with 2Mbit
15+
16+
tle9012.wakeUp(); //Issue wakeup command
17+
tle9012.setExtendedWatchdog(0);
18+
delay(200); //Wait some time to ensure wakeup was completed
19+
20+
tle9012.setNodeID(0, 1, 1); //Set device address from 0 to 1 and set device 1 as final Node
21+
22+
tle9012.writeMailboxRegister(1, 0xAA55); //Write and Readback Mailbox register to check if communication works
23+
uint16_t mailbox = tle9012.readMailboxRegister(1);
24+
25+
if(mailbox == 0xAA55) //Show Successfull communication
26+
tle_init_cplt = 1;
27+
28+
tle9012.setNumberofCells(1, N_CELLS); //Configure the number of cells
29+
tle9012.setBAVMConfig(1,1);
30+
tle9012.resetWatchdog(); //Reset Watchdog Timer
31+
tle_init_cplt = 1;
32+
}
33+
34+
extern "C" void get_cell_voltages(float* voltages)
35+
{
36+
if(tle_init_cplt == 0)
37+
return;
38+
39+
tle9012.resetWatchdog(); //Reset Watchdog Timer
40+
tle9012.readCellVoltages(1); //Read all cell voltages from device 1
41+
for(uint8_t n = 0; n < N_CELLS; n++)
42+
voltages[n] = ADCVALUE_TO_FLOAT_VOLTAGE(tle9012.devices[0].cell_voltages[n]);
43+
}
44+
45+
extern "C" void get_cell_voltages_with_current(float* voltages, float* current)
46+
{
47+
if(tle_init_cplt == 0)
48+
return;
49+
50+
tle9012.resetWatchdog(); //Reset Watchdog Timer
51+
tle9012.readCellVoltagesWithBAVM(1); //Read all cell voltages from device 1
52+
for(uint8_t n = 0; n < N_CELLS; n++)
53+
voltages[n] = ADCVALUE_TO_FLOAT_VOLTAGE(tle9012.devices[0].cell_voltages[n]);
54+
55+
current[0] = (float)tle9012.devices[0].bipolar_auxilary_voltage*2.0/32768 * 5;
56+
}
57+
58+
extern "C" void tle9012_Terminate(void)
59+
{
60+
61+
}

0 commit comments

Comments
 (0)