Skip to content

Commit

Permalink
works with crank ThermostatIO demo
Browse files Browse the repository at this point in the history
  • Loading branch information
SpieringsAE committed Apr 10, 2024
1 parent 00f2b8f commit 5ca8c2b
Show file tree
Hide file tree
Showing 8 changed files with 4,637 additions and 17 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ Then place any number of Crank deserialize event blocks in this subsystem to get
### Sending events from Simulink
Place a Crank serialize event block and select the desired event to serialize.

Copy the model from examples to the root of your project, this model is made to work with the ThermostatIO example from Crank.
This is also the project that the example header file comes from so you can move that to your project aswell.

### Notes

The first startup might take some time as it needs to compile some MEX files.
12 changes: 7 additions & 5 deletions blockset_crank/blocks/sfcn_crank_deserialize.tlc
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,13 @@

{
if (%<event_num_set> == %<event_num_in>) {
%<event_name>_event_t *data = (%<event_name>_event_t *)%<event_data>;

%foreach outnum = 2 %%SIZE(output_names)[1]
%<LibBlockOutputSignal(outnum + 1, "", "", 0)> = data->%<output_names[outnum]>;
%endforeach
%if SIZE(output_names, 1)
%<event_name>_event_t *data = (%<event_name>_event_t *)%<event_data>;

%foreach outnum = SIZE(output_names, 1)
%<LibBlockOutputSignal(outnum + 1, "", "", 0)> = data->%<output_names[outnum]>;
%endforeach
%endif

%% Call a function to process the received message via function-call subsystem
%foreach callIdx = NumSFcnSysOutputCalls
Expand Down
3 changes: 2 additions & 1 deletion blockset_crank/blocks/sfcn_crank_receive.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ static void mdlInitializeSizes(SimStruct *S) {
}

static void mdlInitializeSampleTimes(SimStruct *S) {
ssSetSampleTime(S, 0, (int_T)mxGetPr(ssGetSFcnParam(S, PARAM_TSAMP))[0]);
ssSetNumSampleTimes(S, 1);
ssSetSampleTime(S, 0, mxGetPr(ssGetSFcnParam(S, PARAM_TSAMP))[0]);
ssSetOffsetTime(S, 0, 0);
ssSetCallSystemOutput(S,OUT_FCN_CALL);
}
Expand Down
15 changes: 7 additions & 8 deletions blockset_crank/blocks/sfcn_crank_receive.tlc
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@

%function BlockInstanceSetup(block, system) void
%assign channel = "::gre_%<block.SFcnParamSettings.channel>"
%if EXISTS(%<channel>)
%if EXISTS(%<channel>) == 0
%assign %<channel> = 1
%else
%assign error = "Can't make more than one crank channel receive block for the same channel %<channel>"
%<LibBlockReportError(block, error)>
%else
%assign %<channel> = 1
%endif
%endfunction

Expand All @@ -67,8 +67,7 @@

%assign srcFile = LibGetModelDotCFile()
%<LibSetSourceFileSection(srcFile, "UserTop", tmpBuf)>

%<handle> = gre_io_open("%<block.SFcnParamSettings.channel>", GRE_IO_TYPE_RDONLY, GRE_IO_FLAG_NONBLOCK);
%<handle> = gre_io_open("%<block.SFcnParamSettings.channel>", GRE_IO_TYPE_RDONLY | GRE_IO_FLAG_NONBLOCK);
%<nbuffer> = gre_io_size_buffer(NULL, %<nbuffer_len>);
%endfunction

Expand All @@ -87,7 +86,6 @@
char *event_addr;
char *event_name;
char *event_fmt;

int32_t n = 0;
while ((n = gre_io_receive(%<handle>, &%<nbuffer>)) > 0){
gre_io_unserialize(%<nbuffer>, &event_addr, &event_name, &event_fmt, &%<event_data>);
Expand All @@ -97,18 +95,19 @@

%foreach eventnum = SIZE(events,1)
%if eventnum == 0
if (strcmp(event_name, "%<events[eventnum]>")) {
if (strcmp(event_name, "%<events[eventnum]>") == 0) {
%<event> = %<eventnum>;
}
%else
else if (strcmp(event_name, "%<events[eventnum]>")) {
else if (strcmp(event_name, "%<events[eventnum]>") == 0) {
%<event> = %<eventnum>;
}
%endif
%endforeach
else {
continue; %% no matching event found
}
event_name = NULL;
%% Call a function to process the received message via function-call subsystem
%foreach callIdx = NumSFcnSysOutputCalls
%if LibIsEqual(SFcnSystemOutputCall[callIdx].BlockToCall,"unconnected")
Expand Down
3 changes: 2 additions & 1 deletion blockset_crank/blocks/sfcn_crank_serialize.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ static void mdlInitializeSizes(SimStruct *S) {
}

static void mdlInitializeSampleTimes(SimStruct *S) {
ssSetSampleTime(S, 0, (int_T)mxGetPr(ssGetSFcnParam(S, PARAM_TSAMP))[0]);
ssSetNumSampleTimes(S, 1);
ssSetSampleTime(S, 0, mxGetPr(ssGetSFcnParam(S, PARAM_TSAMP))[0]);
ssSetOffsetTime(S, 0, 0);
}

Expand Down
4 changes: 3 additions & 1 deletion blockset_crank/blocks/sfcn_crank_serialize.tlc
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@

%assign srcFile = LibGetModelDotCFile()
%<LibSetSourceFileSection(srcFile, "UserTop", tmpBuf)>

%<handle> = gre_io_open("%<block.SFcnParamSettings.channel>", GRE_IO_TYPE_WRONLY | GRE_IO_FLAG_NONBLOCK);
%endif
%openfile tmpBuf
gre_io_serialized_data_t *%<nbuffer> = NULL; %% serialized send buffer, one per event
Expand All @@ -68,7 +70,7 @@

%assign srcFile = LibGetModelDotCFile()
%<LibSetSourceFileSection(srcFile, "UserTop", tmpBuf)>

memset(&%<event_data>, 0, sizeof(%<event_data>));
%endfunction

Expand Down
10 changes: 9 additions & 1 deletion blockset_crank/blockset_crank_version.m
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,12 @@

function [version] = blockset_crank_version()

version = "Crank v0.0.1";
version = "Crank v0.0.2";

%% V0.0.2 Update 10-4-2024
%% fixed deserialize name number
%% fixed deserialize for events containing no data
%% fixed sample time for receive and serialize blocks
%% fixed event name matching in the receive block
%% fixed channels in receive and serialize
%% added example model
Loading

0 comments on commit 5ca8c2b

Please sign in to comment.