Skip to content

Commit

Permalink
Code reorganization
Browse files Browse the repository at this point in the history
  • Loading branch information
aditya00j committed Dec 2, 2020
1 parent b312d9f commit 243f8fc
Show file tree
Hide file tree
Showing 337 changed files with 165 additions and 140,110 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Generated/*
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
function [busObj, bus_name, bus_orig_dtypes] = create_bus_from_mavlink_header(filename)
% CREATE_BUS_FROM_MAVLINK_HEADER: Create a Simulink Bus from a single
% MAVLink message header file
%
% NOTE: This function is called by another function create_sfun_header,
% so the user would typically not have to directly use this function.
% CREATE_BUS_FROM_MAVLINK_HEADER: Create a Simulink Bus from a MAVLink message header file
%
% Input: string containing the MAVLink message header file name
%
% Input: string containing the header file name.
% Output:
% busObj: bus object
% bus_name: string containing name of the bus
Expand All @@ -19,14 +14,14 @@
fid = fopen(filename, 'r');
datatypes = readtable('datatype_map.csv','ReadVariableNames',1);

% Skip lines till "MAVPACKED"
% Assume that the first line containing "typedef struct __" is the message
% definition.
lin = fgetl(fid);
while ~contains(lin,'MAVPACKED')
while ~startsWith(lin, 'typedef struct __')
lin = fgetl(fid);
end

% Next line is "typedef struct" - extract bus name from this
lin = fgetl(fid);
% Extract bus name
bus_name = strtrim(erase(lin,{'typedef struct __','{'}));
bus_orig_dtypes = struct;

Expand All @@ -44,12 +39,13 @@
if isempty(dimensions)
dimensions = 1;
else
% DO NOT CHANGE the following to str2double
dimensions = str2num(dimensions{1});
name = regexp(name,'\[(\d*)\]','split');
name = name{1};
end

idx = find(strcmp(datatype,datatypes.mavlink), 1);
idx = find(strcmp(datatype,datatypes.px4), 1);
if isempty(idx)
error(['Could not find datatype ' datatype ' in datatype_map.csv'])
else
Expand All @@ -59,7 +55,7 @@
busElements(nfields).DataType = char(datatypes.simulink(idx));
busElements(nfields).Dimensions = dimensions;
busElements(nfields).Description = description;
bus_orig_dtypes.(name) = char(datatypes.mavlink(idx));
bus_orig_dtypes.(name) = char(datatypes.px4(idx));
end
lin = fgetl(fid);
end
Expand Down
66 changes: 25 additions & 41 deletions create_sfun_decode.m → Functions/create_sfun_decode.m
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,10 @@ function create_sfun_decode(filenames, sys_id, comp_id)
% messages from an incoming MAVLink stream
%
% Inputs:
% filenames: strings containing the full path to the MAVLink messages
% header file names. These files must be created as a part of a
% single MAVLink dialect, and must reside in the directory
% structure of the corresponding dialect. In other words, the
% directory containing this message file must also contain the
% "mavlink.h" file, and its parent directory must contain the
% other commond mavlink files such as "protocol.h".
%
% NOTE: To ensure compiler independence, provide the full paths
% of the message files, and not relative paths.
%
% sys_id: MAVLink SYSID to be used for Simulink (default 100)
% comp_id: MAVLink COMPID to be used for Simulink (default 200)
% filenames: cell array of strings containing the MAVLink message header
% files for messages to be decoded
% sys_id: MAVLink SYSID to be used for Simulink (default 100)
% comp_id: MAVLink COMPID to be used for Simulink (default 200)
%
% Output:
% This function creates the Simulink buses, the s-function header files,
Expand All @@ -24,20 +15,14 @@ function create_sfun_decode(filenames, sys_id, comp_id)
%Part of the Simulink MAVLink package.
%(c) Aditya Joshi, November 2017


%% Parse inputs

if ~iscell(filenames), filenames = {filenames}; end

disp('*** Running create_sfun_decode:')
disp('--')
disp('Creating decoder s-function:')

if nargin < 2, sys_id = 100; end
if nargin < 3, comp_id = 200; end

pathname = fileparts(mfilename('fullpath'));
sfun_include_dir = fullfile(pathname,'include');
mavlink_dialect_dir = fileparts(filenames{1});


%% Create message header files

Expand All @@ -47,12 +32,13 @@ function create_sfun_decode(filenames, sys_id, comp_id)
end


%% Create decode header file
%% Create header file

fprintf('Creating s-function header file... ')
fprintf('Writing the decoder s-function header file... ')

header_filename = fullfile(sfun_include_dir,'sfun_decode_mavlink.h');
fid = fopen(header_filename,'w');
header_filename = 'Generated/sfun_decode_mavlink.h';

fid = fopen(header_filename, 'w');

% Write header
fprintf(fid,'%s\n','/*');
Expand All @@ -62,9 +48,9 @@ function create_sfun_decode(filenames, sys_id, comp_id)
fprintf(fid,'%s\n','*/');
fprintf(fid,'%s\n','');

% Include sfun headers
% Include message headers
for i = 1:length(filenames)
fprintf(fid,'%s\n',['#include "' sfun_include_dir filesep 'sfun_mavlink_msg_' mavlink_msg_names{i} '.h"']);
fprintf(fid,'%s\n',['#include "sfun_mavlink_msg_' mavlink_msg_names{i} '.h"']);
end

% Define NFIELDS_OUTPUT_BUS
Expand Down Expand Up @@ -120,8 +106,8 @@ function create_sfun_decode(filenames, sys_id, comp_id)

%% Create cpp file

output_filename = 'sfun_decode_mavlink';
fprintf(['Creating the output file ' output_filename '.cpp ... ']);
output_filename = 'Generated/sfun_decode_mavlink';
fprintf(['Writing source file ' output_filename '.cpp ... ']);

fin = fopen('sfun_decode_mavlink_template.cpp','r');
fout = fopen([output_filename '.cpp'],'w');
Expand Down Expand Up @@ -158,12 +144,13 @@ function create_sfun_decode(filenames, sys_id, comp_id)
fprintf(fout,'%s\n',['#define COMP_ID ' num2str(comp_id)]);

case 3
% include mavlink common header
fprintf(fout,'%s\n',['#include "' mavlink_dialect_dir filesep 'mavlink.h"']);
% include mavlink.h file
mavlink_h = strcat(fileparts(filenames{1}), filesep, 'mavlink.h');
fprintf(fout,'%s\n',['#include "' mavlink_h '"']);

case 4
% include header file
fprintf(fout,'%s\n',['#include "' header_filename '"']);
% include message header file
fprintf(fout,'%s\n', '#include "sfun_decode_mavlink.h"');

case 5
% configure output ports
Expand Down Expand Up @@ -229,12 +216,9 @@ function create_sfun_decode(filenames, sys_id, comp_id)
%% Compile cpp file

disp('Compiling the s-function...')
eval(['mex ' output_filename '.cpp']);

movefile([output_filename '.*'],'sfunctions');
disp('S-function source and compiled files are in the folder ''sfunctions''')

% Add the sfunctions directory to path
addpath(fullfile(pathname,'sfunctions'));
eval('cd Generated')
eval('mex sfun_decode_mavlink.cpp');
eval('cd ..')

disp('***')
disp('Finished, compiled s-function is in the folder ''Generated''')
disp('--')
62 changes: 21 additions & 41 deletions create_sfun_encode.m → Functions/create_sfun_encode.m
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,9 @@ function create_sfun_encode(filename, sys_id, comp_id)
% message
%
% Inputs:
% filename: string containing the full path to the MAVLink message
% header file name. This file must be created as a part of a
% MAVLink dialect, and must reside in the directory structure
% of the corresponding dialect. In other words, the directory
% containing this message file must also contain the
% "mavlink.h" file, and its parent directory must contain the
% other commond mavlink files such as "protocol.h".
%
% NOTE: To ensure compiler independence, provide the full path
% of the message file, and not relative path.
%
% sys_id: MAVLink SYSID to be used for Simulink (default 100)
% comp_id: MAVLink COMPID to be used for Simulink (default 200)
% filename: string containing the MAVLink message header file
% sys_id: MAVLink SYSID to be used for Simulink (default 100)
% comp_id: MAVLink COMPID to be used for Simulink (default 200)
%
% Output:
% This function creates the Simulink bus, the s-function header file, the
Expand All @@ -25,27 +15,21 @@ function create_sfun_encode(filename, sys_id, comp_id)
%(c) Aditya Joshi, November 2017


%% Parse inputs
%% Create header file

disp('--')
disp(['Creating encoder s-function for ' filename ':'])

if nargin < 2, sys_id = 100; end
if nargin < 3, comp_id = 200; end

pathname = fileparts(mfilename('fullpath'));
sfun_include_dir = fullfile(pathname,'include');
mavlink_dialect_dir = fileparts(filename);


%% Create header file

disp(' ')
disp(['*** Running create_sfun_encode for ' filename ':'])

mavlink_msg_name = create_sfun_header(filename);


%% Create cpp file

output_filename = ['sfun_encode_msg_' mavlink_msg_name];
fprintf(['Creating the output file ' output_filename '.cpp ... ']);
output_filename = ['Generated/sfun_encode_msg_' mavlink_msg_name];
fprintf(['Writing source file ' output_filename '.cpp ... ']);

fin = fopen('sfun_encode_msg_template.cpp','r');
fout = fopen([output_filename '.cpp'],'w');
Expand Down Expand Up @@ -83,24 +67,22 @@ function create_sfun_encode(filename, sys_id, comp_id)
fprintf(fout,'%s\n',['#define COMP_ID ' num2str(comp_id)]);

case 4
fprintf(fout,'%s\n',['#include "' mavlink_dialect_dir filesep 'mavlink.h"']);
mavlink_h = strcat(fileparts(filename), filesep, 'mavlink.h');
fprintf(fout,'%s\n',['#include "' mavlink_h '"']);

case 5
fprintf(fout,'%s\n',['#include "' sfun_include_dir filesep 'sfun_mavlink_msg_' mavlink_msg_name '.h"']);
fprintf(fout,'%s\n',['#include "sfun_mavlink_msg_' mavlink_msg_name '.h"']);

case 6
fprintf(fout,'\t%s\n',['ssRegisterTypeFromNamedObject(S, BUS_NAME_' upper(mavlink_msg_name) ', &BusType);']);

case 7
fprintf(fout,'\t%s\n',['ssSetOutputPortWidth(S, 0, ENCODED_LEN_' upper(mavlink_msg_name) ');']);

case 8
fprintf(fout,'\t%s\n',['int_T *busInfo = (int_T *) malloc(2*NFIELDS_BUS_' upper(mavlink_msg_name) '*sizeof(int_T));']);

case 9
case 8
fprintf(fout,'\t%s\n',['encode_businfo_' mavlink_msg_name '(S, busInfo, 0);']);

case 10
case 9
fprintf(fout,'\t%s\n',['encode_vector_' mavlink_msg_name '(uvec, busInfo, yvec, 0);']);
end

Expand All @@ -119,13 +101,11 @@ function create_sfun_encode(filename, sys_id, comp_id)
%% Compile cpp file

disp('Compiling the s-function...')
eval('cd Generated')
output_filename = ['sfun_encode_msg_' mavlink_msg_name];
eval(['mex ' output_filename '.cpp']);
eval('cd ..')

movefile([output_filename '.*'],'sfunctions');
disp('S-function source and compiled files are in the folder ''sfunctions''')

% Add the sfunctions directory to path
addpath(fullfile(pathname,'sfunctions'));

disp('***')
disp('Finished, compiled s-function is in the folder ''Generated''')
disp('--')

51 changes: 15 additions & 36 deletions create_sfun_header.m → Functions/create_sfun_header.m
Original file line number Diff line number Diff line change
@@ -1,56 +1,38 @@
function mavlink_msg_name = create_sfun_header(filename)
% CREATE_SFUN_HEADER: Create the message header files to be included in the
% s-functions used in the Simulink MAVLink library
%
% NOTE: This function is called by other functions create_sfun_encode and
% create_sfun_decode, so the user would typically not have to
% directly use this function.
% CREATE_SFUN_HEADER: Create the message header files for s-functions in Simulink MAVLink
%
% Inputs:
% filename: string containing the full path to the MAVLink message
% header file name. This file must be created as a part of a
% MAVLink dialect, and must reside in the directory structure
% of the corresponding dialect. In other words, the directory
% containing this message file must also contain the
% "mavlink.h" file, and its parent directory must contain the
% other commond mavlink files such as "protocol.h".
% NOTE: To ensure compiler independence, provide the full path
% of the message file, and not relative path.
% mavlink_msg_name: string containing the name of the mavlink message. In the
% standard naming convention, everythin after the "mavlink_msg_"
% in the name of the mavlink header file is considered the
% message name. e.g. if the header file is "mavlink_msg_altitude",
% the message name is "altitude".
% simulink_bus: the Simulink bus object corresponding to this message. This bus
% is created by the function "create_bus_from_mavlink_header"
%
% Output: The function creates the .h header file in the "include"
% directory of the Simulink MAVLink library.
% Output: the function creates the .h header file in the "include" directory.
%
%Part of the Simulink MAVLink package.
%(c) Aditya Joshi, October 2017


%% Parse inputs

pathname = fileparts(mfilename('fullpath'));


%% Create and save bus

disp('**')

fprintf('Creating Simulink bus from message... ');
[simulink_bus, simulink_bus_name, bus_orig_dtypes] = create_bus_from_mavlink_header(filename);
assignin('base',simulink_bus_name,simulink_bus);
disp('done');

eval([simulink_bus_name '= simulink_bus;'])
busfilename = ['buses/bus_' simulink_bus_name '.mat'];
busfilename = ['Generated/bus_' simulink_bus_name '.mat'];
save(busfilename,simulink_bus_name);
disp(['Bus is saved in ' busfilename]);

% Add the buses directory to path
addpath(fullfile(pathname,'buses'));
disp(['done, bus is saved in ' busfilename]);


%% Prepare header file

fprintf('Creating the s-function header file... ');
fprintf('Writing the message s-function header file... ');
mavlink_msg_name = erase(simulink_bus_name,{'mavlink_','_t'});
fileName = fullfile(pathname,'include',['sfun_mavlink_msg_' mavlink_msg_name '.h']);
fileName = ['Generated/sfun_mavlink_msg_' mavlink_msg_name '.h'];
fid = fopen(fileName,'w');


Expand All @@ -65,7 +47,6 @@
% Use full path in the #include statements
fprintf(fid,'%s\n','');
fprintf(fid,'%s\n',['#include "' filename '"']);
fprintf(fid,'%s\n','');
fprintf(fid,'%s\n',['#define BUS_NAME_' upper(mavlink_msg_name) ' "' simulink_bus_name '"']);
fprintf(fid,'%s\n',['#define NFIELDS_BUS_' upper(mavlink_msg_name) ' ' num2str(length(simulink_bus.Elements))]);
fprintf(fid,'%s\n',['#define ENCODED_LEN_' upper(mavlink_msg_name) ' (MAVLINK_NUM_NON_PAYLOAD_BYTES + MAVLINK_MSG_ID_' upper(mavlink_msg_name) '_LEN)']);
Expand Down Expand Up @@ -198,7 +179,5 @@


fclose(fid);
disp('done');
disp(['Header file is saved in ' fileName]);
disp(['done, header file is saved in ' fileName]);

disp('**')
2 changes: 1 addition & 1 deletion datatype_map.csv → Functions/datatype_map.csv
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
mavlink,simulink
px4,simulink
uint8_t,uint8
int8_t,int8
uint16_t,uint16
Expand Down
Loading

0 comments on commit 243f8fc

Please sign in to comment.